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 "replication/slot.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)
 
static textpg_read_file_common (text *filename_t, int64 seek_offset, int64 bytes_to_read, bool read_to_eof, bool missing_ok)
 
static byteapg_read_binary_file_common (text *filename_t, int64 seek_offset, int64 bytes_to_read, bool read_to_eof, bool missing_ok)
 
Datum pg_read_file_off_len (PG_FUNCTION_ARGS)
 
Datum pg_read_file_off_len_missing (PG_FUNCTION_ARGS)
 
Datum pg_read_file_all (PG_FUNCTION_ARGS)
 
Datum pg_read_file_all_missing (PG_FUNCTION_ARGS)
 
Datum pg_read_binary_file_off_len (PG_FUNCTION_ARGS)
 
Datum pg_read_binary_file_off_len_missing (PG_FUNCTION_ARGS)
 
Datum pg_read_binary_file_all (PG_FUNCTION_ARGS)
 
Datum pg_read_binary_file_all_missing (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)
 
Datum pg_ls_logicalsnapdir (PG_FUNCTION_ARGS)
 
Datum pg_ls_logicalmapdir (PG_FUNCTION_ARGS)
 
Datum pg_ls_replslotdir (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ MIN_READ_SIZE

#define MIN_READ_SIZE   4096

Function Documentation

◆ convert_and_check_filename()

static char* convert_and_check_filename ( text arg)
static

Definition at line 54 of file genfile.c.

55 {
56  char *filename;
57 
59  canonicalize_path(filename); /* filename can change length here */
60 
61  /*
62  * Roles with privileges of the 'pg_read_server_files' role are allowed to
63  * access any files on the server as the PG user, so no need to do any
64  * further checks here.
65  */
66  if (has_privs_of_role(GetUserId(), ROLE_PG_READ_SERVER_FILES))
67  return filename;
68 
69  /*
70  * User isn't a member of the pg_read_server_files role, so check if it's
71  * allowable
72  */
74  {
75  /*
76  * Allow absolute paths if within DataDir or Log_directory, even
77  * though Log_directory might be outside DataDir.
78  */
82  ereport(ERROR,
83  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
84  errmsg("absolute path not allowed")));
85  }
87  ereport(ERROR,
88  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
89  errmsg("path must be in or below the current directory")));
90 
91  return filename;
92 }
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4949
int errcode(int sqlerrcode)
Definition: elog.c:695
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define ERROR
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:145
char * DataDir
Definition: globals.c:66
Oid GetUserId(void)
Definition: miscinit.c:497
void * arg
static char * filename
Definition: pg_dumpall.c:119
#define is_absolute_path(filename)
Definition: port.h:103
bool path_is_prefix_of_path(const char *path1, const char *path2)
Definition: path.c:559
bool path_is_relative_and_below_cwd(const char *path)
Definition: path.c:526
void canonicalize_path(char *path)
Definition: path.c:264
char * Log_directory
Definition: syslogger.c:74
char * text_to_cstring(const text *t)
Definition: varlena.c:222

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

Referenced by pg_ls_dir(), pg_read_binary_file_common(), pg_read_file(), pg_read_file_common(), and pg_stat_file().

◆ pg_ls_archive_statusdir()

Datum pg_ls_archive_statusdir ( PG_FUNCTION_ARGS  )

Definition at line 733 of file genfile.c.

734 {
735  return pg_ls_dir_files(fcinfo, XLOGDIR "/archive_status", true);
736 }
static Datum pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir, bool missing_ok)
Definition: genfile.c:616
#define XLOGDIR

References pg_ls_dir_files(), and XLOGDIR.

◆ pg_ls_dir()

Datum pg_ls_dir ( PG_FUNCTION_ARGS  )

Definition at line 544 of file genfile.c.

