mirror of
https://github.com/restic/restic.git
synced 2025-08-23 17:28:02 +00:00
Ensure consistent naming for <snapshot>:<subfolder>
syntax
This commit is contained in:
@@ -83,15 +83,15 @@ func (f *SnapshotFilter) findLatest(ctx context.Context, be Lister, loader Loade
|
||||
return latest, nil
|
||||
}
|
||||
|
||||
func splitSnapshotID(s string) (id, subpath string) {
|
||||
id, subpath, _ = strings.Cut(s, ":")
|
||||
func splitSnapshotID(s string) (id, subfolder string) {
|
||||
id, subfolder, _ = strings.Cut(s, ":")
|
||||
return
|
||||
}
|
||||
|
||||
// FindSnapshot takes a string and tries to find a snapshot whose ID matches
|
||||
// the string as closely as possible.
|
||||
func FindSnapshot(ctx context.Context, be Lister, loader LoaderUnpacked, s string) (*Snapshot, string, error) {
|
||||
s, subpath := splitSnapshotID(s)
|
||||
s, subfolder := splitSnapshotID(s)
|
||||
|
||||
// no need to list snapshots if `s` is already a full id
|
||||
id, err := ParseID(s)
|
||||
@@ -103,27 +103,27 @@ func FindSnapshot(ctx context.Context, be Lister, loader LoaderUnpacked, s strin
|
||||
}
|
||||
}
|
||||
sn, err := LoadSnapshot(ctx, loader, id)
|
||||
return sn, subpath, err
|
||||
return sn, subfolder, err
|
||||
}
|
||||
|
||||
// FindLatest returns either the latest of a filtered list of all snapshots
|
||||
// or a snapshot specified by `snapshotID`.
|
||||
func (f *SnapshotFilter) FindLatest(ctx context.Context, be Lister, loader LoaderUnpacked, snapshotID string) (*Snapshot, string, error) {
|
||||
id, subpath := splitSnapshotID(snapshotID)
|
||||
id, subfolder := splitSnapshotID(snapshotID)
|
||||
if id == "latest" {
|
||||
sn, err := f.findLatest(ctx, be, loader)
|
||||
if err == ErrNoSnapshotFound {
|
||||
err = fmt.Errorf("snapshot filter (Paths:%v Tags:%v Hosts:%v): %w",
|
||||
f.Paths, f.Tags, f.Hosts, err)
|
||||
}
|
||||
return sn, subpath, err
|
||||
return sn, subfolder, err
|
||||
}
|
||||
return FindSnapshot(ctx, be, loader, snapshotID)
|
||||
}
|
||||
|
||||
type SnapshotFindCb func(string, *Snapshot, error) error
|
||||
|
||||
var ErrInvalidSnapshotSyntax = errors.New("snapshot:path syntax not allowed")
|
||||
var ErrInvalidSnapshotSyntax = errors.New("<snapshot>:<subfolder> syntax not allowed")
|
||||
|
||||
// FindAll yields Snapshots, either given explicitly by `snapshotIDs` or filtered from the list of all snapshots.
|
||||
func (f *SnapshotFilter) FindAll(ctx context.Context, be Lister, loader LoaderUnpacked, snapshotIDs []string, fn SnapshotFindCb) error {
|
||||
@@ -153,9 +153,9 @@ func (f *SnapshotFilter) FindAll(ctx context.Context, be Lister, loader LoaderUn
|
||||
} else if strings.HasPrefix(s, "latest:") {
|
||||
err = ErrInvalidSnapshotSyntax
|
||||
} else {
|
||||
var subpath string
|
||||
sn, subpath, err = FindSnapshot(ctx, be, loader, s)
|
||||
if err == nil && subpath != "" {
|
||||
var subfolder string
|
||||
sn, subfolder, err = FindSnapshot(ctx, be, loader, s)
|
||||
if err == nil && subfolder != "" {
|
||||
err = ErrInvalidSnapshotSyntax
|
||||
} else if err == nil {
|
||||
if ids.Has(*sn.ID()) {
|
||||
|
@@ -51,24 +51,24 @@ func TestFindLatestWithSubpath(t *testing.T) {
|
||||
desiredSnapshot := restic.TestCreateSnapshot(t, repo, parseTimeUTC("2017-07-07 07:07:07"), 1)
|
||||
|
||||
for _, exp := range []struct {
|
||||
query string
|
||||
subpath string
|
||||
query string
|
||||
subfolder string
|
||||
}{
|
||||
{"latest", ""},
|
||||
{"latest:subpath", "subpath"},
|
||||
{"latest:subfolder", "subfolder"},
|
||||
{desiredSnapshot.ID().Str(), ""},
|
||||
{desiredSnapshot.ID().Str() + ":subpath", "subpath"},
|
||||
{desiredSnapshot.ID().Str() + ":subfolder", "subfolder"},
|
||||
{desiredSnapshot.ID().String(), ""},
|
||||
{desiredSnapshot.ID().String() + ":subpath", "subpath"},
|
||||
{desiredSnapshot.ID().String() + ":subfolder", "subfolder"},
|
||||
} {
|
||||
t.Run("", func(t *testing.T) {
|
||||
sn, subpath, err := (&restic.SnapshotFilter{}).FindLatest(context.TODO(), repo.Backend(), repo, exp.query)
|
||||
sn, subfolder, err := (&restic.SnapshotFilter{}).FindLatest(context.TODO(), repo.Backend(), repo, exp.query)
|
||||
if err != nil {
|
||||
t.Fatalf("FindLatest returned error: %v", err)
|
||||
}
|
||||
|
||||
test.Assert(t, *sn.ID() == *desiredSnapshot.ID(), "FindLatest returned wrong snapshot ID: %v", *sn.ID())
|
||||
test.Assert(t, subpath == exp.subpath, "FindLatest returned wrong path in snapshot: %v", subpath)
|
||||
test.Assert(t, subfolder == exp.subfolder, "FindLatest returned wrong path in snapshot: %v", subfolder)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -79,7 +79,7 @@ func TestFindAllSubpathError(t *testing.T) {
|
||||
|
||||
count := 0
|
||||
test.OK(t, (&restic.SnapshotFilter{}).FindAll(context.TODO(), repo.Backend(), repo,
|
||||
[]string{"latest:subpath", desiredSnapshot.ID().Str() + ":subpath"},
|
||||
[]string{"latest:subfolder", desiredSnapshot.ID().Str() + ":subfolder"},
|
||||
func(id string, sn *restic.Snapshot, err error) error {
|
||||
if err == restic.ErrInvalidSnapshotSyntax {
|
||||
count++
|
||||
@@ -87,5 +87,5 @@ func TestFindAllSubpathError(t *testing.T) {
|
||||
}
|
||||
return err
|
||||
}))
|
||||
test.Assert(t, count == 2, "unexpected number of subpath errors: %v, wanted %v", count, 2)
|
||||
test.Assert(t, count == 2, "unexpected number of subfolder errors: %v, wanted %v", count, 2)
|
||||
}
|
||||
|
@@ -193,23 +193,23 @@ func FindTreeDirectory(ctx context.Context, repo BlobLoader, id *ID, dir string)
|
||||
}
|
||||
|
||||
dirs := strings.Split(path.Clean(dir), "/")
|
||||
subpath := ""
|
||||
subfolder := ""
|
||||
|
||||
for _, name := range dirs {
|
||||
if name == "" || name == "." {
|
||||
continue
|
||||
}
|
||||
subpath = path.Join(subpath, name)
|
||||
subfolder = path.Join(subfolder, name)
|
||||
tree, err := LoadTree(ctx, repo, *id)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("path %s: %w", subpath, err)
|
||||
return nil, fmt.Errorf("path %s: %w", subfolder, err)
|
||||
}
|
||||
node := tree.Find(name)
|
||||
if node == nil {
|
||||
return nil, fmt.Errorf("path %s: not found", subpath)
|
||||
return nil, fmt.Errorf("path %s: not found", subfolder)
|
||||
}
|
||||
if node.Type != "dir" || node.Subtree == nil {
|
||||
return nil, fmt.Errorf("path %s: not a directory", subpath)
|
||||
return nil, fmt.Errorf("path %s: not a directory", subfolder)
|
||||
}
|
||||
id = node.Subtree
|
||||
}
|
||||
|
@@ -216,9 +216,9 @@ func TestFindTreeDirectory(t *testing.T) {
|
||||
sn := restic.TestCreateSnapshot(t, repo, parseTimeUTC("2017-07-07 07:07:08"), 3)
|
||||
|
||||
for _, exp := range []struct {
|
||||
subpath string
|
||||
id restic.ID
|
||||
err error
|
||||
subfolder string
|
||||
id restic.ID
|
||||
err error
|
||||
}{
|
||||
{"", restic.TestParseID("c25199703a67455b34cc0c6e49a8ac8861b268a5dd09dc5b2e31e7380973fc97"), nil},
|
||||
{"/", restic.TestParseID("c25199703a67455b34cc0c6e49a8ac8861b268a5dd09dc5b2e31e7380973fc97"), nil},
|
||||
@@ -231,7 +231,7 @@ func TestFindTreeDirectory(t *testing.T) {
|
||||
{"dir-21/dir-24", restic.TestParseID("74626b3fb2bd4b3e572b81a4059b3e912bcf2a8f69fecd9c187613b7173f13b1"), nil},
|
||||
} {
|
||||
t.Run("", func(t *testing.T) {
|
||||
id, err := restic.FindTreeDirectory(context.TODO(), repo, sn.Tree, exp.subpath)
|
||||
id, err := restic.FindTreeDirectory(context.TODO(), repo, sn.Tree, exp.subfolder)
|
||||
if exp.err == nil {
|
||||
rtest.OK(t, err)
|
||||
rtest.Assert(t, exp.id == *id, "unexpected id, expected %v, got %v", exp.id, id)
|
||||
|
Reference in New Issue
Block a user