Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * pgfnames.c
4 : : * directory handling functions
5 : : *
6 : : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : * IDENTIFICATION
10 : : * src/common/pgfnames.c
11 : : *
12 : : *-------------------------------------------------------------------------
13 : : */
14 : :
15 : : #ifndef FRONTEND
16 : : #include "postgres.h"
17 : : #else
18 : : #include "postgres_fe.h"
19 : : #endif
20 : :
21 : : #include <dirent.h>
22 : :
23 : : #ifndef FRONTEND
24 : : #define pg_log_warning(...) elog(WARNING, __VA_ARGS__)
25 : : #else
26 : : #include "common/logging.h"
27 : : #endif
28 : :
29 : : /*
30 : : * pgfnames
31 : : *
32 : : * return a list of the names of objects in the argument directory. Caller
33 : : * must call pgfnames_cleanup later to free the memory allocated by this
34 : : * function.
35 : : */
36 : : char **
4442 peter_e@gmx.net 37 :UBC 0 : pgfnames(const char *path)
38 : : {
39 : : DIR *dir;
40 : : struct dirent *file;
41 : : char **filenames;
42 : 0 : int numnames = 0;
43 : 0 : int fnsize = 200; /* enough for many small dbs */
44 : :
45 : 0 : dir = opendir(path);
46 [ # # ]: 0 : if (dir == NULL)
47 : : {
2451 peter@eisentraut.org 48 [ # # ]: 0 : pg_log_warning("could not open directory \"%s\": %m", path);
4442 peter_e@gmx.net 49 : 0 : return NULL;
50 : : }
51 : :
7 michael@paquier.xyz 52 :UNC 0 : filenames = palloc_array(char *, fnsize);
53 : :
4288 bruce@momjian.us 54 [ # # ]:UBC 0 : while (errno = 0, (file = readdir(dir)) != NULL)
55 : : {
4442 peter_e@gmx.net 56 [ # # # # ]: 0 : if (strcmp(file->d_name, ".") != 0 && strcmp(file->d_name, "..") != 0)
57 : : {
58 [ # # ]: 0 : if (numnames + 1 >= fnsize)
59 : : {
60 : 0 : fnsize *= 2;
7 michael@paquier.xyz 61 :UNC 0 : filenames = repalloc_array(filenames, char *, fnsize);
62 : : }
4442 peter_e@gmx.net 63 :UBC 0 : filenames[numnames++] = pstrdup(file->d_name);
64 : : }
65 : : }
66 : :
67 [ # # ]: 0 : if (errno)
2451 peter@eisentraut.org 68 [ # # ]: 0 : pg_log_warning("could not read directory \"%s\": %m", path);
69 : :
4442 peter_e@gmx.net 70 : 0 : filenames[numnames] = NULL;
71 : :
4288 bruce@momjian.us 72 [ # # ]: 0 : if (closedir(dir))
2451 peter@eisentraut.org 73 [ # # ]: 0 : pg_log_warning("could not close directory \"%s\": %m", path);
74 : :
4442 peter_e@gmx.net 75 : 0 : return filenames;
76 : : }
77 : :
78 : :
79 : : /*
80 : : * pgfnames_cleanup
81 : : *
82 : : * deallocate memory used for filenames
83 : : */
84 : : void
85 : 0 : pgfnames_cleanup(char **filenames)
86 : : {
87 : : char **fn;
88 : :
89 [ # # ]: 0 : for (fn = filenames; *fn; fn++)
90 : 0 : pfree(*fn);
91 : :
92 : 0 : pfree(filenames);
93 : 0 : }
|