|
23 | 23 | #include "access/htup_details.h"
|
24 | 24 | #include "access/xlog_internal.h"
|
25 | 25 | #include "catalog/pg_authid.h"
|
| 26 | +#include "catalog/pg_tablespace_d.h" |
26 | 27 | #include "catalog/pg_type.h"
|
27 | 28 | #include "funcapi.h"
|
28 | 29 | #include "mb/pg_wchar.h"
|
|
31 | 32 | #include "storage/fd.h"
|
32 | 33 | #include "utils/builtins.h"
|
33 | 34 | #include "utils/memutils.h"
|
| 35 | +#include "utils/syscache.h" |
34 | 36 | #include "utils/timestamp.h"
|
35 | 37 |
|
36 | 38 | typedef struct
|
@@ -520,7 +522,7 @@ pg_ls_dir_1arg(PG_FUNCTION_ARGS)
|
520 | 522 |
|
521 | 523 | /* Generic function to return a directory listing of files */
|
522 | 524 | static Datum
|
523 |
| -pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir) |
| 525 | +pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir, bool missing_ok) |
524 | 526 | {
|
525 | 527 | FuncCallContext *funcctx;
|
526 | 528 | struct dirent *de;
|
@@ -549,10 +551,18 @@ pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir)
|
549 | 551 | fctx->dirdesc = AllocateDir(fctx->location);
|
550 | 552 |
|
551 | 553 | if (!fctx->dirdesc)
|
552 |
| - ereport(ERROR, |
553 |
| - (errcode_for_file_access(), |
554 |
| - errmsg("could not open directory \"%s\": %m", |
555 |
| - fctx->location))); |
| 554 | + { |
| 555 | + if (missing_ok && errno == ENOENT) |
| 556 | + { |
| 557 | + MemoryContextSwitchTo(oldcontext); |
| 558 | + SRF_RETURN_DONE(funcctx); |
| 559 | + } |
| 560 | + else |
| 561 | + ereport(ERROR, |
| 562 | + (errcode_for_file_access(), |
| 563 | + errmsg("could not open directory \"%s\": %m", |
| 564 | + fctx->location))); |
| 565 | + } |
556 | 566 |
|
557 | 567 | funcctx->user_fctx = fctx;
|
558 | 568 | MemoryContextSwitchTo(oldcontext);
|
@@ -601,12 +611,50 @@ pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir)
|
601 | 611 | Datum
|
602 | 612 | pg_ls_logdir(PG_FUNCTION_ARGS)
|
603 | 613 | {
|
604 |
| - return pg_ls_dir_files(fcinfo, Log_directory); |
| 614 | + return pg_ls_dir_files(fcinfo, Log_directory, false); |
605 | 615 | }
|
606 | 616 |
|
607 | 617 | /* Function to return the list of files in the WAL directory */
|
608 | 618 | Datum
|
609 | 619 | pg_ls_waldir(PG_FUNCTION_ARGS)
|
610 | 620 | {
|
611 |
| - return pg_ls_dir_files(fcinfo, XLOGDIR); |
| 621 | + return pg_ls_dir_files(fcinfo, XLOGDIR, false); |
| 622 | +} |
| 623 | + |
| 624 | +/* |
| 625 | + * Generic function to return the list of files in pgsql_tmp |
| 626 | + */ |
| 627 | +static Datum |
| 628 | +pg_ls_tmpdir(FunctionCallInfo fcinfo, Oid tblspc) |
| 629 | +{ |
| 630 | + char path[MAXPGPATH]; |
| 631 | + |
| 632 | + if (!SearchSysCacheExists1(TABLESPACEOID, ObjectIdGetDatum(tblspc))) |
| 633 | + ereport(ERROR, |
| 634 | + (errcode(ERRCODE_UNDEFINED_OBJECT), |
| 635 | + errmsg("tablespace with OID %u does not exist", |
| 636 | + tblspc))); |
| 637 | + |
| 638 | + TempTablespacePath(path, tblspc); |
| 639 | + return pg_ls_dir_files(fcinfo, path, true); |
| 640 | +} |
| 641 | + |
| 642 | +/* |
| 643 | + * Function to return the list of temporary files in the pg_default tablespace's |
| 644 | + * pgsql_tmp directory |
| 645 | + */ |
| 646 | +Datum |
| 647 | +pg_ls_tmpdir_noargs(PG_FUNCTION_ARGS) |
| 648 | +{ |
| 649 | + return pg_ls_tmpdir(fcinfo, DEFAULTTABLESPACE_OID); |
| 650 | +} |
| 651 | + |
| 652 | +/* |
| 653 | + * Function to return the list of temporary files in the specified tablespace's |
| 654 | + * pgsql_tmp directory |
| 655 | + */ |
| 656 | +Datum |
| 657 | +pg_ls_tmpdir_1arg(PG_FUNCTION_ARGS) |
| 658 | +{ |
| 659 | + return pg_ls_tmpdir(fcinfo, PG_GETARG_OID(0)); |
612 | 660 | }
|
0 commit comments