cholcombe973 / block-utils

Rust utilities for working with block devices
MIT License
22 stars 17 forks source link

`get_block_dev_properties` doesn't work #22

Closed Apostoln closed 3 years ago

Apostoln commented 3 years ago

I've found one another bug in my last PR.

pub fn get_block_dev_properties(
    device_path: impl AsRef<Path>,
) -> BlockResult<HashMap<String, String>> {
    let syspath = device_path
        .as_ref()
        .file_name()
        .map(PathBuf::from)
        .ok_or_else(|| {
            BlockUtilsError::new(format!(
                "Unable to get file_name on device {:?}",
                device_path.as_ref()
            ))
        })?;

    let udev_device = udev::Device::from_syspath(&syspath)?; //HERE
    Ok(udev_device
        .clone()
        .properties()
        .map(|property| {
            let key = property.name().to_string_lossy().to_string();
            let value = property.value().to_string_lossy().to_string();
            (key, value)
        })
        .collect()) // We can't return iterator because `udev_device` doesn't live long enough
}

udev::Device::from_syspath expects full syspath like /sys/block/loop0 instead of just a sysname loop0 like in udevadm CLI. udevadm CLI expects sysname and found exact syspath automatically. So, these functions get_block_dev_properties and get_block_dev_property doesn't work properly.

And I'm not sure how to fix it right now. As far as I understand, this path can lay not only in /sys/block/$dev but also in other /sys/ subdirectories.

Unfortunately I don't have enough time right now so I'm going to investigate it over the next few weeks.