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 ¶
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.
const BackupStampLayout = "20060102T150405"
BackupStampLayout is the time.Format layout used in backup filenames. UTC is mandatory so backups sort chronologically across timezones.
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 ¶
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 ¶
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.
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 ¶
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 ¶
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.