From 971ecee17118344e5d09751cb112deff7d0f8513 Mon Sep 17 00:00:00 2001 From: Pauline Middelink Date: Wed, 17 May 2017 01:25:52 +0200 Subject: [PATCH 1/4] Fix ineffassign mistakes --- build.go | 18 ++++-------------- src/cmds/restic/integration_test.go | 2 +- src/restic/snapshot_filter_test.go | 4 ++++ 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/build.go b/build.go index 4c3351d51..7b614f145 100644 --- a/build.go +++ b/build.go @@ -125,6 +125,7 @@ func copyFile(dst, src string) error { if err != nil { return err } + defer fsrc.Close() if err = os.MkdirAll(filepath.Dir(dst), 0755); err != nil { fmt.Printf("MkdirAll(%v)\n", filepath.Dir(dst)) @@ -135,28 +136,17 @@ func copyFile(dst, src string) error { if err != nil { return err } + defer fdst.Close() - if _, err = io.Copy(fdst, fsrc); err != nil { - return err - } - - if err == nil { - err = fsrc.Close() - } - - if err == nil { - err = fdst.Close() - } - + _, err = io.Copy(fdst, fsrc) if err == nil { err = os.Chmod(dst, fi.Mode()) } - if err == nil { err = os.Chtimes(dst, fi.ModTime(), fi.ModTime()) } - return nil + return err } // die prints the message with fmt.Fprintf() to stderr and exits with an error diff --git a/src/cmds/restic/integration_test.go b/src/cmds/restic/integration_test.go index be1ce755e..e499e6394 100644 --- a/src/cmds/restic/integration_test.go +++ b/src/cmds/restic/integration_test.go @@ -559,7 +559,7 @@ func TestBackupExclude(t *testing.T) { opts.Excludes = []string{"*.tar.gz", "private/secret"} testRunBackup(t, []string{datadir}, opts, gopts) - snapshots, snapshotID = lastSnapshot(snapshots, loadSnapshotMap(t, gopts)) + _, snapshotID = lastSnapshot(snapshots, loadSnapshotMap(t, gopts)) files = testRunLs(t, gopts, snapshotID) Assert(t, !includes(files, filepath.Join(string(filepath.Separator), "testdata", "foo.tar.gz")), "expected file %q not in first snapshot, but it's included", "foo.tar.gz") diff --git a/src/restic/snapshot_filter_test.go b/src/restic/snapshot_filter_test.go index 05e25fab6..1bf1e7460 100644 --- a/src/restic/snapshot_filter_test.go +++ b/src/restic/snapshot_filter_test.go @@ -212,6 +212,10 @@ func TestApplyPolicy(t *testing.T) { var want restic.Snapshots err = json.Unmarshal(buf, &want) + if err != nil { + t.Errorf("error unmarshalling golden file %v: %v", goldenFilename, err) + continue + } if !reflect.DeepEqual(keep, want) { t.Errorf("test %v: wrong result, want:\n %v\ngot:\n %v", i, want, keep) From 2b9323529ffaca78d60b119bb70b58f898ce4999 Mon Sep 17 00:00:00 2001 From: Pauline Middelink Date: Wed, 17 May 2017 01:28:39 +0200 Subject: [PATCH 2/4] Fix gofmt -s warnings --- src/cmds/restic/cmd_prune.go | 2 +- src/cmds/restic/cmd_rebuild_index.go | 2 +- src/restic/archiver/archive_reader.go | 2 +- src/restic/archiver/archiver_test.go | 2 +- src/restic/fuse/file.go | 2 +- src/restic/node_test.go | 16 ++++++++-------- src/restic/options/options_test.go | 18 +++++++++--------- src/restic/repository/repack.go | 2 +- src/restic/tree_test.go | 10 +++++----- src/restic/walk/walk_test.go | 2 +- 10 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/cmds/restic/cmd_prune.go b/src/cmds/restic/cmd_prune.go index 03d14d300..2fb5a4c12 100644 --- a/src/cmds/restic/cmd_prune.go +++ b/src/cmds/restic/cmd_prune.go @@ -92,7 +92,7 @@ func pruneRepository(gopts GlobalOptions, repo restic.Repository) error { } Verbosef("counting files in repo\n") - for _ = range repo.List(restic.DataFile, ctx.Done()) { + for range repo.List(restic.DataFile, ctx.Done()) { stats.packs++ } diff --git a/src/cmds/restic/cmd_rebuild_index.go b/src/cmds/restic/cmd_rebuild_index.go index e392b80ca..72fa5d574 100644 --- a/src/cmds/restic/cmd_rebuild_index.go +++ b/src/cmds/restic/cmd_rebuild_index.go @@ -45,7 +45,7 @@ func rebuildIndex(ctx context.Context, repo restic.Repository) error { Verbosef("counting files in repo\n") var packs uint64 - for _ = range repo.List(restic.DataFile, ctx.Done()) { + for range repo.List(restic.DataFile, ctx.Done()) { packs++ } diff --git a/src/restic/archiver/archive_reader.go b/src/restic/archiver/archive_reader.go index 6ed72ab96..28f228828 100644 --- a/src/restic/archiver/archive_reader.go +++ b/src/restic/archiver/archive_reader.go @@ -72,7 +72,7 @@ func (r *Reader) Archive(name string, rd io.Reader, p *restic.Progress) (*restic tree := &restic.Tree{ Nodes: []*restic.Node{ - &restic.Node{ + { Name: name, AccessTime: time.Now(), ModTime: time.Now(), diff --git a/src/restic/archiver/archiver_test.go b/src/restic/archiver/archiver_test.go index 72e3b3a96..3536ff727 100644 --- a/src/restic/archiver/archiver_test.go +++ b/src/restic/archiver/archiver_test.go @@ -129,7 +129,7 @@ func BenchmarkArchiveDirectory(b *testing.B) { } func countPacks(repo restic.Repository, t restic.FileType) (n uint) { - for _ = range repo.Backend().List(t, nil) { + for range repo.Backend().List(t, nil) { n++ } diff --git a/src/restic/fuse/file.go b/src/restic/fuse/file.go index dfc8aa308..1e943212a 100644 --- a/src/restic/fuse/file.go +++ b/src/restic/fuse/file.go @@ -143,7 +143,7 @@ func (f *file) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadR } if offset > 0 { - blob = blob[offset:len(blob)] + blob = blob[offset:] offset = 0 } diff --git a/src/restic/node_test.go b/src/restic/node_test.go index 16414150f..f0c16b368 100644 --- a/src/restic/node_test.go +++ b/src/restic/node_test.go @@ -71,7 +71,7 @@ func parseTime(s string) time.Time { } var nodeTests = []restic.Node{ - restic.Node{ + { Name: "testFile", Type: "file", Content: restic.IDs{}, @@ -82,7 +82,7 @@ var nodeTests = []restic.Node{ AccessTime: parseTime("2015-05-14 21:07:24.222"), ChangeTime: parseTime("2015-05-14 21:07:25.333"), }, - restic.Node{ + { Name: "testSuidFile", Type: "file", Content: restic.IDs{}, @@ -93,7 +93,7 @@ var nodeTests = []restic.Node{ AccessTime: parseTime("2015-05-14 21:07:24.222"), ChangeTime: parseTime("2015-05-14 21:07:25.333"), }, - restic.Node{ + { Name: "testSuidFile2", Type: "file", Content: restic.IDs{}, @@ -104,7 +104,7 @@ var nodeTests = []restic.Node{ AccessTime: parseTime("2015-05-14 21:07:24.222"), ChangeTime: parseTime("2015-05-14 21:07:25.333"), }, - restic.Node{ + { Name: "testSticky", Type: "file", Content: restic.IDs{}, @@ -115,7 +115,7 @@ var nodeTests = []restic.Node{ AccessTime: parseTime("2015-05-14 21:07:24.222"), ChangeTime: parseTime("2015-05-14 21:07:25.333"), }, - restic.Node{ + { Name: "testDir", Type: "dir", Subtree: nil, @@ -126,7 +126,7 @@ var nodeTests = []restic.Node{ AccessTime: parseTime("2015-05-14 21:07:24.222"), ChangeTime: parseTime("2015-05-14 21:07:25.333"), }, - restic.Node{ + { Name: "testSymlink", Type: "symlink", LinkTarget: "invalid", @@ -140,7 +140,7 @@ var nodeTests = []restic.Node{ // include "testFile" and "testDir" again with slightly different // metadata, so we can test if CreateAt works with pre-existing files. - restic.Node{ + { Name: "testFile", Type: "file", Content: restic.IDs{}, @@ -151,7 +151,7 @@ var nodeTests = []restic.Node{ AccessTime: parseTime("2005-05-14 21:07:04.222"), ChangeTime: parseTime("2005-05-14 21:07:05.333"), }, - restic.Node{ + { Name: "testDir", Type: "dir", Subtree: nil, diff --git a/src/restic/options/options_test.go b/src/restic/options/options_test.go index f5d43b9a7..ecdd918aa 100644 --- a/src/restic/options/options_test.go +++ b/src/restic/options/options_test.go @@ -233,7 +233,7 @@ func TestListOptions(t *testing.T) { Foo string `option:"foo" help:"bar text help"` }{}, []Help{ - Help{Name: "foo", Text: "bar text help"}, + {Name: "foo", Text: "bar text help"}, }, }, { @@ -242,8 +242,8 @@ func TestListOptions(t *testing.T) { Bar string `option:"bar" help:"bar text help"` }{}, []Help{ - Help{Name: "foo", Text: "bar text help"}, - Help{Name: "bar", Text: "bar text help"}, + {Name: "foo", Text: "bar text help"}, + {Name: "bar", Text: "bar text help"}, }, }, { @@ -252,14 +252,14 @@ func TestListOptions(t *testing.T) { Foo string `option:"foo" help:"bar text help"` }{}, []Help{ - Help{Name: "bar", Text: "bar text help"}, - Help{Name: "foo", Text: "bar text help"}, + {Name: "bar", Text: "bar text help"}, + {Name: "foo", Text: "bar text help"}, }, }, { &teststruct, []Help{ - Help{Name: "foo", Text: "bar text help"}, + {Name: "foo", Text: "bar text help"}, }, }, } @@ -290,9 +290,9 @@ func TestAppendAllOptions(t *testing.T) { }{}, }, []Help{ - Help{Namespace: "local", Name: "foo", Text: "bar text help"}, - Help{Namespace: "sftp", Name: "bar", Text: "bar text help"}, - Help{Namespace: "sftp", Name: "foo", Text: "bar text help2"}, + {Namespace: "local", Name: "foo", Text: "bar text help"}, + {Namespace: "sftp", Name: "bar", Text: "bar text help"}, + {Namespace: "sftp", Name: "foo", Text: "bar text help2"}, }, }, } diff --git a/src/restic/repository/repack.go b/src/restic/repository/repack.go index ef11034cb..b049a4255 100644 --- a/src/restic/repository/repack.go +++ b/src/restic/repository/repack.go @@ -71,7 +71,7 @@ func Repack(repo restic.Repository, packs restic.IDSet, keepBlobs restic.BlobSet debug.Log(" process blob %v", h) - buf = buf[:len(buf)] + buf = buf[:] if uint(len(buf)) < entry.Length { buf = make([]byte, entry.Length) } diff --git a/src/restic/tree_test.go b/src/restic/tree_test.go index 967ac18cc..0bf7cfddc 100644 --- a/src/restic/tree_test.go +++ b/src/restic/tree_test.go @@ -56,11 +56,11 @@ func TestTree(t *testing.T) { } var testNodes = []restic.Node{ - restic.Node{Name: "normal"}, - restic.Node{Name: "with backslashes \\zzz"}, - restic.Node{Name: "test utf-8 föbärß"}, - restic.Node{Name: "test invalid \x00\x01\x02\x03\x04"}, - restic.Node{Name: "test latin1 \x75\x6d\x6c\xe4\xfc\x74\xf6\x6e\xdf\x6e\x6c\x6c"}, + {Name: "normal"}, + {Name: "with backslashes \\zzz"}, + {Name: "test utf-8 föbärß"}, + {Name: "test invalid \x00\x01\x02\x03\x04"}, + {Name: "test latin1 \x75\x6d\x6c\xe4\xfc\x74\xf6\x6e\xdf\x6e\x6c\x6c"}, } func TestNodeMarshal(t *testing.T) { diff --git a/src/restic/walk/walk_test.go b/src/restic/walk/walk_test.go index 057e512ca..6a824827d 100644 --- a/src/restic/walk/walk_test.go +++ b/src/restic/walk/walk_test.go @@ -1389,7 +1389,7 @@ func BenchmarkDelayedWalkTree(t *testing.B) { treeJobs := make(chan walk.TreeJob) go walk.Tree(dr, root, nil, treeJobs) - for _ = range treeJobs { + for range treeJobs { } } } From f3d09ce7c889bf87d25af560fbcebe40fe863260 Mon Sep 17 00:00:00 2001 From: Pauline Middelink Date: Wed, 17 May 2017 01:34:33 +0200 Subject: [PATCH 3/4] Fix vet warnings --- src/restic/backend/test/tests.go | 10 +++++----- src/restic/filter/filter_test.go | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/restic/backend/test/tests.go b/src/restic/backend/test/tests.go index 594acd412..9919220b9 100644 --- a/src/restic/backend/test/tests.go +++ b/src/restic/backend/test/tests.go @@ -170,7 +170,7 @@ func (s *Suite) TestLoad(t *testing.T) { if err != nil { t.Errorf("Load(%d, %d) ReadAll() returned unexpected error: %+v", l, o, err) if err = rd.Close(); err != nil { - t.Errorf("Load(%d, %d) rd.Close() returned error: %+v", err) + t.Errorf("Load(%d, %d) rd.Close() returned error: %+v", l, o, err) } continue } @@ -178,7 +178,7 @@ func (s *Suite) TestLoad(t *testing.T) { if l == 0 && len(buf) != len(d) { t.Errorf("Load(%d, %d) wrong number of bytes read: want %d, got %d", l, o, len(d), len(buf)) if err = rd.Close(); err != nil { - t.Errorf("Load(%d, %d) rd.Close() returned error: %+v", err) + t.Errorf("Load(%d, %d) rd.Close() returned error: %+v", l, o, err) } continue } @@ -186,7 +186,7 @@ func (s *Suite) TestLoad(t *testing.T) { if l > 0 && l <= len(d) && len(buf) != l { t.Errorf("Load(%d, %d) wrong number of bytes read: want %d, got %d", l, o, l, len(buf)) if err = rd.Close(); err != nil { - t.Errorf("Load(%d, %d) rd.Close() returned error: %+v", err) + t.Errorf("Load(%d, %d) rd.Close() returned error: %+v", l, o, err) } continue } @@ -194,7 +194,7 @@ func (s *Suite) TestLoad(t *testing.T) { if l > len(d) && len(buf) != len(d) { t.Errorf("Load(%d, %d) wrong number of bytes read for overlong read: want %d, got %d", l, o, l, len(buf)) if err = rd.Close(); err != nil { - t.Errorf("Load(%d, %d) rd.Close() returned error: %+v", err) + t.Errorf("Load(%d, %d) rd.Close() returned error: %+v", l, o, err) } continue } @@ -202,7 +202,7 @@ func (s *Suite) TestLoad(t *testing.T) { if !bytes.Equal(buf, d) { t.Errorf("Load(%d, %d) returned wrong bytes", l, o) if err = rd.Close(); err != nil { - t.Errorf("Load(%d, %d) rd.Close() returned error: %+v", err) + t.Errorf("Load(%d, %d) rd.Close() returned error: %+v", l, o, err) } continue } diff --git a/src/restic/filter/filter_test.go b/src/restic/filter/filter_test.go index 232e2c4d3..78cb3ec5b 100644 --- a/src/restic/filter/filter_test.go +++ b/src/restic/filter/filter_test.go @@ -155,7 +155,7 @@ var filterListTests = []struct { {[]string{"", "*.c"}, "/foo/bar/test.go", false}, } -func TestMatchList(t *testing.T) { +func TestList(t *testing.T) { for i, test := range filterListTests { match, err := filter.List(test.patterns, test.path) if err != nil { @@ -171,7 +171,7 @@ func TestMatchList(t *testing.T) { } } -func ExampleMatchList() { +func ExampleList() { match, _ := filter.List([]string{"*.c", "*.go"}, "/home/user/file.go") fmt.Printf("match: %v\n", match) // Output: From 120af801cf8e7562ecbb1824c89ad2d394958e96 Mon Sep 17 00:00:00 2001 From: Pauline Middelink Date: Wed, 17 May 2017 01:39:39 +0200 Subject: [PATCH 4/4] Fix golint warnings (except the exported fields/functions without comments) --- src/restic/crypto/crypto.go | 10 +++++----- src/restic/crypto/crypto_int_test.go | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/restic/crypto/crypto.go b/src/restic/crypto/crypto.go index 03d3d10a2..fe7d3e5f2 100644 --- a/src/restic/crypto/crypto.go +++ b/src/restic/crypto/crypto.go @@ -173,10 +173,10 @@ func (m *MACKey) UnmarshalJSON(data []byte) error { } // Valid tests whether the key k is valid (i.e. not zero). -func (k *MACKey) Valid() bool { +func (m *MACKey) Valid() bool { nonzeroK := false - for i := 0; i < len(k.K); i++ { - if k.K[i] != 0 { + for i := 0; i < len(m.K); i++ { + if m.K[i] != 0 { nonzeroK = true } } @@ -185,8 +185,8 @@ func (k *MACKey) Valid() bool { return false } - for i := 0; i < len(k.R); i++ { - if k.R[i] != 0 { + for i := 0; i < len(m.R); i++ { + if m.R[i] != 0 { return true } } diff --git a/src/restic/crypto/crypto_int_test.go b/src/restic/crypto/crypto_int_test.go index 1dbc32623..88c7b1fa7 100644 --- a/src/restic/crypto/crypto_int_test.go +++ b/src/restic/crypto/crypto_int_test.go @@ -7,7 +7,7 @@ import ( ) // test vectors from http://cr.yp.to/mac/poly1305-20050329.pdf -var poly1305_tests = []struct { +var poly1305Tests = []struct { msg []byte r []byte k []byte @@ -44,7 +44,7 @@ var poly1305_tests = []struct { } func TestPoly1305(t *testing.T) { - for _, test := range poly1305_tests { + for _, test := range poly1305Tests { key := &MACKey{} copy(key.K[:], test.k) copy(key.R[:], test.r)