From bcdebfb84e249738d6573fd678f8ff480f09e610 Mon Sep 17 00:00:00 2001 From: George Armhold Date: Wed, 25 Oct 2017 12:03:55 -0400 Subject: [PATCH 1/3] small cleanup: - be explicit when discarding returned errors from .Close(), etc. - remove named return values from funcs when naked return not used - fix some "err" shadowing when redeclaration not needed --- internal/backend/b2/b2.go | 4 ++-- internal/backend/local/local.go | 2 +- internal/cache/cache.go | 2 +- internal/cache/file.go | 2 +- internal/checker/checker.go | 4 ++-- internal/pipe/pipe.go | 2 +- internal/restic/lock.go | 2 +- internal/restic/restorer.go | 5 +++-- 8 files changed, 12 insertions(+), 11 deletions(-) diff --git a/internal/backend/b2/b2.go b/internal/backend/b2/b2.go index 4c68953b4..4bca95653 100644 --- a/internal/backend/b2/b2.go +++ b/internal/backend/b2/b2.go @@ -218,7 +218,7 @@ func (be *b2Backend) Load(ctx context.Context, h restic.Handle, length int, offs } // Save stores data in the backend at the handle. -func (be *b2Backend) Save(ctx context.Context, h restic.Handle, rd io.Reader) (err error) { +func (be *b2Backend) Save(ctx context.Context, h restic.Handle, rd io.Reader) (error) { ctx, cancel := context.WithCancel(ctx) defer cancel() @@ -233,7 +233,7 @@ func (be *b2Backend) Save(ctx context.Context, h restic.Handle, rd io.Reader) (e debug.Log("Save %v, name %v", h, name) obj := be.bucket.Object(name) - _, err = obj.Attrs(ctx) + _, err := obj.Attrs(ctx) if err == nil { debug.Log(" %v already exists", h) return errors.New("key already exists") diff --git a/internal/backend/local/local.go b/internal/backend/local/local.go index b738bff13..e5012771c 100644 --- a/internal/backend/local/local.go +++ b/internal/backend/local/local.go @@ -118,7 +118,7 @@ func (b *Local) IsNotExist(err error) bool { } // Save stores data in the backend at the handle. -func (b *Local) Save(ctx context.Context, h restic.Handle, rd io.Reader) (err error) { +func (b *Local) Save(ctx context.Context, h restic.Handle, rd io.Reader) (error) { debug.Log("Save %v", h) if err := h.Valid(); err != nil { return err diff --git a/internal/cache/cache.go b/internal/cache/cache.go index 0268b242d..f83142e19 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -70,7 +70,7 @@ func writeCachedirTag(dir string) error { debug.Log("Create CACHEDIR.TAG at %v", dir) if _, err := f.Write([]byte(cachedirTagSignature)); err != nil { - f.Close() + _ = f.Close() return errors.Wrap(err, "Write") } diff --git a/internal/cache/file.go b/internal/cache/file.go index 5041ad965..f232bc7c1 100644 --- a/internal/cache/file.go +++ b/internal/cache/file.go @@ -65,7 +65,7 @@ func (c *Cache) Load(h restic.Handle, length int, offset int64) (io.ReadCloser, if offset > 0 { if _, err = f.Seek(offset, io.SeekStart); err != nil { - f.Close() + _ = f.Close() return nil, err } } diff --git a/internal/checker/checker.go b/internal/checker/checker.go index e995634ed..c557ab475 100644 --- a/internal/checker/checker.go +++ b/internal/checker/checker.go @@ -674,8 +674,8 @@ func checkPack(ctx context.Context, r restic.Repository, id restic.ID) error { } defer func() { - packfile.Close() - os.Remove(packfile.Name()) + _ = packfile.Close() + _ = os.Remove(packfile.Name()) }() hrd := hashing.NewReader(rd, sha256.New()) diff --git a/internal/pipe/pipe.go b/internal/pipe/pipe.go index dfa140c0c..1cf7ff6bd 100644 --- a/internal/pipe/pipe.go +++ b/internal/pipe/pipe.go @@ -67,7 +67,7 @@ func readDirNames(dirname string) ([]string, error) { return nil, errors.Wrap(err, "Open") } names, err := f.Readdirnames(-1) - f.Close() + _ = f.Close() if err != nil { return nil, errors.Wrap(err, "Readdirnames") } diff --git a/internal/restic/lock.go b/internal/restic/lock.go index 0a4146174..177b0d707 100644 --- a/internal/restic/lock.go +++ b/internal/restic/lock.go @@ -109,7 +109,7 @@ func newLock(ctx context.Context, repo Repository, excl bool) (*Lock, error) { time.Sleep(waitBeforeLockCheck) if err = lock.checkForOtherLocks(ctx); err != nil { - lock.Unlock() + _ = lock.Unlock() return nil, err } diff --git a/internal/restic/restorer.go b/internal/restic/restorer.go index 9aafd5063..e3c4f4e36 100644 --- a/internal/restic/restorer.go +++ b/internal/restic/restorer.go @@ -51,7 +51,7 @@ func (res *Restorer) restoreTo(ctx context.Context, dst string, dir string, tree debug.Log("SelectFilter returned %v %v", selectedForRestore, childMayBeSelected) if selectedForRestore { - err := res.restoreNodeTo(ctx, node, dir, dst, idx) + err = res.restoreNodeTo(ctx, node, dir, dst, idx) if err != nil { return err } @@ -74,7 +74,8 @@ func (res *Restorer) restoreTo(ctx context.Context, dst string, dir string, tree if selectedForRestore { // Restore directory timestamp at the end. If we would do it earlier, restoring files within // the directory would overwrite the timestamp of the directory they are in. - if err := node.RestoreTimestamps(filepath.Join(dst, dir, node.Name)); err != nil { + err = node.RestoreTimestamps(filepath.Join(dst, dir, node.Name)) + if err != nil { return err } } From f5fa602482a396507430317499d5c71b3c91ba6a Mon Sep 17 00:00:00 2001 From: George Armhold Date: Thu, 26 Oct 2017 13:53:31 -0400 Subject: [PATCH 2/3] detect and return error from file Close() in Node.createFileAt() gh-1385 --- internal/restic/node.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/internal/restic/node.go b/internal/restic/node.go index 0ae773e98..f191b5131 100644 --- a/internal/restic/node.go +++ b/internal/restic/node.go @@ -253,8 +253,26 @@ func (node Node) createFileAt(ctx context.Context, path string, repo Repository, if err != nil { return errors.Wrap(err, "OpenFile") } - defer f.Close() + err = node.writeNodeContent(ctx, repo, f) + closeErr := f.Close() + + if err != nil { + return err + } + + if closeErr != nil { + return errors.Wrap(closeErr, "Close") + } + + if node.Links > 1 { + idx.Add(node.Inode, node.DeviceID, path) + } + + return nil +} + +func (node Node) writeNodeContent(ctx context.Context, repo Repository, f *os.File) (error) { var buf []byte for _, id := range node.Content { size, err := repo.LookupBlobSize(id, DataBlob) @@ -279,10 +297,6 @@ func (node Node) createFileAt(ctx context.Context, path string, repo Repository, } } - if node.Links > 1 { - idx.Add(node.Inode, node.DeviceID, path) - } - return nil } From eea96f652d17d3acc3c6e878df699c7142200aaf Mon Sep 17 00:00:00 2001 From: George Armhold Date: Thu, 26 Oct 2017 16:22:10 -0400 Subject: [PATCH 3/3] go fmt --- internal/backend/b2/b2.go | 2 +- internal/backend/local/local.go | 2 +- internal/restic/node.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/backend/b2/b2.go b/internal/backend/b2/b2.go index 4bca95653..72a502eb8 100644 --- a/internal/backend/b2/b2.go +++ b/internal/backend/b2/b2.go @@ -218,7 +218,7 @@ func (be *b2Backend) Load(ctx context.Context, h restic.Handle, length int, offs } // Save stores data in the backend at the handle. -func (be *b2Backend) Save(ctx context.Context, h restic.Handle, rd io.Reader) (error) { +func (be *b2Backend) Save(ctx context.Context, h restic.Handle, rd io.Reader) error { ctx, cancel := context.WithCancel(ctx) defer cancel() diff --git a/internal/backend/local/local.go b/internal/backend/local/local.go index e5012771c..91a250e43 100644 --- a/internal/backend/local/local.go +++ b/internal/backend/local/local.go @@ -118,7 +118,7 @@ func (b *Local) IsNotExist(err error) bool { } // Save stores data in the backend at the handle. -func (b *Local) Save(ctx context.Context, h restic.Handle, rd io.Reader) (error) { +func (b *Local) Save(ctx context.Context, h restic.Handle, rd io.Reader) error { debug.Log("Save %v", h) if err := h.Valid(); err != nil { return err diff --git a/internal/restic/node.go b/internal/restic/node.go index f191b5131..0adf4a524 100644 --- a/internal/restic/node.go +++ b/internal/restic/node.go @@ -272,7 +272,7 @@ func (node Node) createFileAt(ctx context.Context, path string, repo Repository, return nil } -func (node Node) writeNodeContent(ctx context.Context, repo Repository, f *os.File) (error) { +func (node Node) writeNodeContent(ctx context.Context, repo Repository, f *os.File) error { var buf []byte for _, id := range node.Content { size, err := repo.LookupBlobSize(id, DataBlob)