atomicfile

package
v0.116.3 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteBytes added in v0.101.0

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

WriteBytes atomically writes raw bytes to path using a unique temp file in the same directory + rename. A concurrent reader sees either the prior file (or ENOENT) or the new file in full — never a partially-written truncation. Two concurrent WriteBytes calls on the same path each get a distinct tmp via os.CreateTemp, so neither can clobber the other's tmp before rename — only the rename itself races, and the loser's content is still atomically visible to any reader that catches the file between the two renames.

perm applies after rename. CreateTemp opens with 0600; we Chmod before rename so the final inode lands with the requested perm regardless of the OS's umask handling.

func WriteJSON

func WriteJSON(path string, data any, perm os.FileMode) error

WriteJSON atomically writes data as indented JSON to path using a temp file + rename.

func WriteWith added in v0.103.0

func WriteWith(path string, perm os.FileMode, write func(io.Writer) error) error

WriteWith atomically writes to path by invoking write against a unique temp file in the same directory, then rename. It shares the temp+rename core with WriteBytes: the write callback streams the payload (avoiding a full in-memory buffer for large encoders), perm is applied via Chmod before rename, and any failure removes the temp file. See WriteBytes for the concurrency guarantees.

Types

This section is empty.

Jump to

Keyboard shortcuts

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