atomic

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: BSD-3-Clause Imports: 4 Imported by: ? 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 atomic provides crash-safe atomic file writes at an absolute path.

It is the unrooted counterpart to the root package: acfs.Write confines every write to a workspace root and replaces a symlinked destination, while atomic.WriteFile takes any absolute path and refuses a symlinked destination. The two symlink policies live in separate namespaces on purpose — reach for this package only when there is no natural root to confine the write to.

WriteFile writes to a temporary file in the destination's directory, fsyncs it, applies the requested permissions, and renames it onto the destination -- so a reader never observes a partially written file and a crash mid-write leaves the previous version intact.

WriteFile's temp-file/fsync/chmod/rename sequence and its destination check's refusal to write through a symlink/directory/named pipe/socket/device destination are adapted from moby's atomicwriter package:

Source:  github.com/moby/sys/atomicwriter
License: Apache License 2.0 (https://github.com/moby/sys/blob/main/LICENSE)
File:    https://github.com/moby/sys/blob/main/atomicwriter/atomicwriter.go

This is a reimplementation against only the standard library, not a copy, so this module stays dependency-free. It deliberately simplifies moby's version in a few ways:

  • os.CreateTemp is used instead of moby's sequential.CreateTemp, a Windows standby-list optimization that isn't a correctness requirement here.
  • WriteFile's destination check does not refuse setuid/setgid/sticky-bit destinations, and does not pre-check that the parent directory exists (every caller here already MkdirAll's its target directory).
  • Parent-directory fsync is not performed, matching moby's own behavior: atomic rename already guarantees a reader never sees a torn file, and the extra fsync only narrows the crash-immediately-after-rename window.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteFile

func WriteFile(name string, data []byte, perm os.FileMode) error

WriteFile atomically writes data to name with the given permissions. It is a drop-in, crash-safe replacement for os.WriteFile: on success the destination either has its previous content or the new content in full, never a partial write.

Types

This section is empty.

Jump to

Keyboard shortcuts

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