Files
opentf/internal/replacefile/replacefile_windows.go
Martin Atkins 068df07d11 various: Remove legacy "+build" comments
Go 1.17 and earlier used a different syntax for build constraint comments,
starting with "+build". Go 1.18 changed this to the modern "go:build" form
as part of standardizing the structure of toolchain directive comments,
and so for a while it was convention to include comments in both styles
to allow building with both old and new Go compilers.

However, Go 1.17 is no longer supported, and regardless of that we only
expect OpenTofu to be built with the specific version we have selected
in "go.mod" and ".go-version" anyway, so we no longer need the legacy form
of these comments: the all supported Go toolchains now support the new
form, which this commit retains.

golangci-lint v2.6.0 includes a check for this legacy form, so removing
this will also allow us to upgrade to a newer version of that linter
aggregator in a future commit.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-11-01 08:00:01 -03:00

46 lines
1.5 KiB
Go

// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2023 HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
//go:build windows
package replacefile
import (
"os"
"syscall"
"golang.org/x/sys/windows"
)
// 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.
func AtomicRename(source, destination string) error {
// On Windows, renaming one file over another is not atomic and certain
// error conditions can result in having only the source file and nothing
// at the destination file. Instead, we need to call into the MoveFileEx
// Windows API function, setting two flags to opt in to replacing an
// existing file.
srcPtr, err := syscall.UTF16PtrFromString(source)
if err != nil {
return &os.LinkError{"replace", source, destination, err}
}
destPtr, err := syscall.UTF16PtrFromString(destination)
if err != nil {
return &os.LinkError{"replace", source, destination, err}
}
flags := uint32(windows.MOVEFILE_REPLACE_EXISTING | windows.MOVEFILE_WRITE_THROUGH)
err = windows.MoveFileEx(srcPtr, destPtr, flags)
if err != nil {
return &os.LinkError{"replace", source, destination, err}
}
return nil
}