Skip to content

Commit 7ff173f

Browse files
authored
chore: factorize (#91)
1 parent 688d3b5 commit 7ff173f

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

flock.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,11 @@ func (f *Flock) setFh() error {
176176
return nil
177177
}
178178

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

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

192+
// ensure the file handle is closed if no lock is held.
193+
func (f *Flock) ensureFhState() {
194+
if f.l || f.r || f.fh == nil {
195+
return
196+
}
197+
198+
f.resetFh()
199+
}
200+
190201
func (f *Flock) reset() {
191202
f.l = false
192203
f.r = false
193204

194-
_ = f.fh.Close()
195-
196-
f.fh = nil
205+
f.resetFh()
197206
}

flock_unix.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package flock
99

1010
import (
1111
"errors"
12-
"os"
1312

1413
"golang.org/x/sys/unix"
1514
)
@@ -197,16 +196,13 @@ func (f *Flock) reopenFDOnError(err error) (bool, error) {
197196
return false, nil
198197
}
199198

200-
_ = f.fh.Close()
201-
f.fh = nil
199+
f.resetFh()
202200

203-
// reopen in read-write mode and set the file handle
204-
fh, err := os.OpenFile(f.path, f.flag, f.perm)
201+
// reopen the file handle
202+
err = f.setFh()
205203
if err != nil {
206204
return false, err
207205
}
208206

209-
f.fh = fh
210-
211207
return true, nil
212208
}

0 commit comments

Comments
 (0)