Age Owner TLA Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * genfile.c
4 : * Functions for direct access to files
5 : *
6 : *
7 : * Copyright (c) 2004-2023, PostgreSQL Global Development Group
8 : *
9 : * Author: Andreas Pflug <pgadmin@pse-consulting.de>
10 : *
11 : * IDENTIFICATION
12 : * src/backend/utils/adt/genfile.c
13 : *
14 : *-------------------------------------------------------------------------
15 : */
16 : #include "postgres.h"
17 :
18 : #include <sys/file.h>
19 : #include <sys/stat.h>
20 : #include <unistd.h>
21 : #include <dirent.h>
22 :
23 : #include "access/htup_details.h"
24 : #include "access/xlog_internal.h"
25 : #include "catalog/pg_authid.h"
26 : #include "catalog/pg_tablespace_d.h"
27 : #include "catalog/pg_type.h"
28 : #include "funcapi.h"
29 : #include "mb/pg_wchar.h"
30 : #include "miscadmin.h"
31 : #include "postmaster/syslogger.h"
32 : #include "replication/slot.h"
33 : #include "storage/fd.h"
34 : #include "utils/acl.h"
35 : #include "utils/builtins.h"
36 : #include "utils/memutils.h"
37 : #include "utils/syscache.h"
38 : #include "utils/timestamp.h"
39 :
40 :
41 : /*
42 : * Convert a "text" filename argument to C string, and check it's allowable.
43 : *
44 : * Filename may be absolute or relative to the DataDir, but we only allow
45 : * absolute paths that match DataDir or Log_directory.
46 : *
47 : * This does a privilege check against the 'pg_read_server_files' role, so
48 : * this function is really only appropriate for callers who are only checking
49 : * 'read' access. Do not use this function if you are looking for a check
50 : * for 'write' or 'program' access without updating it to access the type
51 : * of check as an argument and checking the appropriate role membership.
52 : */
53 : static char *
5998 tgl 54 CBC 9609 : convert_and_check_filename(text *arg)
55 : {
56 : char *filename;
57 :
5493 58 9609 : filename = text_to_cstring(arg);
6449 59 9609 : 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 : */
377 mail 66 9609 : if (has_privs_of_role(GetUserId(), ROLE_PG_READ_SERVER_FILES))
1829 sfrost 67 2749 : 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 : */
6449 bruce 73 6860 : if (is_absolute_path(filename))
74 : {
75 : /*
76 : * Allow absolute paths if within DataDir or Log_directory, even
77 : * though Log_directory might be outside DataDir.
78 : */
4439 bruce 79 UBC 0 : if (!path_is_prefix_of_path(DataDir, filename) &&
80 0 : (!is_absolute_path(Log_directory) ||
81 0 : !path_is_prefix_of_path(Log_directory, filename)))
82 0 : ereport(ERROR,
83 : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
84 : errmsg("absolute path not allowed")));
85 : }
4439 bruce 86 CBC 6860 : else if (!path_is_relative_and_below_cwd(filename))
4439 bruce 87 UBC 0 : ereport(ERROR,
88 : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
89 : errmsg("path must be in or below the data directory")));
90 :
4439 bruce 91 CBC 6860 : return filename;
92 : }
93 :
94 :
95 : /*
96 : * Read a section of a file, returning it as bytea
97 : *
98 : * Caller is responsible for all permissions checking.
99 : *
100 : * We read the whole of the file when bytes_to_read is negative.
101 : */
102 : static bytea *
2842 heikki.linnakangas 103 2470 : read_binary_file(const char *filename, int64 seek_offset, int64 bytes_to_read,
104 : bool missing_ok)
105 : {
106 : bytea *buf;
1009 mail 107 2470 : size_t nbytes = 0;
108 : FILE *file;
109 :
110 : /* clamp request size to what we can actually deliver */
111 2470 : if (bytes_to_read > (int64) (MaxAllocSize - VARHDRSZ))
4497 itagaki.takahiro 112 UBC 0 : ereport(ERROR,
113 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
114 : errmsg("requested length too large")));
115 :
6449 bruce 116 CBC 2470 : if ((file = AllocateFile(filename, PG_BINARY_R)) == NULL)
117 : {
2842 heikki.linnakangas 118 14 : if (missing_ok && errno == ENOENT)
119 6 : return NULL;
120 : else
121 8 : ereport(ERROR,
122 : (errcode_for_file_access(),
123 : errmsg("could not open file \"%s\" for reading: %m",
124 : filename)));
125 : }
126 :
6449 tgl 127 2456 : if (fseeko(file, (off_t) seek_offset,
128 : (seek_offset >= 0) ? SEEK_SET : SEEK_END) != 0)
6449 bruce 129 UBC 0 : ereport(ERROR,
130 : (errcode_for_file_access(),
131 : errmsg("could not seek in file \"%s\": %m", filename)));
132 :
1009 mail 133 CBC 2456 : if (bytes_to_read >= 0)
134 : {
135 : /* If passed explicit read size just do it */
136 2427 : buf = (bytea *) palloc((Size) bytes_to_read + VARHDRSZ);
137 :
138 2427 : 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 29 : initStringInfo(&sbuf);
146 : /* Leave room in the buffer for the varlena length word */
147 29 : sbuf.len += VARHDRSZ;
148 29 : Assert(sbuf.len < sbuf.maxlen);
149 :
150 70 : 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 41 : if (sbuf.len == MaxAllocSize - 1)
165 : {
166 : char rbuf[1];
167 :
1009 mail 168 UBC 0 : if (fread(rbuf, 1, 1, file) != 0 || !feof(file))
169 0 : 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 */
1009 mail 177 CBC 41 : enlargeStringInfo(&sbuf, 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 41 : rbytes = fread(sbuf.data + sbuf.len, 1,
185 41 : (size_t) (sbuf.maxlen - sbuf.len - 1), file);
186 41 : sbuf.len += rbytes;
187 41 : nbytes += rbytes;
188 : }
189 :
190 : /* Now we can commandeer the stringinfo's buffer as the result */
191 29 : buf = (bytea *) sbuf.data;
192 : }
193 :
6385 tgl 194 2456 : if (ferror(file))
6449 bruce 195 UBC 0 : ereport(ERROR,
196 : (errcode_for_file_access(),
197 : errmsg("could not read file \"%s\": %m", filename)));
198 :
5885 tgl 199 CBC 2456 : SET_VARSIZE(buf, nbytes + VARHDRSZ);
200 :
6449 bruce 201 2456 : FreeFile(file);
202 :
4497 itagaki.takahiro 203 2456 : return buf;
204 : }
205 :
206 : /*
207 : * Similar to read_binary_file, but we verify that the contents are valid
208 : * in the database encoding.
209 : */
210 : static text *
2842 heikki.linnakangas 211 20 : read_text_file(const char *filename, int64 seek_offset, int64 bytes_to_read,
212 : bool missing_ok)
213 : {
214 : bytea *buf;
215 :
216 20 : buf = read_binary_file(filename, seek_offset, bytes_to_read, missing_ok);
217 :
218 15 : if (buf != NULL)
219 : {
220 : /* Make sure the input is valid */
221 12 : pg_verifymbstr(VARDATA(buf), VARSIZE(buf) - VARHDRSZ, false);
222 :
223 : /* OK, we can cast it to text safely */
224 12 : return (text *) buf;
225 : }
226 : else
227 3 : return NULL;
228 : }
229 :
230 : /*
231 : * Read a section of a file, returning it as text
232 : *
233 : * This function is kept to support adminpack 1.0.
234 : */
235 : Datum
4497 itagaki.takahiro 236 UBC 0 : pg_read_file(PG_FUNCTION_ARGS)
237 : {
2219 noah 238 0 : text *filename_t = PG_GETARG_TEXT_PP(0);
2842 heikki.linnakangas 239 0 : int64 seek_offset = 0;
240 0 : int64 bytes_to_read = -1;
241 0 : bool missing_ok = false;
242 : char *filename;
243 : text *result;
244 :
1829 sfrost 245 0 : if (!superuser())
246 0 : 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 0 : if (PG_NARGS() >= 3)
255 : {
256 0 : seek_offset = PG_GETARG_INT64(1);
257 0 : bytes_to_read = PG_GETARG_INT64(2);
258 :
259 0 : if (bytes_to_read < 0)
260 0 : ereport(ERROR,
261 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
262 : errmsg("requested length cannot be negative")));
263 : }
264 0 : if (PG_NARGS() >= 4)
265 0 : missing_ok = PG_GETARG_BOOL(3);
266 :
267 0 : filename = convert_and_check_filename(filename_t);
268 :
269 0 : result = read_text_file(filename, seek_offset, bytes_to_read, missing_ok);
270 0 : if (result)
271 0 : PG_RETURN_TEXT_P(result);
272 : else
273 0 : PG_RETURN_NULL();
274 : }
275 :
276 : /*
277 : * Read a section of a file, returning it as text
278 : *
279 : * No superuser check done here- instead privileges are handled by the
280 : * GRANT system.
281 : *
282 : * If read_to_eof is true, bytes_to_read must be -1, otherwise negative values
283 : * are not allowed for bytes_to_read.
284 : */
285 : static text *
254 tgl 286 GNC 26 : pg_read_file_common(text *filename_t, int64 seek_offset, int64 bytes_to_read,
287 : bool read_to_eof, bool missing_ok)
288 : {
289 26 : if (read_to_eof)
290 17 : Assert(bytes_to_read == -1);
291 9 : else if (bytes_to_read < 0)
292 6 : ereport(ERROR,
293 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
294 : errmsg("requested length cannot be negative")));
4443 tgl 295 ECB :
254 tgl 296 GNC 20 : return read_text_file(convert_and_check_filename(filename_t),
297 : seek_offset, bytes_to_read, missing_ok);
4497 itagaki.takahiro 298 ECB : }
299 :
300 : /*
301 : * Read a section of a file, returning it as bytea
302 : *
303 : * Parameters are interpreted the same as pg_read_file_common().
304 : */
305 : static bytea *
254 tgl 306 GNC 2456 : pg_read_binary_file_common(text *filename_t,
307 : int64 seek_offset, int64 bytes_to_read,
308 : bool read_to_eof, bool missing_ok)
309 : {
310 2456 : if (read_to_eof)
311 26 : Assert(bytes_to_read == -1);
312 2430 : else if (bytes_to_read < 0)
313 6 : ereport(ERROR,
314 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
315 : errmsg("requested length cannot be negative")));
316 :
317 2450 : return read_binary_file(convert_and_check_filename(filename_t),
318 : seek_offset, bytes_to_read, missing_ok);
4497 itagaki.takahiro 319 ECB : }
320 :
2842 heikki.linnakangas 321 :
322 : /*
323 : * Wrapper functions for the variants of SQL functions pg_read_file() and
324 : * pg_read_binary_file().
325 : *
326 : * These are necessary to pass the sanity check in opr_sanity, which checks
2842 heikki.linnakangas 327 EUB : * that all built-in functions that share the implementing C function take
328 : * the same number of arguments.
329 : */
4497 itagaki.takahiro 330 : Datum
2842 heikki.linnakangas 331 GIC 6 : pg_read_file_off_len(PG_FUNCTION_ARGS)
332 : {
254 tgl 333 GNC 6 : text *filename_t = PG_GETARG_TEXT_PP(0);
334 6 : int64 seek_offset = PG_GETARG_INT64(1);
335 6 : int64 bytes_to_read = PG_GETARG_INT64(2);
336 : text *ret;
337 :
338 6 : ret = pg_read_file_common(filename_t, seek_offset, bytes_to_read,
339 : false, false);
340 3 : if (!ret)
254 tgl 341 UNC 0 : PG_RETURN_NULL();
342 :
254 tgl 343 GNC 3 : PG_RETURN_TEXT_P(ret);
344 : }
345 :
346 : Datum
347 3 : pg_read_file_off_len_missing(PG_FUNCTION_ARGS)
348 : {
349 3 : text *filename_t = PG_GETARG_TEXT_PP(0);
350 3 : int64 seek_offset = PG_GETARG_INT64(1);
351 3 : int64 bytes_to_read = PG_GETARG_INT64(2);
352 3 : bool missing_ok = PG_GETARG_BOOL(3);
353 : text *ret;
354 :
355 3 : ret = pg_read_file_common(filename_t, seek_offset, bytes_to_read,
356 : false, missing_ok);
357 :
254 tgl 358 UNC 0 : if (!ret)
359 0 : PG_RETURN_NULL();
360 :
361 0 : PG_RETURN_TEXT_P(ret);
2842 heikki.linnakangas 362 ECB : }
363 :
364 : Datum
2842 heikki.linnakangas 365 GIC 14 : pg_read_file_all(PG_FUNCTION_ARGS)
366 : {
254 tgl 367 GNC 14 : text *filename_t = PG_GETARG_TEXT_PP(0);
368 : text *ret;
369 :
370 14 : ret = pg_read_file_common(filename_t, 0, -1, true, false);
371 :
372 9 : if (!ret)
254 tgl 373 UNC 0 : PG_RETURN_NULL();
374 :
254 tgl 375 GNC 9 : PG_RETURN_TEXT_P(ret);
376 : }
377 :
378 : Datum
379 3 : pg_read_file_all_missing(PG_FUNCTION_ARGS)
380 : {
381 3 : text *filename_t = PG_GETARG_TEXT_PP(0);
382 3 : bool missing_ok = PG_GETARG_BOOL(1);
383 : text *ret;
384 :
385 3 : ret = pg_read_file_common(filename_t, 0, -1, true, missing_ok);
386 :
387 3 : if (!ret)
388 3 : PG_RETURN_NULL();
389 :
254 tgl 390 UNC 0 : PG_RETURN_TEXT_P(ret);
391 : }
4443 tgl 392 ECB :
2842 heikki.linnakangas 393 EUB : Datum
2842 heikki.linnakangas 394 GIC 6 : pg_read_binary_file_off_len(PG_FUNCTION_ARGS)
2842 heikki.linnakangas 395 ECB : {
254 tgl 396 GNC 6 : text *filename_t = PG_GETARG_TEXT_PP(0);
397 6 : int64 seek_offset = PG_GETARG_INT64(1);
398 6 : int64 bytes_to_read = PG_GETARG_INT64(2);
399 : text *ret;
400 :
401 6 : ret = pg_read_binary_file_common(filename_t, seek_offset, bytes_to_read,
402 : false, false);
403 3 : if (!ret)
254 tgl 404 UNC 0 : PG_RETURN_NULL();
405 :
254 tgl 406 GNC 3 : PG_RETURN_BYTEA_P(ret);
407 : }
408 :
409 : Datum
410 2424 : pg_read_binary_file_off_len_missing(PG_FUNCTION_ARGS)
411 : {
412 2424 : text *filename_t = PG_GETARG_TEXT_PP(0);
413 2424 : int64 seek_offset = PG_GETARG_INT64(1);
414 2424 : int64 bytes_to_read = PG_GETARG_INT64(2);
415 2424 : bool missing_ok = PG_GETARG_BOOL(3);
416 : text *ret;
417 :
418 2424 : ret = pg_read_binary_file_common(filename_t, seek_offset, bytes_to_read,
419 : false, missing_ok);
420 2421 : if (!ret)
254 tgl 421 UNC 0 : PG_RETURN_NULL();
422 :
254 tgl 423 GNC 2421 : PG_RETURN_BYTEA_P(ret);
424 : }
425 :
2842 heikki.linnakangas 426 ECB : Datum
2842 heikki.linnakangas 427 GIC 23 : pg_read_binary_file_all(PG_FUNCTION_ARGS)
2842 heikki.linnakangas 428 ECB : {
254 tgl 429 GNC 23 : text *filename_t = PG_GETARG_TEXT_PP(0);
430 : text *ret;
431 :
432 23 : ret = pg_read_binary_file_common(filename_t, 0, -1, true, false);
433 :
434 20 : if (!ret)
254 tgl 435 UNC 0 : PG_RETURN_NULL();
436 :
254 tgl 437 GNC 20 : PG_RETURN_BYTEA_P(ret);
438 : }
439 :
440 : Datum
441 3 : pg_read_binary_file_all_missing(PG_FUNCTION_ARGS)
442 : {
443 3 : text *filename_t = PG_GETARG_TEXT_PP(0);
444 3 : bool missing_ok = PG_GETARG_BOOL(1);
445 : text *ret;
446 :
447 3 : ret = pg_read_binary_file_common(filename_t, 0, -1, true, missing_ok);
448 :
449 3 : if (!ret)
450 3 : PG_RETURN_NULL();
451 :
254 tgl 452 UNC 0 : PG_RETURN_BYTEA_P(ret);
453 : }
454 :
6449 tgl 455 ECB : /*
456 : * stat a file
457 : */
458 : Datum
6449 tgl 459 GIC 6952 : pg_stat_file(PG_FUNCTION_ARGS)
6449 bruce 460 EUB : {
2219 noah 461 GIC 6952 : text *filename_t = PG_GETARG_TEXT_PP(0);
462 : char *filename;
463 : struct stat fst;
6446 bruce 464 ECB : Datum values[6];
465 : bool isnull[6];
6449 466 : HeapTuple tuple;
tgl 467 : TupleDesc tupdesc;
2842 heikki.linnakangas 468 CBC 6952 : bool missing_ok = false;
469 :
470 : /* check the optional argument */
471 6952 : if (PG_NARGS() == 2)
2842 heikki.linnakangas 472 GIC 6947 : missing_ok = PG_GETARG_BOOL(1);
2842 heikki.linnakangas 473 ECB :
5998 tgl 474 GBC 6952 : filename = convert_and_check_filename(filename_t);
475 :
6449 bruce 476 CBC 6952 : if (stat(filename, &fst) < 0)
477 : {
2842 heikki.linnakangas 478 GIC 1 : if (missing_ok && errno == ENOENT)
2842 heikki.linnakangas 479 UIC 0 : PG_RETURN_NULL();
2842 heikki.linnakangas 480 ECB : else
2842 heikki.linnakangas 481 GIC 1 : ereport(ERROR,
2842 heikki.linnakangas 482 ECB : (errcode_for_file_access(),
483 : errmsg("could not stat file \"%s\": %m", filename)));
484 : }
6449 tgl 485 :
486 : /*
487 : * This record type had better match the output parameters declared for me
5980 488 : * in pg_proc.h.
489 : */
1601 andres 490 CBC 6951 : tupdesc = CreateTemplateTupleDesc(6);
6449 tgl 491 GBC 6951 : TupleDescInitEntry(tupdesc, (AttrNumber) 1,
492 : "size", INT8OID, -1, 0);
6449 tgl 493 CBC 6951 : TupleDescInitEntry(tupdesc, (AttrNumber) 2,
494 : "access", TIMESTAMPTZOID, -1, 0);
6449 tgl 495 GIC 6951 : TupleDescInitEntry(tupdesc, (AttrNumber) 3,
496 : "modification", TIMESTAMPTZOID, -1, 0);
6449 tgl 497 CBC 6951 : TupleDescInitEntry(tupdesc, (AttrNumber) 4,
498 : "change", TIMESTAMPTZOID, -1, 0);
499 6951 : TupleDescInitEntry(tupdesc, (AttrNumber) 5,
500 : "creation", TIMESTAMPTZOID, -1, 0);
6446 bruce 501 GIC 6951 : TupleDescInitEntry(tupdesc, (AttrNumber) 6,
6449 tgl 502 ECB : "isdir", BOOLOID, -1, 0);
6449 tgl 503 GIC 6951 : BlessTupleDesc(tupdesc);
6449 tgl 504 ECB :
6446 bruce 505 GBC 6951 : memset(isnull, false, sizeof(isnull));
506 :
6449 tgl 507 CBC 6951 : values[0] = Int64GetDatum((int64) fst.st_size);
6449 tgl 508 GIC 6951 : values[1] = TimestampTzGetDatum(time_t_to_timestamptz(fst.st_atime));
509 6951 : values[2] = TimestampTzGetDatum(time_t_to_timestamptz(fst.st_mtime));
510 : /* Unix has file status change time, while Win32 has creation time */
6446 bruce 511 ECB : #if !defined(WIN32) && !defined(__CYGWIN__)
6449 tgl 512 GIC 6951 : values[3] = TimestampTzGetDatum(time_t_to_timestamptz(fst.st_ctime));
6446 bruce 513 CBC 6951 : isnull[4] = true;
6446 bruce 514 ECB : #else
515 : isnull[3] = true;
516 : values[4] = TimestampTzGetDatum(time_t_to_timestamptz(fst.st_ctime));
517 : #endif
5487 tgl 518 GIC 6951 : values[5] = BoolGetDatum(S_ISDIR(fst.st_mode));
6449 tgl 519 ECB :
6449 tgl 520 CBC 6951 : tuple = heap_form_tuple(tupdesc, values, isnull);
521 :
6449 tgl 522 GBC 6951 : pfree(filename);
523 :
6449 tgl 524 GIC 6951 : PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
525 : }
526 :
527 : /*
528 : * stat a file (1 argument version)
2842 heikki.linnakangas 529 ECB : *
530 : * note: this wrapper is necessary to pass the sanity check in opr_sanity,
531 : * which checks that all built-in functions that share the implementing C
532 : * function take the same number of arguments
533 : */
534 : Datum
2842 heikki.linnakangas 535 GIC 5 : pg_stat_file_1arg(PG_FUNCTION_ARGS)
536 : {
537 5 : return pg_stat_file(fcinfo);
2842 heikki.linnakangas 538 ECB : }
539 :
540 : /*
6449 tgl 541 : * List a directory (returns the filenames only)
542 : */
543 : Datum
6449 tgl 544 CBC 187 : pg_ls_dir(PG_FUNCTION_ARGS)
545 : {
1119 546 187 : ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
547 : char *location;
548 187 : bool missing_ok = false;
1119 tgl 549 GBC 187 : bool include_dot_dirs = false;
550 : DIR *dirdesc;
6385 bruce 551 ECB : struct dirent *de;
552 :
1119 tgl 553 GIC 187 : location = convert_and_check_filename(PG_GETARG_TEXT_PP(0));
554 :
555 : /* check the optional arguments */
556 187 : if (PG_NARGS() == 3)
557 : {
558 180 : if (!PG_ARGISNULL(1))
559 180 : missing_ok = PG_GETARG_BOOL(1);
1119 tgl 560 CBC 180 : if (!PG_ARGISNULL(2))
561 180 : include_dot_dirs = PG_GETARG_BOOL(2);
562 : }
2842 heikki.linnakangas 563 ECB :
173 michael 564 GIC 187 : InitMaterializedSRF(fcinfo, MAT_SRF_USE_EXPECTED_DESC);
6449 bruce 565 ECB :
1119 tgl 566 GIC 187 : dirdesc = AllocateDir(location);
1119 tgl 567 CBC 187 : if (!dirdesc)
568 : {
1119 tgl 569 ECB : /* Return empty tuplestore if appropriate */
1119 tgl 570 GIC 6 : if (missing_ok && errno == ENOENT)
1119 tgl 571 CBC 3 : return (Datum) 0;
572 : /* Otherwise, we can let ReadDir() throw the error */
6449 bruce 573 ECB : }
574 :
1119 tgl 575 CBC 7790 : while ((de = ReadDir(dirdesc, location)) != NULL)
576 : {
1119 tgl 577 ECB : Datum values[1];
578 : bool nulls[1];
579 :
1119 tgl 580 GIC 7606 : if (!include_dot_dirs &&
2842 heikki.linnakangas 581 7528 : (strcmp(de->d_name, ".") == 0 ||
2842 heikki.linnakangas 582 CBC 7350 : strcmp(de->d_name, "..") == 0))
6385 bruce 583 356 : continue;
584 :
1119 tgl 585 GIC 7250 : values[0] = CStringGetTextDatum(de->d_name);
586 7250 : nulls[0] = false;
587 :
398 michael 588 CBC 7250 : tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
589 : values, nulls);
1119 tgl 590 ECB : }
591 :
1119 tgl 592 CBC 181 : FreeDir(dirdesc);
1119 tgl 593 GIC 181 : return (Datum) 0;
6449 bruce 594 ECB : }
595 :
596 : /*
597 : * List a directory (1 argument version)
598 : *
599 : * note: this wrapper is necessary to pass the sanity check in opr_sanity,
600 : * which checks that all built-in functions that share the implementing C
601 : * function take the same number of arguments.
602 : */
603 : Datum
2842 heikki.linnakangas 604 GIC 7 : pg_ls_dir_1arg(PG_FUNCTION_ARGS)
2842 heikki.linnakangas 605 ECB : {
2842 heikki.linnakangas 606 GIC 7 : return pg_ls_dir(fcinfo);
2842 heikki.linnakangas 607 ECB : }
608 :
609 : /*
610 : * Generic function to return a directory listing of files.
611 : *
612 : * If the directory isn't there, silently return an empty set if missing_ok.
613 : * Other unreadable-directory cases throw an error.
1124 tgl 614 : */
615 : static Datum
1647 michael 616 CBC 18 : pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir, bool missing_ok)
617 : {
1124 tgl 618 18 : ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1124 tgl 619 ECB : DIR *dirdesc;
620 : struct dirent *de;
621 :
173 michael 622 GIC 18 : InitMaterializedSRF(fcinfo, 0);
2215 rhaas 623 ECB :
624 : /*
625 : * Now walk the directory. Note that we must do this within a single SRF
1124 tgl 626 : * call, not leave the directory open across multiple calls, since we
627 : * can't count on the SRF being run to completion.
628 : */
1124 tgl 629 CBC 18 : dirdesc = AllocateDir(dir);
630 18 : if (!dirdesc)
1124 tgl 631 ECB : {
632 : /* Return empty tuplestore if appropriate */
1124 tgl 633 UIC 0 : if (missing_ok && errno == ENOENT)
1124 tgl 634 LBC 0 : return (Datum) 0;
635 : /* Otherwise, we can let ReadDir() throw the error */
2215 rhaas 636 ECB : }
637 :
1124 tgl 638 GIC 473 : while ((de = ReadDir(dirdesc, dir)) != NULL)
639 : {
2215 rhaas 640 ECB : Datum values[3];
641 : bool nulls[3];
642 : char path[MAXPGPATH * 2];
643 : struct stat attrib;
644 :
645 : /* Skip hidden files */
2215 rhaas 646 GIC 455 : if (de->d_name[0] == '.')
647 48 : continue;
648 :
649 : /* Get the file info */
1124 tgl 650 CBC 419 : snprintf(path, sizeof(path), "%s/%s", dir, de->d_name);
2215 rhaas 651 419 : if (stat(path, &attrib) < 0)
1104 tgl 652 ECB : {
653 : /* Ignore concurrently-deleted files, else complain */
1104 tgl 654 UIC 0 : if (errno == ENOENT)
1104 tgl 655 LBC 0 : continue;
2215 rhaas 656 0 : ereport(ERROR,
657 : (errcode_for_file_access(),
1124 tgl 658 ECB : errmsg("could not stat file \"%s\": %m", path)));
659 : }
660 :
661 : /* Ignore anything but regular files */
2215 rhaas 662 CBC 419 : if (!S_ISREG(attrib.st_mode))
663 12 : continue;
664 :
2215 rhaas 665 GIC 407 : values[0] = CStringGetTextDatum(de->d_name);
666 407 : values[1] = Int64GetDatum((int64) attrib.st_size);
667 407 : values[2] = TimestampTzGetDatum(time_t_to_timestamptz(attrib.st_mtime));
668 407 : memset(nulls, 0, sizeof(nulls));
669 :
398 michael 670 407 : tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
671 : }
672 :
1124 tgl 673 18 : FreeDir(dirdesc);
1124 tgl 674 CBC 18 : return (Datum) 0;
675 : }
2215 rhaas 676 ECB :
677 : /* Function to return the list of files in the log directory */
678 : Datum
2215 rhaas 679 UIC 0 : pg_ls_logdir(PG_FUNCTION_ARGS)
680 : {
1647 michael 681 0 : return pg_ls_dir_files(fcinfo, Log_directory, false);
682 : }
683 :
684 : /* Function to return the list of files in the WAL directory */
685 : Datum
2215 rhaas 686 CBC 12 : pg_ls_waldir(PG_FUNCTION_ARGS)
687 : {
1647 michael 688 12 : return pg_ls_dir_files(fcinfo, XLOGDIR, false);
689 : }
690 :
691 : /*
1647 michael 692 ECB : * Generic function to return the list of files in pgsql_tmp
693 : */
694 : static Datum
1647 michael 695 UIC 0 : pg_ls_tmpdir(FunctionCallInfo fcinfo, Oid tblspc)
696 : {
697 : char path[MAXPGPATH];
698 :
1647 michael 699 LBC 0 : if (!SearchSysCacheExists1(TABLESPACEOID, ObjectIdGetDatum(tblspc)))
700 0 : ereport(ERROR,
701 : (errcode(ERRCODE_UNDEFINED_OBJECT),
702 : errmsg("tablespace with OID %u does not exist",
1647 michael 703 EUB : tblspc)));
704 :
1647 michael 705 UIC 0 : TempTablespacePath(path, tblspc);
706 0 : return pg_ls_dir_files(fcinfo, path, true);
707 : }
1647 michael 708 ECB :
709 : /*
710 : * Function to return the list of temporary files in the pg_default tablespace's
711 : * pgsql_tmp directory
712 : */
713 : Datum
1647 michael 714 UIC 0 : pg_ls_tmpdir_noargs(PG_FUNCTION_ARGS)
715 : {
1647 michael 716 LBC 0 : return pg_ls_tmpdir(fcinfo, DEFAULTTABLESPACE_OID);
1647 michael 717 ECB : }
718 :
719 : /*
720 : * Function to return the list of temporary files in the specified tablespace's
721 : * pgsql_tmp directory
722 : */
723 : Datum
1647 michael 724 UBC 0 : pg_ls_tmpdir_1arg(PG_FUNCTION_ARGS)
1647 michael 725 EUB : {
1647 michael 726 UBC 0 : return pg_ls_tmpdir(fcinfo, PG_GETARG_OID(0));
727 : }
728 :
729 : /*
730 : * Function to return the list of files in the WAL archive status directory.
731 : */
1643 michael 732 ECB : Datum
1643 michael 733 CBC 3 : pg_ls_archive_statusdir(PG_FUNCTION_ARGS)
734 : {
735 3 : return pg_ls_dir_files(fcinfo, XLOGDIR "/archive_status", true);
1643 michael 736 ECB : }
502 737 :
738 : /*
739 : * Function to return the list of files in the pg_logical/snapshots directory.
740 : */
741 : Datum
502 michael 742 GIC 1 : pg_ls_logicalsnapdir(PG_FUNCTION_ARGS)
502 michael 743 ECB : {
502 michael 744 CBC 1 : return pg_ls_dir_files(fcinfo, "pg_logical/snapshots", false);
745 : }
746 :
747 : /*
748 : * Function to return the list of files in the pg_logical/mappings directory.
502 michael 749 EUB : */
750 : Datum
502 michael 751 GBC 1 : pg_ls_logicalmapdir(PG_FUNCTION_ARGS)
752 : {
502 michael 753 GIC 1 : return pg_ls_dir_files(fcinfo, "pg_logical/mappings", false);
754 : }
755 :
502 michael 756 ECB : /*
757 : * Function to return the list of files in the pg_replslot/<replication_slot>
758 : * directory.
759 : */
760 : Datum
502 michael 761 GIC 2 : pg_ls_replslotdir(PG_FUNCTION_ARGS)
762 : {
763 : text *slotname_t;
764 : char path[MAXPGPATH];
502 michael 765 EUB : char *slotname;
766 :
502 michael 767 GIC 2 : slotname_t = PG_GETARG_TEXT_PP(0);
768 :
502 michael 769 GBC 2 : slotname = text_to_cstring(slotname_t);
502 michael 770 EUB :
502 michael 771 GIC 2 : if (!SearchNamedReplicationSlot(slotname, true))
772 1 : ereport(ERROR,
773 : (errcode(ERRCODE_UNDEFINED_OBJECT),
774 : errmsg("replication slot \"%s\" does not exist",
502 michael 775 EUB : slotname)));
776 :
502 michael 777 GIC 1 : snprintf(path, sizeof(path), "pg_replslot/%s", slotname);
778 1 : return pg_ls_dir_files(fcinfo, path, false);
779 : }
|