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(), DEFAULT_ROLE_WRITE_SERVER_FILES))
83  return filename;
84 
85  /* User isn't a member of the default role, so check if it's allowable */
86  if (is_absolute_path(filename))
87  {
88  /* Disallow '/a/b/data/..' */
89  if (path_contains_parent_reference(filename))
90  ereport(ERROR,
91  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
92  errmsg("reference to parent directory (\"..\") not allowed")));
93 
94  /* Allow absolute paths if within DataDir */
95  if (!path_is_prefix_of_path(DataDir, filename))
96  ereport(ERROR,
97  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
98  errmsg("absolute path not allowed")));
99  }
100  else if (!path_is_relative_and_below_cwd(filename))
101  ereport(ERROR,
102  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
103  errmsg("path must be in or below the current directory")));
104 
105  return filename;
106 }
Oid GetUserId(void)
Definition: miscinit.c:448
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:610
bool path_contains_parent_reference(const char *path)
Definition: path.c:376
#define ERROR
Definition: elog.h:43
#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:144
bool is_member_of_role(Oid member, Oid role)
Definition: acl.c:4916
char * text_to_cstring(const text *t)
Definition: varlena.c:205
static char * filename
Definition: pg_dumpall.c:90
int errmsg(const char *fmt,...)
Definition: elog.c:824
char * DataDir
Definition: globals.c:62

◆ pg_file_rename()

Datum pg_file_rename ( PG_FUNCTION_ARGS  )

Definition at line 242 of file adminpack.c.

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

243 {
244  text *file1;
245  text *file2;
246  text *file3;
247  bool result;
248 
250 
251  if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
252  PG_RETURN_NULL();
253 
254  file1 = PG_GETARG_TEXT_PP(0);
255  file2 = PG_GETARG_TEXT_PP(1);
256 
257  if (PG_ARGISNULL(2))
258  file3 = NULL;
259  else
260  file3 = PG_GETARG_TEXT_PP(2);
261 
262  result = pg_file_rename_internal(file1, file2, file3);
263 
264  PG_RETURN_BOOL(result);
265 }
static bool pg_file_rename_internal(text *file1, text *file2, text *file3)
Definition: adminpack.c:306
static void requireSuperuser(void)
Definition: adminpack.c:113
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
Definition: c.h:555
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ pg_file_rename_internal()

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

Definition at line 306 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().

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

◆ pg_file_rename_v1_1()

Datum pg_file_rename_v1_1 ( PG_FUNCTION_ARGS  )

Definition at line 277 of file adminpack.c.

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

278 {
279  text *file1;
280  text *file2;
281  text *file3;
282  bool result;
283 
284  if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
285  PG_RETURN_NULL();
286 
287  file1 = PG_GETARG_TEXT_PP(0);
288  file2 = PG_GETARG_TEXT_PP(1);
289 
290  if (PG_ARGISNULL(2))
291  file3 = NULL;
292  else
293  file3 = PG_GETARG_TEXT_PP(2);
294 
295  result = pg_file_rename_internal(file1, file2, file3);
296 
297  PG_RETURN_BOOL(result);
298 }
static bool pg_file_rename_internal(text *file1, text *file2, text *file3)
Definition: adminpack.c:306
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
Definition: c.h:555
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ pg_file_sync()

Datum pg_file_sync ( PG_FUNCTION_ARGS  )

Definition at line 217 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, and stat.

218 {
219  char *filename;
220  struct stat fst;
221 
223 
224  if (stat(filename, &fst) < 0)
225  ereport(ERROR,
227  errmsg("could not stat file \"%s\": %m", filename)));
228 
229  fsync_fname_ext(filename, S_ISDIR(fst.st_mode), false, ERROR);
230 
231  PG_RETURN_VOID();
232 }
static char * convert_and_check_filename(text *arg)
Definition: adminpack.c:71
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:633
#define stat(a, b)
Definition: win32_port.h:255
#define ereport(elevel,...)
Definition: elog.h:144
#define PG_RETURN_VOID()
Definition: fmgr.h:348
#define S_ISDIR(m)
Definition: win32_port.h:296
static char * filename
Definition: pg_dumpall.c:90
int errmsg(const char *fmt,...)
Definition: elog.c:824
int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel)
Definition: fd.c:3458

◆ pg_file_unlink()

Datum pg_file_unlink ( PG_FUNCTION_ARGS  )

Definition at line 399 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.

400 {
401  char *filename;
402 
404 
406 
407  if (access(filename, W_OK) < 0)
408  {
409  if (errno == ENOENT)
410  PG_RETURN_BOOL(false);
411  else
412  ereport(ERROR,
414  errmsg("file \"%s\" is not accessible: %m", filename)));
415  }
416 
417  if (unlink(filename) < 0)
418  {
421  errmsg("could not unlink file \"%s\": %m", filename)));
422 
423  PG_RETURN_BOOL(false);
424  }
425  PG_RETURN_BOOL(true);
426 }
static char * convert_and_check_filename(text *arg)
Definition: adminpack.c:71
static void requireSuperuser(void)
Definition: adminpack.c:113
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:633
#define WARNING
Definition: elog.h:40
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define ereport(elevel,...)
Definition: elog.h:144
static char * filename
Definition: pg_dumpall.c:90
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ pg_file_unlink_v1_1()

