forked from proxmox-mirrors/proxmox
sys: use anyhow Error type for create_dir, and improve error messages
Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
This commit is contained in:
parent
86898b9a59
commit
b25edb67de
@ -23,8 +23,8 @@ pub fn init<P: AsRef<Path>>(acme_config_dir: P, create_subdirs: bool) -> Result<
|
|||||||
}
|
}
|
||||||
|
|
||||||
if create_subdirs {
|
if create_subdirs {
|
||||||
create_secret_dir(self::acme_config_dir())?;
|
create_secret_dir(self::acme_config_dir(), false)?;
|
||||||
create_secret_dir(acme_account_dir())?;
|
create_secret_dir(acme_account_dir(), false)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -3,7 +3,6 @@ use std::path::Path;
|
|||||||
use anyhow::Error;
|
use anyhow::Error;
|
||||||
use nix::sys::stat::Mode;
|
use nix::sys::stat::Mode;
|
||||||
|
|
||||||
use proxmox_sys::error::SysError;
|
|
||||||
use proxmox_sys::fs::CreateOptions;
|
use proxmox_sys::fs::CreateOptions;
|
||||||
|
|
||||||
use super::{get_api_user, get_priv_user};
|
use super::{get_api_user, get_priv_user};
|
||||||
@ -91,14 +90,11 @@ pub fn replace_secret_config<P: AsRef<Path>>(path: P, data: &[u8]) -> Result<(),
|
|||||||
|
|
||||||
/// Creates a directory owned by `priv_user.uid:priv_user.gid` with permission `0700`.
|
/// Creates a directory owned by `priv_user.uid:priv_user.gid` with permission `0700`.
|
||||||
///
|
///
|
||||||
/// Simply returns Ok if the directory already exists.
|
/// Simply returns Ok if the directory already exists. Directory permissions are verified
|
||||||
pub fn create_secret_dir<P: AsRef<Path>>(dir: P) -> Result<(), Error> {
|
/// and raise an error if enforce_permissions is set.
|
||||||
|
pub fn create_secret_dir<P: AsRef<Path>>(dir: P, enforce_permissions: bool) -> Result<(), Error> {
|
||||||
let options = secret_create_options().perm(Mode::from_bits_truncate(0o700));
|
let options = secret_create_options().perm(Mode::from_bits_truncate(0o700));
|
||||||
match proxmox_sys::fs::create_dir(dir, options) {
|
proxmox_sys::fs::ensure_dir_exists(dir, &options, enforce_permissions)
|
||||||
Ok(()) => Ok(()),
|
|
||||||
Err(err) if err.already_exists() => Ok(()),
|
|
||||||
Err(err) => Err(err.into()),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Atomically write data to file owned by `root:root` with permission `0644`.
|
/// Atomically write data to file owned by `root:root` with permission `0644`.
|
||||||
|
@ -14,7 +14,7 @@ use crate::fs::{fchown, CreateOptions};
|
|||||||
/// Creates directory at the provided path with specified ownership.
|
/// Creates directory at the provided path with specified ownership.
|
||||||
///
|
///
|
||||||
/// Errors if the directory already exists.
|
/// Errors if the directory already exists.
|
||||||
pub fn create_dir<P: AsRef<Path>>(path: P, options: CreateOptions) -> Result<(), nix::Error> {
|
pub fn create_dir<P: AsRef<Path>>(path: P, options: CreateOptions) -> 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 = options
|
let mode: stat::Mode = options
|
||||||
@ -22,8 +22,12 @@ pub fn create_dir<P: AsRef<Path>>(path: P, options: CreateOptions) -> Result<(),
|
|||||||
.unwrap_or(stat::Mode::from_bits_truncate(0o750));
|
.unwrap_or(stat::Mode::from_bits_truncate(0o750));
|
||||||
|
|
||||||
let path = path.as_ref();
|
let path = path.as_ref();
|
||||||
nix::unistd::mkdir(path, mode)?;
|
|
||||||
unistd::chown(path, options.owner, options.group)?;
|
nix::unistd::mkdir(path, mode)
|
||||||
|
.map_err(|err| format_err!("unable to create directory {path:?} - {err}"))?;
|
||||||
|
|
||||||
|
unistd::chown(path, options.owner, options.group)
|
||||||
|
.map_err(|err| format_err!("unable to set ownership for directory {path:?} - {err}"))?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -66,7 +70,7 @@ pub fn ensure_dir_exists<P: AsRef<Path>>(
|
|||||||
nix::sys::stat::fchmod(fd.as_raw_fd(), mode)
|
nix::sys::stat::fchmod(fd.as_raw_fd(), mode)
|
||||||
.map_err(|err| format_err!("unable to set mode for directory {path:?} - {err}"))?;
|
.map_err(|err| format_err!("unable to set mode for directory {path:?} - {err}"))?;
|
||||||
nix::unistd::fchown(fd.as_raw_fd(), uid, gid)
|
nix::unistd::fchown(fd.as_raw_fd(), uid, gid)
|
||||||
.map_err(|err| format_err!("unable to set ownership directory {path:?} - {err}"))?;
|
.map_err(|err| format_err!("unable to set ownership for directory {path:?} - {err}"))?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user