Add EWAD header hash (format v2) and atomic file writes #3

Merged
fputs merged 1 commit from ewad-v2 into master 2026-11-08 14:52:13 -08:00
Owner

Summary

Two changes ahead of Wayfinder adopting EWAD for game data and save games.

  • EWAD format version 2. The 8 bytes at header offset 48 hold a header hash: XXH64, seed 0, over the header with the hash field zero, the directory, then the string table, in that order wherever they sit in the file. wad_view_open bounds-checks both ranges, checks the hash, then runs the per-entry validation. It reads no lump data. The value is exposed as WAD_View.archive_hash. Version 1 is rejected, and all structural checks stay.
  • Atomic file writes. wad_write_file and wad_write_file_ex write a uniquely named temporary file beside the target, fsync it, rename it over the target, and fsync the directory. On failure the target keeps its bytes and the temporary file is removed. POSIX-only.

Also updates README.md, docs/spitwad.md, docs/WAD Format.md and the spitwad.h comments, and removes docs/research/, which described the earlier design.

Behaviour changes

  • struct WAD_View gains archive_hash as its last field.
  • spitwad.c no longer compiles on Windows.
  • Symlinks are followed: the file they point at is replaced and the link is kept. A dangling symlink returns SPITWAD_ERR_IO and creates nothing.
  • A target that is a device, FIFO or directory returns SPITWAD_ERR_IO. Renaming over /dev/full as root would otherwise destroy the node and report success.
  • Writing needs write permission on the directory, so a writable file in a read-only directory now fails.
  • A replaced file is a new inode: permission bits are kept, but owner, extended attributes and hard links are not.
  • A failed directory fsync happens after the rename, so the target then holds the new bytes while the call returns SPITWAD_ERR_IO. This is the one exception to the target being untouched on failure, and it is documented.

Testing

  • 40 of 40 tests pass from a clean SPITWAD_SANITIZE=ON build on Apple clang 21, and on GCC 15.2 in alpine:3.24 as root and as non-root with leak detection on.
  • spitwad.c compiles without warnings under Wayfinder's flags on both compilers.
  • New tests: every bit flipped in named, stripped and hand-built archives, where only computed padding offsets may be accepted; a stale hash alone is rejected; opening with lump pages PROT_NONE; atomic-write failure, permission and symlink cases.
  • The fixture re-stamps the hash after each deliberate corruption. Disabling each validation and atomic-write check on a scratch copy caught 31 of 32 mutants. The survivor is equivalent, since a missing NUL is still rejected by the length check.
  • Classic output is byte-identical to master for IWAD, PWAD, WAD2, WAD3 and PACK. The fixture's header hash is pinned from python-xxhash.

Not verified

  • No real power-loss or full-disk test. The write failure path is simulated with RLIMIT_FSIZE, and the fsync order was confirmed from an instrumented build.
  • The directory fsync failure path has no test.
  • Linux was tested on musl under x86_64 emulation, not glibc.
  • On macOS fsync does not flush the drive's own cache.

🤖 Generated with Claude Code

## Summary Two changes ahead of Wayfinder adopting EWAD for game data and save games. - **EWAD format version 2.** The 8 bytes at header offset 48 hold a header hash: XXH64, seed 0, over the header with the hash field zero, the directory, then the string table, in that order wherever they sit in the file. `wad_view_open` bounds-checks both ranges, checks the hash, then runs the per-entry validation. It reads no lump data. The value is exposed as `WAD_View.archive_hash`. Version 1 is rejected, and all structural checks stay. - **Atomic file writes.** `wad_write_file` and `wad_write_file_ex` write a uniquely named temporary file beside the target, fsync it, rename it over the target, and fsync the directory. On failure the target keeps its bytes and the temporary file is removed. POSIX-only. Also updates `README.md`, `docs/spitwad.md`, `docs/WAD Format.md` and the `spitwad.h` comments, and removes `docs/research/`, which described the earlier design. ## Behaviour changes - `struct WAD_View` gains `archive_hash` as its last field. - `spitwad.c` no longer compiles on Windows. - Symlinks are followed: the file they point at is replaced and the link is kept. A dangling symlink returns `SPITWAD_ERR_IO` and creates nothing. - A target that is a device, FIFO or directory returns `SPITWAD_ERR_IO`. Renaming over `/dev/full` as root would otherwise destroy the node and report success. - Writing needs write permission on the directory, so a writable file in a read-only directory now fails. - A replaced file is a new inode: permission bits are kept, but owner, extended attributes and hard links are not. - A failed directory fsync happens after the rename, so the target then holds the new bytes while the call returns `SPITWAD_ERR_IO`. This is the one exception to the target being untouched on failure, and it is documented. ## Testing - 40 of 40 tests pass from a clean `SPITWAD_SANITIZE=ON` build on Apple clang 21, and on GCC 15.2 in `alpine:3.24` as root and as non-root with leak detection on. - `spitwad.c` compiles without warnings under Wayfinder's flags on both compilers. - New tests: every bit flipped in named, stripped and hand-built archives, where only computed padding offsets may be accepted; a stale hash alone is rejected; opening with lump pages `PROT_NONE`; atomic-write failure, permission and symlink cases. - The fixture re-stamps the hash after each deliberate corruption. Disabling each validation and atomic-write check on a scratch copy caught 31 of 32 mutants. The survivor is equivalent, since a missing NUL is still rejected by the length check. - Classic output is byte-identical to master for IWAD, PWAD, WAD2, WAD3 and PACK. The fixture's header hash is pinned from python-xxhash. ## Not verified - No real power-loss or full-disk test. The write failure path is simulated with `RLIMIT_FSIZE`, and the fsync order was confirmed from an instrumented build. - The directory fsync failure path has no test. - Linux was tested on musl under x86_64 emulation, not glibc. - On macOS `fsync` does not flush the drive's own cache. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Add EWAD header hash (format v2) and atomic file writes
All checks were successful
test / test (pull_request) Successful in 14s
test / test (push) Successful in 12s
test / sanitize (pull_request) Successful in 17s
test / sanitize (push) Successful in 11s
4edbbbd6dc
Bump EWAD to version 2 with an XXH64 header hash over the header,
directory and string table, exposed as WAD_View.archive_hash, so that
damaged metadata can no longer open as a valid archive.

Replace in-place file writes with temp file, fsync, rename and directory
fsync, so a crash or full disk cannot destroy an existing file.

Remove docs/research, which described the earlier design.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
fputs merged commit af2b0c8c1c into master 2026-11-08 14:52:13 -08:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
fputs/spitwad!3
No description provided.