github.com/opentofu/opentofu@v1.7.1/internal/replacefile/replacefile_unix.go (about)

     1  // Copyright (c) The OpenTofu Authors
     2  // SPDX-License-Identifier: MPL-2.0
     3  // Copyright (c) 2023 HashiCorp, Inc.
     4  // SPDX-License-Identifier: MPL-2.0
     5  
     6  //go:build !windows
     7  // +build !windows
     8  
     9  package replacefile
    10  
    11  import (
    12  	"os"
    13  )
    14  
    15  // AtomicRename renames from the source path to the destination path,
    16  // atomically replacing any file that might already exist at the destination.
    17  //
    18  // Typically this operation can succeed only if the source and destination
    19  // are within the same physical filesystem, so this function is best reserved
    20  // for cases where the source and destination exist in the same directory and
    21  // only the local filename differs between them.
    22  //
    23  // The Unix implementation of AtomicRename relies on the atomicity of renaming
    24  // that is required by the ISO C standard, which in turn assumes that Go's
    25  // implementation of rename is calling into a system call that preserves that
    26  // guarantee.
    27  func AtomicRename(source, destination string) error {
    28  	// On Unix systems, a rename is sufficiently atomic.
    29  	return os.Rename(source, destination)
    30  }