1 /*-------------------------------------------------------------------------
5 * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
6 * Portions Copyright (c) 1994, Regents of the University of California
11 *-------------------------------------------------------------------------
17 #include "postgres_fe.h"
27 * Delete a directory tree recursively.
28 * Assumes path points to a valid directory.
29 * Deletes everything under path.
30 * If rmtopdir is true deletes the directory too.
31 * Returns true if successful, false if there was any problem.
32 * (The details of the problem are reported already, so caller
33 * doesn't really have to say anything more, but most do.)
36 rmtree(const char *path, bool rmtopdir)
39 char pathbuf[MAXPGPATH];
45 * we copy all the names out of the directory before we start modifying
48 filenames = pgfnames(path);
50 if (filenames == NULL)
53 /* now we have the names we can start removing things */
54 for (filename = filenames; *filename; filename++)
56 snprintf(pathbuf, MAXPGPATH, "%s/%s", path, *filename);
59 * It's ok if the file is not there anymore; we were just about to
62 * This is not an academic possibility. One scenario where this
63 * happens is when bgwriter has a pending unlink request for a file in
64 * a database that's being dropped. In dropdb(), we call
65 * ForgetDatabaseFsyncRequests() to flush out any such pending unlink
66 * requests, but because that's asynchronous, it's not guaranteed that
67 * the bgwriter receives the message in time.
69 if (lstat(pathbuf, &statbuf) != 0)
74 elog(WARNING, "could not stat file or directory \"%s\": %m",
77 fprintf(stderr, _("could not stat file or directory \"%s\": %s\n"),
78 pathbuf, strerror(errno));
85 if (S_ISDIR(statbuf.st_mode))
87 /* call ourselves recursively for a directory */
88 if (!rmtree(pathbuf, true))
90 /* we already reported the error */
96 if (unlink(pathbuf) != 0)
101 elog(WARNING, "could not remove file or directory \"%s\": %m",
104 fprintf(stderr, _("could not remove file or directory \"%s\": %s\n"),
105 pathbuf, strerror(errno));
115 if (rmdir(path) != 0)
118 elog(WARNING, "could not remove file or directory \"%s\": %m",
121 fprintf(stderr, _("could not remove file or directory \"%s\": %s\n"),
122 path, strerror(errno));
128 pgfnames_cleanup(filenames);