From 337725c35424b1b039448eec671677e11ee4b67e Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 6 Mar 2020 23:22:10 +0100 Subject: [PATCH 01/23] Use sort.Strings --- cmd/restic/cmd_diff.go | 2 +- internal/restic/snapshot_group.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/restic/cmd_diff.go b/cmd/restic/cmd_diff.go index b2ce7fca0..090568845 100644 --- a/cmd/restic/cmd_diff.go +++ b/cmd/restic/cmd_diff.go @@ -196,7 +196,7 @@ func uniqueNodeNames(tree1, tree2 *restic.Tree) (tree1Nodes, tree2Nodes map[stri uniqueNames = append(uniqueNames, name) } - sort.Sort(sort.StringSlice(uniqueNames)) + sort.Strings(uniqueNames) return tree1Nodes, tree2Nodes, uniqueNames } diff --git a/internal/restic/snapshot_group.go b/internal/restic/snapshot_group.go index 1e5ac18ee..bc3ef8ae3 100644 --- a/internal/restic/snapshot_group.go +++ b/internal/restic/snapshot_group.go @@ -51,7 +51,7 @@ func GroupSnapshots(snapshots Snapshots, options string) (map[string]Snapshots, if GroupByTag { tags = sn.Tags - sort.StringSlice(tags).Sort() + sort.Strings(tags) } if GroupByHost { hostname = sn.Hostname @@ -60,7 +60,7 @@ func GroupSnapshots(snapshots Snapshots, options string) (map[string]Snapshots, paths = sn.Paths } - sort.StringSlice(sn.Paths).Sort() + sort.Strings(sn.Paths) var k []byte var err error From 37113282ca22fa9ab9d781c8c9ae377e73ba6cb0 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 6 Mar 2020 23:27:37 +0100 Subject: [PATCH 02/23] Merge a few variable declaration and initializations --- cmd/restic/cmd_snapshots.go | 3 +-- internal/fuse/dir.go | 3 +-- internal/restic/hardlinks_index_test.go | 6 ++---- internal/restic/snapshot_group.go | 4 +--- 4 files changed, 5 insertions(+), 11 deletions(-) diff --git a/cmd/restic/cmd_snapshots.go b/cmd/restic/cmd_snapshots.go index d92499e30..380b9b75c 100644 --- a/cmd/restic/cmd_snapshots.go +++ b/cmd/restic/cmd_snapshots.go @@ -251,9 +251,8 @@ func PrintSnapshots(stdout io.Writer, list restic.Snapshots, reasons []restic.Ke // Prints nothing, if we did not group at all. func PrintSnapshotGroupHeader(stdout io.Writer, groupKeyJSON string) error { var key restic.SnapshotGroupKey - var err error - err = json.Unmarshal([]byte(groupKeyJSON), &key) + err := json.Unmarshal([]byte(groupKeyJSON), &key) if err != nil { return err } diff --git a/internal/fuse/dir.go b/internal/fuse/dir.go index 22530ea49..8ebb63d60 100644 --- a/internal/fuse/dir.go +++ b/internal/fuse/dir.go @@ -125,8 +125,7 @@ func (d *dir) Attr(ctx context.Context, a *fuse.Attr) error { func (d *dir) calcNumberOfLinks() uint32 { // a directory d has 2 hardlinks + the number // of directories contained by d - var count uint32 - count = 2 + count := uint32(2) for _, node := range d.items { if node.Type == "dir" { count++ diff --git a/internal/restic/hardlinks_index_test.go b/internal/restic/hardlinks_index_test.go index 7e4bcd51a..8040d657f 100644 --- a/internal/restic/hardlinks_index_test.go +++ b/internal/restic/hardlinks_index_test.go @@ -15,15 +15,13 @@ func TestHardLinks(t *testing.T) { idx.Add(1, 2, "inode1-file1-on-device2") idx.Add(2, 3, "inode2-file2-on-device3") - var sresult string - sresult = idx.GetFilename(1, 2) + sresult := idx.GetFilename(1, 2) rtest.Equals(t, sresult, "inode1-file1-on-device2") sresult = idx.GetFilename(2, 3) rtest.Equals(t, sresult, "inode2-file2-on-device3") - var bresult bool - bresult = idx.Has(1, 2) + bresult := idx.Has(1, 2) rtest.Equals(t, bresult, true) bresult = idx.Has(1, 3) diff --git a/internal/restic/snapshot_group.go b/internal/restic/snapshot_group.go index bc3ef8ae3..c8b1a5faa 100644 --- a/internal/restic/snapshot_group.go +++ b/internal/restic/snapshot_group.go @@ -25,9 +25,7 @@ func GroupSnapshots(snapshots Snapshots, options string) (map[string]Snapshots, var GroupByTag bool var GroupByHost bool var GroupByPath bool - var GroupOptionList []string - - GroupOptionList = strings.Split(options, ",") + GroupOptionList := strings.Split(options, ",") for _, option := range GroupOptionList { switch option { From 2f8335554c8e84c5b6eb19f8b2c62b927f4b17ef Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 6 Mar 2020 23:32:12 +0100 Subject: [PATCH 03/23] Remove a few unused variables --- cmd/restic/integration_helpers_test.go | 2 +- cmd/restic/integration_test.go | 4 ++-- internal/repository/master_index_test.go | 6 +++--- internal/repository/worker_group.go | 2 +- internal/ui/termstatus/status.go | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cmd/restic/integration_helpers_test.go b/cmd/restic/integration_helpers_test.go index 0069e27a9..1bbd41cb8 100644 --- a/cmd/restic/integration_helpers_test.go +++ b/cmd/restic/integration_helpers_test.go @@ -55,7 +55,7 @@ func walkDir(dir string) <-chan *dirEntry { }() // first element is root - _ = <-ch + <-ch return ch } diff --git a/cmd/restic/integration_test.go b/cmd/restic/integration_test.go index 74661ca1c..20e5e6402 100644 --- a/cmd/restic/integration_test.go +++ b/cmd/restic/integration_test.go @@ -1105,14 +1105,14 @@ func TestRestoreNoMetadataOnIgnoredIntermediateDirs(t *testing.T) { testRunRestoreIncludes(t, env.gopts, filepath.Join(env.base, "restore0"), snapshotID, []string{"*.ext"}) f1 := filepath.Join(env.base, "restore0", "testdata", "subdir1", "subdir2") - fi, err := os.Stat(f1) + _, err := os.Stat(f1) rtest.OK(t, err) // restore with filter "*", this should restore meta data on everything. testRunRestoreIncludes(t, env.gopts, filepath.Join(env.base, "restore1"), snapshotID, []string{"*"}) f2 := filepath.Join(env.base, "restore1", "testdata", "subdir1", "subdir2") - fi, err = os.Stat(f2) + fi, err := os.Stat(f2) rtest.OK(t, err) rtest.Assert(t, fi.ModTime() == time.Unix(0, 0), diff --git a/internal/repository/master_index_test.go b/internal/repository/master_index_test.go index 5831fba63..28eba03a8 100644 --- a/internal/repository/master_index_test.go +++ b/internal/repository/master_index_test.go @@ -120,7 +120,7 @@ func TestMasterIndex(t *testing.T) { rtest.Assert(t, !found, "Expected no blobs when fetching with a random id") blobs = mIdx.Lookup(restic.NewRandomID(), restic.DataBlob) rtest.Assert(t, blobs == nil, "Expected no blobs when fetching with a random id") - size, found = mIdx.LookupSize(restic.NewRandomID(), restic.DataBlob) + _, found = mIdx.LookupSize(restic.NewRandomID(), restic.DataBlob) rtest.Assert(t, !found, "Expected no blobs when fetching with a random id") // Test Count @@ -172,7 +172,7 @@ func TestMasterMergeFinalIndexes(t *testing.T) { rtest.Equals(t, 1, len(allIndexes)) blobCount := 0 - for _ = range mIdx.Each(context.TODO()) { + for range mIdx.Each(context.TODO()) { blobCount++ } rtest.Equals(t, 2, blobCount) @@ -207,7 +207,7 @@ func TestMasterMergeFinalIndexes(t *testing.T) { rtest.Equals(t, []restic.PackedBlob{blob2}, blobs) blobCount = 0 - for _ = range mIdx.Each(context.TODO()) { + for range mIdx.Each(context.TODO()) { blobCount++ } rtest.Equals(t, 2, blobCount) diff --git a/internal/repository/worker_group.go b/internal/repository/worker_group.go index 20783b188..52988f0bd 100644 --- a/internal/repository/worker_group.go +++ b/internal/repository/worker_group.go @@ -11,7 +11,7 @@ import ( // one of the workers, it is returned. FinalFunc is always run, regardless of // any other previous errors. func RunWorkers(ctx context.Context, count int, workerFunc func() error, finalFunc func()) error { - wg, ctx := errgroup.WithContext(ctx) + wg, _ := errgroup.WithContext(ctx) // run workers for i := 0; i < count; i++ { diff --git a/internal/ui/termstatus/status.go b/internal/ui/termstatus/status.go index e8bfa95cd..c1648206b 100644 --- a/internal/ui/termstatus/status.go +++ b/internal/ui/termstatus/status.go @@ -204,7 +204,7 @@ func (t *Terminal) runWithoutStatus(ctx context.Context) { fmt.Fprintf(os.Stderr, "flush failed: %v\n", err) } - case _ = <-t.status: + case <-t.status: // discard status lines } } From 49b6aac3fa9e1c4de24095c05a8fbb9234cf9f82 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 6 Mar 2020 23:33:26 +0100 Subject: [PATCH 04/23] Use !bytes.Equal instead of bytes.Compare !=0 to check for inequality --- cmd/restic/exclude.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/restic/exclude.go b/cmd/restic/exclude.go index e2c386f24..2e5349611 100644 --- a/cmd/restic/exclude.go +++ b/cmd/restic/exclude.go @@ -190,7 +190,7 @@ func isDirExcludedByFile(dir, tagFilename, header string) bool { Warnf("could not read signature from exclusion tagfile %q: %v\n", tf, err) return false } - if bytes.Compare(buf, []byte(header)) != 0 { + if !bytes.Equal(buf, []byte(header)) { Warnf("invalid signature in exclusion tagfile %q\n", tf) return false } From 460e2ffbf69f240c63c54ad247ff945607230cac Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 6 Mar 2020 23:35:09 +0100 Subject: [PATCH 05/23] Collapse a few boolean operations --- cmd/restic/integration_test.go | 6 +----- internal/archiver/archiver_test.go | 10 ++-------- internal/walker/walker.go | 2 +- 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/cmd/restic/integration_test.go b/cmd/restic/integration_test.go index 20e5e6402..f1b1ae7c6 100644 --- a/cmd/restic/integration_test.go +++ b/cmd/restic/integration_test.go @@ -1417,11 +1417,7 @@ func linksEqual(source, dest map[uint64][]string) bool { } } - if len(dest) != 0 { - return false - } - - return true + return len(dest) == 0 } func linkEqual(source, dest []string) bool { diff --git a/internal/archiver/archiver_test.go b/internal/archiver/archiver_test.go index d2e9c175b..3c4e3fdf6 100644 --- a/internal/archiver/archiver_test.go +++ b/internal/archiver/archiver_test.go @@ -1458,10 +1458,7 @@ func TestArchiverSnapshotSelect(t *testing.T) { "other": TestFile{Content: "another file"}, }, selFn: func(item string, fi os.FileInfo) bool { - if filepath.Ext(item) == ".txt" { - return false - } - return true + return filepath.Ext(item) != ".txt" }, }, { @@ -1485,10 +1482,7 @@ func TestArchiverSnapshotSelect(t *testing.T) { "other": TestFile{Content: "another file"}, }, selFn: func(item string, fi os.FileInfo) bool { - if filepath.Base(item) == "subdir" { - return false - } - return true + return filepath.Base(item) != "subdir" }, }, { diff --git a/internal/walker/walker.go b/internal/walker/walker.go index 1794e8a71..0146b0668 100644 --- a/internal/walker/walker.go +++ b/internal/walker/walker.go @@ -84,7 +84,7 @@ func walk(ctx context.Context, repo restic.TreeLoader, prefix string, parentTree return false, err } - if ignore == false { + if !ignore { allNodesIgnored = false } From d19f05c960b09d0c2b65e500a07e56a76b00c756 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 6 Mar 2020 23:36:27 +0100 Subject: [PATCH 06/23] Use buf.String() instead of string(buf.Bytes()) --- cmd/restic/integration_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/restic/integration_test.go b/cmd/restic/integration_test.go index f1b1ae7c6..f066e907d 100644 --- a/cmd/restic/integration_test.go +++ b/cmd/restic/integration_test.go @@ -151,7 +151,7 @@ func testRunCheckOutput(gopts GlobalOptions) (string, error) { } err := runCheck(opts, gopts, nil) - return string(buf.Bytes()), err + return buf.String(), err } func testRunRebuildIndex(t testing.TB, gopts GlobalOptions) { @@ -177,7 +177,7 @@ func testRunLs(t testing.TB, gopts GlobalOptions, snapshotID string) []string { rtest.OK(t, runLs(opts, gopts, []string{snapshotID})) - return strings.Split(string(buf.Bytes()), "\n") + return strings.Split(buf.String(), "\n") } func testRunFind(t testing.TB, wantJSON bool, gopts GlobalOptions, pattern string) []byte { From d9a80e07b993c487ef6aa868ecbfc21a67cae243 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 6 Mar 2020 23:37:51 +0100 Subject: [PATCH 07/23] repository: Simplify index age calculation --- internal/repository/index.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/repository/index.go b/internal/repository/index.go index 216d1afc1..ce0d1da82 100644 --- a/internal/repository/index.go +++ b/internal/repository/index.go @@ -107,7 +107,7 @@ var IndexFull = func(idx *Index) bool { for typ := range idx.byType { blobs += idx.byType[typ].len() } - age := time.Now().Sub(idx.created) + age := time.Since(idx.created) switch { case age >= indexMaxAge: From 014600bee6eb6d8861ee9b8c24e6c938f60e8e89 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 6 Mar 2020 23:38:30 +0100 Subject: [PATCH 08/23] archiver: Remove unused variable from test --- internal/archiver/blob_saver_test.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/internal/archiver/blob_saver_test.go b/internal/archiver/blob_saver_test.go index bd70e85f5..df63d3209 100644 --- a/internal/archiver/blob_saver_test.go +++ b/internal/archiver/blob_saver_test.go @@ -92,12 +92,9 @@ func TestBlobSaverError(t *testing.T) { b := NewBlobSaver(ctx, tmb, saver, uint(runtime.NumCPU())) - var results []FutureBlob - for i := 0; i < test.blobs; i++ { buf := &Buffer{Data: []byte(fmt.Sprintf("foo%d", i))} - fb := b.Save(ctx, restic.DataBlob, buf) - results = append(results, fb) + b.Save(ctx, restic.DataBlob, buf) } tmb.Kill(nil) From ee05501ce76433179882b0f2f91f61efc215b1f6 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 6 Mar 2020 23:39:02 +0100 Subject: [PATCH 09/23] archiver: Remove unused chmod function --- internal/archiver/archiver_test.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/internal/archiver/archiver_test.go b/internal/archiver/archiver_test.go index 3c4e3fdf6..e84d4c0b9 100644 --- a/internal/archiver/archiver_test.go +++ b/internal/archiver/archiver_test.go @@ -1984,13 +1984,6 @@ func snapshot(t testing.TB, repo restic.Repository, fs fs.FS, parent restic.ID, return snapshotID, node } -func chmod(t testing.TB, filename string, mode os.FileMode) { - err := os.Chmod(filename, mode) - if err != nil { - t.Fatal(err) - } -} - // StatFS allows overwriting what is returned by the Lstat function. type StatFS struct { fs.FS From 068a3ce23f7a30e917e1ea7e82fdf9b4f86e0d43 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 6 Mar 2020 23:39:35 +0100 Subject: [PATCH 10/23] Remove redundant return --- cmd/restic/integration_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/restic/integration_test.go b/cmd/restic/integration_test.go index f066e907d..582f7efdc 100644 --- a/cmd/restic/integration_test.go +++ b/cmd/restic/integration_test.go @@ -253,7 +253,6 @@ func testRunForgetJSON(t testing.TB, gopts GlobalOptions, args ...string) { "Expected 1 snapshot to be kept, got %v", len(forgets[0].Keep)) rtest.Assert(t, len(forgets[0].Remove) == 2, "Expected 2 snapshots to be removed, got %v", len(forgets[0].Remove)) - return } func testRunPrune(t testing.TB, gopts GlobalOptions) { From b22655367c293e37a46b8e034351fdb197134d34 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 6 Mar 2020 23:42:34 +0100 Subject: [PATCH 11/23] integration_test: Replace fprintf without format string --- cmd/restic/integration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/restic/integration_test.go b/cmd/restic/integration_test.go index 582f7efdc..439ea8510 100644 --- a/cmd/restic/integration_test.go +++ b/cmd/restic/integration_test.go @@ -449,7 +449,7 @@ func TestBackupExclude(t *testing.T) { f, err := os.Create(fp) rtest.OK(t, err) - fmt.Fprintf(f, filename) + fmt.Fprint(f, filename) rtest.OK(t, f.Close()) } From 9a1b3cb5d962f767765f47f273c3315695c602ae Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 6 Mar 2020 23:48:22 +0100 Subject: [PATCH 12/23] restorer: Remove unused treeID struct member from test --- internal/restorer/restorer_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/restorer/restorer_test.go b/internal/restorer/restorer_test.go index 0c86135a7..333d4598b 100644 --- a/internal/restorer/restorer_test.go +++ b/internal/restorer/restorer_test.go @@ -19,8 +19,7 @@ import ( type Node interface{} type Snapshot struct { - Nodes map[string]Node - treeID restic.ID + Nodes map[string]Node } type File struct { From e2dc5034d3a79415d4f624c662f220d9d0021f21 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 6 Mar 2020 23:51:07 +0100 Subject: [PATCH 13/23] restic/file: Use FileType for all File constants The data type of a variable is only carried on when using iota. --- internal/restic/file.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/restic/file.go b/internal/restic/file.go index 86b375192..7e32f09aa 100644 --- a/internal/restic/file.go +++ b/internal/restic/file.go @@ -12,11 +12,11 @@ type FileType string // These are the different data types a backend can store. const ( PackFile FileType = "data" // use data, as packs are stored under /data in repo - KeyFile = "key" - LockFile = "lock" - SnapshotFile = "snapshot" - IndexFile = "index" - ConfigFile = "config" + KeyFile FileType = "key" + LockFile FileType = "lock" + SnapshotFile FileType = "snapshot" + IndexFile FileType = "index" + ConfigFile FileType = "config" ) // Handle is used to store and access data in a backend. From f6f11400c22d629b9935ec6b04a0ed4d3d8f0193 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 7 Mar 2020 00:04:47 +0100 Subject: [PATCH 14/23] restic: Remove unused variable value --- internal/restic/backend_find.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/restic/backend_find.go b/internal/restic/backend_find.go index 096ac214c..36b2ba4ca 100644 --- a/internal/restic/backend_find.go +++ b/internal/restic/backend_find.go @@ -67,7 +67,7 @@ func PrefixLength(be Lister, t FileType) (int, error) { } // select prefixes of length l, test if the last one is the same as the current one - id := ID{} + var id ID outer: for l := minPrefixLength; l < len(id); l++ { var last string From 7ee096488075f6550d22bf4ae95626ca625317a3 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 7 Mar 2020 00:33:10 +0100 Subject: [PATCH 15/23] crypto: Remove unused error --- internal/crypto/crypto.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/internal/crypto/crypto.go b/internal/crypto/crypto.go index b56a9da2e..56ee61dba 100644 --- a/internal/crypto/crypto.go +++ b/internal/crypto/crypto.go @@ -231,10 +231,6 @@ func (k *EncryptionKey) Valid() bool { return false } -// ErrInvalidCiphertext is returned when trying to encrypt into the slice that -// holds the plaintext. -var ErrInvalidCiphertext = errors.New("invalid ciphertext, same slice used for plaintext") - // validNonce checks that nonce is not all zero. func validNonce(nonce []byte) bool { var sum byte From b4a7ce86cf941038710e7767e0d200c0521c3d25 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 7 Mar 2020 21:48:59 +0100 Subject: [PATCH 16/23] uint cannot be less than zero --- internal/archiver/archiver_test.go | 4 ++-- internal/backend/semaphore.go | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/internal/archiver/archiver_test.go b/internal/archiver/archiver_test.go index e84d4c0b9..6fc5ab648 100644 --- a/internal/archiver/archiver_test.go +++ b/internal/archiver/archiver_test.go @@ -832,7 +832,7 @@ func TestArchiverSaveDir(t *testing.T) { if stats.DataBlobs != 0 { t.Errorf("wrong stats returned in DataBlobs, want 0, got %d", stats.DataBlobs) } - if stats.TreeSize <= 0 { + if stats.TreeSize == 0 { t.Errorf("wrong stats returned in TreeSize, want > 0, got %d", stats.TreeSize) } if stats.TreeBlobs <= 0 { @@ -910,7 +910,7 @@ func TestArchiverSaveDirIncremental(t *testing.T) { if stats.DataBlobs != 0 { t.Errorf("wrong stats returned in DataBlobs, want 0, got %d", stats.DataBlobs) } - if stats.TreeSize <= 0 { + if stats.TreeSize == 0 { t.Errorf("wrong stats returned in TreeSize, want > 0, got %d", stats.TreeSize) } if stats.TreeBlobs <= 0 { diff --git a/internal/backend/semaphore.go b/internal/backend/semaphore.go index 2146db2f3..28b97472b 100644 --- a/internal/backend/semaphore.go +++ b/internal/backend/semaphore.go @@ -2,8 +2,9 @@ package backend import ( "context" - "github.com/restic/restic/internal/errors" "io" + + "github.com/restic/restic/internal/errors" ) // Semaphore limits access to a restricted resource. @@ -13,7 +14,7 @@ type Semaphore struct { // NewSemaphore returns a new semaphore with capacity n. func NewSemaphore(n uint) (*Semaphore, error) { - if n <= 0 { + if n == 0 { return nil, errors.New("must be a positive number") } return &Semaphore{ From 84ea2389ae15b788079a69ad0f2c3c0b6bcea033 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 7 Mar 2020 21:43:29 +0100 Subject: [PATCH 17/23] archiver_test: Only skip symlinks on windows --- internal/archiver/testing_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/archiver/testing_test.go b/internal/archiver/testing_test.go index 68563948b..5d9c43363 100644 --- a/internal/archiver/testing_test.go +++ b/internal/archiver/testing_test.go @@ -125,7 +125,6 @@ func TestTestCreateFiles(t *testing.T) { if _, ok := item.(TestSymlink); ok { continue } - continue } targetPath := filepath.Join(tempdir, filepath.FromSlash(name)) From 4784540f049060cb992cbbf2b36819a3d6438703 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sun, 8 Mar 2020 20:48:51 +0100 Subject: [PATCH 18/23] repository: Simplify worker group code --- internal/checker/checker.go | 2 +- internal/repository/repository.go | 2 +- internal/repository/worker_group.go | 6 ++---- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/internal/checker/checker.go b/internal/checker/checker.go index 8b3ca9695..1f3233a01 100644 --- a/internal/checker/checker.go +++ b/internal/checker/checker.go @@ -142,7 +142,7 @@ func (c *Checker) LoadIndex(ctx context.Context) (hints []error, errs []error) { // run workers on ch wg.Go(func() error { - return repository.RunWorkers(ctx, defaultParallelism, worker, final) + return repository.RunWorkers(defaultParallelism, worker, final) }) // receive decoded indexes diff --git a/internal/repository/repository.go b/internal/repository/repository.go index e25bf369e..72fc423c3 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -479,7 +479,7 @@ func (r *Repository) LoadIndex(ctx context.Context) error { // run workers on ch wg.Go(func() error { - return RunWorkers(ctx, loadIndexParallelism, worker, final) + return RunWorkers(loadIndexParallelism, worker, final) }) // receive decoded indexes diff --git a/internal/repository/worker_group.go b/internal/repository/worker_group.go index 52988f0bd..a87ef4f49 100644 --- a/internal/repository/worker_group.go +++ b/internal/repository/worker_group.go @@ -1,8 +1,6 @@ package repository import ( - "context" - "golang.org/x/sync/errgroup" ) @@ -10,8 +8,8 @@ import ( // After all workers have terminated, finalFunc is run. If an error occurs in // one of the workers, it is returned. FinalFunc is always run, regardless of // any other previous errors. -func RunWorkers(ctx context.Context, count int, workerFunc func() error, finalFunc func()) error { - wg, _ := errgroup.WithContext(ctx) +func RunWorkers(count int, workerFunc func() error, finalFunc func()) error { + var wg errgroup.Group // run workers for i := 0; i < count; i++ { From dc31529fc3acd0891d5d814e4d28732645c789c5 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Tue, 28 Jul 2020 22:19:21 +0200 Subject: [PATCH 19/23] Unindent else block after if block ending with a return statement --- internal/archiver/archiver.go | 10 +++++----- internal/ui/json/backup.go | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/internal/archiver/archiver.go b/internal/archiver/archiver.go index f334a1af2..f4fe5f7db 100644 --- a/internal/archiver/archiver.go +++ b/internal/archiver/archiver.go @@ -414,12 +414,12 @@ func (arch *Archiver) Save(ctx context.Context, snPath, target string, previous _ = file.Close() return fn, false, nil - } else { - debug.Log("%v hasn't changed, but contents are missing!", target) - // There are contents missing - inform user! - err := errors.Errorf("parts of %v not found in the repository index; storing the file again", target) - arch.error(abstarget, fi, err) } + + debug.Log("%v hasn't changed, but contents are missing!", target) + // There are contents missing - inform user! + err := errors.Errorf("parts of %v not found in the repository index; storing the file again", target) + arch.error(abstarget, fi, err) } fn.isFile = true diff --git a/internal/ui/json/backup.go b/internal/ui/json/backup.go index ff7af18c4..9380422b9 100644 --- a/internal/ui/json/backup.go +++ b/internal/ui/json/backup.go @@ -230,10 +230,10 @@ func (b *Backup) CompleteItem(item string, previous, current *restic.Node, s arc done: true, } return - } else { - b.summary.ProcessedBytes += current.Size } + b.summary.ProcessedBytes += current.Size + switch current.Type { case "file": b.processedCh <- counter{Files: 1} From d0329cf3eb0275d57edd4c4cb5f28796d2af6ef2 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Tue, 28 Jul 2020 22:24:43 +0200 Subject: [PATCH 20/23] Adjust comments to match name of exported methods --- cmd/restic/delete.go | 2 +- internal/repository/index.go | 2 +- internal/repository/master_index.go | 2 +- internal/repository/repository.go | 2 ++ 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/cmd/restic/delete.go b/cmd/restic/delete.go index 0d92f3375..6d7657e80 100644 --- a/cmd/restic/delete.go +++ b/cmd/restic/delete.go @@ -12,7 +12,7 @@ func DeleteFiles(gopts GlobalOptions, repo restic.Repository, fileList restic.ID deleteFiles(gopts, true, repo, fileList, fileType) } -// DeleteFiles deletes the given fileList of fileType in parallel +// DeleteFilesChecked deletes the given fileList of fileType in parallel // if an error occurs, it will cancel and return this error func DeleteFilesChecked(gopts GlobalOptions, repo restic.Repository, fileList restic.IDSet, fileType restic.FileType) error { return deleteFiles(gopts, false, repo, fileList, fileType) diff --git a/internal/repository/index.go b/internal/repository/index.go index ce0d1da82..4beecc008 100644 --- a/internal/repository/index.go +++ b/internal/repository/index.go @@ -394,7 +394,7 @@ func (idx *Index) Finalize() { idx.packIDToIndex = nil } -// ID returns the IDs of the index, if available. If the index is not yet +// IDs returns the IDs of the index, if available. If the index is not yet // finalized, an error is returned. func (idx *Index) IDs() (restic.IDs, error) { idx.m.Lock() diff --git a/internal/repository/master_index.go b/internal/repository/master_index.go index 318bf7c31..57b2985a6 100644 --- a/internal/repository/master_index.go +++ b/internal/repository/master_index.go @@ -134,7 +134,7 @@ func (mi *MasterIndex) Insert(idx *Index) { mi.idx = append(mi.idx, idx) } -// Store remembers the id and pack in the index. +// StorePack remembers the id and pack in the index. func (mi *MasterIndex) StorePack(id restic.ID, blobs []restic.Blob) { mi.idxMutex.Lock() defer mi.idxMutex.Unlock() diff --git a/internal/repository/repository.go b/internal/repository/repository.go index 72fc423c3..905982cba 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -47,6 +47,8 @@ func New(be restic.Backend) *Repository { return repo } +// DisableAutoIndexUpdate deactives the automatic finalization and upload of new +// indexes once these are full func (r *Repository) DisableAutoIndexUpdate() { r.noAutoIndexUpdate = true } From b77e933d80cf26fa975b92c190fe433a99329389 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Tue, 28 Jul 2020 22:28:19 +0200 Subject: [PATCH 21/23] Convert underscore variable names to camelCase --- internal/restic/backend_find_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/restic/backend_find_test.go b/internal/restic/backend_find_test.go index 4b1828a92..d122b4f94 100644 --- a/internal/restic/backend_find_test.go +++ b/internal/restic/backend_find_test.go @@ -42,9 +42,9 @@ func TestFind(t *testing.T) { if err != nil { t.Error(err) } - expected_match := "20bdc1402a6fc9b633aaffffffffffffffffffffffffffffffffffffffffffff" - if f != expected_match { - t.Errorf("Wrong match returned want %s, got %s", expected_match, f) + expectedMatch := "20bdc1402a6fc9b633aaffffffffffffffffffffffffffffffffffffffffffff" + if f != expectedMatch { + t.Errorf("Wrong match returned want %s, got %s", expectedMatch, f) } f, err = Find(m, SnapshotFile, "NotAPrefix") @@ -56,8 +56,8 @@ func TestFind(t *testing.T) { } // Try to match with a prefix longer than any ID. - extra_length_id := samples[0].String() + "f" - f, err = Find(m, SnapshotFile, extra_length_id) + extraLengthID := samples[0].String() + "f" + f, err = Find(m, SnapshotFile, extraLengthID) if err != ErrNoIDPrefixFound { t.Error("Expected no snapshots to be matched.") } From 1ede018ea67df6be89c68078f141227e0e5fb756 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Tue, 28 Jul 2020 22:32:57 +0200 Subject: [PATCH 22/23] error variable names should start with 'Err' --- cmd/restic/cmd_find.go | 6 +++--- cmd/restic/cmd_ls.go | 2 +- internal/walker/walker.go | 14 +++++++------- internal/walker/walker_test.go | 8 ++++---- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/cmd/restic/cmd_find.go b/cmd/restic/cmd_find.go index 68b5947ee..84a709b15 100644 --- a/cmd/restic/cmd_find.go +++ b/cmd/restic/cmd_find.go @@ -270,7 +270,7 @@ func (f *Finder) findInSnapshot(ctx context.Context, sn *restic.Snapshot) error Printf("Unable to load tree %s\n ... which belongs to snapshot %s.\n", parentTreeID, sn.ID()) - return false, walker.SkipNode + return false, walker.ErrSkipNode } if node == nil { @@ -314,7 +314,7 @@ func (f *Finder) findInSnapshot(ctx context.Context, sn *restic.Snapshot) error if !childMayMatch { ignoreIfNoMatch = true - errIfNoMatch = walker.SkipNode + errIfNoMatch = walker.ErrSkipNode } else { ignoreIfNoMatch = false } @@ -354,7 +354,7 @@ func (f *Finder) findIDs(ctx context.Context, sn *restic.Snapshot) error { Printf("Unable to load tree %s\n ... which belongs to snapshot %s.\n", parentTreeID, sn.ID()) - return false, walker.SkipNode + return false, walker.ErrSkipNode } if node == nil { diff --git a/cmd/restic/cmd_ls.go b/cmd/restic/cmd_ls.go index dec46a375..0fa21b145 100644 --- a/cmd/restic/cmd_ls.go +++ b/cmd/restic/cmd_ls.go @@ -222,7 +222,7 @@ func runLs(opts LsOptions, gopts GlobalOptions, args []string) error { // otherwise, signal the walker to not walk recursively into any // subdirs if node.Type == "dir" { - return false, walker.SkipNode + return false, walker.ErrSkipNode } return false, nil }) diff --git a/internal/walker/walker.go b/internal/walker/walker.go index 0146b0668..e06343ac3 100644 --- a/internal/walker/walker.go +++ b/internal/walker/walker.go @@ -10,15 +10,15 @@ import ( "github.com/restic/restic/internal/restic" ) -// SkipNode is returned by WalkFunc when a dir node should not be walked. -var SkipNode = errors.New("skip this node") +// ErrSkipNode is returned by WalkFunc when a dir node should not be walked. +var ErrSkipNode = errors.New("skip this node") // WalkFunc is the type of the function called for each node visited by Walk. // Path is the slash-separated path from the root node. If there was a problem // loading a node, err is set to a non-nil error. WalkFunc can chose to ignore // it by returning nil. // -// When the special value SkipNode is returned and node is a dir node, it is +// When the special value ErrSkipNode is returned and node is a dir node, it is // not walked. When the node is not a dir node, the remaining items in this // tree are skipped. // @@ -26,7 +26,7 @@ var SkipNode = errors.New("skip this node") // For tree nodes, this means that the function is not called for the // referenced tree. If the node is not a tree, and all nodes in the current // tree have ignore set to true, the current tree will not be visited again. -// When err is not nil and different from SkipNode, the value returned for +// When err is not nil and different from ErrSkipNode, the value returned for // ignore is ignored. type WalkFunc func(parentTreeID restic.ID, path string, node *restic.Node, nodeErr error) (ignore bool, err error) @@ -38,7 +38,7 @@ func Walk(ctx context.Context, repo restic.TreeLoader, root restic.ID, ignoreTre _, err = walkFn(root, "/", nil, err) if err != nil { - if err == SkipNode { + if err == ErrSkipNode { err = nil } return err @@ -76,7 +76,7 @@ func walk(ctx context.Context, repo restic.TreeLoader, prefix string, parentTree if node.Type != "dir" { ignore, err := walkFn(parentTreeID, p, node, nil) if err != nil { - if err == SkipNode { + if err == ErrSkipNode { // skip the remaining entries in this tree return allNodesIgnored, nil } @@ -102,7 +102,7 @@ func walk(ctx context.Context, repo restic.TreeLoader, prefix string, parentTree subtree, err := repo.LoadTree(ctx, *node.Subtree) ignore, err := walkFn(parentTreeID, p, node, err) if err != nil { - if err == SkipNode { + if err == ErrSkipNode { if ignore { ignoreTrees.Insert(*node.Subtree) } diff --git a/internal/walker/walker_test.go b/internal/walker/walker_test.go index af5c25f42..930c40716 100644 --- a/internal/walker/walker_test.go +++ b/internal/walker/walker_test.go @@ -138,7 +138,7 @@ func checkParentTreeOrder(want []string) checkFunc { } } -// checkSkipFor returns SkipNode if path is in skipFor, it checks that the +// checkSkipFor returns ErrSkipNode if path is in skipFor, it checks that the // paths the walk func is called for are exactly the ones in wantPaths. func checkSkipFor(skipFor map[string]struct{}, wantPaths []string) checkFunc { var pos int @@ -161,7 +161,7 @@ func checkSkipFor(skipFor map[string]struct{}, wantPaths []string) checkFunc { pos++ if _, ok := skipFor[path]; ok { - return false, SkipNode + return false, ErrSkipNode } return false, nil @@ -177,7 +177,7 @@ func checkSkipFor(skipFor map[string]struct{}, wantPaths []string) checkFunc { } } -// checkIgnore returns SkipNode if path is in skipFor and sets ignore according +// checkIgnore returns ErrSkipNode if path is in skipFor and sets ignore according // to ignoreFor. It checks that the paths the walk func is called for are exactly // the ones in wantPaths. func checkIgnore(skipFor map[string]struct{}, ignoreFor map[string]bool, wantPaths []string) checkFunc { @@ -201,7 +201,7 @@ func checkIgnore(skipFor map[string]struct{}, ignoreFor map[string]bool, wantPat pos++ if _, ok := skipFor[path]; ok { - return ignoreFor[path], SkipNode + return ignoreFor[path], ErrSkipNode } return ignoreFor[path], nil From c46edcd9d6af48863c4b7d46c9ee4b90409ea638 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Tue, 28 Jul 2020 22:38:03 +0200 Subject: [PATCH 23/23] error strings should not end with punctuation --- internal/repository/index.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/repository/index.go b/internal/repository/index.go index 4beecc008..0a3aaead8 100644 --- a/internal/repository/index.go +++ b/internal/repository/index.go @@ -472,7 +472,7 @@ func (idx *Index) merge(idx2 *Index) error { defer idx2.m.Unlock() if !idx2.final { - return errors.New("index to merge is not final!") + return errors.New("index to merge is not final") } packlen := len(idx.packs)