atomicfile

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package atomicfile provides the write-temp + os.Link publish-if-absent primitive used by boid's "load or generate and persist" secret files, so two daemon instances racing to boot against the same fresh data dir can never clobber each other's write (see docs/plans/volume-only-daemon.md §論点 d).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PublishIfAbsent

func PublishIfAbsent(path string, perm os.FileMode, content []byte) ([]byte, error)

PublishIfAbsent atomically publishes content to path if path does not already hold legitimate content, returning the winning content — this call's own content if it won the publish race, or whatever a concurrent (or earlier) winner already published if it lost. path's parent directory must already exist.

This is publish-if-absent plus a safe read-back only; there is no repair path. An empty existing file, or any read-back error, is reported to the caller rather than acted on — every caller treats the files this publishes as volatile/regenerable, so failing and asking the operator to remove the stale artifact is an acceptable default.

No crash-durability guarantee is made: Write+Close+Link can all report success while content still only lives in page cache, so a crash before writeback can still leave path short on the next boot.

func WriteAtomic

func WriteAtomic(path string, perm os.FileMode, content []byte) error

WriteAtomic atomically (over)writes content to path — write-temp + os.Rename, unconditional overwrite — for content that is always "the latest, correct content" rather than a value that must only ever be generated once (PublishIfAbsent's use case). Unlike a bare os.WriteFile, which truncates path in place, this guarantees any reader always sees either the complete old content or the complete new content, never a partial mix. path's parent directory must already exist.

Types

This section is empty.

Jump to

Keyboard shortcuts

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