atomic

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2023 License: MIT Imports: 5 Imported by: 6

README

atomic

Go package for atomic file writing

Go Reference

By default, writing to a file in go (and generally any language) can fail partway through... you then have a partially written file, which probably was truncated when the write began, and bam, now you've lost data.

This go package avoids this problem, by writing first to a temp file, and then overwriting the target file in an atomic way. This is easy on linux, os.Rename just is atomic. However, on Windows, os.Rename is not atomic, and so bad things can happen. By wrapping the windows API moveFileEx, we can ensure that the move is atomic, and we can be safe in knowing that either the move succeeds entirely, or neither file will be modified.

func ReplaceFile

func ReplaceFile(source, destination string) error

ReplaceFile atomically replaces the destination file or directory with the source. It is guaranteed to either replace the target file entirely, or not change either file.

func WriteFile

func WriteFile(filename string, r io.Reader, opts ...Option) (err error)

WriteFile atomically writes the contents of r to the specified filepath. If an error occurs, the target file is guaranteed to be either fully written, or not written at all. WriteFile overwrites any file that exists at the location (but only if the write fully succeeds, otherwise the existing file is unmodified). Additional option arguments can be used to change the default configuration for the target file.

Example

import (
	"strings"

	"github.com/sdassow/atomic"
)

func main() {
	r := strings.NewReader("yes\n")
	err := atomic.WriteFile("consistent.txt", r, atomic.FileMode(0440))
	if err != nil {
		// handle error
	}
}

Documentation

Overview

package atomic provides functions to atomically change files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReplaceFile

func ReplaceFile(source, destination string) error

ReplaceFile atomically replaces the destination file or directory with the source. It is guaranteed to either replace the target file entirely, or not change either file.

func WriteFile

func WriteFile(filename string, r io.Reader, opts ...Option) (err error)

WriteFile atomically writes the contents of r to the specified filepath. If an error occurs, the target file is guaranteed to be either fully written, or not written at all. WriteFile overwrites any file that exists at the location (but only if the write fully succeeds, otherwise the existing file is unmodified). Additional option arguments can be used to change the default configuration for the target file.

Types

type FileOptions

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

FileOptions define the behaviour of `FileWrite()`.

type Option

type Option func(*FileOptions)

Option functions modify FileOptions.

func DefaultFileMode

func DefaultFileMode(mode os.FileMode) Option

DefaultFileMode sets the default file mode instead of using the `ioutil.TempFile()` default of `0600`.

func FileMode

func FileMode(mode os.FileMode) Option

FileMode sets the file mode to the desired value and has precedence over all other options.

func KeepFileMode

func KeepFileMode(keep bool) Option

KeepFileMode preserves the file mode of an existing file instead of using the default value.

Jump to

Keyboard shortcuts

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