forked from proxmox-mirrors/proxmox
sys: rust fmt
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
parent
3a3dd296cc
commit
0a651e00a9
@ -1,6 +1,6 @@
|
|||||||
//! Helpers to run a [Command] and check status code
|
//! 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};
|
use anyhow::{bail, format_err, Error};
|
||||||
|
|
||||||
|
@ -56,7 +56,6 @@ pub fn crypt(password: &[u8], salt: &[u8]) -> Result<String, Error> {
|
|||||||
|
|
||||||
/// Encrypt a pasword using sha256 hashing method
|
/// Encrypt a pasword using sha256 hashing method
|
||||||
pub fn encrypt_pw(password: &str) -> Result<String, Error> {
|
pub fn encrypt_pw(password: &str) -> Result<String, Error> {
|
||||||
|
|
||||||
let salt = crate::linux::random_data(8)?;
|
let salt = crate::linux::random_data(8)?;
|
||||||
let salt = format!("$5${}$", base64::encode_config(&salt, base64::CRYPT));
|
let salt = format!("$5${}$", base64::encode_config(&salt, base64::CRYPT));
|
||||||
|
|
||||||
|
@ -52,4 +52,3 @@ impl std::ops::Deref for RawFdNum {
|
|||||||
self.as_fd_ref()
|
self.as_fd_ref()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,7 +76,10 @@ impl Drop for ACL {
|
|||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
let ret = unsafe { acl_free(self.ptr) };
|
let ret = unsafe { acl_free(self.ptr) };
|
||||||
if ret != 0 {
|
if ret != 0 {
|
||||||
panic!("invalid pointer encountered while dropping ACL - {}", Errno::last());
|
panic!(
|
||||||
|
"invalid pointer encountered while dropping ACL - {}",
|
||||||
|
Errno::last()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -146,9 +149,12 @@ impl ACL {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_entry_full(&mut self, tag: ACLTag, qualifier: Option<u64>, permissions: u64)
|
pub fn add_entry_full(
|
||||||
-> Result<(), nix::errno::Errno>
|
&mut self,
|
||||||
{
|
tag: ACLTag,
|
||||||
|
qualifier: Option<u64>,
|
||||||
|
permissions: u64,
|
||||||
|
) -> Result<(), nix::errno::Errno> {
|
||||||
let mut entry = self.create_entry()?;
|
let mut entry = self.create_entry()?;
|
||||||
entry.set_tag_type(tag)?;
|
entry.set_tag_type(tag)?;
|
||||||
if let Some(qualifier) = qualifier {
|
if let Some(qualifier) = qualifier {
|
||||||
@ -240,7 +246,10 @@ impl<'a> ACLEntry<'a> {
|
|||||||
let result = unsafe { *(qualifier as *const u32) as u64 };
|
let result = unsafe { *(qualifier as *const u32) as u64 };
|
||||||
let ret = unsafe { acl_free(qualifier) };
|
let ret = unsafe { acl_free(qualifier) };
|
||||||
if ret != 0 {
|
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)
|
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) };
|
let res = unsafe { acl_get_entry(self.acl.ptr, self.current, &mut entry_ptr) };
|
||||||
self.current = ACL_NEXT_ENTRY;
|
self.current = ACL_NEXT_ENTRY;
|
||||||
if res == 1 {
|
if res == 1 {
|
||||||
return Some(ACLEntry { ptr: entry_ptr, _phantom: PhantomData });
|
return Some(ACLEntry {
|
||||||
|
ptr: entry_ptr,
|
||||||
|
_phantom: PhantomData,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
@ -312,10 +324,15 @@ impl ACLXAttrBuffer {
|
|||||||
/// Add ACL entry to buffer.
|
/// Add ACL entry to buffer.
|
||||||
pub fn add_entry(&mut self, tag: ACLTag, qualifier: Option<u64>, permissions: u64) {
|
pub fn add_entry(&mut self, tag: ACLTag, qualifier: Option<u64>, permissions: u64) {
|
||||||
self.buffer.extend_from_slice(&(tag as u16).to_le_bytes());
|
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 {
|
match qualifier {
|
||||||
Some(qualifier) => self.buffer.extend_from_slice(&(qualifier as u32).to_le_bytes()),
|
Some(qualifier) => self
|
||||||
None => self.buffer.extend_from_slice(&ACL_UNDEFINED_ID.to_le_bytes()),
|
.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
|
/// 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.
|
/// Borrow raw buffer as mut slice.
|
||||||
pub fn as_mut_slice(&mut self) -> &mut [u8] {
|
pub fn as_mut_slice(&mut self) -> &mut [u8] {
|
||||||
|
@ -316,7 +316,6 @@ pub fn atomic_open_or_create_file<P: AsRef<Path>>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// /usr/include/linux/fs.h: #define BLKGETSIZE64 _IOR(0x12,114,size_t)
|
// /usr/include/linux/fs.h: #define BLKGETSIZE64 _IOR(0x12,114,size_t)
|
||||||
// return device size in bytes (u64 *arg)
|
// return device size in bytes (u64 *arg)
|
||||||
nix::ioctl_read!(blkgetsize64, 0x12, 114, u64);
|
nix::ioctl_read!(blkgetsize64, 0x12, 114, u64);
|
||||||
|
@ -4,9 +4,9 @@ use std::path::Path;
|
|||||||
|
|
||||||
use anyhow::{bail, Error};
|
use anyhow::{bail, Error};
|
||||||
|
|
||||||
use std::os::unix::io::{AsRawFd, RawFd};
|
|
||||||
use nix::unistd::{Gid, Uid};
|
|
||||||
use nix::sys::stat;
|
use nix::sys::stat;
|
||||||
|
use nix::unistd::{Gid, Uid};
|
||||||
|
use std::os::unix::io::{AsRawFd, RawFd};
|
||||||
|
|
||||||
pub mod acl;
|
pub mod acl;
|
||||||
|
|
||||||
@ -68,11 +68,9 @@ impl CreateOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn apply_to(&self, file: &mut File, path: &Path) -> Result<(), Error> {
|
pub fn apply_to(&self, file: &mut File, path: &Path) -> Result<(), Error> {
|
||||||
|
|
||||||
// clippy bug?: from_bits_truncate is actually a const fn...
|
// clippy bug?: from_bits_truncate is actually a const fn...
|
||||||
#[allow(clippy::or_fun_call)]
|
#[allow(clippy::or_fun_call)]
|
||||||
let mode: stat::Mode = self.perm
|
let mode: stat::Mode = self.perm.unwrap_or(stat::Mode::from_bits_truncate(0o644));
|
||||||
.unwrap_or(stat::Mode::from_bits_truncate(0o644));
|
|
||||||
|
|
||||||
if let Err(err) = stat::fchmod(file.as_raw_fd(), mode) {
|
if let Err(err) = stat::fchmod(file.as_raw_fd(), mode) {
|
||||||
bail!("fchmod {:?} failed: {}", path, err);
|
bail!("fchmod {:?} failed: {}", path, err);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
//! Linux specific helpers and syscall wrapper
|
//! Linux specific helpers and syscall wrapper
|
||||||
|
|
||||||
use anyhow::{bail,Error};
|
use anyhow::{bail, Error};
|
||||||
|
|
||||||
use proxmox_io::vec;
|
use proxmox_io::vec;
|
||||||
|
|
||||||
|
@ -14,8 +14,8 @@ use nix::NixPath;
|
|||||||
use proxmox_lang::{c_str, error::io_err_other};
|
use proxmox_lang::{c_str, error::io_err_other};
|
||||||
|
|
||||||
use crate::error::SysResult;
|
use crate::error::SysResult;
|
||||||
use crate::linux::procfs::{MountInfo, PidStat};
|
|
||||||
use crate::fd::Fd;
|
use crate::fd::Fd;
|
||||||
|
use crate::linux::procfs::{MountInfo, PidStat};
|
||||||
use crate::{c_result, c_try};
|
use crate::{c_result, c_try};
|
||||||
|
|
||||||
/// asm-generic pidfd_open syscall number
|
/// asm-generic pidfd_open syscall number
|
||||||
|
@ -311,10 +311,7 @@ fn test_entry() {
|
|||||||
}]
|
}]
|
||||||
);
|
);
|
||||||
assert_eq!(entry.fs_type, "cgroup");
|
assert_eq!(entry.fs_type, "cgroup");
|
||||||
assert_eq!(
|
assert_eq!(entry.mount_source.as_deref(), Some(OsStr::new("cgroup")));
|
||||||
entry.mount_source.as_deref(),
|
|
||||||
Some(OsStr::new("cgroup"))
|
|
||||||
);
|
|
||||||
assert_eq!(entry.super_options, "rw,blkio");
|
assert_eq!(entry.super_options, "rw,blkio");
|
||||||
|
|
||||||
let l2 = b"49 28 0:44 / /proxmox/debian rw,relatime shared:27 - autofs systemd-1 \
|
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.fs_type, "autofs");
|
||||||
assert_eq!(
|
assert_eq!(entry.mount_source.as_deref(), Some(OsStr::new("systemd-1")));
|
||||||
entry.mount_source.as_deref(),
|
|
||||||
Some(OsStr::new("systemd-1"))
|
|
||||||
);
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
entry.super_options,
|
entry.super_options,
|
||||||
"rw,fd=26,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=27726"
|
"rw,fd=26,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=27726"
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use std::os::unix::io::RawFd;
|
use std::os::unix::io::RawFd;
|
||||||
|
|
||||||
use nix::sys::socket::sockopt::{KeepAlive, TcpKeepIdle};
|
|
||||||
use nix::sys::socket::setsockopt;
|
use nix::sys::socket::setsockopt;
|
||||||
|
use nix::sys::socket::sockopt::{KeepAlive, TcpKeepIdle};
|
||||||
|
|
||||||
/// Set TCP keepalive time on a socket
|
/// 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
|
/// The default on Linux is 7200 (2 hours) which is far too long for
|
||||||
/// many of our use cases.
|
/// many of our use cases.
|
||||||
pub fn set_tcp_keepalive(
|
pub fn set_tcp_keepalive(socket_fd: RawFd, tcp_keepalive_time: u32) -> nix::Result<()> {
|
||||||
socket_fd: RawFd,
|
|
||||||
tcp_keepalive_time: u32,
|
|
||||||
) -> nix::Result<()> {
|
|
||||||
|
|
||||||
setsockopt(socket_fd, KeepAlive, &true)?;
|
setsockopt(socket_fd, KeepAlive, &true)?;
|
||||||
setsockopt(socket_fd, TcpKeepIdle, &tcp_keepalive_time)?;
|
setsockopt(socket_fd, TcpKeepIdle, &tcp_keepalive_time)?;
|
||||||
|
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
//! Log rotation helper
|
//! Log rotation helper
|
||||||
|
|
||||||
use std::path::{Path, PathBuf};
|
use std::fs::{rename, File};
|
||||||
use std::fs::{File, rename};
|
|
||||||
use std::os::unix::io::{FromRawFd, IntoRawFd};
|
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
use std::os::unix::io::{FromRawFd, IntoRawFd};
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use anyhow::{bail, Error};
|
use anyhow::{bail, Error};
|
||||||
use nix::unistd;
|
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
|
/// Used for rotating log files and iterating over them
|
||||||
pub struct LogRotate {
|
pub struct LogRotate {
|
||||||
@ -47,7 +47,7 @@ impl LogRotate {
|
|||||||
LogRotateFileNames {
|
LogRotateFileNames {
|
||||||
base_path: self.base_path.clone(),
|
base_path: self.base_path.clone(),
|
||||||
count: 0,
|
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 mut source = File::open(source_path)?;
|
||||||
let (fd, tmp_path) = make_tmp_file(target_path, options.clone())?;
|
let (fd, tmp_path) = make_tmp_file(target_path, options.clone())?;
|
||||||
let target = unsafe { File::from_raw_fd(fd.into_raw_fd()) };
|
let target = unsafe { File::from_raw_fd(fd.into_raw_fd()) };
|
||||||
@ -114,14 +118,14 @@ impl LogRotate {
|
|||||||
|
|
||||||
filenames.push(PathBuf::from(next_filename));
|
filenames.push(PathBuf::from(next_filename));
|
||||||
|
|
||||||
for i in (0..count-1).rev() {
|
for i in (0..count - 1).rev() {
|
||||||
if self.compress
|
if self.compress
|
||||||
&& filenames[i].extension() != Some(std::ffi::OsStr::new("zst"))
|
&& 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 {
|
} 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'
|
/// Conditional rotate if file bigger than 'max_size'
|
||||||
pub fn rotate(
|
pub fn rotate(&mut self, max_size: u64) -> Result<bool, Error> {
|
||||||
&mut self,
|
|
||||||
max_size: u64,
|
|
||||||
) -> Result<bool, Error> {
|
|
||||||
|
|
||||||
let metadata = match self.base_path.metadata() {
|
let metadata = match self.base_path.metadata() {
|
||||||
Ok(metadata) => metadata,
|
Ok(metadata) => metadata,
|
||||||
Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(false),
|
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 {
|
if metadata.len() > max_size {
|
||||||
|
@ -4,7 +4,6 @@ use anyhow::{bail, Error};
|
|||||||
///
|
///
|
||||||
/// A worker task is a long running task, which usually logs output into a separate file.
|
/// A worker task is a long running task, which usually logs output into a separate file.
|
||||||
pub trait WorkerTaskContext: Send + Sync {
|
pub trait WorkerTaskContext: Send + Sync {
|
||||||
|
|
||||||
/// Test if there was a request to abort the task.
|
/// Test if there was a request to abort the task.
|
||||||
fn abort_requested(&self) -> bool;
|
fn abort_requested(&self) -> bool;
|
||||||
|
|
||||||
@ -19,7 +18,6 @@ pub trait WorkerTaskContext: Send + Sync {
|
|||||||
/// Test if there was a request to shutdown the server.
|
/// Test if there was a request to shutdown the server.
|
||||||
fn shutdown_requested(&self) -> bool;
|
fn shutdown_requested(&self) -> bool;
|
||||||
|
|
||||||
|
|
||||||
/// This should fail with a reasonable error message if there was
|
/// This should fail with a reasonable error message if there was
|
||||||
/// a request to shutdown the server.
|
/// a request to shutdown the server.
|
||||||
fn fail_on_shutdown(&self) -> Result<(), Error> {
|
fn fail_on_shutdown(&self) -> Result<(), Error> {
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
use std::path::PathBuf;
|
|
||||||
use std::fs::OpenOptions;
|
use std::fs::OpenOptions;
|
||||||
use std::os::unix::io::AsRawFd;
|
use std::os::unix::io::AsRawFd;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use nix::errno::Errno;
|
use nix::errno::Errno;
|
||||||
|
|
||||||
use proxmox_lang::c_str;
|
use proxmox_lang::c_str;
|
||||||
use proxmox_sys::fs::xattr::{fgetxattr,fsetxattr};
|
use proxmox_sys::fs::xattr::{fgetxattr, fsetxattr};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_fsetxattr_fgetxattr() {
|
fn test_fsetxattr_fgetxattr() {
|
||||||
@ -18,7 +18,6 @@ fn test_fsetxattr_fgetxattr() {
|
|||||||
.open(&path)
|
.open(&path)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
|
||||||
let fd = file.as_raw_fd();
|
let fd = file.as_raw_fd();
|
||||||
|
|
||||||
if let Err(Errno::EOPNOTSUPP) = fsetxattr(fd, c_str!("user.attribute0"), b"value0") {
|
if let Err(Errno::EOPNOTSUPP) = fsetxattr(fd, c_str!("user.attribute0"), b"value0") {
|
||||||
|
Loading…
Reference in New Issue
Block a user