From b5eb65b0b02f7d3637d87990dab46fa5ce57b202 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Mon, 22 Jul 2024 18:25:39 +0200 Subject: [PATCH 1/2] fix missing read-write flag in reopenFDOnError --- flock_unix.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/flock_unix.go b/flock_unix.go index b27089a..025e05f 100644 --- a/flock_unix.go +++ b/flock_unix.go @@ -9,6 +9,7 @@ package flock import ( "errors" + "os" "golang.org/x/sys/unix" ) @@ -198,11 +199,12 @@ func (f *Flock) reopenFDOnError(err error) (bool, error) { f.resetFh() - // reopen the file handle - err = f.setFh() + // reopen in read-write mode and set the file handle + fh, err := os.OpenFile(f.path, f.flag|os.O_RDWR, f.perm) if err != nil { return false, err } + f.fh = fh return true, nil } From 57f59368939b2693091d8ac825ca241c2389305b Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Mon, 22 Jul 2024 22:12:25 +0200 Subject: [PATCH 2/2] review: change setFh signature --- flock.go | 4 ++-- flock_unix.go | 8 ++++---- flock_unix_fcntl.go | 4 ++-- flock_windows.go | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/flock.go b/flock.go index 73a2579..ff942b2 100644 --- a/flock.go +++ b/flock.go @@ -163,9 +163,9 @@ func tryCtx(ctx context.Context, fn func() (bool, error), retryDelay time.Durati } } -func (f *Flock) setFh() error { +func (f *Flock) setFh(flag int) error { // open a new os.File instance - fh, err := os.OpenFile(f.path, f.flag, f.perm) + fh, err := os.OpenFile(f.path, flag, f.perm) if err != nil { return err } diff --git a/flock_unix.go b/flock_unix.go index 025e05f..cf8919c 100644 --- a/flock_unix.go +++ b/flock_unix.go @@ -50,7 +50,7 @@ func (f *Flock) lock(locked *bool, flag int) error { } if f.fh == nil { - if err := f.setFh(); err != nil { + if err := f.setFh(f.flag); err != nil { return err } @@ -147,7 +147,7 @@ func (f *Flock) try(locked *bool, flag int) (bool, error) { } if f.fh == nil { - if err := f.setFh(); err != nil { + if err := f.setFh(f.flag); err != nil { return false, err } @@ -183,6 +183,7 @@ retry: // This comes from `util-linux/sys-utils/flock.c`: // > Since Linux 3.4 (commit 55725513) // > Probably NFSv4 where flock() is emulated by fcntl(). +// > https://github.com/util-linux/util-linux/blob/198e920aa24743ef6ace4e07cf6237de527f9261/sys-utils/flock.c#L374-L390 func (f *Flock) reopenFDOnError(err error) (bool, error) { if !errors.Is(err, unix.EIO) && !errors.Is(err, unix.EBADF) { return false, nil @@ -200,11 +201,10 @@ func (f *Flock) reopenFDOnError(err error) (bool, error) { f.resetFh() // reopen in read-write mode and set the file handle - fh, err := os.OpenFile(f.path, f.flag|os.O_RDWR, f.perm) + err = f.setFh(f.flag | os.O_RDWR) if err != nil { return false, err } - f.fh = fh return true, nil } diff --git a/flock_unix_fcntl.go b/flock_unix_fcntl.go index e882516..ea007b4 100644 --- a/flock_unix_fcntl.go +++ b/flock_unix_fcntl.go @@ -112,7 +112,7 @@ func (f *Flock) lock(locked *bool, flag lockType) error { } if f.fh == nil { - if err := f.setFh(); err != nil { + if err := f.setFh(f.flag); err != nil { return err } @@ -359,7 +359,7 @@ func (f *Flock) try(locked *bool, flag lockType) (bool, error) { } if f.fh == nil { - if err := f.setFh(); err != nil { + if err := f.setFh(f.flag); err != nil { return false, err } diff --git a/flock_windows.go b/flock_windows.go index 4df90e2..dfd31e1 100644 --- a/flock_windows.go +++ b/flock_windows.go @@ -56,7 +56,7 @@ func (f *Flock) lock(locked *bool, flag uint32) error { } if f.fh == nil { - if err := f.setFh(); err != nil { + if err := f.setFh(f.flag); err != nil { return err } @@ -136,7 +136,7 @@ func (f *Flock) try(locked *bool, flag uint32) (bool, error) { } if f.fh == nil { - if err := f.setFh(); err != nil { + if err := f.setFh(f.flag); err != nil { return false, err }