]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Move AtomicFile to core
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 5 Aug 2023 13:22:06 +0000 (15:22 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 5 Aug 2023 13:22:06 +0000 (15:22 +0200)
12 files changed:
src/CMakeLists.txt
src/Config.cpp
src/ccache.cpp
src/core/AtomicFile.cpp [moved from src/AtomicFile.cpp with 93% similarity]
src/core/AtomicFile.hpp [moved from src/AtomicFile.hpp with 87% similarity]
src/core/CMakeLists.txt
src/core/FileRecompressor.cpp
src/storage/local/LocalStorage.cpp
src/storage/local/StatsFile.cpp
src/storage/remote/FileStorage.cpp
unittest/CMakeLists.txt
unittest/test_core_AtomicFile.cpp [moved from unittest/test_AtomicFile.cpp with 94% similarity]

index 8ec9abe259c1ccb47b8a0c62d85160a97a2ad064..2bf8e36ebc58373528625a334e481058795523a3 100644 (file)
@@ -1,7 +1,6 @@
 set(
   source_files
   Args.cpp
-  AtomicFile.cpp
   Config.cpp
   Context.cpp
   Depfile.cpp
index 021be48888e74afdbd946b08bf4c311f31bd01be..4106775349f362c5b6f6b21060f8d1f5706854b5 100644 (file)
 
 #include "Config.hpp"
 
-#include "AtomicFile.hpp"
 #include "MiniTrace.hpp"
 #include "Util.hpp"
 #include "assertions.hpp"
 
+#include <core/AtomicFile.hpp>
 #include <core/common.hpp>
 #include <core/exceptions.hpp>
 #include <core/types.hpp>
@@ -923,7 +923,7 @@ Config::set_value_in_file(const std::string& path,
       FMT("failed to write to {}: ", resolved_path));
   }
 
-  AtomicFile output(resolved_path, AtomicFile::Mode::text);
+  core::AtomicFile output(resolved_path, core::AtomicFile::Mode::text);
   bool found = false;
 
   if (!parse_config_file(
index f607275e4e15f1dc4ffcb2d8926c627ba9767d68..b5d30dac7a20a80fc2547788eb52b8fa8fceb711 100644 (file)
@@ -38,7 +38,6 @@
 #include "hashutil.hpp"
 #include "language.hpp"
 
-#include <AtomicFile.hpp>
 #include <core/CacheEntry.hpp>
 #include <core/Manifest.hpp>
 #include <core/MsvcShowIncludesOutput.hpp>
similarity index 93%
rename from src/AtomicFile.cpp
rename to src/core/AtomicFile.cpp
index ee64863f46e4fa12a080452131608c55ff27cd75..b95c6685d4978c91d86678d22fd5b51b28ef3c5d 100644 (file)
 // this program; if not, write to the Free Software Foundation, Inc., 51
 // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
-#include "AtomicFile.hpp"
-
-#include "assertions.hpp"
-
+#include <assertions.hpp>
+#include <core/AtomicFile.hpp>
 #include <core/exceptions.hpp>
 #include <fmtmacros.hpp>
 #include <util/TemporaryFile.hpp>
@@ -29,7 +27,9 @@
 
 namespace fs = util::filesystem;
 
-AtomicFile::AtomicFile(const std::string& path, Mode mode) : m_path(path)
+namespace core {
+
+AtomicFile::AtomicFile(const fs::path& path, Mode mode) : m_path(path)
 {
   auto tmp_file =
     util::value_or_throw<core::Fatal>(util::TemporaryFile::create(path));
@@ -92,3 +92,5 @@ AtomicFile::commit()
                           result.error().message()));
   }
 }
+
+} // namespace core
similarity index 87%
rename from src/AtomicFile.hpp
rename to src/core/AtomicFile.hpp
index 89dd05902d11df537ccd361ee1a01524ac5facf7..666dda948b5a86fb889f9f54ff279b00a563ce0b 100644 (file)
 #include <cstdint>
 #include <cstdio>
 #include <filesystem>
