From 4dda9b5865ba0db1b85d5acf76f42330d3e8f1a5 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Wed, 21 Apr 2021 11:32:07 +0200 Subject: [PATCH] proxmox: constify CreateOptions methods and add helpers Adds convenience helpers `.owner_root()`. Unfortunately for the group equivalent `nix` needs to have const `Gid` constructors... Signed-off-by: Wolfgang Bumiller --- proxmox/src/tools/fs.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/proxmox/src/tools/fs.rs b/proxmox/src/tools/fs.rs index 5c9f8371..cd0ba359 100644 --- a/proxmox/src/tools/fs.rs +++ b/proxmox/src/tools/fs.rs @@ -219,20 +219,40 @@ impl CreateOptions { } } - pub fn perm(mut self, perm: stat::Mode) -> Self { + pub const fn perm(mut self, perm: stat::Mode) -> Self { self.perm = Some(perm); self } - pub fn owner(mut self, owner: Uid) -> Self { + pub const fn owner(mut self, owner: Uid) -> Self { self.owner = Some(owner); self } - pub fn group(mut self, group: Gid) -> Self { + pub const fn group(mut self, group: Gid) -> Self { self.group = Some(group); self } + + /// Convenience shortcut around having to import `Uid` from nix. + pub const fn owner_root(self) -> Self { + self.owner(nix::unistd::ROOT) + } + + // TODO: once 'nix' has `const fn` constructors for Uid and Gid we can enable these: + + /* + /// Convenience shortcut around having to import `Gid` from nix. + pub const fn group_root(self) -> Self { + // nix hasn't constified these yet, but it's just an alias to gid_t: + self.group(Gid::from_raw(0)) + } + + /// Convenience shortcut to set both owner and group to 0. + pub const fn root_only(self) -> Self { + self.owner_root().group_root() + } + */ } /// Creates directory at the provided path with specified ownership.