diff --git a/rust/utils/src/file.rs b/rust/utils/src/file.rs index 8fe5dd30..4014799d 100644 --- a/rust/utils/src/file.rs +++ b/rust/utils/src/file.rs @@ -3,44 +3,15 @@ // Copyright IBM Corp. 2024 use std::{ - ffi::{CString, OsStr}, + ffi::OsStr, fs::{rename, File, OpenOptions}, io::{self, Seek, SeekFrom, Write}, - os::unix::{ffi::OsStrExt, fs::OpenOptionsExt}, + os::unix::fs::OpenOptionsExt, path::Path, }; use pv::{Error, FileAccessErrorType, PvCoreError, Result}; -/// Rust wrapper for `libc::renameat2` -fn renameat2, Q: AsRef>(oldpath: P, newpath: Q, flags: u32) -> io::Result<()> { - let oldpath_cstr = CString::new(oldpath.as_ref().as_os_str().as_bytes())?; - let oldpath_raw = oldpath_cstr.into_raw(); - let newpath_cstr = CString::new(newpath.as_ref().as_os_str().as_bytes())?; - let newpath_raw = newpath_cstr.into_raw(); - unsafe { - // SAFETY: oldpath_raw and newpath_raw are valid CStrings because they were - // generated by the `CString::new` function. - let ret = libc::renameat2( - libc::AT_FDCWD, - oldpath_raw, - libc::AT_FDCWD, - newpath_raw, - flags, - ); - // SAFETY: libc::renameat2 does not modify `newpath_raw` and is - // therefore still valid. - let _ = CString::from_raw(newpath_raw); - // SAFETY: libc::renameat2 does not modify `oldpath_raw` and is - // therefore still valid. - let _ = CString::from_raw(oldpath_raw); - if ret == -1 { - return Err(io::Error::last_os_error()); - } - Ok(()) - } -} - /// This type helps to perform atomic operations by writing to a temporary file /// and renaming it to the actual filename when the [`AtomicFile::finish`] /// function is called. If the [`AtomicFile::finish`] function is never called,