Datum pg_file_unlink_v1_1 ( PG_FUNCTION_ARGS  )

Definition at line 439 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.

440 {
441  char *filename;
442 
444 
445  if (access(filename, W_OK) < 0)
446  {
447  if (errno == ENOENT)
448  PG_RETURN_BOOL(false);
449  else
450  ereport(ERROR,
452  errmsg("file \"%s\" is not accessible: %m", filename)));
453  }
454 
455  if (unlink(filename) < 0)
456  {
459  errmsg("could not unlink file \"%s\": %m", filename)));
460 
461  PG_RETURN_BOOL(false);
462  }
463  PG_RETURN_BOOL(true);
464 }
static char * convert_and_check_filename(text *arg)
Definition: adminpack.c:71
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:633
#define WARNING
Definition: elog.h:40
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
#define ereport(elevel,...)
Definition: elog.h:144
static char * filename
Definition: pg_dumpall.c:90
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ pg_file_write()

Datum pg_file_write ( PG_FUNCTION_ARGS  )

Definition at line 131 of file adminpack.c.

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

132 {
133  text *file = PG_GETARG_TEXT_PP(0);
134  text *data = PG_GETARG_TEXT_PP(1);
135  bool replace = PG_GETARG_BOOL(2);
136  int64 count = 0;
137 
139 
140  count = pg_file_write_internal(file, data, replace);
141 
142  PG_RETURN_INT64(count);
143 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:366
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
static void requireSuperuser(void)
Definition: adminpack.c:113
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
static int64 pg_file_write_internal(text *file, text *data, bool replace)
Definition: adminpack.c:173
Definition: c.h:555

◆ pg_file_write_internal()

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

Definition at line 173 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().

174 {
175  FILE *f;
176  char *filename;
177  int64 count = 0;
178 
179  filename = convert_and_check_filename(file);
180 
181  if (!replace)
182  {
183  struct stat fst;
184 
185  if (stat(filename, &fst) >= 0)
186  ereport(ERROR,
187  (errcode(ERRCODE_DUPLICATE_FILE),
188  errmsg("file \"%s\" exists", filename)));
189 
190  f = AllocateFile(filename, "wb");
191  }
192  else
193  f = AllocateFile(filename, "ab");
194 
195  if (!f)
196  ereport(ERROR,
198  errmsg("could not open file \"%s\" for writing: %m",
199  filename)));
200 
201  count = fwrite(VARDATA_ANY(data), 1, VARSIZE_ANY_EXHDR(data), f);
202  if (count != VARSIZE_ANY_EXHDR(data) || FreeFile(f))
203  ereport(ERROR,
205  errmsg("could not write file \"%s\": %m", filename)));
206 
207  return (count);
208 }
static char * convert_and_check_filename(text *arg)
Definition: adminpack.c:71
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:633
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2320
#define stat(a, b)
Definition: win32_port.h:255
#define ereport(elevel,...)
Definition: elog.h:144
int FreeFile(FILE *file)
Definition: fd.c:2519
static char * filename
Definition: pg_dumpall.c:90
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ pg_file_write_v1_1()

Datum pg_file_write_v1_1 ( PG_FUNCTION_ARGS  )

Definition at line 155 of file adminpack.c.

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

156 {
157  text *file = PG_GETARG_TEXT_PP(0);
158  text *data = PG_GETARG_TEXT_PP(1);
159  bool replace = PG_GETARG_BOOL(2);
160  int64 count = 0;
161 
162  count = pg_file_write_internal(file, data, replace);
163 
164  PG_RETURN_INT64(count);
165 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:366
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
static int64 pg_file_write_internal(text *file, text *data, bool replace)
Definition: adminpack.c:173
Definition: c.h:555

◆ 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 474 of file adminpack.c.

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

475 {
476  if (!superuser())
477  ereport(ERROR,
478  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
479  errmsg("only superuser can list the log directory")));
480 
481  return (pg_logdir_ls_internal(fcinfo));
482 }
int errcode(int sqlerrcode)
Definition: elog.c:610
bool superuser(void)
Definition: superuser.c:46
static Datum pg_logdir_ls_internal(FunctionCallInfo fcinfo)
Definition: adminpack.c:500
#define ERROR
Definition: elog.h:43
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ pg_logdir_ls_internal()

static Datum pg_logdir_ls_internal ( FunctionCallInfo  fcinfo)
static

Definition at line 500 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().

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

◆ pg_logdir_ls_v1_1()

Datum pg_logdir_ls_v1_1 ( PG_FUNCTION_ARGS  )

Definition at line 494 of file adminpack.c.

References pg_logdir_ls_internal().

495 {
496  return (pg_logdir_ls_internal(fcinfo));
497 }
static Datum pg_logdir_ls_internal(FunctionCallInfo fcinfo)
Definition: adminpack.c:500

◆ requireSuperuser()

static void requireSuperuser ( void  )
static

Definition at line 113 of file adminpack.c.

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

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

114 {
115  if (!superuser())
116  ereport(ERROR,
117  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
118  errmsg("only superuser may access generic file functions")));
119 }
int errcode(int sqlerrcode)
Definition: elog.c:610
bool superuser(void)
Definition: superuser.c:46
#define ERROR
Definition: elog.h:43
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 43 of file adminpack.c.