]> git.bitcoin.ninja Git - rust-lightning/commitdiff
f Use sync_all rather than libc::fsync
authorElias Rohrer <dev@tnull.de>
Wed, 23 Aug 2023 11:58:36 +0000 (13:58 +0200)
committerElias Rohrer <dev@tnull.de>
Wed, 23 Aug 2023 11:58:36 +0000 (13:58 +0200)
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).

lightning-persister/src/fs_store.rs

index e1fd9811bcd8a19fd697bc429bdc291533140c4f..11cf2d40215831655fea7b8f32d26111d2650ed1 100644 (file)
@@ -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() {