From: Joel Rosdahl Date: Sat, 5 Aug 2023 13:22:06 +0000 (+0200) Subject: refactor: Move AtomicFile to core X-Git-Tag: v4.9~62 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=50084bb29ea819e78650affe4dcd5a970a4e7fa9;p=thirdparty%2Fccache.git refactor: Move AtomicFile to core --- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8ec9abe25..2bf8e36eb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,7 +1,6 @@ set( source_files Args.cpp - AtomicFile.cpp Config.cpp Context.cpp Depfile.cpp diff --git a/src/Config.cpp b/src/Config.cpp index 021be4888..410677534 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -18,11 +18,11 @@ #include "Config.hpp" -#include "AtomicFile.hpp" #include "MiniTrace.hpp" #include "Util.hpp" #include "assertions.hpp" +#include #include #include #include @@ -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( diff --git a/src/ccache.cpp b/src/ccache.cpp index f607275e4..b5d30dac7 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -38,7 +38,6 @@ #include "hashutil.hpp" #include "language.hpp" -#include #include #include #include diff --git a/src/AtomicFile.cpp b/src/core/AtomicFile.cpp similarity index 93% rename from src/AtomicFile.cpp rename to src/core/AtomicFile.cpp index ee64863f4..b95c6685d 100644 --- a/src/AtomicFile.cpp +++ b/src/core/AtomicFile.cpp @@ -16,10 +16,8 @@ // 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 +#include #include #include #include @@ -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(util::TemporaryFile::create(path)); @@ -92,3 +92,5 @@ AtomicFile::commit() result.error().message())); } } + +} // namespace core diff --git a/src/AtomicFile.hpp b/src/core/AtomicFile.hpp similarity index 87% rename from src/AtomicFile.hpp rename to src/core/AtomicFile.hpp index 89dd05902..666dda948 100644 --- a/src/AtomicFile.hpp +++ b/src/core/AtomicFile.hpp @@ -23,16 +23,18 @@ #include #include #include -#include +#include + +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 diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 8e85c123d..eadb9f92b 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -1,5 +1,6 @@ set( sources + AtomicFile.cpp CacheEntry.cpp FileRecompressor.cpp Manifest.cpp diff --git a/src/core/FileRecompressor.cpp b/src/core/FileRecompressor.cpp index 4e2f563a6..da66dc306 100644 --- a/src/core/FileRecompressor.cpp +++ b/src/core/FileRecompressor.cpp @@ -18,8 +18,8 @@ #include "FileRecompressor.hpp" -#include #include +#include #include #include #include diff --git a/src/storage/local/LocalStorage.cpp b/src/storage/local/LocalStorage.cpp index 781fcb515..7d4fbad66 100644 --- a/src/storage/local/LocalStorage.cpp +++ b/src/storage/local/LocalStorage.cpp @@ -18,7 +18,6 @@ #include "LocalStorage.hpp" -#include #include #include #include @@ -26,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -87,6 +87,7 @@ namespace fs = util::filesystem; +using core::AtomicFile; using core::Statistic; using core::StatisticsCounters; using util::DirEntry; diff --git a/src/storage/local/StatsFile.cpp b/src/storage/local/StatsFile.cpp index af5854a7c..edd5e0039 100644 --- a/src/storage/local/StatsFile.cpp +++ b/src/storage/local/StatsFile.cpp @@ -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 #include +#include #include #include #include @@ -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))); } diff --git a/src/storage/remote/FileStorage.cpp b/src/storage/remote/FileStorage.cpp index 840c63001..3866fed6a 100644 --- a/src/storage/remote/FileStorage.cpp +++ b/src/storage/remote/FileStorage.cpp @@ -18,10 +18,10 @@ #include "FileStorage.hpp" -#include #include #include #include +#include #include #include #include @@ -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; diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt index d455ee8ba..a34c57df3 100644 --- a/unittest/CMakeLists.txt +++ b/unittest/CMakeLists.txt @@ -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 diff --git a/unittest/test_AtomicFile.cpp b/unittest/test_core_AtomicFile.cpp similarity index 94% rename from unittest/test_AtomicFile.cpp rename to unittest/test_core_AtomicFile.cpp index 25c0f6a20..fd3d9ede3 100644 --- a/unittest/test_AtomicFile.cpp +++ b/unittest/test_core_AtomicFile.cpp @@ -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 #include #include @@ -27,9 +27,10 @@ #include #include +using core::AtomicFile; using TestUtil::TestContext; -TEST_SUITE_BEGIN("AtomicFile"); +TEST_SUITE_BEGIN("core::AtomicFile"); TEST_CASE("Base case") {