PostgreSQL Source Code  git master
genfile.c File Reference
#include "postgres.h"
#include <sys/file.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include "access/htup_details.h"
#include "access/xlog_internal.h"
#include "catalog/pg_authid.h"
#include "catalog/pg_tablespace_d.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "postmaster/syslogger.h"
#include "storage/fd.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
#include "utils/syscache.h"
#include "utils/timestamp.h"
Include dependency graph for genfile.c:

Go to the source code of this file.

Macros

#define MIN_READ_SIZE   4096
 

Functions

static char * convert_and_check_filename (text *arg)
 
static bytearead_binary_file (const char *filename, int64 seek_offset, int64 bytes_to_read, bool missing_ok)
 
static textread_text_file (const char *filename, int64 seek_offset, int64 bytes_to_read, bool missing_ok)
 
Datum pg_read_file (PG_FUNCTION_ARGS)
 
Datum pg_read_file_v2 (PG_FUNCTION_ARGS)
 
Datum pg_read_binary_file (PG_FUNCTION_ARGS)
 
Datum pg_read_file_off_len (PG_FUNCTION_ARGS)
 
Datum pg_read_file_all (PG_FUNCTION_ARGS)
 
Datum pg_read_binary_file_off_len (PG_FUNCTION_ARGS)
 
Datum pg_read_binary_file_all (PG_FUNCTION_ARGS)
 
Datum pg_stat_file (PG_FUNCTION_ARGS)
 
Datum pg_stat_file_1arg (PG_FUNCTION_ARGS)
 
Datum pg_ls_dir (PG_FUNCTION_ARGS)
 
Datum pg_ls_dir_1arg (PG_FUNCTION_ARGS)
 
static Datum pg_ls_dir_files (FunctionCallInfo fcinfo, const char *dir, bool missing_ok)
 
Datum pg_ls_logdir (PG_FUNCTION_ARGS)
 
Datum pg_ls_waldir (PG_FUNCTION_ARGS)
 
static Datum pg_ls_tmpdir (FunctionCallInfo fcinfo, Oid tblspc)
 
Datum pg_ls_tmpdir_noargs (PG_FUNCTION_ARGS)
 
Datum pg_ls_tmpdir_1arg (PG_FUNCTION_ARGS)
 
Datum pg_ls_archive_statusdir (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ MIN_READ_SIZE

#define MIN_READ_SIZE   4096

Referenced by read_binary_file().

Function Documentation

◆ convert_and_check_filename()

static char* convert_and_check_filename ( text arg)
static

Definition at line 53 of file genfile.c.

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

Referenced by pg_ls_dir(), pg_read_binary_file(), pg_read_file(), pg_read_file_v2(), and pg_stat_file().

54 {
55  char *filename;
56 
57  filename = text_to_cstring(arg);
58  canonicalize_path(filename); /* filename can change length here */
59 
60  /*
61  * Members of the 'pg_read_server_files' role are allowed to access any
62  * files on the server as the PG user, so no need to do any further checks
63  * here.
64  */
65  if (is_member_of_role(GetUserId(), ROLE_PG_READ_SERVER_FILES))
66  return filename;
67 
68  /*
69  * User isn't a member of the pg_read_server_files role, so check if it's
70  * allowable
71  */
72  if (is_absolute_path(filename))
73  {
74  /* Disallow '/a/b/data/..' */
75  if (path_contains_parent_reference(filename))
76  ereport(ERROR,
77  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
78  errmsg("reference to parent directory (\"..\") not allowed")));
79 
80  /*
81  * Allow absolute paths if within DataDir or Log_directory, even
82  * though Log_directory might be outside DataDir.
83  */
84  if (!path_is_prefix_of_path(DataDir, filename) &&
87  ereport(ERROR,
88  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
89  errmsg("absolute path not allowed")));
90  }
91  else if (!path_is_relative_and_below_cwd(filename))
92  ereport(ERROR,
93  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
94  errmsg("path must be in or below the current directory")));
95 
96  return filename;
97 }
char * Log_directory
Definition: syslogger.c:73
Oid GetUserId(void)
Definition: miscinit.c:478
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:223
static char * filename
Definition: pg_dumpall.c:91
int errmsg(const char *fmt,...)
Definition: elog.c:909
char * DataDir
Definition: globals.c:65

◆ pg_ls_archive_statusdir()

Datum pg_ls_archive_statusdir ( PG_FUNCTION_ARGS  )

Definition at line 720 of file genfile.c.

References pg_ls_dir_files(), and XLOGDIR.

