diff --git a/flock_unix.go b/flock_unix.go index 36e61cb..1c9d079 100644 --- a/flock_unix.go +++ b/flock_unix.go @@ -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. @@ -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. @@ -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 { @@ -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 { @@ -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 } @@ -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, @@ -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 } @@ -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. @@ -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) { @@ -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 @@ -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 } diff --git a/flock_unix_fcntl.go b/flock_unix_fcntl.go index ddb90ec..e882516 100644 --- a/flock_unix_fcntl.go +++ b/flock_unix_fcntl.go @@ -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() @@ -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 } diff --git a/flock_windows.go b/flock_windows.go index 98fdff9..4df90e2 100644 --- a/flock_windows.go +++ b/flock_windows.go @@ -9,7 +9,6 @@ package flock import ( "errors" - "syscall" "golang.org/x/sys/windows" ) @@ -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. @@ -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 } @@ -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 } @@ -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 }