Skip to content

Commit

Permalink
refactor(chmod): factor out chmod transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
mhx committed Dec 24, 2023
1 parent ee178ac commit 1b39994
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 20 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions include/dwarfs/chmod_entry_transformer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* 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 <memory>
#include <string_view>

#include "dwarfs/entry_transformer.h"

namespace dwarfs {

std::unique_ptr<entry_transformer>
create_chmod_entry_transformer(std::string_view spec, uint16_t umask);

} // namespace dwarfs
24 changes: 20 additions & 4 deletions include/dwarfs/chmod_transformer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,29 @@

#include <cstdint>
#include <memory>
#include <optional>
#include <string_view>

#include "dwarfs/entry_transformer.h"

namespace dwarfs {

std::unique_ptr<entry_transformer>
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<uint16_t> transform(uint16_t mode, bool isdir) const {
return impl_->transform(mode, isdir);
}

class impl {
public:
virtual ~impl() = default;

virtual std::optional<uint16_t>
transform(uint16_t mode, bool isdir) const = 0;
};

private:
std::unique_ptr<impl> impl_;
};

} // namespace dwarfs
53 changes: 53 additions & 0 deletions src/dwarfs/chmod_entry_transformer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* 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 "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<entry_transformer>
create_chmod_entry_transformer(std::string_view spec, uint16_t umask) {
return std::make_unique<chmod_entry_transformer>(spec, umask);
}

} // namespace dwarfs
25 changes: 11 additions & 14 deletions src/dwarfs/chmod_transformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include <fmt/format.h>

#include "dwarfs/chmod_transformer.h"
#include "dwarfs/entry_interface.h"

namespace dwarfs {

Expand Down Expand Up @@ -151,19 +150,19 @@ 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<uint16_t> transform(uint16_t mode, bool isdir) const override;

private:
std::unique_ptr<permission_modifier const> modifier_;
bool flag_D_{false};
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};
Expand Down Expand Up @@ -344,21 +343,19 @@ chmod_transformer::chmod_transformer(std::string_view spec, uint16_t umask) {
}
}

void chmod_transformer::transform(entry_interface& ei) {
std::optional<uint16_t>
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<entry_transformer>
create_chmod_transformer(std::string_view spec, uint16_t umask) {
return std::make_unique<chmod_transformer>(spec, umask);
}
chmod_transformer::chmod_transformer(std::string_view spec, uint16_t umask)
: impl_{std::make_unique<chmod_transformer_>(spec, umask)} {}

} // namespace dwarfs
4 changes: 2 additions & 2 deletions src/mkdwarfs_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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));
}
}

Expand Down

0 comments on commit 1b39994

Please sign in to comment.