refactor: extract fs lock into go-fs-lock

License: MIT
Signed-off-by: dignifiedquire <dignifiedquire@gmail.com>
This commit is contained in:
dignifiedquire 2018-01-31 14:43:08 +01:00 committed by Steven Allen
parent 3f6519b420
commit 816787448f
4 changed files with 14 additions and 75 deletions

View File

@ -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)

View File

@ -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",

View File

@ -16,10 +16,10 @@ 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"
lockfile "gx/ipfs/QmPdqSMmiwtQCBC515gFtMW2mP14HsfgnyQ2k5xPQVxMge/go-fs-lock"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/mitchellh/go-homedir"
@ -29,6 +29,10 @@ import (
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)
}

View File

@ -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")
}