1package atomic
2
3import (
4	"os"
5	"syscall"
6)
7
8const (
9	movefile_replace_existing = 0x1
10	movefile_write_through    = 0x8
11)
12
13//sys moveFileEx(lpExistingFileName *uint16, lpNewFileName *uint16, dwFlags uint32) (err error) = MoveFileExW
14
15// ReplaceFile atomically replaces the destination file or directory with the
16// source.  It is guaranteed to either replace the target file entirely, or not
17// change either file.
18func ReplaceFile(source, destination string) error {
19	src, err := syscall.UTF16PtrFromString(source)
20	if err != nil {
21		return &os.LinkError{"replace", source, destination, err}
22	}
23	dest, err := syscall.UTF16PtrFromString(destination)
24	if err != nil {
25		return &os.LinkError{"replace", source, destination, err}
26	}
27
28	// see http://msdn.microsoft.com/en-us/library/windows/desktop/aa365240(v=vs.85).aspx
29	if err := moveFileEx(src, dest, movefile_replace_existing|movefile_write_through); err != nil {
30		return &os.LinkError{"replace", source, destination, err}
31	}
32	return nil
33}
34