diff --git a/freeze.go b/freeze.go index aca1ee7..887f882 100644 --- a/freeze.go +++ b/freeze.go @@ -74,7 +74,7 @@ func (server *Server) FreezeToFile() (err error) { func (server *Server) openFreezeLog() (err error) { logfn := filepath.Join(Args.DataDir, "servers", strconv.FormatInt(server.Id, 10), "log.fz") err = os.Remove(logfn) - if pe, ok := err.(*os.PathError); ok && pe.Err == os.ENOENT { + if os.IsNotExist(err) { // OK. File does not exist... } else if err != nil { return err diff --git a/grumble.go b/grumble.go index c99839e..fd55a1a 100644 --- a/grumble.go +++ b/grumble.go @@ -54,16 +54,8 @@ func main() { // and will return an error if something's amiss. blobDir := filepath.Join(Args.DataDir, "blob") err = os.Mkdir(blobDir, 0700) - if err != nil { - exists := false - if e, ok := err.(*os.PathError); ok { - if e.Err == os.EEXIST { - exists = true - } - } - if !exists { - log.Fatal("Unable to create blob directory: %v", err.Error()) - } + if err != nil && !os.IsExist(err) { + log.Fatal("Unable to create blob directory: %v", err.Error()) } err = blobstore.Open(blobDir) if err != nil { @@ -88,20 +80,12 @@ func main() { hasKey := true hasCert := true _, err = os.Stat(certFn) - if err != nil { - if e, ok := err.(*os.PathError); ok { - if e.Err == os.ENOENT { - hasCert = false - } - } + if err != nil && os.IsNotExist(err) { + hasCert = false } _, err = os.Stat(keyFn) - if err != nil { - if e, ok := err.(*os.PathError); ok { - if e.Err == os.ENOENT { - hasKey = false - } - } + if err != nil && os.IsNotExist(err) { + hasKey = false } if !hasCert && !hasKey { shouldRegen = true @@ -167,16 +151,8 @@ func main() { // exist. serversDirPath := filepath.Join(Args.DataDir, "servers") err = os.Mkdir(serversDirPath, 0700) - if err != nil { - exists := false - if e, ok := err.(*os.PathError); ok { - if e.Err == os.EEXIST { - exists = true - } - } - if !exists { - log.Fatal("Unable to create servers directory: %v", err.Error()) - } + if err != nil && !os.IsExist(err) { + log.Fatal("Unable to create servers directory: %v", err.Error()) } // Read all entries of the servers directory. diff --git a/pkg/blobstore/blobstore.go b/pkg/blobstore/blobstore.go index 9b5d569..5796d50 100644 --- a/pkg/blobstore/blobstore.go +++ b/pkg/blobstore/blobstore.go @@ -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 -} +} \ No newline at end of file diff --git a/pkg/blobstore/lock_unix.go b/pkg/blobstore/lock_unix.go deleted file mode 100644 index 49b1c6f..0000000 --- a/pkg/blobstore/lock_unix.go +++ /dev/null @@ -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) -} diff --git a/pkg/blobstore/lock_windows.go b/pkg/blobstore/lock_windows.go deleted file mode 100644 index 40378de..0000000 --- a/pkg/blobstore/lock_windows.go +++ /dev/null @@ -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 -} diff --git a/pkg/cryptstate/cryptstate.go b/pkg/cryptstate/cryptstate.go index 4189940..97f4b74 100644 --- a/pkg/cryptstate/cryptstate.go +++ b/pkg/cryptstate/cryptstate.go @@ -7,6 +7,7 @@ package cryptstate import ( "crypto/aes" + "crypto/cipher" "crypto/rand" "errors" "time" @@ -31,7 +32,7 @@ type CryptState struct { RemoteLost uint32 RemoteResync uint32 - cipher *aes.Cipher + cipher cipher.Block } func New() (cs *CryptState, err error) { diff --git a/signal_unix.go b/signal_unix.go index 7c113a0..4aa298a 100644 --- a/signal_unix.go +++ b/signal_unix.go @@ -5,26 +5,25 @@ package main import ( - "exp/signal" "fmt" "github.com/mkrautz/grumble/pkg/logtarget" "os" + "os/signal" "syscall" ) func SignalHandler() { - for { - sig := <-signal.Incoming - - if sig == os.UnixSignal(syscall.SIGUSR2) { + sigchan := make(chan os.Signal, 10) + signal.Notify(sigchan, syscall.SIGUSR2, syscall.SIGTERM, syscall.SIGINT) + for sig := range sigchan { + if sig == syscall.SIGUSR2 { err := logtarget.Target.Rotate() if err != nil { - fmt.Fprintf(os.Stderr, "Unable to rotate log file: %v", err) + fmt.Fprintf(os.Stderr, "unable to rotate log file: %v", err) } continue } - - if sig == os.UnixSignal(syscall.SIGINT) || sig == os.UnixSignal(syscall.SIGTERM) { + if sig == syscall.SIGINT || sig == syscall.SIGTERM { os.Exit(0) } }