mirror of
https://github.com/mumble-voip/grumble.git
synced 2025-12-20 06:10:00 -08:00
More Go 1 updates.
This commit is contained in:
parent
e46a65109f
commit
fee67fa856
7 changed files with 40 additions and 211 deletions
|
|
@ -14,21 +14,13 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
type BlobStore struct {
|
||||
dir string
|
||||
lockfn string
|
||||
}
|
||||
|
||||
const (
|
||||
win32AlreadyExists = 183
|
||||
)
|
||||
|
||||
var (
|
||||
ErrLocked = errors.New("lockfile acquired by another process")
|
||||
ErrLockAcquirement = errors.New("unable to acquire lockfile")
|
||||
ErrBadFile = errors.New("a bad file exists in the blobstore directory. unable to create container directores.")
|
||||
ErrNoSuchKey = errors.New("no such key")
|
||||
ErrInvalidKey = errors.New("invalid key")
|
||||
|
|
@ -92,31 +84,14 @@ func NewBlobStore(path string) (bs *BlobStore, err error) {
|
|||
}
|
||||
dir.Close()
|
||||
|
||||
// Try to acquire an exclusive lock on the blobstore.
|
||||
lockfn := filepath.Join(path, "lock")
|
||||
err = AcquireLockFile(lockfn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Make sure to remove the lockfile if we return with an error.
|
||||
// It would be impossible for users to remove it (they wouldn't
|
||||
// know the filename.)
|
||||
defer func() {
|
||||
if err != nil {
|
||||
ReleaseLockFile(lockfn)
|
||||
}
|
||||
}()
|
||||
|
||||
dirStructureExists := true
|
||||
// Check whether a 'blobstore' file exists in the directory.
|
||||
// The existence of the file signals that the directory already
|
||||
// has the correct hierarchy structure.
|
||||
bsf, err := os.Open(filepath.Join(path, "blobstore"))
|
||||
if err != nil {
|
||||
if e, ok := err.(*os.PathError); ok {
|
||||
if e.Err == os.ENOENT {
|
||||
dirStructureExists = false
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
dirStructureExists = false
|
||||
}
|
||||
} else {
|
||||
bsf.Close()
|
||||
|
|
@ -126,18 +101,14 @@ func NewBlobStore(path string) (bs *BlobStore, err error) {
|
|||
for i := 0; i < 256; i++ {
|
||||
outer := filepath.Join(path, hex.EncodeToString([]byte{byte(i)}))
|
||||
err = os.Mkdir(outer, 0700)
|
||||
if e, ok := err.(*os.PathError); ok {
|
||||
if isExistError(e) {
|
||||
// The file alread exists. Stat it to check whether it is indeed
|
||||
// a directory.
|
||||
fi, err := os.Stat(outer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !fi.IsDir() {
|
||||
return nil, ErrBadFile
|
||||
}
|
||||
} else if e.Err == os.ENOTDIR {
|
||||
if os.IsExist(err) {
|
||||
// The path already exists. Stat it to check whether it is indeed
|
||||
// a directory.
|
||||
fi, err := os.Stat(outer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !fi.IsDir() {
|
||||
return nil, ErrBadFile
|
||||
}
|
||||
} else if err != nil {
|
||||
|
|
@ -146,18 +117,14 @@ func NewBlobStore(path string) (bs *BlobStore, err error) {
|
|||
for j := 0; j < 256; j++ {
|
||||
inner := filepath.Join(outer, hex.EncodeToString([]byte{byte(j)}))
|
||||
err = os.Mkdir(inner, 0700)
|
||||
if e, ok := err.(*os.PathError); ok {
|
||||
if isExistError(e) {
|
||||
// The file alread exists. Stat it to check whether it is indeed
|
||||
// a directory.
|
||||
fi, err := os.Stat(inner)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !fi.IsDir() {
|
||||
return nil, ErrBadFile
|
||||
}
|
||||
} else if e.Err == os.ENOTDIR {
|
||||
if os.IsExist(err) {
|
||||
// The path already exists. Stat it to check whether it is indeed
|
||||
// a directory.
|
||||
fi, err := os.Stat(outer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !fi.IsDir() {
|
||||
return nil, ErrBadFile
|
||||
}
|
||||
} else if err != nil {
|
||||
|
|
@ -177,7 +144,6 @@ func NewBlobStore(path string) (bs *BlobStore, err error) {
|
|||
|
||||
bs = &BlobStore{
|
||||
dir: path,
|
||||
lockfn: lockfn,
|
||||
}
|
||||
return bs, nil
|
||||
}
|
||||
|
|
@ -185,7 +151,7 @@ func NewBlobStore(path string) (bs *BlobStore, err error) {
|
|||
// Close an open BlobStore. This removes the lockfile allowing
|
||||
// other processes to open the BlobStore.
|
||||
func (bs *BlobStore) Close() (err error) {
|
||||
return os.Remove(bs.lockfn)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Checks that a given key is a valid key for the BlobStore.
|
||||
|
|
@ -228,7 +194,7 @@ func (bs *BlobStore) Get(key string) (buf []byte, err error) {
|
|||
}
|
||||
|
||||
file, err := os.Open(fn)
|
||||
if e, ok := err.(*os.PathError); ok && (e.Err == os.ENOENT || e.Err == os.ENOTDIR) {
|
||||
if os.IsNotExist(err) {
|
||||
err = ErrNoSuchKey
|
||||
return
|
||||
} else if err != nil {
|
||||
|
|
@ -277,7 +243,7 @@ func (bs *BlobStore) Put(buf []byte) (key string, err error) {
|
|||
file.Close()
|
||||
return
|
||||
} else {
|
||||
if e, ok := err.(*os.PathError); ok && (e.Err == os.ENOENT || e.Err == os.ENOTDIR) {
|
||||
if os.IsNotExist(err) {
|
||||
// No such file exists on disk. Ready to rock!
|
||||
} else {
|
||||
return
|
||||
|
|
@ -315,17 +281,4 @@ func (bs *BlobStore) Put(buf []byte) (key string, err error) {
|
|||
}
|
||||
|
||||
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.Err.(syscall.Errno); ok && e == win32AlreadyExists {
|
||||
exists = true
|
||||
}
|
||||
if err.Err == os.EEXIST {
|
||||
exists = true
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
@ -1,74 +0,0 @@
|
|||
// Copyright (c) 2011 The Grumble Authors
|
||||
// The use of this source code is goverened by a BSD-style
|
||||
// license that can be found in the LICENSE-file.
|
||||
|
||||
package blobstore
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// Acquire lockfile at path.
|
||||
func AcquireLockFile(path string) error {
|
||||
dir, fn := filepath.Split(path)
|
||||
lockfn := filepath.Join(dir, fn)
|
||||
|
||||
lockfile, err := os.OpenFile(lockfn, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
|
||||
if e, ok := err.(*os.PathError); ok && e.Err == os.EEXIST {
|
||||
content, err := ioutil.ReadFile(lockfn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pid, err := strconv.Atoi(string(content))
|
||||
if err == nil {
|
||||
err = syscall.Kill(pid, 0)
|
||||
if err != nil {
|
||||
return ErrLocked
|
||||
}
|
||||
}
|
||||
|
||||
lockfile, err = ioutil.TempFile(dir, "lock")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = lockfile.WriteString(strconv.Itoa(syscall.Getpid()))
|
||||
if err != nil {
|
||||
lockfile.Close()
|
||||
return ErrLockAcquirement
|
||||
}
|
||||
|
||||
curfn := lockfile.Name()
|
||||
|
||||
err = lockfile.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = os.Rename(curfn, lockfn)
|
||||
if err != nil {
|
||||
os.Remove(curfn)
|
||||
return ErrLockAcquirement
|
||||
}
|
||||
} else if err != nil {
|
||||
return err
|
||||
} else {
|
||||
_, err = lockfile.WriteString(strconv.Itoa(syscall.Getpid()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
lockfile.Close()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Release lockfile at path.
|
||||
func ReleaseLockFile(path string) error {
|
||||
return os.Remove(path)
|
||||
}
|
||||
|
|
@ -1,26 +0,0 @@
|
|||
// Copyright (c) 2011 The Grumble Authors
|
||||
// The use of this source code is goverened by a BSD-style
|
||||
// license that can be found in the LICENSE-file.
|
||||
|
||||
package blobstore
|
||||
|
||||
import "syscall"
|
||||
|
||||
const (
|
||||
FILE_FLAG_DELETE_ON_CLOSE = 0x04000000
|
||||
)
|
||||
|
||||
// Acquire a lockfile at path.
|
||||
func AcquireLockFile(path string) error {
|
||||
handle, _ := syscall.CreateFile(syscall.StringToUTF16Ptr(path), syscall.GENERIC_WRITE, 0, nil, syscall.CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, 0)
|
||||
if handle < 0 {
|
||||
return ErrLocked
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Release the lockfile at path.
|
||||
func ReleaseLockFile(path string) error {
|
||||
// No-op because we use FLAG_DELETE_ON_CLOSE.
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue