Help us shape the future of Go! Take the 2026 Go Developer Survey

atomicfile

package
v2.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 7 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Replace

func Replace(path string, data []byte, perm os.FileMode) error

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

func ReplaceIn(root *os.Root, rel string, data []byte, defaultPerm os.FileMode) (err error)

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

func ReplaceKeepMode(path string, data []byte, defaultPerm os.FileMode) error

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

func Write(path string, data []byte, perm os.FileMode) error

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.

func WriteKeepMode

func WriteKeepMode(path string, data []byte, defaultPerm os.FileMode) error

WriteKeepMode is Write that keeps an existing file's mode (following symlinks); a new file gets defaultPerm.

Types

type TempError

type TempError struct{ Err error }

TempError is Write's error when it could not create its temporary file next to the target — typically a directory the process cannot write, though the file itself may be writable. Nothing was changed. It reads as the underlying error.

func (*TempError) Error

func (e *TempError) Error() string

func (*TempError) Unwrap

func (e *TempError) Unwrap() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL