open-vault/vendor/github.com/natefinch/atomic
ncabatoff 55609f1d38
Ensure that the .vault-token file writen by `vault login` always has the correct permissions and ownership. (#8867)
2020-04-27 19:55:13 -04:00
..
.gitignore Ensure that the .vault-token file writen by `vault login` always has the correct permissions and ownership. (#8867) 2020-04-27 19:55:13 -04:00
LICENSE Ensure that the .vault-token file writen by `vault login` always has the correct permissions and ownership. (#8867) 2020-04-27 19:55:13 -04:00
README.md Ensure that the .vault-token file writen by `vault login` always has the correct permissions and ownership. (#8867) 2020-04-27 19:55:13 -04:00
atomic.go Ensure that the .vault-token file writen by `vault login` always has the correct permissions and ownership. (#8867) 2020-04-27 19:55:13 -04:00
file_unix.go Ensure that the .vault-token file writen by `vault login` always has the correct permissions and ownership. (#8867) 2020-04-27 19:55:13 -04:00
file_windows.go Ensure that the .vault-token file writen by `vault login` always has the correct permissions and ownership. (#8867) 2020-04-27 19:55:13 -04:00
zfile_windows.go Ensure that the .vault-token file writen by `vault login` always has the correct permissions and ownership. (#8867) 2020-04-27 19:55:13 -04:00

README.md

atomic

import "github.com/natefinch/atomic"

atomic is a go package for atomic file writing

By default, writing to a file in go (and generally any language) can fail partway through... you then have a partially written file, which probably was truncated when the write began, and bam, now you've lost data.

This go package avoids this problem, by writing first to a temp file, and then overwriting the target file in an atomic way. This is easy on linux, os.Rename just is atomic. However, on Windows, os.Rename is not atomic, and so bad things can happen. By wrapping the windows API moveFileEx, we can ensure that the move is atomic, and we can be safe in knowing that either the move succeeds entirely, or neither file will be modified.

func ReplaceFile

func ReplaceFile(source, destination string) error

ReplaceFile atomically replaces the destination file or directory with the source. It is guaranteed to either replace the target file entirely, or not change either file.

func WriteFile

func WriteFile(filename string, r io.Reader) (err error)

WriteFile atomically writes the contents of r to the specified filepath. If an error occurs, the target file is guaranteed to be either fully written, or not written at all. WriteFile overwrites any file that exists at the location (but only if the write fully succeeds, otherwise the existing file is unmodified).