Documentation
¶
Overview ¶
Package fsutil holds file system helpers that behave the same on every platform.
Index ¶
- Constants
- func Canonical(path string) string
- func MkdirAll(path string, perm fs.FileMode) error
- func Rename(oldpath, newpath string) error
- func WriteFileAtomic(path string, data []byte, perm os.FileMode) error
- func WriteFileAtomicWith(path string, data []byte, perm os.FileMode, opts AtomicOptions) error
- type AtomicOptions
Constants ¶
const TempPrefix = ".enbu-tmp-"
TempPrefix starts the name of every temporary file WriteFileAtomic creates.
Variables ¶
This section is empty.
Functions ¶
func Canonical ¶ added in v0.10.0
Canonical returns one spelling of path, so that the same folder reached from the CLI, a folder picker and git compares equal: absolute and clean, with symbolic links resolved when it exists (on Windows that also gives the on-disk letter case and long names instead of 8.3 short names). On macOS, whose file systems match a name in any Unicode normalization, it is NFC, the form git (core.precomposeUnicode) and typed input use, while a picker or an archive may hand over NFD. The result still opens the same file.
func MkdirAll ¶ added in v0.10.0
MkdirAll creates directories and syncs each new directory and its parent before returning. Callers can safely persist a reference to files inside it.
func Rename ¶
Rename renames oldpath to newpath, replacing newpath if it exists. On Windows a replace fails while another process, such as an antivirus scanner, the search indexer or another enbu, has newpath open without sharing delete access. Those failures pass on their own, so Rename retries them for a short while instead of failing the whole operation.
On Unix the rename is then made durable: the directories that hold the old and new names are synced, without which ext4, XFS and btrfs may lose the rename, and with it a new identity or checkpoint, on a power failure. The sync is best effort: the rename has happened by then, and reporting it as failed would make callers retry or roll back a change that is in place.
func WriteFileAtomic ¶ added in v0.10.0
WriteFileAtomic replaces path with data through a synced temporary file in the same directory, which then gets mode perm. A failure leaves the old file intact, and a reader sees either the old content or the new, never part. The directory must exist.
func WriteFileAtomicWith ¶ added in v0.10.0
WriteFileAtomicWith is WriteFileAtomic with the steps of opts.
Types ¶
type AtomicOptions ¶ added in v0.10.0
type AtomicOptions struct {
// Prepare runs on the temporary file before its mode is set and data is
// written, for example to give it the owner of the file it replaces.
Prepare func(*os.File) error
// BeforeRename runs once data is synced and the file closed. An error
// leaves path untouched.
BeforeRename func() error
// NoSync skips syncing the file and its directory, for a cache that is
// rebuilt when lost: a sync costs milliseconds on Windows, and caches are
// written often. A reader still sees the old content or the new, never part.
NoSync bool
}
AtomicOptions adjusts WriteFileAtomicWith.