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 ¶
- func CopyTo(path string, r io.Reader, perm os.FileMode) error
- func EnsureDir(path string) error
- func Lock(ctx context.Context, path string) (func(), error)
- func Update(ctx context.Context, path string, perm os.FileMode, ...) error
- func WriteAtomic(path string, data []byte, perm os.FileMode) error
- func WriteNew(path string, data []byte, perm os.FileMode) (err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EnsureDir ¶
EnsureDir creates a directory that only its owner can read, for state that holds host keys, control sockets and certificate pins.
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.
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.
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.