-
Notifications
You must be signed in to change notification settings - Fork 0
/
filelock_unix.go
49 lines (41 loc) · 1.04 KB
/
filelock_unix.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
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build !posix && (darwin || dragonfly || freebsd || illumos || linux || netbsd || openbsd)
package filelock
import (
"io/fs"
"syscall"
)
type lockType int16
const (
readLock lockType = syscall.LOCK_SH
readLockNB lockType = syscall.LOCK_SH | syscall.LOCK_NB
writeLock lockType = syscall.LOCK_EX
writeLockNB lockType = syscall.LOCK_EX | syscall.LOCK_NB
)
func lock(f File, lt lockType) (err error) {
for {
err = syscall.Flock(int(f.Fd()), int(lt))
if err != syscall.EINTR {
break
}
}
if err != nil {
if err == syscall.EWOULDBLOCK {
return ErrWouldBlock
}
return &fs.PathError{
Op: lt.String(),
Path: f.Name(),
Err: err,
}
}
return nil
}
func unlock(f File) error {
return lock(f, syscall.LOCK_UN)
}
func isNotSupported(err error) bool {
return err == syscall.ENOSYS || err == syscall.ENOTSUP || err == syscall.EOPNOTSUPP || err == ErrNotSupported
}