Skip to content

Commit

Permalink
Fix error checking (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto authored Nov 13, 2023
1 parent 21cb665 commit beb04e4
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 23 deletions.
5 changes: 3 additions & 2 deletions cmd-x-index-all.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"math/rand"
Expand Down Expand Up @@ -211,7 +212,7 @@ func createAllIndexes(
for {
_cid, sectionLength, block, err := rd.NextNode()
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
return nil, err
Expand Down Expand Up @@ -611,7 +612,7 @@ func verifyAllIndexes(
for {
_cid, sectionLength, block, err := rd.NextNode()
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
return err
Expand Down
3 changes: 2 additions & 1 deletion gsfa/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package manifest

import (
"encoding/binary"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -213,7 +214,7 @@ func (m *Manifest) readAllContent() (Values, error) {
values := make([][2]uint64, 0, currentContentSize/16)
for {
_, err := io.ReadFull(sectionReader, buf)
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion index-cid-to-offset.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func CreateIndex_cid2offset(ctx context.Context, tmpDir string, carPath string,
for {
c, sectionLength, err := rd.NextInfo()
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
return "", err
Expand Down
3 changes: 2 additions & 1 deletion readahead/readahead.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package readahead

import (
"bytes"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -78,7 +79,7 @@ func (cr *CachingReader) Read(p []byte) (int, error) {
if n > 0 {
cr.buffer.Write(tmp[:n])
}
if err == io.EOF && cr.buffer.Len() == 0 {
if errors.Is(err, io.EOF) && cr.buffer.Len() == 0 {
// If EOF is reached and buffer is empty, return EOF
return 0, io.EOF
}
Expand Down
6 changes: 3 additions & 3 deletions readers.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func isDirEmpty(dir string) (bool, error) {
defer file.Close()

_, err = file.Readdir(1)
if err == io.EOF {
if errors.Is(err, io.EOF) {
return true, nil
}
return false, err
Expand Down Expand Up @@ -202,7 +202,7 @@ func carCountItems(carPath string) (uint64, error) {
for {
_, _, err := rd.NextInfo()
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
return 0, err
Expand Down Expand Up @@ -230,7 +230,7 @@ func carCountItemsByFirstByte(carPath string) (map[byte]uint64, error) {
for {
_, _, block, err := rd.NextNode()
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
return nil, err
Expand Down
5 changes: 3 additions & 2 deletions store/index/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package index
import (
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -308,7 +309,7 @@ func (index *Index) reapIndexRecords(ctx context.Context, fileNum uint32, indexP
return false, ctx.Err()
}
if _, err = file.ReadAt(sizeBuf, pos); err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
// Finished reading entire index.
break
}
Expand Down Expand Up @@ -348,7 +349,7 @@ func (index *Index) reapIndexRecords(ctx context.Context, fileNum uint32, indexP
}
data := scratch[:size]
if _, err = file.ReadAt(data, pos+sizePrefixSize); err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
// The data has not been written yet, or the file is corrupt.
// Take the data we are able to use and move on.
break
Expand Down
9 changes: 5 additions & 4 deletions store/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"bytes"
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -362,7 +363,7 @@ func scanIndexFile(ctx context.Context, basePath string, fileNum uint32, buckets
var i int
for {
if _, err = file.ReadAt(sizeBuffer, pos); err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
// Finished reading entire index.
break
}
Expand Down Expand Up @@ -392,7 +393,7 @@ func scanIndexFile(ctx context.Context, basePath string, fileNum uint32, buckets
}
data := scratch[:size]
if _, err = file.ReadAt(data, pos); err != nil {
if err == io.ErrUnexpectedEOF || err == io.EOF {
if err == io.ErrUnexpectedEOF || errors.Is(err, io.EOF) {
// The file is corrupt since the expected data could not be
// read. Take the usable data and move on.
log.Errorw("Unexpected EOF scanning index record", "file", indexPath)
Expand Down Expand Up @@ -1059,7 +1060,7 @@ func (iter *RawIterator) Next() ([]byte, types.Position, bool, error) {
_, err := iter.file.ReadAt(sizeBuf, iter.pos)
if err != nil {
iter.file.Close()
if err == io.EOF {
if errors.Is(err, io.EOF) {
iter.file = nil
iter.fileNum++
return iter.Next()
Expand Down Expand Up @@ -1488,7 +1489,7 @@ func MoveFiles(indexPath, newDir string) error {
for {
fileName, err := fileIter.next()
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
return err
Expand Down
3 changes: 2 additions & 1 deletion store/index/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bufio"
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -91,7 +92,7 @@ func chunkOldIndex(ctx context.Context, file *os.File, name string, fileSizeLimi
for {
_, err = io.ReadFull(reader, sizeBuffer)
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
return 0, err
Expand Down
4 changes: 2 additions & 2 deletions store/index/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func testScanIndexFile(file *os.File, fileNum uint32, buckets Buckets, prevSize
for {
_, err := io.ReadFull(buffered, sizeBuffer)
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
return err
Expand All @@ -119,7 +119,7 @@ func testScanIndexFile(file *os.File, fileNum uint32, buckets Buckets, prevSize
data := scratch[:size]
_, err = io.ReadFull(buffered, data)
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
return errors.New("unexpected EOF")
}
return err
Expand Down
2 changes: 1 addition & 1 deletion store/primary/gsfaprimary/gsfaprimary.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ func (iter *Iterator) Next() ([]byte, []byte, error) {
_, err := iter.file.ReadAt(data, pos)
if err != nil {
iter.file.Close()
// if err == io.EOF {
// if errors.Is(err, io.EOF) {
// err = io.ErrUnexpectedEOF
// }
return nil, nil, err
Expand Down
4 changes: 2 additions & 2 deletions store/primary/gsfaprimary/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func testScanPrimaryFile(file *os.File) ([][]byte, error) {
for {
_, err := io.ReadFull(buffered, sizeBuffer)
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
return nil, err
Expand All @@ -143,7 +143,7 @@ func testScanPrimaryFile(file *os.File) ([][]byte, error) {
data := scratch[:size]
_, err = io.ReadFull(buffered, data)
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
return nil, errors.New("unexpected EOF")
}
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions store/primary/sig2epochprimary/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func testScanPrimaryFile(file *os.File) ([][]byte, error) {
for {
_, err := io.ReadFull(buffered, sizeBuffer)
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
return nil, err
Expand All @@ -143,7 +143,7 @@ func testScanPrimaryFile(file *os.File) ([][]byte, error) {
data := scratch[:size]
_, err = io.ReadFull(buffered, data)
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
return nil, errors.New("unexpected EOF")
}
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package store_test

import (
"context"
"errors"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -89,7 +90,7 @@ func TestUpdate(t *testing.T) {
var count int
for {
key, val, err := storeIter.Next()
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
require.Zero(t, count)
Expand Down

0 comments on commit beb04e4

Please sign in to comment.