From: Joel Rosdahl Date: Fri, 4 Aug 2023 05:57:25 +0000 (+0200) Subject: refactor: fs::path-ify TemporaryFile X-Git-Tag: v4.9~72 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9e86c21c131d2a950653d3d52df37cd80a3d7afa;p=thirdparty%2Fccache.git refactor: fs::path-ify TemporaryFile --- diff --git a/src/MiniTrace.cpp b/src/MiniTrace.cpp index 3e7e6e320..ca65a90ab 100644 --- a/src/MiniTrace.cpp +++ b/src/MiniTrace.cpp @@ -41,7 +41,7 @@ MiniTrace::MiniTrace(const ArgsInfo& args_info) if (!tmp_dir) { tmp_dir = "/tmp"; } - TemporaryFile tmp_file((*tmp_dir / "ccache-trace").string()); + TemporaryFile tmp_file(*tmp_dir / "ccache-trace"); m_tmp_trace_file = tmp_file.path; mtr_init(m_tmp_trace_file.c_str()); diff --git a/src/TemporaryFile.cpp b/src/TemporaryFile.cpp index e82c8d3d1..c933fd2e9 100644 --- a/src/TemporaryFile.cpp +++ b/src/TemporaryFile.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -36,11 +37,16 @@ # include "third_party/win32/mktemp.h" #endif -TemporaryFile::TemporaryFile(std::string_view path_prefix, +namespace fs = util::filesystem; + +TemporaryFile::TemporaryFile(const fs::path& path_prefix, std::string_view suffix) - : path(FMT("{}{}XXXXXX{}", path_prefix, tmp_file_infix, suffix)) { - core::ensure_dir_exists(Util::dir_name(path)); + if (path_prefix.has_parent_path()) { + core::ensure_dir_exists(path_prefix.parent_path()); + } + std::string path_template = + FMT("{}{}XXXXXX{}", path_prefix, tmp_file_infix, suffix); #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 @@ -48,14 +54,15 @@ TemporaryFile::TemporaryFile(std::string_view path_prefix, // [1]: - fd = Fd(bsd_mkstemps(&path[0], suffix.length())); + fd = Fd(bsd_mkstemps(&path_template[0], suffix.length())); #else - fd = Fd(mkstemps(&path[0], suffix.length())); + fd = Fd(mkstemps(&path_template[0], suffix.length())); #endif if (!fd) { throw core::Fatal( FMT("Failed to create temporary file for {}: {}", path, strerror(errno))); } + path = path_template; util::set_cloexec_flag(*fd); #ifndef _WIN32 @@ -64,7 +71,7 @@ TemporaryFile::TemporaryFile(std::string_view path_prefix, } bool -TemporaryFile::is_tmp_file(std::string_view path) +TemporaryFile::is_tmp_file(const fs::path& path) { - return Util::base_name(path).find(tmp_file_infix) != std::string::npos; + return path.filename().string().find(tmp_file_infix) != std::string::npos; } diff --git a/src/TemporaryFile.hpp b/src/TemporaryFile.hpp index b773c0a4b..089d92c90 100644 --- a/src/TemporaryFile.hpp +++ b/src/TemporaryFile.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2020-2022 Joel Rosdahl and other contributors +// Copyright (C) 2020-2023 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -18,8 +18,9 @@ #pragma once -#include "Fd.hpp" +#include +#include #include #include @@ -33,17 +34,18 @@ public: // `path_prefix` is the base path. The resulting filename will be this path // plus a unique string plus `suffix`. If `path_prefix` refers to a // nonexistent directory the directory will be created if possible. - TemporaryFile(std::string_view path_prefix, std::string_view suffix = ".tmp"); + TemporaryFile(const std::filesystem::path& path_prefix, + std::string_view suffix = ".tmp"); TemporaryFile(TemporaryFile&& other) noexcept = default; TemporaryFile& operator=(TemporaryFile&& other) noexcept = default; - static bool is_tmp_file(std::string_view path); + static bool is_tmp_file(const std::filesystem::path& path); // The resulting open file descriptor in read/write mode. Unset on error. Fd fd; // The actual filename. Empty on error. - std::string path; + std::filesystem::path path; }; diff --git a/src/storage/local/LocalStorage.cpp b/src/storage/local/LocalStorage.cpp index e97ac996a..55640f93d 100644 --- a/src/storage/local/LocalStorage.cpp +++ b/src/storage/local/LocalStorage.cpp @@ -332,7 +332,7 @@ clean_dir( // Delete any tmp files older than 1 hour right away. if (file.mtime() + util::Duration(3600) < current_time - && TemporaryFile::is_tmp_file(file.path().string())) { + && TemporaryFile::is_tmp_file(file.path())) { util::remove(file.path().string()); continue; } @@ -893,7 +893,7 @@ LocalStorage::recompress(const std::optional level, incompressible_size += file.size_on_disk(); } }); - } else if (!TemporaryFile::is_tmp_file(file.path().string())) { + } else if (!TemporaryFile::is_tmp_file(file.path())) { incompressible_size += file.size_on_disk(); }