return success;
}
-void
-wipe_path(const std::string& path)
-{
- if (!Stat::lstat(path)) {
- return;
- }
- traverse(path, [](const std::string& p, bool is_dir) {
- if (is_dir) {
- if (rmdir(p.c_str()) != 0 && errno != ENOENT && errno != ESTALE) {
- throw core::Error(FMT("failed to rmdir {}: {}", p, strerror(errno)));
- }
- } else if (unlink(p.c_str()) != 0 && errno != ENOENT && errno != ESTALE) {
- throw core::Error(FMT("failed to unlink {}: {}", p, strerror(errno)));
- }
- });
-}
-
} // namespace Util
bool unlink_tmp(const std::string& path,
UnlinkLog unlink_log = UnlinkLog::log_failure);
-// Remove `path` (and its contents if it's a directory). A nonexistent path is
-// not considered an error.
-//
-// Throws core::Error on error.
-void wipe_path(const std::string& path);
-
} // namespace Util
DEFINE_FS_WRAPPER(current_path, ())
DEFINE_FS_WRAPPER(read_symlink, (path{}))
DEFINE_FS_WRAPPER(remove, (path{}))
+DEFINE_FS_WRAPPER(remove_all, (path{}))
DEFINE_FS_WRAPPER(temp_directory_path, ())
DEFINE_FS_PREDICATE_WRAPPER(exists, (path{}))
// this program; if not, write to the Free Software Foundation, Inc., 51
// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-#include "../src/Util.hpp"
#include "../src/fmtmacros.hpp"
#include "TestUtil.hpp"
std::string dir_before = util::actual_cwd();
std::string testdir = FMT("testdir/{}", getpid());
- Util::wipe_path(testdir);
+ fs::remove_all(testdir);
fs::create_directories(testdir);
TestUtil::check_chdir(testdir);
if (result == 0) {
TestUtil::check_chdir(dir_before);
- Util::wipe_path(testdir);
+ fs::remove_all(testdir);
} else {
PRINT(stderr, "Note: Test data has been left in {}\n", testdir);
}
}
}
-TEST_CASE("Util::wipe_path")
-{
- TestContext test_context;
-
- SUBCASE("Wipe nonexistent path")
- {
- CHECK_NOTHROW(Util::wipe_path("a"));
- }
-
- SUBCASE("Wipe file")
- {
- util::write_file("a", "");
- CHECK_NOTHROW(Util::wipe_path("a"));
- CHECK(!Stat::stat("a"));
- }
-
- SUBCASE("Wipe directory")
- {
- REQUIRE(fs::create_directories("a/b"));
- util::write_file("a/1", "");
- util::write_file("a/b/1", "");
- CHECK_NOTHROW(Util::wipe_path("a"));
- CHECK(!Stat::stat("a"));
- }
-
- SUBCASE("Wipe bad path")
- {
-#ifdef _WIN32
- const char error[] = "failed to rmdir .: Permission denied";
-#elif defined(_AIX)
- const char error[] = "failed to rmdir .: Device busy";
-#else
- const char error[] = "failed to rmdir .: Invalid argument";
-#endif
- CHECK_THROWS_WITH(Util::wipe_path("."), error);
- }
-}
-
TEST_SUITE_END();