fileutil

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: GPL-3.0, LGPL-3.0 Imports: 7 Imported by: 0 modules, 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 fileutil writes files the way a tool several administrators run at once has to: completely or not at all, and under a lock when the file is shared.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyTo

func CopyTo(path string, r io.Reader, perm os.FileMode) error

CopyTo copies a reader into a new file with the given permission.

func EnsureDir

func EnsureDir(path string) error

EnsureDir creates a directory that only its owner can read, for state that holds host keys, control sockets and certificate pins.

func Lock

func Lock(ctx context.Context, path string) (func(), error)

Lock takes an exclusive lock for a path and returns the function that releases it. The lock lives in a sibling file, so that the locked file can still be replaced atomically.

func Update

func Update(ctx context.Context, path string, perm os.FileMode, change func([]byte) ([]byte, error)) error

Update reads a file, hands its content to change and writes back whatever comes out, holding an exclusive lock for the whole cycle. A file that does not exist yet is presented as empty.

The known_hosts file is shared and versioned, and ssh appends to it without locking, so every write clusterctl makes to it goes through here.

func WriteAtomic

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

WriteAtomic writes data to path through a temporary file in the same directory, so a reader never sees a half written file and a failure leaves the previous content in place.

func WriteNew added in v0.2.0

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

WriteNew writes data to a file that does not exist yet, and never to one that does. The file is created exclusively, so one that appeared since it was last looked for is left alone and reported with an error errors.Is matches to fs.ErrExist. A write that fails takes the new file away again.

Types

This section is empty.

Jump to

Keyboard shortcuts

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