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 ¶
- Variables
- func AppendPrivate(path string) (*os.File, error)
- func CheckTrusted(path string) error
- func CheckTrustedParent(path string) error
- func EnsureDir(path string) error
- func Lock(ctx context.Context, path string) (func(), error)
- func ReadCache(dir string, key any, ttl time.Duration) ([]byte, bool)
- func Update(ctx context.Context, path string, perm os.FileMode, ...) error
- func WriteAtomic(path string, data []byte, perm os.FileMode) error
- func WriteCache(dir string, key any, data []byte)
- func WriteNew(path string, data []byte, perm os.FileMode) (err error)
Constants ¶
This section is empty.
Variables ¶
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 AppendPrivate ¶ added in v0.4.0
AppendPrivate opens a file to append to that nobody but this user may read, such as a log of host names and errors, and creates it readable and writable by this user alone when it is not there. A regular file that is there already has to be one this user owns and nobody else can read or write: the lines appended would be read by whoever else can read it, and could be mixed with lines of theirs by whoever else can write it. A named pipe has to be this user's or root's, since whoever reads it reads the lines. A terminal or another device is opened as it is. A path that names one of this process's own descriptors, /dev/stderr, /dev/fd/3, is written through that descriptor, wherever the shell that started the process pointed it: the lines follow what the process writes there, not at the start of a file stderr was redirected to.
The path is often in a directory others can write to, so a symbolic link on the way to the file has to be this user's or root's too, as for WriteAtomic: one another user made would have the lines appended to a file of this user's that it points at, or a file created where it points. A path that is no link when it is looked at is opened without following one, so that none can take its place in between.
func CheckTrusted ¶ added in v0.3.0
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
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 EnsureDir ¶
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 ¶
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 ReadCache ¶ added in v0.4.0
ReadCache returns what WriteCache stored under key in dir, if it was written less than ttl ago. The key is kept in the entry and compared, so an entry is only ever read back for the lookup that wrote it, and one written in the future, which would never age, is not trusted. Anything missing, stale or unreadable is a miss: the caller fetches again.
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 ¶
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 WriteCache ¶ added in v0.4.0
WriteCache stores data under key in dir for ReadCache. Nothing is kept of empty data, and a cache that cannot be written is not worth failing a command over, so nothing is reported.
func WriteNew ¶ added in v0.2.0
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.