PostgreSQL Source Code  git master
adminpack.c File Reference
#include "postgres.h"
#include <sys/file.h>
#include <sys/stat.h>
#include <unistd.h>
#include "catalog/pg_authid.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "postmaster/syslogger.h"
#include "storage/fd.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/datetime.h"
Include dependency graph for adminpack.c:

Go to the source code of this file.

Functions

 PG_FUNCTION_INFO_V1 (pg_file_write)
 
 PG_FUNCTION_INFO_V1 (pg_file_write_v1_1)
 
 PG_FUNCTION_INFO_V1 (pg_file_sync)
 
 PG_FUNCTION_INFO_V1 (pg_file_rename)
 
 PG_FUNCTION_INFO_V1 (pg_file_rename_v1_1)
 
 PG_FUNCTION_INFO_V1 (pg_file_unlink)
 
 PG_FUNCTION_INFO_V1 (pg_file_unlink_v1_1)
 
 PG_FUNCTION_INFO_V1 (pg_logdir_ls)
 
 PG_FUNCTION_INFO_V1 (pg_logdir_ls_v1_1)
 
static int64 pg_file_write_internal (text *file, text *data, bool replace)
 
static bool pg_file_rename_internal (text *file1, text *file2, text *file3)
 
static Datum pg_logdir_ls_internal (FunctionCallInfo fcinfo)
 
static char * convert_and_check_filename (text *arg)
 
static void requireSuperuser (void)
 
Datum pg_file_write (PG_FUNCTION_ARGS)
 
Datum pg_file_write_v1_1 (PG_FUNCTION_ARGS)
 
Datum pg_file_sync (PG_FUNCTION_ARGS)
 
Datum pg_file_rename (PG_FUNCTION_ARGS)
 
Datum pg_file_rename_v1_1 (PG_FUNCTION_ARGS)
 
Datum pg_file_unlink (PG_FUNCTION_ARGS)
 
Datum pg_file_unlink_v1_1 (PG_FUNCTION_ARGS)
 
Datum pg_logdir_ls (PG_FUNCTION_ARGS)
 
Datum pg_logdir_ls_v1_1 (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 

Function Documentation

◆ convert_and_check_filename()

static char* convert_and_check_filename ( text arg)
static

Definition at line 71 of file adminpack.c.

References canonicalize_path(), DataDir, ereport, errcode(), errmsg(), ERROR, filename, GetUserId(), is_absolute_path, is_member_of_role(), path_contains_parent_reference(), path_is_prefix_of_path(), path_is_relative_and_below_cwd(), and text_to_cstring().

Referenced by pg_file_rename_internal(), pg_file_sync(), pg_file_unlink(), pg_file_unlink_v1_1(), and pg_file_write_internal().

72 {
73  char *filename = text_to_cstring(arg);
74 
75  canonicalize_path(filename); /* filename can change length here */
76 
77  /*
78  * Members of the 'pg_write_server_files' role are allowed to access any
79  * files on the server as the PG user, so no need to do any further checks
80  * here.
81  */
82  if (is_member_of_role(GetUserId(), ROLE_PG_WRITE_SERVER_FILES))
83  return filename;
84 
85  /*
86  * User isn't a member of the pg_write_server_files role, so check if it's
87  * allowable
88  */
89  if (is_absolute_path(filename))
90  {
91  /* Disallow '/a/b/data/..' */
92  if (path_contains_parent_reference(filename))
93  ereport(ERROR,
94  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
95  errmsg("reference to parent directory (\"..\") not allowed")));
96 
97  /* Allow absolute paths if within DataDir */
98  if (!path_is_prefix_of_path(DataDir, filename))
99  ereport(ERROR,
100  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
101  errmsg("absolute path not allowed")));
102  }
103  else if (!path_is_relative_and_below_cwd(filename))
104  ereport(ERROR,
105  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
106  errmsg("path must be in or below the current directory")));
107 
108  return filename;
109 }
Oid GetUserId(void)
Definition: miscinit.c:495
bool path_is_prefix_of_path(const char *path1, const char *path2)
Definition: path.c:438
void canonicalize_path(char *path)
Definition: path.c:254
int errcode(int sqlerrcode)
Definition: elog.c:698
bool path_contains_parent_reference(const char *path)
Definition: path.c:376
#define ERROR
Definition: elog.h:46
#define is_absolute_path(filename)
Definition: port.h:86
bool path_is_relative_and_below_cwd(const char *path)
Definition: path.c:405
#define ereport(elevel,...)
Definition: elog.h:157
bool is_member_of_role(Oid member, Oid role)
Definition: acl.c:4869
char * text_to_cstring(const text *t)
Definition: varlena.c:222
static char * filename
Definition: pg_dumpall.c:92
int errmsg(const char *fmt,...)
Definition: elog.c:909
char * DataDir
Definition: globals.c:65

