diff --git a/proxmox-tools/src/fs.rs b/proxmox-tools/src/fs.rs index d1f9cdcf..bc68ceab 100644 --- a/proxmox-tools/src/fs.rs +++ b/proxmox-tools/src/fs.rs @@ -159,6 +159,33 @@ pub fn fchown( Ok(()) } +/// Creates directory at the provided path with specified ownership +/// +/// Simply returns if the directory already exists. +pub fn create_dir_chown>( + path: P, + perm: Option, + owner: Option, + group: Option, +) -> Result<(), nix::Error> +{ + let mode : stat::Mode = perm.unwrap_or(stat::Mode::from_bits_truncate(0o770)); + + let path = path.as_ref(); + + match nix::unistd::mkdir(path, mode) { + Ok(()) => {}, + Err(nix::Error::Sys(nix::errno::Errno::EEXIST)) => { + return Ok(()); + }, + err => return err, + } + + unistd::chown(path, owner, group)?; + + Ok(()) +} + // /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);