From: Elias Rohrer Date: Wed, 23 Aug 2023 11:58:36 +0000 (+0200) Subject: f Use sync_all rather than libc::fsync X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=5043bd793d12f9091ca3f58023cec75163eac920;p=rust-lightning f Use sync_all rather than libc::fsync As it iternally does the right thing (tm): call `libc::fsync` on non-macOS UNIXes and `libc::fcntl` with `libc::F_FULLSYNC` set on macOS (as Apple decided to break POSIX to cheat benchmarks). --- diff --git a/lightning-persister/src/fs_store.rs b/lightning-persister/src/fs_store.rs index e1fd9811b..11cf2d402 100644 --- a/lightning-persister/src/fs_store.rs +++ b/lightning-persister/src/fs_store.rs @@ -9,9 +9,6 @@ use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, Mutex, RwLock}; -#[cfg(not(target_os = "windows"))] -use std::os::unix::io::AsRawFd; - #[cfg(target_os = "windows")] use {std::ffi::OsStr, std::os::windows::ffi::OsStrExt}; @@ -134,9 +131,7 @@ impl KVStore for FilesystemStore { { fs::rename(&tmp_file_path, &dest_file_path)?; let dir_file = fs::OpenOptions::new().read(true).open(&parent_directory)?; - unsafe { - libc::fsync(dir_file.as_raw_fd()); - } + dir_file.sync_all()?; Ok(()) } @@ -204,15 +199,13 @@ impl KVStore for FilesystemStore { std::io::Error::new(std::io::ErrorKind::InvalidInput, msg) })?; let dir_file = fs::OpenOptions::new().read(true).open(parent_directory)?; - unsafe { - // The above call to `fs::remove_file` corresponds to POSIX `unlink`, whose changes - // to the inode might get cached (and hence possibly lost on crash), depending on - // the target platform and file system. - // - // In order to assert we permanently removed the file in question we therefore - // call `fsync` on the parent directory on platforms that support it, - libc::fsync(dir_file.as_raw_fd()); - } + // The above call to `fs::remove_file` corresponds to POSIX `unlink`, whose changes + // to the inode might get cached (and hence possibly lost on crash), depending on + // the target platform and file system. + // + // In order to assert we permanently removed the file in question we therefore + // call `fsync` on the parent directory on platforms that support it, + dir_file.sync_all()?; } if dest_file_path.is_file() {