721 {
722  return pg_ls_dir_files(fcinfo, XLOGDIR "/archive_status", true);
723 }
static Datum pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir, bool missing_ok)
Definition: genfile.c:577
#define XLOGDIR

◆ pg_ls_dir()

Datum pg_ls_dir ( PG_FUNCTION_ARGS  )

Definition at line 480 of file genfile.c.

References AllocateDir(), ReturnSetInfo::allowedModes, convert_and_check_filename(), CreateTemplateTupleDesc(), CStringGetTextDatum, ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, ereport, errcode(), errmsg(), ERROR, FreeDir(), IsA, MemoryContextSwitchTo(), PG_ARGISNULL, PG_GETARG_BOOL, PG_GETARG_TEXT_PP, PG_NARGS, ReadDir(), ReturnSetInfo::returnMode, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, SFRM_Materialize_Random, TupleDescInitEntry(), tuplestore_begin_heap(), tuplestore_putvalues(), values, and work_mem.

Referenced by pg_ls_dir_1arg().

481 {
482  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
483  char *location;
484  bool missing_ok = false;
485  bool include_dot_dirs = false;
486  bool randomAccess;
487  TupleDesc tupdesc;
488  Tuplestorestate *tupstore;
489  DIR *dirdesc;
490  struct dirent *de;
491  MemoryContext oldcontext;
492 
494 
495  /* check the optional arguments */
496  if (PG_NARGS() == 3)
497  {
498  if (!PG_ARGISNULL(1))
499  missing_ok = PG_GETARG_BOOL(1);
500  if (!PG_ARGISNULL(2))
501  include_dot_dirs = PG_GETARG_BOOL(2);
502  }
503 
504  /* check to see if caller supports us returning a tuplestore */
505  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
506  ereport(ERROR,
507  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
508  errmsg("set-valued function called in context that cannot accept a set")));
509  if (!(rsinfo->allowedModes & SFRM_Materialize))
510  ereport(ERROR,
511  (errcode(ERRCODE_SYNTAX_ERROR),
512  errmsg("materialize mode required, but it is not allowed in this context")));
513 
514  /* The tupdesc and tuplestore must be created in ecxt_per_query_memory */
516 
517  tupdesc = CreateTemplateTupleDesc(1);
518  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "pg_ls_dir", TEXTOID, -1, 0);
519 
520  randomAccess = (rsinfo->allowedModes & SFRM_Materialize_Random) != 0;
521  tupstore = tuplestore_begin_heap(randomAccess, false, work_mem);
522  rsinfo->returnMode = SFRM_Materialize;
523  rsinfo->setResult = tupstore;
524  rsinfo->setDesc = tupdesc;
525 
526  MemoryContextSwitchTo(oldcontext);
527 
528  dirdesc = AllocateDir(location);
529  if (!dirdesc)
530  {
531  /* Return empty tuplestore if appropriate */
532  if (missing_ok && errno == ENOENT)
533  return (Datum) 0;
534  /* Otherwise, we can let ReadDir() throw the error */
535  }
536 
537  while ((de = ReadDir(dirdesc, location)) != NULL)
538  {
539  Datum values[1];
540  bool nulls[1];
541 
542  if (!include_dot_dirs &&
543  (strcmp(de->d_name, ".") == 0 ||
544  strcmp(de->d_name, "..") == 0))
545  continue;
546 
547  values[0] = CStringGetTextDatum(de->d_name);
548  nulls[0] = false;
549 
550  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
551  }
552 
553  FreeDir(dirdesc);
554  return (Datum) 0;
555 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
static char * convert_and_check_filename(text *arg)
Definition: genfile.c:53
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:698
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
Definition: dirent.h:9
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
Definition: dirent.c:25
#define ERROR
Definition: elog.h:46
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2634
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:580
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
uintptr_t Datum
Definition: postgres.h:411
int work_mem
Definition: globals.c:124
#define ereport(elevel,...)
Definition: elog.h:157
int allowedModes
Definition: execnodes.h:305
SetFunctionReturnMode returnMode
Definition: execnodes.h:307
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2700
#define PG_NARGS()
Definition: fmgr.h:203
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:233
Tuplestorestate * setResult
Definition: execnodes.h:310
static Datum values[MAXATTR]
Definition: bootstrap.c:166
ExprContext * econtext
Definition: execnodes.h:303
TupleDesc setDesc
Definition: execnodes.h:311
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define CStringGetTextDatum(s)
Definition: builtins.h:82
int16 AttrNumber
Definition: attnum.h:21
int FreeDir(DIR *dir)
Definition: fd.c:2752

