diff options
| author | Denys Vlasenko <vda.linux@googlemail.com> | 2021-06-24 17:54:11 +0200 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2021-06-24 17:54:11 +0200 |
| commit | 3b267e99259191eca0865179a56429c4c441e2b2 (patch) | |
| tree | e20c9dcf8dab20a8f3cc77b222ea16954c95f3ab /util-linux | |
| parent | 4a68697521392f461c8627841531548d4bd6841a (diff) | |
| download | busybox-w32-3b267e99259191eca0865179a56429c4c441e2b2.tar.gz busybox-w32-3b267e99259191eca0865179a56429c4c441e2b2.tar.bz2 busybox-w32-3b267e99259191eca0865179a56429c4c441e2b2.zip | |
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>
Diffstat (limited to 'util-linux')
| -rw-r--r-- | util-linux/switch_root.c | 35 |
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); | |||
| 68 | # define MS_MOVE 8192 | 68 | # define MS_MOVE 8192 |
| 69 | #endif | 69 | #endif |
| 70 | 70 | ||
| 71 | static void delete_contents(const char *directory, dev_t rootdev); | ||
| 72 | |||
| 73 | static int FAST_FUNC rmrf(const char *directory, struct dirent *d, void *rootdevp) | ||
| 74 | { | ||
| 75 | char *newdir = concat_subpath_file(directory, d->d_name); | ||
| 76 | if (newdir) { // not . or .. | ||
| 77 | // Recurse to delete contents | ||
| 78 | delete_contents(newdir, *(dev_t*)rootdevp); | ||
| 79 | free(newdir); | ||
| 80 | } | ||
| 81 | return 0; | ||
| 82 | } | ||
| 83 | |||
| 71 | // Recursively delete contents of rootfs | 84 | // Recursively delete contents of rootfs |
| 72 | static void delete_contents(const char *directory, dev_t rootdev) | 85 | static void delete_contents(const char *directory, dev_t rootdev) |
| 73 | { | 86 | { |
| 74 | DIR *dir; | ||
| 75 | struct dirent *d; | ||
| 76 | struct stat st; | 87 | struct stat st; |
| 77 | 88 | ||
| 78 | // Don't descend into other filesystems | 89 | // Don't descend into other filesystems |
| @@ -81,25 +92,7 @@ static void delete_contents(const char *directory, dev_t rootdev) | |||
| 81 | 92 | ||
| 82 | // Recursively delete the contents of directories | 93 | // Recursively delete the contents of directories |
| 83 | if (S_ISDIR(st.st_mode)) { | 94 | if (S_ISDIR(st.st_mode)) { |
| 84 | dir = opendir(directory); | 95 | iterate_on_dir(directory, rmrf, &rootdev); |
| 85 | if (dir) { | ||
| 86 | while ((d = readdir(dir))) { | ||
| 87 | char *newdir = d->d_name; | ||
| 88 | |||
| 89 | // Skip . and .. | ||
| 90 | if (DOT_OR_DOTDOT(newdir)) | ||
| 91 | continue; | ||
| 92 | |||
| 93 | // Recurse to delete contents | ||
| 94 | newdir = concat_path_file(directory, newdir); | ||
| 95 | delete_contents(newdir, rootdev); | ||
| 96 | free(newdir); | ||
| 97 | } | ||
| 98 | closedir(dir); | ||
| 99 | |||
| 100 | // Directory should now be empty, zap it | ||
| 101 | rmdir(directory); | ||
| 102 | } | ||
| 103 | } else { | 96 | } else { |
| 104 | // It wasn't a directory, zap it | 97 | // It wasn't a directory, zap it |
| 105 | unlink(directory); | 98 | unlink(directory); |
