提交 35bb5022 作者: Jakub Sztandera

Remove go-os-rename

It is included in go-datastore

License: MIT
Signed-off-by: 's avatarJakub Sztandera <kubuxu@protonmail.ch>
上级 58d222fb
language: go
go:
- 1.4
- release
script:
- go test -race -cpu=5 -v ./...
# go-os-rename
Easily rename files in place. This is needed because Windows errors on rename if a file already exists. Please see this commit, from which the code is extracted: https://github.com/lvarvel/cacheddownloader/commit/505a1fd
#### Godoc: https://godoc.org/github.com/jbenet/go-os-rename
## Author
The original author of this code are "David Morhovich, David Varvel and John Shahid" (see [this commit](https://github.com/lvarvel/cacheddownloader/commit/505a1fdcc5af7823f20d7c87d9e4d1c833c59053))
## License
The code originally comes from https://github.com/lvarvel/cacheddownloader, which is licensed Apache 2.0.
// +build !windows
package osrename
import "os"
func Rename(src, dst string) error {
return os.Rename(src, dst)
}
package osrename_test
import (
"bytes"
rn "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-os-rename"
"io/ioutil"
"os"
"testing"
)
func tempdir(t testing.TB) (path string, cleanup func()) {
path, err := ioutil.TempDir("", "test-windows-rename")
if err != nil {
t.Fatalf("cannot create temp directory: %v", err)
}
cleanup = func() {
if err := os.RemoveAll(path); err != nil {
t.Errorf("tempdir cleanup failed: %v", err)
}
}
return path, cleanup
}
func TestAtomicRename(t *testing.T) {
dirBase, cleanup := tempdir(t)
defer cleanup()
// Create base file
origFilePath := dirBase + "original.txt"
err := ioutil.WriteFile(origFilePath, []byte("tests"), 0644)
if err != nil {
t.Fatalf("Could not write original test file")
}
// Create secondary file
tempFilePath := dirBase + "newTempFile.txt"
err = ioutil.WriteFile(tempFilePath, []byte("success"), 0644)
if err != nil {
t.Fatalf("Could not write temp file")
}
// Execute our magic rename function
err = rn.Rename(tempFilePath, origFilePath)
if err != nil {
t.Fatalf("Could not rename temp file")
}
// Let's read the renamed file and ensure that we get data
renamedFileBytes, err := ioutil.ReadFile(origFilePath)
if err != nil {
t.Fatalf("Could not read renamed file")
}
// Let's compare the bytes of the renamed file
if bytes.Compare(renamedFileBytes, []byte("success")) != 0 {
t.Fatalf("Did not find expected bytes in renamed file %d vs %d", renamedFileBytes, []byte("success"))
}
}
// +build windows
package osrename
import (
"syscall"
"unsafe"
)
func Rename(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
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论