safeio

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2023 License: MIT Imports: 5 Imported by: 19

README

Safe I/O

Provides functions to perform atomic, fsync-safe disk operations.

Build Status

Documentation

Overview

Package safeio provides functions to perform atomic, fsync-safe disk operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Remove

func Remove(fn string) error

Remove is just like os.Remove, except this also calls sync on the parent directory.

func Rename

func Rename(oldname, newname string) error

Rename renames the file using os.Rename and fsyncs the NEW parent directory. It should only be used if both oldname and newname are in the same directory.

func WriteToFile

func WriteToFile(src io.Reader, path string, perm os.FileMode) (written int64, err error)

WriteToFile consumes the provided io.Reader and writes it to a temp file in the provided directory.

Types

type File

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

File is an implementation detail of OpenFile.

func OpenFile

func OpenFile(path string, perm os.FileMode) (*File, error)

OpenFile is the incremental version of WriteToFile. It opens a temp file and proxies writes through to the underlying file.

If Close is called before Commit, the temp file is closed and erased.

If Commit is called before Close, the temp file is closed, fsynced, and atomically renamed to the desired final name.

func (*File) Close

func (f *File) Close() error

Close closes the current file and erases it, unless Commit was previously called. In that case it does nothing.

Close is idempotent.

After Close is called, Write and Commit will fail.

func (*File) Commit

func (f *File) Commit() error

Commit causes the current temp file to be safely persisted to disk and atomically renamed to the desired final filename.

It is safe to call Close after commit, so you can defer Close as usual without worries about write-safey.

func (*File) Write

func (f *File) Write(p []byte) (n int, err error)

Write is a thin proxy to *os.File#Write.

If Close or Commit were called, this immediately exits with an error.

Jump to

Keyboard shortcuts

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