safe

package
v1.113.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MPL-2.0 Imports: 5 Imported by: 6

Documentation

Overview

Package safe provides safe, atomic saving of files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteFile

func WriteFile(filename string, writer func(io.Writer) error) (err error)

WriteFile uses writer to write data safely and atomically to a file.

func WriteFileWithMode added in v1.40.0

func WriteFileWithMode(filename string, writer func(io.Writer) error, mode os.FileMode) (err error)

WriteFileWithMode uses writer to write data safely and atomically to a file.

Types

type File

type File struct {
	*os.File
	// contains filtered or unexported fields
}

File provides safe, atomic saving of files. Instead of truncating and overwriting the destination file, it creates a temporary file in the same directory, writes to it, and then renames the temporary file to the original name when Commit() is called. If Close() is called without calling Commit(), or the Commit() fails, then the original file is left untouched.

func Create

func Create(filename string) (*File, error)

Create creates a temporary file in the same directory as filename, which will be renamed to the given filename when calling Commit.

func CreateWithMode

func CreateWithMode(filename string, mode os.FileMode) (*File, error)

CreateWithMode creates a temporary file in the same directory as filename, which will be renamed to the given filename when calling Commit.

func (*File) Close

func (f *File) Close() error

Close the temporary file and remove it, if it hasn't already been committed. If it has been committed, nothing happens.

func (*File) Commit

func (f *File) Commit() error

Commit the data into the original file and remove the temporary file from disk. Close() may still be called, but will do nothing.

func (*File) OriginalName

func (f *File) OriginalName() string

OriginalName returns the original filename passed into Create().

Jump to

Keyboard shortcuts

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