From 96b21cc589349d346e4c587d92867ddee66f45dd Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Thu, 12 Aug 2021 10:56:44 -0700 Subject: [PATCH] libdm: Make ExtractBlockDeviceName() return its result From https://engdoc.corp.google.com/eng/doc/devguide/cpp/styleguide.md: "Prefer using return values over output parameters: they improve readability, and often provide the same or better performance (see the C++ Primer)." Implement this advice for ExtractBlockDeviceName(). This patch does not change any functionality. Bug: 194450129 Test: mm libfs_mgr libdm_test Merged-In: I6363781163eba08e6128507b403200f472f68a59 Change-Id: I7d340b33281ebccded0836cd0b5a293e941f4043 Ignore-AOSP-First: Already in AOSP. Signed-off-by: Bart Van Assche --- fs_mgr/libdm/dm.cpp | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/fs_mgr/libdm/dm.cpp b/fs_mgr/libdm/dm.cpp index c4874b8d1..9a518c696 100644 --- a/fs_mgr/libdm/dm.cpp +++ b/fs_mgr/libdm/dm.cpp @@ -560,34 +560,30 @@ std::string DeviceMapper::GetTargetType(const struct dm_target_spec& spec) { return std::string{spec.target_type, sizeof(spec.target_type)}; } -static bool ExtractBlockDeviceName(const std::string& path, std::string* name) { +static std::optional ExtractBlockDeviceName(const std::string& path) { static constexpr std::string_view kDevBlockPrefix("/dev/block/"); if (android::base::StartsWith(path, kDevBlockPrefix)) { - *name = path.substr(kDevBlockPrefix.length()); - return true; + return path.substr(kDevBlockPrefix.length()); } - return false; + return {}; } bool DeviceMapper::IsDmBlockDevice(const std::string& path) { - std::string name; - if (!ExtractBlockDeviceName(path, &name)) { - return false; - } - return android::base::StartsWith(name, "dm-"); + std::optional name = ExtractBlockDeviceName(path); + return name && android::base::StartsWith(*name, "dm-"); } std::optional DeviceMapper::GetDmDeviceNameByPath(const std::string& path) { - std::string name; - if (!ExtractBlockDeviceName(path, &name)) { + std::optional name = ExtractBlockDeviceName(path); + if (!name) { LOG(WARNING) << path << " is not a block device"; return std::nullopt; } - if (!android::base::StartsWith(name, "dm-")) { + if (!android::base::StartsWith(*name, "dm-")) { LOG(WARNING) << path << " is not a dm device"; return std::nullopt; } - std::string dm_name_file = "/sys/block/" + name + "/dm/name"; + std::string dm_name_file = "/sys/block/" + *name + "/dm/name"; std::string dm_name; if (!android::base::ReadFileToString(dm_name_file, &dm_name)) { PLOG(ERROR) << "Failed to read file " << dm_name_file; @@ -598,16 +594,16 @@ std::optional DeviceMapper::GetDmDeviceNameByPath(const std::string } std::optional DeviceMapper::GetParentBlockDeviceByPath(const std::string& path) { - std::string name; - if (!ExtractBlockDeviceName(path, &name)) { + std::optional name = ExtractBlockDeviceName(path); + if (!name) { LOG(WARNING) << path << " is not a block device"; return std::nullopt; } - if (!android::base::StartsWith(name, "dm-")) { + if (!android::base::StartsWith(*name, "dm-")) { // Reached bottom of the device mapper stack. return std::nullopt; } - auto slaves_dir = "/sys/block/" + name + "/slaves"; + auto slaves_dir = "/sys/block/" + *name + "/slaves"; auto dir = std::unique_ptr(opendir(slaves_dir.c_str()), closedir); if (dir == nullptr) { PLOG(ERROR) << "Failed to open: " << slaves_dir;