Documentation
¶
Overview ¶
Package atomicfile replaces a file so that a concurrent reader (another celeste process, a new lane's setup) sees either the old content or the new, never a half-written file, and a crash never leaves a truncated one.
Index ¶
- func Replace(path string, data []byte, perm os.FileMode) error
- func ReplaceIn(root *os.Root, rel string, data []byte, defaultPerm os.FileMode) (err error)
- func ReplaceKeepMode(path string, data []byte, defaultPerm os.FileMode) error
- func Write(path string, data []byte, perm os.FileMode) error
- func WriteKeepMode(path string, data []byte, defaultPerm os.FileMode) error
- type TempError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Replace ¶
Replace is Write without following symlinks: the temp file is renamed over path itself, so a symlink there, even one swapped in after the caller checked, is replaced by a regular file with mode perm instead of written through.
func ReplaceIn ¶
ReplaceIn is ReplaceKeepMode for rel inside root: the temp file is created next to rel and renamed over it through root, so no symlink or ".." in any component, swapped in at any time, can take the write out of root's directory. A symlink at rel itself is replaced, not followed. A regular file at rel keeps its mode; anything else gets defaultPerm.
func ReplaceKeepMode ¶
ReplaceKeepMode is WriteKeepMode without following symlinks: the rename lands on path itself, so a symlink there is replaced by a regular file instead of written through. Callers pass a path they already resolved and checked (the edit tools, 2.0 W4), so a symlink swapped in after that check cannot redirect the write. A regular file at path keeps its mode; anything else gets defaultPerm.
func Write ¶
Write replaces path with data and gives the file mode perm, exactly: the umask does not apply, unlike os.WriteFile on a new file. It writes a temp file in the target's directory, syncs it, closes it and renames it over the target, retrying the rename briefly (see renameAttempts).
path is resolved through any symlinks first (dotfile managers commonly symlink ~/.celeste files to ones they track elsewhere): the temp file is created next to, and the rename lands on, the real target, so the symlink itself survives. The directory must already exist.