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