From 2d6bccbb70ce2f8195aad348c653c4c13f21c90c Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Sun, 22 Mar 2015 14:08:27 +0100 Subject: [PATCH] overlay: create a common function for deleting all overlay data (optionally excluding sysupgrade.tgz) Signed-off-by: Felix Fietkau --- jffs2reset.c | 29 +---------------------------- libfstools/libfstools.h | 2 ++ libfstools/overlay.c | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 28 deletions(-) diff --git a/jffs2reset.c b/jffs2reset.c index 6beb81e..c9d4f5f 100644 --- a/jffs2reset.c +++ b/jffs2reset.c @@ -27,33 +27,6 @@ #include "libfstools/libfstools.h" #include "libfstools/volume.h" -static int -handle_rmdir(const char *dir) -{ - struct dirent *dt; - struct stat st; - DIR *d; - int fd; - - d = opendir(dir); - if (!d) - return -1; - - fd = dirfd(d); - - while ((dt = readdir(d)) != NULL) { - if (fstatat(fd, dt->d_name, &st, AT_SYMLINK_NOFOLLOW) || S_ISDIR(st.st_mode)) - continue; - - unlinkat(fd, dt->d_name, 0); - } - - closedir(d); - rmdir(dir); - - return 0; -} - static int ask_user(int argc, char **argv) { @@ -89,7 +62,7 @@ jffs2_reset(int argc, char **argv) mp = find_mount_point(v->blk, 1); if (mp) { ULOG_INFO("%s is mounted as %s, only erasing files\n", v->blk, mp); - foreachdir(mp, handle_rmdir); + overlay_delete(mp, false); mount(mp, "/", NULL, MS_REMOUNT, 0); } else { ULOG_INFO("%s is not mounted, erasing it\n", v->blk); diff --git a/libfstools/libfstools.h b/libfstools/libfstools.h index a9f8576..1d41eea 100644 --- a/libfstools/libfstools.h +++ b/libfstools/libfstools.h @@ -48,4 +48,6 @@ extern int jffs2_switch(struct volume *v); extern int handle_whiteout(const char *dir); extern void foreachdir(const char *dir, int (*cb)(const char*)); +extern void overlay_delete(const char *dir, bool keep_sysupgrade); + #endif diff --git a/libfstools/overlay.c b/libfstools/overlay.c index 21012ba..c535693 100644 --- a/libfstools/overlay.c +++ b/libfstools/overlay.c @@ -32,6 +32,38 @@ #define SWITCH_JFFS2 "/tmp/.switch_jffs2" +static bool keep_sysupgrade; + +static int +handle_rmdir(const char *dir) +{ + struct dirent *dt; + struct stat st; + DIR *d; + int fd; + + d = opendir(dir); + if (!d) + return -1; + + fd = dirfd(d); + + while ((dt = readdir(d)) != NULL) { + if (fstatat(fd, dt->d_name, &st, AT_SYMLINK_NOFOLLOW) || S_ISDIR(st.st_mode)) + continue; + + if (keep_sysupgrade && !strcmp(dt->d_name, "sysupgrade.tgz")) + continue; + + unlinkat(fd, dt->d_name, 0); + } + + closedir(d); + rmdir(dir); + + return 0; +} + void foreachdir(const char *dir, int (*cb)(const char*)) { @@ -51,6 +83,13 @@ foreachdir(const char *dir, int (*cb)(const char*)) cb(dir); } +void +overlay_delete(const char *dir, bool _keep_sysupgrade) +{ + keep_sysupgrade = _keep_sysupgrade; + foreachdir(dir, handle_rmdir); +} + static int overlay_mount(struct volume *v, char *fs) { -- 2.25.1