mirror of
https://github.com/restic/restic.git
synced 2026-12-04 15:44:46 +00:00
data: require an empty tag to be the only tag in a filter (#22222)
Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
This commit is contained in:
1 parent
f25ec9a5ab
commit
885519b1ba
5 files changed
+96
-12
No files matched your search
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user