From 08c2fc4acbe590861d0648d6a9262ff4216a6e78 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Tue, 20 Jul 2021 13:51:56 +0200 Subject: [PATCH] open_file_locked: add options parameter (CreateOptions) To be able to set file permissions and ownership. This is a breaking change. --- proxmox/src/tools/fs.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/proxmox/src/tools/fs.rs b/proxmox/src/tools/fs.rs index 013fb27f..8087cc8f 100644 --- a/proxmox/src/tools/fs.rs +++ b/proxmox/src/tools/fs.rs @@ -1,7 +1,7 @@ //! File related utilities such as `replace_file`. use std::ffi::CStr; -use std::fs::{File, OpenOptions}; +use std::fs::File; use std::io::{self, BufRead, BufReader, Write}; use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; use std::path::{Path, PathBuf}; @@ -592,12 +592,17 @@ pub fn open_file_locked>( path: P, timeout: Duration, exclusive: bool, + options: CreateOptions, ) -> Result { let path = path.as_ref(); - let mut file = match OpenOptions::new().create(true).append(true).open(path) { - Ok(file) => file, - Err(err) => bail!("Unable to open lock {:?} - {}", path, err), - }; + + let mut file = atomic_open_or_create_file( + path, + OFlag::O_RDWR | OFlag::O_CLOEXEC | OFlag::O_APPEND, + &[], + options, + )?; + match lock_file(&mut file, exclusive, Some(timeout)) { Ok(_) => Ok(file), Err(err) => bail!("Unable to acquire lock {:?} - {}", path, err),