//! 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};
/// Encrypt a pasword using sha256 hashing method
pub fn encrypt_pw(password: &str) -> Result<String, Error> {
-
let salt = crate::linux::random_data(8)?;
let salt = format!("$5${}$", base64::encode_config(&salt, base64::CRYPT));
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()
+ );
}
}
}
if ptr.is_null() {
return Err(Errno::last());
}
-
+
Ok(ACL { ptr })
}
}
}
- pub fn add_entry_full(&mut self, tag: ACLTag, qualifier: Option<u64>, permissions: u64)
- -> Result<(), nix::errno::Errno>
- {
+ pub fn add_entry_full(
+ &mut self,
+ tag: ACLTag,
+ qualifier: Option<u64>,
+ permissions: u64,
+ ) -> Result<(), nix::errno::Errno> {
let mut entry = self.create_entry()?;
entry.set_tag_type(tag)?;
if let Some(qualifier) = qualifier {
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)
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
/// Add ACL entry to buffer.
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(&(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()),
}
}
}
/// 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] {
}
}
-
// /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);
-//! 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;
}
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);
//! Linux specific helpers and syscall wrapper
-use anyhow::{bail,Error};
+use anyhow::{bail, Error};
use proxmox_io::vec;
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
}]
);
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 \
}]
);
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"
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
///
///
/// 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)?;
//! 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 {
LogRotateFileNames {
base_path: self.base_path.clone(),
count: 0,
- compress: self.compress
+ compress: self.compress,
}
}
}
}
- 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()) };
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])?;
}
}
}
/// Conditional rotate if file bigger than 'max_size'
- pub fn rotate(
- &mut self,
- max_size: u64,
- ) -> Result<bool, Error> {
-
+ pub fn rotate(&mut self, max_size: u64) -> Result<bool, Error> {
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 {
///
/// 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;
/// 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> {
-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() {
.open(&path)
.unwrap();
-
let fd = file.as_raw_fd();
if let Err(Errno::EOPNOTSUPP) = fsetxattr(fd, c_str!("user.attribute0"), b"value0") {
);
std::fs::remove_file(&path).unwrap();
-}
\ No newline at end of file
+}