forked from External/grumble
More Go 1 updates.
This commit is contained in:
parent
e46a65109f
commit
fee67fa856
7 changed files with 40 additions and 211 deletions
|
|
@ -74,7 +74,7 @@ func (server *Server) FreezeToFile() (err error) {
|
||||||
func (server *Server) openFreezeLog() (err error) {
|
func (server *Server) openFreezeLog() (err error) {
|
||||||
logfn := filepath.Join(Args.DataDir, "servers", strconv.FormatInt(server.Id, 10), "log.fz")
|
logfn := filepath.Join(Args.DataDir, "servers", strconv.FormatInt(server.Id, 10), "log.fz")
|
||||||
err = os.Remove(logfn)
|
err = os.Remove(logfn)
|
||||||
if pe, ok := err.(*os.PathError); ok && pe.Err == os.ENOENT {
|
if os.IsNotExist(err) {
|
||||||
// OK. File does not exist...
|
// OK. File does not exist...
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
40
grumble.go
40
grumble.go
|
|
@ -54,16 +54,8 @@ func main() {
|
||||||
// and will return an error if something's amiss.
|
// and will return an error if something's amiss.
|
||||||
blobDir := filepath.Join(Args.DataDir, "blob")
|
blobDir := filepath.Join(Args.DataDir, "blob")
|
||||||
err = os.Mkdir(blobDir, 0700)
|
err = os.Mkdir(blobDir, 0700)
|
||||||
if err != nil {
|
if err != nil && !os.IsExist(err) {
|
||||||
exists := false
|
log.Fatal("Unable to create blob directory: %v", err.Error())
|
||||||
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())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
err = blobstore.Open(blobDir)
|
err = blobstore.Open(blobDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -88,20 +80,12 @@ func main() {
|
||||||
hasKey := true
|
hasKey := true
|
||||||
hasCert := true
|
hasCert := true
|
||||||
_, err = os.Stat(certFn)
|
_, err = os.Stat(certFn)
|
||||||
if err != nil {
|
if err != nil && os.IsNotExist(err) {
|
||||||
if e, ok := err.(*os.PathError); ok {
|
hasCert = false
|
||||||
if e.Err == os.ENOENT {
|
|
||||||
hasCert = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
_, err = os.Stat(keyFn)
|
_, err = os.Stat(keyFn)
|
||||||
if err != nil {
|
if err != nil && os.IsNotExist(err) {
|
||||||
if e, ok := err.(*os.PathError); ok {
|
hasKey = false
|
||||||
if e.Err == os.ENOENT {
|
|
||||||
hasKey = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if !hasCert && !hasKey {
|
if !hasCert && !hasKey {
|
||||||
shouldRegen = true
|
shouldRegen = true
|
||||||
|
|
@ -167,16 +151,8 @@ func main() {
|
||||||
// exist.
|
// exist.
|
||||||
serversDirPath := filepath.Join(Args.DataDir, "servers")
|
serversDirPath := filepath.Join(Args.DataDir, "servers")
|
||||||
err = os.Mkdir(serversDirPath, 0700)
|
err = os.Mkdir(serversDirPath, 0700)
|
||||||
if err != nil {
|
if err != nil && !os.IsExist(err) {
|
||||||
exists := false
|
log.Fatal("Unable to create servers directory: %v", err.Error())
|
||||||
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())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read all entries of the servers directory.
|
// Read all entries of the servers directory.
|
||||||
|
|
|
||||||
|
|
@ -14,21 +14,13 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type BlobStore struct {
|
type BlobStore struct {
|
||||||
dir string
|
dir string
|
||||||
lockfn string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
|
||||||
win32AlreadyExists = 183
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
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.")
|
ErrBadFile = errors.New("a bad file exists in the blobstore directory. unable to create container directores.")
|
||||||
ErrNoSuchKey = errors.New("no such key")
|
ErrNoSuchKey = errors.New("no such key")
|
||||||
ErrInvalidKey = errors.New("invalid key")
|
ErrInvalidKey = errors.New("invalid key")
|
||||||
|
|
@ -92,31 +84,14 @@ func NewBlobStore(path string) (bs *BlobStore, err error) {
|
||||||
}
|
}
|
||||||
dir.Close()
|
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
|
dirStructureExists := true
|
||||||
// Check whether a 'blobstore' file exists in the directory.
|
// Check whether a 'blobstore' file exists in the directory.
|
||||||
// The existence of the file signals that the directory already
|
// The existence of the file signals that the directory already
|
||||||
// has the correct hierarchy structure.
|
// has the correct hierarchy structure.
|
||||||
bsf, err := os.Open(filepath.Join(path, "blobstore"))
|
bsf, err := os.Open(filepath.Join(path, "blobstore"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if e, ok := err.(*os.PathError); ok {
|
if os.IsNotExist(err) {
|
||||||
if e.Err == os.ENOENT {
|
dirStructureExists = false
|
||||||
dirStructureExists = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
bsf.Close()
|
bsf.Close()
|
||||||
|
|
@ -126,18 +101,14 @@ func NewBlobStore(path string) (bs *BlobStore, err error) {
|
||||||
for i := 0; i < 256; i++ {
|
for i := 0; i < 256; i++ {
|
||||||
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 os.IsExist(err) {
|
||||||
if isExistError(e) {
|
// The path already 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)
|
if err != nil {
|
||||||
if err != nil {
|
return nil, err
|
||||||
return nil, err
|
}
|
||||||
}
|
if !fi.IsDir() {
|
||||||
if !fi.IsDir() {
|
|
||||||
return nil, ErrBadFile
|
|
||||||
}
|
|
||||||
} else if e.Err == os.ENOTDIR {
|
|
||||||
return nil, ErrBadFile
|
return nil, ErrBadFile
|
||||||
}
|
}
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
|
|
@ -146,18 +117,14 @@ func NewBlobStore(path string) (bs *BlobStore, err error) {
|
||||||
for j := 0; j < 256; j++ {
|
for j := 0; j < 256; j++ {
|
||||||
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 os.IsExist(err) {
|
||||||
if isExistError(e) {
|
// The path already 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(inner)
|
if err != nil {
|
||||||
if err != nil {
|
return nil, err
|
||||||
return nil, err
|
}
|
||||||
}
|
if !fi.IsDir() {
|
||||||
if !fi.IsDir() {
|
|
||||||
return nil, ErrBadFile
|
|
||||||
}
|
|
||||||
} else if e.Err == os.ENOTDIR {
|
|
||||||
return nil, ErrBadFile
|
return nil, ErrBadFile
|
||||||
}
|
}
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
|
|
@ -177,7 +144,6 @@ func NewBlobStore(path string) (bs *BlobStore, err error) {
|
||||||
|
|
||||||
bs = &BlobStore{
|
bs = &BlobStore{
|
||||||
dir: path,
|
dir: path,
|
||||||
lockfn: lockfn,
|
|
||||||
}
|
}
|
||||||
return bs, nil
|
return bs, nil
|
||||||
}
|
}
|
||||||
|
|
@ -185,7 +151,7 @@ func NewBlobStore(path string) (bs *BlobStore, err error) {
|
||||||
// Close an open BlobStore. This removes the lockfile allowing
|
// Close an open BlobStore. This removes the lockfile allowing
|
||||||
// other processes to open the BlobStore.
|
// other processes to open the BlobStore.
|
||||||
func (bs *BlobStore) Close() (err error) {
|
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.
|
// 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)
|
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
|
err = ErrNoSuchKey
|
||||||
return
|
return
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
|
|
@ -277,7 +243,7 @@ func (bs *BlobStore) Put(buf []byte) (key string, err error) {
|
||||||
file.Close()
|
file.Close()
|
||||||
return
|
return
|
||||||
} else {
|
} 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!
|
// No such file exists on disk. Ready to rock!
|
||||||
} else {
|
} else {
|
||||||
return
|
return
|
||||||
|
|
@ -315,17 +281,4 @@ func (bs *BlobStore) Put(buf []byte) (key string, err 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.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
|
|
||||||
}
|
|
||||||
|
|
@ -7,6 +7,7 @@ package cryptstate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/aes"
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"errors"
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -31,7 +32,7 @@ type CryptState struct {
|
||||||
RemoteLost uint32
|
RemoteLost uint32
|
||||||
RemoteResync uint32
|
RemoteResync uint32
|
||||||
|
|
||||||
cipher *aes.Cipher
|
cipher cipher.Block
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() (cs *CryptState, err error) {
|
func New() (cs *CryptState, err error) {
|
||||||
|
|
|
||||||
|
|
@ -5,26 +5,25 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"exp/signal"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/mkrautz/grumble/pkg/logtarget"
|
"github.com/mkrautz/grumble/pkg/logtarget"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SignalHandler() {
|
func SignalHandler() {
|
||||||
for {
|
sigchan := make(chan os.Signal, 10)
|
||||||
sig := <-signal.Incoming
|
signal.Notify(sigchan, syscall.SIGUSR2, syscall.SIGTERM, syscall.SIGINT)
|
||||||
|
for sig := range sigchan {
|
||||||
if sig == os.UnixSignal(syscall.SIGUSR2) {
|
if sig == syscall.SIGUSR2 {
|
||||||
err := logtarget.Target.Rotate()
|
err := logtarget.Target.Rotate()
|
||||||
if err != nil {
|
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
|
continue
|
||||||
}
|
}
|
||||||
|
if sig == syscall.SIGINT || sig == syscall.SIGTERM {
|
||||||
if sig == os.UnixSignal(syscall.SIGINT) || sig == os.UnixSignal(syscall.SIGTERM) {
|
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue