26 #include "catalog/pg_tablespace_d.h" 74 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
75 errmsg(
"reference to parent directory (\"..\") not allowed")));
85 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
86 errmsg(
"absolute path not allowed")));
90 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
91 errmsg(
"path must be in or below the current directory")));
115 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
116 errmsg(
"requested length too large")));
120 if (missing_ok && errno == ENOENT)
125 errmsg(
"could not open file \"%s\" for reading: %m",
129 if (
fseeko(file, (off_t) seek_offset,
130 (seek_offset >= 0) ? SEEK_SET : SEEK_END) != 0)
133 errmsg(
"could not seek in file \"%s\": %m", filename)));
135 if (bytes_to_read >= 0)
140 nbytes = fread(
VARDATA(buf), 1, (
size_t) bytes_to_read, file);
152 while (!(feof(file) || ferror(file)))
157 #define MIN_READ_SIZE 4096 171 if (fread(rbuf, 1, 1, file) != 0 || !feof(file))
173 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
174 errmsg(
"file length too large")));
187 rbytes = fread(sbuf.
data + sbuf.
len, 1,
188 (
size_t) (sbuf.
maxlen - sbuf.
len - 1), file);
200 errmsg(
"could not read file \"%s\": %m", filename)));
242 int64 seek_offset = 0;
243 int64 bytes_to_read = -1;
244 bool missing_ok =
false;
250 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
251 errmsg(
"must be superuser to read files with adminpack 1.0"),
253 errhint(
"Consider using %s, which is part of core, instead.",
262 if (bytes_to_read < 0)
264 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
265 errmsg(
"requested length cannot be negative")));
272 result =
read_text_file(filename, seek_offset, bytes_to_read, missing_ok);
289 int64 seek_offset = 0;
290 int64 bytes_to_read = -1;
291 bool missing_ok =
false;
301 if (bytes_to_read < 0)
303 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
304 errmsg(
"requested length cannot be negative")));
311 result =
read_text_file(filename, seek_offset, bytes_to_read, missing_ok);
325 int64 seek_offset = 0;
326 int64 bytes_to_read = -1;
327 bool missing_ok =
false;
337 if (bytes_to_read < 0)
339 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
340 errmsg(
"requested length cannot be negative")));
348 bytes_to_read, missing_ok);
401 bool missing_ok =
false;
409 if (
stat(filename, &fst) < 0)
411 if (missing_ok && errno == ENOENT)
416 errmsg(
"could not stat file \"%s\": %m", filename)));
425 "size", INT8OID, -1, 0);
427 "access", TIMESTAMPTZOID, -1, 0);
429 "modification", TIMESTAMPTZOID, -1, 0);
431 "change", TIMESTAMPTZOID, -1, 0);
433 "creation", TIMESTAMPTZOID, -1, 0);
435 "isdir", BOOLOID, -1, 0);
438 memset(isnull,
false,
sizeof(isnull));
444 #if !defined(WIN32) && !defined(__CYGWIN__) 481 bool missing_ok =
false;
482 bool include_dot_dirs =
false;
504 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
505 errmsg(
"set-valued function called in context that cannot accept a set")));
508 (
errcode(ERRCODE_SYNTAX_ERROR),
509 errmsg(
"materialize mode required, but it is not allowed in this context")));
529 if (missing_ok && errno == ENOENT)
534 while ((de =
ReadDir(dirdesc, location)) != NULL)
539 if (!include_dot_dirs &&
540 (strcmp(de->d_name,
".") == 0 ||
541 strcmp(de->d_name,
"..") == 0))
587 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
588 errmsg(
"set-valued function called in context that cannot accept a set")));
591 (
errcode(ERRCODE_SYNTAX_ERROR),
592 errmsg(
"materialize mode required, but it is not allowed in this context")));
598 elog(
ERROR,
"return type must be a row type");
617 if (missing_ok && errno == ENOENT)
622 while ((de =
ReadDir(dirdesc, dir)) != NULL)
630 if (de->d_name[0] ==
'.')
634 snprintf(path,
sizeof(path),
"%s/%s", dir, de->d_name);
635 if (
stat(path, &attrib) < 0)
642 errmsg(
"could not stat file \"%s\": %m", path)));
652 memset(nulls, 0,
sizeof(nulls));
685 (
errcode(ERRCODE_UNDEFINED_OBJECT),
686 errmsg(
"tablespace with OID %u does not exist",
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Datum pg_ls_logdir(PG_FUNCTION_ARGS)
static char * convert_and_check_filename(text *arg)
#define IsA(nodeptr, _type_)
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
int errhint(const char *fmt,...)
Datum pg_ls_tmpdir_noargs(PG_FUNCTION_ARGS)
TupleDesc CreateTemplateTupleDesc(int natts)
bool path_is_prefix_of_path(const char *path1, const char *path2)
Datum pg_read_file_all(PG_FUNCTION_ARGS)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
void canonicalize_path(char *path)
int errcode(int sqlerrcode)
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
#define PG_GETARG_BOOL(n)
#define PG_RETURN_BYTEA_P(x)
void TempTablespacePath(char *path, Oid tablespace)
bool path_contains_parent_reference(const char *path)
#define PG_GETARG_TEXT_PP(n)
#define SearchSysCacheExists1(cacheId, key1)
void pfree(void *pointer)
Datum pg_read_binary_file_off_len(PG_FUNCTION_ARGS)
static Datum pg_ls_tmpdir(FunctionCallInfo fcinfo, Oid tblspc)
Datum pg_stat_file(PG_FUNCTION_ARGS)
#define ObjectIdGetDatum(X)
Datum pg_read_file_off_len(PG_FUNCTION_ARGS)
#define TimestampTzGetDatum(X)
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
static bytea * read_binary_file(const char *filename, int64 seek_offset, int64 bytes_to_read, bool missing_ok)
int errcode_for_file_access(void)
#define is_absolute_path(filename)
bool path_is_relative_and_below_cwd(const char *path)
FILE * AllocateFile(const char *name, const char *mode)
void enlargeStringInfo(StringInfo str, int needed)
DIR * AllocateDir(const char *dirname)
Datum Int64GetDatum(int64 X)
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Datum pg_read_binary_file(PG_FUNCTION_ARGS)
Datum pg_ls_dir_1arg(PG_FUNCTION_ARGS)
void initStringInfo(StringInfo str)
static Datum pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir, bool missing_ok)
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Datum pg_ls_archive_statusdir(PG_FUNCTION_ARGS)
Datum pg_ls_dir(PG_FUNCTION_ARGS)
#define PG_RETURN_DATUM(x)
TimestampTz time_t_to_timestamptz(pg_time_t tm)
#define fseeko(stream, offset, origin)
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
#define ereport(elevel,...)
Datum pg_read_file_v2(PG_FUNCTION_ARGS)
bool is_member_of_role(Oid member, Oid role)
#define PG_RETURN_TEXT_P(x)
SetFunctionReturnMode returnMode
Datum pg_read_file(PG_FUNCTION_ARGS)
#define Assert(condition)
Datum pg_read_binary_file_all(PG_FUNCTION_ARGS)
struct dirent * ReadDir(DIR *dir, const char *dirname)
Datum pg_stat_file_1arg(PG_FUNCTION_ARGS)
#define HeapTupleGetDatum(tuple)
MemoryContext ecxt_per_query_memory
Tuplestorestate * setResult
static Datum values[MAXATTR]
char * text_to_cstring(const text *t)
int errmsg(const char *fmt,...)
Datum pg_ls_tmpdir_1arg(PG_FUNCTION_ARGS)
#define CStringGetTextDatum(s)
#define SET_VARSIZE(PTR, len)
static text * read_text_file(const char *filename, int64 seek_offset, int64 bytes_to_read, bool missing_ok)
#define PG_GETARG_INT64(n)
Datum pg_ls_waldir(PG_FUNCTION_ARGS)