Skip to content

Commit

Permalink
Rename plugged_device to attach_device
Browse files Browse the repository at this point in the history
  • Loading branch information
nbdd0121 committed Apr 19, 2024
1 parent 810aa15 commit 27140f4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ use std::path::PathBuf;
use crate::dev::Device;

#[derive(Clone)]
pub struct PluggedDevice {
pub struct AttachedDevice {
pub(super) device: Device,
pub(super) symlinks: Vec<PathBuf>,
}

impl Deref for PluggedDevice {
impl Deref for AttachedDevice {
type Target = Device;

fn deref(&self) -> &Self::Target {
&self.device
}
}

impl Display for PluggedDevice {
impl Display for AttachedDevice {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
if let Some(devnode) = self.devnode() {
let (major, minor) = devnode.devnum;
Expand Down
24 changes: 12 additions & 12 deletions src/hotplug/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod plugged_device;
mod attached_device;

use crate::cgroup::Access;
use crate::cli;
Expand All @@ -12,19 +12,19 @@ use std::path::PathBuf;
use tokio_stream::StreamExt;

pub use crate::dev::{Device, DeviceEvent};
pub use plugged_device::PluggedDevice;
pub use attached_device::AttachedDevice;

#[derive(Clone)]
pub enum Event {
Add(PluggedDevice),
Remove(PluggedDevice),
Attach(AttachedDevice),
Detach(AttachedDevice),
}

pub struct HotPlug {
pub container: Container,
symlinks: Vec<cli::Symlink>,
monitor: DeviceMonitor,
devices: HashMap<PathBuf, PluggedDevice>,
devices: HashMap<PathBuf, AttachedDevice>,
}

impl HotPlug {
Expand Down Expand Up @@ -60,11 +60,11 @@ impl HotPlug {
continue;
}
let device = self.allow_device(&device).await?;
yield Event::Add(device);
yield Event::Attach(device);
}
DeviceEvent::Remove(device) => {
if let Some(plugged) = self.deny_device(device.udev()).await? {
yield Event::Remove(plugged);
yield Event::Detach(plugged);
}
}
}
Expand All @@ -81,22 +81,22 @@ impl HotPlug {
continue;
}
let device = self.allow_device(&device).await?;
yield Event::Add(device);
yield Event::Attach(device);
}
DeviceEvent::Remove(device) => {
if let Some(plugged) = self.deny_device(device.udev()).await? {
yield Event::Remove(plugged);
yield Event::Detach(plugged);
}
}
}
}
}
}

async fn allow_device(&mut self, device: &Device) -> Result<PluggedDevice> {
async fn allow_device(&mut self, device: &Device) -> Result<AttachedDevice> {
let device = device.clone();
let symlinks = self.find_symlinks(&device);
let device = PluggedDevice { device, symlinks };
let device = AttachedDevice { device, symlinks };
let devnode = device.devnode().unwrap();
self.container.device(devnode.devnum, Access::all()).await?;
self.container.mknod(&devnode.path, devnode.devnum).await?;
Expand All @@ -108,7 +108,7 @@ impl HotPlug {
Ok(device)
}

async fn deny_device(&mut self, device: &udev::Device) -> Result<Option<PluggedDevice>> {
async fn deny_device(&mut self, device: &udev::Device) -> Result<Option<AttachedDevice>> {
let syspath = device.syspath().to_owned();
if let Some(device) = self.devices.remove(&syspath) {
let devnode = device.devnode().unwrap();
Expand Down
16 changes: 8 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod util;

use cli::{Action, DeviceRef, Symlink};
use docker::{Container, Docker};
use hotplug::{Event as HotPlugEvent, HotPlug, PluggedDevice};
use hotplug::{AttachedDevice, Event as HotPlugEvent, HotPlug};

use std::fmt::Display;
use std::pin::pin;
Expand All @@ -20,28 +20,28 @@ use log::info;

#[derive(Clone)]
enum Event {
Add(PluggedDevice),
Remove(PluggedDevice),
Attach(AttachedDevice),
Detach(AttachedDevice),
Initialized,
Stopped(i64),
}

impl From<HotPlugEvent> for Event {
fn from(evt: HotPlugEvent) -> Self {
match evt {
HotPlugEvent::Add(dev) => Self::Add(dev),
HotPlugEvent::Remove(dev) => Self::Remove(dev),
HotPlugEvent::Attach(dev) => Self::Attach(dev),
HotPlugEvent::Detach(dev) => Self::Detach(dev),
}
}
}

impl Display for Event {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Event::Add(dev) => {
Event::Attach(dev) => {
write!(f, "Attaching device {dev}")
}
Event::Remove(dev) => {
Event::Detach(dev) => {
write!(f, "Detaching device {dev}")
}
Event::Initialized => {
Expand Down Expand Up @@ -124,7 +124,7 @@ async fn run(param: cli::Run, verbosity: Verbosity<InfoLevel>) -> Result<u8> {
let event = event?;
info!("{}", event);
match event {
Event::Remove(dev) if dev.syspath() == hub_path => {
Event::Detach(dev) if dev.syspath() == hub_path => {
info!("Hub device detached. Stopping container.");
status = param.root_unplugged_exit_code;
container.kill(15).await?;
Expand Down

0 comments on commit 27140f4

Please sign in to comment.