2005-08-12 05:25:13 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* genfile.c
|
2005-08-12 20:23:56 +02:00
|
|
|
* Functions for direct access to files
|
2005-08-12 05:25:13 +02:00
|
|
|
*
|
|
|
|
*
|
2017-01-03 19:48:53 +01:00
|
|
|
* Copyright (c) 2004-2017, PostgreSQL Global Development Group
|
2005-10-15 04:49:52 +02:00
|
|
|
*
|
2005-08-12 05:25:13 +02:00
|
|
|
* Author: Andreas Pflug <pgadmin@pse-consulting.de>
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/utils/adt/genfile.c
|
2005-08-12 05:25:13 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
2012-08-30 22:15:44 +02:00
|
|
|
#include "access/htup_details.h"
|
2005-08-12 05:25:13 +02:00
|
|
|
#include "catalog/pg_type.h"
|
|
|
|
#include "funcapi.h"
|
2010-01-05 02:29:36 +01:00
|
|
|
#include "mb/pg_wchar.h"
|
2005-08-12 20:23:56 +02:00
|
|
|
#include "miscadmin.h"
|
|
|
|
#include "postmaster/syslogger.h"
|
|
|
|
#include "storage/fd.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "utils/memutils.h"
|
2011-09-09 19:23:41 +02:00
|
|
|
#include "utils/timestamp.h"
|
2005-08-12 05:25:13 +02:00
|
|
|
|
2005-10-15 04:49:52 +02:00
|
|
|
typedef struct
|
2005-08-12 05:25:13 +02:00
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
char *location;
|
|
|
|
DIR *dirdesc;
|
2015-06-28 20:35:46 +02:00
|
|
|
bool include_dot_dirs;
|
2005-08-12 05:25:13 +02:00
|
|
|
} directory_fctx;
|
|
|
|
|
2005-08-12 20:23:56 +02:00
|
|
|
|
2005-08-12 05:25:13 +02:00
|
|
|
/*
|
2006-11-06 04:06:41 +01:00
|
|
|
* Convert a "text" filename argument to C string, and check it's allowable.
|
2005-08-12 20:23:56 +02:00
|
|
|
*
|
2006-11-06 04:06:41 +01:00
|
|
|
* Filename may be absolute or relative to the DataDir, but we only allow
|
|
|
|
* absolute paths that match DataDir or Log_directory.
|
2005-08-12 05:25:13 +02:00
|
|
|
*/
|
2005-08-12 20:23:56 +02:00
|
|
|
static char *
|
2006-11-06 04:06:41 +01:00
|
|
|
convert_and_check_filename(text *arg)
|
2005-08-12 05:25:13 +02:00
|
|
|
{
|
2008-03-25 23:42:46 +01:00
|
|
|
char *filename;
|
2005-08-12 05:25:13 +02:00
|
|
|
|
2008-03-25 23:42:46 +01:00
|
|
|
filename = text_to_cstring(arg);
|
2005-08-12 23:07:53 +02:00
|
|
|
canonicalize_path(filename); /* filename can change length here */
|
2005-08-12 05:25:13 +02:00
|
|
|
|
|
|
|
if (is_absolute_path(filename))
|
|
|
|
{
|
2011-02-12 15:47:51 +01:00
|
|
|
/* Disallow '/a/b/data/..' */
|
|
|
|
if (path_contains_parent_reference(filename))
|
|
|
|
ereport(ERROR,
|
2011-04-10 17:42:00 +02:00
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
(errmsg("reference to parent directory (\"..\") not allowed"))));
|
|
|
|
|
2011-02-12 15:47:51 +01:00
|
|
|
/*
|
2011-04-10 17:42:00 +02:00
|
|
|
* Allow absolute paths if within DataDir or Log_directory, even
|
|
|
|
* though Log_directory might be outside DataDir.
|
2011-02-12 15:47:51 +01:00
|
|
|
*/
|
|
|
|
if (!path_is_prefix_of_path(DataDir, filename) &&
|
|
|
|
(!is_absolute_path(Log_directory) ||
|
|
|
|
!path_is_prefix_of_path(Log_directory, filename)))
|
|
|
|
ereport(ERROR,
|
2011-04-10 17:42:00 +02:00
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
(errmsg("absolute path not allowed"))));
|
2005-08-12 05:25:13 +02:00
|
|
|
}
|
2011-02-12 15:47:51 +01:00
|
|
|
else if (!path_is_relative_and_below_cwd(filename))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
(errmsg("path must be in or below the current directory"))));
|
|
|
|
|
|
|
|
return filename;
|
2005-08-12 05:25:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-12 20:23:56 +02:00
|
|
|
/*
|
2010-12-15 22:56:28 +01:00
|
|
|
* Read a section of a file, returning it as bytea
|
|
|
|
*
|
2011-02-08 22:08:41 +01:00
|
|
|
* Caller is responsible for all permissions checking.
|
|
|
|
*
|
|
|
|
* We read the whole of the file when bytes_to_read is negative.
|
2005-08-12 20:23:56 +02:00
|
|
|
*/
|
2015-06-28 20:35:46 +02:00
|
|
|
static bytea *
|
|
|
|
read_binary_file(const char *filename, int64 seek_offset, int64 bytes_to_read,
|
|
|
|
bool missing_ok)
|
2005-08-12 05:25:13 +02:00
|
|
|
{
|
2010-12-15 22:56:28 +01:00
|
|
|
bytea *buf;
|
2005-08-12 05:25:13 +02:00
|
|
|
size_t nbytes;
|
2005-10-15 04:49:52 +02:00
|
|
|
FILE *file;
|
2005-08-12 05:25:13 +02:00
|
|
|
|
2010-12-15 22:56:28 +01:00
|
|
|
if (bytes_to_read < 0)
|
|
|
|
{
|
|
|
|
if (seek_offset < 0)
|
|
|
|
bytes_to_read = -seek_offset;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct stat fst;
|
|
|
|
|
|
|
|
if (stat(filename, &fst) < 0)
|
2015-06-28 20:35:46 +02:00
|
|
|
{
|
|
|
|
if (missing_ok && errno == ENOENT)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not stat file \"%s\": %m", filename)));
|
|
|
|
}
|
2010-12-15 22:56:28 +01:00
|
|
|
|
|
|
|
bytes_to_read = fst.st_size - seek_offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not sure why anyone thought that int64 length was a good idea */
|
|
|
|
if (bytes_to_read > (MaxAllocSize - VARHDRSZ))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("requested length too large")));
|
|
|
|
|
2005-08-12 05:25:13 +02:00
|
|
|
if ((file = AllocateFile(filename, PG_BINARY_R)) == NULL)
|
2015-06-28 20:35:46 +02:00
|
|
|
{
|
|
|
|
if (missing_ok && errno == ENOENT)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not open file \"%s\" for reading: %m",
|
|
|
|
filename)));
|
|
|
|
}
|
2005-08-12 05:25:13 +02:00
|
|
|
|
2005-08-12 20:23:56 +02:00
|
|
|
if (fseeko(file, (off_t) seek_offset,
|
|
|
|
(seek_offset >= 0) ? SEEK_SET : SEEK_END) != 0)
|
2005-08-12 05:25:13 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
2005-08-12 20:23:56 +02:00
|
|
|
errmsg("could not seek in file \"%s\": %m", filename)));
|
2005-08-12 05:25:13 +02:00
|
|
|
|
2010-12-15 22:56:28 +01:00
|
|
|
buf = (bytea *) palloc((Size) bytes_to_read + VARHDRSZ);
|
2005-08-12 05:25:13 +02:00
|
|
|
|
2005-08-12 20:23:56 +02:00
|
|
|
nbytes = fread(VARDATA(buf), 1, (size_t) bytes_to_read, file);
|
2005-08-12 05:25:13 +02:00
|
|
|
|
2005-10-15 21:47:09 +02:00
|
|
|
if (ferror(file))
|
2005-08-12 05:25:13 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
2005-08-12 20:23:56 +02:00
|
|
|
errmsg("could not read file \"%s\": %m", filename)));
|
|
|
|
|
2007-02-28 00:48:10 +01:00
|
|
|
SET_VARSIZE(buf, nbytes + VARHDRSZ);
|
2005-08-12 05:25:13 +02:00
|
|
|
|
|
|
|
FreeFile(file);
|
2005-08-12 20:23:56 +02:00
|
|
|
|
2010-12-15 22:56:28 +01:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-12-16 03:02:31 +01:00
|
|
|
* Similar to read_binary_file, but we verify that the contents are valid
|
2010-12-15 22:56:28 +01:00
|
|
|
* in the database encoding.
|
|
|
|
*/
|
|
|
|
static text *
|
2015-06-28 20:35:46 +02:00
|
|
|
read_text_file(const char *filename, int64 seek_offset, int64 bytes_to_read,
|
|
|
|
bool missing_ok)
|
2010-12-15 22:56:28 +01:00
|
|
|
{
|
2011-02-08 22:08:41 +01:00
|
|
|
bytea *buf;
|
|
|
|
|
2015-06-28 20:35:46 +02:00
|
|
|
buf = read_binary_file(filename, seek_offset, bytes_to_read, missing_ok);
|
2010-12-15 22:56:28 +01:00
|
|
|
|
2015-06-28 20:35:46 +02:00
|
|
|
if (buf != NULL)
|
|
|
|
{
|
|
|
|
/* Make sure the input is valid */
|
|
|
|
pg_verifymbstr(VARDATA(buf), VARSIZE(buf) - VARHDRSZ, false);
|
2010-12-15 22:56:28 +01:00
|
|
|
|
2015-06-28 20:35:46 +02:00
|
|
|
/* OK, we can cast it to text safely */
|
|
|
|
return (text *) buf;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return NULL;
|
2010-12-15 22:56:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a section of a file, returning it as text
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_read_file(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-03-13 00:35:34 +01:00
|
|
|
text *filename_t = PG_GETARG_TEXT_PP(0);
|
2015-06-28 20:35:46 +02:00
|
|
|
int64 seek_offset = 0;
|
|
|
|
int64 bytes_to_read = -1;
|
|
|
|
bool missing_ok = false;
|
2011-02-08 22:08:41 +01:00
|
|
|
char *filename;
|
2015-06-28 20:35:46 +02:00
|
|
|
text *result;
|
2011-02-08 22:08:41 +01:00
|
|
|
|
|
|
|
if (!superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
(errmsg("must be superuser to read files"))));
|
|
|
|
|
2015-06-28 20:35:46 +02:00
|
|
|
/* handle optional arguments */
|
|
|
|
if (PG_NARGS() >= 3)
|
|
|
|
{
|
|
|
|
seek_offset = PG_GETARG_INT64(1);
|
|
|
|
bytes_to_read = PG_GETARG_INT64(2);
|
2010-12-15 22:56:28 +01:00
|
|
|
|
2015-06-28 20:35:46 +02:00
|
|
|
if (bytes_to_read < 0)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("requested length cannot be negative")));
|
|
|
|
}
|
|
|
|
if (PG_NARGS() >= 4)
|
|
|
|
missing_ok = PG_GETARG_BOOL(3);
|
2011-02-08 22:08:41 +01:00
|
|
|
|
|
|
|
filename = convert_and_check_filename(filename_t);
|
|
|
|
|
2015-06-28 20:35:46 +02:00
|
|
|
result = read_text_file(filename, seek_offset, bytes_to_read, missing_ok);
|
|
|
|
if (result)
|
|
|
|
PG_RETURN_TEXT_P(result);
|
|
|
|
else
|
|
|
|
PG_RETURN_NULL();
|
2010-12-15 22:56:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a section of a file, returning it as bytea
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_read_binary_file(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-03-13 00:35:34 +01:00
|
|
|
text *filename_t = PG_GETARG_TEXT_PP(0);
|
2015-06-28 20:35:46 +02:00
|
|
|
int64 seek_offset = 0;
|
|
|
|
int64 bytes_to_read = -1;
|
|
|
|
bool missing_ok = false;
|
2011-02-08 22:08:41 +01:00
|
|
|
char *filename;
|
2015-06-28 20:35:46 +02:00
|
|
|
bytea *result;
|
2011-02-08 22:08:41 +01:00
|
|
|
|
|
|
|
if (!superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
(errmsg("must be superuser to read files"))));
|
|
|
|
|
2015-06-28 20:35:46 +02:00
|
|
|
/* handle optional arguments */
|
|
|
|
if (PG_NARGS() >= 3)
|
|
|
|
{
|
|
|
|
seek_offset = PG_GETARG_INT64(1);
|
|
|
|
bytes_to_read = PG_GETARG_INT64(2);
|
2010-12-15 22:56:28 +01:00
|
|
|
|
2015-06-28 20:35:46 +02:00
|
|
|
if (bytes_to_read < 0)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("requested length cannot be negative")));
|
|
|
|
}
|
|
|
|
if (PG_NARGS() >= 4)
|
|
|
|
missing_ok = PG_GETARG_BOOL(3);
|
|
|
|
|
|
|
|
filename = convert_and_check_filename(filename_t);
|
2010-12-15 22:56:28 +01:00
|
|
|
|
2015-06-28 20:35:46 +02:00
|
|
|
result = read_binary_file(filename, seek_offset,
|
|
|
|
bytes_to_read, missing_ok);
|
|
|
|
if (result)
|
|
|
|
PG_RETURN_BYTEA_P(result);
|
|
|
|
else
|
|
|
|
PG_RETURN_NULL();
|
2010-12-15 22:56:28 +01:00
|
|
|
}
|
|
|
|
|
2015-06-28 20:35:46 +02:00
|
|
|
|
2010-12-15 22:56:28 +01:00
|
|
|
/*
|
2015-06-28 20:35:46 +02:00
|
|
|
* Wrapper functions for the 1 and 3 argument variants of pg_read_file()
|
|
|
|
* and pg_binary_read_file().
|
|
|
|
*
|
|
|
|
* These are necessary to pass the sanity check in opr_sanity, which checks
|
|
|
|
* that all built-in functions that share the implementing C function take
|
|
|
|
* the same number of arguments.
|
2010-12-15 22:56:28 +01:00
|
|
|
*/
|
|
|
|
Datum
|
2015-06-28 20:35:46 +02:00
|
|
|
pg_read_file_off_len(PG_FUNCTION_ARGS)
|
2010-12-15 22:56:28 +01:00
|
|
|
{
|
2015-06-28 20:35:46 +02:00
|
|
|
return pg_read_file(fcinfo);
|
|
|
|
}
|
2011-02-08 22:08:41 +01:00
|
|
|
|
2015-06-28 20:35:46 +02:00
|
|
|
Datum
|
|
|
|
pg_read_file_all(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
return pg_read_file(fcinfo);
|
|
|
|
}
|
2011-02-08 22:08:41 +01:00
|
|
|
|
2015-06-28 20:35:46 +02:00
|
|
|
Datum
|
|
|
|
pg_read_binary_file_off_len(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
return pg_read_binary_file(fcinfo);
|
|
|
|
}
|
2010-12-15 22:56:28 +01:00
|
|
|
|
2015-06-28 20:35:46 +02:00
|
|
|
Datum
|
|
|
|
pg_read_binary_file_all(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
return pg_read_binary_file(fcinfo);
|
2005-08-12 05:25:13 +02:00
|
|
|
}
|
|
|
|
|
2005-08-12 20:23:56 +02:00
|
|
|
/*
|
|
|
|
* stat a file
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_stat_file(PG_FUNCTION_ARGS)
|
2005-08-12 05:25:13 +02:00
|
|
|
{
|
2017-03-13 00:35:34 +01:00
|
|
|
text *filename_t = PG_GETARG_TEXT_PP(0);
|
2005-10-15 04:49:52 +02:00
|
|
|
char *filename;
|
2005-08-12 05:25:13 +02:00
|
|
|
struct stat fst;
|
2005-08-16 01:00:14 +02:00
|
|
|
Datum values[6];
|
|
|
|
bool isnull[6];
|
2005-08-12 05:25:13 +02:00
|
|
|
HeapTuple tuple;
|
2005-08-12 20:23:56 +02:00
|
|
|
TupleDesc tupdesc;
|
2015-06-28 20:35:46 +02:00
|
|
|
bool missing_ok = false;
|
2005-08-12 05:25:13 +02:00
|
|
|
|
|
|
|
if (!superuser())
|
2005-10-15 04:49:52 +02:00
|
|
|
ereport(ERROR,
|
2005-08-12 05:25:13 +02:00
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
(errmsg("must be superuser to get file information"))));
|
|
|
|
|
2015-06-28 20:35:46 +02:00
|
|
|
/* check the optional argument */
|
|
|
|
if (PG_NARGS() == 2)
|
|
|
|
missing_ok = PG_GETARG_BOOL(1);
|
|
|
|
|
2006-11-06 04:06:41 +01:00
|
|
|
filename = convert_and_check_filename(filename_t);
|
2005-08-12 05:25:13 +02:00
|
|
|
|
|
|
|
if (stat(filename, &fst) < 0)
|
2015-06-28 20:35:46 +02:00
|
|
|
{
|
|
|
|
if (missing_ok && errno == ENOENT)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
else
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not stat file \"%s\": %m", filename)));
|
|
|
|
}
|
2005-08-12 20:23:56 +02:00
|
|
|
|
2005-08-13 21:02:34 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* This record type had better match the output parameters declared for me
|
2006-11-24 22:18:42 +01:00
|
|
|
* in pg_proc.h.
|
2005-08-13 21:02:34 +02:00
|
|
|
*/
|
2005-08-16 01:00:14 +02:00
|
|
|
tupdesc = CreateTemplateTupleDesc(6, false);
|
2005-08-12 20:23:56 +02:00
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 1,
|
2005-08-16 01:00:14 +02:00
|
|
|
"size", INT8OID, -1, 0);
|
2005-08-12 20:23:56 +02:00
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 2,
|
2005-08-16 01:00:14 +02:00
|
|
|
"access", TIMESTAMPTZOID, -1, 0);
|
2005-08-12 20:23:56 +02:00
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 3,
|
2005-08-16 01:00:14 +02:00
|
|
|
"modification", TIMESTAMPTZOID, -1, 0);
|
2005-08-12 20:23:56 +02:00
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 4,
|
2005-08-16 01:00:14 +02:00
|
|
|
"change", TIMESTAMPTZOID, -1, 0);
|
2005-08-12 20:23:56 +02:00
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 5,
|
2005-08-16 01:00:14 +02:00
|
|
|
"creation", TIMESTAMPTZOID, -1, 0);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 6,
|
2005-08-12 20:23:56 +02:00
|
|
|
"isdir", BOOLOID, -1, 0);
|
|
|
|
BlessTupleDesc(tupdesc);
|
|
|
|
|
2005-08-16 01:00:14 +02:00
|
|
|
memset(isnull, false, sizeof(isnull));
|
|
|
|
|
2005-08-12 20:23:56 +02:00
|
|
|
values[0] = Int64GetDatum((int64) fst.st_size);
|
|
|
|
values[1] = TimestampTzGetDatum(time_t_to_timestamptz(fst.st_atime));
|
|
|
|
values[2] = TimestampTzGetDatum(time_t_to_timestamptz(fst.st_mtime));
|
2005-08-16 01:00:14 +02:00
|
|
|
/* Unix has file status change time, while Win32 has creation time */
|
|
|
|
#if !defined(WIN32) && !defined(__CYGWIN__)
|
2005-08-12 20:23:56 +02:00
|
|
|
values[3] = TimestampTzGetDatum(time_t_to_timestamptz(fst.st_ctime));
|
2005-08-16 01:00:14 +02:00
|
|
|
isnull[4] = true;
|
|
|
|
#else
|
|
|
|
isnull[3] = true;
|
|
|
|
values[4] = TimestampTzGetDatum(time_t_to_timestamptz(fst.st_ctime));
|
|
|
|
#endif
|
Fix a number of places that were making file-type tests infelicitously.
The places that did, eg,
(statbuf.st_mode & S_IFMT) == S_IFDIR
were correct, but there is no good reason not to use S_ISDIR() instead,
especially when that's what the other 90% of our code does. The places
that did, eg,
(statbuf.st_mode & S_IFDIR)
were flat out *wrong* and would fail in various platform-specific ways,
eg a symlink could be mistaken for a regular file on most Unixen.
The actual impact of this is probably small, since the problem cases
seem to always involve symlinks or sockets, which are unlikely to be
found in the directories that PG code might be scanning. But it's
clearly trouble waiting to happen, so patch all the way back anyway.
(There seem to be no occurrences of the mistake in 7.4.)
2008-03-31 03:31:43 +02:00
|
|
|
values[5] = BoolGetDatum(S_ISDIR(fst.st_mode));
|
2005-08-12 20:23:56 +02:00
|
|
|
|
|
|
|
tuple = heap_form_tuple(tupdesc, values, isnull);
|
2005-08-12 05:25:13 +02:00
|
|
|
|
2005-08-12 20:23:56 +02:00
|
|
|
pfree(filename);
|
2005-08-12 05:25:13 +02:00
|
|
|
|
2005-08-12 20:23:56 +02:00
|
|
|
PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
|
2005-08-12 05:25:13 +02:00
|
|
|
}
|
|
|
|
|
2015-06-28 20:35:46 +02:00
|
|
|
/*
|
|
|
|
* stat a file (1 argument version)
|
|
|
|
*
|
|
|
|
* note: this wrapper is necessary to pass the sanity check in opr_sanity,
|
|
|
|
* which checks that all built-in functions that share the implementing C
|
|
|
|
* function take the same number of arguments
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_stat_file_1arg(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
return pg_stat_file(fcinfo);
|
|
|
|
}
|
2005-08-12 05:25:13 +02:00
|
|
|
|
2005-08-12 20:23:56 +02:00
|
|
|
/*
|
|
|
|
* List a directory (returns the filenames only)
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_ls_dir(PG_FUNCTION_ARGS)
|
2005-08-12 05:25:13 +02:00
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
FuncCallContext *funcctx;
|
|
|
|
struct dirent *de;
|
|
|
|
directory_fctx *fctx;
|
2015-06-28 20:35:46 +02:00
|
|
|
MemoryContext oldcontext;
|
2005-08-12 05:25:13 +02:00
|
|
|
|
|
|
|
if (!superuser())
|
2005-10-15 04:49:52 +02:00
|
|
|
ereport(ERROR,
|
2005-08-12 05:25:13 +02:00
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
(errmsg("must be superuser to get directory listings"))));
|
|
|
|
|
|
|
|
if (SRF_IS_FIRSTCALL())
|
|
|
|
{
|
2015-06-28 20:35:46 +02:00
|
|
|
bool missing_ok = false;
|
|
|
|
bool include_dot_dirs = false;
|
|
|
|
|
|
|
|
/* check the optional arguments */
|
|
|
|
if (PG_NARGS() == 3)
|
|
|
|
{
|
|
|
|
if (!PG_ARGISNULL(1))
|
|
|
|
missing_ok = PG_GETARG_BOOL(1);
|
|
|
|
if (!PG_ARGISNULL(2))
|
|
|
|
include_dot_dirs = PG_GETARG_BOOL(2);
|
|
|
|
}
|
2005-08-12 05:25:13 +02:00
|
|
|
|
|
|
|
funcctx = SRF_FIRSTCALL_INIT();
|
|
|
|
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
|
|
|
|
|
|
|
|
fctx = palloc(sizeof(directory_fctx));
|
2017-03-13 00:35:34 +01:00
|
|
|
fctx->location = convert_and_check_filename(PG_GETARG_TEXT_PP(0));
|
2005-08-12 05:25:13 +02:00
|
|
|
|
2015-06-28 20:35:46 +02:00
|
|
|
fctx->include_dot_dirs = include_dot_dirs;
|
2005-08-12 05:25:13 +02:00
|
|
|
fctx->dirdesc = AllocateDir(fctx->location);
|
|
|
|
|
|
|
|
if (!fctx->dirdesc)
|
2015-06-28 20:35:46 +02:00
|
|
|
{
|
|
|
|
if (missing_ok && errno == ENOENT)
|
|
|
|
{
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
SRF_RETURN_DONE(funcctx);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not open directory \"%s\": %m",
|
|
|
|
fctx->location)));
|
|
|
|
}
|
2005-08-12 05:25:13 +02:00
|
|
|
funcctx->user_fctx = fctx;
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
}
|
|
|
|
|
|
|
|
funcctx = SRF_PERCALL_SETUP();
|
2005-10-15 04:49:52 +02:00
|
|
|
fctx = (directory_fctx *) funcctx->user_fctx;
|
2005-08-12 05:25:13 +02:00
|
|
|
|
|
|
|
while ((de = ReadDir(fctx->dirdesc, fctx->location)) != NULL)
|
|
|
|
{
|
2015-06-28 20:35:46 +02:00
|
|
|
if (!fctx->include_dot_dirs &&
|
|
|
|
(strcmp(de->d_name, ".") == 0 ||
|
|
|
|
strcmp(de->d_name, "..") == 0))
|
2005-10-15 04:49:52 +02:00
|
|
|
continue;
|
2005-08-12 05:25:13 +02:00
|
|
|
|
2008-03-25 23:42:46 +01:00
|
|
|
SRF_RETURN_NEXT(funcctx, CStringGetTextDatum(de->d_name));
|
2005-08-12 05:25:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FreeDir(fctx->dirdesc);
|
2005-08-12 20:23:56 +02:00
|
|
|
|
2005-08-12 05:25:13 +02:00
|
|
|
SRF_RETURN_DONE(funcctx);
|
|
|
|
}
|
2015-06-28 20:35:46 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* List a directory (1 argument version)
|
|
|
|
*
|
|
|
|
* note: this wrapper is necessary to pass the sanity check in opr_sanity,
|
|
|
|
* which checks that all built-in functions that share the implementing C
|
|
|
|
* function take the same number of arguments.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_ls_dir_1arg(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
return pg_ls_dir(fcinfo);
|
|
|
|
}
|