From 0a651e00a9edf74c78c3d9fd4e0b637b6e154412 Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Sun, 10 Apr 2022 17:22:39 +0200 Subject: [PATCH] sys: rust fmt Signed-off-by: Thomas Lamprecht --- proxmox-sys/src/command.rs | 2 +- proxmox-sys/src/crypt.rs | 1 - proxmox-sys/src/fd/raw_fd_num.rs | 1 - proxmox-sys/src/fs/acl.rs | 41 +++++++++++++++++------ proxmox-sys/src/fs/file.rs | 1 - proxmox-sys/src/fs/mod.rs | 10 +++--- proxmox-sys/src/linux/mod.rs | 2 +- proxmox-sys/src/linux/pid.rs | 2 +- proxmox-sys/src/linux/procfs/mountinfo.rs | 10 ++---- proxmox-sys/src/linux/socket.rs | 8 ++--- proxmox-sys/src/logrotate.rs | 32 +++++++++--------- proxmox-sys/src/worker_task_context.rs | 2 -- proxmox-sys/tests/xattr.rs | 7 ++-- 13 files changed, 60 insertions(+), 59 deletions(-) diff --git a/proxmox-sys/src/command.rs b/proxmox-sys/src/command.rs index b99466da..bc2ff249 100644 --- a/proxmox-sys/src/command.rs +++ b/proxmox-sys/src/command.rs @@ -1,6 +1,6 @@ //! Helpers to run a [Command] and check status code -use std::process::{Output, Command}; +use std::process::{Command, Output}; use anyhow::{bail, format_err, Error}; diff --git a/proxmox-sys/src/crypt.rs b/proxmox-sys/src/crypt.rs index 28e21aee..ab426852 100644 --- a/proxmox-sys/src/crypt.rs +++ b/proxmox-sys/src/crypt.rs @@ -56,7 +56,6 @@ pub fn crypt(password: &[u8], salt: &[u8]) -> Result { /// Encrypt a pasword using sha256 hashing method pub fn encrypt_pw(password: &str) -> Result { - let salt = crate::linux::random_data(8)?; let salt = format!("$5${}$", base64::encode_config(&salt, base64::CRYPT)); diff --git a/proxmox-sys/src/fd/raw_fd_num.rs b/proxmox-sys/src/fd/raw_fd_num.rs index 7356c05f..1b83c6d6 100644 --- a/proxmox-sys/src/fd/raw_fd_num.rs +++ b/proxmox-sys/src/fd/raw_fd_num.rs @@ -52,4 +52,3 @@ impl std::ops::Deref for RawFdNum { self.as_fd_ref() } } - diff --git a/proxmox-sys/src/fs/acl.rs b/proxmox-sys/src/fs/acl.rs index 80e27812..6cf00fe6 100644 --- a/proxmox-sys/src/fs/acl.rs +++ b/proxmox-sys/src/fs/acl.rs @@ -76,7 +76,10 @@ impl Drop for ACL { fn drop(&mut self) { let ret = unsafe { acl_free(self.ptr) }; if ret != 0 { - panic!("invalid pointer encountered while dropping ACL - {}", Errno::last()); + panic!( + "invalid pointer encountered while dropping ACL - {}", + Errno::last() + ); } } } @@ -97,7 +100,7 @@ impl ACL { if ptr.is_null() { return Err(Errno::last()); } - + Ok(ACL { ptr }) } @@ -146,9 +149,12 @@ impl ACL { } } - pub fn add_entry_full(&mut self, tag: ACLTag, qualifier: Option, permissions: u64) - -> Result<(), nix::errno::Errno> - { + pub fn add_entry_full( + &mut self, + tag: ACLTag, + qualifier: Option, + permissions: u64, + ) -> Result<(), nix::errno::Errno> { let mut entry = self.create_entry()?; entry.set_tag_type(tag)?; if let Some(qualifier) = qualifier { @@ -240,7 +246,10 @@ impl<'a> ACLEntry<'a> { let result = unsafe { *(qualifier as *const u32) as u64 }; let ret = unsafe { acl_free(qualifier) }; if ret != 0 { - panic!("invalid pointer encountered while dropping ACL qualifier - {}", Errno::last()); + panic!( + "invalid pointer encountered while dropping ACL qualifier - {}", + Errno::last() + ); } Ok(result) @@ -272,7 +281,10 @@ impl<'a> Iterator for &'a mut ACLEntriesIterator { let res = unsafe { acl_get_entry(self.acl.ptr, self.current, &mut entry_ptr) }; self.current = ACL_NEXT_ENTRY; if res == 1 { - return Some(ACLEntry { ptr: entry_ptr, _phantom: PhantomData }); + return Some(ACLEntry { + ptr: entry_ptr, + _phantom: PhantomData, + }); } None @@ -312,10 +324,15 @@ impl ACLXAttrBuffer { /// Add ACL entry to buffer. pub fn add_entry(&mut self, tag: ACLTag, qualifier: Option, permissions: u64) { self.buffer.extend_from_slice(&(tag as u16).to_le_bytes()); - self.buffer.extend_from_slice(&(permissions as u16).to_le_bytes()); + self.buffer + .extend_from_slice(&(permissions as u16).to_le_bytes()); match qualifier { - Some(qualifier) => self.buffer.extend_from_slice(&(qualifier as u32).to_le_bytes()), - None => self.buffer.extend_from_slice(&ACL_UNDEFINED_ID.to_le_bytes()), + Some(qualifier) => self + .buffer + .extend_from_slice(&(qualifier as u32).to_le_bytes()), + None => self + .buffer + .extend_from_slice(&ACL_UNDEFINED_ID.to_le_bytes()), } } @@ -325,7 +342,9 @@ impl ACLXAttrBuffer { } /// The buffer always contains at least the version, it is never empty - pub const fn is_empty(&self) -> bool { false } + pub const fn is_empty(&self) -> bool { + false + } /// Borrow raw buffer as mut slice. pub fn as_mut_slice(&mut self) -> &mut [u8] { diff --git a/proxmox-sys/src/fs/file.rs b/proxmox-sys/src/fs/file.rs index 180a35f2..a096db43 100644 --- a/proxmox-sys/src/fs/file.rs +++ b/proxmox-sys/src/fs/file.rs @@ -316,7 +316,6 @@ pub fn atomic_open_or_create_file>( } } - // /usr/include/linux/fs.h: #define BLKGETSIZE64 _IOR(0x12,114,size_t) // return device size in bytes (u64 *arg) nix::ioctl_read!(blkgetsize64, 0x12, 114, u64); diff --git a/proxmox-sys/src/fs/mod.rs b/proxmox-sys/src/fs/mod.rs index ee4ef423..4aa06b20 100644 --- a/proxmox-sys/src/fs/mod.rs +++ b/proxmox-sys/src/fs/mod.rs @@ -1,12 +1,12 @@ -//! File system related utilities +//! File system related utilities use std::fs::File; use std::path::Path; use anyhow::{bail, Error}; -use std::os::unix::io::{AsRawFd, RawFd}; -use nix::unistd::{Gid, Uid}; use nix::sys::stat; +use nix::unistd::{Gid, Uid}; +use std::os::unix::io::{AsRawFd, RawFd}; pub mod acl; @@ -68,11 +68,9 @@ impl CreateOptions { } pub fn apply_to(&self, file: &mut File, path: &Path) -> Result<(), Error> { - // clippy bug?: from_bits_truncate is actually a const fn... #[allow(clippy::or_fun_call)] - let mode: stat::Mode = self.perm - .unwrap_or(stat::Mode::from_bits_truncate(0o644)); + let mode: stat::Mode = self.perm.unwrap_or(stat::Mode::from_bits_truncate(0o644)); if let Err(err) = stat::fchmod(file.as_raw_fd(), mode) { bail!("fchmod {:?} failed: {}", path, err); diff --git a/proxmox-sys/src/linux/mod.rs b/proxmox-sys/src/linux/mod.rs index 7477a4e6..fce3b8b4 100644 --- a/proxmox-sys/src/linux/mod.rs +++ b/proxmox-sys/src/linux/mod.rs @@ -1,6 +1,6 @@ //! Linux specific helpers and syscall wrapper -use anyhow::{bail,Error}; +use anyhow::{bail, Error}; use proxmox_io::vec; diff --git a/proxmox-sys/src/linux/pid.rs b/proxmox-sys/src/linux/pid.rs index 07d71632..9d708243 100644 --- a/proxmox-sys/src/linux/pid.rs +++ b/proxmox-sys/src/linux/pid.rs @@ -14,8 +14,8 @@ use nix::NixPath; use proxmox_lang::{c_str, error::io_err_other}; use crate::error::SysResult; -use crate::linux::procfs::{MountInfo, PidStat}; use crate::fd::Fd; +use crate::linux::procfs::{MountInfo, PidStat}; use crate::{c_result, c_try}; /// asm-generic pidfd_open syscall number diff --git a/proxmox-sys/src/linux/procfs/mountinfo.rs b/proxmox-sys/src/linux/procfs/mountinfo.rs index 2848425c..ab449c6a 100644 --- a/proxmox-sys/src/linux/procfs/mountinfo.rs +++ b/proxmox-sys/src/linux/procfs/mountinfo.rs @@ -311,10 +311,7 @@ fn test_entry() { }] ); assert_eq!(entry.fs_type, "cgroup"); - assert_eq!( - entry.mount_source.as_deref(), - Some(OsStr::new("cgroup")) - ); + assert_eq!(entry.mount_source.as_deref(), Some(OsStr::new("cgroup"))); assert_eq!(entry.super_options, "rw,blkio"); let l2 = b"49 28 0:44 / /proxmox/debian rw,relatime shared:27 - autofs systemd-1 \ @@ -340,10 +337,7 @@ fn test_entry() { }] ); assert_eq!(entry.fs_type, "autofs"); - assert_eq!( - entry.mount_source.as_deref(), - Some(OsStr::new("systemd-1")) - ); + assert_eq!(entry.mount_source.as_deref(), Some(OsStr::new("systemd-1"))); assert_eq!( entry.super_options, "rw,fd=26,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=27726" diff --git a/proxmox-sys/src/linux/socket.rs b/proxmox-sys/src/linux/socket.rs index 6c63c3c2..a9a575cb 100644 --- a/proxmox-sys/src/linux/socket.rs +++ b/proxmox-sys/src/linux/socket.rs @@ -1,7 +1,7 @@ use std::os::unix::io::RawFd; -use nix::sys::socket::sockopt::{KeepAlive, TcpKeepIdle}; use nix::sys::socket::setsockopt; +use nix::sys::socket::sockopt::{KeepAlive, TcpKeepIdle}; /// Set TCP keepalive time on a socket /// @@ -9,11 +9,7 @@ use nix::sys::socket::setsockopt; /// /// The default on Linux is 7200 (2 hours) which is far too long for /// many of our use cases. -pub fn set_tcp_keepalive( - socket_fd: RawFd, - tcp_keepalive_time: u32, -) -> nix::Result<()> { - +pub fn set_tcp_keepalive(socket_fd: RawFd, tcp_keepalive_time: u32) -> nix::Result<()> { setsockopt(socket_fd, KeepAlive, &true)?; setsockopt(socket_fd, TcpKeepIdle, &tcp_keepalive_time)?; diff --git a/proxmox-sys/src/logrotate.rs b/proxmox-sys/src/logrotate.rs index 6b82970b..704a18ce 100644 --- a/proxmox-sys/src/logrotate.rs +++ b/proxmox-sys/src/logrotate.rs @@ -1,14 +1,14 @@ //! Log rotation helper -use std::path::{Path, PathBuf}; -use std::fs::{File, rename}; -use std::os::unix::io::{FromRawFd, IntoRawFd}; +use std::fs::{rename, File}; use std::io::Read; +use std::os::unix::io::{FromRawFd, IntoRawFd}; +use std::path::{Path, PathBuf}; use anyhow::{bail, Error}; use nix::unistd; -use crate::fs::{CreateOptions, make_tmp_file}; +use crate::fs::{make_tmp_file, CreateOptions}; /// Used for rotating log files and iterating over them pub struct LogRotate { @@ -47,7 +47,7 @@ impl LogRotate { LogRotateFileNames { base_path: self.base_path.clone(), count: 0, - compress: self.compress + compress: self.compress, } } @@ -58,7 +58,11 @@ impl LogRotate { } } - fn compress(source_path: &Path, target_path: &Path, options: &CreateOptions) -> Result<(), Error> { + fn compress( + source_path: &Path, + target_path: &Path, + options: &CreateOptions, + ) -> Result<(), Error> { let mut source = File::open(source_path)?; let (fd, tmp_path) = make_tmp_file(target_path, options.clone())?; let target = unsafe { File::from_raw_fd(fd.into_raw_fd()) }; @@ -114,14 +118,14 @@ impl LogRotate { filenames.push(PathBuf::from(next_filename)); - for i in (0..count-1).rev() { + for i in (0..count - 1).rev() { if self.compress && filenames[i].extension() != Some(std::ffi::OsStr::new("zst")) - && filenames[i+1].extension() == Some(std::ffi::OsStr::new("zst")) + && filenames[i + 1].extension() == Some(std::ffi::OsStr::new("zst")) { - Self::compress(&filenames[i], &filenames[i+1], &self.options)?; + Self::compress(&filenames[i], &filenames[i + 1], &self.options)?; } else { - rename(&filenames[i], &filenames[i+1])?; + rename(&filenames[i], &filenames[i + 1])?; } } @@ -137,15 +141,11 @@ impl LogRotate { } /// Conditional rotate if file bigger than 'max_size' - pub fn rotate( - &mut self, - max_size: u64, - ) -> Result { - + pub fn rotate(&mut self, max_size: u64) -> Result { let metadata = match self.base_path.metadata() { Ok(metadata) => metadata, Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(false), - Err(err) => bail!("unable to open {:?} - {}", self.base_path, err), + Err(err) => bail!("unable to open {:?} - {}", self.base_path, err), }; if metadata.len() > max_size { diff --git a/proxmox-sys/src/worker_task_context.rs b/proxmox-sys/src/worker_task_context.rs index eeb4d797..89d57560 100644 --- a/proxmox-sys/src/worker_task_context.rs +++ b/proxmox-sys/src/worker_task_context.rs @@ -4,7 +4,6 @@ use anyhow::{bail, Error}; /// /// A worker task is a long running task, which usually logs output into a separate file. pub trait WorkerTaskContext: Send + Sync { - /// Test if there was a request to abort the task. fn abort_requested(&self) -> bool; @@ -19,7 +18,6 @@ pub trait WorkerTaskContext: Send + Sync { /// Test if there was a request to shutdown the server. fn shutdown_requested(&self) -> bool; - /// This should fail with a reasonable error message if there was /// a request to shutdown the server. fn fail_on_shutdown(&self) -> Result<(), Error> { diff --git a/proxmox-sys/tests/xattr.rs b/proxmox-sys/tests/xattr.rs index 55ce81ac..43ae523d 100644 --- a/proxmox-sys/tests/xattr.rs +++ b/proxmox-sys/tests/xattr.rs @@ -1,11 +1,11 @@ -use std::path::PathBuf; use std::fs::OpenOptions; use std::os::unix::io::AsRawFd; +use std::path::PathBuf; use nix::errno::Errno; use proxmox_lang::c_str; -use proxmox_sys::fs::xattr::{fgetxattr,fsetxattr}; +use proxmox_sys::fs::xattr::{fgetxattr, fsetxattr}; #[test] fn test_fsetxattr_fgetxattr() { @@ -18,7 +18,6 @@ fn test_fsetxattr_fgetxattr() { .open(&path) .unwrap(); - let fd = file.as_raw_fd(); if let Err(Errno::EOPNOTSUPP) = fsetxattr(fd, c_str!("user.attribute0"), b"value0") { @@ -46,4 +45,4 @@ fn test_fsetxattr_fgetxattr() { ); std::fs::remove_file(&path).unwrap(); -} \ No newline at end of file +}