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>
This commit is contained in:
Denys Vlasenko 2021-06-24 17:54:11 +02:00
parent 4a68697521
commit 3b267e9925

View File

@ -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);