From: Joel Rosdahl Date: Tue, 18 Jul 2023 17:26:13 +0000 (+0200) Subject: refactor: Move Util::ensure_dir_exists to util X-Git-Tag: v4.9~102 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=84e6614e42c494953c9dcc43e91786510d74f058;p=thirdparty%2Fccache.git refactor: Move Util::ensure_dir_exists to util --- diff --git a/src/Config.cpp b/src/Config.cpp index 4d947f492..9290acd41 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -24,6 +24,7 @@ #include "assertions.hpp" #include +#include #include #include #include @@ -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( diff --git a/src/TemporaryFile.cpp b/src/TemporaryFile.cpp index 4f52d58e9..e82c8d3d1 100644 --- a/src/TemporaryFile.cpp +++ b/src/TemporaryFile.cpp @@ -20,6 +20,7 @@ #include "Util.hpp" +#include #include #include #include @@ -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 diff --git a/src/Util.cpp b/src/Util.cpp index e8aa61fff..81ef5f712 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -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) { diff --git a/src/Util.hpp b/src/Util.hpp index ed90f57aa..70ef15cff 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -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); diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 6eee59c82..8e85c123d 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -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 index 000000000..c1eebaa64 --- /dev/null +++ b/src/core/common.cpp @@ -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 +#include +#include + +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 index 000000000..695238e9e --- /dev/null +++ b/src/core/common.hpp @@ -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 + +namespace core { + +// Like std::filesystem::create_directories but throws core::Fatal on error. +void ensure_dir_exists(std::string_view dir); + +} // namespace core diff --git a/src/storage/local/LocalStorage.cpp b/src/storage/local/LocalStorage.cpp index e4294963c..092b558e2 100644 --- a/src/storage/local/LocalStorage.cpp +++ b/src/storage/local/LocalStorage.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -615,7 +616,7 @@ LocalStorage::put_raw_files( const std::vector 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; } diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt index ec743312f..ddf574a5d 100644 --- a/unittest/CMakeLists.txt +++ b/unittest/CMakeLists.txt @@ -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 diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index d3879afef..498e6d3ff 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -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 index 000000000..66cd6e031 --- /dev/null +++ b/unittest/test_core_common.cpp @@ -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 +#include +#include + +#include + +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();