Skip to content

Commit 96fd11a

Browse files
authored
refactor: use x/sys/* instead of Syscall when possible (#87)
1 parent c527283 commit 96fd11a

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

flock_unix.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ package flock
1010
import (
1111
"errors"
1212
"os"
13-
"syscall"
13+
14+
"golang.org/x/sys/unix"
1415
)
1516

1617
// Lock is a blocking call to try and take an exclusive file lock.
@@ -26,7 +27,7 @@ import (
2627
// Be careful when using exclusive locks in conjunction with shared locks (RLock()),
2728
// because calling Unlock() may accidentally release the exclusive lock that was once a shared lock.
2829
func (f *Flock) Lock() error {
29-
return f.lock(&f.l, syscall.LOCK_EX)
30+
return f.lock(&f.l, unix.LOCK_EX)
3031
}
3132

3233
// RLock is a blocking call to try and take a shared file lock.
@@ -37,7 +38,7 @@ func (f *Flock) Lock() error {
3738
// If we are already shared-locked,
3839
// this function short-circuits and returns immediately assuming it can take the mutex lock.
3940
func (f *Flock) RLock() error {
40-
return f.lock(&f.r, syscall.LOCK_SH)
41+
return f.lock(&f.r, unix.LOCK_SH)
4142
}
4243

4344
func (f *Flock) lock(locked *bool, flag int) error {
@@ -56,7 +57,7 @@ func (f *Flock) lock(locked *bool, flag int) error {
5657
defer f.ensureFhState()
5758
}
5859

59-
err := syscall.Flock(int(f.fh.Fd()), flag)
60+
err := unix.Flock(int(f.fh.Fd()), flag)
6061
if err != nil {
6162
shouldRetry, reopenErr := f.reopenFDOnError(err)
6263
if reopenErr != nil {
@@ -67,7 +68,7 @@ func (f *Flock) lock(locked *bool, flag int) error {
6768
return err
6869
}
6970

70-
err = syscall.Flock(int(f.fh.Fd()), flag)
71+
err = unix.Flock(int(f.fh.Fd()), flag)
7172
if err != nil {
7273
return err
7374
}
@@ -83,7 +84,7 @@ func (f *Flock) lock(locked *bool, flag int) error {
8384
// so while it is running the Locked() and RLocked() functions will be blocked.
8485
//
8586
// 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.
8788
// It does not remove the file from disk. It's up to your application to do.
8889
//
8990
// Please note,
@@ -101,7 +102,7 @@ func (f *Flock) Unlock() error {
101102
}
102103

103104
// 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)
105106
if err != nil {
106107
return err
107108
}
@@ -121,7 +122,7 @@ func (f *Flock) Unlock() error {
121122
// the function will return false instead of waiting for the lock.
122123
// If we get the lock, we also set the *Flock instance as being exclusive-locked.
123124
func (f *Flock) TryLock() (bool, error) {
124-
return f.try(&f.l, syscall.LOCK_EX)
125+
return f.try(&f.l, unix.LOCK_EX)
125126
}
126127

127128
// TryRLock is the preferred function for taking a shared file lock.
@@ -134,7 +135,7 @@ func (f *Flock) TryLock() (bool, error) {
134135
// the function will return false instead of waiting for the lock.
135136
// If we get the lock, we also set the *Flock instance as being share-locked.
136137
func (f *Flock) TryRLock() (bool, error) {
137-
return f.try(&f.r, syscall.LOCK_SH)
138+
return f.try(&f.r, unix.LOCK_SH)
138139
}
139140

140141
func (f *Flock) try(locked *bool, flag int) (bool, error) {
@@ -155,10 +156,10 @@ func (f *Flock) try(locked *bool, flag int) (bool, error) {
155156

156157
var retried bool
157158
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)
159160

160161
switch {
161-
case errors.Is(err, syscall.EWOULDBLOCK):
162+
case errors.Is(err, unix.EWOULDBLOCK):
162163
return false, nil
163164
case err == nil:
164165
*locked = true
@@ -183,7 +184,7 @@ retry:
183184
// > Since Linux 3.4 (commit 55725513)
184185
// > Probably NFSv4 where flock() is emulated by fcntl().
185186
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) {
187188
return false, nil
188189
}
189190

flock_unix_fcntl.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ func (f *Flock) doLock(cmd cmdType, lt lockType, blocking bool) (bool, error) {
140140
return false, err
141141
}
142142

143+
// Note(ldez): don't replace `syscall.Stat_t` by `unix.Stat_t` because `FileInfo.Sys()` returns `syscall.Stat_t`
143144
ino := fi.Sys().(*syscall.Stat_t).Ino
144145

145146
mu.Lock()
@@ -234,7 +235,7 @@ func (f *Flock) doLock(cmd cmdType, lt lockType, blocking bool) (bool, error) {
234235
const maxSleep = 500 * time.Millisecond
235236
for {
236237
err = setlkw(f.fh.Fd(), cmd, lt)
237-
if !errors.Is(err, syscall.EDEADLK) {
238+
if !errors.Is(err, unix.EDEADLK) {
238239
break
239240
}
240241

flock_windows.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package flock
99

1010
import (
1111
"errors"
12-
"syscall"
1312

1413
"golang.org/x/sys/windows"
1514
)
@@ -24,7 +23,7 @@ const winLockfileSharedLock = 0x00000000
2423

2524
// ErrorLockViolation is the error code returned from the Windows syscall when a lock would block,
2625
// and you ask to fail immediately.
27-
const ErrorLockViolation syscall.Errno = 0x21 // 33
26+
const ErrorLockViolation windows.Errno = 0x21 // 33
2827

2928
// Lock is a blocking call to try and take an exclusive file lock.
3029
// It will wait until it is able to obtain the exclusive file lock.
@@ -65,7 +64,7 @@ func (f *Flock) lock(locked *bool, flag uint32) error {
6564
}
6665

6766
err := windows.LockFileEx(windows.Handle(f.fh.Fd()), flag, 0, 1, 0, &windows.Overlapped{})
68-
if err != nil && !errors.Is(err, syscall.Errno(0)) {
67+
if err != nil && !errors.Is(err, windows.Errno(0)) {
6968
return err
7069
}
7170

@@ -94,7 +93,7 @@ func (f *Flock) Unlock() error {
9493

9594
// mark the file as unlocked
9695
err := windows.UnlockFileEx(windows.Handle(f.fh.Fd()), 0, 1, 0, &windows.Overlapped{})
97-
if err != nil && !errors.Is(err, syscall.Errno(0)) {
96+
if err != nil && !errors.Is(err, windows.Errno(0)) {
9897
return err
9998
}
10099

@@ -145,8 +144,8 @@ func (f *Flock) try(locked *bool, flag uint32) (bool, error) {
145144
}
146145

147146
err := windows.LockFileEx(windows.Handle(f.fh.Fd()), flag|windows.LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, &windows.Overlapped{})
148-
if err != nil && !errors.Is(err, syscall.Errno(0)) {
149-
if errors.Is(err, ErrorLockViolation) || errors.Is(err, syscall.ERROR_IO_PENDING) {
147+
if err != nil && !errors.Is(err, windows.Errno(0)) {
148+
if errors.Is(err, ErrorLockViolation) || errors.Is(err, windows.ERROR_IO_PENDING) {
150149
return false, nil
151150
}
152151

0 commit comments

Comments
 (0)