Move rmtree() from libpgport to libpgcommon
authorPeter Eisentraut <peter_e@gmx.net>
Sat, 19 Oct 2013 14:20:51 +0000 (10:20 -0400)
committerPeter Eisentraut <peter_e@gmx.net>
Sat, 19 Oct 2013 14:20:51 +0000 (10:20 -0400)
It requires pgfnames() from libpgcommon.

src/bin/initdb/nls.mk
src/common/Makefile
src/common/rmtree.c [new file with mode: 0644]
src/port/dirmod.c
src/tools/msvc/Mkvcbuild.pm

index 8febf184403640afc395ab417421344224f6fb1e..377e75970b844b638ed2a5fdd86aaeb18d0a2d5b 100644 (file)
@@ -1,5 +1,5 @@
 # src/bin/initdb/nls.mk
 CATALOG_NAME     = initdb
 AVAIL_LANGUAGES  = cs de es fr it ja pl pt_BR ru zh_CN
-GETTEXT_FILES    = findtimezone.c initdb.c ../../common/exec.c ../../common/fe_memutils.c ../../common/pgfnames.c ../../common/wait_error.c ../../port/dirmod.c
+GETTEXT_FILES    = findtimezone.c initdb.c ../../common/exec.c ../../common/fe_memutils.c ../../common/pgfnames.c ../../common/rmtree.c ../../common/wait_error.c ../../port/dirmod.c
 GETTEXT_TRIGGERS = simple_prompt
index 575a48a94de54930eb442d0e00d66a27e6b87356..d4ef2f1a30785aa69841ae96aa28f3004c254098 100644 (file)
@@ -23,7 +23,7 @@ include $(top_builddir)/src/Makefile.global
 override CPPFLAGS := -DFRONTEND $(CPPFLAGS)
 LIBS += $(PTHREAD_LIBS)
 
-OBJS_COMMON = exec.o pgfnames.o relpath.o wait_error.o
+OBJS_COMMON = exec.o pgfnames.o relpath.o rmtree.o wait_error.o
 
 OBJS_FRONTEND = $(OBJS_COMMON) fe_memutils.o
 
diff --git a/src/common/rmtree.c b/src/common/rmtree.c
new file mode 100644 (file)
index 0000000..ec7bce8
--- /dev/null
@@ -0,0 +1,131 @@
+/*-------------------------------------------------------------------------
+ *
+ * rmtree.c
+ *
+ * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * IDENTIFICATION
+ *   src/common/rmtree.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#ifndef FRONTEND
+#include "postgres.h"
+#else
+#include "postgres_fe.h"
+#endif
+
+#include <unistd.h>
+#include <sys/stat.h>
+
+
+/*
+ * rmtree
+ *
+ * Delete a directory tree recursively.
+ * Assumes path points to a valid directory.
+ * Deletes everything under path.
+ * If rmtopdir is true deletes the directory too.
+ * Returns true if successful, false if there was any problem.
+ * (The details of the problem are reported already, so caller
+ * doesn't really have to say anything more, but most do.)
+ */
+bool
+rmtree(const char *path, bool rmtopdir)
+{
+   bool        result = true;
+   char        pathbuf[MAXPGPATH];
+   char      **filenames;
+   char      **filename;
+   struct stat statbuf;
+
+   /*
+    * we copy all the names out of the directory before we start modifying
+    * it.
+    */
+   filenames = pgfnames(path);
+
+   if (filenames == NULL)
+       return false;
+
+   /* now we have the names we can start removing things */
+   for (filename = filenames; *filename; filename++)
+   {
+       snprintf(pathbuf, MAXPGPATH, "%s/%s", path, *filename);
+
+       /*
+        * It's ok if the file is not there anymore; we were just about to
+        * delete it anyway.
+        *
+        * This is not an academic possibility. One scenario where this
+        * happens is when bgwriter has a pending unlink request for a file in
+        * a database that's being dropped. In dropdb(), we call
+        * ForgetDatabaseFsyncRequests() to flush out any such pending unlink
+        * requests, but because that's asynchronous, it's not guaranteed that
+        * the bgwriter receives the message in time.
+        */
+       if (lstat(pathbuf, &statbuf) != 0)
+       {
+           if (errno != ENOENT)
+           {
+#ifndef FRONTEND
+               elog(WARNING, "could not stat file or directory \"%s\": %m",
+                    pathbuf);
+#else
+               fprintf(stderr, _("could not stat file or directory \"%s\": %s\n"),
+                       pathbuf, strerror(errno));
+#endif
+               result = false;
+           }
+           continue;
+       }
+
+       if (S_ISDIR(statbuf.st_mode))
+       {
+           /* call ourselves recursively for a directory */
+           if (!rmtree(pathbuf, true))
+           {
+               /* we already reported the error */
+               result = false;
+           }
+       }
+       else
+       {
+           if (unlink(pathbuf) != 0)
+           {
+               if (errno != ENOENT)
+               {
+#ifndef FRONTEND
+                   elog(WARNING, "could not remove file or directory \"%s\": %m",
+                        pathbuf);
+#else
+                   fprintf(stderr, _("could not remove file or directory \"%s\": %s\n"),
+                           pathbuf, strerror(errno));
+#endif
+                   result = false;
+               }
+           }
+       }
+   }
+
+   if (rmtopdir)
+   {
+       if (rmdir(path) != 0)
+       {
+#ifndef FRONTEND
+           elog(WARNING, "could not remove file or directory \"%s\": %m",
+                path);
+#else
+           fprintf(stderr, _("could not remove file or directory \"%s\": %s\n"),
+                   path, strerror(errno));
+#endif
+           result = false;
+       }
+   }
+
+   pgfnames_cleanup(filenames);
+
+   return result;
+}
index a18d2ef7ba8e904a6a59d001e24eb1fd65a51886..0a630ff697fbdb7c15e14c184344ac00a5e15b96 100644 (file)
@@ -351,116 +351,6 @@ pgwin32_is_junction(char *path)
 #endif   /* defined(WIN32) && !defined(__CYGWIN__) */
 
 