545 {
546  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
547  char *location;
548  bool missing_ok = false;
549  bool include_dot_dirs = false;
550  DIR *dirdesc;
551  struct dirent *de;
552 
554 
555  /* check the optional arguments */
556  if (PG_NARGS() == 3)
557  {
558  if (!PG_ARGISNULL(1))
559  missing_ok = PG_GETARG_BOOL(1);
560  if (!PG_ARGISNULL(2))
561  include_dot_dirs = PG_GETARG_BOOL(2);
562  }
563 
565 
566  dirdesc = AllocateDir(location);
567  if (!dirdesc)
568  {
569  /* Return empty tuplestore if appropriate */
570  if (missing_ok && errno == ENOENT)
571  return (Datum) 0;
572  /* Otherwise, we can let ReadDir() throw the error */
573  }
574 
575  while ((de = ReadDir(dirdesc, location)) != NULL)
576  {
577  Datum values[1];
578  bool nulls[1];
579 
580  if (!include_dot_dirs &&
581  (strcmp(de->d_name, ".") == 0 ||
582  strcmp(de->d_name, "..") == 0))
583  continue;
584 
585  values[0] = CStringGetTextDatum(de->d_name);
586  nulls[0] = false;
587 
588  tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
589  values, nulls);
590  }
591 
592  FreeDir(dirdesc);
593  return (Datum) 0;
594 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define CStringGetTextDatum(s)
Definition: builtins.h:85
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2709
int FreeDir(DIR *dir)
Definition: fd.c:2761
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2643
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_NARGS()
Definition: fmgr.h:203
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
void InitMaterializedSRF(FunctionCallInfo fcinfo, bits32 flags)
Definition: funcapi.c:76
#define MAT_SRF_USE_EXPECTED_DESC
Definition: funcapi.h:295
static char * convert_and_check_filename(text *arg)
Definition: genfile.c:54
uintptr_t Datum
Definition: postgres.h:412
Definition: dirent.c:26
TupleDesc setDesc
Definition: execnodes.h:332
Tuplestorestate * setResult
Definition: execnodes.h:331
Definition: dirent.h:10
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750

References AllocateDir(), convert_and_check_filename(), CStringGetTextDatum, FreeDir(), InitMaterializedSRF(), MAT_SRF_USE_EXPECTED_DESC, PG_ARGISNULL, PG_GETARG_BOOL, PG_GETARG_TEXT_PP, PG_NARGS, ReadDir(), ReturnSetInfo::setDesc, ReturnSetInfo::setResult, tuplestore_putvalues(), and values.

Referenced by pg_ls_dir_1arg().

◆ pg_ls_dir_1arg()

Datum pg_ls_dir_1arg ( PG_FUNCTION_ARGS  )

Definition at line 604 of file genfile.c.

605 {
606  return pg_ls_dir(fcinfo);
607 }
Datum pg_ls_dir(PG_FUNCTION_ARGS)
Definition: genfile.c:544

References pg_ls_dir().

◆ pg_ls_dir_files()

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

Definition at line 616 of file genfile.c.

617 {
618  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
619  DIR *dirdesc;
620  struct dirent *de;
621 
622  InitMaterializedSRF(fcinfo, 0);
623 
624  /*
625  * Now walk the directory. Note that we must do this within a single SRF
626  * call, not leave the directory open across multiple calls, since we
627  * can't count on the SRF being run to completion.
628  */
629  dirdesc = AllocateDir(dir);
630  if (!dirdesc)
631  {
632  /* Return empty tuplestore if appropriate */
633  if (missing_ok && errno == ENOENT)
634  return (Datum) 0;
635  /* Otherwise, we can let ReadDir() throw the error */
636  }
637 
638  while ((de = ReadDir(dirdesc, dir)) != NULL)
639  {
640  Datum values[3];
641  bool nulls[3];
642  char path[MAXPGPATH * 2];
643  struct stat attrib;
644 
645  /* Skip hidden files */
646  if (de->d_name[0] == '.')
647  continue;
648 
649  /* Get the file info */
650  snprintf(path, sizeof(path), "%s/%s", dir, de->d_name);
651  if (stat(path, &attrib) < 0)
652  {
653  /* Ignore concurrently-deleted files, else complain */
654  if (errno == ENOENT)
655  continue;
656  ereport(ERROR,
658  errmsg("could not stat file \"%s\": %m", path)));
659  }
660 
661  /* Ignore anything but regular files */
662  if (!S_ISREG(attrib.st_mode))
663  continue;
664 
665  values[0] = CStringGetTextDatum(de->d_name);
666  values[1] = Int64GetDatum((int64) attrib.st_size);
667  values[2] = TimestampTzGetDatum(time_t_to_timestamptz(attrib.st_mtime));
668  memset(nulls, 0, sizeof(nulls));
669 
670  tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
671  }
672 
673  FreeDir(dirdesc);
674  return (Datum) 0;
675 }
TimestampTz time_t_to_timestamptz(pg_time_t tm)
Definition: timestamp.c:1740
int errcode_for_file_access(void)
Definition: elog.c:718
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1683
#define MAXPGPATH
#define snprintf
Definition: port.h:238
fmNodePtr resultinfo
Definition: fmgr.h:89
static Datum TimestampTzGetDatum(TimestampTz X)
Definition: timestamp.h:52
#define stat
Definition: win32_port.h:286
#define S_ISREG(m)
Definition: win32_port.h:330

