Skip to content

Commit

Permalink
refactor: move implementation details to internal namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
mhx committed Aug 9, 2024
1 parent c953131 commit 4e0c48e
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -937,6 +937,7 @@ if(WITH_TESTS)
tool_main_test
tools_test
utils_test
file_utils_test
worker_group_test
)

Expand Down
4 changes: 0 additions & 4 deletions include/dwarfs/file_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#pragma once

#include <cstdint>
#include <filesystem>

namespace dwarfs {

Expand All @@ -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
32 changes: 32 additions & 0 deletions include/dwarfs/internal/file_status_conv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* vim:set ts=2 sw=2 sts=2 et: */
/**
* \author Marcus Holland-Moritz ([email protected])
* \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 <https://www.gnu.org/licenses/>.
*/

#pragma once

#include <cstdint>
#include <filesystem>

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
6 changes: 4 additions & 2 deletions src/dwarfs/file_stat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
#include <dwarfs/mmap.h>
#include <dwarfs/util.h>

#include <dwarfs/internal/file_status_conv.h>

namespace dwarfs {

namespace {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

#include <dwarfs/file_type.h>

namespace dwarfs {
#include <dwarfs/internal/file_status_conv.h>

namespace dwarfs::internal {

namespace fs = std::filesystem;

Expand Down Expand Up @@ -98,4 +100,4 @@ uint16_t file_status_to_mode(std::filesystem::file_status status) {
static_cast<uint16_t>(status.permissions());
}

} // namespace dwarfs
} // namespace dwarfs::internal
81 changes: 81 additions & 0 deletions test/file_utils_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* vim:set ts=2 sw=2 sts=2 et: */
/**
* \author Marcus Holland-Moritz ([email protected])
* \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 <https://www.gnu.org/licenses/>.
*/

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <dwarfs/internal/file_status_conv.h>

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<std::runtime_error>("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<std::runtime_error>(
testing::HasSubstr("invalid file type: ")));
}

0 comments on commit 4e0c48e

Please sign in to comment.