Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrAlreadyDone = errors.New("safe file was already committed or closed")
ErrAlreadyDone is returned when the safe file has already been closed or committed
var ErrFileAlreadyLocked = errors.New("file already locked")
ErrFileAlreadyLocked is returned when trying to lock a file which has already been locked by another concurrent process.
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 ¶
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.
type FileWriterConfig ¶
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 LockingDirectory ¶
type LockingDirectory struct {
// contains filtered or unexported fields
}
LockingDirectory allows locking and unlocking a directory for safe access and modification.
func NewLockingDirectory ¶
func NewLockingDirectory(path, name string) (*LockingDirectory, error)
NewLockingDirectory creates a new LockingDirectory.
func (*LockingDirectory) IsLocked ¶
func (ld *LockingDirectory) IsLocked() bool
IsLocked returns whether or not the directory has been locked.
func (*LockingDirectory) Lock ¶
func (ld *LockingDirectory) Lock() error
Lock locks the directory and prevents a second process with a LockingDirectory from also locking the directory.
func (*LockingDirectory) Unlock ¶
func (ld *LockingDirectory) Unlock() error
Unlock unlocks the directory.
type LockingFileWriter ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
type LockingFileWriterConfig ¶
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.
type Syncer ¶
type Syncer struct {
// contains filtered or unexported fields
}
Syncer implements helper methods for fsyncing files and directories.
func (Syncer) Sync ¶
Sync opens the file/directory at the given path and syncs it. Syncing a directory syncs just the directory itself, not the children. If the children need to be synced as well, use SyncRecursive.
func (Syncer) SyncHierarchy ¶
SyncHierarchy walks the hierarchy from <rootPath>/<relativePath> up to the rootPath and syncs everything on the way. If the path points to a directory, it only syncs from the directory and upwards, not the contents of the directory.
func (Syncer) SyncParent ¶
SyncParent syncs the parent directory of the given path. The path is cleaned prior to determining the parent
func (Syncer) SyncRecursive ¶
SyncRecursive walks the file tree rooted at path and fsyncing the root and the children.