]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Move Util::ensure_dir_exists to util
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 18 Jul 2023 17:26:13 +0000 (19:26 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 18 Jul 2023 19:55:03 +0000 (21:55 +0200)
src/Config.cpp
src/TemporaryFile.cpp
src/Util.cpp
src/Util.hpp
src/core/CMakeLists.txt
src/core/common.cpp [new file with mode: 0644]
src/core/common.hpp [new file with mode: 0644]
src/storage/local/LocalStorage.cpp
unittest/CMakeLists.txt
unittest/test_Util.cpp
unittest/test_core_common.cpp [new file with mode: 0644]

index 4d947f492c124f0fc3784074f06047adb3cf3df2..9290acd41ca3f99e7a6edb82665aab5331820a48 100644 (file)
@@ -24,6 +24,7 @@
 #include "assertions.hpp"
 
 #include <Stat.hpp>
+#include <core/common.hpp>
 #include <core/exceptions.hpp>
 #include <core/types.hpp>
 #include <core/wincompat.hpp>
@@ -876,7 +877,7 @@ Config::set_value_in_file(const std::string& path,
   const auto resolved_path = util::real_path(path);
   const auto st = Stat::stat(resolved_path);
   if (!st) {
-    Util::ensure_dir_exists(Util::dir_name(resolved_path));
+    core::ensure_dir_exists(Util::dir_name(resolved_path));
     const auto result = util::write_file(resolved_path, "");
     if (!result) {
       throw core::Error(
index 4f52d58e9b82c5575b679af08551e37657dd2257..e82c8d3d1c9e9c0d9a4546156bc414194d28d558 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "Util.hpp"
 
+#include <core/common.hpp>
 #include <core/exceptions.hpp>
 #include <fmtmacros.hpp>
 #include <util/file.hpp>
@@ -39,7 +40,7 @@ TemporaryFile::TemporaryFile(std::string_view path_prefix,
                              std::string_view suffix)
   : path(FMT("{}{}XXXXXX{}", path_prefix, tmp_file_infix, suffix))
 {
-  Util::ensure_dir_exists(Util::dir_name(path));
+  core::ensure_dir_exists(Util::dir_name(path));
 #ifdef _WIN32
   // MSVC lacks mkstemps() and Mingw-w64's implementation[1] is problematic, as
   // it can reuse the names of recently-deleted files unless the caller
index e8aa61fff4dc80a9fc50fa4a1b45656532f46e67..81ef5f712669d2a7caafc2bf6ad1c348b896e86a 100644 (file)
@@ -212,15 +212,6 @@ dir_name(std::string_view path)
   }
 }
 
-void
-ensure_dir_exists(std::string_view dir)
-{
-  if (auto result = fs::create_directories(dir); !result) {
-    throw core::Fatal(
-      FMT("Failed to create directory {}: {}", dir, result.error().message()));
-  }
-}
-
 std::string_view
 get_extension(std::string_view path)
 {
index ed90f57aa0e3b384a589e9337323a4d62df37495..70ef15cffbf05a9312ce88548ad73dfee86c4220 100644 (file)
@@ -52,9 +52,6 @@ size_t common_dir_prefix_length(std::string_view dir, std::string_view path);
 // Get directory name of path.
 std::string_view dir_name(std::string_view path);
 
-// Like create_dir but throws Fatal on error.
-void ensure_dir_exists(std::string_view dir);
-
 // Return the file extension (including the dot) as a view into `path`. If
 // `path` has no file extension, an empty string_view is returned.
 std::string_view get_extension(std::string_view path);
index 6eee59c8230b7977a536557f68d07071a470e6b1..8e85c123df1c7dca391250e24f9f74bbd20aab48 100644 (file)
@@ -11,6 +11,7 @@ set(
   Statistics.cpp
   StatisticsCounters.cpp
   StatsLog.cpp
+  common.cpp
   mainoptions.cpp
   types.cpp
 )
diff --git a/src/core/common.cpp b/src/core/common.cpp
new file mode 100644 (file)
index 0000000..c1eebaa
--- /dev/null
@@ -0,0 +1,38 @@
+// Copyright (C) 2023 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#include "common.hpp"
+
+#include <core/exceptions.hpp>
+#include <fmtmacros.hpp>
+#include <util/filesystem.hpp>
+
+namespace fs = util::filesystem;
+
+namespace core {
+
+void
+ensure_dir_exists(std::string_view dir)
+{
+  if (auto result = fs::create_directories(dir); !result) {
+    throw core::Fatal(
+      FMT("Failed to create directory {}: {}", dir, result.error().message()));
+  }
+}
+
+} // namespace core
diff --git a/src/core/common.hpp b/src/core/common.hpp
new file mode 100644 (file)
index 0000000..695238e
--- /dev/null
@@ -0,0 +1,28 @@
+// Copyright (C) 2023 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#pragma once
+
+#include <string_view>
+
+namespace core {
+
+// Like std::filesystem::create_directories but throws core::Fatal on error.
+void ensure_dir_exists(std::string_view dir);
+
+} // namespace core
index e4294963ccb96445ba9df6a757a81f4f777adb50..092b558e2561777725d919c639463577c27ccfcf 100644 (file)
@@ -32,6 +32,7 @@
 #include <core/FileRecompressor.hpp>
 #include <core/Manifest.hpp>
 #include <core/Statistics.hpp>
+#include <core/common.hpp>
 #include <core/exceptions.hpp>
 #include <core/wincompat.hpp>
 #include <fmtmacros.hpp>
@@ -615,7 +616,7 @@ LocalStorage::put_raw_files(
   const std::vector<core::Result::Serializer::RawFile> raw_files)
 {
   const auto cache_file = look_up_cache_file(key, core::CacheEntryType::result);
-  Util::ensure_dir_exists(Util::dir_name(cache_file.path));
+  core::ensure_dir_exists(Util::dir_name(cache_file.path));
 
   for (auto [file_number, source_path] : raw_files) {
     const auto dest_path = get_raw_file_path(cache_file.path, file_number);
@@ -1042,7 +1043,7 @@ LocalStorage::move_to_wanted_cache_level(const StatisticsCounters& counters,
   const auto wanted_path = get_path_in_cache(
     wanted_level, util::format_digest(key) + suffix_from_type(type));
   if (cache_file_path != wanted_path) {
-    Util::ensure_dir_exists(Util::dir_name(wanted_path));
+    core::ensure_dir_exists(Util::dir_name(wanted_path));
 
     // Note: Two ccache processes may move the file at the same time, so failure
     // to rename is OK.
@@ -1410,7 +1411,7 @@ LocalStorage::clean_internal_tempdir()
   }
 
   LOG("Cleaning up {}", m_config.temporary_dir());
-  Util::ensure_dir_exists(m_config.temporary_dir());
+  core::ensure_dir_exists(m_config.temporary_dir());
   Util::traverse(m_config.temporary_dir(),
                  [now](const std::string& path, bool is_dir) {
                    if (is_dir) {
@@ -1451,7 +1452,7 @@ std::string
 LocalStorage::get_lock_path(const std::string& name) const
 {
   auto path = FMT("{}/lock/{}", m_config.cache_dir(), name);
-  Util::ensure_dir_exists(Util::dir_name(path));
+  core::ensure_dir_exists(Util::dir_name(path));
   return path;
 }
 
index ec743312f95dff61008844ed07cd97a50e8b170f..ddf574a5d30da81e79d531ef4de3b7045a703c4f 100644 (file)
@@ -17,6 +17,7 @@ set(
   test_core_Statistics.cpp
   test_core_StatisticsCounters.cpp
   test_core_StatsLog.cpp
+  test_core_common.cpp
   test_hashutil.cpp
   test_storage_local_StatsFile.cpp
   test_storage_local_util.cpp
index d3879afef337880fe528f742d65295210089a829..498e6d3ffe60172e5fc808ab67e8d9b1156fc252 100644 (file)
@@ -124,21 +124,6 @@ TEST_CASE("Util::strip_ansi_csi_seqs")
   CHECK(Util::strip_ansi_csi_seqs(input) == "Normal, bold, red, bold green.\n");
 }
 
-TEST_CASE("Util::ensure_dir_exists")
-{
-  TestContext test_context;
-
-  CHECK_NOTHROW(Util::ensure_dir_exists("/"));
-
-  CHECK_NOTHROW(Util::ensure_dir_exists("create/dir"));
-  CHECK(Stat::stat("create/dir").is_directory());
-
-  util::write_file("create/dir/file", "");
-  CHECK_THROWS_WITH(
-    Util::ensure_dir_exists("create/dir/file"),
-    doctest::Contains("Failed to create directory create/dir/file:"));
-}
-
 TEST_CASE("Util::get_extension")
 {
   CHECK(Util::get_extension("") == "");
diff --git a/unittest/test_core_common.cpp b/unittest/test_core_common.cpp
new file mode 100644 (file)
index 0000000..66cd6e0
--- /dev/null
@@ -0,0 +1,46 @@
+// Copyright (C) 2019-2023 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#include "TestUtil.hpp"
+
+#include <Stat.hpp>
+#include <core/common.hpp>
+#include <util/file.hpp>
+
+#include <third_party/doctest.h>
+
+using TestUtil::TestContext;
+
+TEST_SUITE_BEGIN("core");
+
+TEST_CASE("core::ensure_dir_exists")
+{
+  TestContext test_context;
+
+  CHECK_NOTHROW(core::ensure_dir_exists("/"));
+
+  CHECK_NOTHROW(core::ensure_dir_exists("create/dir"));
+  CHECK(Stat::stat("create/dir").is_directory());
+
+  util::write_file("create/dir/file", "");
+  CHECK_THROWS_WITH(
+    core::ensure_dir_exists("create/dir/file"),
+    doctest::Contains("Failed to create directory create/dir/file:"));
+}
+
+TEST_SUITE_END();