fileutil

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2026 License: GPL-3.0, LGPL-3.0 Imports: 10 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

View Source
var ErrUntrusted = errors.New("written by someone else")

ErrUntrusted is matched by errors.Is for every file or directory refused because someone other than this user or root could have written it.

Functions

func CheckTrusted added in v0.3.0

func CheckTrusted(path string) error

CheckTrusted refuses a file or directory that someone other than this user or root could have written: one another user owns, or one its group or anyone can write. Configuration names the programs clusterctl runs, so it is held to what OpenSSH holds ~/.ssh/config to. A link is followed, and what it points at is checked.

func CheckTrustedParent added in v0.3.0

func CheckTrustedParent(path string) error

CheckTrustedParent checks the directory holding path, whoever could replace path. A directory with the sticky bit set may be writable by anyone, the way /tmp is, because nobody can then replace or remove a file another user put there.

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 write, for state that holds host keys, control sockets and certificate pins, and checks the one that is there already. It has to be a directory, not a link to one, owned by this user and writable by nobody else: whoever else can write it can replace the ssh configuration clusterctl hands to ssh.

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.

The lock file is readable, which is all taking the lock needs, by whoever can write the file: its group when the file, or while there is none yet its directory, is writable by the group. A lock only its first user could open would lock every other administrator out of a shared file for good.

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.

A file that exists already keeps its mode and group, and its owner when root writes it, so that a file several administrators share stays writable by all of them; perm applies only to a new file. Write permission for anyone is never kept. A symbolic link is written through rather than replaced, as long as this user or root made it.

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