提交 ce0b10a9 作者: Tommi Virtanen

Vendor new go-datastore, with Windows fixes

fixes #1108
上级 db2acd9b
......@@ -162,7 +162,7 @@
},
{
"ImportPath": "github.com/jbenet/go-datastore",
"Rev": "f1a0a0fd88f23b67589957f02b7500372aca186f"
"Rev": "2525cae416316b9cf2eb66ec8d4792f567436efa"
},
{
"ImportPath": "github.com/jbenet/go-detect-race",
......@@ -195,6 +195,10 @@
"Rev": "87e53a9d2875a18a7863b351d22f912545e6b3a3"
},
{
"ImportPath": "github.com/jbenet/go-os-rename",
"Rev": "2d93ae970ba96c41f717036a5bf5494faf1f38c0"
},
{
"ImportPath": "github.com/jbenet/go-peerstream",
"Rev": "bbe2a6461aa80ee25fd87eccf35bd54bac7f788d"
},
......
......@@ -19,6 +19,10 @@
"Rev": "4dfff096c4973178c8f35cf6dd1a732a0a139370"
},
{
"ImportPath": "github.com/jbenet/go-os-rename",
"Rev": "2d93ae970ba96c41f717036a5bf5494faf1f38c0"
},
{
"ImportPath": "github.com/jbenet/goprocess",
"Rev": "5b02f8d275a2dd882fb06f8bbdf74347795ff3b1"
},
......
......@@ -13,6 +13,7 @@ import (
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/query"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-os-rename"
)
const (
......@@ -79,12 +80,7 @@ func (fs *Datastore) makePrefixDir(dir string) error {
// it, the creation of the prefix dir itself might not be
// durable yet. Sync the root dir after a successful mkdir of
// a prefix dir, just to be paranoid.
f, err := os.Open(fs.path)
if err != nil {
return err
}
defer f.Close()
if err := f.Sync(); err != nil {
if err := syncDir(fs.path); err != nil {
return err
}
return nil
......@@ -101,12 +97,6 @@ func (fs *Datastore) Put(key datastore.Key, value interface{}) error {
return err
}
dirF, err := os.Open(dir)
if err != nil {
return err
}
defer dirF.Close()
tmp, err := ioutil.TempFile(dir, "put-")
if err != nil {
return err
......@@ -135,16 +125,15 @@ func (fs *Datastore) Put(key datastore.Key, value interface{}) error {
}
closed = true
err = os.Rename(tmp.Name(), path)
err = osrename.Rename(tmp.Name(), path)
if err != nil {
return err
}
removed = true
if err := dirF.Sync(); err != nil {
if err := syncDir(dir); err != nil {
return err
}
return nil
}
......
......@@ -4,6 +4,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
......@@ -147,8 +148,8 @@ func TestStorage(t *testing.T) {
defer cleanup()
const prefixLen = 2
const prefix = "2f71"
const target = prefix + "/2f71757578.data"
const prefix = "7175"
const target = prefix + string(os.PathSeparator) + "71757578.data"
fs, err := flatfs.New(temp, prefixLen)
if err != nil {
t.Fatalf("New fail: %v\n", err)
......@@ -182,8 +183,10 @@ func TestStorage(t *testing.T) {
if !fi.Mode().IsRegular() {
t.Errorf("expected a regular file, mode: %04o", fi.Mode())
}
if g, e := fi.Mode()&os.ModePerm&0007, os.FileMode(0000); g != e {
t.Errorf("file should not be world accessible: %04o", fi.Mode())
if runtime.GOOS != "windows" {
if g, e := fi.Mode()&os.ModePerm&0007, os.FileMode(0000); g != e {
t.Errorf("file should not be world accessible: %04o", fi.Mode())
}
}
default:
t.Errorf("saw unexpected directory entry: %q %v", path, fi.Mode())
......
// +build !windows
package flatfs
import "os"
func syncDir(dir string) error {
dirF, err := os.Open(dir)
if err != nil {
return err
}
defer dirF.Close()
if err := dirF.Sync(); err != nil {
return err
}
return nil
}
package flatfs
func syncDir(dir string) error {
return nil
}
......@@ -5,7 +5,7 @@ import (
"testing"
ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
lru "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/lru"
lru "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/lru" // Hook up gocheck into the "go test" runner.
. "gopkg.in/check.v1"
)
......
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)
}
// +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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论