From e894fb61d8aa88c017617e07b51610faed843ebf Mon Sep 17 00:00:00 2001 From: Finn Callies Date: Tue, 12 Aug 2025 11:28:37 +0200 Subject: [PATCH] rust-utils: use exists/rename instead of renameat2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use exists and rename instead of renameat2 which is only available in the linux implementation of libc. To enable compilation of pvsecret on macos the rust wrapper function renameat2 which calls the libc renameat2 function is replaced with rust native std::fs::exists ad std::fs::rename functions because macos' implementation of libc does not have the renameat2 function. Reviewed-by: Jan Höppner Reviewed-by: Steffen Eiden Signed-off-by: Finn Callies Signed-off-by: Steffen Eiden --- rust/utils/src/file.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/rust/utils/src/file.rs b/rust/utils/src/file.rs index 26974208..8fe5dd30 100644 --- a/rust/utils/src/file.rs +++ b/rust/utils/src/file.rs @@ -4,7 +4,7 @@ use std::{ ffi::{CString, OsStr}, - fs::{File, OpenOptions}, + fs::{rename, File, OpenOptions}, io::{self, Seek, SeekFrom, Write}, os::unix::{ffi::OsStrExt, fs::OpenOptionsExt}, path::Path, @@ -218,17 +218,16 @@ impl TempPath { } fn persist(self, operation: AtomicFileOperation) -> Result<()> { - let options = match operation { - AtomicFileOperation::Replace => 0, - AtomicFileOperation::NoReplace => libc::RENAME_NOREPLACE, - }; - - renameat2(&self.temp_path, &self.path, options).map_err(|e| { - PvCoreError::FileAccessRename { - src: self.temp_path.as_ref().to_str().unwrap().to_string(), - dst: self.path.as_ref().to_str().unwrap().to_string(), - source: e, + if let Ok(true) = self.path.try_exists() { + if operation == AtomicFileOperation::NoReplace { + return Err(Error::Io(io::Error::from(io::ErrorKind::AlreadyExists))); } + } + + rename(&self.temp_path, &self.path).map_err(|e| PvCoreError::FileAccessRename { + src: self.temp_path.as_ref().to_str().unwrap().to_string(), + dst: self.path.as_ref().to_str().unwrap().to_string(), + source: e, })?; self.forget(); Ok(())