Repository navigation
Conversation
Before a save replaces the user or project config, copy the current file into config-backups/ in the Magic Context store (same resolution as the plugin: MAGIC_CONTEXT_STORAGE_DIR, XDG_DATA_HOME, ~/.local/share). The folder is 0700 and each copy 0600, written with O_NOFOLLOW and create_new. Copies carry a UTC timestamp with nanoseconds and a counter; project copies carry a short hash of the canonical project path. The newest 5 copies per config stay. If the copy fails, the save is refused and the config is not changed.
|
Waiting for This PR needs a linked issue with the |
| if let Err(e) = back_up_current_config(path, &backup_dir, &backup_scope) { | ||
| let _ = std::fs::remove_file(&temp_path); | ||
| return Err(e); | ||
| } |
There was a problem hiding this comment.
There was a problem hiding this comment.
Confirmed: read, copy, prune and replace were not serialized, so two overlapping saves could both copy the same old version. Fixed in 6b58b23. Each save now holds an in-process mutex for its backup set and an exclusive flock on .<scope>.lock in the owner-only backup folder (opened with O_NOFOLLOW, mode 0600) from the read until after the replace. If the lock is still held after 10 s, the save is refused, the same as a failed copy. Saves of different configs do not wait for each other; on non-unix only the in-process mutex applies. Tests force the overlap with a read hook and with a lock held from another handle; with both locks removed, 3 tests fail.
Two overlapping saves could both copy the same old version, so the version between them had no copy. Hold a per-config in-process mutex and an exclusive flock on a lock file in the owner-only backup folder from the read of the current config until after the replace. A lock that stays held past 10 s refuses the save.
A symlinked user config named its copies after the link target, so a link that changed to a different file started a new set of copies that was never pruned. Name and prune the copies by the config path before symlinks resolve; use the resolved path only to read and write.
Approved issue: #956 (implemented in the shape proposed there).
Behaviour
<store>/config-backups/. The store is resolved as the plugin resolves it:MAGIC_CONTEXT_STORAGE_DIR, elseXDG_DATA_HOME, else the platform's default data folder, thencortexkit/magic-context.magic-context.user.<UTC stamp with nanoseconds>-NN.jsoncfor the user config, andmagic-context.project-<12 hex of the sha256 of the canonical project path>.<stamp>-NN.jsoncfor a project config.create_newand a counter keep two saves in the same second apart.0700through anO_NOFOLLOWhandle, and each copy is written withO_NOFOLLOWand mode0600.Points to review
MAGIC_CONTEXT_STORAGE_DIRrefuses the save, the same as the plugin refuses a relative override.config-backupsis still followed, because the plugin store follows it too.usercopy set.Tests
The three requested tests (a save keeps the previous bytes; a sixth save drops the oldest copy; a copy failure leaves the config untouched), plus: copies go to the data folder and to the storage override; a project copy carries the project hash; owner-only modes; a symlinked backup folder refuses the save.
cargo test --lib config: 24 pass. With the backup call removed, the 7 new behaviour tests fail.Separate Windows dashboard processes can still lose an intermediate config version, so the PR needs another fix before merging.
Findings
Summary
Adds config backups before dashboard saves.
Reviews (2) · Last reviewed commit: "fix(dashboard): name backups by the logi..." · Reviewed by Greptile