PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pgfnames.c File Reference
#include "postgres.h"
#include <dirent.h>
Include dependency graph for pgfnames.c:

Go to the source code of this file.

Functions

char ** pgfnames (const char *path)
 
void pgfnames_cleanup (char **filenames)
 

Function Documentation

char** pgfnames ( const char *  path)

Definition at line 31 of file pgfnames.c.

References _, closedir(), dirent::d_name, elog, NULL, opendir(), palloc(), pstrdup(), readdir(), repalloc(), strerror(), and WARNING.

Referenced by convert_sourcefiles_in(), rmtree(), and scan_available_timezones().

32 {
33  DIR *dir;
34  struct dirent *file;
35  char **filenames;
36  int numnames = 0;
37  int fnsize = 200; /* enough for many small dbs */
38 
39  dir = opendir(path);
40  if (dir == NULL)
41  {
42 #ifndef FRONTEND
43  elog(WARNING, "could not open directory \"%s\": %m", path);
44 #else
45  fprintf(stderr, _("could not open directory \"%s\": %s\n"),
46  path, strerror(errno));
47 #endif
48  return NULL;
49  }
50 
51  filenames = (char **) palloc(fnsize * sizeof(char *));
52 
53  while (errno = 0, (file = readdir(dir)) != NULL)
54  {
55  if (strcmp(file->d_name, ".") != 0 && strcmp(file->d_name, "..") != 0)
56  {
57  if (numnames + 1 >= fnsize)
58  {
59  fnsize *= 2;
60  filenames = (char **) repalloc(filenames,
61  fnsize * sizeof(char *));
62  }
63  filenames[numnames++] = pstrdup(file->d_name);
64  }
65  }
66 
67  if (errno)
68  {
69 #ifndef FRONTEND
70  elog(WARNING, "could not read directory \"%s\": %m", path);
71 #else
72  fprintf(stderr, _("could not read directory \"%s\": %s\n"),
73  path, strerror(errno));
74 #endif
75  }
76 
77  filenames[numnames] = NULL;
78 
79  if (closedir(dir))
80  {
81 #ifndef FRONTEND
82  elog(WARNING, "could not close directory \"%s\": %m", path);
83 #else
84  fprintf(stderr, _("could not close directory \"%s\": %s\n"),
85  path, strerror(errno));
86 #endif
87  }
88 
89  return filenames;
90 }
char * pstrdup(const char *in)
Definition: mcxt.c:1165
int closedir(DIR *)
Definition: dirent.c:113
Definition: dirent.h:9
Definition: dirent.c:25
DIR * opendir(const char *)
Definition: dirent.c:33
#define WARNING
Definition: elog.h:40
#define NULL
Definition: c.h:226
struct dirent * readdir(DIR *)
Definition: dirent.c:78
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1021
void * palloc(Size size)
Definition: mcxt.c:891
const char * strerror(int errnum)
Definition: strerror.c:19
char d_name[MAX_PATH]
Definition: dirent.h:14
#define elog
Definition: elog.h:219
#define _(x)
Definition: elog.c:84
void pgfnames_cleanup ( char **  filenames)

Definition at line 99 of file pgfnames.c.

References fn(), and pfree().

Referenced by convert_sourcefiles_in(), rmtree(), and scan_available_timezones().

100 {
101  char **fn;
102 
103  for (fn = filenames; *fn; fn++)
104  pfree(*fn);
105 
106  pfree(filenames);
107 }
void pfree(void *pointer)
Definition: mcxt.c:992
static void * fn(void *arg)