remove pbs-tools/src/sys.rs

Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
This commit is contained in:
Dietmar Maurer 2021-11-25 08:52:17 +01:00
parent 0df179c2b4
commit fddb9bcc3e
4 changed files with 16 additions and 38 deletions

View File

@ -20,20 +20,30 @@ mod config_version_cache;
pub use config_version_cache::ConfigVersionCache; pub use config_version_cache::ConfigVersionCache;
use anyhow::{format_err, Error}; use anyhow::{format_err, Error};
use nix::unistd::{Gid, Group, Uid, User};
pub use pbs_buildcfg::{BACKUP_USER_NAME, BACKUP_GROUP_NAME}; pub use pbs_buildcfg::{BACKUP_USER_NAME, BACKUP_GROUP_NAME};
/// Return User info for the 'backup' user (``getpwnam_r(3)``) /// Return User info for the 'backup' user (``getpwnam_r(3)``)
pub fn backup_user() -> Result<nix::unistd::User, Error> { pub fn backup_user() -> Result<nix::unistd::User, Error> {
pbs_tools::sys::query_user(BACKUP_USER_NAME)? if cfg!(test) {
.ok_or_else(|| format_err!("Unable to lookup '{}' user.", BACKUP_USER_NAME)) Ok(User::from_uid(Uid::current())?.expect("current user does not exist"))
} else {
User::from_name(BACKUP_USER_NAME)?
.ok_or_else(|| format_err!("Unable to lookup '{}' user.", BACKUP_USER_NAME))
}
} }
/// Return Group info for the 'backup' group (``getgrnam(3)``) /// Return Group info for the 'backup' group (``getgrnam(3)``)
pub fn backup_group() -> Result<nix::unistd::Group, Error> { pub fn backup_group() -> Result<nix::unistd::Group, Error> {
pbs_tools::sys::query_group(BACKUP_GROUP_NAME)? if cfg!(test) {
.ok_or_else(|| format_err!("Unable to lookup '{}' group.", BACKUP_GROUP_NAME)) Ok(Group::from_gid(Gid::current())?.expect("current group does not exist"))
} else {
Group::from_name(BACKUP_GROUP_NAME)?
.ok_or_else(|| format_err!("Unable to lookup '{}' group.", BACKUP_GROUP_NAME))
}
} }
pub struct BackupLockGuard(Option<std::fs::File>); pub struct BackupLockGuard(Option<std::fs::File>);
#[doc(hidden)] #[doc(hidden)]

View File

@ -9,7 +9,6 @@ pub mod percent_encoding;
pub mod sha; pub mod sha;
pub mod str; pub mod str;
pub mod sync; pub mod sync;
pub mod sys;
pub mod ticket; pub mod ticket;
pub mod async_lru_cache; pub mod async_lru_cache;

View File

@ -1,31 +0,0 @@
//! System level helpers.
use nix::unistd::{Gid, Group, Uid, User};
/// Query a user by name but only unless built with `#[cfg(test)]`.
///
/// This is to avoid having regression tests query the users of development machines which may
/// not be compatible with PBS or privileged enough.
pub fn query_user(user_name: &str) -> Result<Option<User>, nix::Error> {
if cfg!(test) {
Ok(Some(
User::from_uid(Uid::current())?.expect("current user does not exist"),
))
} else {
User::from_name(user_name)
}
}
/// Query a group by name but only unless built with `#[cfg(test)]`.
///
/// This is to avoid having regression tests query the groups of development machines which may
/// not be compatible with PBS or privileged enough.
pub fn query_group(group_name: &str) -> Result<Option<Group>, nix::Error> {
if cfg!(test) {
Ok(Some(
Group::from_gid(Gid::current())?.expect("current group does not exist"),
))
} else {
Group::from_name(group_name)
}
}

View File

@ -506,7 +506,7 @@ fn create_run_dir() -> Result<(), Error> {
/// Return User info for the 'backup' user (``getpwnam_r(3)``) /// Return User info for the 'backup' user (``getpwnam_r(3)``)
pub fn backup_user() -> Result<nix::unistd::User, Error> { pub fn backup_user() -> Result<nix::unistd::User, Error> {
pbs_tools::sys::query_user(pbs_buildcfg::BACKUP_USER_NAME)? nix::unistd::User::from_name(pbs_buildcfg::BACKUP_USER_NAME)?
.ok_or_else(|| format_err!("Unable to lookup '{}' user.", pbs_buildcfg::BACKUP_USER_NAME)) .ok_or_else(|| format_err!("Unable to lookup '{}' user.", pbs_buildcfg::BACKUP_USER_NAME))
} }