trying to track down why undo doesn't remove xfade rendering on OS X
[ardour.git] / libs / pbd / clear_dir.cc
1 #include <string>
2 #include <dirent.h>
3 #include <unistd.h>
4 #include <sys/stat.h>
5 #include <errno.h>
6 #include <string.h>
7
8 #include <glibmm/miscutils.h>
9
10 #include "pbd/error.h"
11 #include "pbd/compose.h"
12 #include "pbd/clear_dir.h"
13
14 #include "i18n.h"
15
16 using namespace PBD;
17 using namespace std;
18
19 int
20 PBD::clear_directory (const string& dir, size_t* size, vector<string>* paths)
21 {
22         struct dirent* dentry;
23         struct stat statbuf;
24         DIR* dead;
25         int ret = 0;
26
27         if ((dead = ::opendir (dir.c_str())) == 0) {
28                 return -1;
29         }
30         
31         while ((dentry = ::readdir (dead)) != 0) {
32                 
33                 /* avoid '.' and '..' */
34                 
35                 if ((dentry->d_name[0] == '.' && dentry->d_name[1] == '\0') ||
36                     (dentry->d_name[2] == '\0' && dentry->d_name[0] == '.' && dentry->d_name[1] == '.')) {
37                         continue;
38                 }
39                 
40                 string fullpath = Glib::build_filename (dir, dentry->d_name);
41
42                 if (::stat (fullpath.c_str(), &statbuf)) {
43                         continue;
44                 }
45                 
46                 if (!S_ISREG (statbuf.st_mode)) {
47                         continue;
48                 }
49                 
50                 if (::unlink (fullpath.c_str())) {
51                         error << string_compose (_("cannot remove file %1 (%2)"), fullpath, strerror (errno))
52                               << endmsg;
53                         ret = 1;
54                 }
55
56                 if (paths) {
57                         paths->push_back (dentry->d_name);
58                 }
59
60                 if (size) {
61                         *size += statbuf.st_size;
62                 }
63         }
64         
65         ::closedir (dead);
66
67         return ret;
68 }