◆ pg_file_rename()

Datum pg_file_rename ( PG_FUNCTION_ARGS  )

Definition at line 245 of file adminpack.c.

References PG_ARGISNULL, pg_file_rename_internal(), PG_GETARG_TEXT_PP, PG_RETURN_BOOL, PG_RETURN_NULL, and requireSuperuser().

246 {
247  text *file1;
248  text *file2;
249  text *file3;
250  bool result;
251 
253 
254  if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
255  PG_RETURN_NULL();
256 
257  file1 = PG_GETARG_TEXT_PP(0);
258  file2 = PG_GETARG_TEXT_PP(1);
259 
260  if (PG_ARGISNULL(2))
261  file3 = NULL;
262  else
263  file3 = PG_GETARG_TEXT_PP(2);
264 
265  result = pg_file_rename_internal(file1, file2, file3);
266 
267  PG_RETURN_BOOL(result);
268 }
static bool pg_file_rename_internal(text *file1, text *file2, text *file3)
Definition: adminpack.c:309
static void requireSuperuser(void)
Definition: adminpack.c:116
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
Definition: c.h:621
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_file_rename_internal()

static bool pg_file_rename_internal ( text file1,
text file2,
text file3 
)
static

Definition at line 309 of file adminpack.c.

References convert_and_check_filename(), ereport, errcode(), errcode_for_file_access(), errmsg(), ERROR, and WARNING.

Referenced by pg_file_rename(), and pg_file_rename_v1_1().

310 {
311  char *fn1,
312  *fn2,
313  *fn3;
314  int rc;
315 
316  fn1 = convert_and_check_filename(file1);
317  fn2 = convert_and_check_filename(file2);
318 
319  if (file3 == NULL)
320  fn3 = NULL;
321  else
322  fn3 = convert_and_check_filename(file3);
323 
324  if (access(fn1, W_OK) < 0)
325  {
328  errmsg("file \"%s\" is not accessible: %m", fn1)));
329 
330  return false;
331  }
332 
333  if (fn3 && access(fn2, W_OK) < 0)
334  {
337  errmsg("file \"%s\" is not accessible: %m", fn2)));
338 
339  return false;
340  }
341 
342  rc = access(fn3 ? fn3 : fn2, W_OK);
343  if (rc >= 0 || errno != ENOENT)
344  {
345  ereport(ERROR,
346  (errcode(ERRCODE_DUPLICATE_FILE),
347  errmsg("cannot rename to target file \"%s\"",
348  fn3 ? fn3 : fn2)));
349  }
350 
351  if (fn3)
352  {
353  if (rename(fn2, fn3) != 0)
354  {
355  ereport(ERROR,
357  errmsg("could not rename \"%s\" to \"%s\": %m",
358  fn2, fn3)));
359  }
360  if (rename(fn1, fn2) != 0)
361  {
364  errmsg("could not rename \"%s\" to \"%s\": %m",
365  fn1, fn2)));
366 
367  if (rename(fn3, fn2) != 0)
368  {
369  ereport(ERROR,
371  errmsg("could not rename \"%s\" back to \"%s\": %m",
372  fn3, fn2)));
373  }
374  else
375  {
376  ereport(ERROR,
377  (errcode(ERRCODE_UNDEFINED_FILE),
378  errmsg("renaming \"%s\" to \"%s\" was reverted",
379  fn2, fn3)));
380  }
381  }
382  }
383  else if (rename(fn1, fn2) != 0)
384  {
385  ereport(ERROR,
387  errmsg("could not rename \"%s\" to \"%s\": %m", fn1, fn2)));
388  }
389 
390  return true;
391 }
static char * convert_and_check_filename(text *arg)
Definition: adminpack.c:71
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
int errcode_for_file_access(void)
Definition: elog.c:721
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pg_file_rename_v1_1()

