mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-30 18:13:54 +08:00
Merge pull request #1276 from ipfs/debug/perm-fail
trying to debug permissions failure
This commit is contained in:
@ -1,9 +1,12 @@
|
|||||||
package lock
|
package lock
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
lock "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/camlistore/lock"
|
lock "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/camlistore/lock"
|
||||||
"github.com/ipfs/go-ipfs/util"
|
"github.com/ipfs/go-ipfs/util"
|
||||||
@ -13,6 +16,10 @@ import (
|
|||||||
// TODO rename repo lock and hide name
|
// TODO rename repo lock and hide name
|
||||||
const LockFile = "repo.lock"
|
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) {
|
func Lock(confdir string) (io.Closer, error) {
|
||||||
c, err := lock.Lock(path.Join(confdir, LockFile))
|
c, err := lock.Lock(path.Join(confdir, LockFile))
|
||||||
return c, err
|
return c, err
|
||||||
@ -23,12 +30,28 @@ func Locked(confdir string) (bool, error) {
|
|||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
if lk, err := Lock(confdir); err != nil {
|
if lk, err := Lock(confdir); err != nil {
|
||||||
if os.IsPermission(err) {
|
// EAGAIN == someone else has the lock
|
||||||
return false, err
|
if err == syscall.EAGAIN {
|
||||||
|
return true, nil
|
||||||
}
|
}
|
||||||
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 {
|
} else {
|
||||||
lk.Close()
|
lk.Close()
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isLockCreatePermFail(err error) bool {
|
||||||
|
s := err.Error()
|
||||||
|
return strings.Contains(s, "Lock Create of") && strings.Contains(s, "permission denied")
|
||||||
|
}
|
||||||
|
@ -20,7 +20,7 @@ test_expect_success "ipfs init fails" '
|
|||||||
'
|
'
|
||||||
|
|
||||||
test_expect_success "ipfs init output looks good" '
|
test_expect_success "ipfs init output looks good" '
|
||||||
echo "Error: open $IPFS_PATH/repo.lock: permission denied" > init_fail_exp &&
|
echo "Error: failed to take lock at $IPFS_PATH: permission denied" > init_fail_exp &&
|
||||||
test_cmp init_fail_out init_fail_exp
|
test_cmp init_fail_out init_fail_exp
|
||||||
'
|
'
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user