-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix file moving to be safe on Windows.
Fix file moving to be safe on Windows. A shim was added that calls into the Win32 API / MoveFileEx that prevents an error occurring on windows if the destination already exists. This shim might not be needed once os.Replace lands, possibly in Go 1.5. See: golang/go#8914 [#88907658]
- Loading branch information
David Morhovich, David Varvel and John Shahid
committed
Feb 24, 2015
1 parent
1e59e50
commit 8e2f69d
Showing
4 changed files
with
46 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// +build !windows | ||
|
||
package cacheddownloader | ||
|
||
import "os" | ||
|
||
func Replace(src, dst string) error { | ||
return os.Rename(src, dst) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package cacheddownloader | ||
|
||
import ( | ||
"syscall" | ||
"unsafe" | ||
) | ||
|
||
func Replace(src, dst string) error { | ||
kernel32, err := syscall.LoadLibrary("kernel32.dll") | ||
if err != nil { | ||
return err | ||
} | ||
defer syscall.FreeLibrary(kernel32) | ||
moveFileExUnicode, err := syscall.GetProcAddress(kernel32, "MoveFileExW") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
srcString := syscall.StringToUTF16Ptr(src) | ||
dstString := syscall.StringToUTF16Ptr(dst) | ||
|
||
srcPtr := uintptr(unsafe.Pointer(srcString)) | ||
dstPtr := uintptr(unsafe.Pointer(dstString)) | ||
|
||
MOVEFILE_REPLACE_EXISTING := 0x1 | ||
flag := uintptr(MOVEFILE_REPLACE_EXISTING) | ||
|
||
_, _, callErr := syscall.Syscall(uintptr(moveFileExUnicode), 3, srcPtr, dstPtr, flag) | ||
if callErr != 0 { | ||
return callErr | ||
} | ||
|
||
return nil | ||
} |