Skip to content

Commit

Permalink
i2c: validate the uniqueness of adapter with 'bus-name'
Browse files Browse the repository at this point in the history
When the user selects 'bus-name' as device parameter,
we need to ensure that there is only one adapter with
that name. If not, the user should be notified.

Signed-off-by: Wei Liu <quic_wliu8@quicinc.com>
  • Loading branch information
Q-LiuWei authored and vireshk committed Mar 21, 2024
1 parent ef5689a commit d689c9d
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions vhost-device-i2c/src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub(crate) enum Error {
ClientAddressInvalid,
#[error("Adapter not found")]
AdapterNotFound,
#[error("Multiple adapters share the same name")]
AdapterShareSameName,
#[error("Std IO Error")]
StdIoErr,
#[error("Failed while parsing to integer")]
Expand Down Expand Up @@ -348,6 +350,8 @@ impl PhysDevice {
}

fn find_adapter(name: &str) -> Result<u32> {
let mut adapter_no = None;

for entry in
fs::read_dir(Path::new("/sys/bus/i2c/devices/")).map_err(|_| Error::StdIoErr)?
{
Expand All @@ -357,15 +361,15 @@ impl PhysDevice {
let adapter_name = fs::read_to_string(path).map_err(|_| Error::StdIoErr)?;

if adapter_name.trim() == name {
if adapter_no.is_some() {
return Err(Error::AdapterShareSameName);
}
let path = entry.path();
let list: Vec<&str> = path.to_str().unwrap().split('-').collect();
let adapter_no = list[1].parse::<u32>().map_err(|_| Error::ParseFailure)?;

return Ok(adapter_no);
adapter_no = Some(list[1].parse::<u32>().map_err(|_| Error::ParseFailure)?);
}
}

Err(Error::AdapterNotFound)
adapter_no.ok_or(Error::AdapterNotFound)
}
}

Expand Down

0 comments on commit d689c9d

Please sign in to comment.