-#include <string>
+#include <string_view>
+
+namespace core {
 
 // This class represents a file whose data will be atomically written to a path
-// by renaming a temporary file in place.
+// by renaming a temporary file in place. Throws core::Error on error.
 class AtomicFile
 {
 public:
   enum class Mode { binary, text };
 
-  AtomicFile(const std::string& path, Mode mode);
+  AtomicFile(const std::filesystem::path& path, Mode mode);
   ~AtomicFile();
 
   FILE* stream();
@@ -47,7 +49,7 @@ public:
   void commit();
 
 private:
-  const std::string m_path;
+  std::filesystem::path m_path;
   std::filesystem::path m_tmp_path;
   FILE* m_stream;
 };
@@ -57,3 +59,5 @@ AtomicFile::stream()
 {
   return m_stream;
 }
+
+} // namespace core
index 8e85c123df1c7dca391250e24f9f74bbd20aab48..eadb9f92b17d5dffa77a687c94ba98fdda718e69 100644 (file)
@@ -1,5 +1,6 @@
 set(
   sources
+  AtomicFile.cpp
   CacheEntry.cpp
   FileRecompressor.cpp
   Manifest.cpp
index 4e2f563a6e65f554d1f1b185245666cd9e450471..da66dc30611e96d491bbac7ab92317601cccab63 100644 (file)
@@ -18,8 +18,8 @@
 
 #include "FileRecompressor.hpp"
 
-#include <AtomicFile.hpp>
 #include <Util.hpp>
+#include <core/AtomicFile.hpp>
 #include <core/CacheEntry.hpp>
 #include <core/exceptions.hpp>
 #include <util/expected.hpp>
index 781fcb515d90ba4574e71ea12e31e298eb921d63..7d4fbad66706a04ebd8f0199094a2843a519b9a7 100644 (file)
@@ -18,7 +18,6 @@
 
 #include "LocalStorage.hpp"
 
-#include <AtomicFile.hpp>
 #include <Config.hpp>
 #include <Context.hpp>
 #include <File.hpp>
@@ -26,6 +25,7 @@
 #include <MiniTrace.hpp>
 #include <Util.hpp>
 #include <assertions.hpp>
+#include <core/AtomicFile.hpp>
 #include <core/CacheEntry.hpp>
 #include <core/FileRecompressor.hpp>
 #include <core/Manifest.hpp>
@@ -87,6 +87,7 @@
 
 namespace fs = util::filesystem;
 
+using core::AtomicFile;
 using core::Statistic;
 using core::StatisticsCounters;
 using util::DirEntry;
index af5854a7c7e897b9216f80bd8926d55cd92a13c8..edd5e003997e95bfa67fde090b2d86b4a9e88414 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2021-2022 Joel Rosdahl and other contributors
+// Copyright (C) 2021-2023 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -18,8 +18,8 @@
 
 #include "StatsFile.hpp"
 
-#include <AtomicFile.hpp>
 #include <Logging.hpp>
+#include <core/AtomicFile.hpp>
 #include <core/exceptions.hpp>
 #include <fmtmacros.hpp>
 #include <util/LockFile.hpp>
@@ -71,7 +71,7 @@ StatsFile::update(
   auto counters = read();
   function(counters);
 
-  AtomicFile file(m_path, AtomicFile::Mode::text);
+  core::AtomicFile file(m_path, core::AtomicFile::Mode::text);
   for (size_t i = 0; i < counters.size(); ++i) {
     file.write(FMT("{}\n", counters.get_raw(i)));
   }
index 840c63001026aea13f32cdacb58eb58051d60f1a..3866fed6a708bd25e1864d6788df0272a50866d9 100644 (file)
 
 #include "FileStorage.hpp"
 
-#include <AtomicFile.hpp>
 #include <Logging.hpp>
 #include <Util.hpp>
 #include <assertions.hpp>
+#include <core/AtomicFile.hpp>
 #include <core/exceptions.hpp>
 #include <fmtmacros.hpp>
 #include <util/Bytes.hpp>
@@ -161,7 +161,7 @@ FileStorageBackend::put(const Hash::Digest& key,
 
     LOG("Writing {}", path);
     try {
-      AtomicFile file(path, AtomicFile::Mode::binary);
+      core::AtomicFile file(path, core::AtomicFile::Mode::binary);
       file.write(value);
       file.commit();
       return true;
index d455ee8babade6dff17ec98efbabce47fbdefc41..a34c57df3face5a0d203ee242177f8480b557a2b 100644 (file)
@@ -3,7 +3,6 @@ set(
   TestUtil.cpp
   main.cpp
   test_Args.cpp
-  test_AtomicFile.cpp
   test_Config.cpp
   test_Depfile.cpp
   test_Hash.cpp
@@ -12,6 +11,7 @@ set(
   test_ccache.cpp
   test_compopt.cpp
   test_compression_types.cpp
+  test_core_AtomicFile.cpp
   test_core_MsvcShowIncludesOutput.cpp
   test_core_Statistics.cpp
   test_core_StatisticsCounters.cpp
similarity index 94%
rename from unittest/test_AtomicFile.cpp
rename to unittest/test_core_AtomicFile.cpp
index 25c0f6a20cc0c11b55999380c44e5d55a023fdcd..fd3d9ede3deab895d72b4b68e09f69eae5e78cd6 100644 (file)
@@ -16,9 +16,9 @@
 // this program; if not, write to the Free Software Foundation, Inc., 51
 // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
-#include "../src/AtomicFile.hpp"
 #include "TestUtil.hpp"
 
+#include <core/AtomicFile.hpp>
 #include <util/DirEntry.hpp>
 #include <util/file.hpp>
 
 #include <string>
 #include <vector>
 
+using core::AtomicFile;
 using TestUtil::TestContext;
 
-TEST_SUITE_BEGIN("AtomicFile");
+TEST_SUITE_BEGIN("core::AtomicFile");
 
 TEST_CASE("Base case")
 {