Datum pg_file_rename_v1_1 ( PG_FUNCTION_ARGS  )

Definition at line 280 of file adminpack.c.

References PG_ARGISNULL, pg_file_rename_internal(), PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and PG_RETURN_NULL.

281 {
282  text *file1;
283  text *file2;
284  text *file3;
285  bool result;
286 
287  if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
288  PG_RETURN_NULL();
289 
290  file1 = PG_GETARG_TEXT_PP(0);
291  file2 = PG_GETARG_TEXT_PP(1);
292 
293  if (PG_ARGISNULL(2))
294  file3 = NULL;
295  else
296  file3 = PG_GETARG_TEXT_PP(2);
297 
298  result = pg_file_rename_internal(file1, file2, file3);
299 
300  PG_RETURN_BOOL(result);
301 }
static bool pg_file_rename_internal(text *file1, text *file2, text *file3)
Definition: adminpack.c:309
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
Definition: c.h:621
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_file_sync()

Datum pg_file_sync ( PG_FUNCTION_ARGS  )

Definition at line 220 of file adminpack.c.

References convert_and_check_filename(), ereport, errcode_for_file_access(), errmsg(), ERROR, filename, fsync_fname_ext(), PG_GETARG_TEXT_PP, PG_RETURN_VOID, S_ISDIR, stat::st_mode, and stat.

221 {
222  char *filename;
223  struct stat fst;
224 
226 
227  if (stat(filename, &fst) < 0)
228  ereport(ERROR,
230  errmsg("could not stat file \"%s\": %m", filename)));
231 
232  fsync_fname_ext(filename, S_ISDIR(fst.st_mode), false, ERROR);
233 
234  PG_RETURN_VOID();
235 }
static char * convert_and_check_filename(text *arg)
Definition: adminpack.c:71
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define ERROR
Definition: elog.h:46
int errcode_for_file_access(void)
Definition: elog.c:721
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define S_ISDIR(m)
Definition: win32_port.h:324
static char * filename
Definition: pg_dumpall.c:92
int errmsg(const char *fmt,...)
Definition: elog.c:909
int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel)
Definition: fd.c:3685
#define stat
Definition: win32_port.h:283

◆ pg_file_unlink()

Datum pg_file_unlink ( PG_FUNCTION_ARGS  )

Definition at line 402 of file adminpack.c.

References convert_and_check_filename(), ereport, errcode_for_file_access(), errmsg(), ERROR, filename, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, requireSuperuser(), and WARNING.

403 {
404  char *filename;
405 
407 
409 
410  if (access(filename, W_OK) < 0)
411  {
412  if (errno == ENOENT)
413  PG_RETURN_BOOL(false);
414  else
415  ereport(ERROR,
417  errmsg("file \"%s\" is not accessible: %m", filename)));
418  }
419 
420  if (unlink(filename) < 0)
421  {
424  errmsg("could not unlink file \"%s\": %m", filename)));
425 
426  PG_RETURN_BOOL(false);
427  }
428  PG_RETURN_BOOL(true);
429 }
static char * convert_and_check_filename(text *arg)
Definition: adminpack.c:71
static void requireSuperuser(void)
Definition: adminpack.c:116
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define ERROR
Definition: elog.h:46
int errcode_for_file_access(void)
Definition: elog.c:721
#define WARNING
Definition: elog.h:40
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define ereport(elevel,...)
Definition: elog.h:157
static char * filename
Definition: pg_dumpall.c:92
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pg_file_unlink_v1_1()

Datum pg_file_unlink_v1_1 ( PG_FUNCTION_ARGS  )

Definition at line 442 of file adminpack.c.

References convert_and_check_filename(), ereport, errcode_for_file_access(), errmsg(), ERROR, filename, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and WARNING.

443 {
444  char *filename;
445 
447 
448  if (access(filename, W_OK) < 0)
449  {
450  if (errno == ENOENT)
451  PG_RETURN_BOOL(false);
452  else
453  ereport(ERROR,
455  errmsg("file \"%s\" is not accessible: %m", filename)));
456  }
457 
458  if (unlink(filename) < 0)
459  {
462  errmsg("could not unlink file \"%s\": %m", filename)));
463 
464  PG_RETURN_BOOL(false);
465  }
466  PG_RETURN_BOOL(true);
467 }
static char * convert_and_check_filename(text *arg)
Definition: adminpack.c:71
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define ERROR
Definition: elog.h:46
int errcode_for_file_access(void)
Definition: elog.c:721
#define WARNING
Definition: elog.h:40
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define ereport(elevel,...)
Definition: elog.h:157
static char * filename
Definition: pg_dumpall.c:92
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pg_file_write()

