@cryptotaxi247 / kubo / commits / 816787448

refactor: extract fs lock into go-fs-lock

License: MIT Signed-off-by: dignifiedquire <dignifiedquire@gmail.com>

dignifiedquire committed Jan 31, 2018 at 14:43 UTC 816787448f8dfd35212c24158040e41f77ce9a13
4 files changed +14 -75
core/commands/repo.go
+1 -2
@@ -15,7 +15,6 @@ import (
15 corerepo "github.com/ipfs/go-ipfs/core/corerepo"
16 config "github.com/ipfs/go-ipfs/repo/config"
17 fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
18 - lockfile "github.com/ipfs/go-ipfs/repo/fsrepo/lock"
18
19 bstore "gx/ipfs/QmaG4DZ4JaqEfvPWt5nPPgoTzhc1tr1T3f4Nu9Jpdm8ymY/go-ipfs-blockstore"
20 cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
@@ -233,7 +232,7 @@ daemons are running.
232 }
233
234 dsLockFile := filepath.Join(dsPath, "LOCK") // TODO: get this lockfile programmatically
236 - repoLockFile := filepath.Join(configRoot, lockfile.LockFile)
235 + repoLockFile := filepath.Join(configRoot, fsrepo.LockFile)
236 apiFile := filepath.Join(configRoot, "api") // TODO: get this programmatically
237
238 log.Infof("Removing repo lockfile: %s", repoLockFile)
package.json
+6
@@ -575,6 +575,12 @@
575 "hash": "QmWLWmRVSiagqP15jczsGME1qpob6HDbtbHAY2he9W5iUo",
576 "name": "opentracing-go",
577 "version": "0.0.3"
578 + },
579 + {
580 + "author": "dignifiedquire",
581 + "hash": "QmPdqSMmiwtQCBC515gFtMW2mP14HsfgnyQ2k5xPQVxMge",
582 + "name": "go-fs-lock",
583 + "version": "0.1.2"
584 }
585 ],
586 "gxVersion": "0.10.0",
repo/fsrepo/fsrepo.go
+7 -3
@@ -16,10 +16,10 @@ import (
16 repo "github.com/ipfs/go-ipfs/repo"
17 "github.com/ipfs/go-ipfs/repo/common"
18 config "github.com/ipfs/go-ipfs/repo/config"
19 - lockfile "github.com/ipfs/go-ipfs/repo/fsrepo/lock"
19 mfsr "github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
20 serialize "github.com/ipfs/go-ipfs/repo/fsrepo/serialize"
21 dir "github.com/ipfs/go-ipfs/thirdparty/dir"
22 + lockfile "gx/ipfs/QmPdqSMmiwtQCBC515gFtMW2mP14HsfgnyQ2k5xPQVxMge/go-fs-lock"
23
24 "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/mitchellh/go-homedir"
25
@@ -29,6 +29,10 @@ import (
29 measure "gx/ipfs/QmbJgZGRtkFeSdCxBCPaMKWRDYbqMxHyFfvjQGcWzpqsDe/go-ds-measure"
30 )
31
32 +// LockFile is the filename of the repo lock, relative to config dir
33 +// TODO rename repo lock and hide name
34 +const LockFile = "repo.lock"
35 +
36 var log = logging.Logger("fsrepo")
37
38 // version number that we are currently expecting to see
@@ -126,7 +130,7 @@ func open(repoPath string) (repo.Repo, error) {
130 return nil, err
131 }
132
129 - r.lockfile, err = lockfile.Lock(r.path)
133 + r.lockfile, err = lockfile.Lock(r.path, LockFile)
134 if err != nil {
135 return nil, err
136 }
@@ -297,7 +301,7 @@ func Init(repoPath string, conf *config.Config) error {
301 // process. If true, then the repo cannot be opened by this process.
302 func LockedByOtherProcess(repoPath string) (bool, error) {
303 repoPath = filepath.Clean(repoPath)
300 - locked, err := lockfile.Locked(repoPath)
304 + locked, err := lockfile.Locked(repoPath, LockFile)
305 if locked {
306 log.Debugf("(%t)<->Lock is held at %s", locked, repoPath)
307 }
repo/fsrepo/lock/lock.go deleted
-70
@@ -1,70 +0,0 @@
1 -package lock
2 -
3 -import (
4 - "fmt"
5 - "io"
6 - "os"
7 - "path"
8 - "strings"
9 - "syscall"
10 -
11 - "gx/ipfs/QmNiJuT8Ja3hMVpBHXv3Q6dwmperaQ6JjLtpMQgMCD7xvx/go-ipfs-util"
12 - logging "gx/ipfs/QmRb5jh8z2E8hMGN2tkvs1yHynUanqnZ3UeKwgN1i9P1F8/go-log"
13 - lock "gx/ipfs/QmVUAoR89E6KDBJmsfRVkAoBMEfgVfy8rRmvzf4y9rWp1d/go4-lock"
14 -)
15 -
16 -// LockFile is the filename of the repo lock, relative to config dir
17 -// TODO rename repo lock and hide name
18 -const LockFile = "repo.lock"
19 -
20 -// log is the fsrepo logger
21 -var log = logging.Logger("lock")
22 -
23 -func errPerm(path string) error {
24 - return fmt.Errorf("failed to take lock at %s: permission denied", path)
25 -}
26 -
27 -func Lock(confdir string) (io.Closer, error) {
28 - return lock.Lock(path.Join(confdir, LockFile))
29 -}
30 -
31 -func Locked(confdir string) (bool, error) {
32 - log.Debugf("Checking lock")
33 - if !util.FileExists(path.Join(confdir, LockFile)) {
34 - log.Debugf("File doesn't exist: %s", path.Join(confdir, LockFile))
35 - return false, nil
36 - }
37 - if lk, err := Lock(confdir); err != nil {
38 - // EAGAIN == someone else has the lock
39 - if err == syscall.EAGAIN {
40 - log.Debugf("Someone else has the lock: %s", path.Join(confdir, LockFile))
41 - return true, nil
42 - }
43 - if strings.Contains(err.Error(), "resource temporarily unavailable") {
44 - log.Debugf("Can't lock file: %s.\n reason: %s", path.Join(confdir, LockFile), err.Error())
45 - return true, nil
46 - }
47 -
48 - // lock fails on permissions error
49 - if os.IsPermission(err) {
50 - log.Debugf("Lock fails on permissions error")
51 - return false, errPerm(confdir)
52 - }
53 - if isLockCreatePermFail(err) {
54 - log.Debugf("Lock fails on permissions error")
55 - return false, errPerm(confdir)
56 - }
57 -
58 - // otherwise, we cant guarantee anything, error out
59 - return false, err
60 - } else {
61 - log.Debugf("No one has a lock")
62 - lk.Close()
63 - return false, nil
64 - }
65 -}
66 -
67 -func isLockCreatePermFail(err error) bool {
68 - s := err.Error()
69 - return strings.Contains(s, "Lock Create of") && strings.Contains(s, "permission denied")
70 -}