-/*
- * rmtree
- *
- * Delete a directory tree recursively.
- * Assumes path points to a valid directory.
- * Deletes everything under path.
- * If rmtopdir is true deletes the directory too.
- * Returns true if successful, false if there was any problem.
- * (The details of the problem are reported already, so caller
- * doesn't really have to say anything more, but most do.)
- */
-bool
-rmtree(const char *path, bool rmtopdir)
-{
-   bool        result = true;
-   char        pathbuf[MAXPGPATH];
-   char      **filenames;
-   char      **filename;
-   struct stat statbuf;
-
-   /*
-    * we copy all the names out of the directory before we start modifying
-    * it.
-    */
-   filenames = pgfnames(path);
-
-   if (filenames == NULL)
-       return false;
-
-   /* now we have the names we can start removing things */
-   for (filename = filenames; *filename; filename++)
-   {
-       snprintf(pathbuf, MAXPGPATH, "%s/%s", path, *filename);
-
-       /*
-        * It's ok if the file is not there anymore; we were just about to
-        * delete it anyway.
-        *
-        * This is not an academic possibility. One scenario where this
-        * happens is when bgwriter has a pending unlink request for a file in
-        * a database that's being dropped. In dropdb(), we call
-        * ForgetDatabaseFsyncRequests() to flush out any such pending unlink
-        * requests, but because that's asynchronous, it's not guaranteed that
-        * the bgwriter receives the message in time.
-        */
-       if (lstat(pathbuf, &statbuf) != 0)
-       {
-           if (errno != ENOENT)
-           {
-#ifndef FRONTEND
-               elog(WARNING, "could not stat file or directory \"%s\": %m",
-                    pathbuf);
-#else
-               fprintf(stderr, _("could not stat file or directory \"%s\": %s\n"),
-                       pathbuf, strerror(errno));
-#endif
-               result = false;
-           }
-           continue;
-       }
-
-       if (S_ISDIR(statbuf.st_mode))
-       {
-           /* call ourselves recursively for a directory */
-           if (!rmtree(pathbuf, true))
-           {
-               /* we already reported the error */
-               result = false;
-           }
-       }
-       else
-       {
-           if (unlink(pathbuf) != 0)
-           {
-               if (errno != ENOENT)
-               {
-#ifndef FRONTEND
-                   elog(WARNING, "could not remove file or directory \"%s\": %m",
-                        pathbuf);
-#else
-                   fprintf(stderr, _("could not remove file or directory \"%s\": %s\n"),
-                           pathbuf, strerror(errno));
-#endif
-                   result = false;
-               }
-           }
-       }
-   }
-
-   if (rmtopdir)
-   {
-       if (rmdir(path) != 0)
-       {
-#ifndef FRONTEND
-           elog(WARNING, "could not remove file or directory \"%s\": %m",
-                path);
-#else
-           fprintf(stderr, _("could not remove file or directory \"%s\": %s\n"),
-                   path, strerror(errno));
-#endif
-           result = false;
-       }
-   }
-
-   pgfnames_cleanup(filenames);
-
-   return result;
-}
-
-
 #if defined(WIN32) && !defined(__CYGWIN__)
 
 #undef stat
index 2dc8b1d88194f522f45f21dd3ea05801ba2326a1..0e84348e4d2d0b0771744980ddbeb50f6c55f2f8 100644 (file)
@@ -74,7 +74,7 @@ sub mkvcbuild
      win32error.c win32setlocale.c);
 
    our @pgcommonallfiles = qw(
-     exec.c pgfnames.c relpath.c wait_error.c);
+     exec.c pgfnames.c relpath.c rmtree.c wait_error.c);
 
    our @pgcommonfrontendfiles = (@pgcommonallfiles, qw(fe_memutils.c));