References AllocateDir(), CStringGetTextDatum, ereport, errcode_for_file_access(), errmsg(), ERROR, FreeDir(), InitMaterializedSRF(), Int64GetDatum(), MAXPGPATH, ReadDir(), FunctionCallInfoBaseData::resultinfo, S_ISREG, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, snprintf, stat::st_mode, stat::st_mtime, stat::st_size, stat, time_t_to_timestamptz(), TimestampTzGetDatum(), tuplestore_putvalues(), and values.

Referenced by pg_ls_archive_statusdir(), pg_ls_logdir(), pg_ls_logicalmapdir(), pg_ls_logicalsnapdir(), pg_ls_replslotdir(), pg_ls_tmpdir(), and pg_ls_waldir().

◆ pg_ls_logdir()

Datum pg_ls_logdir ( PG_FUNCTION_ARGS  )

Definition at line 679 of file genfile.c.

680 {
681  return pg_ls_dir_files(fcinfo, Log_directory, false);
682 }

References Log_directory, and pg_ls_dir_files().

◆ pg_ls_logicalmapdir()

Datum pg_ls_logicalmapdir ( PG_FUNCTION_ARGS  )

Definition at line 751 of file genfile.c.

752 {
753  return pg_ls_dir_files(fcinfo, "pg_logical/mappings", false);
754 }

References pg_ls_dir_files().

◆ pg_ls_logicalsnapdir()

Datum pg_ls_logicalsnapdir ( PG_FUNCTION_ARGS  )

Definition at line 742 of file genfile.c.

743 {
744  return pg_ls_dir_files(fcinfo, "pg_logical/snapshots", false);
745 }

References pg_ls_dir_files().

◆ pg_ls_replslotdir()

Datum pg_ls_replslotdir ( PG_FUNCTION_ARGS  )

Definition at line 761 of file genfile.c.

762 {
763  text *slotname_t;
764  char path[MAXPGPATH];
765  char *slotname;
766 
767  slotname_t = PG_GETARG_TEXT_PP(0);
768 
769  slotname = text_to_cstring(slotname_t);
770 
771  if (!SearchNamedReplicationSlot(slotname, true))
772  ereport(ERROR,
773  (errcode(ERRCODE_UNDEFINED_OBJECT),
774  errmsg("replication slot \"%s\" does not exist",
775  slotname)));
776 
777  snprintf(path, sizeof(path), "pg_replslot/%s", slotname);
778  return pg_ls_dir_files(fcinfo, path, false);
779 }
ReplicationSlot * SearchNamedReplicationSlot(const char *name, bool need_lock)
Definition: slot.c:374
Definition: c.h:623

References ereport, errcode(), errmsg(), ERROR, MAXPGPATH, PG_GETARG_TEXT_PP, pg_ls_dir_files(), SearchNamedReplicationSlot(), snprintf, and text_to_cstring().

◆ pg_ls_tmpdir()

static Datum pg_ls_tmpdir ( FunctionCallInfo  fcinfo,
Oid  tblspc 
)
static

Definition at line 695 of file genfile.c.

696 {
697  char path[MAXPGPATH];
698 
700  ereport(ERROR,
701  (errcode(ERRCODE_UNDEFINED_OBJECT),
702  errmsg("tablespace with OID %u does not exist",
703  tblspc)));
704 
705  TempTablespacePath(path, tblspc);
706  return pg_ls_dir_files(fcinfo, path, true);
707 }
void TempTablespacePath(char *path, Oid tablespace)
Definition: fd.c:1684
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:600
@ TABLESPACEOID
Definition: syscache.h:101
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:188

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().

◆ pg_ls_tmpdir_1arg()

Datum pg_ls_tmpdir_1arg ( PG_FUNCTION_ARGS  )

Definition at line 724 of file genfile.c.

725 {
726  return pg_ls_tmpdir(fcinfo, PG_GETARG_OID(0));
727 }
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
static Datum pg_ls_tmpdir(FunctionCallInfo fcinfo, Oid tblspc)
Definition: genfile.c:695

References PG_GETARG_OID, and pg_ls_tmpdir().

◆ pg_ls_tmpdir_noargs()

Datum pg_ls_tmpdir_noargs ( PG_FUNCTION_ARGS  )

