From cb82728e03795bfbcdfcc7793e17f164191599b9 Mon Sep 17 00:00:00 2001 From: Kevin Mehall Date: Sat, 3 Aug 2024 18:20:54 -0600 Subject: [PATCH] linux: enumeration: filter non-devices from sysfs * Avoids logging errors when probing USB interfaces that don't have device properties * Ignores root hubs (they're not useful to open, not exposed on other platforms, and sometimes slow to read properties from for some reason) --- src/platform/linux_usbfs/enumeration.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/platform/linux_usbfs/enumeration.rs b/src/platform/linux_usbfs/enumeration.rs index e046f36..eb401e8 100644 --- a/src/platform/linux_usbfs/enumeration.rs +++ b/src/platform/linux_usbfs/enumeration.rs @@ -5,6 +5,7 @@ use std::path::PathBuf; use std::str::FromStr; use log::debug; +use log::warn; use crate::enumeration::InterfaceInfo; use crate::DeviceInfo; @@ -67,11 +68,24 @@ const SYSFS_PREFIX: &'static str = "/sys/bus/usb/devices/"; pub fn list_devices() -> Result, Error> { Ok(fs::read_dir(SYSFS_PREFIX)?.flat_map(|entry| { - let res = probe_device(SysfsPath(entry.ok()?.path())); - if let Err(x) = &res { - debug!("failed to probe, skipping: {x}") + let path = entry.ok()?.path(); + let name = path.file_name()?; + + // Device names look like `1-6` or `1-6.4.2` + // We'll ignore: + // * root hubs (`usb1`) -- they're not useful to talk to and are not exposed on other platforms + // * interfaces (`1-6:1.0`) + if !name + .as_encoded_bytes() + .iter() + .all(|c| matches!(c, b'0'..=b'9' | b'-' | b'.')) + { + return None; } - res.ok() + + probe_device(SysfsPath(path)) + .inspect_err(|e| warn!("{e}; ignoring device")) + .ok() })) }