diff options
author | Alex Saveau <saveau.alexandre@gmail.com> | 2022-11-06 11:27:08 -0800 |
---|---|---|
committer | Alex Saveau <saveau.alexandre@gmail.com> | 2022-11-06 11:39:09 -0800 |
commit | 17833268cb3269d83766bcdb4d9cbe0e25ebd38e (patch) | |
tree | b86885991e1d8b6f9c610747c2e8c3a3370960cd /src/mount | |
parent | 20df092bd067908fba23e49120eed7ad62f29108 (diff) | |
download | nix-17833268cb3269d83766bcdb4d9cbe0e25ebd38e.zip |
Reformat everything
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
Diffstat (limited to 'src/mount')
-rw-r--r-- | src/mount/bsd.rs | 78 | ||||
-rw-r--r-- | src/mount/linux.rs | 57 | ||||
-rw-r--r-- | src/mount/mod.rs | 25 |
3 files changed, 81 insertions, 79 deletions
diff --git a/src/mount/bsd.rs b/src/mount/bsd.rs index 1ba8b809..d124f1f9 100644 --- a/src/mount/bsd.rs +++ b/src/mount/bsd.rs @@ -1,25 +1,17 @@ #[cfg(target_os = "freebsd")] -use crate::{ - Error, -}; -use crate::{ - Errno, - NixPath, - Result, -}; +use crate::Error; +use crate::{Errno, NixPath, Result}; +use libc::c_int; #[cfg(target_os = "freebsd")] use libc::{c_char, c_uint, c_void}; -use libc::c_int; #[cfg(target_os = "freebsd")] use std::{ borrow::Cow, - ffi::{CString, CStr}, + ffi::{CStr, CString}, + fmt, io, marker::PhantomData, - fmt, - io, }; - libc_bitflags!( /// Used with [`Nmount::nmount`]. pub struct MntFlags: c_int { @@ -111,7 +103,6 @@ libc_bitflags!( } ); - /// The Error type of [`Nmount::nmount`]. /// /// It wraps an [`Errno`], but also may contain an additional message returned @@ -120,7 +111,7 @@ libc_bitflags!( #[derive(Debug)] pub struct NmountError { errno: Error, - errmsg: Option<String> + errmsg: Option<String>, } #[cfg(target_os = "freebsd")] @@ -138,7 +129,7 @@ impl NmountError { fn new(error: Error, errmsg: Option<&CStr>) -> Self { Self { errno: error, - errmsg: errmsg.map(CStr::to_string_lossy).map(Cow::into_owned) + errmsg: errmsg.map(CStr::to_string_lossy).map(Cow::into_owned), } } } @@ -199,7 +190,7 @@ pub type NmountResult = std::result::Result<(), NmountError>; /// .str_opt_owned("fspath", mountpoint.path().to_str().unwrap()) /// .str_opt_owned("target", target.path().to_str().unwrap()) /// .nmount(MntFlags::empty()).unwrap(); -/// +/// /// unmount(mountpoint.path(), MntFlags::empty()).unwrap(); /// ``` /// @@ -209,7 +200,7 @@ pub type NmountResult = std::result::Result<(), NmountError>; #[cfg(target_os = "freebsd")] #[cfg_attr(docsrs, doc(cfg(all())))] #[derive(Debug, Default)] -pub struct Nmount<'a>{ +pub struct Nmount<'a> { // n.b. notgull: In reality, this is a list that contains // both mutable and immutable pointers. // Be careful using this. @@ -231,7 +222,12 @@ impl<'a> Nmount<'a> { } /// Helper function to push a pointer and its length onto the `iov` array. - fn push_pointer_and_length(&mut self, val: *const u8, len: usize, is_owned: bool) { + fn push_pointer_and_length( + &mut self, + val: *const u8, + len: usize, + is_owned: bool, + ) { self.iov.push(libc::iovec { iov_base: val as *mut _, iov_len: len, @@ -246,7 +242,8 @@ impl<'a> Nmount<'a> { let ptr = s.to_owned().into_raw() as *const u8; self.push_pointer_and_length(ptr, len, true); - }).unwrap(); + }) + .unwrap(); } /// Add an opaque mount option. @@ -280,9 +277,8 @@ impl<'a> Nmount<'a> { &mut self, name: &'a CStr, val: *mut c_void, - len: usize - ) -> &mut Self - { + len: usize, + ) -> &mut Self { self.push_slice(name.to_bytes_with_nul(), false); self.push_pointer_and_length(val.cast(), len, false); self @@ -321,8 +317,10 @@ impl<'a> Nmount<'a> { /// let mut nmount: Nmount<'static> = Nmount::new(); /// nmount.null_opt_owned(read_only); /// ``` - pub fn null_opt_owned<P: ?Sized + NixPath>(&mut self, name: &P) -> &mut Self - { + pub fn null_opt_owned<P: ?Sized + NixPath>( + &mut self, + name: &P, + ) -> &mut Self { self.push_nix_path(name); self.push_slice(&[], false); self @@ -340,12 +338,7 @@ impl<'a> Nmount<'a> { /// Nmount::new() /// .str_opt(&fstype, &nullfs); /// ``` - pub fn str_opt( - &mut self, - name: &'a CStr, - val: &'a CStr - ) -> &mut Self - { + pub fn str_opt(&mut self, name: &'a CStr, val: &'a CStr) -> &mut Self { self.push_slice(name.to_bytes_with_nul(), false); self.push_slice(val.to_bytes_with_nul(), false); self @@ -367,8 +360,9 @@ impl<'a> Nmount<'a> { /// .str_opt_owned("fspath", mountpoint.to_str().unwrap()); /// ``` pub fn str_opt_owned<P1, P2>(&mut self, name: &P1, val: &P2) -> &mut Self - where P1: ?Sized + NixPath, - P2: ?Sized + NixPath + where + P1: ?Sized + NixPath, + P2: ?Sized + NixPath, { self.push_nix_path(name); self.push_nix_path(val); @@ -398,9 +392,7 @@ impl<'a> Nmount<'a> { let niov = self.iov.len() as c_uint; let iovp = self.iov.as_mut_ptr() as *mut libc::iovec; - let res = unsafe { - libc::nmount(iovp, niov, flags.bits) - }; + let res = unsafe { libc::nmount(iovp, niov, flags.bits) }; match Errno::result(res) { Ok(_) => Ok(()), Err(error) => { @@ -437,7 +429,9 @@ impl<'a> Drop for Nmount<'a> { /// /// Useful flags include /// * `MNT_FORCE` - Unmount even if still in use. -#[cfg_attr(target_os = "freebsd", doc = " +#[cfg_attr( + target_os = "freebsd", + doc = " * `MNT_BYFSID` - `mountpoint` is not a path, but a file system ID encoded as `FSID:val0:val1`, where `val0` and `val1` are the contents of the `fsid_t val[]` array in decimal. @@ -445,12 +439,14 @@ impl<'a> Drop for Nmount<'a> { will be unmounted. See [`statfs`](crate::sys::statfs::statfs) to determine the `fsid`. -")] +" +)] pub fn unmount<P>(mountpoint: &P, flags: MntFlags) -> Result<()> - where P: ?Sized + NixPath +where + P: ?Sized + NixPath, { - let res = mountpoint.with_nix_path(|cstr| { - unsafe { libc::unmount(cstr.as_ptr(), flags.bits) } + let res = mountpoint.with_nix_path(|cstr| unsafe { + libc::unmount(cstr.as_ptr(), flags.bits) })?; Errno::result(res).map(drop) diff --git a/src/mount/linux.rs b/src/mount/linux.rs index 4c976dcb..cf6a60b0 100644 --- a/src/mount/linux.rs +++ b/src/mount/linux.rs @@ -1,7 +1,7 @@ #![allow(missing_docs)] -use libc::{self, c_ulong, c_int}; -use crate::{Result, NixPath}; use crate::errno::Errno; +use crate::{NixPath, Result}; +use libc::{self, c_int, c_ulong}; libc_bitflags!( pub struct MsFlags: c_ulong { @@ -57,36 +57,40 @@ libc_bitflags!( } ); -pub fn mount<P1: ?Sized + NixPath, P2: ?Sized + NixPath, P3: ?Sized + NixPath, P4: ?Sized + NixPath>( - source: Option<&P1>, - target: &P2, - fstype: Option<&P3>, - flags: MsFlags, - data: Option<&P4>) -> Result<()> { - +pub fn mount< + P1: ?Sized + NixPath, + P2: ?Sized + NixPath, + P3: ?Sized + NixPath, + P4: ?Sized + NixPath, +>( + source: Option<&P1>, + target: &P2, + fstype: Option<&P3>, + flags: MsFlags, + data: Option<&P4>, +) -> Result<()> { fn with_opt_nix_path<P, T, F>(p: Option<&P>, f: F) -> Result<T> - where P: ?Sized + NixPath, - F: FnOnce(*const libc::c_char) -> T + where + P: ?Sized + NixPath, + F: FnOnce(*const libc::c_char) -> T, { match p { Some(path) => path.with_nix_path(|p_str| f(p_str.as_ptr())), - None => Ok(f(std::ptr::null())) + None => Ok(f(std::ptr::null())), } } let res = with_opt_nix_path(source, |s| { target.with_nix_path(|t| { with_opt_nix_path(fstype, |ty| { - with_opt_nix_path(data, |d| { - unsafe { - libc::mount( - s, - t.as_ptr(), - ty, - flags.bits, - d as *const libc::c_void - ) - } + with_opt_nix_path(data, |d| unsafe { + libc::mount( + s, + t.as_ptr(), + ty, + flags.bits, + d as *const libc::c_void, + ) }) }) }) @@ -96,16 +100,15 @@ pub fn mount<P1: ?Sized + NixPath, P2: ?Sized + NixPath, P3: ?Sized + NixPath, P } pub fn umount<P: ?Sized + NixPath>(target: &P) -> Result<()> { - let res = target.with_nix_path(|cstr| { - unsafe { libc::umount(cstr.as_ptr()) } - })?; + let res = + target.with_nix_path(|cstr| unsafe { libc::umount(cstr.as_ptr()) })?; Errno::result(res).map(drop) } pub fn umount2<P: ?Sized + NixPath>(target: &P, flags: MntFlags) -> Result<()> { - let res = target.with_nix_path(|cstr| { - unsafe { libc::umount2(cstr.as_ptr(), flags.bits) } + let res = target.with_nix_path(|cstr| unsafe { + libc::umount2(cstr.as_ptr(), flags.bits) })?; Errno::result(res).map(drop) diff --git a/src/mount/mod.rs b/src/mount/mod.rs index e89c1a07..e98b49c3 100644 --- a/src/mount/mod.rs +++ b/src/mount/mod.rs @@ -6,18 +6,21 @@ mod linux; #[cfg(any(target_os = "android", target_os = "linux"))] pub use self::linux::*; -#[cfg(any(target_os = "dragonfly", - target_os = "freebsd", - target_os = "macos", - target_os = "netbsd", - target_os = "openbsd"))] +#[cfg(any( + target_os = "dragonfly", + target_os = "freebsd", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd" +))] #[cfg_attr(docsrs, doc(cfg(all())))] mod bsd; -#[cfg(any(target_os = "dragonfly", - target_os = "freebsd", - target_os = "macos", - target_os = "netbsd", - target_os = "openbsd" - ))] +#[cfg(any( + target_os = "dragonfly", + target_os = "freebsd", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd" +))] pub use self::bsd::*; |