From 36b205a46342b6a0782fbdd56eca677b459b0a7e Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Sat, 6 Jul 2024 02:01:17 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20gucc:=20refactor=20fstab?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gucc/src/fstab.cpp | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/gucc/src/fstab.cpp b/gucc/src/fstab.cpp index d7090ee..b4a60d4 100644 --- a/gucc/src/fstab.cpp +++ b/gucc/src/fstab.cpp @@ -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) { + 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 { + 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(std::tolower(static_cast(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 { // 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; @@ -78,21 +96,8 @@ auto gen_fstab_entry(const Partition& partition) noexcept -> std::optional 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) {