From 28f31300525b2ee18cb77df0796fa5a83e5b8ac9 Mon Sep 17 00:00:00 2001 From: Felix Obenhuber Date: Thu, 27 May 2021 08:46:36 +0200 Subject: [PATCH 1/2] Implement `AsRawFd` and `IntoRawFd` for LoopControl and LoopDevice Getting the raw fd of loop control can be neccecary because the `next_free` and `attach` operation is (unfortunately) not atomic. The fd can be used to acquire a `flock`. Add `IntoRawFd` for LoopDevice. --- src/lib.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index ff8eec0..a52eaf4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { From 546d63449f9b5116ed967d55fabae46340d58d0a Mon Sep 17 00:00:00 2001 From: Felix Obenhuber Date: Thu, 27 May 2021 08:49:02 +0200 Subject: [PATCH 2/2] Add `metadata` for a `LoopDevice` Getting the `Metadata` is usefull to access the major/minor number of the device that is needed when using the loopdev in combination with device-mapper. --- src/lib.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index a52eaf4..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}, @@ -263,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