提交 81678744 作者: dignifiedquire 提交者: Steven Allen

refactor: extract fs lock into go-fs-lock

License: MIT
Signed-off-by: 's avatardignifiedquire <dignifiedquire@gmail.com>
上级 3f6519b4
...@@ -15,7 +15,6 @@ import ( ...@@ -15,7 +15,6 @@ import (
corerepo "github.com/ipfs/go-ipfs/core/corerepo" corerepo "github.com/ipfs/go-ipfs/core/corerepo"
config "github.com/ipfs/go-ipfs/repo/config" config "github.com/ipfs/go-ipfs/repo/config"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo" fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
lockfile "github.com/ipfs/go-ipfs/repo/fsrepo/lock"
bstore "gx/ipfs/QmaG4DZ4JaqEfvPWt5nPPgoTzhc1tr1T3f4Nu9Jpdm8ymY/go-ipfs-blockstore" bstore "gx/ipfs/QmaG4DZ4JaqEfvPWt5nPPgoTzhc1tr1T3f4Nu9Jpdm8ymY/go-ipfs-blockstore"
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid" cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
...@@ -233,7 +232,7 @@ daemons are running. ...@@ -233,7 +232,7 @@ daemons are running.
} }
dsLockFile := filepath.Join(dsPath, "LOCK") // TODO: get this lockfile programmatically dsLockFile := filepath.Join(dsPath, "LOCK") // TODO: get this lockfile programmatically
repoLockFile := filepath.Join(configRoot, lockfile.LockFile) repoLockFile := filepath.Join(configRoot, fsrepo.LockFile)
apiFile := filepath.Join(configRoot, "api") // TODO: get this programmatically apiFile := filepath.Join(configRoot, "api") // TODO: get this programmatically
log.Infof("Removing repo lockfile: %s", repoLockFile) log.Infof("Removing repo lockfile: %s", repoLockFile)
......
...@@ -575,6 +575,12 @@ ...@@ -575,6 +575,12 @@
"hash": "QmWLWmRVSiagqP15jczsGME1qpob6HDbtbHAY2he9W5iUo", "hash": "QmWLWmRVSiagqP15jczsGME1qpob6HDbtbHAY2he9W5iUo",
"name": "opentracing-go", "name": "opentracing-go",
"version": "0.0.3" "version": "0.0.3"
},
{
"author": "dignifiedquire",
"hash": "QmPdqSMmiwtQCBC515gFtMW2mP14HsfgnyQ2k5xPQVxMge",
"name": "go-fs-lock",
"version": "0.1.2"
} }
], ],
"gxVersion": "0.10.0", "gxVersion": "0.10.0",
......
...@@ -16,10 +16,10 @@ import ( ...@@ -16,10 +16,10 @@ import (
repo "github.com/ipfs/go-ipfs/repo" repo "github.com/ipfs/go-ipfs/repo"
"github.com/ipfs/go-ipfs/repo/common" "github.com/ipfs/go-ipfs/repo/common"
config "github.com/ipfs/go-ipfs/repo/config" config "github.com/ipfs/go-ipfs/repo/config"
lockfile "github.com/ipfs/go-ipfs/repo/fsrepo/lock"
mfsr "github.com/ipfs/go-ipfs/repo/fsrepo/migrations" mfsr "github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
serialize "github.com/ipfs/go-ipfs/repo/fsrepo/serialize" serialize "github.com/ipfs/go-ipfs/repo/fsrepo/serialize"
dir "github.com/ipfs/go-ipfs/thirdparty/dir" dir "github.com/ipfs/go-ipfs/thirdparty/dir"
lockfile "gx/ipfs/QmPdqSMmiwtQCBC515gFtMW2mP14HsfgnyQ2k5xPQVxMge/go-fs-lock"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/mitchellh/go-homedir" "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/mitchellh/go-homedir"
...@@ -29,6 +29,10 @@ import ( ...@@ -29,6 +29,10 @@ import (
measure "gx/ipfs/QmbJgZGRtkFeSdCxBCPaMKWRDYbqMxHyFfvjQGcWzpqsDe/go-ds-measure" measure "gx/ipfs/QmbJgZGRtkFeSdCxBCPaMKWRDYbqMxHyFfvjQGcWzpqsDe/go-ds-measure"
) )
// LockFile is the filename of the repo lock, relative to config dir
// TODO rename repo lock and hide name
const LockFile = "repo.lock"
var log = logging.Logger("fsrepo") var log = logging.Logger("fsrepo")
// version number that we are currently expecting to see // version number that we are currently expecting to see
...@@ -126,7 +130,7 @@ func open(repoPath string) (repo.Repo, error) { ...@@ -126,7 +130,7 @@ func open(repoPath string) (repo.Repo, error) {
return nil, err return nil, err
} }
r.lockfile, err = lockfile.Lock(r.path) r.lockfile, err = lockfile.Lock(r.path, LockFile)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -297,7 +301,7 @@ func Init(repoPath string, conf *config.Config) error { ...@@ -297,7 +301,7 @@ func Init(repoPath string, conf *config.Config) error {
// process. If true, then the repo cannot be opened by this process. // process. If true, then the repo cannot be opened by this process.
func LockedByOtherProcess(repoPath string) (bool, error) { func LockedByOtherProcess(repoPath string) (bool, error) {
repoPath = filepath.Clean(repoPath) repoPath = filepath.Clean(repoPath)
locked, err := lockfile.Locked(repoPath) locked, err := lockfile.Locked(repoPath, LockFile)
if locked { if locked {
log.Debugf("(%t)<->Lock is held at %s", locked, repoPath) log.Debugf("(%t)<->Lock is held at %s", locked, repoPath)
} }
......
package lock
import (
"fmt"
"io"
"os"
"path"
"strings"
"syscall"
"gx/ipfs/QmNiJuT8Ja3hMVpBHXv3Q6dwmperaQ6JjLtpMQgMCD7xvx/go-ipfs-util"
logging "gx/ipfs/QmRb5jh8z2E8hMGN2tkvs1yHynUanqnZ3UeKwgN1i9P1F8/go-log"
lock "gx/ipfs/QmVUAoR89E6KDBJmsfRVkAoBMEfgVfy8rRmvzf4y9rWp1d/go4-lock"
)
// LockFile is the filename of the repo lock, relative to config dir
// TODO rename repo lock and hide name
const LockFile = "repo.lock"
// log is the fsrepo logger
var log = logging.Logger("lock")
func errPerm(path string) error {
return fmt.Errorf("failed to take lock at %s: permission denied", path)
}
func Lock(confdir string) (io.Closer, error) {
return lock.Lock(path.Join(confdir, LockFile))
}
func Locked(confdir string) (bool, error) {
log.Debugf("Checking lock")
if !util.FileExists(path.Join(confdir, LockFile)) {
log.Debugf("File doesn't exist: %s", path.Join(confdir, LockFile))
return false, nil
}
if lk, err := Lock(confdir); err != nil {
// EAGAIN == someone else has the lock
if err == syscall.EAGAIN {
log.Debugf("Someone else has the lock: %s", path.Join(confdir, LockFile))
return true, nil
}
if strings.Contains(err.Error(), "resource temporarily unavailable") {
log.Debugf("Can't lock file: %s.\n reason: %s", path.Join(confdir, LockFile), err.Error())
return true, nil
}
// lock fails on permissions error
if os.IsPermission(err) {
log.Debugf("Lock fails on permissions error")
return false, errPerm(confdir)
}
if isLockCreatePermFail(err) {
log.Debugf("Lock fails on permissions error")
return false, errPerm(confdir)
}
// otherwise, we cant guarantee anything, error out
return false, err
} else {
log.Debugf("No one has a lock")
lk.Close()
return false, nil
}
}
func isLockCreatePermFail(err error) bool {
s := err.Error()
return strings.Contains(s, "Lock Create of") && strings.Contains(s, "permission denied")
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论