-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlock_unix.go
More file actions
67 lines (58 loc) · 1.38 KB
/
lock_unix.go
File metadata and controls
67 lines (58 loc) · 1.38 KB
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
//go:build !windows
package update
import (
"os"
"syscall"
)
// fileLock represents a file-based lock for coordinating updates across processes
type fileLock struct {
path string
file *os.File
}
// newFileLock creates a new file lock at the given path
func newFileLock(path string) *fileLock {
return &fileLock{path: path + ".lock"}
}
// Lock acquires an exclusive lock, blocking until available
func (l *fileLock) Lock() error {
f, err := os.OpenFile(l.path, os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return err
}
l.file = f
return syscall.Flock(int(f.Fd()), syscall.LOCK_EX)
}
// TryLock attempts to acquire the lock without blocking
// Returns true if lock was acquired, false if already held by another process
func (l *fileLock) TryLock() (bool, error) {
f, err := os.OpenFile(l.path, os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return false, err
}
l.file = f
err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
if err != nil {
_ = f.Close()
l.file = nil
if err == syscall.EWOULDBLOCK {
return false, nil
}
return false, err
}
return true, nil
}
// Unlock releases the lock
func (l *fileLock) Unlock() error {
if l.file == nil {
return nil
}
if err := syscall.Flock(int(l.file.Fd()), syscall.LOCK_UN); err != nil {
return err
}
if err := l.file.Close(); err != nil {
return err
}
l.file = nil
_ = os.Remove(l.path)
return nil
}