Skip to content

Commit

Permalink
fix truncate err handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vadiminshakov committed Aug 13, 2023
1 parent e70ddec commit 2742299
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 2742299

Please sign in to comment.