diff --git a/changelog/unreleased/pull-22222 b/changelog/unreleased/pull-22222 new file mode 100644 index 000000000..1c3ce3fa1 --- /dev/null +++ b/changelog/unreleased/pull-22222 @@ -0,0 +1,15 @@ +Bugfix: Correctly apply a `--tag` filter that contains an empty tag + +The empty tag in a `--tag` or `--keep-tag` list selects snapshots without +any tags. Restic stopped looking at a tag list once it reached the empty +tag, so a list like `,foo` matched every untagged snapshot instead of +matching nothing, while the equivalent `foo,` matched nothing. For +`forget` that meant snapshots outside the filter were considered for +removal. + +Restic now requires an empty tag to be the only tag in its list. A list +that combines it with another tag, such as `,foo` or `foo,`, is rejected +when the command line is parsed, instead of quietly selecting either the +wrong snapshots or none at all. + +https://github.com/restic/restic/pull/22222 diff --git a/internal/data/snapshot.go b/internal/data/snapshot.go index 4e2f81503..18972ff79 100644 --- a/internal/data/snapshot.go +++ b/internal/data/snapshot.go @@ -178,11 +178,15 @@ func (sn *Snapshot) hasTag(tag string) bool { return slices.Contains(sn.Tags, tag) } -// HasTags returns true if the snapshot has all the tags in l. +// HasTags returns true if the snapshot has all the tags in l. The empty tag +// only matches a snapshot without any tags. func (sn *Snapshot) HasTags(l []string) bool { for _, tag := range l { - if tag == "" && len(sn.Tags) == 0 { - return true + if tag == "" { + if len(sn.Tags) != 0 { + return false + } + continue // Continue here to ensure that any other tag in `l` will result in no match. } if !sn.hasTag(tag) { return false diff --git a/internal/data/snapshot_test.go b/internal/data/snapshot_test.go index cf5a7aab5..84c93228e 100644 --- a/internal/data/snapshot_test.go +++ b/internal/data/snapshot_test.go @@ -19,12 +19,29 @@ func TestNewSnapshot(t *testing.T) { func TestTagList(t *testing.T) { paths := []string{"/home/foobar"} - tags := []string{""} - sn, _ := data.NewSnapshot(paths, nil, "foo", time.Now()) + for _, test := range []struct { + snapshotTags []string + filterTags []string + match bool + }{ + {nil, []string{""}, true}, + {[]string{"foo"}, []string{""}, false}, + {nil, []string{"foo"}, false}, + {[]string{"foo"}, []string{"foo"}, true}, + // the empty tag requires an untagged snapshot, so it can never be + // combined with another tag, no matter in which order they are given + {nil, []string{"", "foo"}, false}, + {nil, []string{"foo", ""}, false}, + {[]string{"foo"}, []string{"", "foo"}, false}, + {[]string{"foo"}, []string{"foo", ""}, false}, + } { + sn, err := data.NewSnapshot(paths, test.snapshotTags, "foo", time.Now()) + rtest.OK(t, err) - r := sn.HasTags(tags) - rtest.Assert(t, r, "Failed to match untagged snapshot") + rtest.Assert(t, sn.HasTags(test.filterTags) == test.match, + "expected %v for snapshot tags %v and filter %v", test.match, test.snapshotTags, test.filterTags) + } } func TestLoadJSONUnpacked(t *testing.T) { diff --git a/internal/data/tag_list.go b/internal/data/tag_list.go index ec02f7e1b..4139e5624 100644 --- a/internal/data/tag_list.go +++ b/internal/data/tag_list.go @@ -2,6 +2,7 @@ package data import ( "fmt" + "slices" "strings" ) @@ -10,12 +11,17 @@ type TagList []string // splitTagList splits a string into a list of tags. The tags in the string // need to be separated by commas. Whitespace is stripped around the individual -// tags. -func splitTagList(s string) (l TagList) { +// tags. The empty tag only matches snapshots without any tags, so it is +// rejected unless it is the only tag in the list. +func splitTagList(s string) (TagList, error) { + var l TagList for t := range strings.SplitSeq(s, ",") { l = append(l, strings.TrimSpace(t)) } - return l + if len(l) > 1 && slices.Contains(l, "") { + return nil, fmt.Errorf("invalid tag list %q, the empty tag must be the only tag in the list", s) + } + return l, nil } func (l TagList) String() string { @@ -24,7 +30,11 @@ func (l TagList) String() string { // Set updates the TagList's value. func (l *TagList) Set(s string) error { - *l = splitTagList(s) + tags, err := splitTagList(s) + if err != nil { + return err + } + *l = tags return nil } @@ -56,7 +66,11 @@ func (l TagLists) Flatten() (tags TagList) { // Set updates the TagList's value. func (l *TagLists) Set(s string) error { - *l = append(*l, splitTagList(s)) + tags, err := splitTagList(s) + if err != nil { + return err + } + *l = append(*l, tags) return nil } diff --git a/internal/data/tag_list_test.go b/internal/data/tag_list_test.go index 1c406b8c9..17f9df669 100644 --- a/internal/data/tag_list_test.go +++ b/internal/data/tag_list_test.go @@ -44,3 +44,37 @@ func TestTagLists_Flatten(t *testing.T) { }) } } + +func TestTagListSet(t *testing.T) { + for _, test := range []struct { + value string + want TagList + }{ + {"", TagList{""}}, + {"foo", TagList{"foo"}}, + {"foo,bar", TagList{"foo", "bar"}}, + {" foo , bar ", TagList{"foo", "bar"}}, + } { + var l TagList + rtest.OK(t, l.Set(test.value)) + rtest.Equals(t, test.want, l) + + var ls TagLists + rtest.OK(t, ls.Set(test.value)) + rtest.Equals(t, TagLists{test.want}, ls) + } +} + +func TestTagListSetInvalid(t *testing.T) { + // the empty tag only matches snapshots without any tags, so a list that + // mixes it with another tag can never match and is rejected + for _, value := range []string{",", ",foo", "foo,", "foo,,bar", "foo, ,bar"} { + var l TagList + rtest.Assert(t, l.Set(value) != nil, "missing error for tag list %q", value) + rtest.Equals(t, TagList(nil), l) + + var ls TagLists + rtest.Assert(t, ls.Set(value) != nil, "missing error for tag list %q", value) + rtest.Equals(t, TagLists(nil), ls) + } +}