pbs fuse loop: rust fmt

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2022-04-06 16:59:54 +02:00
parent b2df21bb02
commit a527b54f84
3 changed files with 19 additions and 10 deletions

View File

@ -365,7 +365,9 @@ fn unmap_from_backing(backing_file: &Path, loopdev: Option<&str>) -> Result<(),
pub fn find_all_mappings() -> Result<impl Iterator<Item = (String, Option<String>)>, Error> { pub fn find_all_mappings() -> Result<impl Iterator<Item = (String, Option<String>)>, Error> {
// get map of all /dev/loop mappings belonging to us // get map of all /dev/loop mappings belonging to us
let mut loopmap = HashMap::new(); let mut loopmap = HashMap::new();
for ent in proxmox_sys::fs::scan_subdir(libc::AT_FDCWD, Path::new("/dev/"), &LOOPDEV_REGEX)?.flatten() { for ent in
proxmox_sys::fs::scan_subdir(libc::AT_FDCWD, Path::new("/dev/"), &LOOPDEV_REGEX)?.flatten()
{
let loopdev = format!("/dev/{}", ent.file_name().to_string_lossy()); let loopdev = format!("/dev/{}", ent.file_name().to_string_lossy());
if let Ok(file) = get_backing_file(&loopdev) { if let Ok(file) = get_backing_file(&loopdev) {
// insert filename only, strip RUN_DIR/ // insert filename only, strip RUN_DIR/

View File

@ -1,5 +1,4 @@
pub mod loopdev; pub mod loopdev;
mod fuse_loop; mod fuse_loop;
pub use fuse_loop::*; pub use fuse_loop::*;

View File

@ -1,8 +1,8 @@
//! Helpers to work with /dev/loop* devices //! Helpers to work with /dev/loop* devices
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};
use std::os::unix::io::{AsRawFd, RawFd};
use std::path::Path; use std::path::Path;
use std::os::unix::io::{RawFd, AsRawFd};
use anyhow::Error; use anyhow::Error;
@ -24,7 +24,11 @@ mod loop_ioctl {
ioctl_write_int_bad!(ioctl_set_fd, (LOOP_IOCTL << 8) | LOOP_SET_FD); ioctl_write_int_bad!(ioctl_set_fd, (LOOP_IOCTL << 8) | LOOP_SET_FD);
ioctl_none!(ioctl_clr_fd, LOOP_IOCTL, LOOP_CLR_FD); ioctl_none!(ioctl_clr_fd, LOOP_IOCTL, LOOP_CLR_FD);
ioctl_none!(ioctl_ctrl_get_free, LOOP_IOCTL, LOOP_CTRL_GET_FREE); ioctl_none!(ioctl_ctrl_get_free, LOOP_IOCTL, LOOP_CTRL_GET_FREE);
ioctl_write_ptr_bad!(ioctl_set_status64, (LOOP_IOCTL << 8) | LOOP_SET_STATUS64, LoopInfo64); ioctl_write_ptr_bad!(
ioctl_set_status64,
(LOOP_IOCTL << 8) | LOOP_SET_STATUS64,
LoopInfo64
);
pub const LO_FLAGS_READ_ONLY: u32 = 1; pub const LO_FLAGS_READ_ONLY: u32 = 1;
pub const LO_FLAGS_PARTSCAN: u32 = 8; pub const LO_FLAGS_PARTSCAN: u32 = 8;
@ -66,12 +70,16 @@ pub fn get_or_create_free_dev() -> Result<String, Error> {
} }
fn assign_dev(fd: RawFd, backing_fd: RawFd) -> Result<(), Error> { fn assign_dev(fd: RawFd, backing_fd: RawFd) -> Result<(), Error> {
unsafe { ioctl_set_fd(fd, backing_fd)?; } unsafe {
ioctl_set_fd(fd, backing_fd)?;
}
// set required read-only flag and partscan for convenience // set required read-only flag and partscan for convenience
let mut info: LoopInfo64 = unsafe { std::mem::zeroed() }; let mut info: LoopInfo64 = unsafe { std::mem::zeroed() };
info.lo_flags = LO_FLAGS_READ_ONLY | LO_FLAGS_PARTSCAN; info.lo_flags = LO_FLAGS_READ_ONLY | LO_FLAGS_PARTSCAN;
unsafe { ioctl_set_status64(fd, &info)?; } unsafe {
ioctl_set_status64(fd, &info)?;
}
Ok(()) Ok(())
} }
@ -80,9 +88,7 @@ fn assign_dev(fd: RawFd, backing_fd: RawFd) -> Result<(), Error> {
/// it as it's backing file in read-only mode. /// it as it's backing file in read-only mode.
pub fn assign<P: AsRef<Path>>(loop_dev: P, backing: P) -> Result<(), Error> { pub fn assign<P: AsRef<Path>>(loop_dev: P, backing: P) -> Result<(), Error> {
let loop_file = File::open(loop_dev)?; let loop_file = File::open(loop_dev)?;
let backing_file = OpenOptions::new() let backing_file = OpenOptions::new().read(true).open(backing)?;
.read(true)
.open(backing)?;
assign_dev(loop_file.as_raw_fd(), backing_file.as_raw_fd())?; assign_dev(loop_file.as_raw_fd(), backing_file.as_raw_fd())?;
Ok(()) Ok(())
} }
@ -91,6 +97,8 @@ pub fn assign<P: AsRef<Path>>(loop_dev: P, backing: P) -> Result<(), Error> {
/// /dev/loopN device. /// /dev/loopN device.
pub fn unassign<P: AsRef<Path>>(path: P) -> Result<(), Error> { pub fn unassign<P: AsRef<Path>>(path: P) -> Result<(), Error> {
let loop_file = File::open(path)?; let loop_file = File::open(path)?;
unsafe { ioctl_clr_fd(loop_file.as_raw_fd())?; } unsafe {
ioctl_clr_fd(loop_file.as_raw_fd())?;
}
Ok(()) Ok(())
} }