atomicfile

package
v0.11.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0, MIT Imports: 4 Imported by: 0

Documentation

Overview

Package atomicfile provides crash-safe file writes via the temp-then-rename pattern. A bare os.WriteFile can leave a truncated or zero-byte file on the disk if the process crashes (or the host loses power) mid-write — the next boot then loads the corrupted file and silently drops whatever was in it. Across the project this hit real persisted state: alerts.json dropped pending alerts, the flight recorder dropped its tail, personality/soul.md left an empty file that re-triggered the cold-start flow.

WriteFile is the drop-in replacement: same signature as os.WriteFile, same semantics on success, but the on-disk file is either the OLD contents or the FULL new contents — never a half-written truncation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteFile

func WriteFile(path string, data []byte, perm fs.FileMode) error

WriteFile writes data to path atomically. The temp file is created in the same directory as path so the final rename stays on the same filesystem (rename across filesystems is not atomic on Linux).

Permissions are applied to the temp file before rename, so the final file inherits perm and never has a wider transient mode. The temp file is fsync'd before rename so a crash after rename cannot expose an empty file caused by buffer reordering.

On any error after temp file creation, the temp is removed best-effort. Callers see a single error; the on-disk state reflects the prior file (or absence) exactly.

Types

This section is empty.

Jump to

Keyboard shortcuts

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