opentofu/internal/replacefile/replacefile_unix.go
Martin Atkins 55e6f64977 internal/depsfile: Factor out our atomic file replacement logic
This originated in the cliconfig code to write out credentials files. The
Windows implementation of this in particular was quite onerous to get
right because it needs a very specific sequence of operations to avoid
running into exclusive file locks, and so by factoring this out with
only cosmetic modification we can avoid repeating all of that engineering
effort for other atomic file writing use-cases.
2020-10-14 08:01:19 -07:00

25 lines
888 B
Go

// +build !windows
package replacefile
import (
"os"
)
// AtomicRename renames from the source path to the destination path,
// atomically replacing any file that might already exist at the destination.
//
// Typically this operation can succeed only if the source and destination
// are within the same physical filesystem, so this function is best reserved
// for cases where the source and destination exist in the same directory and
// only the local filename differs between them.
//
// The Unix implementation of AtomicRename relies on the atomicity of renaming
// that is required by the ISO C standard, which in turn assumes that Go's
// implementation of rename is calling into a system call that preserves that
// guarantee.
func AtomicRename(source, destination string) error {
// On Unix systems, a rename is sufficiently atomic.
return os.Rename(source, destination)
}