From: Joel Rosdahl Date: Sat, 5 Aug 2023 08:49:04 +0000 (+0200) Subject: refactor: Move Fd to util X-Git-Tag: v4.9~67 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=54cbb06ad71ac4bd1da23a0ceb782932eb49be35;p=thirdparty%2Fccache.git refactor: Move Fd to util --- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a5df950ce..9e51429cd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,7 +5,6 @@ set( Config.cpp Context.cpp Depfile.cpp - Fd.cpp Hash.cpp Logging.cpp ProgressBar.cpp diff --git a/src/Fd.cpp b/src/Fd.cpp deleted file mode 100644 index 9a0448bcf..000000000 --- a/src/Fd.cpp +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (C) 2021 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 "Fd.hpp" - -#include - -#ifdef HAVE_UNISTD_H -# include -#endif - -bool -Fd::close() -{ - return m_fd != -1 && ::close(release()) == 0; -} diff --git a/src/Hash.cpp b/src/Hash.cpp index 6e8985f6a..749d0ba0f 100644 --- a/src/Hash.cpp +++ b/src/Hash.cpp @@ -18,10 +18,10 @@ #include "Hash.hpp" -#include "Fd.hpp" #include "Logging.hpp" #include "fmtmacros.hpp" +#include #include #include #include @@ -116,7 +116,7 @@ Hash::hash_fd(int fd) tl::expected Hash::hash_file(const std::string& path) { - Fd fd(open(path.c_str(), O_RDONLY | O_BINARY)); + util::Fd fd(open(path.c_str(), O_RDONLY | O_BINARY)); if (!fd) { LOG("Failed to open {}: {}", path, strerror(errno)); return tl::unexpected(strerror(errno)); diff --git a/src/InodeCache.cpp b/src/InodeCache.cpp index 8fd1be75b..4add4bd9e 100644 --- a/src/InodeCache.cpp +++ b/src/InodeCache.cpp @@ -26,6 +26,7 @@ #include "fmtmacros.hpp" #include +#include #include #include #include @@ -237,7 +238,7 @@ InodeCache::mmap_file(const std::string& inode_cache_file) munmap(m_sr, sizeof(SharedRegion)); m_sr = nullptr; } - m_fd = Fd(open(inode_cache_file.c_str(), O_RDWR)); + m_fd = util::Fd(open(inode_cache_file.c_str(), O_RDWR)); if (!m_fd) { LOG("Failed to open inode cache {}: {}", inode_cache_file, strerror(errno)); return false; diff --git a/src/InodeCache.hpp b/src/InodeCache.hpp index 94dd00895..a5f75ec68 100644 --- a/src/InodeCache.hpp +++ b/src/InodeCache.hpp @@ -18,10 +18,10 @@ #pragma once -#include #include #include #include +#include #include #include @@ -135,7 +135,7 @@ private: const Config& m_config; util::Duration m_min_age; - Fd m_fd; + util::Fd m_fd; struct SharedRegion* m_sr = nullptr; bool m_failed = false; const pid_t m_self_pid; diff --git a/src/Util.cpp b/src/Util.cpp index d48eec21a..9281775fd 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -20,7 +20,6 @@ #include "Config.hpp" #include "Context.hpp" -#include "Fd.hpp" #include "Logging.hpp" #include "Win32Util.hpp" diff --git a/src/ccache.cpp b/src/ccache.cpp index 2b820fd53..f607275e4 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -23,7 +23,6 @@ #include "ArgsInfo.hpp" #include "Context.hpp" #include "Depfile.hpp" -#include "Fd.hpp" #include "File.hpp" #include "Finalizer.hpp" #include "Hash.hpp" @@ -52,6 +51,7 @@ #include #include #include +#include #include #include #include @@ -707,7 +707,7 @@ result_key_from_depfile(Context& ctx, Hash& hash) struct GetTmpFdResult { - Fd fd; + util::Fd fd; fs::path path; }; @@ -724,7 +724,7 @@ get_tmp_fd(Context& ctx, return {std::move(tmp_stdout.fd), std::move(tmp_stdout.path)}; } else { const auto dev_null_path = util::get_dev_null_path(); - return {Fd(open(dev_null_path, O_WRONLY | O_BINARY)), dev_null_path}; + return {util::Fd(open(dev_null_path, O_WRONLY | O_BINARY)), dev_null_path}; } } diff --git a/src/core/Result.cpp b/src/core/Result.cpp index 31b3513c8..4de7c729f 100644 --- a/src/core/Result.cpp +++ b/src/core/Result.cpp @@ -20,7 +20,6 @@ #include "Config.hpp" #include "Context.hpp" -#include "Fd.hpp" #include "File.hpp" #include "Logging.hpp" #include "Util.hpp" diff --git a/src/core/ResultExtractor.hpp b/src/core/ResultExtractor.hpp index 916bea758..33e91a3f2 100644 --- a/src/core/ResultExtractor.hpp +++ b/src/core/ResultExtractor.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,6 @@ #pragma once -#include "Fd.hpp" - #include #include diff --git a/src/core/ResultRetriever.cpp b/src/core/ResultRetriever.cpp index 83993331b..0f48c8ecb 100644 --- a/src/core/ResultRetriever.cpp +++ b/src/core/ResultRetriever.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -205,7 +206,8 @@ ResultRetriever::write_dependency_file(const std::string& path, { ASSERT(m_ctx.args_info.dependency_target); - Fd fd(open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666)); + util::Fd fd( + open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666)); if (!fd) { throw WriteError(FMT("Failed to open {} for writing", path)); } diff --git a/src/core/ResultRetriever.hpp b/src/core/ResultRetriever.hpp index fb16857c7..5632a19b5 100644 --- a/src/core/ResultRetriever.hpp +++ b/src/core/ResultRetriever.hpp @@ -18,8 +18,6 @@ #pragma once -#include "Fd.hpp" - #include #include #include diff --git a/src/core/mainoptions.cpp b/src/core/mainoptions.cpp index b7f67a985..02341b47f 100644 --- a/src/core/mainoptions.cpp +++ b/src/core/mainoptions.cpp @@ -19,7 +19,6 @@ #include "mainoptions.hpp" #include -#include #include #include #include @@ -41,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -571,7 +571,7 @@ process_main_options(int argc, const char* const* argv) case CHECKSUM_FILE: { util::XXH3_128 checksum; - Fd fd(arg == "-" ? STDIN_FILENO : open(arg.c_str(), O_RDONLY)); + util::Fd fd(arg == "-" ? STDIN_FILENO : open(arg.c_str(), O_RDONLY)); if (fd) { util::read_fd(*fd, [&checksum](auto data) { checksum.update(data); }); const auto digest = checksum.digest(); diff --git a/src/execute.cpp b/src/execute.cpp index 37dba2c2f..63f64a51c 100644 --- a/src/execute.cpp +++ b/src/execute.cpp @@ -21,7 +21,6 @@ #include "Config.hpp" #include "Context.hpp" -#include "Fd.hpp" #include "Logging.hpp" #include "SignalHandler.hpp" #include "Util.hpp" @@ -31,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -64,7 +64,10 @@ static int win32execute(const char* path, const std::string& temp_dir); int -execute(Context& ctx, const char* const* argv, Fd&& fd_out, Fd&& fd_err) +execute(Context& ctx, + const char* const* argv, + util::Fd&& fd_out, + util::Fd&& fd_err) { LOG("Executing {}", util::format_argv_for_logging(argv)); @@ -289,7 +292,10 @@ win32execute(const char* path, // Execute a compiler backend, capturing all output to the given paths the full // path to the compiler to run is in argv[0]. int -execute(Context& ctx, const char* const* argv, Fd&& fd_out, Fd&& fd_err) +execute(Context& ctx, + const char* const* argv, + util::Fd&& fd_out, + util::Fd&& fd_err) { LOG("Executing {}", util::format_argv_for_logging(argv)); diff --git a/src/execute.hpp b/src/execute.hpp index 5d0527dfb..df5668e31 100644 --- a/src/execute.hpp +++ b/src/execute.hpp @@ -18,7 +18,7 @@ #pragma once -#include "Fd.hpp" +#include #include #include @@ -26,7 +26,10 @@ class Context; -int execute(Context& ctx, const char* const* argv, Fd&& fd_out, Fd&& fd_err); +int execute(Context& ctx, + const char* const* argv, + util::Fd&& fd_out, + util::Fd&& fd_err); void execute_noreturn(const char* const* argv, const std::string& temp_dir); diff --git a/src/storage/local/LocalStorage.cpp b/src/storage/local/LocalStorage.cpp index f0cd9b36a..fea093cdb 100644 --- a/src/storage/local/LocalStorage.cpp +++ b/src/storage/local/LocalStorage.cpp @@ -244,12 +244,12 @@ static void clone_file(const std::string& src, const std::string& dest, bool via_tmp_file) { # if defined(__linux__) - Fd src_fd(open(src.c_str(), O_RDONLY)); + util::Fd src_fd(open(src.c_str(), O_RDONLY)); if (!src_fd) { throw core::Error(FMT("{}: {}", src, strerror(errno))); } - Fd dest_fd; + util::Fd dest_fd; std::string tmp_file; if (via_tmp_file) { auto temp_file = @@ -257,8 +257,8 @@ clone_file(const std::string& src, const std::string& dest, bool via_tmp_file) dest_fd = std::move(temp_file.fd); tmp_file = std::move(temp_file.path); } else { - dest_fd = - Fd(open(dest.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666)); + dest_fd = util::Fd( + open(dest.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666)); if (!dest_fd) { throw core::Error(FMT("{}: {}", src, strerror(errno))); } diff --git a/src/Fd.hpp b/src/util/Fd.hpp similarity index 84% rename from src/Fd.hpp rename to src/util/Fd.hpp index 3a47278c0..14d5aa27f 100644 --- a/src/Fd.hpp +++ b/src/util/Fd.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2020-2021 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,15 @@ #pragma once -#include "NonCopyable.hpp" -#include "assertions.hpp" +#include +#include +#include + +#ifdef HAVE_UNISTD_H +# include +#endif + +namespace util { class Fd : NonCopyable { @@ -85,6 +92,12 @@ Fd::operator=(Fd&& other_fd) noexcept return *this; } +inline bool +Fd::close() +{ + return m_fd != -1 && ::close(release()) == 0; +} + inline int Fd::release() { @@ -92,3 +105,5 @@ Fd::release() m_fd = -1; return fd; } + +} // namespace util diff --git a/src/util/TemporaryFile.hpp b/src/util/TemporaryFile.hpp index f7d36ba00..8e121c5cb 100644 --- a/src/util/TemporaryFile.hpp +++ b/src/util/TemporaryFile.hpp @@ -18,7 +18,7 @@ #pragma once -#include +#include #include diff --git a/src/util/file.cpp b/src/util/file.cpp index 220c7b819..8099a1022 100644 --- a/src/util/file.cpp +++ b/src/util/file.cpp @@ -18,13 +18,13 @@ #include "file.hpp" -#include #include #include #include #include #include #include +#include #include #include #include diff --git a/unittest/test_InodeCache.cpp b/unittest/test_InodeCache.cpp index c5bbf796e..12980ee77 100644 --- a/unittest/test_InodeCache.cpp +++ b/unittest/test_InodeCache.cpp @@ -22,7 +22,7 @@ #include "../src/InodeCache.hpp" #include "TestUtil.hpp" -#include +#include #include #include @@ -39,7 +39,7 @@ namespace { bool inode_cache_available() { - Fd fd(open(util::actual_cwd().c_str(), O_RDONLY)); + util::Fd fd(open(util::actual_cwd().c_str(), O_RDONLY)); return fd && InodeCache::available(*fd); } diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index f0c355bb1..da7713804 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -17,7 +17,6 @@ // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #include "../src/Config.hpp" -#include "../src/Fd.hpp" #include "../src/Util.hpp" #include "../src/fmtmacros.hpp" #include "TestUtil.hpp" diff --git a/unittest/test_bsdmkstemp.cpp b/unittest/test_bsdmkstemp.cpp index 6cda0f9ce..30aaa7fe4 100644 --- a/unittest/test_bsdmkstemp.cpp +++ b/unittest/test_bsdmkstemp.cpp @@ -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. // @@ -16,10 +16,10 @@ // this program; if not, write to the Free Software Foundation, Inc., 51 // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -#include "../src/Fd.hpp" #include "../src/Finalizer.hpp" #include "TestUtil.hpp" +#include #include #include "third_party/doctest.h" @@ -33,6 +33,7 @@ #include using TestUtil::TestContext; +using util::Fd; namespace { diff --git a/unittest/test_util_file.cpp b/unittest/test_util_file.cpp index ed78dff2e..4af0b0758 100644 --- a/unittest/test_util_file.cpp +++ b/unittest/test_util_file.cpp @@ -18,10 +18,10 @@ #include "TestUtil.hpp" -#include #include #include #include +#include #include #include #include @@ -46,15 +46,16 @@ TEST_CASE("util::fallocate") const char filename[] = "test-file"; - CHECK(util::fallocate(Fd(creat(filename, S_IRUSR | S_IWUSR)).get(), 10000)); + CHECK( + util::fallocate(util::Fd(creat(filename, S_IRUSR | S_IWUSR)).get(), 10000)); CHECK(DirEntry(filename).size() == 10000); - CHECK( - util::fallocate(Fd(open(filename, O_RDWR, S_IRUSR | S_IWUSR)).get(), 5000)); + CHECK(util::fallocate( + util::Fd(open(filename, O_RDWR, S_IRUSR | S_IWUSR)).get(), 5000)); CHECK(DirEntry(filename).size() == 10000); - CHECK(util::fallocate(Fd(open(filename, O_RDWR, S_IRUSR | S_IWUSR)).get(), - 20000)); + CHECK(util::fallocate( + util::Fd(open(filename, O_RDWR, S_IRUSR | S_IWUSR)).get(), 20000)); CHECK(DirEntry(filename).size() == 20000); }