From 0aa39a46b707212e6487b6b537238e31bf7da1b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrez...@redhat.com> Date: Wed, 13 Jul 2016 12:17:58 +0200 Subject: [PATCH 1/2] utils: add remove_subtree
Remove all entries in a directory but will not remove the directory itself. --- src/tests/files-tests.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ src/tools/files.c | 34 ++++++++++++++++++++++++------- src/tools/tools_util.h | 1 + 3 files changed, 81 insertions(+), 7 deletions(-) diff --git a/src/tests/files-tests.c b/src/tests/files-tests.c index 596069e2858e07953b3d48f6b8015ce66e2dd423..e96a60af1817b5f7a2e99d8b09ebc91c1a52667b 100644 --- a/src/tests/files-tests.c +++ b/src/tests/files-tests.c @@ -153,6 +153,58 @@ START_TEST(test_remove_tree) } END_TEST +START_TEST(test_remove_subtree) +{ + int ret; + char origpath[PATH_MAX+1]; + + errno = 0; + fail_unless(getcwd(origpath, PATH_MAX) == origpath, "Cannot getcwd\n"); + fail_unless(errno == 0, "Cannot getcwd\n"); + + DEBUG(SSSDBG_FUNC_DATA, "About to delete %s\n", dir_path); + + /* create a file */ + ret = chdir(dir_path); + fail_if(ret == -1, "Cannot chdir1\n"); + + ret = create_simple_file("bar", "bar"); + fail_if(ret == -1, "Cannot create file1\n"); + + /* create a subdir and file inside it */ + ret = mkdir("subdir", 0700); + fail_if(ret == -1, "Cannot create subdir\n"); + + ret = chdir("subdir"); + fail_if(ret == -1, "Cannot chdir\n"); + + ret = create_simple_file("foo", "foo"); + fail_if(ret == -1, "Cannot create file\n"); + + /* create another subdir, empty this time */ + ret = mkdir("subdir2", 0700); + fail_if(ret == -1, "Cannot create subdir\n"); + + ret = chdir(origpath); + fail_if(ret == -1, "Cannot chdir2\n"); + + /* go back */ + ret = chdir(origpath); + fail_if(ret == -1, "Cannot chdir\n"); + + /* and finally wipe it out.. */ + ret = remove_subtree(dir_path); + fail_unless(ret == EOK, "remove_subtree failed\n"); + + /* check if really gone */ + ret = access(dir_path, F_OK); + fail_unless(ret == 0, "directory was deleted\n"); + + ret = rmdir(dir_path); + fail_unless(ret == 0, "unable to delete root directory\n"); +} +END_TEST + START_TEST(test_simple_copy) { int ret; @@ -337,6 +389,7 @@ static Suite *files_suite(void) teardown_files_test); tcase_add_test(tc_files, test_remove_tree); + tcase_add_test(tc_files, test_remove_subtree); tcase_add_test(tc_files, test_simple_copy); tcase_add_test(tc_files, test_copy_file); tcase_add_test(tc_files, test_copy_symlink); diff --git a/src/tools/files.c b/src/tools/files.c index 5364f5c0dd53aad71452e18b8d7f1f04532132a4..182048be4dca9b6c5255695409fe9e52b730c744 100644 --- a/src/tools/files.c +++ b/src/tools/files.c @@ -137,7 +137,8 @@ static int sss_futime_set(int fd, const struct stat *statp) static int remove_tree_with_ctx(TALLOC_CTX *mem_ctx, int parent_fd, const char *dir_name, - dev_t parent_dev); + dev_t parent_dev, + bool subtree); int remove_tree(const char *root) { @@ -149,7 +150,22 @@ int remove_tree(const char *root) return ENOMEM; } - ret = remove_tree_with_ctx(tmp_ctx, AT_FDCWD, root, 0); + ret = remove_tree_with_ctx(tmp_ctx, AT_FDCWD, root, 0, false); + talloc_free(tmp_ctx); + return ret; +} + +int remove_subtree(const char *root) +{ + TALLOC_CTX *tmp_ctx = NULL; + int ret; + + tmp_ctx = talloc_new(NULL); + if (!tmp_ctx) { + return ENOMEM; + } + + ret = remove_tree_with_ctx(tmp_ctx, AT_FDCWD, root, 0, true); talloc_free(tmp_ctx); return ret; } @@ -162,7 +178,8 @@ int remove_tree(const char *root) static int remove_tree_with_ctx(TALLOC_CTX *mem_ctx, int parent_fd, const char *dir_name, - dev_t parent_dev) + dev_t parent_dev, + bool subtree) { struct dirent *result; struct stat statres; @@ -213,7 +230,8 @@ static int remove_tree_with_ctx(TALLOC_CTX *mem_ctx, goto fail; } - ret = remove_tree_with_ctx(mem_ctx, dir_fd, result->d_name, statres.st_dev); + ret = remove_tree_with_ctx(mem_ctx, dir_fd, result->d_name, + statres.st_dev, false); if (ret != EOK) { DEBUG(SSSDBG_CRIT_FAILURE, "Removing subdirectory failed: [%d][%s]\n", @@ -238,9 +256,11 @@ static int remove_tree_with_ctx(TALLOC_CTX *mem_ctx, goto fail; } - ret = unlinkat(parent_fd, dir_name, AT_REMOVEDIR); - if (ret == -1) { - ret = errno; + if (!subtree) { + ret = unlinkat(parent_fd, dir_name, AT_REMOVEDIR); + if (ret == -1) { + ret = errno; + } } ret = EOK; diff --git a/src/tools/tools_util.h b/src/tools/tools_util.h index 93bc621fa2c98384b22ccd1066f02e9f4db10468..c9a6ac86c5cb586aa390dca8662824493b711a62 100644 --- a/src/tools/tools_util.h +++ b/src/tools/tools_util.h @@ -116,6 +116,7 @@ errno_t sss_mc_refresh_grouplist(struct tools_ctx *tctx, /* from files.c */ int remove_tree(const char *root); +int remove_subtree(const char *root); int copy_tree(const char *src_root, const char *dst_root, mode_t mode_root, uid_t uid, gid_t gid); -- 2.1.0
From 6413cb17138d8b24d579109454ae5dd7049b552a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrez...@redhat.com> Date: Wed, 13 Jul 2016 13:29:54 +0200 Subject: [PATCH 2/2] sssctl: use internal API to remove files --- src/tools/sssctl/sssctl_data.c | 6 ++---- src/tools/sssctl/sssctl_logs.c | 3 ++- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/tools/sssctl/sssctl_data.c b/src/tools/sssctl/sssctl_data.c index 3ab2ddf20006b2d5a26e2f167819677431854eb6..049c26b8f6d3b9b86c45a02712575dd6a95ca0e1 100644 --- a/src/tools/sssctl/sssctl_data.c +++ b/src/tools/sssctl/sssctl_data.c @@ -26,9 +26,7 @@ #include "tools/common/sss_tools.h" #include "tools/common/sss_process.h" #include "tools/sssctl/sssctl.h" - -#define CACHE_FILE(db) " " DB_PATH "/" db -#define CACHE_FILES CACHE_FILE("*.ldb") CACHE_FILE("ccache_*") +#include "tools/tools_util.h" #define SSS_BACKUP_DIR SSS_STATEDIR "/backup" #define SSS_BACKUP_USER_OVERRIDES SSS_BACKUP_DIR "/sssd_user_overrides.bak" @@ -243,7 +241,7 @@ errno_t sssctl_remove_cache(struct sss_cmdline *cmdline, } printf(_("Removing cache files...\n")); - ret = sssctl_run_command("rm -f " CACHE_FILES); + ret = remove_subtree(DB_PATH); if (ret != EOK) { fprintf(stderr, _("Unable to remove cache files\n")); return ret; diff --git a/src/tools/sssctl/sssctl_logs.c b/src/tools/sssctl/sssctl_logs.c index a203474648e3c1719e16146f8f7b484f9d62541c..160deea9a4e69b5d85337f913c3d111d1966f400 100644 --- a/src/tools/sssctl/sssctl_logs.c +++ b/src/tools/sssctl/sssctl_logs.c @@ -25,6 +25,7 @@ #include "tools/common/sss_tools.h" #include "tools/common/sss_process.h" #include "tools/sssctl/sssctl.h" +#include "tools/tools_util.h" #define LOG_FILE(file) " " LOG_PATH "/" file #define LOG_FILES LOG_FILE("*.log") @@ -55,7 +56,7 @@ errno_t sssctl_remove_logs(struct sss_cmdline *cmdline, if (opts.delete) { printf(_("Deleting log files...\n")); - ret = sssctl_run_command("rm -f " LOG_FILES); + ret = remove_subtree(LOG_PATH); if (ret != EOK) { fprintf(stderr, _("Unable to remove log files\n")); return ret; -- 2.1.0
_______________________________________________ sssd-devel mailing list sssd-devel@lists.fedorahosted.org https://lists.fedorahosted.org/admin/lists/sssd-devel@lists.fedorahosted.org