atomicfile

package
v0.270.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 5 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 a file by staging it next to its destination and renaming it into place only once the caller is satisfied with the contents.

Commands that write a result file (`tuples read --output-file`, `model test --report-file`) used to open the destination with O_TRUNC before the work that produces the content had even started. That makes the destination a casualty of any later failure: pointing an export at an existing file and then losing the connection destroyed the previous contents, and a failure partway through left a truncated file that looks complete. Staging means the destination keeps its old contents until a complete result is ready to replace them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type File

type File struct {
	// contains filtered or unexported fields
}

File is an io.Writer that accumulates into a temporary file in the destination's directory — the same filesystem, so the final rename is atomic. Commit (or CommitNew) writes the destination; Abort discards the staged data. Exactly one of them should run, and Abort is a no-op after a successful Commit or CommitNew, so the usual shape is a deferred Abort plus a Commit on the success path.

func Create

func Create(dest string, perm os.FileMode) (*File, error)

Create stages a replacement for dest. The destination's directory must already exist — staging deliberately does not create it, so a mistyped path still fails instead of quietly materialising a tree. perm is applied to the finished file, overriding the 0600 that os.CreateTemp uses while staging.

func (*File) Abort

func (f *File) Abort()

Abort discards the staged file, leaving the destination untouched. It is a no-op once Commit or CommitNew has run.

func (*File) Commit

func (f *File) Commit() error

Commit flushes the staged file and renames it over the destination. The flush is what makes a full disk (ENOSPC, EDQUOT, a deferred NFS commit) surface as an error here instead of silently leaving a short file behind.

func (*File) CommitNew added in v0.268.0

func (f *File) CommitNew() error

CommitNew is Commit for a destination that must not exist: it links the staged file into place, so a file that appears at dest after staging began is never replaced. That case fails with an error wrapping fs.ErrExist and leaves dest untouched. A filesystem that cannot hard link (any link error other than fs.ErrExist) instead reserves dest with an exclusive create, which fails the same way if anything is there, and renames over the reservation: dest is briefly empty, but never someone else's file. On failure the staged file is removed; after a successful link a failed remove is ignored, because the destination is complete.

func (*File) Write

func (f *File) Write(p []byte) (int, error)

Write appends to the staged file.

Jump to

Keyboard shortcuts

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