From 75f53955ee3ffe3c5199dee35810ce35c96b8806 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sat, 30 Jan 2021 16:32:00 +0100 Subject: [PATCH] errcheck: Add error checks Most added checks are straight forward. --- internal/backend/foreground_test.go | 4 ++- internal/fuse/snapshots_dir.go | 45 +++++++++++++++++++----- internal/hashing/writer.go | 10 +++++- internal/repository/index_test.go | 6 +++- internal/repository/master_index_test.go | 5 ++- internal/repository/repack_test.go | 6 +++- internal/repository/repository.go | 11 +++--- 7 files changed, 69 insertions(+), 18 deletions(-) diff --git a/internal/backend/foreground_test.go b/internal/backend/foreground_test.go index 34c55d1f3..81adefe32 100644 --- a/internal/backend/foreground_test.go +++ b/internal/backend/foreground_test.go @@ -23,7 +23,9 @@ func TestForeground(t *testing.T) { bg, err := backend.StartForeground(cmd) rtest.OK(t, err) - defer cmd.Wait() + defer func() { + rtest.OK(t, cmd.Wait()) + }() err = bg() rtest.OK(t, err) diff --git a/internal/fuse/snapshots_dir.go b/internal/fuse/snapshots_dir.go index bdbdbe60b..34484b597 100644 --- a/internal/fuse/snapshots_dir.go +++ b/internal/fuse/snapshots_dir.go @@ -229,7 +229,10 @@ func updateSnapshots(ctx context.Context, root *Root) error { if root.snCount != len(snapshots) { root.snCount = len(snapshots) - root.repo.LoadIndex(ctx) + err := root.repo.LoadIndex(ctx) + if err != nil { + return err + } root.snapshots = snapshots } root.lastCheck = time.Now() @@ -272,7 +275,10 @@ func (d *SnapshotsDir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) { debug.Log("ReadDirAll()") // update snapshots - updateSnapshots(ctx, d.root) + err := updateSnapshots(ctx, d.root) + if err != nil { + return nil, err + } // update snapshot names updateSnapshotNames(d, d.root.cfg.SnapshotTemplate) @@ -314,7 +320,10 @@ func (d *SnapshotsIDSDir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) debug.Log("ReadDirAll()") // update snapshots - updateSnapshots(ctx, d.root) + err := updateSnapshots(ctx, d.root) + if err != nil { + return nil, err + } // update snapshot ids updateSnapshotIDSNames(d) @@ -348,7 +357,10 @@ func (d *HostsDir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) { debug.Log("ReadDirAll()") // update snapshots - updateSnapshots(ctx, d.root) + err := updateSnapshots(ctx, d.root) + if err != nil { + return nil, err + } // update host names updateHostsNames(d) @@ -382,7 +394,10 @@ func (d *TagsDir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) { debug.Log("ReadDirAll()") // update snapshots - updateSnapshots(ctx, d.root) + err := updateSnapshots(ctx, d.root) + if err != nil { + return nil, err + } // update tag names updateTagNames(d) @@ -443,7 +458,10 @@ func (d *SnapshotsDir) Lookup(ctx context.Context, name string) (fs.Node, error) sn, ok := d.names[name] if !ok { // could not find entry. Updating repository-state - updateSnapshots(ctx, d.root) + err := updateSnapshots(ctx, d.root) + if err != nil { + return nil, err + } // update snapshot names updateSnapshotNames(d, d.root.cfg.SnapshotTemplate) @@ -476,7 +494,10 @@ func (d *SnapshotsIDSDir) Lookup(ctx context.Context, name string) (fs.Node, err sn, ok := d.names[name] if !ok { // could not find entry. Updating repository-state - updateSnapshots(ctx, d.root) + err := updateSnapshots(ctx, d.root) + if err != nil { + return nil, err + } // update snapshot ids updateSnapshotIDSNames(d) @@ -499,7 +520,10 @@ func (d *HostsDir) Lookup(ctx context.Context, name string) (fs.Node, error) { _, ok := d.hosts[name] if !ok { // could not find entry. Updating repository-state - updateSnapshots(ctx, d.root) + err := updateSnapshots(ctx, d.root) + if err != nil { + return nil, err + } // update host names updateHostsNames(d) @@ -522,7 +546,10 @@ func (d *TagsDir) Lookup(ctx context.Context, name string) (fs.Node, error) { _, ok := d.tags[name] if !ok { // could not find entry. Updating repository-state - updateSnapshots(ctx, d.root) + err := updateSnapshots(ctx, d.root) + if err != nil { + return nil, err + } // update tag names updateTagNames(d) diff --git a/internal/hashing/writer.go b/internal/hashing/writer.go index 8eb157a9f..0b2d8c5b2 100644 --- a/internal/hashing/writer.go +++ b/internal/hashing/writer.go @@ -21,8 +21,16 @@ func NewWriter(w io.Writer, h hash.Hash) *Writer { // Write wraps the write method of the underlying writer and also hashes all data. func (h *Writer) Write(p []byte) (int, error) { + // write the data to the underlying writing n, err := h.w.Write(p) - h.h.Write(p[:n]) + + // according to the interface documentation, Write() on a hash.Hash never + // returns an error. + _, hashErr := h.h.Write(p[:n]) + if hashErr != nil { + panic(hashErr) + } + return n, err } diff --git a/internal/repository/index_test.go b/internal/repository/index_test.go index 1084558a6..c4f0179db 100644 --- a/internal/repository/index_test.go +++ b/internal/repository/index_test.go @@ -301,7 +301,11 @@ var ( func initBenchmarkIndexJSON() { idx, _ := createRandomIndex(rand.New(rand.NewSource(0)), 200000) var buf bytes.Buffer - idx.Encode(&buf) + err := idx.Encode(&buf) + if err != nil { + panic(err) + } + benchmarkIndexJSON = buf.Bytes() } diff --git a/internal/repository/master_index_test.go b/internal/repository/master_index_test.go index 3c279696e..167cad2f8 100644 --- a/internal/repository/master_index_test.go +++ b/internal/repository/master_index_test.go @@ -338,7 +338,10 @@ func TestIndexSave(t *testing.T) { repo, cleanup := createFilledRepo(t, 3, 0) defer cleanup() - repo.LoadIndex(context.TODO()) + err := repo.LoadIndex(context.TODO()) + if err != nil { + t.Fatal(err) + } obsoletes, err := repo.Index().(*repository.MasterIndex).Save(context.TODO(), repo, nil, nil, nil) if err != nil { diff --git a/internal/repository/repack_test.go b/internal/repository/repack_test.go index a4505d4d2..108c167d9 100644 --- a/internal/repository/repack_test.go +++ b/internal/repository/repack_test.go @@ -201,7 +201,11 @@ func rebuildIndex(t *testing.T, repo restic.Repository) { } func reloadIndex(t *testing.T, repo restic.Repository) { - repo.SetIndex(repository.NewMasterIndex()) + err := repo.SetIndex(repository.NewMasterIndex()) + if err != nil { + t.Fatal(err) + } + if err := repo.LoadIndex(context.TODO()); err != nil { t.Fatalf("error loading new index: %v", err) } diff --git a/internal/repository/repository.go b/internal/repository/repository.go index a0d27cc3f..298800008 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -781,16 +781,19 @@ func DownloadAndHash(ctx context.Context, be Loader, h restic.Handle) (tmpfile * hash = restic.IDFromHash(hrd.Sum(nil)) return ierr }) + if err != nil { - tmpfile.Close() - os.Remove(tmpfile.Name()) + // ignore subsequent errors + _ = tmpfile.Close() + _ = os.Remove(tmpfile.Name()) return nil, restic.ID{}, -1, errors.Wrap(err, "Load") } _, err = tmpfile.Seek(0, io.SeekStart) if err != nil { - tmpfile.Close() - os.Remove(tmpfile.Name()) + // ignore subsequent errors + _ = tmpfile.Close() + _ = os.Remove(tmpfile.Name()) return nil, restic.ID{}, -1, errors.Wrap(err, "Seek") }