From 4e0c48edfdea08e217450d66ae40f73c5798179a Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Fri, 2 Aug 2024 23:54:14 +0200 Subject: [PATCH] refactor: move implementation details to internal namespace --- CMakeLists.txt | 3 +- include/dwarfs/file_type.h | 4 - include/dwarfs/internal/file_status_conv.h | 32 ++++++++ src/dwarfs/file_stat.cpp | 6 +- .../file_status_conv.cpp} | 6 +- test/file_utils_test.cpp | 81 +++++++++++++++++++ 6 files changed, 123 insertions(+), 9 deletions(-) create mode 100644 include/dwarfs/internal/file_status_conv.h rename src/dwarfs/{file_type.cpp => internal/file_status_conv.cpp} (96%) create mode 100644 test/file_utils_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9345f213..fe6a3af3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -611,11 +611,11 @@ list(APPEND LIBDWARFS_COMMON_SRC src/dwarfs/error.cpp src/dwarfs/file_access_generic.cpp src/dwarfs/file_stat.cpp - src/dwarfs/file_type.cpp src/dwarfs/filesystem_writer.cpp src/dwarfs/fstypes.cpp src/dwarfs/history.cpp src/dwarfs/internal/features.cpp + src/dwarfs/internal/file_status_conv.cpp src/dwarfs/internal/string_table.cpp src/dwarfs/internal/wcwidth.c src/dwarfs/internal/worker_group.cpp @@ -937,6 +937,7 @@ if(WITH_TESTS) tool_main_test tools_test utils_test + file_utils_test worker_group_test ) diff --git a/include/dwarfs/file_type.h b/include/dwarfs/file_type.h index 752d5619..3864f316 100644 --- a/include/dwarfs/file_type.h +++ b/include/dwarfs/file_type.h @@ -22,7 +22,6 @@ #pragma once #include -#include namespace dwarfs { @@ -44,7 +43,4 @@ struct posix_file_type { } }; -std::filesystem::file_status file_mode_to_status(uint16_t mode); -uint16_t file_status_to_mode(std::filesystem::file_status status); - } // namespace dwarfs diff --git a/include/dwarfs/internal/file_status_conv.h b/include/dwarfs/internal/file_status_conv.h new file mode 100644 index 00000000..3fd39841 --- /dev/null +++ b/include/dwarfs/internal/file_status_conv.h @@ -0,0 +1,32 @@ +/* vim:set ts=2 sw=2 sts=2 et: */ +/** + * \author Marcus Holland-Moritz (github@mhxnet.de) + * \copyright Copyright (c) Marcus Holland-Moritz + * + * This file is part of dwarfs. + * + * dwarfs 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. + * + * dwarfs 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 dwarfs. If not, see . + */ + +#pragma once + +#include +#include + +namespace dwarfs::internal { + +std::filesystem::file_status file_mode_to_status(uint16_t mode); +uint16_t file_status_to_mode(std::filesystem::file_status status); + +} // namespace dwarfs::internal diff --git a/src/dwarfs/file_stat.cpp b/src/dwarfs/file_stat.cpp index 75ab8577..e3628958 100644 --- a/src/dwarfs/file_stat.cpp +++ b/src/dwarfs/file_stat.cpp @@ -44,6 +44,8 @@ #include #include +#include + namespace dwarfs { namespace { @@ -126,7 +128,7 @@ file_stat::file_stat(fs::path const& path) { } valid_fields_ = file_stat::mode_valid; - mode_ = file_status_to_mode(status); + mode_ = internal::file_status_to_mode(status); blksize_ = 0; blocks_ = 0; @@ -273,7 +275,7 @@ void file_stat::ensure_valid(valid_fields_type fields) const { std::filesystem::file_status file_stat::status() const { ensure_valid(mode_valid); - return file_mode_to_status(mode_); + return internal::file_mode_to_status(mode_); }; posix_file_type::value file_stat::type() const { diff --git a/src/dwarfs/file_type.cpp b/src/dwarfs/internal/file_status_conv.cpp similarity index 96% rename from src/dwarfs/file_type.cpp rename to src/dwarfs/internal/file_status_conv.cpp index a73177d5..7ac1c975 100644 --- a/src/dwarfs/file_type.cpp +++ b/src/dwarfs/internal/file_status_conv.cpp @@ -23,7 +23,9 @@ #include -namespace dwarfs { +#include + +namespace dwarfs::internal { namespace fs = std::filesystem; @@ -98,4 +100,4 @@ uint16_t file_status_to_mode(std::filesystem::file_status status) { static_cast(status.permissions()); } -} // namespace dwarfs +} // namespace dwarfs::internal diff --git a/test/file_utils_test.cpp b/test/file_utils_test.cpp new file mode 100644 index 00000000..d058d76c --- /dev/null +++ b/test/file_utils_test.cpp @@ -0,0 +1,81 @@ +/* vim:set ts=2 sw=2 sts=2 et: */ +/** + * \author Marcus Holland-Moritz (github@mhxnet.de) + * \copyright Copyright (c) Marcus Holland-Moritz + * + * This file is part of dwarfs. + * + * dwarfs 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. + * + * dwarfs 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 dwarfs. If not, see . + */ + +#include +#include + +#include + +using namespace dwarfs::internal; +namespace fs = std::filesystem; + +TEST(file_utils, file_status_conversion) { + using fs::file_type; + using fs::perms; + + EXPECT_THAT( + [] { return file_mode_to_status(0); }, + testing::ThrowsMessage("invalid file mode: 0x0000")); + + auto status = file_mode_to_status(0140755); + EXPECT_EQ(status.type(), file_type::socket); + EXPECT_EQ(status.permissions(), perms::owner_all | perms::group_read | + perms::group_exec | perms::others_read | + perms::others_exec); + EXPECT_EQ(file_status_to_mode(status), 0140755); + + status = file_mode_to_status(0120644); + EXPECT_EQ(status.type(), file_type::symlink); + EXPECT_EQ(status.permissions(), perms::owner_read | perms::owner_write | + perms::group_read | perms::others_read); + EXPECT_EQ(file_status_to_mode(status), 0120644); + + status = file_mode_to_status(0104400); + EXPECT_EQ(status.type(), file_type::regular); + EXPECT_EQ(status.permissions(), perms::set_uid | perms::owner_read); + EXPECT_EQ(file_status_to_mode(status), 0104400); + + status = file_mode_to_status(0060004); + EXPECT_EQ(status.type(), file_type::block); + EXPECT_EQ(status.permissions(), perms::others_read); + EXPECT_EQ(file_status_to_mode(status), 0060004); + + status = file_mode_to_status(0042010); + EXPECT_EQ(status.type(), file_type::directory); + EXPECT_EQ(status.permissions(), perms::set_gid | perms::group_exec); + EXPECT_EQ(file_status_to_mode(status), 0042010); + + status = file_mode_to_status(0021007); + EXPECT_EQ(status.type(), file_type::character); + EXPECT_EQ(status.permissions(), perms::sticky_bit | perms::others_all); + EXPECT_EQ(file_status_to_mode(status), 0021007); + + status = file_mode_to_status(0017777); + EXPECT_EQ(status.type(), file_type::fifo); + EXPECT_EQ(status.permissions(), + perms::sticky_bit | perms::set_uid | perms::set_gid | perms::all); + EXPECT_EQ(file_status_to_mode(status), 0017777); + + status.type(file_type::none); + EXPECT_THAT([&] { file_status_to_mode(status); }, + testing::ThrowsMessage( + testing::HasSubstr("invalid file type: "))); +}