From b213dbb7c89bc179ac7a0cea9d21eaefc852990e Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Tue, 7 Dec 2021 11:41:47 +0100 Subject: [PATCH] sys: formatting Signed-off-by: Wolfgang Bumiller --- proxmox-sys/src/fd/fd.rs | 2 -- proxmox-sys/src/fs/read_dir.rs | 50 +++++++++++++++++++++------------- proxmox-sys/src/fs/xattr.rs | 48 ++++++++++++++++++++++---------- 3 files changed, 65 insertions(+), 35 deletions(-) diff --git a/proxmox-sys/src/fd/fd.rs b/proxmox-sys/src/fd/fd.rs index a752482e..6781d706 100644 --- a/proxmox-sys/src/fd/fd.rs +++ b/proxmox-sys/src/fd/fd.rs @@ -1,4 +1,3 @@ - use std::borrow::Borrow; use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; @@ -107,4 +106,3 @@ impl AsRawFd for FdRef { unsafe { *(self as *const Self as *const RawFd) } } } - diff --git a/proxmox-sys/src/fs/read_dir.rs b/proxmox-sys/src/fs/read_dir.rs index 430372da..f687748e 100644 --- a/proxmox-sys/src/fs/read_dir.rs +++ b/proxmox-sys/src/fs/read_dir.rs @@ -97,8 +97,11 @@ impl Iterator for ReadDir { fn next(&mut self) -> Option { self.iter.next().map(|res| { - res.map(|entry| ReadDirEntry { entry, parent_fd: self.dir_fd }) - .map_err(Error::from) + res.map(|entry| ReadDirEntry { + entry, + parent_fd: self.dir_fd, + }) + .map_err(Error::from) }) } } @@ -157,7 +160,6 @@ where Ok(()) } - /// Helper trait to provide a combinators for directory entry iterators. pub trait FileIterOps where @@ -255,7 +257,7 @@ where } // file did not match regex or isn't valid utf-8 continue; - }, + } Err(_) => return Some(item), } } @@ -288,24 +290,34 @@ fn do_lock_dir_noblock( would_block_msg: &str, exclusive: bool, ) -> Result { - let mut handle = Dir::open(path, OFlag::O_RDONLY, Mode::empty()) - .map_err(|err| { - format_err!("unable to open {} directory {:?} for locking - {}", what, path, err) - })?; + let mut handle = Dir::open(path, OFlag::O_RDONLY, Mode::empty()).map_err(|err| { + format_err!( + "unable to open {} directory {:?} for locking - {}", + what, + path, + err + ) + })?; // acquire in non-blocking mode, no point in waiting here since other // backups could still take a very long time - crate::fs::lock_file(&mut handle, exclusive, Some(std::time::Duration::from_nanos(0))) - .map_err(|err| { - format_err!( - "unable to acquire lock on {} directory {:?} - {}", what, path, - if err.would_block() { - String::from(would_block_msg) - } else { - err.to_string() - } - ) - })?; + crate::fs::lock_file( + &mut handle, + exclusive, + Some(std::time::Duration::from_nanos(0)), + ) + .map_err(|err| { + format_err!( + "unable to acquire lock on {} directory {:?} - {}", + what, + path, + if err.would_block() { + String::from(would_block_msg) + } else { + err.to_string() + } + ) + })?; Ok(handle) } diff --git a/proxmox-sys/src/fs/xattr.rs b/proxmox-sys/src/fs/xattr.rs index 163a4119..28be2504 100644 --- a/proxmox-sys/src/fs/xattr.rs +++ b/proxmox-sys/src/fs/xattr.rs @@ -81,23 +81,21 @@ pub fn flistxattr(fd: RawFd) -> Result { // it gets dynamically increased until big enough. let mut size = 256; let mut buffer = vec::undefined(size); - let mut bytes = unsafe { - libc::flistxattr(fd, buffer.as_mut_ptr() as *mut libc::c_char, buffer.len()) - }; + let mut bytes = + unsafe { libc::flistxattr(fd, buffer.as_mut_ptr() as *mut libc::c_char, buffer.len()) }; while bytes < 0 { let err = Errno::last(); match err { Errno::ERANGE => { // Buffer was not big enough to fit the list, retry with double the size size = size.checked_mul(2).ok_or(Errno::ENOMEM)?; - }, + } _ => return Err(err), } // Retry to read the list with new buffer buffer.resize(size, 0); - bytes = unsafe { - libc::flistxattr(fd, buffer.as_mut_ptr() as *mut libc::c_char, buffer.len()) - }; + bytes = + unsafe { libc::flistxattr(fd, buffer.as_mut_ptr() as *mut libc::c_char, buffer.len()) }; } buffer.truncate(bytes as usize); @@ -112,7 +110,12 @@ pub fn fgetxattr(fd: RawFd, name: &CStr) -> Result, nix::errno::Errno> { let mut size = 256; let mut buffer = vec::undefined(size); let mut bytes = unsafe { - libc::fgetxattr(fd, name.as_ptr(), buffer.as_mut_ptr() as *mut core::ffi::c_void, buffer.len()) + libc::fgetxattr( + fd, + name.as_ptr(), + buffer.as_mut_ptr() as *mut core::ffi::c_void, + buffer.len(), + ) }; while bytes < 0 { let err = Errno::last(); @@ -120,12 +123,17 @@ pub fn fgetxattr(fd: RawFd, name: &CStr) -> Result, nix::errno::Errno> { Errno::ERANGE => { // Buffer was not big enough to fit the value, retry with double the size size = size.checked_mul(2).ok_or(Errno::ENOMEM)?; - }, + } _ => return Err(err), } buffer.resize(size, 0); bytes = unsafe { - libc::fgetxattr(fd, name.as_ptr() as *const libc::c_char, buffer.as_mut_ptr() as *mut core::ffi::c_void, buffer.len()) + libc::fgetxattr( + fd, + name.as_ptr() as *const libc::c_char, + buffer.as_mut_ptr() as *mut core::ffi::c_void, + buffer.len(), + ) }; } buffer.resize(bytes as usize, 0); @@ -136,7 +144,13 @@ pub fn fgetxattr(fd: RawFd, name: &CStr) -> Result, nix::errno::Errno> { /// Set an extended attribute on a file descriptor. pub fn fsetxattr(fd: RawFd, name: &CStr, data: &[u8]) -> Result<(), nix::errno::Errno> { let result = unsafe { - libc::fsetxattr(fd, name.as_ptr(), data.as_ptr() as *const libc::c_void, data.len(), 0) + libc::fsetxattr( + fd, + name.as_ptr(), + data.as_ptr() as *const libc::c_void, + data.len(), + 0, + ) }; if result < 0 { return Err(Errno::last()); @@ -156,7 +170,7 @@ pub fn is_security_capability(name: &CStr) -> bool { pub fn is_acl(name: &CStr) -> bool { name.to_bytes() == xattr_acl_access().to_bytes() - || name.to_bytes() == xattr_acl_default().to_bytes() + || name.to_bytes() == xattr_acl_default().to_bytes() } /// Check if the passed name buffer starts with a valid xattr namespace prefix @@ -203,7 +217,10 @@ mod tests { assert!(fsetxattr(fd, c_str!("user.empty"), b"").is_ok()); if nix::unistd::Uid::current() != nix::unistd::ROOT { - assert_eq!(fsetxattr(fd, c_str!("trusted.attribute0"), b"value0"), Err(Errno::EPERM)); + assert_eq!( + fsetxattr(fd, c_str!("trusted.attribute0"), b"value0"), + Err(Errno::EPERM) + ); } let v0 = fgetxattr(fd, c_str!("user.attribute0")).unwrap(); @@ -211,7 +228,10 @@ mod tests { assert_eq!(v0, b"value0".as_ref()); assert_eq!(v1, b"".as_ref()); - assert_eq!(fgetxattr(fd, c_str!("user.attribute1")), Err(Errno::ENODATA)); + assert_eq!( + fgetxattr(fd, c_str!("user.attribute1")), + Err(Errno::ENODATA) + ); std::fs::remove_file(&path).unwrap(); }