Skip to content

Commit

Permalink
fix: missing read-write flag in reopenFDOnError (#95)
Browse files Browse the repository at this point in the history
Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
  • Loading branch information
crazy-max and ldez authored Jul 22, 2024
1 parent ffdd9d4 commit 9de625d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions flock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 6 additions & 4 deletions flock_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package flock

import (
"errors"
"os"

"golang.org/x/sys/unix"
)
Expand Down Expand Up @@ -49,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
}

Expand Down Expand Up @@ -146,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
}

Expand Down Expand Up @@ -182,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
Expand All @@ -198,8 +200,8 @@ 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
err = f.setFh(f.flag | os.O_RDWR)
if err != nil {
return false, err
}
Expand Down
4 changes: 2 additions & 2 deletions flock_unix_fcntl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions flock_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 9de625d

Please sign in to comment.