◆ pg_ls_dir_1arg()

Datum pg_ls_dir_1arg ( PG_FUNCTION_ARGS  )

Definition at line 565 of file genfile.c.

References pg_ls_dir().

566 {
567  return pg_ls_dir(fcinfo);
568 }
Datum pg_ls_dir(PG_FUNCTION_ARGS)
Definition: genfile.c:480

◆ pg_ls_dir_files()

static Datum pg_ls_dir_files ( FunctionCallInfo  fcinfo,
const char *  dir,
bool  missing_ok 
)
static

Definition at line 577 of file genfile.c.

References AllocateDir(), ReturnSetInfo::allowedModes, CStringGetTextDatum, ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, elog, ereport, errcode(), errcode_for_file_access(), errmsg(), ERROR, FreeDir(), get_call_result_type(), Int64GetDatum(), IsA, MAXPGPATH, MemoryContextSwitchTo(), ReadDir(), FunctionCallInfoBaseData::resultinfo, ReturnSetInfo::returnMode, S_ISREG, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, SFRM_Materialize_Random, snprintf, stat::st_mode, stat::st_mtime, stat::st_size, stat, time_t_to_timestamptz(), TimestampTzGetDatum, tuplestore_begin_heap(), tuplestore_putvalues(), TYPEFUNC_COMPOSITE, values, and work_mem.

Referenced by pg_ls_archive_statusdir(), pg_ls_logdir(), pg_ls_tmpdir(), and pg_ls_waldir().

578 {
579  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
580  bool randomAccess;
581  TupleDesc tupdesc;
582  Tuplestorestate *tupstore;
583  DIR *dirdesc;
584  struct dirent *de;
585  MemoryContext oldcontext;
586 
587  /* check to see if caller supports us returning a tuplestore */
588  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
589  ereport(ERROR,
590  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
591  errmsg("set-valued function called in context that cannot accept a set")));
592  if (!(rsinfo->allowedModes & SFRM_Materialize))
593  ereport(ERROR,
594  (errcode(ERRCODE_SYNTAX_ERROR),
595  errmsg("materialize mode required, but it is not allowed in this context")));
596 
597  /* The tupdesc and tuplestore must be created in ecxt_per_query_memory */
599 
600  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
601  elog(ERROR, "return type must be a row type");
602 
603  randomAccess = (rsinfo->allowedModes & SFRM_Materialize_Random) != 0;
604  tupstore = tuplestore_begin_heap(randomAccess, false, work_mem);
605  rsinfo->returnMode = SFRM_Materialize;
606  rsinfo->setResult = tupstore;
607  rsinfo->setDesc = tupdesc;
608 
609  MemoryContextSwitchTo(oldcontext);
610 
611  /*
612  * Now walk the directory. Note that we must do this within a single SRF
613  * call, not leave the directory open across multiple calls, since we
614  * can't count on the SRF being run to completion.
615  */
616  dirdesc = AllocateDir(dir);
617  if (!dirdesc)
618  {
619  /* Return empty tuplestore if appropriate */
620  if (missing_ok && errno == ENOENT)
621  return (Datum) 0;
622  /* Otherwise, we can let ReadDir() throw the error */
623  }
624 
625  while ((de = ReadDir(dirdesc, dir)) != NULL)
626  {
627  Datum values[3];
628  bool nulls[3];
629  char path[MAXPGPATH * 2];
630  struct stat attrib;
631 
632  /* Skip hidden files */
633  if (de->d_name[0] == '.')
634  continue;
635 
636  /* Get the file info */
637  snprintf(path, sizeof(path), "%s/%s", dir, de->d_name);
638  if (stat(path, &attrib) < 0)
639  {
640  /* Ignore concurrently-deleted files, else complain */
641  if (errno == ENOENT)
642  continue;
643  ereport(ERROR,
645  errmsg("could not stat file \"%s\": %m", path)));
646  }
647 
648  /* Ignore anything but regular files */
649  if (!S_ISREG(attrib.st_mode))
650  continue;
651 
652  values[0] = CStringGetTextDatum(de->d_name);
653  values[1] = Int64GetDatum((int64) attrib.st_size);
654  values[2] = TimestampTzGetDatum(time_t_to_timestamptz(attrib.st_mtime));
655  memset(nulls, 0, sizeof(nulls));
656 
657  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
658  }
659 
660  FreeDir(dirdesc);
661  return (Datum) 0;
662 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:207
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:698
Definition: dirent.h:9
Definition: dirent.c:25
#define ERROR
Definition: elog.h:46
fmNodePtr resultinfo
Definition: fmgr.h:89
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
#define MAXPGPATH
int errcode_for_file_access(void)
Definition: elog.c:721
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2634
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1700
#define S_ISREG(m)
Definition: win32_port.h:319
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
uintptr_t Datum
Definition: postgres.h:411
int work_mem
Definition: globals.c:124
TimestampTz time_t_to_timestamptz(pg_time_t tm)
Definition: timestamp.c:1732
#define ereport(elevel,...)
Definition: elog.h:157
int allowedModes
Definition: execnodes.h:305
SetFunctionReturnMode returnMode
Definition: execnodes.h:307
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2700
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:233
Tuplestorestate * setResult
Definition: execnodes.h:310
static Datum values[MAXATTR]
Definition: bootstrap.c:166
ExprContext * econtext
Definition: execnodes.h:303
TupleDesc setDesc
Definition: execnodes.h:311
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define CStringGetTextDatum(s)
Definition: builtins.h:82
#define snprintf
Definition: port.h:216
int FreeDir(DIR *dir)
Definition: fd.c:2752
#define stat
Definition: win32_port.h:275

