Repository navigation
Make config file writes durable and reads safe - #161
Merged
Merged
Conversation
- Flush the directory after replacing a file, and make temp files 0600. - Make the key file without relying on hard links, and wait for one that is mid-write. - Tell our own writes from edits by a SHA-256 of the bytes, with the size and time taken from the temp file before it moves. No plaintext copy is kept for comparison. - ConfigFileError gets kinds (Io, Decrypt, Parse, Invalid, Newer); advice follows the kind. - Cap config files at 8 MiB. - Refuse a symlinked config directory and one owned by another user on Linux. - update gives up waiting for subscribers after 15 seconds instead of for ever. - shareOnStartup has one default, true. - Files carry "version": 1; a newer one is refused. Unknown names in hand-edited files are logged. - Fault-injection tests for each write step, a two-process key file race, and tests for the rest. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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.
Closes #143
What changed and why
All in
koppel-config(plusdocs/config.md).0600. A directory-flush error that only means "this file system cannot" (InvalidInput,Unsupported) is logged at debug and ignored.create_newin place, flushed, with the directory flushed after. A start that finds a short key file waits up to 3 seconds for it. A crash mid-write in the fallback leaves a short file that the next start refuses with the existing message; nothing was sealed with it.State.current_jsonis gone. "Unchanged" is a typedPartialEqcompare (ConfigFile<T>now needsT: PartialEq; every file type already has it). This also covers the first bullet of Secrets are not copied, printed or left beside the key #156.ConfigFileErrorgetskind: ConfigErrorKind(Io,Decrypt,Parse,Invalid,Newer, non-exhaustive);pathandreasonstay. The advice is composed from the kind: onlyDecryptsays "restore the keys directory". The secret container's own "newer format" refusal still arrives asDecrypt, becauseCodecError(pub String)is used by koppel-usb tests and I did not change its shape.harden_directoryrefuses a symlink and a directory owned by anyone but the effective user (libc::geteuid;libcwas already a workspace dependency for koppel-usb).updatestill returns after delivery, and the nested shortcut stays, but the wait now times out afterOptions::delivery_timeout(default 15 s). A handler that spawns an update and awaits it used to deadlock; it now costs one timeout and the change was on disk already. I kept the deliver-then-return ordering because the confirm rule needs it.shareOnStartup: one default, true. A hand-editedsettings.jsonwithout the key used to read as false."version": 1after its fields (done at the file layer, so the config structs did not change). A newer version is refused (Newer). Unknown top-level names insettings.json,devices.jsonandattached.jsonare logged.Tests
write_atomic_withtakes a hook called after each step; a failure at each of six steps leaves the old file or the whole new one and no temp file. Plus a failing codec throughConfigFile::update(nothing kept, nothing delivered).tests/key_file_race.rs): 6 real processes open a new key file together, 5 rounds; one key, nothing left over.rust:1.99.1-trixie):cargo test --workspace --lockedall green;cargo clippy --workspace --all-targets --locked -- -D warningsclean. Native Windows:cargo build --workspace --locked,cargo clippy ... -D warnings,cargo fmt --all --checkclean.koppel-contractagainst a fresh build, the UI tests,cargo deny. The directory fsync itself cannot be observed; the test checks it is the step after the replace. The owner test needs root and is skipped otherwise (it ran as root in the container)."version": 1at the end). That test was right for the old format and is updated for the new one.Docs
docs/config.mdupdated.Changelog notes:
"version": 1; 0.1.x and older 0.2 builds ignore it. A file without it is read as version 1. The key file and its format are unchanged. Temp and replaced files are0600on Linux.settings.jsonwithoutshareOnStartupnow means true (was false). Linux: a symlinked or foreign-owned config or keys directory is refused at start.ConfigFileError.kind,ConfigErrorKind,Options.warn_unknown_keys,Options.delivery_timeoutadded.🤖 Generated with Claude Code