Help us shape the future of Go! Take the 2026 Go Developer Survey

fsutil

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package fsutil provides small filesystem helpers shared across packages.

Index

Constants

View Source
const (
	OpenNonblock = syscall.O_NONBLOCK
	OpenNoFollow = syscall.O_NOFOLLOW
)

Open flags for reading a file a path may name a FIFO or a symlink in place of the regular file expected there. OpenNonblock keeps opening a FIFO with no writer from waiting forever (it has no effect on a regular file), and OpenNoFollow makes opening a symlink fail instead of following it.

Variables

View Source
var ErrNotRegularFile = errors.New("not a regular file")

ErrNotRegularFile is returned by AtomicReplaceFile when path exists and is not a regular file: a symlink, a directory or a special file.

Functions

func AtomicReplaceFile added in v1.1.0

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

AtomicReplaceFile is AtomicWriteFile for a path that must not be redirected: it never follows a symlink at path. It writes data to path atomically, as AtomicWriteFile does, when path does not exist or is a regular file (keeping its permission bits). It returns an error wrapping ErrNotRegularFile, without writing anything, when path is a symlink, directory or special file, and otherwise the errors of AtomicWriteFile.

func AtomicWriteFile

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

AtomicWriteFile writes data to path using a write-to-temp-then-rename pattern in path's directory: readers see either the old content or the new one, never a half-written file, and the data and the rename are flushed to disk before it returns, so a crash or power loss leaves one of the two versions in place.

path is the destination file, data its full new content, and perm the mode for a newly created file. A path that is a symlink is written through to its target, keeping the link; a dangling symlink, whose target doesn't exist, is replaced by a regular file instead. An existing file keeps its permission bits; perm only applies to a newly created file, and is applied exactly (not masked by the umask). Missing parent directories are created private to the user (0700).

It returns an error when the path cannot be resolved, the directory or temporary file cannot be created, or writing, syncing, closing, chmod or renaming fails; the temporary file is removed in every failure case. A failure to sync the directory after the rename is ignored.

func Rename added in v1.2.0

func Rename(oldpath, newpath string) error

Rename renames oldpath to newpath like os.Rename. On Windows, a rename blocked by another process holding either file open (an antivirus scan, a reader) is retried for up to a second.

func SHA256File added in v1.2.0

func SHA256File(path string) (string, error)

SHA256File returns the lowercase hex SHA-256 digest of the file at path, or an error if it cannot be read.

func SyncDir added in v1.2.0

func SyncDir(dir string)

SyncDir flushes dir's entries to disk, so a rename or a new file in it survives a crash. It is best-effort: a failure is ignored, and it does nothing on Windows, where a directory can't be synced this way.

Types

This section is empty.

Jump to

Keyboard shortcuts

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