◆ pg_ls_logdir()

Datum pg_ls_logdir ( PG_FUNCTION_ARGS  )

Definition at line 666 of file genfile.c.

References Log_directory, and pg_ls_dir_files().

667 {
668  return pg_ls_dir_files(fcinfo, Log_directory, false);
669 }
char * Log_directory
Definition: syslogger.c:73
static Datum pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir, bool missing_ok)
Definition: genfile.c:577

◆ pg_ls_tmpdir()

static Datum pg_ls_tmpdir ( FunctionCallInfo  fcinfo,
Oid  tblspc 
)
static

Definition at line 682 of file genfile.c.

References ereport, errcode(), errmsg(), ERROR, MAXPGPATH, ObjectIdGetDatum, pg_ls_dir_files(), SearchSysCacheExists1, TABLESPACEOID, and TempTablespacePath().

Referenced by pg_ls_tmpdir_1arg(), and pg_ls_tmpdir_noargs().

683 {
684  char path[MAXPGPATH];
685 
687  ereport(ERROR,
688  (errcode(ERRCODE_UNDEFINED_OBJECT),
689  errmsg("tablespace with OID %u does not exist",
690  tblspc)));
691 
692  TempTablespacePath(path, tblspc);
693  return pg_ls_dir_files(fcinfo, path, true);
694 }
int errcode(int sqlerrcode)
Definition: elog.c:698
void TempTablespacePath(char *path, Oid tablespace)
Definition: fd.c:1678
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:184
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define MAXPGPATH
static Datum pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir, bool missing_ok)
Definition: genfile.c:577
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pg_ls_tmpdir_1arg()

Datum pg_ls_tmpdir_1arg ( PG_FUNCTION_ARGS  )

Definition at line 711 of file genfile.c.

References PG_GETARG_OID, and pg_ls_tmpdir().

712 {
713  return pg_ls_tmpdir(fcinfo, PG_GETARG_OID(0));
714 }
static Datum pg_ls_tmpdir(FunctionCallInfo fcinfo, Oid tblspc)
Definition: genfile.c:682
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

◆ pg_ls_tmpdir_noargs()

Datum pg_ls_tmpdir_noargs ( PG_FUNCTION_ARGS  )

Definition at line 701 of file genfile.c.

References pg_ls_tmpdir().

702 {
703  return pg_ls_tmpdir(fcinfo, DEFAULTTABLESPACE_OID);
704 }
static Datum pg_ls_tmpdir(FunctionCallInfo fcinfo, Oid tblspc)
Definition: genfile.c:682

◆ pg_ls_waldir()

Datum pg_ls_waldir ( PG_FUNCTION_ARGS  )

Definition at line 673 of file genfile.c.

References pg_ls_dir_files(), and XLOGDIR.

674 {
675  return pg_ls_dir_files(fcinfo, XLOGDIR, false);
676 }
static Datum pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir, bool missing_ok)
Definition: genfile.c:577
#define XLOGDIR

◆ pg_read_binary_file()

Datum pg_read_binary_file ( PG_FUNCTION_ARGS  )

Definition at line 325 of file genfile.c.

References convert_and_check_filename(), ereport, errcode(), errmsg(), ERROR, filename, PG_GETARG_BOOL, PG_GETARG_INT64, PG_GETARG_TEXT_PP, PG_NARGS, PG_RETURN_BYTEA_P, PG_RETURN_NULL, and read_binary_file().

Referenced by pg_read_binary_file_all(), and pg_read_binary_file_off_len().

