From e1ee669affe9e08d20735b80d10abc0a5eb06751 Mon Sep 17 00:00:00 2001 From: Vlad Zolotarov Date: Mon, 6 Feb 2017 13:44:16 -0500 Subject: [PATCH] database: lister: add the rmdir() static method Removes the directory with all its contents (like 'rm -rf ' shell command). Signed-off-by: Vlad Zolotarov --- database.cc | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/database.cc b/database.cc index 47c90a415f..456e3aabf4 100644 --- a/database.cc +++ b/database.cc @@ -780,6 +780,7 @@ public: static future<> scan_dir(sstring dir, dir_entry_types type, show_hidden do_show_hidden, walker_type walker) { return scan_dir(path(std::move(dir)), std::move(type), do_show_hidden, std::move(walker), [] (const path& parent_dir, const directory_entry& entry) { return true; }); } + static future<> rmdir(path dir); protected: future<> _visit(directory_entry de) { @@ -3193,6 +3194,22 @@ const sstring& database::get_snitch_name() const { return _cfg->endpoint_snitch(); } +future<> lister::rmdir(lister::path dir) { + // first, kill the contents of the directory + return lister::scan_dir(dir, {}, show_hidden::yes, [] (lister::path parent_dir, directory_entry de) mutable { + path current_entry_path(parent_dir / de.name.c_str()); + + if (de.type.value() == directory_entry_type::directory) { + return rmdir(std::move(current_entry_path)); + } else { + return io_check(remove_file, current_entry_path.native()); + } + }).then([dir] { + // ...then kill the directory itself + return io_check(remove_file, dir.native()); + }); +} + // For the filesystem operations, this code will assume that all keyspaces are visible in all shards // (as we have been doing for a lot of the other operations, like the snapshot itself). future<> database::clear_snapshot(sstring tag, std::vector keyspace_names) {