[CRIU] [PATCH v4 2/7] util: add rm -rf function
Tycho Andersen
tycho.andersen at canonical.com
Thu Oct 27 15:54:56 PDT 2016
Signed-off-by: Tycho Andersen <tycho.andersen at canonical.com>
---
criu/include/util.h | 3 +++
criu/util.c | 40 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 43 insertions(+)
diff --git a/criu/include/util.h b/criu/include/util.h
index 68ff103..b7313cf 100644
--- a/criu/include/util.h
+++ b/criu/include/util.h
@@ -278,6 +278,9 @@ int setup_tcp_server(char *type);
int run_tcp_server(bool daemon_mode, int *ask, int cfd, int sk);
int setup_tcp_client(char *addr);
+/* *dir should be writable and at least PATH_MAX long */
+int rm_rf(char *dir);
+
#define LAST_PID_PATH "sys/kernel/ns_last_pid"
#define PID_MAX_PATH "sys/kernel/pid_max"
diff --git a/criu/util.c b/criu/util.c
index 2cf0355..cb03bdd 100644
--- a/criu/util.c
+++ b/criu/util.c
@@ -1185,3 +1185,43 @@ int setup_tcp_client(char *addr)
return sk;
}
+
+int rm_rf(char *target)
+{
+ int offset = strlen(target);
+ DIR *dir = NULL;
+ struct dirent *de;
+ int ret = -1;
+
+ dir = opendir(target);
+ if (!dir) {
+ pr_perror("unable to open %s", target);
+ return -1;
+ }
+
+ while ((de = readdir(dir))) {
+ int n;
+
+ if (dir_dots(de))
+ continue;
+
+ n = snprintf(target+offset, PATH_MAX-offset, "/%s", de->d_name);
+ if (n < 0 || n >= PATH_MAX) {
+ pr_err("snprintf failed\n");
+ goto out;
+ }
+
+ if (de->d_type == DT_DIR && rm_rf(target))
+ goto out;
+
+ if (remove(target) < 0) {
+ pr_perror("unable to remove %s", target);
+ goto out;
+ }
+ }
+
+ ret = 0;
+out:
+ target[offset] = 0;
+ return ret;
+}
--
2.9.3
More information about the CRIU
mailing list