Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ pub(crate) fn hash_payload(root: &Path) -> Result<Vec<ArtifactFile>, SnapshotErr
Ok(files)
}

fn fsync_dir(path: &Path) -> Result<(), SnapshotError> {
pub(crate) fn fsync_dir(path: &Path) -> Result<(), SnapshotError> {
File::open(path)?.sync_all()?;
Ok(())
}
Expand Down
27 changes: 27 additions & 0 deletions src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,10 @@ fn compact_to_file(

tmp.as_file().sync_all()?;
tmp.persist(path).map_err(|e| SnapshotError::Io(e.error))?;
// persist() is rename(2). The inode is durable; the directory entry is
// not until the parent is fsynced. artifact::rename_into_place already
// does this — compact is the fold's only other rename-publish.
crate::artifact::fsync_dir(dir)?;

Ok(())
}
Expand Down Expand Up @@ -1630,4 +1634,27 @@ mod tests {
);
assert_eq!(snap.entries["node.a"].value, b"survives");
}

/// Compact is the fold's durable publish: inode `sync_all` then rename.
/// Without a parent-directory fsync the new dirent can vanish on power
/// loss — the same hole `artifact::rename_into_place` already closes.
#[test]
fn compact_to_file_fsyncs_parent_dirent() {
let src = include_str!("snapshot.rs");
let compact = src
.split("fn compact_to_file(")
.nth(1)
.expect("compact_to_file must exist");
let compact = compact
.split(
"\n// ---------------------------------------------------------------------------",
)
.next()
.unwrap();
assert!(
compact.contains("fsync_dir"),
"compact_to_file must fsync the parent directory after persist, \
matching artifact::rename_into_place (Pedra F17)"
);
}
}
Loading