errcheck: Add error checks

Most added checks are straight forward.
This commit is contained in:
Alexander Neumann 2021-01-30 16:32:00 +01:00
parent 1632a84e7b
commit 75f53955ee
7 changed files with 69 additions and 18 deletions

View File

@ -23,7 +23,9 @@ func TestForeground(t *testing.T) {
bg, err := backend.StartForeground(cmd) bg, err := backend.StartForeground(cmd)
rtest.OK(t, err) rtest.OK(t, err)
defer cmd.Wait() defer func() {
rtest.OK(t, cmd.Wait())
}()
err = bg() err = bg()
rtest.OK(t, err) rtest.OK(t, err)

View File

@ -229,7 +229,10 @@ func updateSnapshots(ctx context.Context, root *Root) error {
if root.snCount != len(snapshots) { if root.snCount != len(snapshots) {
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.snapshots = snapshots
} }
root.lastCheck = time.Now() root.lastCheck = time.Now()
@ -272,7 +275,10 @@ func (d *SnapshotsDir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
debug.Log("ReadDirAll()") debug.Log("ReadDirAll()")
// update snapshots // update snapshots
updateSnapshots(ctx, d.root) err := updateSnapshots(ctx, d.root)
if err != nil {
return nil, err
}
// update snapshot names // update snapshot names
updateSnapshotNames(d, d.root.cfg.SnapshotTemplate) updateSnapshotNames(d, d.root.cfg.SnapshotTemplate)
@ -314,7 +320,10 @@ func (d *SnapshotsIDSDir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error)
debug.Log("ReadDirAll()") debug.Log("ReadDirAll()")
// update snapshots // update snapshots
updateSnapshots(ctx, d.root) err := updateSnapshots(ctx, d.root)
if err != nil {
return nil, err
}
// update snapshot ids // update snapshot ids
updateSnapshotIDSNames(d) updateSnapshotIDSNames(d)
@ -348,7 +357,10 @@ func (d *HostsDir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
debug.Log("ReadDirAll()") debug.Log("ReadDirAll()")
// update snapshots // update snapshots
updateSnapshots(ctx, d.root) err := updateSnapshots(ctx, d.root)
if err != nil {
return nil, err
}
// update host names // update host names
updateHostsNames(d) updateHostsNames(d)
@ -382,7 +394,10 @@ func (d *TagsDir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
debug.Log("ReadDirAll()") debug.Log("ReadDirAll()")
// update snapshots // update snapshots
updateSnapshots(ctx, d.root) err := updateSnapshots(ctx, d.root)
if err != nil {
return nil, err
}
// update tag names // update tag names
updateTagNames(d) updateTagNames(d)
@ -443,7 +458,10 @@ func (d *SnapshotsDir) Lookup(ctx context.Context, name string) (fs.Node, error)
sn, ok := d.names[name] sn, ok := d.names[name]
if !ok { if !ok {
// could not find entry. Updating repository-state // 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 // update snapshot names
updateSnapshotNames(d, d.root.cfg.SnapshotTemplate) 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] sn, ok := d.names[name]
if !ok { if !ok {
// could not find entry. Updating repository-state // 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 // update snapshot ids
updateSnapshotIDSNames(d) updateSnapshotIDSNames(d)
@ -499,7 +520,10 @@ func (d *HostsDir) Lookup(ctx context.Context, name string) (fs.Node, error) {
_, ok := d.hosts[name] _, ok := d.hosts[name]
if !ok { if !ok {
// could not find entry. Updating repository-state // 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 // update host names
updateHostsNames(d) updateHostsNames(d)
@ -522,7 +546,10 @@ func (d *TagsDir) Lookup(ctx context.Context, name string) (fs.Node, error) {
_, ok := d.tags[name] _, ok := d.tags[name]
if !ok { if !ok {
// could not find entry. Updating repository-state // 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 // update tag names
updateTagNames(d) updateTagNames(d)

View File

@ -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. // Write wraps the write method of the underlying writer and also hashes all data.
func (h *Writer) Write(p []byte) (int, error) { func (h *Writer) Write(p []byte) (int, error) {
// write the data to the underlying writing
n, err := h.w.Write(p) 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 return n, err
} }

View File

@ -301,7 +301,11 @@ var (
func initBenchmarkIndexJSON() { func initBenchmarkIndexJSON() {
idx, _ := createRandomIndex(rand.New(rand.NewSource(0)), 200000) idx, _ := createRandomIndex(rand.New(rand.NewSource(0)), 200000)
var buf bytes.Buffer var buf bytes.Buffer
idx.Encode(&buf) err := idx.Encode(&buf)
if err != nil {
panic(err)
}
benchmarkIndexJSON = buf.Bytes() benchmarkIndexJSON = buf.Bytes()
} }

View File

@ -338,7 +338,10 @@ func TestIndexSave(t *testing.T) {
repo, cleanup := createFilledRepo(t, 3, 0) repo, cleanup := createFilledRepo(t, 3, 0)
defer cleanup() 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) obsoletes, err := repo.Index().(*repository.MasterIndex).Save(context.TODO(), repo, nil, nil, nil)
if err != nil { if err != nil {

View File

@ -201,7 +201,11 @@ func rebuildIndex(t *testing.T, repo restic.Repository) {
} }
func reloadIndex(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 { if err := repo.LoadIndex(context.TODO()); err != nil {
t.Fatalf("error loading new index: %v", err) t.Fatalf("error loading new index: %v", err)
} }

View File

@ -781,16 +781,19 @@ func DownloadAndHash(ctx context.Context, be Loader, h restic.Handle) (tmpfile *
hash = restic.IDFromHash(hrd.Sum(nil)) hash = restic.IDFromHash(hrd.Sum(nil))
return ierr return ierr
}) })
if err != nil { if err != nil {
tmpfile.Close() // ignore subsequent errors
os.Remove(tmpfile.Name()) _ = tmpfile.Close()
_ = os.Remove(tmpfile.Name())
return nil, restic.ID{}, -1, errors.Wrap(err, "Load") return nil, restic.ID{}, -1, errors.Wrap(err, "Load")
} }
_, err = tmpfile.Seek(0, io.SeekStart) _, err = tmpfile.Seek(0, io.SeekStart)
if err != nil { if err != nil {
tmpfile.Close() // ignore subsequent errors
os.Remove(tmpfile.Name()) _ = tmpfile.Close()
_ = os.Remove(tmpfile.Name())
return nil, restic.ID{}, -1, errors.Wrap(err, "Seek") return nil, restic.ID{}, -1, errors.Wrap(err, "Seek")
} }