Skip to content

Commit

Permalink
chore: factorize (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Jul 6, 2024
1 parent 688d3b5 commit 7ff173f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 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()
}
10 changes: 3 additions & 7 deletions flock_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package flock

import (
"errors"
"os"

"golang.org/x/sys/unix"
)
Expand Down Expand Up @@ -197,16 +196,13 @@ func (f *Flock) reopenFDOnError(err error) (bool, error) {
return false, nil
}

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

// reopen in read-write mode and set the file handle
fh, err := os.OpenFile(f.path, f.flag, f.perm)
// reopen the file handle
err = f.setFh()
if err != nil {
return false, err
}

f.fh = fh

return true, nil
}

0 comments on commit 7ff173f

Please sign in to comment.