Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b9f40f83 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Automerger Merge Worker
Browse files

Merge "libdm: Make ExtractBlockDeviceName() return its result" into sc-dev am:...

Merge "libdm: Make ExtractBlockDeviceName() return its result" into sc-dev am: 232ec664 am: 10e14c95

Original change: https://googleplex-android-review.googlesource.com/c/platform/system/core/+/15584612

Change-Id: I3b11fd3d20d34a9440ddb74e96a75922877534c7
parents 9e6087a8 10e14c95
Loading
Loading
Loading
Loading
+13 −17
Original line number Diff line number Diff line
@@ -578,34 +578,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<std::string> 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<std::string> name = ExtractBlockDeviceName(path);
    return name && android::base::StartsWith(*name, "dm-");
}

std::optional<std::string> DeviceMapper::GetDmDeviceNameByPath(const std::string& path) {
    std::string name;
    if (!ExtractBlockDeviceName(path, &name)) {
    std::optional<std::string> 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;
@@ -616,16 +612,16 @@ std::optional<std::string> DeviceMapper::GetDmDeviceNameByPath(const std::string
}

std::optional<std::string> DeviceMapper::GetParentBlockDeviceByPath(const std::string& path) {
    std::string name;
    if (!ExtractBlockDeviceName(path, &name)) {
    std::optional<std::string> 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<DIR, decltype(&closedir)>(opendir(slaves_dir.c_str()), closedir);
    if (dir == nullptr) {
        PLOG(ERROR) << "Failed to open: " << slaves_dir;