Definition at line 714 of file genfile.c.

715 {
716  return pg_ls_tmpdir(fcinfo, DEFAULTTABLESPACE_OID);
717 }

References pg_ls_tmpdir().

◆ pg_ls_waldir()

Datum pg_ls_waldir ( PG_FUNCTION_ARGS  )

Definition at line 686 of file genfile.c.

687 {
688  return pg_ls_dir_files(fcinfo, XLOGDIR, false);
689 }

References pg_ls_dir_files(), and XLOGDIR.

◆ pg_read_binary_file_all()

Datum pg_read_binary_file_all ( PG_FUNCTION_ARGS  )

Definition at line 427 of file genfile.c.

428 {
429  text *filename_t = PG_GETARG_TEXT_PP(0);
430  text *ret;
431 
432  ret = pg_read_binary_file_common(filename_t, 0, -1, true, false);
433 
434  if (!ret)
435  PG_RETURN_NULL();
436 
437  PG_RETURN_BYTEA_P(ret);
438 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
#define PG_RETURN_NULL()
Definition: fmgr.h:345
static bytea * pg_read_binary_file_common(text *filename_t, int64 seek_offset, int64 bytes_to_read, bool read_to_eof, bool missing_ok)
Definition: genfile.c:306

References PG_GETARG_TEXT_PP, pg_read_binary_file_common(), PG_RETURN_BYTEA_P, and PG_RETURN_NULL.

◆ pg_read_binary_file_all_missing()

Datum pg_read_binary_file_all_missing ( PG_FUNCTION_ARGS  )

Definition at line 441 of file genfile.c.

442 {
443  text *filename_t = PG_GETARG_TEXT_PP(0);
444  bool missing_ok = PG_GETARG_BOOL(1);
445  text *ret;
446 
447  ret = pg_read_binary_file_common(filename_t, 0, -1, true, missing_ok);
448 
449  if (!ret)
450  PG_RETURN_NULL();
451 
452  PG_RETURN_BYTEA_P(ret);
453 }

References PG_GETARG_BOOL, PG_GETARG_TEXT_PP, pg_read_binary_file_common(), PG_RETURN_BYTEA_P, and PG_RETURN_NULL.

◆ pg_read_binary_file_common()

static bytea* pg_read_binary_file_common ( text filename_t,
int64  seek_offset,
int64  bytes_to_read,
bool  read_to_eof,
bool  missing_ok 
)
static

Definition at line 306 of file genfile.c.

309 {
310  if (read_to_eof)
311  Assert(bytes_to_read == -1);
312  else if (bytes_to_read < 0)
313  ereport(ERROR,
314  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
315  errmsg("requested length cannot be negative")));
316 
317  return read_binary_file(convert_and_check_filename(filename_t),
318  seek_offset, bytes_to_read, missing_ok);
319 }
static bytea * read_binary_file(const char *filename, int64 seek_offset, int64 bytes_to_read, bool missing_ok)
Definition: genfile.c:103
Assert(fmt[strlen(fmt) - 1] !='\n')

References Assert(), convert_and_check_filename(), ereport, errcode(), errmsg(), ERROR, and read_binary_file().

Referenced by pg_read_binary_file_all(), pg_read_binary_file_all_missing(), pg_read_binary_file_off_len(), and pg_read_binary_file_off_len_missing().

◆ pg_read_binary_file_off_len()

Datum pg_read_binary_file_off_len ( PG_FUNCTION_ARGS  )

Definition at line 394 of file genfile.c.

395 {
396  text *filename_t = PG_GETARG_TEXT_PP(0);
397  int64 seek_offset = PG_GETARG_INT64(1);
398  int64 bytes_to_read = PG_GETARG_INT64(2);
399  text *ret;
400 
401  ret = pg_read_binary_file_common(filename_t, seek_offset, bytes_to_read,
402  false, false);
403  if (!ret)
404  PG_RETURN_NULL();
405 
406  PG_RETURN_BYTEA_P(ret);
407 }
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283

References PG_GETARG_INT64, PG_GETARG_TEXT_PP, pg_read_binary_file_common(), PG_RETURN_BYTEA_P, and PG_RETURN_NULL.

◆ pg_read_binary_file_off_len_missing()

Datum pg_read_binary_file_off_len_missing ( PG_FUNCTION_ARGS  )

Definition at line 410 of file genfile.c.

