Skip to content

Commit 4da6f6a

Browse files
committed
fix: detect correct "locked by others" error on windows
Also use the newer "errors.Is" where possible.
1 parent 5cb87f5 commit 4da6f6a

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

Diff for: fslock.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func Lock(confdir, lockFileName string) (io.Closer, error) {
4242
Path: lockFilePath,
4343
Err: LockedError("lock is already held by us"),
4444
}
45-
case os.IsPermission(err) || isLockCreatePermFail(err):
45+
case errors.Is(err, os.ErrPermission) || isLockCreatePermFail(err):
4646
// lock fails on permissions error
4747

4848
// Using a path error like this ensures that

Diff for: fslock_posix.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,27 @@
33
package fslock
44

55
import (
6+
"errors"
67
"strings"
78
"syscall"
89
)
910

11+
var lockedMsgs = []string{
12+
// unix
13+
"resource temporarily unavailable",
14+
// linux
15+
"cannot access the file because it is being used by another process",
16+
}
17+
1018
func lockedByOthers(err error) bool {
11-
return err == syscall.EAGAIN || strings.Contains(err.Error(), "resource temporarily unavailable")
19+
if errors.Is(err, syscall.EAGAIN) {
20+
return true
21+
}
22+
msg := err.Error()
23+
for _, target := range lockedMsgs {
24+
if strings.Contains(msg, target) {
25+
return true
26+
}
27+
}
28+
return false
1229
}

Diff for: fslock_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func TestLockedByOthers(t *testing.T) {
153153
}
154154
pe, ok := err.(*os.PathError)
155155
if !ok {
156-
t.Fatalf("wrong error type %T", err)
156+
t.Fatalf("wrong error type %T: %s", err, err)
157157
}
158158
if got := pe.Error(); !strings.Contains(got, wantErr) {
159159
t.Fatalf("error %q does not contain %q", got, wantErr)

0 commit comments

Comments
 (0)