atomicfile

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package atomicfile provides atomic file writes via temp-file + rename.

Writes land in a temporary file in the same directory as the target, are fsynced, then atomically renamed into place. Readers never see a partial or truncated file — only the previous complete version or the new complete version.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteFile

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

WriteFile atomically writes data to path with the given permissions. It creates an atomic Writer, copies data in via io.Copy, then fsyncs and renames into place.

Types

type Writer

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

Writer is an io.WriteCloser that writes to a temporary file and atomically renames it to the target path on Close. Call Abort to discard the temp file without touching the target.

func Create

func Create(path string, perm os.FileMode) (*Writer, error)

Create returns a Writer that will atomically replace path on Close. The temp file is created in the same directory as path to guarantee same-filesystem rename semantics.

func (*Writer) Abort

func (w *Writer) Abort() error

Abort discards the temp file without renaming. The target is untouched. Safe to call multiple times or after Close.

func (*Writer) Close

func (w *Writer) Close() error

Close fsyncs the temp file and atomically renames it to the target path. After Close returns successfully, the target contains exactly the data written. On error the temp file is removed and the target is untouched.

func (*Writer) Write

func (w *Writer) Write(p []byte) (int, error)

Write delegates to the underlying temp file.

Jump to

Keyboard shortcuts

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