From 1b39994bad7144a5d94dad245b308cb482524989 Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Sun, 24 Dec 2023 12:45:43 +0100 Subject: [PATCH] refactor(chmod): factor out chmod transformer --- CMakeLists.txt | 1 + include/dwarfs/chmod_entry_transformer.h | 35 ++++++++++++++++ include/dwarfs/chmod_transformer.h | 24 +++++++++-- src/dwarfs/chmod_entry_transformer.cpp | 53 ++++++++++++++++++++++++ src/dwarfs/chmod_transformer.cpp | 25 +++++------ src/mkdwarfs_main.cpp | 4 +- 6 files changed, 122 insertions(+), 20 deletions(-) create mode 100644 include/dwarfs/chmod_entry_transformer.h create mode 100644 src/dwarfs/chmod_entry_transformer.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c314679b..0162cd46d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -373,6 +373,7 @@ list( src/dwarfs/category_parser.cpp src/dwarfs/checksum.cpp src/dwarfs/chmod_transformer.cpp + src/dwarfs/chmod_entry_transformer.cpp src/dwarfs/console_writer.cpp src/dwarfs/entry.cpp src/dwarfs/error.cpp diff --git a/include/dwarfs/chmod_entry_transformer.h b/include/dwarfs/chmod_entry_transformer.h new file mode 100644 index 000000000..ff7c6cb79 --- /dev/null +++ b/include/dwarfs/chmod_entry_transformer.h @@ -0,0 +1,35 @@ +/* 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 +#include + +#include "dwarfs/entry_transformer.h" + +namespace dwarfs { + +std::unique_ptr +create_chmod_entry_transformer(std::string_view spec, uint16_t umask); + +} // namespace dwarfs diff --git a/include/dwarfs/chmod_transformer.h b/include/dwarfs/chmod_transformer.h index d02dd16b9..cc18f69fd 100644 --- a/include/dwarfs/chmod_transformer.h +++ b/include/dwarfs/chmod_transformer.h @@ -23,13 +23,29 @@ #include #include +#include #include -#include "dwarfs/entry_transformer.h" - namespace dwarfs { -std::unique_ptr -create_chmod_transformer(std::string_view spec, uint16_t umask); +class chmod_transformer { + public: + chmod_transformer(std::string_view spec, uint16_t umask); + + std::optional transform(uint16_t mode, bool isdir) const { + return impl_->transform(mode, isdir); + } + + class impl { + public: + virtual ~impl() = default; + + virtual std::optional + transform(uint16_t mode, bool isdir) const = 0; + }; + + private: + std::unique_ptr impl_; +}; } // namespace dwarfs diff --git a/src/dwarfs/chmod_entry_transformer.cpp b/src/dwarfs/chmod_entry_transformer.cpp new file mode 100644 index 000000000..da161324d --- /dev/null +++ b/src/dwarfs/chmod_entry_transformer.cpp @@ -0,0 +1,53 @@ +/* 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 "dwarfs/chmod_entry_transformer.h" +#include "dwarfs/chmod_transformer.h" +#include "dwarfs/entry_interface.h" + +namespace dwarfs { + +namespace { + +class chmod_entry_transformer : public entry_transformer { + public: + chmod_entry_transformer(std::string_view spec, uint16_t umask) + : transformer_{spec, umask} {} + + void transform(entry_interface& ei) override { + if (auto perm = + transformer_.transform(ei.get_permissions(), ei.is_directory())) { + ei.set_permissions(perm.value()); + } + } + + private: + chmod_transformer transformer_; +}; + +} // namespace + +std::unique_ptr +create_chmod_entry_transformer(std::string_view spec, uint16_t umask) { + return std::make_unique(spec, umask); +} + +} // namespace dwarfs diff --git a/src/dwarfs/chmod_transformer.cpp b/src/dwarfs/chmod_transformer.cpp index ea908064c..f7584ed20 100644 --- a/src/dwarfs/chmod_transformer.cpp +++ b/src/dwarfs/chmod_transformer.cpp @@ -30,7 +30,6 @@ #include #include "dwarfs/chmod_transformer.h" -#include "dwarfs/entry_interface.h" namespace dwarfs { @@ -151,11 +150,11 @@ class dynamic_permission_modifier : public permission_modifier { uint16_t const umask_; }; -class chmod_transformer : public entry_transformer { +class chmod_transformer_ : public chmod_transformer::impl { public: - chmod_transformer(std::string_view spec, uint16_t umask); + chmod_transformer_(std::string_view spec, uint16_t umask); - void transform(entry_interface& ei) override; + std::optional transform(uint16_t mode, bool isdir) const override; private: std::unique_ptr modifier_; @@ -163,7 +162,7 @@ class chmod_transformer : public entry_transformer { bool flag_F_{false}; }; -chmod_transformer::chmod_transformer(std::string_view spec, uint16_t umask) { +chmod_transformer_::chmod_transformer_(std::string_view spec, uint16_t umask) { enum class state { PARSE_WHERE, PARSE_PERMS, PARSE_OCTAL }; state st{state::PARSE_WHERE}; oper op{oper::NONE}; @@ -344,21 +343,19 @@ chmod_transformer::chmod_transformer(std::string_view spec, uint16_t umask) { } } -void chmod_transformer::transform(entry_interface& ei) { +std::optional +chmod_transformer_::transform(uint16_t mode, bool isdir) const { // skip entries for which this isn't intended - if ((flag_D_ and !ei.is_directory()) or (flag_F_ and ei.is_directory())) { - return; + if ((flag_D_ and !isdir) or (flag_F_ and isdir)) { + return std::nullopt; } - ei.set_permissions( - modifier_->modify(ei.get_permissions(), ei.is_directory())); + return modifier_->modify(mode, isdir); } } // namespace -std::unique_ptr -create_chmod_transformer(std::string_view spec, uint16_t umask) { - return std::make_unique(spec, umask); -} +chmod_transformer::chmod_transformer(std::string_view spec, uint16_t umask) + : impl_{std::make_unique(spec, umask)} {} } // namespace dwarfs diff --git a/src/mkdwarfs_main.cpp b/src/mkdwarfs_main.cpp index db8cf3abc..eb4ddced2 100644 --- a/src/mkdwarfs_main.cpp +++ b/src/mkdwarfs_main.cpp @@ -55,7 +55,7 @@ #include "dwarfs/builtin_script.h" #include "dwarfs/categorizer.h" #include "dwarfs/category_parser.h" -#include "dwarfs/chmod_transformer.h" +#include "dwarfs/chmod_entry_transformer.h" #include "dwarfs/console_writer.h" #include "dwarfs/entry.h" #include "dwarfs/error.h" @@ -894,7 +894,7 @@ int mkdwarfs_main(int argc, sys_char** argv) { ::umask(mask); /* Flawfinder: ignore */ for (auto expr : chmod_exprs) { - bs->add_transformer(create_chmod_transformer(expr, mask)); + bs->add_transformer(create_chmod_entry_transformer(expr, mask)); } }