forked from External/grumble
Major gofix run.
This commit is contained in:
parent
92e6ac5276
commit
71b8314c2e
28 changed files with 522 additions and 559 deletions
|
|
@ -1,73 +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"
|
||||
)
|
||||
|
||||
|
||||
package blobstore
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"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
|
||||
}
|
||||
|
||||
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 {
|
||||
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)
|
||||
}
|
||||
func ReleaseLockFile(path string) error {
|
||||
return os.Remove(path)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue