Use PBD::find_files_matching_pattern instead of other variations
[ardour.git] / libs / pbd / clear_dir.cc
1 /*
2     Copyright (C) 2012 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifdef COMPILER_MSVC
21 #include <io.h>      // Microsoft's nearest equivalent to <unistd.h>
22 using PBD::readdir;
23 using PBD::opendir;
24 using PBD::closedir;
25 #else
26 #include <dirent.h>
27 #include <unistd.h>
28 #endif
29
30 #include <string>
31 #include <sys/stat.h>
32 #include <errno.h>
33 #include <string.h>
34
35 #include <glib.h>
36 #include <glib/gstdio.h>
37 #include <glibmm/miscutils.h>
38
39 #include "pbd/error.h"
40 #include "pbd/compose.h"
41 #include "pbd/clear_dir.h"
42
43 #include "i18n.h"
44
45 using namespace PBD;
46 using namespace std;
47
48 int
49 PBD::clear_directory (const string& dir, size_t* size, vector<string>* paths)
50 {
51         struct dirent* dentry;
52         struct stat statbuf;
53         DIR* dead;
54         int ret = 0;
55
56         if ((dead = ::opendir (dir.c_str())) == 0) {
57                 return -1;
58         }
59         
60         while ((dentry = ::readdir (dead)) != 0) {
61                 
62                 /* avoid '.' and '..' */
63                 
64                 if ((dentry->d_name[0] == '.' && dentry->d_name[1] == '\0') ||
65                     (dentry->d_name[2] == '\0' && dentry->d_name[0] == '.' && dentry->d_name[1] == '.')) {
66                         continue;
67                 }
68                 
69                 string fullpath = Glib::build_filename (dir, dentry->d_name);
70
71                 if (::stat (fullpath.c_str(), &statbuf)) {
72                         continue;
73                 }
74                 
75                 if (!S_ISREG (statbuf.st_mode)) {
76                         continue;
77                 }
78                 
79                 if (::g_unlink (fullpath.c_str())) {
80                         error << string_compose (_("cannot remove file %1 (%2)"), fullpath, strerror (errno))
81                               << endmsg;
82                         ret = 1;
83                 }
84
85                 if (paths) {
86                         paths->push_back (dentry->d_name);
87                 }
88
89                 if (size) {
90                         *size += statbuf.st_size;
91                 }
92         }
93         
94         ::closedir (dead);
95
96         return ret;
97 }
98
99 // rm -rf <dir> -- used to remove saved plugin state
100 void
101 PBD::remove_directory (const std::string& dir) {
102         DIR* dead;
103         struct dirent* dentry;
104         struct stat statbuf;
105
106         if ((dead = ::opendir (dir.c_str())) == 0) {
107                 return;
108         }
109
110         while ((dentry = ::readdir (dead)) != 0) {
111                 if(!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, "..")) {
112                         continue;
113                 }
114
115                 string fullpath = Glib::build_filename (dir, dentry->d_name);
116                 if (::stat (fullpath.c_str(), &statbuf)) {
117                         continue;
118                 }
119
120                 if (S_ISDIR (statbuf.st_mode)) {
121                         remove_directory(fullpath);
122                         continue;
123                 }
124
125                 if (::g_unlink (fullpath.c_str())) {
126                         error << string_compose (_("cannot remove file %1 (%2)"), fullpath, strerror (errno)) << endmsg;
127                 }
128         }
129         if (::g_rmdir(dir.c_str())) {
130                 error << string_compose (_("cannot remove directory %1 (%2)"), dir, strerror (errno)) << endmsg;
131         }
132         ::closedir (dead);
133 }