forked from cloudfoundry/cacheddownloader
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
1e59e50
commit 505a1fd
Showing
4 changed files
with
52 additions
and
2 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,41 @@ | ||
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, err := syscall.UTF16PtrFromString(src) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dstString, err := syscall.UTF16PtrFromString(dst) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
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 | ||
} |