]> git.proxmox.com Git - proxmox-backup.git/blob - pbs-tools/src/sys.rs
remove pbs-tools/src/cli.rs
[proxmox-backup.git] / pbs-tools / src / sys.rs
1 //! System level helpers.
2
3 use nix::unistd::{Gid, Group, Uid, User};
4
5 /// Query a user by name but only unless built with `#[cfg(test)]`.
6 ///
7 /// This is to avoid having regression tests query the users of development machines which may
8 /// not be compatible with PBS or privileged enough.
9 pub fn query_user(user_name: &str) -> Result<Option<User>, nix::Error> {
10 if cfg!(test) {
11 Ok(Some(
12 User::from_uid(Uid::current())?.expect("current user does not exist"),
13 ))
14 } else {
15 User::from_name(user_name)
16 }
17 }
18
19 /// Query a group by name but only unless built with `#[cfg(test)]`.
20 ///
21 /// This is to avoid having regression tests query the groups of development machines which may
22 /// not be compatible with PBS or privileged enough.
23 pub fn query_group(group_name: &str) -> Result<Option<Group>, nix::Error> {
24 if cfg!(test) {
25 Ok(Some(
26 Group::from_gid(Gid::current())?.expect("current group does not exist"),
27 ))
28 } else {
29 Group::from_name(group_name)
30 }
31 }