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

fix: missing read-write flag in reopenFDOnError #95

Merged
merged 2 commits into from
Jul 22, 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
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