1
+ //go:build windows
2
+
1
3
package atomic
2
4
3
5
import (
4
6
"fmt"
5
- "os"
6
7
"syscall"
8
+ "unsafe"
7
9
)
8
10
9
11
const (
@@ -16,20 +18,38 @@ const (
16
18
// ReplaceFile atomically replaces the destination file or directory with the
17
19
// source. It is guaranteed to either replace the target file entirely, or not
18
20
// change either file.
19
- func ReplaceFile ( source , destination string ) error {
21
+ func Rename ( src , dst string ) error {
20
22
fmt .Println ("=>=>=>=>" , "Replace WINDOWS" )
21
- src , err := syscall .UTF16PtrFromString (source )
23
+ kernel32 , err := syscall .LoadLibrary ("kernel32.dll" )
24
+ if err != nil {
25
+ return err
26
+ }
27
+ defer syscall .FreeLibrary (kernel32 )
28
+ moveFileExUnicode , err := syscall .GetProcAddress (kernel32 , "MoveFileExW" )
29
+ if err != nil {
30
+ return err
31
+ }
32
+
33
+ srcString , err := syscall .UTF16PtrFromString (src )
22
34
if err != nil {
23
- return & os. LinkError { "replace" , source , destination , err }
35
+ return err
24
36
}
25
- dest , err := syscall .UTF16PtrFromString (destination )
37
+
38
+ dstString , err := syscall .UTF16PtrFromString (dst )
26
39
if err != nil {
27
- return & os. LinkError { "replace" , source , destination , err }
40
+ return err
28
41
}
29
42
30
- // see http://msdn.microsoft.com/en-us/library/windows/desktop/aa365240(v=vs.85).aspx
31
- if err := moveFileEx (src , dest , movefile_replace_existing | movefile_write_through ); err != nil {
32
- return & os.LinkError {"replace" , source , destination , err }
43
+ srcPtr := uintptr (unsafe .Pointer (srcString ))
44
+ dstPtr := uintptr (unsafe .Pointer (dstString ))
45
+
46
+ MOVEFILE_REPLACE_EXISTING := 0x1
47
+ flag := uintptr (MOVEFILE_REPLACE_EXISTING )
48
+
49
+ _ , _ , callErr := syscall .Syscall (uintptr (moveFileExUnicode ), 3 , srcPtr , dstPtr , flag )
50
+ if callErr != 0 {
51
+ return callErr
33
52
}
53
+
34
54
return nil
35
55
}
0 commit comments