Skip to content

Commit

Permalink
🧹 gucc: refactor fstab
Browse files Browse the repository at this point in the history
  • Loading branch information
vnepogodin committed Jul 5, 2024
1 parent 5a7a93d commit 36b205a
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions gucc/src/fstab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,41 @@ constexpr auto convert_fsname_fstab(std::string_view fsname) noexcept -> std::st
return fsname;
}

auto string_tolower(std::string_view text) noexcept -> std::string {
constexpr auto get_check_number(std::string_view mountpoint, std::string_view fstype) noexcept -> std::int32_t {
if (mountpoint == "/"sv && fstype != "btrfs"sv) {
return 1;
} else if (mountpoint != "swap"sv && fstype != "btrfs"sv) {

Check failure on line 56 in gucc/src/fstab.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/gucc/src/fstab.cpp:56:7 [readability-else-after-return

do not use 'else' after 'return'
return 2;
}
return 0;
}

constexpr auto get_mount_options(std::string_view mount_opts, std::string_view btrfs_subvolume, std::string_view fstype) noexcept -> std::string {

Check failure on line 62 in gucc/src/fstab.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/gucc/src/fstab.cpp:62:34 [bugprone-easily-swappable-parameters

2 adjacent parameters of 'get_mount_options' of similar type ('std::string_view') are easily swapped by mistake
if (fstype == "btrfs"sv && !btrfs_subvolume.empty()) {
return fmt::format(FMT_COMPILE("subvol={},{}"), btrfs_subvolume, mount_opts);
}
return std::string{mount_opts};
}

constexpr auto string_tolower(std::string_view text) noexcept -> std::string {
std::string res{text};
ranges::transform(res, res.begin(),
[](char char_elem) { return static_cast<char>(std::tolower(static_cast<unsigned char>(char_elem))); });
return res;
}

constexpr auto get_converted_fs(std::string_view fstype) noexcept -> std::string {
auto lower_fstype = string_tolower(fstype);
return std::string{convert_fsname_fstab(lower_fstype)};
}

} // namespace

namespace gucc::fs {

auto gen_fstab_entry(const Partition& partition) noexcept -> std::optional<std::string> {
// Apperently some FS names named differently in /etc/fstab.
const auto& fstype = [](auto&& fstype) -> std::string {
auto lower_fstype = string_tolower(fstype);
return std::string{convert_fsname_fstab(lower_fstype)};
}(partition.fstype);
const auto& fstype = get_converted_fs(partition.fstype);

const auto& luks_mapper_name = partition.luks_mapper_name;
const auto& mountpoint = partition.mountpoint;
Expand All @@ -78,21 +96,8 @@ auto gen_fstab_entry(const Partition& partition) noexcept -> std::optional<std::
return std::nullopt;
}

const auto check_num = [](auto&& mountpoint, auto&& fstype) -> std::int32_t {
if (mountpoint == "/"sv && fstype != "btrfs"sv) {
return 1;
} else if (mountpoint != "swap"sv && fstype != "btrfs"sv) {
return 2;
}
return 0;
}(mountpoint, fstype);

const auto& mount_options = [](auto&& mount_opts, auto&& subvolume, auto&& fstype) -> std::string {
if (fstype == "btrfs"sv && !subvolume.empty()) {
return fmt::format(FMT_COMPILE("subvol={},{}"), subvolume, mount_opts);
}
return {mount_opts};
}(partition.mount_opts, partition.subvolume.value_or(""), fstype);
const auto check_num = get_check_number(mountpoint, fstype);
const auto& mount_options = get_mount_options(partition.mount_opts, partition.subvolume.value_or(""), fstype);

std::string device_str{partition.device};
if (luks_mapper_name) {
Expand Down

0 comments on commit 36b205a

Please sign in to comment.