Skip to content

Commit

Permalink
👷 gucc: add helper function to find block device by parent
Browse files Browse the repository at this point in the history
  • Loading branch information
vnepogodin committed Jan 11, 2025
1 parent a3ece6c commit 3600729
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions gucc/include/gucc/block_devices.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions gucc/src/block_devices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down

0 comments on commit 3600729

Please sign in to comment.