Abstract away lockfile instead of pid ops in blobstore.

This commit is contained in:
Mikkel Krautz 2011-05-17 01:01:51 +02:00
parent 412480eb85
commit 9ec8c1f46a
5 changed files with 104 additions and 39 deletions

View file

@ -6,9 +6,9 @@ GOFILES = \
blobreader.go
ifeq ($(GOOS),windows)
GOFILES += pid_windows.go
GOFILES += lock_windows.go
else
GOFILES += pid_unix.go
GOFILES += lock_unix.go
endif
include $(GOROOT)/src/Make.pkg

View file

@ -0,0 +1,73 @@
// 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 (
"os"
"path/filepath"
"io/ioutil"
"strconv"
"syscall"
)
// Acquire lockfile at path.
func AcquireLockFile(path string) os.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.Error == os.EEXIST {
content, err := ioutil.ReadFile(lockfn)
if err != nil {
return err
}
pid, err := strconv.Atoi(string(content))
if err == nil {
if syscall.Kill(pid, 0) == 0 {
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) os.Error {
return os.Remove(path)
}

View file

@ -0,0 +1,29 @@
// 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 (
"os"
"syscall"
)
const (
FILE_FLAG_DELETE_ON_CLOSE = 0x04000000
)
// Acquire a lockfile at path.
func AcquireLockFile(path string) os.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) os.Error {
// No-op because we use FLAG_DELETE_ON_CLOSE.
return nil
}

View file

@ -1,17 +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"
)
func getPid() uint64 {
return uint64(syscall.Getpid())
}
func pidRunning(pid uint64) bool {
return syscall.Kill(int(pid), 0) == 0
}

View file

@ -1,20 +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"
)
func getPid() uint64 {
handle, _ := syscall.GetCurrentProcess()
return uint64(handle)
}
func pidRunning(pid uint64) bool {
var status uint32
syscall.GetExitCodeProcess(uint32(pid), &status)
return status == 259 // STILL_ACTIVE
}