Add remove_directory_internal function and use it in PBD::clear_directory and PBD...
[ardour.git] / libs / pbd / clear_dir.cc
index 2f9c7b772d4f618b24c773796ba83e52cfa80af0..08c932c3399abc830aada007196cf431f0a4c5ec 100644 (file)
@@ -32,12 +32,14 @@ using PBD::closedir;
 #include <errno.h>
 #include <string.h>
 
+#include <glib.h>
 #include <glib/gstdio.h>
 #include <glibmm/miscutils.h>
 
 #include "pbd/error.h"
 #include "pbd/compose.h"
 #include "pbd/clear_dir.h"
+#include "pbd/file_utils.h"
 
 #include "i18n.h"
 
@@ -45,52 +47,50 @@ using namespace PBD;
 using namespace std;
 
 int
-PBD::clear_directory (const string& dir, size_t* size, vector<string>* paths)
+remove_directory_internal (const string& dir, size_t* size, vector<string>* paths,
+                           bool just_remove_files)
 {
-       struct dirent* dentry;
+       vector<string> tmp_paths;
        struct stat statbuf;
-       DIR* dead;
-        int ret = 0;
-
-        if ((dead = ::opendir (dir.c_str())) == 0) {
-                return -1;
-        }
-        
-        while ((dentry = ::readdir (dead)) != 0) {
-                
-                /* avoid '.' and '..' */
-                
-                if ((dentry->d_name[0] == '.' && dentry->d_name[1] == '\0') ||
-                    (dentry->d_name[2] == '\0' && dentry->d_name[0] == '.' && dentry->d_name[1] == '.')) {
-                        continue;
-                }
-                
-                string fullpath = Glib::build_filename (dir, dentry->d_name);
+       int ret = 0;
 
-                if (::stat (fullpath.c_str(), &statbuf)) {
-                        continue;
-                }
-                
-                if (!S_ISREG (statbuf.st_mode)) {
-                        continue;
-                }
-                
-                if (::g_unlink (fullpath.c_str())) {
-                        error << string_compose (_("cannot remove file %1 (%2)"), fullpath, strerror (errno))
+       get_directory_contents (dir, tmp_paths, just_remove_files, true);
+
+       for (vector<string>::const_iterator i = tmp_paths.begin();
+            i != tmp_paths.end(); ++i) {
+
+               if (g_stat (i->c_str(), &statbuf)) {
+                       continue;
+               }
+
+                if (::g_remove (i->c_str())) {
+                        error << string_compose (_("cannot remove path %1 (%2)"), *i, strerror (errno))
                               << endmsg;
                         ret = 1;
                 }
 
                 if (paths) {
-                        paths->push_back (dentry->d_name);
+                        paths->push_back (Glib::path_get_basename(*i));
                 }
 
                 if (size) {
                         *size += statbuf.st_size;
                 }
-        }
-        
-        ::closedir (dead);
+
+       }
 
         return ret;
 }
+
+int
+PBD::clear_directory (const string& dir, size_t* size, vector<string>* paths)
+{
+       return remove_directory_internal (dir, size, paths, true);
+}
+
+// rm -rf <dir> -- used to remove saved plugin state
+void
+PBD::remove_directory (const std::string& dir)
+{
+       remove_directory_internal (dir, 0, 0, false);
+}