Documentation
¶
Overview ¶
Package fsutil holds the file-system helpers that more than one package needs. It imports only the standard library, so any package may use it.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WriteFileAtomic ¶
WriteFileAtomic replaces the file at path with data. The bytes go to a temp file in path's directory, which is created with mode and chmodded to it, so the umask cannot change it and it is never wider than mode. The temp is synced, closed and renamed over path, so a reader sees either the old file or the new one. The temp file is removed on every error path. The caller resolves symlinks and creates the directory; this function only writes.
Types ¶
type Options ¶
type Options struct {
// Pattern names the temp file as os.CreateTemp does: the last "*" is
// replaced by a random string, and a pattern without one gets it appended.
Pattern string
// Guard, when set, runs before the temp file is created and returns the
// func that releases it. The guard, AfterCreate and Track all run inside
// it, so a caller's lock makes "temp exists" and "temp registered" one
// step to any other holder. Guard is released exactly once, before any
// byte is written.
Guard func() (release func())
// AfterCreate, when set, runs once the temp file exists, under the guard,
// before Track. It is the window a test hooks.
AfterCreate func(tmp string)
// Track, when set, runs under the guard once the temp file exists and before
// any byte is written, so a caller can record it for a signal handler. It
// returns the func to call once the temp file is gone, renamed or removed.
// An error refuses the write, and the temp file is removed.
Track func(tmp string) (done func(), err error)
// BeforeRename, when set, runs after the temp file is closed and before it
// is renamed over the target. It is the window a test can hook.
BeforeRename func()
}
Options tunes one WriteFileAtomic call. The zero value writes a temp file named by a generic pattern with no tracking and no hook.