Skip to content

Commit

Permalink
review: change setFh signature
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Jul 22, 2024
1 parent b5eb65b commit 57f5936
Show file tree
Hide file tree
Showing 4 changed files with 10 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
8 changes: 4 additions & 4 deletions flock_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

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

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

Please sign in to comment.