👷 gucc: add helper function to find block device by parent
Some checks are pending
Build / Build with CMake (push) Waiting to run
Build / Build with CMake (DEVENV OFF) (push) Waiting to run
Build / Build with Meson (push) Waiting to run
Build / Build with Meson (DEVENV OFF) (push) Waiting to run
Checks / cpp-linter (push) Waiting to run
Checks / Check C++ style (push) Waiting to run

This commit is contained in:
Vladislav Nepogodin 2025-01-12 01:55:15 +04:00
parent a3ece6c01d
commit 36007299a6
No known key found for this signature in database
GPG Key ID: B62C3D10C54D5DA9
2 changed files with 19 additions and 0 deletions

View File

@ -46,6 +46,12 @@ auto list_block_devices() -> std::optional<std::vector<BlockDevice>>;
/// @return An optional BlockDevice object if found, std::nullopt otherwise.
auto find_device_by_name(const std::vector<BlockDevice>& devices, std::string_view device_name) -> std::optional<BlockDevice>;
/// @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<BlockDevice>& devices, std::string_view pkname) -> std::optional<BlockDevice>;
} // namespace gucc::disk
#endif // BLOCK_DEVICES_HPP

View File

@ -88,6 +88,19 @@ auto find_device_by_name(const std::vector<BlockDevice>& devices, std::string_vi
return std::nullopt;
}
auto find_device_by_pkname(const std::vector<BlockDevice>& devices, std::string_view pkname) -> std::optional<BlockDevice> {
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<BlockDevice>(std::move(*it));
}
return std::nullopt;
}
auto list_block_devices() -> std::optional<std::vector<BlockDevice>> {
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()) {