Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use x/sys/* instead of Syscall when possible #87

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions flock_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ package flock
import (
"errors"
"os"
"syscall"

"golang.org/x/sys/unix"
)

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

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

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

err := syscall.Flock(int(f.fh.Fd()), flag)
err := unix.Flock(int(f.fh.Fd()), flag)
if err != nil {
shouldRetry, reopenErr := f.reopenFDOnError(err)
if reopenErr != nil {
Expand All @@ -67,7 +68,7 @@ func (f *Flock) lock(locked *bool, flag int) error {
return err
}

err = syscall.Flock(int(f.fh.Fd()), flag)
err = unix.Flock(int(f.fh.Fd()), flag)
if err != nil {
return err
}
Expand All @@ -83,7 +84,7 @@ func (f *Flock) lock(locked *bool, flag int) error {
// so while it is running the Locked() and RLocked() functions will be blocked.
//
// This function short-circuits if we are unlocked already.
// If not, it calls syscall.LOCK_UN on the file and closes the file descriptor.
// If not, it calls unix.LOCK_UN on the file and closes the file descriptor.
// It does not remove the file from disk. It's up to your application to do.
//
// Please note,
Expand All @@ -101,7 +102,7 @@ func (f *Flock) Unlock() error {
}

// Mark the file as unlocked.
err := syscall.Flock(int(f.fh.Fd()), syscall.LOCK_UN)
err := unix.Flock(int(f.fh.Fd()), unix.LOCK_UN)
if err != nil {
return err
}
Expand All @@ -121,7 +122,7 @@ func (f *Flock) Unlock() error {
// the function will return false instead of waiting for the lock.
// If we get the lock, we also set the *Flock instance as being exclusive-locked.
func (f *Flock) TryLock() (bool, error) {
return f.try(&f.l, syscall.LOCK_EX)
return f.try(&f.l, unix.LOCK_EX)
}

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

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

var retried bool
retry:
err := syscall.Flock(int(f.fh.Fd()), flag|syscall.LOCK_NB)
err := unix.Flock(int(f.fh.Fd()), flag|unix.LOCK_NB)

switch {
case errors.Is(err, syscall.EWOULDBLOCK):
case errors.Is(err, unix.EWOULDBLOCK):
return false, nil
case err == nil:
*locked = true
Expand All @@ -183,7 +184,7 @@ retry:
// > Since Linux 3.4 (commit 55725513)
// > Probably NFSv4 where flock() is emulated by fcntl().
func (f *Flock) reopenFDOnError(err error) (bool, error) {
if !errors.Is(err, syscall.EIO) && !errors.Is(err, syscall.EBADF) {
if !errors.Is(err, unix.EIO) && !errors.Is(err, unix.EBADF) {
return false, nil
}

Expand Down
3 changes: 2 additions & 1 deletion flock_unix_fcntl.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func (f *Flock) doLock(cmd cmdType, lt lockType, blocking bool) (bool, error) {
return false, err
}

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

mu.Lock()
Expand Down Expand Up @@ -234,7 +235,7 @@ func (f *Flock) doLock(cmd cmdType, lt lockType, blocking bool) (bool, error) {
const maxSleep = 500 * time.Millisecond
for {
err = setlkw(f.fh.Fd(), cmd, lt)
if !errors.Is(err, syscall.EDEADLK) {
if !errors.Is(err, unix.EDEADLK) {
break
}

Expand Down
11 changes: 5 additions & 6 deletions flock_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package flock

import (
"errors"
"syscall"

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

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

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

err := windows.LockFileEx(windows.Handle(f.fh.Fd()), flag, 0, 1, 0, &windows.Overlapped{})
if err != nil && !errors.Is(err, syscall.Errno(0)) {
if err != nil && !errors.Is(err, windows.Errno(0)) {
return err
}

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

// mark the file as unlocked
err := windows.UnlockFileEx(windows.Handle(f.fh.Fd()), 0, 1, 0, &windows.Overlapped{})
if err != nil && !errors.Is(err, syscall.Errno(0)) {
if err != nil && !errors.Is(err, windows.Errno(0)) {
return err
}

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

err := windows.LockFileEx(windows.Handle(f.fh.Fd()), flag|windows.LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, &windows.Overlapped{})
if err != nil && !errors.Is(err, syscall.Errno(0)) {
if errors.Is(err, ErrorLockViolation) || errors.Is(err, syscall.ERROR_IO_PENDING) {
if err != nil && !errors.Is(err, windows.Errno(0)) {
if errors.Is(err, ErrorLockViolation) || errors.Is(err, windows.ERROR_IO_PENDING) {
return false, nil
}

Expand Down