From 8bc19745a4911e188a5b15ee3085b0c292afd89c Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sat, 6 Jul 2024 18:39:20 +0200 Subject: [PATCH] refactor: add resetFh --- flock.go | 21 +++++++++++++++------ flock_unix.go | 3 +-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/flock.go b/flock.go index 4c99551..73a2579 100644 --- a/flock.go +++ b/flock.go @@ -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 } @@ -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() } diff --git a/flock_unix.go b/flock_unix.go index 6f695b8..b27089a 100644 --- a/flock_unix.go +++ b/flock_unix.go @@ -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()