safe

package
v14.3.5 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyDone = errors.New("safe file was already committed or closed")

ErrAlreadyDone is returned when the safe file has already been closed or committed

Functions

This section is empty.

Types

type FileWriter

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

FileWriter is a thread safe writer that does an atomic write to the target file. It allows one writer at a time to acquire a lock, write the file, and atomically replace the contents of the target file.

func NewFileWriter added in v14.3.0

func NewFileWriter(path string, optionalCfg ...FileWriterConfig) (*FileWriter, error)

NewFileWriter takes path as an absolute path of the target file and creates a new FileWriter by attempting to create a tempfile. This function either takes no FileWriterConfig or exactly one.

func (*FileWriter) Close

func (fw *FileWriter) Close() error

Close will close and remove the temp file artifact if it exists. If the file was already committed, an ErrAlreadyClosed error will be returned and no changes will be made to the filesystem.

func (*FileWriter) Commit

func (fw *FileWriter) Commit() error

Commit will close the temporary file and rename it to the target file name the first call to Commit() will close and delete the temporary file, so subsequently calls to Commit() are gauaranteed to return an error.

func (*FileWriter) Write

func (fw *FileWriter) Write(p []byte) (n int, err error)

Write wraps the temporary file's Write.

type FileWriterConfig added in v14.3.0

type FileWriterConfig struct {
	// FileMode is the desired file mode of the committed target file. If left at its default
	// value, then no file mode will be explicitly set for the file.
	FileMode os.FileMode
}

FileWriterConfig contains configuration for the `NewFileWriter()` function.

type LockingFileWriter added in v14.3.0

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

LockingFileWriter is a FileWriter which locks the target file on commit and checks whether it has been modified since the LockingFileWriter has been created. The user must first create a new LockingFileWriter via `NewLockingFileWriter()`, at which point it is open for writes. The writer must be `Lock()`ed before `Commit()`ting changes.

func NewLockingFileWriter added in v14.3.0

func NewLockingFileWriter(path string, optionalCfg ...LockingFileWriterConfig) (*LockingFileWriter, error)

NewLockingFileWriter creates a new LockingFileWriter for the given path. At creation, it stats the target file and caches its current size and last modification time such that it can compare on commit whether the file has changed.

func (*LockingFileWriter) Close added in v14.3.0

func (fw *LockingFileWriter) Close() error

Close closes the FileWriter and removes any locks and temporary files without updating the target file. Does nothing if the file has already been closed.

func (*LockingFileWriter) Commit added in v14.3.0

func (fw *LockingFileWriter) Commit() (returnedErr error)

Commit writes whatever has been written to the Filewriter to the target file if and only if the target file has not been modified meanwhile. The writer must be `Lock()`ed first. The writer will be closed after this call, with all locks and temporary files having been removed.

func (*LockingFileWriter) Lock added in v14.3.0

func (fw *LockingFileWriter) Lock() error

Lock locks the file writer such that no other process can concurrently update the same file. Must be called on an open LockingFileWriter.

func (*LockingFileWriter) Path added in v14.3.0

func (fw *LockingFileWriter) Path() string

Path returns the path of the intermediate file the FileWriter is writing to. Exposing the path allows an external process to write to the file directly. While it would be preferable to use the io.Writer interface instead, this is not easily doable e.g. for Git processes.

func (*LockingFileWriter) Write added in v14.3.0

func (fw *LockingFileWriter) Write(p []byte) (int, error)

Write writes to the FileWriter. Must be called on an open LockingFileWriter.

type LockingFileWriterConfig added in v14.3.0

type LockingFileWriterConfig struct {
	// FileWriterConfig is the configuration for the embedded FileWriter.
	FileWriterConfig
	// SeedContents will seed the FileWriter's file with contents of the target file if
	// set. If the target file does not exist, then the file remains empty.
	SeedContents bool
}

LockingFileWriterConfig contains configuration for the `NewLockingFileWriter()` function.

Jump to

Keyboard shortcuts

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