fix: make edit.WriteFile atomic and mode-preserving #22
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "fix/writefile-preserve-mode-atomic"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
edit.WriteFiledid an in-placeos.WriteFile, which can truncate a file if the process crashes mid-write. Now that linkify writes to files other than the page being edited, that risk applies to arbitrary source files in the graph.Change
chmodit to the target's mode (the existing file's perms, or0o644for a new file —os.CreateTempis born0o600), thenos.Renameinto place. Rename is atomic against a process crash, so a botched write leaves the original intact rather than truncated.os.Removecleans up any stranded temp on failure.internal/editremains the sole disk-writing surface; both the editor save path and linkify go through here.Notes
os.WriteFiledid not actually reset an existing file's mode (the mode arg only applies on creation) — so the real defect was atomicity. Mode preservation is a new requirement the temp+rename fix introduces, since the temp is born0o600.fsync: rename guarantees atomicity against a process crash (the failure this protects against); durability against power loss is out of scope for a local notes TUI..weft-*.tmptemps carry a.tmpextension so the graph scan (.md-only) never indexes them.Tests
TestWriteFilein arrange/act/assert form with shared helpers.0o640(distinct from both the0o644default and the0o600temp birth mode) so it genuinely guards the chmod step.Closes the
edit.WriteFileperms/atomicity TODO.