mirror of
https://github.com/mumble-voip/grumble.git
synced 2025-12-19 21:59:59 -08:00
Make pkg/blobstore tests pass on Windows.
This commit is contained in:
parent
4894d3343d
commit
22febd0b13
1 changed files with 24 additions and 9 deletions
|
|
@ -21,6 +21,10 @@ type BlobStore struct {
|
||||||
makeall bool
|
makeall bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
win32AlreadyExists = 183
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrLocked = os.NewError("lockfile acquired by another process")
|
ErrLocked = os.NewError("lockfile acquired by another process")
|
||||||
ErrLockAcquirement = os.NewError("unable to acquire lockfile")
|
ErrLockAcquirement = os.NewError("unable to acquire lockfile")
|
||||||
|
|
@ -116,8 +120,7 @@ func NewBlobStore(path string, makeall bool) (bs *BlobStore, err os.Error) {
|
||||||
outer := filepath.Join(path, hex.EncodeToString([]byte{byte(i)}))
|
outer := filepath.Join(path, hex.EncodeToString([]byte{byte(i)}))
|
||||||
err = os.Mkdir(outer, 0700)
|
err = os.Mkdir(outer, 0700)
|
||||||
if e, ok := err.(*os.PathError); ok {
|
if e, ok := err.(*os.PathError); ok {
|
||||||
switch e.Error {
|
if isExistError(e) {
|
||||||
case os.EEXIST:
|
|
||||||
// The file alread exists. Stat it to check whether it is indeed
|
// The file alread exists. Stat it to check whether it is indeed
|
||||||
// a directory.
|
// a directory.
|
||||||
fi, err := os.Stat(outer)
|
fi, err := os.Stat(outer)
|
||||||
|
|
@ -127,7 +130,7 @@ func NewBlobStore(path string, makeall bool) (bs *BlobStore, err os.Error) {
|
||||||
if !fi.IsDirectory() {
|
if !fi.IsDirectory() {
|
||||||
return nil, ErrBadFile
|
return nil, ErrBadFile
|
||||||
}
|
}
|
||||||
case os.ENOTDIR:
|
} else if e.Error == os.ENOTDIR {
|
||||||
return nil, ErrBadFile
|
return nil, ErrBadFile
|
||||||
}
|
}
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
|
|
@ -137,8 +140,7 @@ func NewBlobStore(path string, makeall bool) (bs *BlobStore, err os.Error) {
|
||||||
inner := filepath.Join(outer, hex.EncodeToString([]byte{byte(j)}))
|
inner := filepath.Join(outer, hex.EncodeToString([]byte{byte(j)}))
|
||||||
err = os.Mkdir(inner, 0700)
|
err = os.Mkdir(inner, 0700)
|
||||||
if e, ok := err.(*os.PathError); ok {
|
if e, ok := err.(*os.PathError); ok {
|
||||||
switch e.Error {
|
if isExistError(e) {
|
||||||
case os.EEXIST:
|
|
||||||
// The file alread exists. Stat it to check whether it is indeed
|
// The file alread exists. Stat it to check whether it is indeed
|
||||||
// a directory.
|
// a directory.
|
||||||
fi, err := os.Stat(inner)
|
fi, err := os.Stat(inner)
|
||||||
|
|
@ -148,7 +150,7 @@ func NewBlobStore(path string, makeall bool) (bs *BlobStore, err os.Error) {
|
||||||
if !fi.IsDirectory() {
|
if !fi.IsDirectory() {
|
||||||
return nil, ErrBadFile
|
return nil, ErrBadFile
|
||||||
}
|
}
|
||||||
case os.ENOTDIR:
|
} else if e.Error == os.ENOTDIR {
|
||||||
return nil, ErrBadFile
|
return nil, ErrBadFile
|
||||||
}
|
}
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
|
|
@ -212,7 +214,7 @@ func (bs *BlobStore) Get(key string) (buf []byte, err os.Error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := os.Open(fn)
|
file, err := os.Open(fn)
|
||||||
if e, ok := err.(*os.PathError); ok && e.Error == os.ENOENT {
|
if e, ok := err.(*os.PathError); ok && (e.Error == os.ENOENT || e.Error == os.ENOTDIR) {
|
||||||
err = ErrNoSuchKey
|
err = ErrNoSuchKey
|
||||||
return
|
return
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
|
|
@ -261,7 +263,7 @@ func (bs *BlobStore) Put(buf []byte) (key string, err os.Error) {
|
||||||
file.Close()
|
file.Close()
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
if e, ok := err.(*os.PathError); ok && e.Error == os.ENOENT {
|
if e, ok := err.(*os.PathError); ok && (e.Error == os.ENOENT || e.Error == os.ENOTDIR) {
|
||||||
// No such file exists on disk. Ready to rock!
|
// No such file exists on disk. Ready to rock!
|
||||||
} else {
|
} else {
|
||||||
return
|
return
|
||||||
|
|
@ -270,7 +272,7 @@ func (bs *BlobStore) Put(buf []byte) (key string, err os.Error) {
|
||||||
|
|
||||||
if !bs.makeall {
|
if !bs.makeall {
|
||||||
// Make sure the leading directories exist...
|
// Make sure the leading directories exist...
|
||||||
err = os.MkdirAll(blobdir, 0700)
|
err = os.MkdirAll(filepath.ToSlash(blobdir), 0700)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -308,3 +310,16 @@ func (bs *BlobStore) Put(buf []byte) (key string, err os.Error) {
|
||||||
|
|
||||||
return key, nil
|
return key, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check whether an os.PathError is an EXIST error.
|
||||||
|
// On Unix, it checks for EEXIST. On Windows, it checks for EEXIST
|
||||||
|
// and Errno code 183 (ERROR_ALREADY_EXISTS)
|
||||||
|
func isExistError(err *os.PathError) (exists bool) {
|
||||||
|
if e, ok := err.Error.(os.Errno); ok && e == win32AlreadyExists {
|
||||||
|
exists = true
|
||||||
|
}
|
||||||
|
if err.Error == os.EEXIST {
|
||||||
|
exists = true
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue