summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2021-06-24 17:54:11 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2021-06-24 17:54:11 +0200
commit3b267e99259191eca0865179a56429c4c441e2b2 (patch)
treee20c9dcf8dab20a8f3cc77b222ea16954c95f3ab
parent4a68697521392f461c8627841531548d4bd6841a (diff)
switch_root: code shrink - use iterate_on_dir()
function old new delta rmrf - 46 +46 delete_contents 181 99 -82 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 0/1 up/down: 46/-82) Total: -36 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--util-linux/switch_root.c35
1 files changed, 14 insertions, 21 deletions
diff --git a/util-linux/switch_root.c b/util-linux/switch_root.c
index f2674b5ac..901c0b8db 100644
--- a/util-linux/switch_root.c
+++ b/util-linux/switch_root.c
@@ -68,11 +68,22 @@ extern int capget(cap_user_header_t header, const cap_user_data_t data);
# define MS_MOVE 8192
#endif
+static void delete_contents(const char *directory, dev_t rootdev);
+
+static int FAST_FUNC rmrf(const char *directory, struct dirent *d, void *rootdevp)
+{
+ char *newdir = concat_subpath_file(directory, d->d_name);
+ if (newdir) { // not . or ..
+ // Recurse to delete contents
+ delete_contents(newdir, *(dev_t*)rootdevp);
+ free(newdir);
+ }
+ return 0;
+}
+
// Recursively delete contents of rootfs
static void delete_contents(const char *directory, dev_t rootdev)
{
- DIR *dir;
- struct dirent *d;
struct stat st;
// Don't descend into other filesystems
@@ -81,25 +92,7 @@ static void delete_contents(const char *directory, dev_t rootdev)
// Recursively delete the contents of directories
if (S_ISDIR(st.st_mode)) {
- dir = opendir(directory);
- if (dir) {
- while ((d = readdir(dir))) {
- char *newdir = d->d_name;
-
- // Skip . and ..
- if (DOT_OR_DOTDOT(newdir))
- continue;
-
- // Recurse to delete contents
- newdir = concat_path_file(directory, newdir);
- delete_contents(newdir, rootdev);
- free(newdir);
- }
- closedir(dir);
-
- // Directory should now be empty, zap it
- rmdir(directory);
- }
+ iterate_on_dir(directory, rmrf, &rootdev);
} else {
// It wasn't a directory, zap it
unlink(directory);