411 {
412  text *filename_t = PG_GETARG_TEXT_PP(0);
413  int64 seek_offset = PG_GETARG_INT64(1);
414  int64 bytes_to_read = PG_GETARG_INT64(2);
415  bool missing_ok = PG_GETARG_BOOL(3);
416  text *ret;
417 
418  ret = pg_read_binary_file_common(filename_t, seek_offset, bytes_to_read,
419  false, missing_ok);
420  if (!ret)
421  PG_RETURN_NULL();
422 
423  PG_RETURN_BYTEA_P(ret);
424 }

References PG_GETARG_BOOL, PG_GETARG_INT64, PG_GETARG_TEXT_PP, pg_read_binary_file_common(), PG_RETURN_BYTEA_P, and PG_RETURN_NULL.

◆ pg_read_file()

Datum pg_read_file ( PG_FUNCTION_ARGS  )

Definition at line 236 of file genfile.c.

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

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().

◆ pg_read_file_all()

Datum pg_read_file_all ( PG_FUNCTION_ARGS  )

Definition at line 365 of file genfile.c.

366 {
367  text *filename_t = PG_GETARG_TEXT_PP(0);
368  text *ret;
369 
370  ret = pg_read_file_common(filename_t, 0, -1, true, false);
371 
372  if (!ret)
373  PG_RETURN_NULL();
374 
375  PG_RETURN_TEXT_P(ret);
376 }
static text * pg_read_file_common(text *filename_t, int64 seek_offset, int64 bytes_to_read, bool read_to_eof, bool missing_ok)
Definition: genfile.c:286

References PG_GETARG_TEXT_PP, pg_read_file_common(), PG_RETURN_NULL, and PG_RETURN_TEXT_P.

◆ pg_read_file_all_missing()

Datum pg_read_file_all_missing ( PG_FUNCTION_ARGS  )

Definition at line 379 of file genfile.c.

380 {
381  text *filename_t = PG_GETARG_TEXT_PP(0);
382  bool missing_ok = PG_GETARG_BOOL(1);
383  text *ret;
384 
385  ret = pg_read_file_common(filename_t, 0, -1, true, missing_ok);
386 
387  if (!ret)
388  PG_RETURN_NULL();
389 
390  PG_RETURN_TEXT_P(ret);
391 }

References PG_GETARG_BOOL, PG_GETARG_TEXT_PP, pg_read_file_common(), PG_RETURN_NULL, and PG_RETURN_TEXT_P.

◆ pg_read_file_common()

static text* pg_read_file_common ( text filename_t,
int64  seek_offset,
int64  bytes_to_read,
bool  read_to_eof,
bool  missing_ok 
)
static

Definition at line 286 of file genfile.c.

288 {
289  if (read_to_eof)
290  Assert(bytes_to_read == -1);
291  else if (bytes_to_read < 0)
292  ereport(ERROR,
293  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
294  errmsg("requested length cannot be negative")));
295 
296  return read_text_file(convert_and_check_filename(filename_t),
297  seek_offset, bytes_to_read, missing_ok);
298 }

References Assert(), convert_and_check_filename(), ereport, errcode(), errmsg(), ERROR, and read_text_file().

Referenced by pg_read_file_all(), pg_read_file_all_missing(), pg_read_file_off_len(), and pg_read_file_off_len_missing().

◆ pg_read_file_off_len()

Datum pg_read_file_off_len ( PG_FUNCTION_ARGS  )

Definition at line 331 of file genfile.c.

332 {
333  text *filename_t = PG_GETARG_TEXT_PP(0);
334  int64 seek_offset = PG_GETARG_INT64(1);
335  int64 bytes_to_read = PG_GETARG_INT64(2);
336  text *ret;
337 
338  ret = pg_read_file_common(filename_t, seek_offset, bytes_to_read,
339  false, false);
340  if (!ret)
341  PG_RETURN_NULL();
342 
343  PG_RETURN_TEXT_P(ret);
344 }

References PG_GETARG_INT64, PG_GETARG_TEXT_PP, pg_read_file_common(), PG_RETURN_NULL, and PG_RETURN_TEXT_P.

◆ pg_read_file_off_len_missing()

Datum pg_read_file_off_len_missing ( PG_FUNCTION_ARGS  )

Definition at line 347 of file genfile.c.

