fsutil

package
v0.10.2 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 9 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package fsutil holds file system helpers that behave the same on every platform.

Index

Constants

View Source
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

func Canonical(path string) string

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

func MkdirAll(path string, perm fs.FileMode) error

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

func Rename(oldpath, newpath string) error

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

func WriteFileAtomic(path string, data []byte, perm os.FileMode) error

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

func WriteFileAtomicWith(path string, data []byte, perm os.FileMode, opts AtomicOptions) error

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.

Jump to

Keyboard shortcuts

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