From 5670da5c8a3b1d58592bb25518f8268e43eaaded Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Sun, 14 Jul 2024 02:54:41 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20gucc:=20refactor=20to=20use=20ut?= =?UTF-8?q?ils::exec=5Fchecked?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gucc/src/luks.cpp | 13 +++++-------- gucc/src/umount_partitions.cpp | 4 ++-- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/gucc/src/luks.cpp b/gucc/src/luks.cpp index 1394873..ae7bbb9 100644 --- a/gucc/src/luks.cpp +++ b/gucc/src/luks.cpp @@ -11,12 +11,12 @@ namespace gucc::crypto { auto luks1_open(std::string_view luks_pass, std::string_view partition, std::string_view luks_name) noexcept -> bool { - auto cmd = fmt::format(FMT_COMPILE("echo \"{}\" | cryptsetup open --type luks1 {} {} &>/dev/null"), luks_pass, partition, luks_name); + auto cmd = fmt::format(FMT_COMPILE("echo '{}' | cryptsetup open --type luks1 {} {} &>/dev/null"), luks_pass, partition, luks_name); return utils::exec_checked(cmd); } auto luks1_format(std::string_view luks_pass, std::string_view partition, std::string_view additional_flags) noexcept -> bool { - auto cmd = fmt::format(FMT_COMPILE("echo \"{}\" | cryptsetup -q {} --type luks1 luksFormat {} &>/dev/null"), luks_pass, additional_flags, partition); + auto cmd = fmt::format(FMT_COMPILE("echo '{}' | cryptsetup -q {} --type luks1 luksFormat {} &>/dev/null"), luks_pass, additional_flags, partition); return utils::exec_checked(cmd); } @@ -33,9 +33,7 @@ auto luks1_setup_keyfile(std::string_view dest_file, std::string_view mountpoint if (!fs::exists(dest_file)) { // TODO(vnepogodin): refactor subprocess exit code and print stderr captured output on failure auto cmd = fmt::format(FMT_COMPILE("dd bs=512 count=4 if=/dev/urandom of={} iflag=fullblock &>/dev/null"), additional_flags, partition, dest_file); - - const auto& ret_status = utils::exec(cmd, true); - if (ret_status != "0") { + if (!utils::exec_checked(cmd)) { spdlog::error("Failed to generate(dd) luks keyfile: {}!"); return false; } @@ -56,9 +54,8 @@ auto luks1_setup_keyfile(std::string_view dest_file, std::string_view mountpoint const auto& mkinitcpio_conf = fmt::format(FMT_COMPILE("{}/etc/mkinitcpio.conf"), mountpoint); // TODO(vnepogodin): that should gathered from dest_file and be relative to mountpoint - const auto& cmd = fmt::format(FMT_COMPILE("grep -q '/crypto_keyfile.bin' {0} || sed -i '/FILES/ s~)~/crypto_keyfile.bin)~' {0}"), mkinitcpio_conf); - const auto& ret_status = utils::exec(cmd, true); - if (ret_status != "0") { + const auto& cmd = fmt::format(FMT_COMPILE("grep -q '/crypto_keyfile.bin' {0} || sed -i '/FILES/ s~)~/crypto_keyfile.bin)~' {0}"), mkinitcpio_conf); + if (!utils::exec_checked(cmd)) { spdlog::error("Failed to add keyfile to {}", mkinitcpio_conf); return false; } diff --git a/gucc/src/umount_partitions.cpp b/gucc/src/umount_partitions.cpp index f327c4d..4a06473 100644 --- a/gucc/src/umount_partitions.cpp +++ b/gucc/src/umount_partitions.cpp @@ -26,7 +26,7 @@ auto umount_partitions(std::string_view root_mountpoint, const std::vectorsize(), root_mountpoint); for (auto&& mtab_entry : std::move(*mtab_entries)) { const auto& umount_cmd = fmt::format(FMT_COMPILE("umount -v {} &>>/tmp/cachyos-install.log"), mtab_entry.mountpoint); - if (utils::exec(umount_cmd, true) != "0") { + if (!utils::exec_checked(umount_cmd)) { spdlog::error("Failed to umount partition: {} {}", mtab_entry.device, mtab_entry.mountpoint); return false; } @@ -34,7 +34,7 @@ auto umount_partitions(std::string_view root_mountpoint, const std::vector>/tmp/cachyos-install.log"), zfs_poolname); - if (utils::exec(zpool_export_cmd, true) != "0") { + if (!utils::exec_checked(zpool_export_cmd)) { spdlog::error("Failed to export zpool: {}", zfs_poolname); return false; }