@ -23,6 +23,7 @@
# include "access/htup_details.h"
# include "access/htup_details.h"
# include "access/xlog_internal.h"
# include "access/xlog_internal.h"
# include "catalog/pg_authid.h"
# include "catalog/pg_authid.h"
# include "catalog/pg_tablespace_d.h"
# include "catalog/pg_type.h"
# include "catalog/pg_type.h"
# include "funcapi.h"
# include "funcapi.h"
# include "mb/pg_wchar.h"
# include "mb/pg_wchar.h"
@ -31,6 +32,7 @@
# include "storage/fd.h"
# include "storage/fd.h"
# include "utils/builtins.h"
# include "utils/builtins.h"
# include "utils/memutils.h"
# include "utils/memutils.h"
# include "utils/syscache.h"
# include "utils/timestamp.h"
# include "utils/timestamp.h"
typedef struct
typedef struct
@ -520,7 +522,7 @@ pg_ls_dir_1arg(PG_FUNCTION_ARGS)
/* Generic function to return a directory listing of files */
/* Generic function to return a directory listing of files */
static Datum
static Datum
pg_ls_dir_files ( FunctionCallInfo fcinfo , const char * dir )
pg_ls_dir_files ( FunctionCallInfo fcinfo , const char * dir , bool missing_ok )
{
{
FuncCallContext * funcctx ;
FuncCallContext * funcctx ;
struct dirent * de ;
struct dirent * de ;
@ -549,10 +551,18 @@ pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir)
fctx - > dirdesc = AllocateDir ( fctx - > location ) ;
fctx - > dirdesc = AllocateDir ( fctx - > location ) ;
if ( ! fctx - > dirdesc )
if ( ! fctx - > dirdesc )
ereport ( ERROR ,
{
( errcode_for_file_access ( ) ,
if ( missing_ok & & errno = = ENOENT )
errmsg ( " could not open directory \" %s \" : %m " ,
{
fctx - > location ) ) ) ;
MemoryContextSwitchTo ( oldcontext ) ;
SRF_RETURN_DONE ( funcctx ) ;
}
else
ereport ( ERROR ,
( errcode_for_file_access ( ) ,
errmsg ( " could not open directory \" %s \" : %m " ,
fctx - > location ) ) ) ;
}
funcctx - > user_fctx = fctx ;
funcctx - > user_fctx = fctx ;
MemoryContextSwitchTo ( oldcontext ) ;
MemoryContextSwitchTo ( oldcontext ) ;
@ -601,12 +611,50 @@ pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir)
Datum
Datum
pg_ls_logdir ( PG_FUNCTION_ARGS )
pg_ls_logdir ( PG_FUNCTION_ARGS )
{
{
return pg_ls_dir_files ( fcinfo , Log_directory ) ;
return pg_ls_dir_files ( fcinfo , Log_directory , false ) ;
}
}
/* Function to return the list of files in the WAL directory */
/* Function to return the list of files in the WAL directory */
Datum
Datum
pg_ls_waldir ( PG_FUNCTION_ARGS )
pg_ls_waldir ( PG_FUNCTION_ARGS )
{
{
return pg_ls_dir_files ( fcinfo , XLOGDIR ) ;
return pg_ls_dir_files ( fcinfo , XLOGDIR , false ) ;
}
/*
* Generic function to return the list of files in pgsql_tmp
*/
static Datum
pg_ls_tmpdir ( FunctionCallInfo fcinfo , Oid tblspc )
{
char path [ MAXPGPATH ] ;
if ( ! SearchSysCacheExists1 ( TABLESPACEOID , ObjectIdGetDatum ( tblspc ) ) )
ereport ( ERROR ,
( errcode ( ERRCODE_UNDEFINED_OBJECT ) ,
errmsg ( " tablespace with OID %u does not exist " ,
tblspc ) ) ) ;
TempTablespacePath ( path , tblspc ) ;
return pg_ls_dir_files ( fcinfo , path , true ) ;
}
/*
* Function to return the list of temporary files in the pg_default tablespace ' s
* pgsql_tmp directory
*/
Datum
pg_ls_tmpdir_noargs ( PG_FUNCTION_ARGS )
{
return pg_ls_tmpdir ( fcinfo , DEFAULTTABLESPACE_OID ) ;
}
/*
* Function to return the list of temporary files in the specified tablespace ' s
* pgsql_tmp directory
*/
Datum
pg_ls_tmpdir_1arg ( PG_FUNCTION_ARGS )
{
return pg_ls_tmpdir ( fcinfo , PG_GETARG_OID ( 0 ) ) ;
}
}