326 {
327  text *filename_t = PG_GETARG_TEXT_PP(0);
328  int64 seek_offset = 0;
329  int64 bytes_to_read = -1;
330  bool missing_ok = false;
331  char *filename;
332  bytea *result;
333 
334  /* handle optional arguments */
335  if (PG_NARGS() >= 3)
336  {
337  seek_offset = PG_GETARG_INT64(1);
338  bytes_to_read = PG_GETARG_INT64(2);
339 
340  if (bytes_to_read < 0)
341  ereport(ERROR,
342  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
343  errmsg("requested length cannot be negative")));
344  }
345  if (PG_NARGS() >= 4)
346  missing_ok = PG_GETARG_BOOL(3);
347 
348  filename = convert_and_check_filename(filename_t);
349 
350  result = read_binary_file(filename, seek_offset,
351  bytes_to_read, missing_ok);
352  if (result)
353  PG_RETURN_BYTEA_P(result);
354  else
355  PG_RETURN_NULL();
356 }
static char * convert_and_check_filename(text *arg)
Definition: genfile.c:53
int errcode(int sqlerrcode)
Definition: elog.c:698
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define ERROR
Definition: elog.h:46
static bytea * read_binary_file(const char *filename, int64 seek_offset, int64 bytes_to_read, bool missing_ok)
Definition: genfile.c:108
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_NARGS()
Definition: fmgr.h:203
static char * filename
Definition: pg_dumpall.c:91
int errmsg(const char *fmt,...)
Definition: elog.c:909
Definition: c.h:621
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_read_binary_file_all()

Datum pg_read_binary_file_all ( PG_FUNCTION_ARGS  )

Definition at line 386 of file genfile.c.

References pg_read_binary_file().

387 {
388  return pg_read_binary_file(fcinfo);
389 }
Datum pg_read_binary_file(PG_FUNCTION_ARGS)
Definition: genfile.c:325

◆ pg_read_binary_file_off_len()

Datum pg_read_binary_file_off_len ( PG_FUNCTION_ARGS  )

Definition at line 380 of file genfile.c.

References pg_read_binary_file().

381 {
382  return pg_read_binary_file(fcinfo);
383 }
Datum pg_read_binary_file(PG_FUNCTION_ARGS)
Definition: genfile.c:325

◆ pg_read_file()

Datum pg_read_file ( PG_FUNCTION_ARGS  )

Definition at line 242 of file genfile.c.

References convert_and_check_filename(), ereport, errcode(), errhint(), errmsg(), ERROR, filename, PG_GETARG_BOOL, PG_GETARG_INT64, PG_GETARG_TEXT_PP, PG_NARGS, PG_RETURN_NULL, PG_RETURN_TEXT_P, read_text_file(), and superuser().

243 {
244  text *filename_t = PG_GETARG_TEXT_PP(0);
245  int64 seek_offset = 0;
246  int64 bytes_to_read = -1;
247  bool missing_ok = false;
248  char *filename;
249  text *result;
250 
251  if (!superuser())
252  ereport(ERROR,
253  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
254  errmsg("must be superuser to read files with adminpack 1.0"),
255  /* translator: %s is a SQL function name */
256  errhint("Consider using %s, which is part of core, instead.",
257  "pg_read_file()")));
258 
259  /* handle optional arguments */
260  if (PG_NARGS() >= 3)
261  {
262  seek_offset = PG_GETARG_INT64(1);
263  bytes_to_read = PG_GETARG_INT64(2);
264 
265  if (bytes_to_read < 0)
266  ereport(ERROR,
267  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
268  errmsg("requested length cannot be negative")));
269  }
270  if (PG_NARGS() >= 4)
271  missing_ok = PG_GETARG_BOOL(3);
272 
273  filename = convert_and_check_filename(filename_t);
274 
275  result = read_text_file(filename, seek_offset, bytes_to_read, missing_ok);
276  if (result)
277  PG_RETURN_TEXT_P(result);
278  else
279  PG_RETURN_NULL();
280 }
static char * convert_and_check_filename(text *arg)
Definition: genfile.c:53
int errhint(const char *fmt,...)
Definition: elog.c:1156
int errcode(int sqlerrcode)
Definition: elog.c:698
bool superuser(void)
Definition: superuser.c:46
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_NARGS()
Definition: fmgr.h:203
static char * filename
Definition: pg_dumpall.c:91
int errmsg(const char *fmt,...)
Definition: elog.c:909
Definition: c.h:621
static text * read_text_file(const char *filename, int64 seek_offset, int64 bytes_to_read, bool missing_ok)
Definition: genfile.c:217
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_read_file_all()

