From 2742299c2aaa6d7616c6825b54f72b10e536ab95 Mon Sep 17 00:00:00 2001 From: Vadim Inshakov Date: Sun, 13 Aug 2023 18:09:39 +0500 Subject: [PATCH] fix truncate err handling --- wal.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/wal.go b/wal.go index ab5eac8..d6db18a 100644 --- a/wal.go +++ b/wal.go @@ -728,7 +728,7 @@ func (l *Log) truncateFront(index uint64) (err error) { ebuf := s.ebuf[epos[0].pos:] // Create a temp file contains the truncated segment. tempName := filepath.Join(l.path, "TEMP") - err = func() error { + if err = func() error { f, err := os.OpenFile(tempName, os.O_CREATE|os.O_RDWR|os.O_TRUNC, l.opts.FilePerms) if err != nil { return err @@ -741,7 +741,9 @@ func (l *Log) truncateFront(index uint64) (err error) { return err } return f.Close() - }() + }(); err != nil { + return fmt.Errorf("failed to create temp file for new start segment: %w", err) + } // Rename the TEMP file to it's START file name. startName := filepath.Join(l.path, segmentName(index)+".START") if err = os.Rename(tempName, startName); err != nil { @@ -834,7 +836,7 @@ func (l *Log) truncateBack(index uint64) (err error) { ebuf := s.ebuf[:epos[len(epos)-1].end] // Create a temp file contains the truncated segment. tempName := filepath.Join(l.path, "TEMP") - err = func() error { + if err = func() error { f, err := os.OpenFile(tempName, os.O_CREATE|os.O_RDWR|os.O_TRUNC, l.opts.FilePerms) if err != nil { return err @@ -847,7 +849,9 @@ func (l *Log) truncateBack(index uint64) (err error) { return err } return f.Close() - }() + }(); err != nil { + return fmt.Errorf("failed to create temp file for new end segment: %w", err) + } // Rename the TEMP file to it's END file name. endName := filepath.Join(l.path, segmentName(s.index)+".END") if err = os.Rename(tempName, endName); err != nil {