...

Package atomicfile

import "github.com/sassoftware/relic/lib/atomicfile"
Overview
Index

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.

func WriteFile

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

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

type AtomicFile

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()

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
}

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.