|
a/src/utils/wipedir.cpp |
|
b/src/utils/wipedir.cpp |
|
... |
|
... |
35 |
|
35 |
|
36 |
#include "debuglog.h"
|
36 |
#include "debuglog.h"
|
37 |
#include "pathut.h"
|
37 |
#include "pathut.h"
|
38 |
#include "wipedir.h"
|
38 |
#include "wipedir.h"
|
39 |
|
39 |
|
40 |
int wipedir(const string& dir)
|
40 |
int wipedir(const string& dir, bool selfalso, bool recurse)
|
41 |
{
|
41 |
{
|
42 |
struct stat st;
|
42 |
struct stat st;
|
43 |
int statret;
|
43 |
int statret;
|
44 |
int ret = -1;
|
44 |
int ret = -1;
|
45 |
|
45 |
|
|
... |
|
... |
76 |
if (statret == -1) {
|
76 |
if (statret == -1) {
|
77 |
LOGERR(("wipedir: cant stat %s, errno %d\n", fn.c_str(), errno));
|
77 |
LOGERR(("wipedir: cant stat %s, errno %d\n", fn.c_str(), errno));
|
78 |
goto out;
|
78 |
goto out;
|
79 |
}
|
79 |
}
|
80 |
if (S_ISDIR(st.st_mode)) {
|
80 |
if (S_ISDIR(st.st_mode)) {
|
|
|
81 |
if (recurse) {
|
|
|
82 |
int rr = wipedir(fn, true, true);
|
|
|
83 |
if (rr == -1)
|
|
|
84 |
goto out;
|
|
|
85 |
else
|
|
|
86 |
remaining += rr;
|
|
|
87 |
} else {
|
81 |
remaining++;
|
88 |
remaining++;
|
|
|
89 |
}
|
82 |
} else {
|
90 |
} else {
|
83 |
if (unlink(fn.c_str()) < 0) {
|
91 |
if (unlink(fn.c_str()) < 0) {
|
84 |
LOGERR(("wipedir: cant unlink %s, errno %d\n",
|
92 |
LOGERR(("wipedir: cant unlink %s, errno %d\n",
|
85 |
fn.c_str(), errno));
|
93 |
fn.c_str(), errno));
|
86 |
goto out;
|
94 |
goto out;
|
87 |
}
|
95 |
}
|
88 |
}
|
96 |
}
|
89 |
}
|
97 |
}
|
90 |
|
98 |
|
91 |
ret = remaining;
|
99 |
ret = remaining;
|
|
|
100 |
if (selfalso && ret == 0) {
|
|
|
101 |
if (rmdir(dir.c_str()) < 0) {
|
|
|
102 |
LOGERR(("wipedir: rmdir(%s) failed, errno %d\n",
|
|
|
103 |
dir.c_str(), errno));
|
|
|
104 |
ret = -1;
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
|
92 |
out:
|
108 |
out:
|
93 |
if (d)
|
109 |
if (d)
|
94 |
closedir(d);
|
110 |
closedir(d);
|
95 |
return ret;
|
111 |
return ret;
|
96 |
}
|
112 |
}
|
|
... |
|
... |
101 |
#include <sys/stat.h>
|
117 |
#include <sys/stat.h>
|
102 |
|
118 |
|
103 |
#include "wipedir.h"
|
119 |
#include "wipedir.h"
|
104 |
|
120 |
|
105 |
using namespace std;
|
121 |
using namespace std;
|
|
|
122 |
static const char *thisprog;
|
106 |
|
123 |
|
|
|
124 |
static int op_flags;
|
|
|
125 |
#define OPT_MOINS 0x1
|
|
|
126 |
#define OPT_r 0x2
|
|
|
127 |
#define OPT_s 0x4
|
|
|
128 |
static char usage [] =
|
|
|
129 |
"wipedir [-r -s] topdir\n"
|
|
|
130 |
" -r : recurse\n"
|
|
|
131 |
" -s : also delete topdir\n"
|
|
|
132 |
;
|
|
|
133 |
static void
|
|
|
134 |
Usage(void)
|
|
|
135 |
{
|
|
|
136 |
fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
|
|
|
137 |
exit(1);
|
|
|
138 |
}
|
107 |
int main(int argc, const char **argv)
|
139 |
int main(int argc, const char **argv)
|
108 |
{
|
140 |
{
|
109 |
if (argc != 2) {
|
141 |
thisprog = argv[0];
|
110 |
fprintf(stderr, "Usage: wipedir <dir>\n");
|
142 |
argc--; argv++;
|
111 |
exit(1);
|
143 |
|
|
|
144 |
while (argc > 0 && **argv == '-') {
|
|
|
145 |
(*argv)++;
|
|
|
146 |
if (!(**argv))
|
|
|
147 |
/* Cas du "adb - core" */
|
|
|
148 |
Usage();
|
|
|
149 |
while (**argv)
|
|
|
150 |
switch (*(*argv)++) {
|
|
|
151 |
case 'r': op_flags |= OPT_r; break;
|
|
|
152 |
case 's': op_flags |= OPT_s; break;
|
|
|
153 |
default: Usage(); break;
|
|
|
154 |
}
|
|
|
155 |
b1: argc--; argv++;
|
112 |
}
|
156 |
}
|
|
|
157 |
|
|
|
158 |
if (argc != 1)
|
|
|
159 |
Usage();
|
|
|
160 |
|
113 |
string dir = argv[1];
|
161 |
string dir = *argv++;argc--;
|
|
|
162 |
|
|
|
163 |
bool topalso = ((op_flags&OPT_s) != 0);
|
|
|
164 |
bool recurse = ((op_flags&OPT_r) != 0);
|
114 |
int cnt = wipedir(dir);
|
165 |
int cnt = wipedir(dir, topalso, recurse);
|
115 |
printf("wipedir returned %d\n", cnt);
|
166 |
printf("wipedir returned %d\n", cnt);
|
116 |
exit(0);
|
167 |
exit(0);
|
117 |
}
|
168 |
}
|
118 |
|
169 |
|
119 |
#endif
|
170 |
#endif
|