1 /*-------------------------------------------------------------------------
4 * Functions for direct access to files
7 * Copyright (c) 2004-2006, PostgreSQL Global Development Group
9 * Author: Andreas Pflug <pgadmin@pse-consulting.de>
12 * $PostgreSQL: pgsql/src/backend/utils/adt/genfile.c,v 1.13 2006/11/24 21:18:42 tgl Exp $
14 *-------------------------------------------------------------------------
23 #include "access/heapam.h"
24 #include "catalog/pg_type.h"
26 #include "miscadmin.h"
27 #include "postmaster/syslogger.h"
28 #include "storage/fd.h"
29 #include "utils/builtins.h"
30 #include "utils/memutils.h"
31 #include "utils/timestamp.h"
41 * Convert a "text" filename argument to C string, and check it's allowable.
43 * Filename may be absolute or relative to the DataDir, but we only allow
44 * absolute paths that match DataDir or Log_directory.
47 convert_and_check_filename(text *arg)
49 int input_len = VARSIZE(arg) - VARHDRSZ;
50 char *filename = palloc(input_len + 1);
52 memcpy(filename, VARDATA(arg), input_len);
53 filename[input_len] = '\0';
55 canonicalize_path(filename); /* filename can change length here */
57 /* Disallow ".." in the path */
58 if (path_contains_parent_reference(filename))
60 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
61 (errmsg("reference to parent directory (\"..\") not allowed"))));
63 if (is_absolute_path(filename))
65 /* Allow absolute references within DataDir */
66 if (path_is_prefix_of_path(DataDir, filename))
68 /* The log directory might be outside our datadir, but allow it */
69 if (is_absolute_path(Log_directory) &&
70 path_is_prefix_of_path(Log_directory, filename))
74 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
75 (errmsg("absolute path not allowed"))));
76 return NULL; /* keep compiler quiet */
86 * Read a section of a file, returning it as text
89 pg_read_file(PG_FUNCTION_ARGS)
91 text *filename_t = PG_GETARG_TEXT_P(0);
92 int64 seek_offset = PG_GETARG_INT64(1);
93 int64 bytes_to_read = PG_GETARG_INT64(2);
101 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
102 (errmsg("must be superuser to read files"))));
104 filename = convert_and_check_filename(filename_t);
106 if ((file = AllocateFile(filename, PG_BINARY_R)) == NULL)
108 (errcode_for_file_access(),
109 errmsg("could not open file \"%s\" for reading: %m",
112 if (fseeko(file, (off_t) seek_offset,
113 (seek_offset >= 0) ? SEEK_SET : SEEK_END) != 0)
115 (errcode_for_file_access(),
116 errmsg("could not seek in file \"%s\": %m", filename)));
118 if (bytes_to_read < 0)
120 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
121 errmsg("requested length may not be negative")));
123 /* not sure why anyone thought that int64 length was a good idea */
124 if (bytes_to_read > (MaxAllocSize - VARHDRSZ))
126 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
127 errmsg("requested length too large")));
129 buf = palloc((Size) bytes_to_read + VARHDRSZ);
131 nbytes = fread(VARDATA(buf), 1, (size_t) bytes_to_read, file);
135 (errcode_for_file_access(),
136 errmsg("could not read file \"%s\": %m", filename)));
138 VARATT_SIZEP(buf) = nbytes + VARHDRSZ;
143 PG_RETURN_TEXT_P(buf);
150 pg_stat_file(PG_FUNCTION_ARGS)
152 text *filename_t = PG_GETARG_TEXT_P(0);
162 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
163 (errmsg("must be superuser to get file information"))));
165 filename = convert_and_check_filename(filename_t);
167 if (stat(filename, &fst) < 0)
169 (errcode_for_file_access(),
170 errmsg("could not stat file \"%s\": %m", filename)));
173 * This record type had better match the output parameters declared for me
176 tupdesc = CreateTemplateTupleDesc(6, false);
177 TupleDescInitEntry(tupdesc, (AttrNumber) 1,
178 "size", INT8OID, -1, 0);
179 TupleDescInitEntry(tupdesc, (AttrNumber) 2,
180 "access", TIMESTAMPTZOID, -1, 0);
181 TupleDescInitEntry(tupdesc, (AttrNumber) 3,
182 "modification", TIMESTAMPTZOID, -1, 0);
183 TupleDescInitEntry(tupdesc, (AttrNumber) 4,
184 "change", TIMESTAMPTZOID, -1, 0);
185 TupleDescInitEntry(tupdesc, (AttrNumber) 5,
186 "creation", TIMESTAMPTZOID, -1, 0);
187 TupleDescInitEntry(tupdesc, (AttrNumber) 6,
188 "isdir", BOOLOID, -1, 0);
189 BlessTupleDesc(tupdesc);
191 memset(isnull, false, sizeof(isnull));
193 values[0] = Int64GetDatum((int64) fst.st_size);
194 values[1] = TimestampTzGetDatum(time_t_to_timestamptz(fst.st_atime));
195 values[2] = TimestampTzGetDatum(time_t_to_timestamptz(fst.st_mtime));
196 /* Unix has file status change time, while Win32 has creation time */
197 #if !defined(WIN32) && !defined(__CYGWIN__)
198 values[3] = TimestampTzGetDatum(time_t_to_timestamptz(fst.st_ctime));
202 values[4] = TimestampTzGetDatum(time_t_to_timestamptz(fst.st_ctime));
204 values[5] = BoolGetDatum(fst.st_mode & S_IFDIR);
206 tuple = heap_form_tuple(tupdesc, values, isnull);
210 PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
215 * List a directory (returns the filenames only)
218 pg_ls_dir(PG_FUNCTION_ARGS)
220 FuncCallContext *funcctx;
222 directory_fctx *fctx;
226 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
227 (errmsg("must be superuser to get directory listings"))));
229 if (SRF_IS_FIRSTCALL())
231 MemoryContext oldcontext;
233 funcctx = SRF_FIRSTCALL_INIT();
234 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
236 fctx = palloc(sizeof(directory_fctx));
237 fctx->location = convert_and_check_filename(PG_GETARG_TEXT_P(0));
239 fctx->dirdesc = AllocateDir(fctx->location);
243 (errcode_for_file_access(),
244 errmsg("could not open directory \"%s\": %m",
247 funcctx->user_fctx = fctx;
248 MemoryContextSwitchTo(oldcontext);
251 funcctx = SRF_PERCALL_SETUP();
252 fctx = (directory_fctx *) funcctx->user_fctx;
254 while ((de = ReadDir(fctx->dirdesc, fctx->location)) != NULL)
256 int len = strlen(de->d_name);
259 if (strcmp(de->d_name, ".") == 0 ||
260 strcmp(de->d_name, "..") == 0)
263 result = palloc(len + VARHDRSZ);
264 VARATT_SIZEP(result) = len + VARHDRSZ;
265 memcpy(VARDATA(result), de->d_name, len);
267 SRF_RETURN_NEXT(funcctx, PointerGetDatum(result));
270 FreeDir(fctx->dirdesc);
272 SRF_RETURN_DONE(funcctx);