merge with master
[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/gstdio.h>
36 #include <glibmm/miscutils.h>
37
38 #include "pbd/error.h"
39 #include "pbd/compose.h"
40 #include "pbd/clear_dir.h"
41
42 #include "i18n.h"
43
44 using namespace PBD;
45 using namespace std;
46
47 int
48 PBD::clear_directory (const string& dir, size_t* size, vector<string>* paths)
49 {
50         struct dirent* dentry;
51         struct stat statbuf;
52         DIR* dead;
53         int ret = 0;
54
55         if ((dead = ::opendir (dir.c_str())) == 0) {
56                 return -1;
57         }
58         
59         while ((dentry = ::readdir (dead)) != 0) {
60                 
61                 /* avoid '.' and '..' */
62                 
63                 if ((dentry->d_name[0] == '.' && dentry->d_name[1] == '\0') ||
64                     (dentry->d_name[2] == '\0' && dentry->d_name[0] == '.' && dentry->d_name[1] == '.')) {
65                         continue;
66                 }
67                 
68                 string fullpath = Glib::build_filename (dir, dentry->d_name);
69
70                 if (::stat (fullpath.c_str(), &statbuf)) {
71                         continue;
72                 }
73                 
74                 if (!S_ISREG (statbuf.st_mode)) {
75                         continue;
76                 }
77                 
78                 if (::g_unlink (fullpath.c_str())) {
79                         error << string_compose (_("cannot remove file %1 (%2)"), fullpath, strerror (errno))
80                               << endmsg;
81                         ret = 1;
82                 }
83
84                 if (paths) {
85                         paths->push_back (dentry->d_name);
86                 }
87
88                 if (size) {
89                         *size += statbuf.st_size;
90                 }
91         }
92         
93         ::closedir (dead);
94
95         return ret;
96 }