]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Use std::filesystem::remove_all
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 16 Jul 2023 16:26:45 +0000 (18:26 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 18 Jul 2023 19:36:39 +0000 (21:36 +0200)
src/Util.cpp
src/Util.hpp
src/util/filesystem.hpp
unittest/main.cpp
unittest/test_Util.cpp

index 28893639ebdb6749840b6c312f5eab036a0ca31f..b5c811666d74c74fc76571d6ed32ac3b637d345f 100644 (file)
@@ -748,21 +748,4 @@ unlink_tmp(const std::string& path, UnlinkLog unlink_log)
   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
index d663efc15cff41607a4b83aff73e85fb5cae6996..9a92aa055e6e5d3772cc39b454432254a611c22c 100644 (file)
@@ -179,10 +179,4 @@ bool unlink_safe(const std::string& path,
 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
index 03f990e26fca2ee44708b1119c93b228844230bd..7f1202c25bea376f95f50cf93b5f15b603cacddf 100644 (file)
@@ -64,6 +64,7 @@ DEFINE_FS_WRAPPER(create_hard_link, (path{}, path{}))
 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{}))
index c03a4f524e817e0552e4f5ca4216c2a928f195de..ef11fcb1bbde6f042e48f51bb00a4788844433a6 100644 (file)
@@ -16,7 +16,6 @@
 // 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"
 
@@ -42,7 +41,7 @@ main(int argc, char** argv)
 
   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);
 
@@ -52,7 +51,7 @@ main(int argc, char** argv)
 
   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);
   }
index 717badad32124b881727d322101074665f91c970..065657836670698e75525ba2ccb2b89837d988a8 100644 (file)
@@ -464,42 +464,4 @@ TEST_CASE("Util::traverse")
   }
 }
 
-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();