atomicfile

package
v7.2.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2019 License: Apache-2.0 Imports: 6 Imported by: 28

Documentation

Overview

Implement atomic write-rename file pattern. Instead of opening the named file it creates a temporary file next to it, then on Commit() renames it. If the file is Close()d before Commit() then it is unlinked instead.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteFile

func WriteFile(path string, data []byte) error

Write bytes to a file, using write-rename when appropriate

Types

type AtomicFile

type AtomicFile interface {
	io.Reader
	io.ReaderAt
	io.Writer
	io.WriterAt
	io.Seeker
	Truncate(size int64) error

	// Close and unlink the underlying file object, discarding the contents.
	// No-op if Close() or Commit() was already called.
	io.Closer
	// Get the underlying *File object
	GetFile() *os.File
	// Complete the write-rename pattern and close the file
	Commit() error
}

File-like interface used by several functions in this package. Some of them may open a file or stdio directly without atomic semantics, in which case Commit() is an alias for Close()

func New

func New(name string) (AtomicFile, error)

Open a temporary file for reading and writing which will ultimately be renamed to the given name when Commit() is called.

func WriteAny

func WriteAny(path string) (AtomicFile, error)

Pick the best strategy for writing to the given path. Pipes and devices will be written to directly, otherwise write-rename.

func WriteInPlace

func WriteInPlace(src *os.File, dest string) (AtomicFile, error)

If src and dest are the same, use src for reading and writing. If they are different, make a copy and open the destination as an atomicfile, after which src will be closed.

Jump to

Keyboard shortcuts

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