Skip to content

Atomic file writes in Go on Linux, macOS, and Windows.

License

Notifications You must be signed in to change notification settings

sashka/atomicfile

Repository files navigation

atomicfile

Package atomicfile provides *os.File wrapper to allow atomic file writes. Works on Linux, macOS, and Windows.

All writes will go to a temporary file. Call Close() explicitly when you are done writing to atomically rename the file making the changes visible. Call Abort() to discard all your writes.

This allows for a file to always be in a consistent state and never represent an in-progress write.

Installation

Standard go get:

$ go get github.com/sashka/atomicfile

Example

import "github.com/sashka/atomicfile"

// Prepare to write a file.
f, err := atomicfile.New(path, 0o666)

// It's safe to call f.Abort() on a successfully closed file.
// Otherwise it's correct to discard the file changes.
defer f.Abort()

// Update the file.
if _, err := f.Write(content); err != nil {
    return err
}

// Make changes visible.
f.Close()

About

Atomic file writes in Go on Linux, macOS, and Windows.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages