2024-01-21 00:13:05 -08:00
|
|
|
use crate::error::{HyphaError, Result};
|
|
|
|
use loopdev::{LoopControl, LoopDevice};
|
|
|
|
use xenclient::BlockDeviceRef;
|
|
|
|
|
|
|
|
pub struct AutoLoop {
|
|
|
|
control: LoopControl,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AutoLoop {
|
|
|
|
pub(crate) fn new(control: LoopControl) -> AutoLoop {
|
|
|
|
AutoLoop { control }
|
|
|
|
}
|
|
|
|
|
2024-01-21 17:21:19 -08:00
|
|
|
pub fn loopify(&self, file: &str) -> Result<BlockDeviceRef> {
|
2024-01-21 00:13:05 -08:00
|
|
|
let device = self.control.next_free()?;
|
|
|
|
device.with().read_only(true).attach(file)?;
|
|
|
|
let path = device
|
|
|
|
.path()
|
|
|
|
.ok_or(HyphaError::new("unable to get loop device path"))?
|
|
|
|
.to_str()
|
|
|
|
.ok_or(HyphaError::new(
|
|
|
|
"unable to convert loop device path to string",
|
|
|
|
))?
|
|
|
|
.to_string();
|
|
|
|
let major = device.major()?;
|
|
|
|
let minor = device.minor()?;
|
|
|
|
Ok(BlockDeviceRef { path, major, minor })
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unloop(&self, device: &str) -> Result<()> {
|
|
|
|
let device = LoopDevice::open(device)?;
|
|
|
|
device.detach()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|