-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlock_nix.go
79 lines (66 loc) · 1.57 KB
/
lock_nix.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//go:build !windows
// +build !windows
package k6provider
import (
"errors"
"fmt"
"path/filepath"
"sync"
"syscall"
)
// A dir lock prevents concurrent access to a directory.
// This code is inspired on the golang's filelock package:
// https://pkg.go.dev/cmd/go/internal/lockedfile/internal/filelock
type dirLock struct {
mutex sync.Mutex
lockFile string
fd int
}
func newDirLock(path string) *dirLock {
return &dirLock{
lockFile: filepath.Join(path, ".lock"),
fd: -1,
}
}
// tryLock places an advisory write lock on the directory
// If the directory is locked, returns ErrLocked immediately.
// If tryLock returns nil, no other process will be able to place a lock until
// this process exits or unlocks it.
func (m *dirLock) tryLock() error {
m.mutex.Lock()
defer m.mutex.Unlock()
// file open, assume already locked
if m.fd != -1 {
return nil
}
fd, err := syscall.Open(m.lockFile, syscall.O_RDWR|syscall.O_CREAT, 0o600)
if err != nil {
return fmt.Errorf("%w %w", errLockFailed, err)
}
err = syscall.Flock(fd, syscall.LOCK_EX|syscall.LOCK_NB)
if err == nil {
m.fd = fd
return nil
}
if errors.Is(err, syscall.EWOULDBLOCK) {
return errLocked
}
return fmt.Errorf("%w %w", errLockFailed, err)
}
func (m *dirLock) unlock() error {
m.mutex.Lock()
defer m.mutex.Unlock()
// if file is not open, assume already unlocked
if m.fd == -1 {
return nil
}
defer func() {
_ = syscall.Close(m.fd)
m.fd = -1
}()
err := syscall.Flock(m.fd, syscall.LOCK_UN)
if err != nil {
return fmt.Errorf("%w %w", errUnLockFailed, err)
}
return nil
}