From 36007299a678a11998192a719f446216e6a95461 Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Sun, 12 Jan 2025 01:55:15 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7=20gucc:=20add=20helper=20function?= =?UTF-8?q?=20to=20find=20block=20device=20by=20parent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gucc/include/gucc/block_devices.hpp | 6 ++++++ gucc/src/block_devices.cpp | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/gucc/include/gucc/block_devices.hpp b/gucc/include/gucc/block_devices.hpp index 613b972..a7a6a6d 100644 --- a/gucc/include/gucc/block_devices.hpp +++ b/gucc/include/gucc/block_devices.hpp @@ -46,6 +46,12 @@ auto list_block_devices() -> std::optional>; /// @return An optional BlockDevice object if found, std::nullopt otherwise. auto find_device_by_name(const std::vector& devices, std::string_view device_name) -> std::optional; +/// @brief Finds a block device by matching its parent kernel name (pkname). +/// @param devices A vector of BlockDevice objects. +/// @param pkname A string view representing the parent kernel name to search for. +/// @return An optional BlockDevice object if found, std::nullopt otherwise. +auto find_device_by_pkname(const std::vector& devices, std::string_view pkname) -> std::optional; + } // namespace gucc::disk #endif // BLOCK_DEVICES_HPP diff --git a/gucc/src/block_devices.cpp b/gucc/src/block_devices.cpp index 42ab25f..386df6d 100644 --- a/gucc/src/block_devices.cpp +++ b/gucc/src/block_devices.cpp @@ -88,6 +88,19 @@ auto find_device_by_name(const std::vector& devices, std::string_vi return std::nullopt; } +auto find_device_by_pkname(const std::vector& devices, std::string_view pkname) -> std::optional { + auto it = std::ranges::find_if(devices, [pkname](auto&& dev) { + if (dev.pkname) { + return *dev.pkname == pkname; + } + return false; + }); + if (it != std::ranges::end(devices)) { + return std::make_optional(std::move(*it)); + } + return std::nullopt; +} + auto list_block_devices() -> std::optional> { const auto& lsblk_output = utils::exec(R"(lsblk -f -o NAME,TYPE,FSTYPE,UUID,PARTUUID,PKNAME,LABEL,SIZE,MOUNTPOINT,MODEL -b -p -a -J -Q "type=='part' || type=='crypt' && fstype")"); if (lsblk_output.empty()) {