Remove 'pipex' from my pbd.vcproj as it's not needed for Ardour3
[ardour.git] / libs / pbd / clear_dir.cc
index 29410d41e51cbdbaab4ee7a8c5dce378ac10be7a..9d2d7ed88311b4024958138a11faac3a4d2484fe 100644 (file)
 
 */
 
-#include <string>
+#ifdef COMPILER_MSVC
+#include <io.h>      // Microsoft's nearest equivalent to <unistd.h>
+using PBD::readdir;
+using PBD::opendir;
+using PBD::closedir;
+#else
 #include <dirent.h>
 #include <unistd.h>
+#endif
+
+#include <string>
 #include <sys/stat.h>
 #include <errno.h>
 #include <string.h>
 
+#include <glib/gstdio.h>
 #include <glibmm/miscutils.h>
 
 #include "pbd/error.h"
@@ -66,7 +75,7 @@ PBD::clear_directory (const string& dir, size_t* size, vector<string>* paths)
                         continue;
                 }
                 
-                if (::unlink (fullpath.c_str())) {
+                if (::g_unlink (fullpath.c_str())) {
                         error << string_compose (_("cannot remove file %1 (%2)"), fullpath, strerror (errno))
                               << endmsg;
                         ret = 1;
@@ -85,3 +94,38 @@ PBD::clear_directory (const string& dir, size_t* size, vector<string>* paths)
 
         return ret;
 }
+
+// rm -rf <dir> -- used to remove saved plugin state
+void
+PBD::remove_directory (const std::string& dir) {
+       DIR* dead;
+       struct dirent* dentry;
+       struct stat statbuf;
+
+       if ((dead = ::opendir (dir.c_str())) == 0) {
+               return;
+       }
+
+       while ((dentry = ::readdir (dead)) != 0) {
+               if(!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, "..")) {
+                       continue;
+               }
+
+               string fullpath = Glib::build_filename (dir, dentry->d_name);
+               if (::stat (fullpath.c_str(), &statbuf)) {
+                       continue;
+               }
+
+               if (S_ISDIR (statbuf.st_mode)) {
+                       remove_directory(fullpath);
+                       continue;
+               }
+
+               if (::g_unlink (fullpath.c_str())) {
+                       error << string_compose (_("cannot remove file %1 (%2)"), fullpath, strerror (errno)) << endmsg;
+               }
+       }
+       if (::g_rmdir(dir.c_str())) {
+               error << string_compose (_("cannot remove directory %1 (%2)"), dir, strerror (errno)) << endmsg;
+       }
+}