From d7ab03757c2d192d101e06ae1bbf3fc7df3735ec Mon Sep 17 00:00:00 2001 From: 2xsec Date: Tue, 3 Jul 2018 14:44:24 +0900 Subject: [PATCH] utils: move recursive_destroy() from cfgsng to utils. Signed-off-by: 2xsec --- src/lxc/cgroups/cgfsng.c | 56 ------------------------------------- src/lxc/tools/lxc_ls.c | 55 +++--------------------------------- src/lxc/utils.c | 60 ++++++++++++++++++++++++++++++++++++++++ src/lxc/utils.h | 1 + 4 files changed, 65 insertions(+), 107 deletions(-) diff --git a/src/lxc/cgroups/cgfsng.c b/src/lxc/cgroups/cgfsng.c index b514a7bb6..3cc9f9f62 100644 --- a/src/lxc/cgroups/cgfsng.c +++ b/src/lxc/cgroups/cgfsng.c @@ -1040,62 +1040,6 @@ static void lxc_cgfsng_print_basecg_debuginfo(char *basecginfo, char **klist, TRACE("named subsystem %d: %s", k, *it); } -static int recursive_destroy(char *dirname) -{ - int ret; - struct dirent *direntp; - DIR *dir; - int r = 0; - - dir = opendir(dirname); - if (!dir) - return -1; - - while ((direntp = readdir(dir))) { - char *pathname; - struct stat mystat; - - if (!strcmp(direntp->d_name, ".") || - !strcmp(direntp->d_name, "..")) - continue; - - pathname = must_make_path(dirname, direntp->d_name, NULL); - - ret = lstat(pathname, &mystat); - if (ret < 0) { - if (!r) - WARN("Failed to stat \"%s\"", pathname); - r = -1; - goto next; - } - - if (!S_ISDIR(mystat.st_mode)) - goto next; - - ret = recursive_destroy(pathname); - if (ret < 0) - r = -1; - next: - free(pathname); - } - - ret = rmdir(dirname); - if (ret < 0) { - if (!r) - SYSWARN("Failed to delete \"%s\"", dirname); - r = -1; - } - - ret = closedir(dir); - if (ret < 0) { - if (!r) - SYSWARN("Failed to delete \"%s\"", dirname); - r = -1; - } - - return r; -} - static int cgroup_rmdir(struct hierarchy **hierarchies, const char *container_cgroup) { diff --git a/src/lxc/tools/lxc_ls.c b/src/lxc/tools/lxc_ls.c index 475dcfdfe..bba72e8d5 100644 --- a/src/lxc/tools/lxc_ls.c +++ b/src/lxc/tools/lxc_ls.c @@ -160,8 +160,6 @@ static int ls_remove_lock(const char *path, const char *name, static int ls_serialize(int wpipefd, struct ls *n); static int my_parser(struct lxc_arguments *args, int c, char *arg); -static int rm_r(char *dirname); - static const struct option my_longopts[] = { {"line", no_argument, 0, '1'}, {"fancy", no_argument, 0, 'f'}, @@ -1088,7 +1086,10 @@ static int ls_remove_lock(const char *path, const char *name, if (check < 0 || (size_t)check >= *len_lockpath) goto out; - (void)rm_r(*lockpath); + ret = recursive_destroy(*lockpath); + if (ret < 0) + WARN("Failed to destroy \"%s\"", *lockpath); + ret = 0; out: @@ -1323,51 +1324,3 @@ static void ls_field_width(const struct ls *l, const size_t size, } } } - -static int rm_r(char *dirname) -{ - int ret; - struct dirent *direntp; - DIR *dir; - int r = 0; - - dir = opendir(dirname); - if (!dir) - return -1; - - while ((direntp = readdir(dir))) { - char *pathname; - struct stat mystat; - - if (!strcmp(direntp->d_name, ".") || - !strcmp(direntp->d_name, "..")) - continue; - - pathname = must_make_path(dirname, direntp->d_name, NULL); - - ret = lstat(pathname, &mystat); - if (ret < 0) { - r = -1; - goto next; - } - - if (!S_ISDIR(mystat.st_mode)) - goto next; - - ret = rm_r(pathname); - if (ret < 0) - r = -1; - next: - free(pathname); - } - - ret = rmdir(dirname); - if (ret < 0) - r = -1; - - ret = closedir(dir); - if (ret < 0) - r = -1; - - return r; -} diff --git a/src/lxc/utils.c b/src/lxc/utils.c index fb7ae2023..e2ee8229d 100644 --- a/src/lxc/utils.c +++ b/src/lxc/utils.c @@ -2708,3 +2708,63 @@ int fd_cloexec(int fd, bool cloexec) return 0; } + +int recursive_destroy(char *dirname) +{ + int ret; + struct dirent *direntp; + DIR *dir; + int r = 0; + + dir = opendir(dirname); + if (!dir) + return -1; + + while ((direntp = readdir(dir))) { + char *pathname; + struct stat mystat; + + if (!strcmp(direntp->d_name, ".") || + !strcmp(direntp->d_name, "..")) + continue; + + pathname = must_make_path(dirname, direntp->d_name, NULL); + + ret = lstat(pathname, &mystat); + if (ret < 0) { + if (!r) + WARN("Failed to stat \"%s\"", pathname); + + r = -1; + goto next; + } + + if (!S_ISDIR(mystat.st_mode)) + goto next; + + ret = recursive_destroy(pathname); + if (ret < 0) + r = -1; + + next: + free(pathname); + } + + ret = rmdir(dirname); + if (ret < 0) { + if (!r) + SYSWARN("Failed to delete \"%s\"", dirname); + + r = -1; + } + + ret = closedir(dir); + if (ret < 0) { + if (!r) + SYSWARN("Failed to delete \"%s\"", dirname); + + r = -1; + } + + return r; +} diff --git a/src/lxc/utils.h b/src/lxc/utils.h index 4a748cd1b..295e7862c 100644 --- a/src/lxc/utils.h +++ b/src/lxc/utils.h @@ -616,5 +616,6 @@ static inline pid_t lxc_raw_gettid(void) /* Set a signal the child process will receive after the parent has died. */ extern int lxc_set_death_signal(int signal); extern int fd_cloexec(int fd, bool cloexec); +extern int recursive_destroy(char *dirname); #endif /* __LXC_UTILS_H */ -- 2.39.2