Datum pg_read_file_all ( PG_FUNCTION_ARGS  )

Definition at line 374 of file genfile.c.

References pg_read_file_v2().

375 {
376  return pg_read_file_v2(fcinfo);
377 }
Datum pg_read_file_v2(PG_FUNCTION_ARGS)
Definition: genfile.c:289

◆ pg_read_file_off_len()

Datum pg_read_file_off_len ( PG_FUNCTION_ARGS  )

Definition at line 368 of file genfile.c.

References pg_read_file_v2().

369 {
370  return pg_read_file_v2(fcinfo);
371 }
Datum pg_read_file_v2(PG_FUNCTION_ARGS)
Definition: genfile.c:289

◆ pg_read_file_v2()

Datum pg_read_file_v2 ( PG_FUNCTION_ARGS  )

Definition at line 289 of file genfile.c.

References convert_and_check_filename(), ereport, errcode(), errmsg(), ERROR, filename, PG_GETARG_BOOL, PG_GETARG_INT64, PG_GETARG_TEXT_PP, PG_NARGS, PG_RETURN_NULL, PG_RETURN_TEXT_P, and read_text_file().

Referenced by pg_read_file_all(), and pg_read_file_off_len().

290 {
291  text *filename_t = PG_GETARG_TEXT_PP(0);
292  int64 seek_offset = 0;
293  int64 bytes_to_read = -1;
294  bool missing_ok = false;
295  char *filename;
296  text *result;
297 
298  /* handle optional arguments */
299  if (PG_NARGS() >= 3)
300  {
301  seek_offset = PG_GETARG_INT64(1);
302  bytes_to_read = PG_GETARG_INT64(2);
303 
304  if (bytes_to_read < 0)
305  ereport(ERROR,
306  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
307  errmsg("requested length cannot be negative")));
308  }
309  if (PG_NARGS() >= 4)
310  missing_ok = PG_GETARG_BOOL(3);
311 
312  filename = convert_and_check_filename(filename_t);
313 
314  result = read_text_file(filename, seek_offset, bytes_to_read, missing_ok);
315  if (result)
316  PG_RETURN_TEXT_P(result);
317  else
318  PG_RETURN_NULL();
319 }
static char * convert_and_check_filename(text *arg)
Definition: genfile.c:53
int errcode(int sqlerrcode)
Definition: elog.c:698
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_NARGS()
Definition: fmgr.h:203
static char * filename
Definition: pg_dumpall.c:91
int errmsg(const char *fmt,...)
Definition: elog.c:909
Definition: c.h:621
static text * read_text_file(const char *filename, int64 seek_offset, int64 bytes_to_read, bool missing_ok)
Definition: genfile.c:217
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_stat_file()

Datum pg_stat_file ( PG_FUNCTION_ARGS  )

Definition at line 395 of file genfile.c.

References BlessTupleDesc(), BoolGetDatum, convert_and_check_filename(), CreateTemplateTupleDesc(), ereport, errcode_for_file_access(), errmsg(), ERROR, filename, heap_form_tuple(), HeapTupleGetDatum, Int64GetDatum(), pfree(), PG_GETARG_BOOL, PG_GETARG_TEXT_PP, PG_NARGS, PG_RETURN_DATUM, PG_RETURN_NULL, S_ISDIR, stat::st_atime, stat::st_ctime, stat::st_mode, stat::st_mtime, stat::st_size, stat, time_t_to_timestamptz(), TimestampTzGetDatum, TupleDescInitEntry(), and values.

Referenced by pg_stat_file_1arg().

