#include "assertions.hpp"
#include <Stat.hpp>
+#include <core/common.hpp>
#include <core/exceptions.hpp>
#include <core/types.hpp>
#include <core/wincompat.hpp>
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(
#include "Util.hpp"
+#include <core/common.hpp>
#include <core/exceptions.hpp>
#include <fmtmacros.hpp>
#include <util/file.hpp>
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
}
}
-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)
{
// 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);
Statistics.cpp
StatisticsCounters.cpp
StatsLog.cpp
+ common.cpp
mainoptions.cpp
types.cpp
)
--- /dev/null
+// 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
--- /dev/null
+// 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
#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>
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);
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.
}
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) {
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;
}
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
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("") == "");
--- /dev/null
+// 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();