Datum pg_file_write ( PG_FUNCTION_ARGS  )

Definition at line 134 of file adminpack.c.

References pg_file_write_internal(), PG_GETARG_BOOL, PG_GETARG_TEXT_PP, PG_RETURN_INT64, and requireSuperuser().

135 {
136  text *file = PG_GETARG_TEXT_PP(0);
137  text *data = PG_GETARG_TEXT_PP(1);
138  bool replace = PG_GETARG_BOOL(2);
139  int64 count = 0;
140 
142 
143  count = pg_file_write_internal(file, data, replace);
144 
145  PG_RETURN_INT64(count);
146 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
static void requireSuperuser(void)
Definition: adminpack.c:116
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
static int64 pg_file_write_internal(text *file, text *data, bool replace)
Definition: adminpack.c:176
Definition: c.h:621

◆ pg_file_write_internal()

static int64 pg_file_write_internal ( text file,
text data,
bool  replace 
)
static

Definition at line 176 of file adminpack.c.

References AllocateFile(), convert_and_check_filename(), ereport, errcode(), errcode_for_file_access(), errmsg(), ERROR, filename, FreeFile(), stat, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by pg_file_write(), and pg_file_write_v1_1().

177 {
178  FILE *f;
179  char *filename;
180  int64 count = 0;
181 
182  filename = convert_and_check_filename(file);
183 
184  if (!replace)
185  {
186  struct stat fst;
187 
188  if (stat(filename, &fst) >= 0)
189  ereport(ERROR,
190  (errcode(ERRCODE_DUPLICATE_FILE),
191  errmsg("file \"%s\" exists", filename)));
192 
193  f = AllocateFile(filename, "wb");
194  }
195  else
196  f = AllocateFile(filename, "ab");
197 
198  if (!f)
199  ereport(ERROR,
201  errmsg("could not open file \"%s\" for writing: %m",
202  filename)));
203 
204  count = fwrite(VARDATA_ANY(data), 1, VARSIZE_ANY_EXHDR(data), f);
205  if (count != VARSIZE_ANY_EXHDR(data) || FreeFile(f))
206  ereport(ERROR,
208  errmsg("could not write file \"%s\": %m", filename)));
209 
210  return (count);
211 }
static char * convert_and_check_filename(text *arg)
Definition: adminpack.c:71
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
int errcode_for_file_access(void)
Definition: elog.c:721
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2460
#define ereport(elevel,...)
Definition: elog.h:157
int FreeFile(FILE *file)
Definition: fd.c:2659
static char * filename
Definition: pg_dumpall.c:92
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define stat
Definition: win32_port.h:283

◆ pg_file_write_v1_1()

Datum pg_file_write_v1_1 ( PG_FUNCTION_ARGS  )

Definition at line 158 of file adminpack.c.

References pg_file_write_internal(), PG_GETARG_BOOL, PG_GETARG_TEXT_PP, and PG_RETURN_INT64.

