Skip to content

fix: pack atomically through a temporary file - #106

Merged
blocksmith314 merged 1 commit into
mainfrom
fix/77-atomic-pack
Nov 28, 2026
Merged

blocksmith314 merged 1 commit into
mainfrom
fix/77-atomic-pack

Conversation

@blocksmith314

@blocksmith314 blocksmith314 commented Nov 28, 2026 •

Copy link
Copy Markdown
Owner

Summary

Make pack atomic: a failed or interrupted pack no longer destroys an existing archive or leaves a partial file under the final name.

  • Temporary file + atomic publish: PackPipeline creates a hidden temporary file with O_EXCL in the destination directory (new NewPosixWriteFileExclusive), writes the archive there, syncs it, and only then publishes it under the final name.
    • --force: rename() replaces the destination atomically.
    • without --force: link() + unlink(). If the destination appears after the early check, link() fails with EEXIST and the existing file is never overwritten. If the filesystem cannot create hard links, the pack fails with a clear error instead of falling back to rename() (which would overwrite).
  • Single exclusive handle: the writer thread receives the already-open WriteFile instead of reopening the temporary path, so there is no window where a replaced/symlinked temporary name could redirect the archive data.
  • Robust existence check: the early check uses symlink_status (a broken symlink is not mistaken for "missing"), and stat failures are reported instead of silently continuing.
  • Cleanup and durability: every failure path removes the temporary file via error_code overloads; a scope guard also covers exception paths (thread creation, allocations). After publishing, the destination directory is fsynced best-effort so the name change survives a crash.
  • Ordering: BuildBlocksFromFileStats() runs before the temporary file is created, so an empty tree fails without creating anything.
  • Behavior changes: packing an empty tree no longer leaves a 0-byte archive; --force replaces a symlink at the destination (the link itself, not its target).
  • README/README_CN and CLAUDE.md document the atomic behavior.

No archive-format change.

Review fixes

  • Removed the no-force rename() fallback: a filesystem without hard links now fails and asks for --force.
  • WriterLoop no longer reopens the temporary file by path; it uses the exclusive handle.
  • Added a best-effort directory fsync and a scope guard for exception-path cleanup.

Tests

  • Release ctest: 220/220 passed
  • ASan ctest: 220/220 passed
  • New AtomicPack.* tests:
    • empty-tree failure keeps the existing archive and leaves no temporary file
    • mid-pack failure (source file deleted after scan) keeps the existing archive and leaves no temporary file
    • no --force refuses to replace an existing archive
    • --force replaces it and the new content is readable
    • a broken symlink without --force is refused; with --force the symlink itself is replaced
  • CLI smoke test: ctar pack -f <empty-dir> existing.ctar fails, the archive bytes are unchanged, no .tmp. files remain, and the old archive still unpacks.

Performance gate

Against origin/main (e377745): pack -1.4% wall time / +0.9% peak RSS; unpack -6.1% wall time / +0.5% peak RSS (5 runs). No regression.

Closes #77

@blocksmith314 blocksmith314 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

The temp-file/rename direction and the AtomicPack tests are good, but two atomicity/security gaps remain.

1. No-force fallback to rename() can overwrite the destination (blocker)

When link(temp, out) fails with EPERM/EOPNOTSUPP/ENOSYS, the code falls back to:

fs::rename(temp_pack_file, output_pack_file, rename_ec);

rename() replaces an existing destination. If the destination appears after the early symlink_status check (or the early check is bypassed by a race), a no-force pack overwrites it on a filesystem without hard links. That violates the stated guarantee. EPERM may also be a permission error rather than "no hard links".

Fix options:

  • fail the no-force pack with a clear error when link() is unsupported, letting --force use rename(); or
  • use a no-replace primitive (renameat2(RENAME_NOREPLACE) on Linux / renamex_np(RENAME_EXCL) on macOS) instead of plain rename.

A test or at least a documented limitation is needed.

2. WriterLoop reopens the temp file by path (blocker)

The temp file is created with O_EXCL, but the writer thread reopens it:

auto w_res = NewPosixWriteFile(packed_file_);

NewPosixWriteFile uses O_CREAT without O_EXCL/O_NOFOLLOW. Between creation and the writer opening it, another process in a writable destination directory can unlink the temp file and replace it with a symlink or another file; the writer then writes the archive blocks into that target. It also silently recreates the temp file if it disappeared.

Pass the already-open exclusive WriteFile handle (or a dup of its fd) to WriterLoop instead of reopening by path. The exclusive creation is the security boundary and should stay the only open-by-name.

Minor

  • After link()/rename(), the parent directory is not fsynced. The archive itself is synced, but a crash after the name change may lose the new directory entry on some filesystems; if durability is part of the claim, add a directory fsync.
  • cleanup_temp is a manual lambda; an exception from std::thread creation, metadata allocation, or formatting after temp creation leaves the hidden temp file behind. A scope guard would cover exception paths.
  • The tests do not cover the no-hard-link fallback or the writer-reopen TOCTOU; the current tests only run on filesystems with hard links.

Verified locally: Release ctest 220/220, ASan ctest 220/220, CI 5/5 green.

PackPipeline opened the destination with O_TRUNC before scanning, so a failed pack (for example an empty tree) destroyed an existing archive, and a crash mid-pack left a partial file under the final name. The --force existence check also ignored stat errors and missed broken symlinks.

Write to a hidden temporary file created with O_EXCL in the destination directory, rename it into place only after the header, metadata and magic are written and synced, and remove the temporary file on every failure path with error_code cleanup that does not hide the primary error. The existence check now uses symlink_status and reports stat failures.

Add AtomicPack tests covering empty-tree failure, mid-pack failure, no-force refusal, successful replacement, and broken symlinks.

Closes #77
@blocksmith314
blocksmith314 merged commit 52bf2fe into main Nov 28, 2026
5 checks passed
@blocksmith314
blocksmith314 deleted the fix/77-atomic-pack branch November 28, 2026 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[reliability] Bug: pack output is non-atomic; --force can destroy an existing archive and failures leave partial files

1 participant