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 replaces the destination; Abort discards the staged data. Exactly one of them should run, and Abort is a no-op after a successful Commit, so the usual shape is a deferred Abort plus a Commit on the success path.
func Create ¶
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 has run.