396 {
397  text *filename_t = PG_GETARG_TEXT_PP(0);
398  char *filename;
399  struct stat fst;
400  Datum values[6];
401  bool isnull[6];
402  HeapTuple tuple;
403  TupleDesc tupdesc;
404  bool missing_ok = false;
405 
406  /* check the optional argument */
407  if (PG_NARGS() == 2)
408  missing_ok = PG_GETARG_BOOL(1);
409 
410  filename = convert_and_check_filename(filename_t);
411 
412  if (stat(filename, &fst) < 0)
413  {
414  if (missing_ok && errno == ENOENT)
415  PG_RETURN_NULL();
416  else
417  ereport(ERROR,
419  errmsg("could not stat file \"%s\": %m", filename)));
420  }
421 
422  /*
423  * This record type had better match the output parameters declared for me
424  * in pg_proc.h.
425  */
426  tupdesc = CreateTemplateTupleDesc(6);
427  TupleDescInitEntry(tupdesc, (AttrNumber) 1,
428  "size", INT8OID, -1, 0);
429  TupleDescInitEntry(tupdesc, (AttrNumber) 2,
430  "access", TIMESTAMPTZOID, -1, 0);
431  TupleDescInitEntry(tupdesc, (AttrNumber) 3,
432  "modification", TIMESTAMPTZOID, -1, 0);
433  TupleDescInitEntry(tupdesc, (AttrNumber) 4,
434  "change", TIMESTAMPTZOID, -1, 0);
435  TupleDescInitEntry(tupdesc, (AttrNumber) 5,
436  "creation", TIMESTAMPTZOID, -1, 0);
437  TupleDescInitEntry(tupdesc, (AttrNumber) 6,
438  "isdir", BOOLOID, -1, 0);
439  BlessTupleDesc(tupdesc);
440 
441  memset(isnull, false, sizeof(isnull));
442 
443  values[0] = Int64GetDatum((int64) fst.st_size);
444  values[1] = TimestampTzGetDatum(time_t_to_timestamptz(fst.st_atime));
445  values[2] = TimestampTzGetDatum(time_t_to_timestamptz(fst.st_mtime));
446  /* Unix has file status change time, while Win32 has creation time */
447 #if !defined(WIN32) && !defined(__CYGWIN__)
448  values[3] = TimestampTzGetDatum(time_t_to_timestamptz(fst.st_ctime));
449  isnull[4] = true;
450 #else
451  isnull[3] = true;
452  values[4] = TimestampTzGetDatum(time_t_to_timestamptz(fst.st_ctime));
453 #endif
454  values[5] = BoolGetDatum(S_ISDIR(fst.st_mode));
455 
456  tuple = heap_form_tuple(tupdesc, values, isnull);
457 
458  pfree(filename);
459 
461 }
static char * convert_and_check_filename(text *arg)
Definition: genfile.c:53
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ERROR
Definition: elog.h:46
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2052
int errcode_for_file_access(void)
Definition: elog.c:721
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1700
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:580
uintptr_t Datum
Definition: postgres.h:411
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
TimestampTz time_t_to_timestamptz(pg_time_t tm)
Definition: timestamp.c:1732
#define BoolGetDatum(X)
Definition: postgres.h:446
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_NARGS()
Definition: fmgr.h:203
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:221
#define S_ISDIR(m)
Definition: win32_port.h:316
static Datum values[MAXATTR]
Definition: bootstrap.c:166
static char * filename
Definition: pg_dumpall.c:91
int errmsg(const char *fmt,...)
Definition: elog.c:909
Definition: c.h:621
int16 AttrNumber
Definition: attnum.h:21
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define stat
Definition: win32_port.h:275

◆ pg_stat_file_1arg()

Datum pg_stat_file_1arg ( PG_FUNCTION_ARGS  )

Definition at line 471 of file genfile.c.

References pg_stat_file().

472 {
473  return pg_stat_file(fcinfo);
474 }
Datum pg_stat_file(PG_FUNCTION_ARGS)
Definition: genfile.c:395

◆ read_binary_file()

static bytea* read_binary_file ( const char *  filename,
int64  seek_offset,
int64  bytes_to_read,
bool  missing_ok 
)
static

Definition at line 108 of file genfile.c.

References AllocateFile(), Assert, buf, StringInfoData::data, enlargeStringInfo(), ereport, errcode(), errcode_for_file_access(), errmsg(), ERROR, FreeFile(), fseeko, initStringInfo(), StringInfoData::len, MaxAllocSize, StringInfoData::maxlen, MIN_READ_SIZE, palloc(), PG_BINARY_R, SET_VARSIZE, VARDATA, and VARHDRSZ.

Referenced by pg_read_binary_file(), and read_text_file().