348 {
349  text *filename_t = PG_GETARG_TEXT_PP(0);
350  int64 seek_offset = PG_GETARG_INT64(1);
351  int64 bytes_to_read = PG_GETARG_INT64(2);
352  bool missing_ok = PG_GETARG_BOOL(3);
353  text *ret;
354 
355  ret = pg_read_file_common(filename_t, seek_offset, bytes_to_read,
356  false, missing_ok);
357 
358  if (!ret)
359  PG_RETURN_NULL();
360 
361  PG_RETURN_TEXT_P(ret);
362 }

References PG_GETARG_BOOL, PG_GETARG_INT64, PG_GETARG_TEXT_PP, pg_read_file_common(), PG_RETURN_NULL, and PG_RETURN_TEXT_P.

◆ pg_stat_file()

Datum pg_stat_file ( PG_FUNCTION_ARGS  )

Definition at line 459 of file genfile.c.

460 {
461  text *filename_t = PG_GETARG_TEXT_PP(0);
462  char *filename;
463  struct stat fst;
464  Datum values[6];
465  bool isnull[6];
466  HeapTuple tuple;
467  TupleDesc tupdesc;
468  bool missing_ok = false;
469 
470  /* check the optional argument */
471  if (PG_NARGS() == 2)
472  missing_ok = PG_GETARG_BOOL(1);
473 
474  filename = convert_and_check_filename(filename_t);
475 
476  if (stat(filename, &fst) < 0)
477  {
478  if (missing_ok && errno == ENOENT)
479  PG_RETURN_NULL();
480  else
481  ereport(ERROR,
483  errmsg("could not stat file \"%s\": %m", filename)));
484  }
485 
486  /*
487  * This record type had better match the output parameters declared for me
488  * in pg_proc.h.
489  */
490  tupdesc = CreateTemplateTupleDesc(6);
491  TupleDescInitEntry(tupdesc, (AttrNumber) 1,
492  "size", INT8OID, -1, 0);
493  TupleDescInitEntry(tupdesc, (AttrNumber) 2,
494  "access", TIMESTAMPTZOID, -1, 0);
495  TupleDescInitEntry(tupdesc, (AttrNumber) 3,
496  "modification", TIMESTAMPTZOID, -1, 0);
497  TupleDescInitEntry(tupdesc, (AttrNumber) 4,
498  "change", TIMESTAMPTZOID, -1, 0);
499  TupleDescInitEntry(tupdesc, (AttrNumber) 5,
500  "creation", TIMESTAMPTZOID, -1, 0);
501  TupleDescInitEntry(tupdesc, (AttrNumber) 6,
502  "isdir", BOOLOID, -1, 0);
503  BlessTupleDesc(tupdesc);
504 
505  memset(isnull, false, sizeof(isnull));
506 
507  values[0] = Int64GetDatum((int64) fst.st_size);
510  /* Unix has file status change time, while Win32 has creation time */
511 #if !defined(WIN32) && !defined(__CYGWIN__)
513  isnull[4] = true;
514 #else
515  isnull[3] = true;
517 #endif
518  values[5] = BoolGetDatum(S_ISDIR(fst.st_mode));
519 
520  tuple = heap_form_tuple(tupdesc, values, isnull);
521 
522  pfree(filename);
523 
525 }
int16 AttrNumber
Definition: attnum.h:21
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2071
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
void pfree(void *pointer)
Definition: mcxt.c:1306
static Datum BoolGetDatum(bool X)
Definition: postgres.h:450
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:583
#define S_ISDIR(m)
Definition: win32_port.h:327

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().

◆ pg_stat_file_1arg()

Datum pg_stat_file_1arg ( PG_FUNCTION_ARGS  )

Definition at line 535 of file genfile.c.

536 {
537  return pg_stat_file(fcinfo);
538 }
Datum pg_stat_file(PG_FUNCTION_ARGS)
Definition: genfile.c:459

References pg_stat_file().

◆ 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 103 of file genfile.c.

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

References AllocateFile(), Assert(), buf, StringInfoData::data, enlargeStringInfo(), ereport, errcode(), errcode_for_file_access(), errmsg(), ERROR, filename, 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_common(), and read_text_file().

◆ 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 211 of file genfile.c.

213 {
214  bytea *buf;
215 
216  buf = read_binary_file(filename, seek_offset, bytes_to_read, missing_ok);
217 
218  if (buf != NULL)
219  {
220  /* Make sure the input is valid */
222 
223  /* OK, we can cast it to text safely */
224  return (text *) buf;
225  }
226  else
227  return NULL;
228 }
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: mbutils.c:1505
#define VARSIZE(PTR)
Definition: postgres.h:317

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

Referenced by pg_read_file(), and pg_read_file_common().