atomicfile

package
v1.19.1 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: Apache-2.0 Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package atomicfile writes files using a temp-file + rename discipline so readers never observe a half-written state.

Atomic-write order: create temp in target dir → write → fsync → close → chmod → rename. Any existing file at the target is copied to a sibling backup (`<path>.dmg-<UTC stamp>.bak`) before the rename.

Ownership is intentionally NOT this package's concern. Under root install, the caller (the install handler) chowns the result to the console user — WriteResult exposes every path we wrote or created so the caller has the full set without having to walk the filesystem.

The `Restore`, `RestoreOptions`, `RestoreResult`, and `ListBackups` operations are omitted — `hooks restore` is not in scope.

Index

Constants

View Source
const (
	BackupPrefix = ".dmg-"
	BackupExt    = ".bak"
)

BackupPrefix is the literal between the original path and the timestamp on backup files. BackupExt is the trailing extension. Together they produce: `<path>.dmg-<UTC stamp>.bak`. The `.bak` ending is the conventional backup marker most editors and gitignore templates already recognize; the `dmg-` token identifies the file as ours.

View Source
const BackupStampLayout = "20060102T150405"

BackupStampLayout is the time.Format layout used in backup filenames. UTC is mandatory so backups sort chronologically across timezones.

View Source
const MaxBackups = 3

MaxBackups caps per-target backup retention. After TakeBackup creates a new backup, older DMG-owned backups for the same target are deleted so at most MaxBackups remain (newest by mtime). Both the current `<path>.dmg-<stamp>.bak` form and the legacy `<path>.dmg-backup.<stamp>` form count toward the same cap so the rotation gradually cleans up files left from before the rename.

Variables

View Source
var ErrRename = errors.New("atomicfile: rename")

ErrRename marks a failure of the final rename: the target is untouched and the new bytes were written successfully to the temp file.

Functions

func PickMode

func PickMode(path string, fallback os.FileMode) os.FileMode

PickMode returns the existing file's permission bits, or fallback if the file does not exist. Used so reinstalls preserve a user-tightened mode instead of clobbering with the default.

func Replace added in v1.18.0

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

Replace writes data to path atomically like WriteAtomic, but without taking a backup of the existing file.

func TakeBackup

func TakeBackup(path string, now time.Time) (string, error)

TakeBackup copies the existing file at path to a sibling `<path>.dmg-<stamp>.bak`. Returns "" with nil error if the source does not exist (the common first-install case).

Types

type WriteResult

type WriteResult struct {
	Path        string   // the target file we wrote
	BackupPath  string   // "" when no pre-existing file was backed up
	CreatedDirs []string // every parent dir we mkdir'd (deepest last); empty if all parents existed
}

WriteResult reports every path WriteAtomic touched. The install handler uses CreatedDirs + Path + BackupPath to chown new files under root.

func InstallBytes

func InstallBytes(path string, data []byte, mode os.FileMode) (WriteResult, error)

InstallBytes is a thin alias used at install sites to make intent clear: "install these bytes at this path." Implementation is identical to WriteAtomic.

func WriteAtomic

func WriteAtomic(path string, data []byte, mode os.FileMode) (WriteResult, error)

WriteAtomic writes data to path atomically. Parent directories are created (and reported) as needed; any existing file is backed up first.

The temp file lives in the target directory (same filesystem) so the final rename is atomic on POSIX.

Jump to

Keyboard shortcuts

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