From: Joel Rosdahl Date: Sun, 1 Oct 2023 08:23:18 +0000 (+0200) Subject: refactor: Move File to util::FileStream X-Git-Tag: v4.9~43 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ea9a264e412950e162bf8689603434e688bc897d;p=thirdparty%2Fccache.git refactor: Move File to util::FileStream --- diff --git a/src/Context.hpp b/src/Context.hpp index aeb3e0af8..51b4787b8 100644 --- a/src/Context.hpp +++ b/src/Context.hpp @@ -21,9 +21,9 @@ #include "Args.hpp" #include "ArgsInfo.hpp" #include "Config.hpp" -#include "File.hpp" #include "MiniTrace.hpp" +#include #include #ifdef INODE_CACHE_SUPPORTED @@ -105,7 +105,7 @@ public: pid_t compiler_pid = 0; // Files used by the hash debugging functionality. - std::vector hash_debug_files; + std::vector hash_debug_files; // Options to ignore for the hash. const std::vector& ignore_options() const; diff --git a/src/Logging.cpp b/src/Logging.cpp index 4090335f9..1705024d7 100644 --- a/src/Logging.cpp +++ b/src/Logging.cpp @@ -20,11 +20,11 @@ #include "Logging.hpp" #include "Config.hpp" -#include "File.hpp" #include "Util.hpp" #include "Win32Util.hpp" #include "execute.hpp" +#include #include #include #include @@ -48,7 +48,7 @@ namespace { // Logfile path and file handle, read from Config::log_file(). std::string logfile_path; -File logfile; +util::FileStream logfile; // Whether to use syslog() instead. bool use_syslog = false; @@ -182,7 +182,7 @@ dump_log(const std::string& path) if (!enabled()) { return; } - File file(path, "w"); + util::FileStream file(path, "w"); if (file) { (void)fwrite(debug_log_buffer.data(), debug_log_buffer.length(), 1, *file); } else { diff --git a/src/ccache.cpp b/src/ccache.cpp index ee1497d93..3e1009b4f 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -23,7 +23,6 @@ #include "ArgsInfo.hpp" #include "Context.hpp" #include "Depfile.hpp" -#include "File.hpp" #include "Hash.hpp" #include "Logging.hpp" #include "MiniTrace.hpp" @@ -49,6 +48,7 @@ #include #include #include +#include #include #include #include @@ -230,7 +230,7 @@ init_hash_debug(Context& ctx, ctx.time_of_invocation, ctx.args_info.output_obj, FMT("input-{}", type)); - File debug_binary_file(path, "wb"); + util::FileStream debug_binary_file(path, "wb"); if (debug_binary_file) { hash.enable_debug(section_name, debug_binary_file.get(), debug_text_file); ctx.hash_debug_files.push_back(std::move(debug_binary_file)); @@ -2496,7 +2496,7 @@ do_cache_compilation(Context& ctx) ctx.time_of_invocation, ctx.args_info.orig_output_obj, "input-text"); - File debug_text_file(path, "w"); + util::FileStream debug_text_file(path, "w"); if (debug_text_file) { ctx.hash_debug_files.push_back(std::move(debug_text_file)); } else { diff --git a/src/core/Result.cpp b/src/core/Result.cpp index c490aceba..84bc8fd90 100644 --- a/src/core/Result.cpp +++ b/src/core/Result.cpp @@ -20,7 +20,6 @@ #include "Config.hpp" #include "Context.hpp" -#include "File.hpp" #include "Logging.hpp" #include "Util.hpp" @@ -31,6 +30,7 @@ #include #include #include +#include #include #include #include diff --git a/src/core/StatsLog.cpp b/src/core/StatsLog.cpp index 71b645562..651fbe088 100644 --- a/src/core/StatsLog.cpp +++ b/src/core/StatsLog.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2021 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,9 +18,9 @@ #include "StatsLog.hpp" -#include #include #include +#include #include #include @@ -56,7 +56,7 @@ void StatsLog::log_result(const std::string& input_file, const std::vector& result_ids) { - File file(m_path, "ab"); + util::FileStream file(m_path, "ab"); if (!file) { LOG("Failed to open {}: {}", m_path, strerror(errno)); return; diff --git a/src/core/mainoptions.cpp b/src/core/mainoptions.cpp index 1e9c85b8a..676433f0f 100644 --- a/src/core/mainoptions.cpp +++ b/src/core/mainoptions.cpp @@ -19,7 +19,6 @@ #include "mainoptions.hpp" #include -#include #include #include #include @@ -38,6 +37,7 @@ #include #include #include +#include #include #include #include diff --git a/src/execute.cpp b/src/execute.cpp index 68450fb09..3eb22193d 100644 --- a/src/execute.cpp +++ b/src/execute.cpp @@ -92,7 +92,7 @@ win32getshell(const std::string& path) } if (sh.empty() && getenv("CCACHE_DETECT_SHEBANG")) { // Detect shebang. - File fp(path, "r"); + util::FileStream fp(path, "r"); if (fp) { char buf[10] = {0}; fgets(buf, sizeof(buf) - 1, fp.get()); diff --git a/src/storage/local/LocalStorage.cpp b/src/storage/local/LocalStorage.cpp index d130843a0..4cc50b86d 100644 --- a/src/storage/local/LocalStorage.cpp +++ b/src/storage/local/LocalStorage.cpp @@ -20,7 +20,6 @@ #include #include -#include #include #include #include @@ -32,6 +31,7 @@ #include #include #include +#include #include #include #include diff --git a/src/File.hpp b/src/util/FileStream.hpp similarity index 68% rename from src/File.hpp rename to src/util/FileStream.hpp index cb4d177bd..e50e47a08 100644 --- a/src/File.hpp +++ b/src/util/FileStream.hpp @@ -23,16 +23,18 @@ #include #include -class File : util::NonCopyable +namespace util { + +class FileStream : util::NonCopyable { public: - File() = default; - explicit File(FILE* file); - File(const std::string& path, const char* mode); - File(File&& other) noexcept; - ~File(); + FileStream() = default; + explicit FileStream(FILE* file); + FileStream(const std::string& path, const char* mode); + FileStream(FileStream&& other) noexcept; + ~FileStream(); - File& operator=(File&& other) noexcept; + FileStream& operator=(FileStream&& other) noexcept; void open(const std::string& path, const char* mode); void close(); @@ -46,16 +48,16 @@ private: bool m_owned = false; }; -inline File::File(FILE* const file) : m_file(file), m_owned(false) +inline FileStream::FileStream(FILE* const file) : m_file(file), m_owned(false) { } -inline File::File(const std::string& path, const char* mode) +inline FileStream::FileStream(const std::string& path, const char* mode) { open(path, mode); } -inline File::File(File&& other) noexcept +inline FileStream::FileStream(FileStream&& other) noexcept : m_file(other.m_file), m_owned(other.m_owned) { @@ -63,13 +65,13 @@ inline File::File(File&& other) noexcept other.m_owned = false; } -inline File::~File() +inline FileStream::~FileStream() { close(); } -inline File& -File::operator=(File&& other) noexcept +inline FileStream& +FileStream::operator=(FileStream&& other) noexcept { m_file = other.m_file; m_owned = other.m_owned; @@ -79,7 +81,7 @@ File::operator=(File&& other) noexcept } inline void -File::open(const std::string& path, const char* mode) +FileStream::open(const std::string& path, const char* mode) { close(); m_file = fopen(path.c_str(), mode); @@ -87,7 +89,7 @@ File::open(const std::string& path, const char* mode) } inline void -File::close() +FileStream::close() { if (m_file && m_owned) { fclose(m_file); @@ -96,19 +98,21 @@ File::close() m_owned = false; } -inline File::operator bool() const +inline FileStream::operator bool() const { return m_file; } inline FILE* -File::operator*() const +FileStream::operator*() const { return m_file; } inline FILE* -File::get() +FileStream::get() { return m_file; } + +} // namespace util