From: Joel Rosdahl Date: Thu, 31 Dec 2020 18:57:26 +0000 (+0100) Subject: Adapt to the ccache code style X-Git-Tag: v4.2~47 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d62ce304f1c323cb3ca4e716069c033931cd50a7;p=thirdparty%2Fccache.git Adapt to the ccache code style --- diff --git a/src/TemporaryFile.cpp b/src/TemporaryFile.cpp index ca0c8374f..feaa5f182 100644 --- a/src/TemporaryFile.cpp +++ b/src/TemporaryFile.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2020 Joel Rosdahl and other contributors +// Copyright (C) 2020-2021 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -50,11 +50,12 @@ TemporaryFile::TemporaryFile(string_view path_prefix) { Util::ensure_dir_exists(Util::dir_name(path)); #ifdef _WIN32 - // MSVC lacks mkstemp() and [mingw-w64's implementation][1] is problematic, as + // MSVC lacks mkstemp() and Mingw-w64's implementation[1] is problematic, as // it can reuse the names of recently-deleted files unless the caller // remembers to call srand(). - // [1]: - // https://github.com/Alexpux/mingw-w64/blob/d0d7f784833bbb0b2d279310ddc6afb52fe47a46/mingw-w64-crt/misc/mkstemp.c + + // [1]: fd = Fd(bsd_mkstemp(&path[0])); #else fd = Fd(mkstemp(&path[0])); diff --git a/src/third_party/CMakeLists.txt b/src/third_party/CMakeLists.txt index 977cff60c..c361974a4 100644 --- a/src/third_party/CMakeLists.txt +++ b/src/third_party/CMakeLists.txt @@ -6,8 +6,8 @@ else() target_compile_definitions(third_party_lib PUBLIC -DSTATIC_GETOPT) endif() -if (WIN32) - target_sources(third_party_lib PRIVATE win32/mktemp.c) +if(WIN32) + target_sources(third_party_lib PRIVATE win32/mktemp.c) endif () if(ENABLE_TRACING) diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt index b29fd653e..65401041a 100644 --- a/unittest/CMakeLists.txt +++ b/unittest/CMakeLists.txt @@ -27,9 +27,7 @@ if(INODE_CACHE_SUPPORTED) endif() if(WIN32) - list(APPEND source_files - test_bsdmkstemp.cpp - test_Win32Util.cpp) + list(APPEND source_files test_bsdmkstemp.cpp test_Win32Util.cpp) endif() add_executable(unittest ${source_files}) diff --git a/unittest/test_bsdmkstemp.cpp b/unittest/test_bsdmkstemp.cpp index 0237f5c80..021c73d58 100644 --- a/unittest/test_bsdmkstemp.cpp +++ b/unittest/test_bsdmkstemp.cpp @@ -1,4 +1,23 @@ +// Copyright (C) 2020-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 "../src/Fd.hpp" +#include "../src/Finalizer.hpp" #include "TestUtil.hpp" #include "third_party/doctest.h" @@ -19,7 +38,7 @@ class ScopedHANDLE public: ScopedHANDLE() = default; - explicit ScopedHANDLE(HANDLE h) : h_(h) + explicit ScopedHANDLE(HANDLE handle) : m_handle(handle) { } @@ -29,74 +48,68 @@ public: ~ScopedHANDLE() { - if (h_ != INVALID_HANDLE_VALUE) { - CloseHandle(h_); + if (m_handle != INVALID_HANDLE_VALUE) { + CloseHandle(m_handle); } } ScopedHANDLE& operator=(ScopedHANDLE rhs) { - std::swap(h_, rhs.h_); + std::swap(m_handle, rhs.m_handle); return *this; } explicit operator bool() const { - return h_ != INVALID_HANDLE_VALUE; + return m_handle != INVALID_HANDLE_VALUE; } HANDLE get() const { - return h_; + return m_handle; } HANDLE release() { - HANDLE h = h_; - h_ = INVALID_HANDLE_VALUE; - return h; + HANDLE handle = m_handle; + m_handle = INVALID_HANDLE_VALUE; + return handle; } private: - HANDLE h_ = INVALID_HANDLE_VALUE; + HANDLE m_handle = INVALID_HANDLE_VALUE; }; } // namespace -TEST_SUITE_BEGIN("thirdparty"); +TEST_SUITE_BEGIN("bsd_mkstemp"); -TEST_CASE("thirdparty::bsd_mkstemp") +TEST_CASE("bsd_mkstemp") { TestContext test_context; - static int rand_iter; + static uint16_t rand_iter; rand_iter = 0; bsd_mkstemp_set_random_source([](void* buf, size_t nbytes) { std::fill_n( static_cast(buf), nbytes / sizeof(uint16_t), rand_iter); - rand_iter++; + ++rand_iter; }); - struct Cleanup - { - ~Cleanup() - { - bsd_mkstemp_set_random_source(nullptr); - } - } cleanup; + Finalizer reset_random_source([] { bsd_mkstemp_set_random_source(nullptr); }); SUBCASE("successful") { - std::string path("XXXXXX"); + std::string path = "XXXXXX"; CHECK_MESSAGE(Fd(bsd_mkstemp(&path[0])), "errno=" << errno); CHECK(path == "AAAAAA"); } - SUBCASE("existing_file") + SUBCASE("existing file") { CHECK_MESSAGE(ScopedHANDLE(CreateFileA("AAAAAA", GENERIC_READ | GENERIC_WRITE, @@ -107,12 +120,12 @@ TEST_CASE("thirdparty::bsd_mkstemp") nullptr)), "errno=" << errno); - std::string path("XXXXXX"); + std::string path = "XXXXXX"; CHECK_MESSAGE(Fd(bsd_mkstemp(&path[0])), "errno=" << errno); CHECK(path == "BBBBBB"); } - SUBCASE("existing_file_pending_delete") + SUBCASE("existing file, pending delete") { ScopedHANDLE h; CHECK_MESSAGE( @@ -133,34 +146,34 @@ TEST_CASE("thirdparty::bsd_mkstemp") h.get(), FileDispositionInfo, &info, sizeof(info)), "errno=" << errno); - std::string path("XXXXXX"); + std::string path = "XXXXXX"; CHECK_MESSAGE(Fd(bsd_mkstemp(&path[0])), "errno=" << errno); CHECK(path == "BBBBBB"); } - SUBCASE("existing_dir") + SUBCASE("existing directory") { CHECK_MESSAGE(CreateDirectoryA("AAAAAA", nullptr), "errno=" << errno); - std::string path("XXXXXX"); + std::string path = "XXXXXX"; CHECK_MESSAGE(Fd(bsd_mkstemp(&path[0])), "errno=" << errno); CHECK(path == "BBBBBB"); } SUBCASE("permission denied") { - auto makeACL = [](const char* aclString) { + auto make_ACL = [](const char* acl_string) { PSECURITY_DESCRIPTOR desc = nullptr; ConvertStringSecurityDescriptorToSecurityDescriptorA( - aclString, SDDL_REVISION_1, &desc, nullptr); + acl_string, SDDL_REVISION_1, &desc, nullptr); return std::shared_ptr( static_cast(desc), &LocalFree); }; - // Create a directory with a contrived ACL that denies creation of new - // files and directories to the "Everybody" (WD) group. + // Create a directory with a contrived ACL that denies creation of new files + // and directories to the "Everybody" (WD) group. std::shared_ptr desc; - CHECK_MESSAGE((desc = makeACL("D:(D;;DCLCRPCR;;;WD)(A;;FA;;;WD)")), + CHECK_MESSAGE((desc = make_ACL("D:(D;;DCLCRPCR;;;WD)(A;;FA;;;WD)")), "errno=" << errno); SECURITY_ATTRIBUTES attrs{}; @@ -169,8 +182,8 @@ TEST_CASE("thirdparty::bsd_mkstemp") CHECK_MESSAGE(CreateDirectoryA("my_readonly_dir", &attrs), "errno=" << errno); - // Sanity check that we cannot write to this directory. (E.g. Wine - // doesn't appear to emulate Windows ACLs properly when run under root.) + // Sanity check that we cannot write to this directory. (E.g. Wine doesn't + // appear to emulate Windows ACLs properly when run under root.) bool broken_acls = static_cast(ScopedHANDLE( CreateFileA("my_readonly_dir/.writable", GENERIC_WRITE, @@ -181,7 +194,7 @@ TEST_CASE("thirdparty::bsd_mkstemp") nullptr))); if (!broken_acls) { - std::string path("my_readonly_dir/XXXXXX"); + std::string path = "my_readonly_dir/XXXXXX"; CHECK(!Fd(bsd_mkstemp(&path[0]))); CHECK(errno == EACCES); } else {