diff --git a/src/lib.rs b/src/lib.rs index ff8eec0..4821f79 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,9 +22,9 @@ use bindings::{ loop_info64, LOOP_CLR_FD, LOOP_CTL_GET_FREE, LOOP_SET_CAPACITY, LOOP_SET_FD, LOOP_SET_STATUS64, }; use libc::{c_int, ioctl}; -use std::fs::{File, OpenOptions}; use std::{ default::Default, + fs::{File, Metadata, OpenOptions}, io, os::unix::prelude::*, path::{Path, PathBuf}, @@ -86,6 +86,18 @@ impl LoopControl { } } +impl AsRawFd for LoopControl { + fn as_raw_fd(&self) -> RawFd { + self.dev_file.as_raw_fd() + } +} + +impl IntoRawFd for LoopControl { + fn into_raw_fd(self) -> RawFd { + self.dev_file.into_raw_fd() + } +} + /// Interface to a loop device ie `/dev/loop0`. #[derive(Debug)] pub struct LoopDevice { @@ -98,6 +110,12 @@ impl AsRawFd for LoopDevice { } } +impl IntoRawFd for LoopDevice { + fn into_raw_fd(self) -> RawFd { + self.device.into_raw_fd() + } +} + impl LoopDevice { /// Opens a loop device. pub fn open>(dev: P) -> io::Result { @@ -245,6 +263,11 @@ impl LoopDevice { std::fs::read_link(&p).ok() } + /// Get the device metadata + pub fn metadata(&self) -> io::Result { + self.device.metadata() + } + /// Detach a loop device from its backing file. /// /// Note that the device won't fully detach until a short delay after the underling device file