1 /*-------------------------------------------------------------------------
5 * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
6 * Portions Copyright (c) 1994, Regents of the University of California
11 *-------------------------------------------------------------------------
17 #include "postgres_fe.h"
24 #define pg_log_warning(...) elog(WARNING, __VA_ARGS__)
26 #include "fe_utils/logging.h"
33 * Delete a directory tree recursively.
34 * Assumes path points to a valid directory.
35 * Deletes everything under path.
36 * If rmtopdir is true deletes the directory too.
37 * Returns true if successful, false if there was any problem.
38 * (The details of the problem are reported already, so caller
39 * doesn't really have to say anything more, but most do.)
42 rmtree(const char *path, bool rmtopdir)
45 char pathbuf[MAXPGPATH];
51 * we copy all the names out of the directory before we start modifying
54 filenames = pgfnames(path);
56 if (filenames == NULL)
59 /* now we have the names we can start removing things */
60 for (filename = filenames; *filename; filename++)
62 snprintf(pathbuf, MAXPGPATH, "%s/%s", path, *filename);
65 * It's ok if the file is not there anymore; we were just about to
68 * This is not an academic possibility. One scenario where this
69 * happens is when bgwriter has a pending unlink request for a file in
70 * a database that's being dropped. In dropdb(), we call
71 * ForgetDatabaseSyncRequests() to flush out any such pending unlink
72 * requests, but because that's asynchronous, it's not guaranteed that
73 * the bgwriter receives the message in time.
75 if (lstat(pathbuf, &statbuf) != 0)
79 pg_log_warning("could not stat file or directory \"%s\": %m",
86 if (S_ISDIR(statbuf.st_mode))
88 /* call ourselves recursively for a directory */
89 if (!rmtree(pathbuf, true))
91 /* we already reported the error */
97 if (unlink(pathbuf) != 0)
101 pg_log_warning("could not remove file or directory \"%s\": %m",
111 if (rmdir(path) != 0)
113 pg_log_warning("could not remove file or directory \"%s\": %m",
119 pgfnames_cleanup(filenames);