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
|
|
|
*
|
|
|
|
*
|
2006-03-05 16:59:11 +01:00
|
|
|
* Copyright (c) 2004-2006, 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
|
2006-07-13 18:49:20 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/utils/adt/genfile.c,v 1.11 2006/07/13 16:49:16 momjian Exp $
|
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>
|
|
|
|
|
2005-08-12 20:23:56 +02:00
|
|
|
#include "access/heapam.h"
|
2005-08-12 05:25:13 +02:00
|
|
|
#include "catalog/pg_type.h"
|
|
|
|
#include "funcapi.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"
|
2006-07-13 18:49:20 +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;
|
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
|
|
|
/*
|
2005-08-12 20:23:56 +02:00
|
|
|
* Validate a path and convert to absolute form.
|
|
|
|
*
|
|
|
|
* Argument may be absolute or relative to the DataDir (but we only allow
|
2005-08-29 21:39:39 +02:00
|
|
|
* 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 *
|
|
|
|
check_and_make_absolute(text *arg)
|
2005-08-12 05:25:13 +02:00
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
int input_len = VARSIZE(arg) - VARHDRSZ;
|
|
|
|
char *filename = palloc(input_len + 1);
|
|
|
|
|
2005-08-12 23:07:53 +02:00
|
|
|
memcpy(filename, VARDATA(arg), input_len);
|
|
|
|
filename[input_len] = '\0';
|
2005-08-12 05:25:13 +02:00
|
|
|
|
2005-08-12 23:07:53 +02:00
|
|
|
canonicalize_path(filename); /* filename can change length here */
|
2005-08-12 05:25:13 +02:00
|
|
|
|
2005-08-12 23:07:53 +02:00
|
|
|
/* Disallow ".." in the path */
|
|
|
|
if (path_contains_parent_reference(filename))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("reference to parent directory (\"..\") not allowed"))));
|
2005-08-12 05:25:13 +02:00
|
|
|
|
|
|
|
if (is_absolute_path(filename))
|
|
|
|
{
|
2005-08-29 21:39:39 +02:00
|
|
|
/* Allow absolute references within DataDir */
|
|
|
|
if (path_is_prefix_of_path(DataDir, filename))
|
|
|
|
return filename;
|
2005-08-12 05:25:13 +02:00
|
|
|
/* The log directory might be outside our datadir, but allow it */
|
2005-08-29 21:39:39 +02:00
|
|
|
if (is_absolute_path(Log_directory) &&
|
|
|
|
path_is_prefix_of_path(Log_directory, filename))
|
2005-08-12 05:25:13 +02:00
|
|
|
return filename;
|
|
|
|
|
2005-10-15 04:49:52 +02:00
|
|
|
ereport(ERROR,
|
2005-08-12 05:25:13 +02:00
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
2005-08-12 20:23:56 +02:00
|
|
|
(errmsg("absolute path not allowed"))));
|
|
|
|
return NULL; /* keep compiler quiet */
|
2005-08-12 05:25:13 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
char *absname = palloc(strlen(DataDir) + strlen(filename) + 2);
|
|
|
|
|
2005-08-12 05:25:13 +02:00
|
|
|
sprintf(absname, "%s/%s", DataDir, filename);
|
|
|
|
pfree(filename);
|
|
|
|
return absname;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-12 20:23:56 +02:00
|
|
|
/*
|
|
|
|
* Read a section of a file, returning it as text
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_read_file(PG_FUNCTION_ARGS)
|
2005-08-12 05:25:13 +02:00
|
|
|
{
|
2005-08-12 20:23:56 +02:00
|
|
|
text *filename_t = PG_GETARG_TEXT_P(0);
|
2005-08-12 05:25:13 +02:00
|
|
|
int64 seek_offset = PG_GETARG_INT64(1);
|
2005-08-12 20:23:56 +02:00
|
|
|
int64 bytes_to_read = PG_GETARG_INT64(2);
|
2005-10-15 04:49:52 +02:00
|
|
|
char *buf;
|
2005-08-12 05:25:13 +02:00
|
|
|
size_t nbytes;
|
2005-10-15 04:49:52 +02:00
|
|
|
FILE *file;
|
|
|
|
char *filename;
|
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 read files"))));
|
|
|
|
|
2005-08-12 20:23:56 +02:00
|
|
|
filename = check_and_make_absolute(filename_t);
|
2005-08-12 05:25:13 +02:00
|
|
|
|
|
|
|
if ((file = AllocateFile(filename, PG_BINARY_R)) == NULL)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
2005-08-12 20:23:56 +02:00
|
|
|
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
|
|
|
|
|
|
|
if (bytes_to_read < 0)
|
|
|
|
ereport(ERROR,
|
2005-08-12 20:23:56 +02:00
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
2005-10-29 02:31:52 +02:00
|
|
|
errmsg("requested length may not be negative")));
|
2005-08-12 20:23:56 +02:00
|
|
|
|
|
|
|
/* 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-10-15 04:49:52 +02:00
|
|
|
|
2005-08-12 20:23:56 +02:00
|
|
|
buf = 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)));
|
|
|
|
|
2005-08-12 05:25:13 +02:00
|
|
|
VARATT_SIZEP(buf) = nbytes + VARHDRSZ;
|
|
|
|
|
|
|
|
FreeFile(file);
|
2005-08-12 20:23:56 +02:00
|
|
|
pfree(filename);
|
|
|
|
|
2005-08-12 05:25:13 +02:00
|
|
|
PG_RETURN_TEXT_P(buf);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2005-08-12 20:23:56 +02:00
|
|
|
text *filename_t = PG_GETARG_TEXT_P(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;
|
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"))));
|
|
|
|
|
2005-08-12 20:23:56 +02:00
|
|
|
filename = check_and_make_absolute(filename_t);
|
2005-08-12 05:25:13 +02:00
|
|
|
|
|
|
|
if (stat(filename, &fst) < 0)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
2005-08-12 20:23:56 +02:00
|
|
|
errmsg("could not stat file \"%s\": %m", filename)));
|
|
|
|
|
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
|
|
|
|
* in pg_proc.h (actually, in system_views.sql at the moment).
|
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
|
|
|
|
values[5] = BoolGetDatum(fst.st_mode & S_IFDIR);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
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())
|
|
|
|
{
|
|
|
|
MemoryContext oldcontext;
|
|
|
|
|
|
|
|
funcctx = SRF_FIRSTCALL_INIT();
|
|
|
|
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
|
|
|
|
|
|
|
|
fctx = palloc(sizeof(directory_fctx));
|
|
|
|
fctx->location = check_and_make_absolute(PG_GETARG_TEXT_P(0));
|
|
|
|
|
|
|
|
fctx->dirdesc = AllocateDir(fctx->location);
|
|
|
|
|
|
|
|
if (!fctx->dirdesc)
|
2005-10-15 04:49:52 +02:00
|
|
|
ereport(ERROR,
|
2005-08-12 05:25:13 +02:00
|
|
|
(errcode_for_file_access(),
|
2005-08-12 20:23:56 +02:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
int len = strlen(de->d_name);
|
2005-10-15 04:49:52 +02:00
|
|
|
text *result;
|
2005-08-12 05:25:13 +02:00
|
|
|
|
2005-08-12 20:23:56 +02:00
|
|
|
if (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
|
|
|
|
2005-08-12 20:23:56 +02:00
|
|
|
result = palloc(len + VARHDRSZ);
|
2005-08-12 05:25:13 +02:00
|
|
|
VARATT_SIZEP(result) = len + VARHDRSZ;
|
|
|
|
memcpy(VARDATA(result), de->d_name, len);
|
|
|
|
|
|
|
|
SRF_RETURN_NEXT(funcctx, PointerGetDatum(result));
|
|
|
|
}
|
|
|
|
|
|
|
|
FreeDir(fctx->dirdesc);
|
2005-08-12 20:23:56 +02:00
|
|
|
|
2005-08-12 05:25:13 +02:00
|
|
|
SRF_RETURN_DONE(funcctx);
|
|
|
|
}
|