fsutil

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteFileAtomic

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

WriteFileAtomic writes data to path with perm, using tempfile + fsync + rename so a concurrent reader never sees a truncated file and a crash mid-write never leaves a torn file on disk.

Parent directories must already exist — callers that want MkdirAll should call it themselves, to keep this helper one syscall wide.

The tempfile is created in the same directory as path (same filesystem = rename is atomic on POSIX) with prefix derived from filepath.Base(path). On any error after tempfile creation, the tempfile is removed best-effort.

func WriteFileAtomicStream

func WriteFileAtomicStream(path string, perm os.FileMode, write func(io.Writer) error) error

WriteFileAtomicStream is like WriteFileAtomic but streams into a caller-provided callback. Used for gob/json encoders that want to write directly into the tempfile without materialising the full payload.

err := WriteFileAtomicStream(path, perm, func(w io.Writer) error {
    return gob.NewEncoder(w).Encode(v)
})

The writer is buffered; flush is handled by the helper. fsync runs after flush and before rename.

Types

This section is empty.

Jump to

Keyboard shortcuts

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