diff --git a/core/commands/repo.go b/core/commands/repo.go index c84962b2caa..df4abb26e17 100644 --- a/core/commands/repo.go +++ b/core/commands/repo.go @@ -15,7 +15,6 @@ import ( corerepo "github.com/ipfs/go-ipfs/core/corerepo" config "github.com/ipfs/go-ipfs/repo/config" fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo" - lockfile "github.com/ipfs/go-ipfs/repo/fsrepo/lock" bstore "gx/ipfs/QmaG4DZ4JaqEfvPWt5nPPgoTzhc1tr1T3f4Nu9Jpdm8ymY/go-ipfs-blockstore" cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid" @@ -233,7 +232,7 @@ daemons are running. } 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 log.Infof("Removing repo lockfile: %s", repoLockFile) diff --git a/package.json b/package.json index 101e829aba9..e2f069fad5e 100644 --- a/package.json +++ b/package.json @@ -575,6 +575,12 @@ "hash": "QmWLWmRVSiagqP15jczsGME1qpob6HDbtbHAY2he9W5iUo", "name": "opentracing-go", "version": "0.0.3" + }, + { + "author": "dignifiedquire", + "hash": "QmPdqSMmiwtQCBC515gFtMW2mP14HsfgnyQ2k5xPQVxMge", + "name": "go-fs-lock", + "version": "0.1.2" } ], "gxVersion": "0.10.0", diff --git a/repo/fsrepo/fsrepo.go b/repo/fsrepo/fsrepo.go index 011d1d75128..197e7b61b39 100644 --- a/repo/fsrepo/fsrepo.go +++ b/repo/fsrepo/fsrepo.go @@ -16,7 +16,6 @@ import ( repo "github.com/ipfs/go-ipfs/repo" "github.com/ipfs/go-ipfs/repo/common" 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" serialize "github.com/ipfs/go-ipfs/repo/fsrepo/serialize" dir "github.com/ipfs/go-ipfs/thirdparty/dir" @@ -24,11 +23,16 @@ import ( "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/mitchellh/go-homedir" util "gx/ipfs/QmNiJuT8Ja3hMVpBHXv3Q6dwmperaQ6JjLtpMQgMCD7xvx/go-ipfs-util" + lockfile "gx/ipfs/QmPdqSMmiwtQCBC515gFtMW2mP14HsfgnyQ2k5xPQVxMge/go-fs-lock" logging "gx/ipfs/QmRb5jh8z2E8hMGN2tkvs1yHynUanqnZ3UeKwgN1i9P1F8/go-log" ma "gx/ipfs/QmWWQ2Txc2c6tqjsBpzg5Ar652cHPGNsQQp2SejkNmkUMb/go-multiaddr" 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") // version number that we are currently expecting to see @@ -126,7 +130,7 @@ func open(repoPath string) (repo.Repo, error) { return nil, err } - r.lockfile, err = lockfile.Lock(r.path) + r.lockfile, err = lockfile.Lock(r.path, LockFile) if err != nil { return nil, err } @@ -297,7 +301,7 @@ func Init(repoPath string, conf *config.Config) error { // process. If true, then the repo cannot be opened by this process. func LockedByOtherProcess(repoPath string) (bool, error) { repoPath = filepath.Clean(repoPath) - locked, err := lockfile.Locked(repoPath) + locked, err := lockfile.Locked(repoPath, LockFile) if locked { log.Debugf("(%t)<->Lock is held at %s", locked, repoPath) } diff --git a/repo/fsrepo/lock/lock.go b/repo/fsrepo/lock/lock.go deleted file mode 100644 index 336926e370a..00000000000 --- a/repo/fsrepo/lock/lock.go +++ /dev/null @@ -1,70 +0,0 @@ -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") -}