From 0ca323351841bc77d31bdc2bd0d396565c24a508 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BChlbacher?= Date: Fri, 31 May 2024 12:24:38 +0200 Subject: [PATCH] refactor: change some fn type sigs and simplify MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Mühlbacher --- src/commands/mount.rs | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/src/commands/mount.rs b/src/commands/mount.rs index 4757bbff..381dabae 100644 --- a/src/commands/mount.rs +++ b/src/commands/mount.rs @@ -192,8 +192,8 @@ fn get_devices_by_uuid( #[allow(clippy::type_complexity)] fn get_uuid_for_dev_node( udev_bcachefs: &HashMap>, - device: &std::path::PathBuf, -) -> anyhow::Result<(Option, Option<(PathBuf, bch_sb_handle)>)> { + device: impl AsRef, +) -> Result<(Option, Option<(PathBuf, bch_sb_handle)>)> { let canonical = fs::canonicalize(device)?; if !udev_bcachefs.is_empty() { @@ -261,11 +261,11 @@ pub struct Cli { fn devs_str_sbs_from_uuid( udev_info: &HashMap>, - uuid: String, + uuid: &str, ) -> anyhow::Result<(String, Vec)> { debug!("enumerating devices with UUID {}", uuid); - let devs_sbs = Uuid::parse_str(&uuid).map(|uuid| get_devices_by_uuid(udev_info, uuid))??; + let devs_sbs = Uuid::parse_str(uuid).map(|uuid| get_devices_by_uuid(udev_info, uuid))??; let devs_str = devs_sbs .iter() @@ -280,7 +280,7 @@ fn devs_str_sbs_from_uuid( fn devs_str_sbs_from_device( udev_info: &HashMap>, - device: &std::path::PathBuf, + device: impl AsRef, ) -> anyhow::Result<(String, Vec)> { let (uuid, sb_info) = get_uuid_for_dev_node(udev_info, device)?; @@ -297,10 +297,10 @@ fn devs_str_sbs_from_device( let dev = path.into_os_string().into_string().unwrap(); Ok((dev, vec![sb])) } else { - devs_str_sbs_from_uuid(udev_info, uuid.to_string()) + devs_str_sbs_from_uuid(udev_info, &uuid.to_string()) } } - (Some(uuid), None) => devs_str_sbs_from_uuid(udev_info, uuid.to_string()), + (Some(uuid), None) => devs_str_sbs_from_uuid(udev_info, &uuid.to_string()), _ => Ok((String::new(), Vec::new())), } } @@ -309,11 +309,9 @@ fn cmd_mount_inner(opt: Cli) -> Result<()> { // Grab the udev information once let udev_info = udev_bcachefs_info()?; - let (devices, sbs) = if opt.dev.starts_with("UUID=") { - let uuid = opt.dev.replacen("UUID=", "", 1); + let (devices, sbs) = if let Some(uuid) = opt.dev.strip_prefix("UUID=") { devs_str_sbs_from_uuid(&udev_info, uuid)? - } else if opt.dev.starts_with("OLD_BLKID_UUID=") { - let uuid = opt.dev.replacen("OLD_BLKID_UUID=", "", 1); + } else if let Some(uuid) = opt.dev.strip_prefix("OLD_BLKID_UUID=") { devs_str_sbs_from_uuid(&udev_info, uuid)? } else { // If the device string contains ":" we will assume the user knows the entire list. @@ -321,16 +319,15 @@ fn cmd_mount_inner(opt: Cli) -> Result<()> { // only 1 of a number of devices which are part of the FS. This appears to be the case // when we get called during fstab mount processing and the fstab specifies a UUID. if opt.dev.contains(':') { - let mut block_devices_to_mount = Vec::new(); + let sbs = opt + .dev + .split(':') + .map(read_super_silent) + .collect::>>()?; - for dev in opt.dev.split(':') { - let dev = PathBuf::from(dev); - block_devices_to_mount.push(read_super_silent(&dev)?); - } - - (opt.dev, block_devices_to_mount) + (opt.dev, sbs) } else { - devs_str_sbs_from_device(&udev_info, &PathBuf::from(opt.dev))? + devs_str_sbs_from_device(&udev_info, Path::new(&opt.dev))? } };