From 9e852af5be802c7a83dfbbd5b980b9aa71cac6d6 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 27 Feb 2021 15:56:40 +0100 Subject: [PATCH] check: Fix crash of --read-data-subset=x% on empty repository Rounding up to at least one pack file should only be done if at least a pack file exists. --- changelog/unreleased/issue-3296 | 7 +++++++ cmd/restic/cmd_check.go | 2 +- cmd/restic/cmd_check_test.go | 7 +++++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 changelog/unreleased/issue-3296 diff --git a/changelog/unreleased/issue-3296 b/changelog/unreleased/issue-3296 new file mode 100644 index 000000000..1e98c30e0 --- /dev/null +++ b/changelog/unreleased/issue-3296 @@ -0,0 +1,7 @@ +Bugfix: Fix crash of `check --read-data-subset=x%` run for an empty repository + +`check --read-data-subset=x%` crashed when run for an empty repository. This +has been fixed. + +https://github.com/restic/restic/issues/3296 +https://github.com/restic/restic/pull/3309 diff --git a/cmd/restic/cmd_check.go b/cmd/restic/cmd_check.go index 859e16580..2b5017ead 100644 --- a/cmd/restic/cmd_check.go +++ b/cmd/restic/cmd_check.go @@ -331,7 +331,7 @@ func selectPacksByBucket(allPacks map[restic.ID]int64, bucket, totalBuckets uint func selectRandomPacksByPercentage(allPacks map[restic.ID]int64, percentage float64) map[restic.ID]int64 { packCount := len(allPacks) packsToCheck := int(float64(packCount) * (percentage / 100.0)) - if packsToCheck < 1 { + if packCount > 0 && packsToCheck < 1 { packsToCheck = 1 } timeNs := time.Now().UnixNano() diff --git a/cmd/restic/cmd_check_test.go b/cmd/restic/cmd_check_test.go index 929771151..ef038c050 100644 --- a/cmd/restic/cmd_check_test.go +++ b/cmd/restic/cmd_check_test.go @@ -122,3 +122,10 @@ func TestSelectRandomPacksByPercentage(t *testing.T) { rtest.Assert(t, ok, "Expected input and output to be equal") } } + +func TestSelectNoRandomPacksByPercentage(t *testing.T) { + // that the a repository without pack files works + var testPacks = make(map[restic.ID]int64) + selectedPacks := selectRandomPacksByPercentage(testPacks, 10.0) + rtest.Assert(t, len(selectedPacks) == 0, "Expected 0 selected packs") +}