Skip to content

Commit

Permalink
Defer &Path -> &str conversion to docker code
Browse files Browse the repository at this point in the history
  • Loading branch information
nbdd0121 committed Apr 18, 2024
1 parent 631e5c1 commit ef5fbc4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
11 changes: 8 additions & 3 deletions src/docker/container.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::mem::ManuallyDrop;
use std::path::Path;
use std::pin::pin;
use std::sync::{Arc, Mutex};
use std::time::Duration;
Expand Down Expand Up @@ -245,8 +246,9 @@ impl Container {
Ok(())
}

pub async fn mknod(&self, node: &str, (major, minor): (u32, u32)) -> Result<()> {
pub async fn mknod(&self, node: &Path, (major, minor): (u32, u32)) -> Result<()> {
self.rm(node).await?;
let node = node.to_str().context("node is not UTF-8")?;
self.mkdir_for(node).await?;
self.exec_as_root(&["mknod", node, "c", &major.to_string(), &minor.to_string()])
.await?
Expand All @@ -256,7 +258,9 @@ impl Container {
Ok(())
}

pub async fn symlink(&self, source: &str, link: &str) -> Result<()> {
pub async fn symlink(&self, source: &Path, link: &Path) -> Result<()> {
let source = source.to_str().context("node is not UTF-8")?;
let link = link.to_str().context("symlink is not UTF-8")?;
self.mkdir_for(link).await?;
self.exec_as_root(&["ln", "-sf", source, link])
.await?
Expand All @@ -266,7 +270,8 @@ impl Container {
Ok(())
}

pub async fn rm(&self, node: &str) -> Result<()> {
pub async fn rm(&self, node: &Path) -> Result<()> {
let node = node.to_str().context("node is not UTF-8")?;
self.exec_as_root(&["rm", "-f", node])
.await?
.collect()
Expand Down
19 changes: 7 additions & 12 deletions src/hotplug/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::docker::Container;

use async_stream::try_stream;

use anyhow::{Context, Result};
use anyhow::Result;
use futures::stream::LocalBoxStream;

use std::collections::HashMap;
Expand Down Expand Up @@ -109,12 +109,11 @@ impl HotPlug {
self.container
.device(device.devnum(), (true, true, true))
.await?;
let devnode = device.devnode().to_str().context("devnode is not UTF-8")?;
self.container.mknod(devnode, device.devnum()).await?;
self.container
.mknod(device.devnode(), device.devnum())
.await?;
if let Some(symlink) = device.symlink() {
self.container
.symlink(devnode, symlink.to_str().context("symlink is not UTF-8")?)
.await?;
self.container.symlink(device.devnode(), symlink).await?;
}
let syspath = device.syspath().to_owned();
self.devices.insert(syspath, device.clone());
Expand All @@ -127,13 +126,9 @@ impl HotPlug {
self.container
.device(device.devnum(), (false, false, false))
.await?;
self.container
.rm(device.devnode().to_str().context("devnode is not UTF-8")?)
.await?;
self.container.rm(device.devnode()).await?;
if let Some(symlink) = device.symlink() {
self.container
.rm(symlink.to_str().context("devnode is not UTF-8")?)
.await?;
self.container.rm(symlink).await?;
}
Ok(Some(device))
} else {
Expand Down

0 comments on commit ef5fbc4

Please sign in to comment.