allow zero-input (i.e. tone generator) processors to be added
[ardour2.git] / libs / pbd / clear_dir.cc
blob3fa9b2233476b68e7a57dd9ba220b030216296da
1 #include <string>
2 #include <dirent.h>
3 #include <unistd.h>
4 #include <sys/stat.h>
5 #include <errno.h>
6 #include <string.h>
8 #include <glibmm/miscutils.h>
10 #include "pbd/error.h"
11 #include "pbd/compose.h"
12 #include "pbd/clear_dir.h"
14 #include "i18n.h"
16 using namespace PBD;
17 using namespace std;
19 int
20 PBD::clear_directory (const string& dir, size_t* size, vector<string>* paths)
22 struct dirent* dentry;
23 struct stat statbuf;
24 DIR* dead;
25 int ret = 0;
27 if ((dead = ::opendir (dir.c_str())) == 0) {
28 return -1;
31 while ((dentry = ::readdir (dead)) != 0) {
33 /* avoid '.' and '..' */
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;
40 string fullpath = Glib::build_filename (dir, dentry->d_name);
42 if (::stat (fullpath.c_str(), &statbuf)) {
43 continue;
46 if (!S_ISREG (statbuf.st_mode)) {
47 continue;
50 if (::unlink (fullpath.c_str())) {
51 error << string_compose (_("cannot remove file %1 (%2)"), fullpath, strerror (errno))
52 << endmsg;
53 ret = 1;
56 if (paths) {
57 paths->push_back (dentry->d_name);
60 if (size) {
61 *size += statbuf.st_size;
65 ::closedir (dead);
67 return ret;