1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-09-09 19:32:24 +08:00

feat(fsrepo): protect with a repo lockfile

NB: daemon is one spot the repo lock is typically acquired
This commit is contained in:
Brian Tiger Chow
2015-01-13 17:19:31 -08:00
parent 67c161fb72
commit 40e41d24f7
5 changed files with 60 additions and 16 deletions

31
repo/fsrepo/lock/lock.go Normal file
View File

@ -0,0 +1,31 @@
package lock
import (
"io"
"path"
lock "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/camlistore/lock"
"github.com/jbenet/go-ipfs/util"
"github.com/jbenet/go-ipfs/util/debugerror"
)
// LockFile is the filename of the daemon lock, relative to config dir
// TODO rename repo lock and hide name
const LockFile = "daemon.lock"
func Lock(confdir string) (io.Closer, error) {
c, err := lock.Lock(path.Join(confdir, LockFile))
return c, debugerror.Wrap(err)
}
func Locked(confdir string) bool {
if !util.FileExists(path.Join(confdir, LockFile)) {
return false
}
if lk, err := Lock(confdir); err != nil {
return true
} else {
lk.Close()
return false
}
}