--- a/src/utils/wipedir.cpp
+++ b/src/utils/wipedir.cpp
@@ -37,7 +37,7 @@
#include "pathut.h"
#include "wipedir.h"
-int wipedir(const string& dir)
+int wipedir(const string& dir, bool selfalso, bool recurse)
{
struct stat st;
int statret;
@@ -78,7 +78,15 @@
goto out;
}
if (S_ISDIR(st.st_mode)) {
- remaining++;
+ if (recurse) {
+ int rr = wipedir(fn, true, true);
+ if (rr == -1)
+ goto out;
+ else
+ remaining += rr;
+ } else {
+ remaining++;
+ }
} else {
if (unlink(fn.c_str()) < 0) {
LOGERR(("wipedir: cant unlink %s, errno %d\n",
@@ -89,6 +97,14 @@
}
ret = remaining;
+ if (selfalso && ret == 0) {
+ if (rmdir(dir.c_str()) < 0) {
+ LOGERR(("wipedir: rmdir(%s) failed, errno %d\n",
+ dir.c_str(), errno));
+ ret = -1;
+ }
+ }
+
out:
if (d)
closedir(d);
@@ -103,15 +119,50 @@
#include "wipedir.h"
using namespace std;
+static const char *thisprog;
+static int op_flags;
+#define OPT_MOINS 0x1
+#define OPT_r 0x2
+#define OPT_s 0x4
+static char usage [] =
+"wipedir [-r -s] topdir\n"
+" -r : recurse\n"
+" -s : also delete topdir\n"
+;
+static void
+Usage(void)
+{
+ fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
+ exit(1);
+}
int main(int argc, const char **argv)
{
- if (argc != 2) {
- fprintf(stderr, "Usage: wipedir <dir>\n");
- exit(1);
+ thisprog = argv[0];
+ argc--; argv++;
+
+ while (argc > 0 && **argv == '-') {
+ (*argv)++;
+ if (!(**argv))
+ /* Cas du "adb - core" */
+ Usage();
+ while (**argv)
+ switch (*(*argv)++) {
+ case 'r': op_flags |= OPT_r; break;
+ case 's': op_flags |= OPT_s; break;
+ default: Usage(); break;
+ }
+ b1: argc--; argv++;
}
- string dir = argv[1];
- int cnt = wipedir(dir);
+
+ if (argc != 1)
+ Usage();
+
+ string dir = *argv++;argc--;
+
+ bool topalso = ((op_flags&OPT_s) != 0);
+ bool recurse = ((op_flags&OPT_r) != 0);
+ int cnt = wipedir(dir, topalso, recurse);
printf("wipedir returned %d\n", cnt);
exit(0);
}