@@ -10,7 +10,8 @@ package flock
10
10
import (
11
11
"errors"
12
12
"os"
13
- "syscall"
13
+
14
+ "golang.org/x/sys/unix"
14
15
)
15
16
16
17
// Lock is a blocking call to try and take an exclusive file lock.
@@ -26,7 +27,7 @@ import (
26
27
// Be careful when using exclusive locks in conjunction with shared locks (RLock()),
27
28
// because calling Unlock() may accidentally release the exclusive lock that was once a shared lock.
28
29
func (f * Flock ) Lock () error {
29
- return f .lock (& f .l , syscall .LOCK_EX )
30
+ return f .lock (& f .l , unix .LOCK_EX )
30
31
}
31
32
32
33
// RLock is a blocking call to try and take a shared file lock.
@@ -37,7 +38,7 @@ func (f *Flock) Lock() error {
37
38
// If we are already shared-locked,
38
39
// this function short-circuits and returns immediately assuming it can take the mutex lock.
39
40
func (f * Flock ) RLock () error {
40
- return f .lock (& f .r , syscall .LOCK_SH )
41
+ return f .lock (& f .r , unix .LOCK_SH )
41
42
}
42
43
43
44
func (f * Flock ) lock (locked * bool , flag int ) error {
@@ -56,7 +57,7 @@ func (f *Flock) lock(locked *bool, flag int) error {
56
57
defer f .ensureFhState ()
57
58
}
58
59
59
- err := syscall .Flock (int (f .fh .Fd ()), flag )
60
+ err := unix .Flock (int (f .fh .Fd ()), flag )
60
61
if err != nil {
61
62
shouldRetry , reopenErr := f .reopenFDOnError (err )
62
63
if reopenErr != nil {
@@ -67,7 +68,7 @@ func (f *Flock) lock(locked *bool, flag int) error {
67
68
return err
68
69
}
69
70
70
- err = syscall .Flock (int (f .fh .Fd ()), flag )
71
+ err = unix .Flock (int (f .fh .Fd ()), flag )
71
72
if err != nil {
72
73
return err
73
74
}
@@ -83,7 +84,7 @@ func (f *Flock) lock(locked *bool, flag int) error {
83
84
// so while it is running the Locked() and RLocked() functions will be blocked.
84
85
//
85
86
// This function short-circuits if we are unlocked already.
86
- // If not, it calls syscall .LOCK_UN on the file and closes the file descriptor.
87
+ // If not, it calls unix .LOCK_UN on the file and closes the file descriptor.
87
88
// It does not remove the file from disk. It's up to your application to do.
88
89
//
89
90
// Please note,
@@ -101,7 +102,7 @@ func (f *Flock) Unlock() error {
101
102
}
102
103
103
104
// Mark the file as unlocked.
104
- err := syscall .Flock (int (f .fh .Fd ()), syscall .LOCK_UN )
105
+ err := unix .Flock (int (f .fh .Fd ()), unix .LOCK_UN )
105
106
if err != nil {
106
107
return err
107
108
}
@@ -121,7 +122,7 @@ func (f *Flock) Unlock() error {
121
122
// the function will return false instead of waiting for the lock.
122
123
// If we get the lock, we also set the *Flock instance as being exclusive-locked.
123
124
func (f * Flock ) TryLock () (bool , error ) {
124
- return f .try (& f .l , syscall .LOCK_EX )
125
+ return f .try (& f .l , unix .LOCK_EX )
125
126
}
126
127
127
128
// TryRLock is the preferred function for taking a shared file lock.
@@ -134,7 +135,7 @@ func (f *Flock) TryLock() (bool, error) {
134
135
// the function will return false instead of waiting for the lock.
135
136
// If we get the lock, we also set the *Flock instance as being share-locked.
136
137
func (f * Flock ) TryRLock () (bool , error ) {
137
- return f .try (& f .r , syscall .LOCK_SH )
138
+ return f .try (& f .r , unix .LOCK_SH )
138
139
}
139
140
140
141
func (f * Flock ) try (locked * bool , flag int ) (bool , error ) {
@@ -155,10 +156,10 @@ func (f *Flock) try(locked *bool, flag int) (bool, error) {
155
156
156
157
var retried bool
157
158
retry:
158
- err := syscall .Flock (int (f .fh .Fd ()), flag | syscall .LOCK_NB )
159
+ err := unix .Flock (int (f .fh .Fd ()), flag | unix .LOCK_NB )
159
160
160
161
switch {
161
- case errors .Is (err , syscall .EWOULDBLOCK ):
162
+ case errors .Is (err , unix .EWOULDBLOCK ):
162
163
return false , nil
163
164
case err == nil :
164
165
* locked = true
@@ -183,7 +184,7 @@ retry:
183
184
// > Since Linux 3.4 (commit 55725513)
184
185
// > Probably NFSv4 where flock() is emulated by fcntl().
185
186
func (f * Flock ) reopenFDOnError (err error ) (bool , error ) {
186
- if ! errors .Is (err , syscall .EIO ) && ! errors .Is (err , syscall .EBADF ) {
187
+ if ! errors .Is (err , unix .EIO ) && ! errors .Is (err , unix .EBADF ) {
187
188
return false , nil
188
189
}
189
190
0 commit comments