159 {
160  text *file = PG_GETARG_TEXT_PP(0);
161  text *data = PG_GETARG_TEXT_PP(1);
162  bool replace = PG_GETARG_BOOL(2);
163  int64 count = 0;
164 
165  count = pg_file_write_internal(file, data, replace);
166 
167  PG_RETURN_INT64(count);
168 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
static int64 pg_file_write_internal(text *file, text *data, bool replace)
Definition: adminpack.c:176
Definition: c.h:621

◆ PG_FUNCTION_INFO_V1() [1/9]

PG_FUNCTION_INFO_V1 ( pg_file_write  )

◆ PG_FUNCTION_INFO_V1() [2/9]

PG_FUNCTION_INFO_V1 ( pg_file_write_v1_1  )

◆ PG_FUNCTION_INFO_V1() [3/9]

PG_FUNCTION_INFO_V1 ( pg_file_sync  )

◆ PG_FUNCTION_INFO_V1() [4/9]

PG_FUNCTION_INFO_V1 ( pg_file_rename  )

◆ PG_FUNCTION_INFO_V1() [5/9]

PG_FUNCTION_INFO_V1 ( pg_file_rename_v1_1  )

◆ PG_FUNCTION_INFO_V1() [6/9]

PG_FUNCTION_INFO_V1 ( pg_file_unlink  )

◆ PG_FUNCTION_INFO_V1() [7/9]

PG_FUNCTION_INFO_V1 ( pg_file_unlink_v1_1  )

◆ PG_FUNCTION_INFO_V1() [8/9]

PG_FUNCTION_INFO_V1 ( pg_logdir_ls  )

◆ PG_FUNCTION_INFO_V1() [9/9]

PG_FUNCTION_INFO_V1 ( pg_logdir_ls_v1_1  )

◆ pg_logdir_ls()

Datum pg_logdir_ls ( PG_FUNCTION_ARGS  )

Definition at line 477 of file adminpack.c.

References ereport, errcode(), errmsg(), ERROR, pg_logdir_ls_internal(), and superuser().

478 {
479  if (!superuser())
480  ereport(ERROR,
481  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
482  errmsg("only superuser can list the log directory")));
483 
484  return (pg_logdir_ls_internal(fcinfo));
485 }
int errcode(int sqlerrcode)
Definition: elog.c:698
bool superuser(void)
Definition: superuser.c:46
static Datum pg_logdir_ls_internal(FunctionCallInfo fcinfo)
Definition: adminpack.c:503
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pg_logdir_ls_internal()

static Datum pg_logdir_ls_internal ( FunctionCallInfo  fcinfo)
static

Definition at line 503 of file adminpack.c.

References AllocateDir(), ReturnSetInfo::allowedModes, BuildTupleFromCStrings(), CreateTemplateTupleDesc(), DecodeDateTime(), ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, ereport, errcode(), errmsg(), ERROR, FreeDir(), IsA, Log_directory, Log_filename, MAXDATEFIELDS, MAXDATELEN, MemoryContextSwitchTo(), ParseDateTime(), psprintf(), ReadDir(), FunctionCallInfoBaseData::resultinfo, ReturnSetInfo::returnMode, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, SFRM_Materialize_Random, TupleDescGetAttInMetadata(), TupleDescInitEntry(), tuplestore_begin_heap(), tuplestore_puttuple(), values, and work_mem.

Referenced by pg_logdir_ls(), and pg_logdir_ls_v1_1().

