-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock.go
60 lines (51 loc) · 1.31 KB
/
lock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package lock
import (
"fmt"
"io"
"os"
"path"
"strings"
"syscall"
lock "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/camlistore/lock"
"github.com/ipfs/go-ipfs/util"
)
// LockFile is the filename of the repo lock, relative to config dir
// TODO rename repo lock and hide name
const LockFile = "repo.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) {
c, err := lock.Lock(path.Join(confdir, LockFile))
return c, err
}
func Locked(confdir string) (bool, error) {
if !util.FileExists(path.Join(confdir, LockFile)) {
return false, nil
}
if lk, err := Lock(confdir); err != nil {
// EAGAIN == someone else has the lock
if err == syscall.EAGAIN {
return true, nil
}
if strings.Contains(err.Error(), "can't Lock file") {
return true, nil
}
// lock fails on permissions error
if os.IsPermission(err) {
return false, errPerm(confdir)
}
if isLockCreatePermFail(err) {
return false, errPerm(confdir)
}
// otherwise, we cant guarantee anything, error out
return false, err
} else {
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")
}