summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2012-11-29 20:21:26 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2012-11-29 20:21:29 +0100
commit5a9d53c47e3c6ce5bc7d38c60501a37ce2c2b276 (patch)
treee04c756e01daa0e71a209a5ed60e464395268d85 /lib
parent159109f5ff2e3b0ee7c44685e2241605ae41b662 (diff)
downloadbarebox-5a9d53c47e3c6ce5bc7d38c60501a37ce2c2b276.tar.gz
barebox-5a9d53c47e3c6ce5bc7d38c60501a37ce2c2b276.tar.xz
rm: implement -r
To recursively remove files and directories. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile1
-rw-r--r--lib/recursive_action.c1
-rw-r--r--lib/unlink-recursive.c56
3 files changed, 57 insertions, 1 deletions
diff --git a/lib/Makefile b/lib/Makefile
index eb0af9248..635d52e65 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -36,3 +36,4 @@ obj-$(CONFIG_BITREV) += bitrev.o
obj-$(CONFIG_QSORT) += qsort.o
obj-y += gui/
obj-$(CONFIG_XYMODEM) += xymodem.o
+obj-y += unlink-recursive.o
diff --git a/lib/recursive_action.c b/lib/recursive_action.c
index 5bc2595db..345d3db0c 100644
--- a/lib/recursive_action.c
+++ b/lib/recursive_action.c
@@ -130,7 +130,6 @@ int recursive_action(const char *fileName,
return 0;
return 1;
done_nak_warn:
- printf("%s", fileName);
return 0;
}
diff --git a/lib/unlink-recursive.c b/lib/unlink-recursive.c
new file mode 100644
index 000000000..a4885538c
--- /dev/null
+++ b/lib/unlink-recursive.c
@@ -0,0 +1,56 @@
+#include <common.h>
+#include <libbb.h>
+#include <fs.h>
+
+static char unlink_recursive_failedpath[PATH_MAX];
+
+struct data {
+ int error;
+};
+
+static int file_action(const char *filename, struct stat *statbuf,
+ void *userdata, int depth)
+{
+ struct data *data = userdata;
+ int ret;
+
+ ret = unlink(filename);
+ if (ret) {
+ strcpy(unlink_recursive_failedpath, filename);
+ data->error = ret;
+ }
+
+ return ret ? 0 : 1;
+}
+
+static int dir_action(const char *dirname, struct stat *statbuf,
+ void *userdata, int depth)
+{
+ struct data *data = userdata;
+ int ret;
+
+ ret = rmdir(dirname);
+ if (ret) {
+ strcpy(unlink_recursive_failedpath, dirname);
+ data->error = ret;
+ }
+
+ return ret ? 0 : 1;
+}
+
+int unlink_recursive(const char *path, char **failedpath)
+{
+ struct data data = {};
+ int ret;
+
+ if (failedpath)
+ *failedpath = NULL;
+
+ ret = recursive_action(path, ACTION_RECURSE | ACTION_DEPTHFIRST,
+ file_action, dir_action, &data, 0);
+
+ if (!ret && failedpath)
+ *failedpath = unlink_recursive_failedpath;
+
+ return ret ? 0 : errno;
+}