PostgreSQL Source Code  git master
rmtree.c File Reference
#include "postgres.h"
#include <unistd.h>
#include <sys/stat.h>
#include "common/file_utils.h"
#include "storage/fd.h"
Include dependency graph for rmtree.c:

Go to the source code of this file.

Macros

#define pg_log_warning(...)   elog(WARNING, __VA_ARGS__)
 
#define LOG_LEVEL   WARNING
 
#define OPENDIR(x)   AllocateDir(x)
 
#define CLOSEDIR(x)   FreeDir(x)
 

Functions

bool rmtree (const char *path, bool rmtopdir)
 

Macro Definition Documentation

◆ CLOSEDIR

#define CLOSEDIR (   x)    FreeDir(x)

Definition at line 30 of file rmtree.c.

◆ LOG_LEVEL

#define LOG_LEVEL   WARNING

Definition at line 28 of file rmtree.c.

◆ OPENDIR

#define OPENDIR (   x)    AllocateDir(x)

Definition at line 29 of file rmtree.c.

◆ pg_log_warning

#define pg_log_warning (   ...)    elog(WARNING, __VA_ARGS__)

Definition at line 27 of file rmtree.c.

Function Documentation

◆ rmtree()

bool rmtree ( const char *  path,
bool  rmtopdir 
)

Definition at line 50 of file rmtree.c.

51 {
52  char pathbuf[MAXPGPATH];
53  DIR *dir;
54  struct dirent *de;
55  bool result = true;
56  size_t dirnames_size = 0;
57  size_t dirnames_capacity = 8;
58  char **dirnames;
59 
60  dir = OPENDIR(path);
61  if (dir == NULL)
62  {
63  pg_log_warning("could not open directory \"%s\": %m", path);
64  return false;
65  }
66 
67  dirnames = (char **) palloc(sizeof(char *) * dirnames_capacity);
68 
69  while (errno = 0, (de = readdir(dir)))
70  {
71  if (strcmp(de->d_name, ".") == 0 ||
72  strcmp(de->d_name, "..") == 0)
73  continue;
74  snprintf(pathbuf, sizeof(pathbuf), "%s/%s", path, de->d_name);
75  switch (get_dirent_type(pathbuf, de, false, LOG_LEVEL))
76  {
77  case PGFILETYPE_ERROR:
78  /* already logged, press on */
79  break;
80  case PGFILETYPE_DIR:
81 
82  /*
83  * Defer recursion until after we've closed this directory, to
84  * avoid using more than one file descriptor at a time.
85  */
86  if (dirnames_size == dirnames_capacity)
87  {
88  dirnames = repalloc(dirnames,
89  sizeof(char *) * dirnames_capacity * 2);
90  dirnames_capacity *= 2;
91  }
92  dirnames[dirnames_size++] = pstrdup(pathbuf);
93  break;
94  default:
95  if (unlink(pathbuf) != 0 && errno != ENOENT)
96  {
97  pg_log_warning("could not remove file \"%s\": %m", pathbuf);
98  result = false;
99  }
100  break;
101  }
102  }
103 
104  if (errno != 0)
105  {
106  pg_log_warning("could not read directory \"%s\": %m", path);
107  result = false;
108  }
109 
110  CLOSEDIR(dir);
111 
112  /* Now recurse into the subdirectories we found. */
113  for (size_t i = 0; i < dirnames_size; ++i)
114  {
115  if (!rmtree(dirnames[i], true))
116  result = false;
117  pfree(dirnames[i]);
118  }
119 
120  if (rmtopdir)
121  {
122  if (rmdir(path) != 0)
123  {
124  pg_log_warning("could not remove directory \"%s\": %m", path);
125  result = false;
126  }
127  }
128 
129  pfree(dirnames);
130 
131  return result;
132 }
struct dirent * readdir(DIR *)
Definition: dirent.c:78
PGFileType get_dirent_type(const char *path, const struct dirent *de, bool look_through_symlinks, int elevel)
Definition: file_utils.c:525
@ PGFILETYPE_DIR
Definition: file_utils.h:23
@ PGFILETYPE_ERROR
Definition: file_utils.h:20
int i
Definition: isn.c:73
char * pstrdup(const char *in)
Definition: mcxt.c:1695
void pfree(void *pointer)
Definition: mcxt.c:1520
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1540
void * palloc(Size size)
Definition: mcxt.c:1316
#define MAXPGPATH
#define snprintf
Definition: port.h:238
#define LOG_LEVEL
Definition: rmtree.c:28
#define OPENDIR(x)
Definition: rmtree.c:29
#define CLOSEDIR(x)
Definition: rmtree.c:30
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:50
#define pg_log_warning(...)
Definition: rmtree.c:27
Definition: dirent.c:26
Definition: dirent.h:10
char d_name[MAX_PATH]
Definition: dirent.h:15

References CLOSEDIR, dirent::d_name, get_dirent_type(), i, LOG_LEVEL, MAXPGPATH, OPENDIR, palloc(), pfree(), pg_log_warning, PGFILETYPE_DIR, PGFILETYPE_ERROR, pstrdup(), readdir(), repalloc(), and snprintf.

Referenced by cleanup_directories_atexit(), cleanup_output_dirs(), CreateSlotOnDisk(), dbase_redo(), movedb(), movedb_failure_callback(), regression_main(), remove_dbtablespaces(), remove_new_subdir(), ReplicationSlotDropPtr(), RestoreSlotFromDisk(), and StartupReplicationSlots().