Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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 {
Expand All @@ -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<P: AsRef<Path>>(dev: P) -> io::Result<Self> {
Expand Down Expand Up @@ -245,6 +263,11 @@ impl LoopDevice {
std::fs::read_link(&p).ok()
}

/// Get the device metadata
pub fn metadata(&self) -> io::Result<Metadata> {
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
Expand Down