110 {
111  bytea *buf;
112  size_t nbytes = 0;
113  FILE *file;
114 
115  /* clamp request size to what we can actually deliver */
116  if (bytes_to_read > (int64) (MaxAllocSize - VARHDRSZ))
117  ereport(ERROR,
118  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
119  errmsg("requested length too large")));
120 
121  if ((file = AllocateFile(filename, PG_BINARY_R)) == NULL)
122  {
123  if (missing_ok && errno == ENOENT)
124  return NULL;
125  else
126  ereport(ERROR,
128  errmsg("could not open file \"%s\" for reading: %m",
129  filename)));
130  }
131 
132  if (fseeko(file, (off_t) seek_offset,
133  (seek_offset >= 0) ? SEEK_SET : SEEK_END) != 0)
134  ereport(ERROR,
136  errmsg("could not seek in file \"%s\": %m", filename)));
137 
138  if (bytes_to_read >= 0)
139  {
140  /* If passed explicit read size just do it */
141  buf = (bytea *) palloc((Size) bytes_to_read + VARHDRSZ);
142 
143  nbytes = fread(VARDATA(buf), 1, (size_t) bytes_to_read, file);
144  }
145  else
146  {
147  /* Negative read size, read rest of file */
148  StringInfoData sbuf;
149 
150  initStringInfo(&sbuf);
151  /* Leave room in the buffer for the varlena length word */
152  sbuf.len += VARHDRSZ;
153  Assert(sbuf.len < sbuf.maxlen);
154 
155  while (!(feof(file) || ferror(file)))
156  {
157  size_t rbytes;
158 
159  /* Minimum amount to read at a time */
160 #define MIN_READ_SIZE 4096
161 
162  /*
163  * If not at end of file, and sbuf.len is equal to
164  * MaxAllocSize - 1, then either the file is too large, or
165  * there is nothing left to read. Attempt to read one more
166  * byte to see if the end of file has been reached. If not,
167  * the file is too large; we'd rather give the error message
168  * for that ourselves.
169  */
170  if (sbuf.len == MaxAllocSize - 1)
171  {
172  char rbuf[1];
173 
174  if (fread(rbuf, 1, 1, file) != 0 || !feof(file))
175  ereport(ERROR,
176  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
177  errmsg("file length too large")));
178  else
179  break;
180  }
181 
182  /* OK, ensure that we can read at least MIN_READ_SIZE */
184 
185  /*
186  * stringinfo.c likes to allocate in powers of 2, so it's likely
187  * that much more space is available than we asked for. Use all
188  * of it, rather than making more fread calls than necessary.
189  */
190  rbytes = fread(sbuf.data + sbuf.len, 1,
191  (size_t) (sbuf.maxlen - sbuf.len - 1), file);
192  sbuf.len += rbytes;
193  nbytes += rbytes;
194  }
195 
196  /* Now we can commandeer the stringinfo's buffer as the result */
197  buf = (bytea *) sbuf.data;
198  }
199 
200  if (ferror(file))
201  ereport(ERROR,
203  errmsg("could not read file \"%s\": %m", filename)));
204 
205  SET_VARSIZE(buf, nbytes + VARHDRSZ);
206 
207  FreeFile(file);
208 
209  return buf;
210 }
#define VARDATA(PTR)
Definition: postgres.h:315
#define VARHDRSZ
Definition: c.h:627
int errcode(int sqlerrcode)
Definition: elog.c:698
#define PG_BINARY_R
Definition: c.h:1273
#define ERROR
Definition: elog.h:46
static char * buf
Definition: pg_test_fsync.c:68
int errcode_for_file_access(void)
Definition: elog.c:721
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2373
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:283
#define MIN_READ_SIZE
#define MaxAllocSize
Definition: memutils.h:40
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define fseeko(stream, offset, origin)
Definition: win32_port.h:207
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
size_t Size
Definition: c.h:540
int FreeFile(FILE *file)
Definition: fd.c:2572
static char * filename
Definition: pg_dumpall.c:91
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
Definition: c.h:621
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342

◆ read_text_file()

static text* read_text_file ( const char *  filename,
int64  seek_offset,
int64  bytes_to_read,
bool  missing_ok 
)
static

Definition at line 217 of file genfile.c.

References buf, pg_verifymbstr(), read_binary_file(), VARDATA, VARHDRSZ, and VARSIZE.

Referenced by pg_read_file(), and pg_read_file_v2().

219 {
220  bytea *buf;
221 
222  buf = read_binary_file(filename, seek_offset, bytes_to_read, missing_ok);
223 
224  if (buf != NULL)
225  {
226  /* Make sure the input is valid */
227  pg_verifymbstr(VARDATA(buf), VARSIZE(buf) - VARHDRSZ, false);
228 
229  /* OK, we can cast it to text safely */
230  return (text *) buf;
231  }
232  else
233  return NULL;
234 }
#define VARDATA(PTR)
Definition: postgres.h:315
#define VARSIZE(PTR)
Definition: postgres.h:316
#define VARHDRSZ
Definition: c.h:627
static bytea * read_binary_file(const char *filename, int64 seek_offset, int64 bytes_to_read, bool missing_ok)
Definition: genfile.c:108
static char * buf
Definition: pg_test_fsync.c:68
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: mbutils.c:1505
static char * filename
Definition: pg_dumpall.c:91
Definition: c.h:621