Skip to content

Commit

Permalink
refactor: add resetFh
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Jul 6, 2024
1 parent 396584b commit 8bc1974
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
21 changes: 15 additions & 6 deletions flock.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,11 @@ func (f *Flock) setFh() error {
return nil
}

// ensure the file handle is closed if no lock is held.
func (f *Flock) ensureFhState() {
if f.l || f.r || f.fh == nil {
// resetFh resets file handle:
// - tries to close the file (ignore errors)
// - sets fh to nil.
func (f *Flock) resetFh() {
if f.fh == nil {
return
}

Expand All @@ -187,11 +189,18 @@ func (f *Flock) ensureFhState() {
f.fh = nil
}

// ensure the file handle is closed if no lock is held.
func (f *Flock) ensureFhState() {
if f.l || f.r || f.fh == nil {
return
}

f.resetFh()
}

func (f *Flock) reset() {
f.l = false
f.r = false

_ = f.fh.Close()

f.fh = nil
f.resetFh()
}
3 changes: 1 addition & 2 deletions flock_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,7 @@ func (f *Flock) reopenFDOnError(err error) (bool, error) {
return false, nil
}

_ = f.fh.Close()
f.fh = nil
f.resetFh()

// reopen the file handle
err = f.setFh()
Expand Down

0 comments on commit 8bc1974

Please sign in to comment.