atomicwrite

package
v0.59.1 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 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 atomicwrite stages a file's new contents beside it and puts them in place only once the whole write has succeeded.

Every write in this module goes through it, because opening a destination with os.Create truncates it before a single byte has been produced and a write can still fail after that. The ACH and Fedwire encoders validate while they encode, so a value the format cannot represent is rejected partway through; the TSV writer refuses a value holding a tab or a newline for the same reason, since TSV has no quoting; and any encoder can fail on a full disk. For an in-place save the destination is the caller's own source file, so a rejected write destroyed the data it was saving. Staging means a failure costs nothing.

Index

Constants

View Source
const DefaultFileMode os.FileMode = 0o644

DefaultFileMode is the permission given to a file this package creates. An existing destination keeps its own mode instead.

Variables

This section is empty.

Functions

func DescribeFileMode added in v0.53.0

func DescribeFileMode(mode os.FileMode) string

DescribeFileMode names the kind of file a mode says it is, for an error a caller reads without knowing Go's mode bits. It is exported so that a write refusing a destination and a load refusing a source name a kind the same way.

func Write

func Write(dest string, write func(io.Writer) error, opt Options) error

Write hands write a writer for a temporary file in dest's directory and renames it over dest only when write and the close both succeed. When either fails, dest is left exactly as it was and the temporary file is removed.

It is Stage followed by Commit, which is the whole of what a single file needs. A caller replacing several files together stages them all first.

Types

type Options

type Options struct {
	// FailIO words a failure of the staging machinery. what is a formatted
	// phrase naming the step ("failed to replace /tmp/out.csv"); err is what
	// the operating system reported. The caller's own write error never
	// reaches it, so a package that marks I/O failures with a sentinel does
	// not put that sentinel on an error the encoder produced.
	FailIO func(what string, err error) error

	// FailCleanup attaches a failure to remove the staged file to the error
	// the write is already returning. primary may be nil, which is the case
	// where the write succeeded and only the cleanup did not.
	FailCleanup func(primary error, what string, err error) error
}

Options say how the calling package words a failure of the staging itself. Both fields are optional: a zero Options reports plain errors, which is what a caller with no sentinel of its own would get. No caller takes that path today.

type Staged added in v0.55.0

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

Staged is a destination's new contents, written and waiting beside it. It is what lets a group of files be produced before any of them is put in place, so a write refused partway through a set costs what it costs for one file: nothing.

Either Commit or Discard has to be called. Until one of them is, the staged file sits in the destination's directory.

func Stage added in v0.55.0

func Stage(dest string, write func(io.Writer) error, opt Options) (_ *Staged, err error)

Stage writes dest's new contents into a temporary file beside it and stops there. Nothing at dest has been touched when Stage returns.

Why the writer and not the staged path is what write receives: the staged name carries a temporary suffix, so a format that reads anything out of its file name gets the wrong answer from it. Excel picks both its container format and its sheet name that way, which is how a staged XLSX dump came out unreadable.

The temporary file is created in dest's directory so the rename stays within one filesystem, where it is atomic. An existing dest keeps its permissions; a new file is created with DefaultFileMode.

A dest that is a symbolic link names the file to replace rather than being it, so the staging and the rename both move to the file it points at and the link stays a link.

func (*Staged) Commit added in v0.55.0

func (s *Staged) Commit() (err error)

Commit puts the staged contents at the destination. A failure leaves the destination as it was and removes the staged file.

func (*Staged) Discard added in v0.55.0

func (s *Staged) Discard() error

Discard throws the staged contents away, leaving the destination as it was.

Jump to

Keyboard shortcuts

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