504 {
505  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
506  bool randomAccess;
507  TupleDesc tupdesc;
508  Tuplestorestate *tupstore;
509  AttInMetadata *attinmeta;
510  DIR *dirdesc;
511  struct dirent *de;
512  MemoryContext oldcontext;
513 
514  if (strcmp(Log_filename, "postgresql-%Y-%m-%d_%H%M%S.log") != 0)
515  ereport(ERROR,
516  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
517  errmsg("the log_filename parameter must equal 'postgresql-%%Y-%%m-%%d_%%H%%M%%S.log'")));
518 
519  /* check to see if caller supports us returning a tuplestore */
520  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
521  ereport(ERROR,
522  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
523  errmsg("set-valued function called in context that cannot accept a set")));
524  if (!(rsinfo->allowedModes & SFRM_Materialize))
525  ereport(ERROR,
526  (errcode(ERRCODE_SYNTAX_ERROR),
527  errmsg("materialize mode required, but it is not allowed in this context")));
528 
529  /* The tupdesc and tuplestore must be created in ecxt_per_query_memory */
531 
532  tupdesc = CreateTemplateTupleDesc(2);
533  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "starttime",
534  TIMESTAMPOID, -1, 0);
535  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "filename",
536  TEXTOID, -1, 0);
537 
538  randomAccess = (rsinfo->allowedModes & SFRM_Materialize_Random) != 0;
539  tupstore = tuplestore_begin_heap(randomAccess, false, work_mem);
540  rsinfo->returnMode = SFRM_Materialize;
541  rsinfo->setResult = tupstore;
542  rsinfo->setDesc = tupdesc;
543 
544  MemoryContextSwitchTo(oldcontext);
545 
546  attinmeta = TupleDescGetAttInMetadata(tupdesc);
547 
548  dirdesc = AllocateDir(Log_directory);
549  while ((de = ReadDir(dirdesc, Log_directory)) != NULL)
550  {
551  char *values[2];
552  HeapTuple tuple;
553  char timestampbuf[32];
554  char *field[MAXDATEFIELDS];
555  char lowstr[MAXDATELEN + 1];
556  int dtype;
557  int nf,
558  ftype[MAXDATEFIELDS];
559  fsec_t fsec;
560  int tz = 0;
561  struct pg_tm date;
562 
563  /*
564  * Default format: postgresql-YYYY-MM-DD_HHMMSS.log
565  */
566  if (strlen(de->d_name) != 32
567  || strncmp(de->d_name, "postgresql-", 11) != 0
568  || de->d_name[21] != '_'
569  || strcmp(de->d_name + 28, ".log") != 0)
570  continue;
571 
572  /* extract timestamp portion of filename */
573  strcpy(timestampbuf, de->d_name + 11);
574  timestampbuf[17] = '\0';
575 
576  /* parse and decode expected timestamp to verify it's OK format */
577  if (ParseDateTime(timestampbuf, lowstr, MAXDATELEN, field, ftype, MAXDATEFIELDS, &nf))
578  continue;
579 
580  if (DecodeDateTime(field, ftype, nf, &dtype, &date, &fsec, &tz))
581  continue;
582 
583  /* Seems the timestamp is OK; prepare and return tuple */
584 
585  values[0] = timestampbuf;
586  values[1] = psprintf("%s/%s", Log_directory, de->d_name);
587 
588  tuple = BuildTupleFromCStrings(attinmeta, values);
589 
590  tuplestore_puttuple(tupstore, tuple);
591  }
592 
593  FreeDir(dirdesc);
594  return (Datum) 0;
595 }
#define MAXDATELEN
Definition: datetime.h:201
#define IsA(nodeptr, _type_)
Definition: nodes.h:589
char * Log_directory
Definition: syslogger.c:74
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:698
long date
Definition: pgtypes_date.h:9
Definition: pgtime.h:32
Definition: dirent.h:9
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2146
int DecodeDateTime(char **field, int *ftype, int nf, int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:803
Definition: dirent.c:25
#define ERROR
Definition: elog.h:46
fmNodePtr resultinfo
Definition: fmgr.h:89
void tuplestore_puttuple(Tuplestorestate *state, HeapTuple tuple)
Definition: tuplestore.c:730
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2721
int32 fsec_t
Definition: timestamp.h:41
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:583
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
uintptr_t Datum
Definition: postgres.h:411
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2097
int work_mem
Definition: globals.c:124
#define ereport(elevel,...)
Definition: elog.h:157
int allowedModes
Definition: execnodes.h:306
SetFunctionReturnMode returnMode
Definition: execnodes.h:308
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2787
#define MAXDATEFIELDS
Definition: datetime.h:203
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:234
char * Log_filename
Definition: syslogger.c:75
Tuplestorestate * setResult
Definition: execnodes.h:311
static Datum values[MAXATTR]
Definition: bootstrap.c:156
ExprContext * econtext
Definition: execnodes.h:304
TupleDesc setDesc
Definition: execnodes.h:312
int errmsg(const char *fmt,...)
Definition: elog.c:909
int ParseDateTime(const char *timestr, char *workbuf, size_t buflen, char **field, int *ftype, int maxfields, int *numfields)
Definition: datetime.c:582
int16 AttrNumber
Definition: attnum.h:21
int FreeDir(DIR *dir)
Definition: fd.c:2839

◆ pg_logdir_ls_v1_1()

Datum pg_logdir_ls_v1_1 ( PG_FUNCTION_ARGS  )

Definition at line 497 of file adminpack.c.

References pg_logdir_ls_internal().

498 {
499  return (pg_logdir_ls_internal(fcinfo));
500 }
static Datum pg_logdir_ls_internal(FunctionCallInfo fcinfo)
Definition: adminpack.c:503

◆ requireSuperuser()

static void requireSuperuser ( void  )
static

Definition at line 116 of file adminpack.c.

References ereport, errcode(), errmsg(), ERROR, and superuser().

Referenced by pg_file_rename(), pg_file_unlink(), and pg_file_write().

117 {
118  if (!superuser())
119  ereport(ERROR,
120  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
121  errmsg("only superuser may access generic file functions")));
122 }
int errcode(int sqlerrcode)
Definition: elog.c:698
bool superuser(void)
Definition: superuser.c:46
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 43 of file adminpack.c.