feat: enhance safety and crash resilience in file operations; add fsync to critical writes and checks for path traversal

This commit is contained in:
asepharyana
2026-07-12 11:49:33 +07:00
parent 8767beef39
commit 87d0aac596
9 changed files with 79 additions and 26 deletions
+6 -3
View File
@@ -48,14 +48,16 @@ impl EditLog {
.collect()
}
/// Append one entry to `edits.jsonl` on disk and to the in-memory log.
/// Append one entry to `edits.jsonl` on disk and to the in-memory log,
/// with fsync for crash safety.
///
/// Flow: serialize `entry` to a JSON line → ensure parent dir exists →
/// open the file in append mode → write the line → push into
/// open the file in append mode → write the line → fsync → push into
/// `self.entries`.
///
/// Why: appending (not rewriting) keeps the log durable and cheap even
/// as it grows across a long session.
/// as it grows across a long session; fsync ensures the entry survives
/// a crash rather than lingering in the page cache.
///
/// Return: `Ok(())` on success; an `io::Error` if serialization or
/// any filesystem operation fails.
@@ -69,6 +71,7 @@ impl EditLog {
.open(&self.path)?;
use std::io::Write;
file.write_all(line.as_bytes())?;
file.sync_all()?;
self.entries.push(entry);
Ok(())
}