Repository navigation
Conversation
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
PR Summary by QodoSave configuration through a flushed temporary file
AI Description
Diagram
High-Level Assessment
Files changed (3)
|
Code Review by Qodo
1.
|
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Updated after the automatic review.
🤖 Generated with Claude Code |
What was wrong
A save (
/api/cmd?save) could leave a truncated or emptydivinus.yaml: a power cut, a crash or a full flash in the middle of the write destroys the only copy of the configuration, and Divinus then fails to start on the next boot.Why
app_config_save()(src/app_config.c) renamesdivinus.yamltodivinus.yaml.bak, then opens a newdivinus.yamlwithfopen(..., "w")and writes into it directly. For the whole duration of the write the configuration exists only as a half-written file, and the result offclose()and of the writes was never checked.What this changes
The new settings are written to
divinus.yaml.newin the same directory, flushed to storage (fflushandfsync), and only then put in place with onerename(), which replaces the old file in a single step. If any write or the flush fails, the.newfile is removed, the old configuration stays untouched andapp_config_save()returns a failure (the web UI already reports that).The previous file is still kept as
divinus.yaml.bakexactly as before, including everything that depends on it today (app_config_restore()at exit is not touched): the old file is linked (or, where hard links are not available, renamed) to.bakright before the rename.A new test,
tst/config-save, saves twice in a temporary directory and checks the result, the.bakfile, the restore, and the failure case.This does not change the rollback at exit; whether that is intended is a separate question that will be opened as an issue.
How it was checked
./test.sh config-saveon a PC passes (before the change its failure case does not pass)../build.sh fhan-v2, no new warnings.Effect on other platforms
Shared code, compiled for all targets, run on a save. Uses
fsync,link,rename,removeandfileno, all available in glibc, musl and uClibc. On file systems without hard links (FAT, some flash file systems),link()fails and the previous file is renamed to.bakinstead, which is what the old code did; the save still completes. During a save the directory briefly holds one more file (.new), so a nearly full configuration partition needs room for one extra copy of the file (a few kilobytes).🤖 Generated with Claude Code