atomicfile

package
v0.22.144 Latest Latest
Warning

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

Go to latest
Published: May 2, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package atomicfile — one canonical primitive for "write a file without leaving a half-written artifact on crash". Used by config stores, daemon state, agent identity, a2a inbox, secrets — every place where a partial write at the target path would corrupt downstream consumers.

Strategy: write to a unique temp file in the *same directory* as the target, then rename(2). Same-filesystem rename is atomic on every platform clawtool supports — readers see either the old file or the new file, never a torn intermediate.

We deliberately do not use a third-party "atomic write" library (per the project's design call): stdlib gives us the right guarantees when the temp lives in the target's directory.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteFile

func WriteFile(path string, content []byte, mode os.FileMode) error

WriteFile writes content to path via temp+rename.

mode controls the final file permission. Pass 0 to preserve the existing file's mode (or fall back to 0o644 for a brand-new path).

The caller is responsible for any parent-directory creation — MkdirAll-and-write doubles up too often (caller already knows the scope, e.g. 0o700 for ~/.config dirs vs 0o755 for repo dirs). Use WriteFileMkdir when the parent directory may not exist.

func WriteFileMkdir

func WriteFileMkdir(path string, content []byte, mode os.FileMode, dirMode os.FileMode) error

WriteFileMkdir is WriteFile + MkdirAll(parent, dirMode) up front. Use when callers know the parent directory may be missing (most $XDG_CONFIG_HOME state files on first run).

Types

This section is empty.

Jump to

Keyboard shortcuts

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