Skip to content

Commit

Permalink
Linux with unnamed-tmpfile: sync the temporary file after linking it
Browse files Browse the repository at this point in the history
This fixes a data loss problem on btrfs (GH-6)
  • Loading branch information
andreacorbellini committed Apr 24, 2024
1 parent 552bdf2 commit f579909
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/imp/unix/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use nix::fcntl::AtFlags;
use nix::fcntl::OFlag;
use nix::libc;
use nix::sys::stat::Mode;
use nix::unistd::fdatasync;
use nix::unistd::linkat;
use std::ffi::OsStr;
use std::ffi::OsString;
Expand Down Expand Up @@ -57,6 +58,13 @@ fn rename_unnamed_temporary_file(dir: &Dir, file: &File, name: &OsStr) -> nix::R
}
};

// The file is supposed to be already synced by `AtomicWriteFile::_commit()`, however on some
// filesystems (notably, btrfs), syncs are ignored for unnamed temporary files, hence we sync
// again for safety.
//
// See https://github.com/andreacorbellini/rust-atomic-write-file/issues/6 for more details.
fdatasync(file.as_raw_fd())?;

rename_temporary_file(dir, &temporary_name, name)
}

Expand Down

0 comments on commit f579909

Please sign in to comment.