From cdb78e04869fce4d29de2feef84b7111f2035dee Mon Sep 17 00:00:00 2001 From: Philip O'Toole Date: Sun, 15 Oct 2023 09:56:11 -0400 Subject: [PATCH] Move to Sink opened flag --- snapshot2/sink.go | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/snapshot2/sink.go b/snapshot2/sink.go index fa36d902..920655a0 100644 --- a/snapshot2/sink.go +++ b/snapshot2/sink.go @@ -24,7 +24,7 @@ type Sink struct { snapTmpDirPath string dataFD *os.File - closed bool + opened bool } // NewSink creates a new Sink object. @@ -37,6 +37,11 @@ func NewSink(str *Store, meta *raft.SnapshotMeta) *Sink { // Open opens the sink for writing. func (s *Sink) Open() error { + if s.opened { + return nil + } + s.opened = true + // Make temp snapshot directory s.snapTmpDirPath = filepath.Join(s.str.Dir(), tmpName(s.meta.ID)) if err := os.MkdirAll(s.snapTmpDirPath, 0755); err != nil { @@ -66,25 +71,25 @@ func (s *Sink) ID() string { // Cancel cancels the snapshot. Cancel must be called if the snapshot is not // going to be closed. func (s *Sink) Cancel() error { - if s.dataFD != nil { - if err := s.dataFD.Close(); err != nil { - return err - } - s.dataFD = nil + if !s.opened { + return nil } + s.opened = false + if err := s.dataFD.Close(); err != nil { + return err + } + s.dataFD = nil return RemoveAllTmpSnapshotData(s.str.Dir()) } // Close closes the sink, and finalizes creation of the snapshot. It is critical // that Close is called, or the snapshot will not be in place. func (s *Sink) Close() error { - if s.closed { - return nil - } - s.closed = true - if s.dataFD == nil { + if !s.opened { return nil } + s.opened = false + if err := s.dataFD.Close(); err != nil { return err }