Write recent.txt atomically - #20
Merged
Merged
Conversation
This was referenced Aug 11, 2026
saveRecents() opened recent.txt with mode "w", which truncates the file before a single byte of the new list is written. On a handheld that window is not theoretical - the power switch is right there, and losing it mid-write leaves behind a truncated or empty recents list. Add openAtomic()/commitAtomic() next to putFile() in utils.c: write to a sibling .tmp file, fsync it, then rename() over the target, so a reader only ever sees the whole old file or the whole new one. Use them in saveRecents().
lepht
force-pushed
the
fix/atomic-writes
branch
from
August 12, 2026 00:41
d43f255 to
c153fb4
Compare
PR testing for these functions covered the happy path only - create, grow, shrink - and asserted the failure behaviour from reading the code. The failure behaviour is the entire reason they exist, so it is the one thing that should not have been taken on trust. Adds tests/ with a test for utils.c, a `make test` target and a CI job. The useful check is that a commit which cannot complete leaves what was already on the card exactly as it was, forced by pointing the rename at a non-empty directory so it fails whether or not the tests run as root. Also covers the truncated-path and unopenable-temp-file paths, and a NULL handle being a safe no-op. While the harness is here it also covers the pure helpers the menu leans on: suffixMatch, which decides whether a multi-disc folder is keyed on its m3u or cue; containsString, which is the rom search filter; and getDisplayName. Note exactMatch is case sensitive where its neighbours are not - asserted as observed, not endorsed, so a change to it becomes visible. utils.c links against libc alone, so the test binary is just the two files and needs SDL for headers only. Nothing opens a window. The job is native and takes about a minute, and sits alongside the builds rather than gating them. The one whitespace change silences a misleading-indentation warning in getInt that -Wall surfaces on every run. Logic is unchanged; `git diff -w` is empty over it. Warnings that always fire are warnings nobody reads.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
saveRecents()opensrecent.txtwith mode"w", which truncates the file before a single byte of the new list is written. On a handheld that window isn't theoretical — the power switch is right there, and losing power mid-write leaves a truncated or empty recents list behind.Changes
openAtomic()/commitAtomic()inworkspace/all/common/utils.c, next to the existingputFile(). Write to a sibling.tmpfile,fsync()it, thenrename()over the target, so a reader only ever sees the whole old file or the whole new one. On failure the temp file is unlinked and the original is left untouched.saveRecents()uses them.tests/with a unit test forutils.c, amake testtarget, and a CI job.fsync()before the rename matters here: without it the rename can land while the contents are still only in the page cache, which on a yanked-power SD card is the same failure in a different disguise.Tests
The original testing here covered the happy path only — create, grow, shrink — and asserted the failure behaviour from reading the code. The failure behaviour is the entire reason these functions exist, so it was the one thing that shouldn't have been taken on trust.
The load-bearing check forces
rename()to fail by pointing it at a non-empty directory (which fails whether or not tests run as root), then asserts the original target and its contents are exactly as they were and the temp file is gone. Also covered: the truncated-path guard, an unopenable temp file, and a NULL handle being a safe no-op.While the harness is there it also covers the pure helpers the menu leans on —
suffixMatch(decides whether a multi-disc folder is keyed on its.m3uor.cue),containsString(is the rom search filter),prefixMatch,hide,getDisplayName,trimSortingMeta.One asymmetry worth a reviewer's eye:
exactMatchis case sensitive whereprefixMatchandsuffixMatchare not.hide()inherits that, somap.txtis hidden butMap.txtis not. Asserted as observed rather than endorsed, so if anyone changes it the test says so.Why this is cheap
utils.clinks against libc alone — no SDL symbols, no project symbols — so the test binary is just the two files, with SDL needed for headers only. Nothing opens a window. The CI job is native, takes about a minute, and sits alongside the builds rather than gating them so a red test reads on its own row.Verified
Plus the original end-to-end run under Xvfb: cleared
recent.txtand launched a game (file created, no.tmp), then launched a second from another system (113 → 176 bytes, newest at top, both intact).One whitespace change
-Wallsurfaced a-Wmisleading-indentationwarning ingetInt— mixed tabs and spaces, logic correct. Fixed the indentation so it doesn't fire on every test run;git diff -wover it is empty. Warnings that always fire are warnings nobody reads.Note
The same atomicity fix applies to
saveFavorites()in #1, which picks these helpers up from this branch rather than duplicating them.