diff --git a/src/internal/kopia/snapshot_manager.go b/src/internal/kopia/snapshot_manager.go index 57e72b9fc..9c72396f0 100644 --- a/src/internal/kopia/snapshot_manager.go +++ b/src/internal/kopia/snapshot_manager.go @@ -42,7 +42,13 @@ func serviceCatTag(p path.Path) string { return p.Service().String() + p.Category().String() } -func makeTagKV(k string) (string, string) { +// MakeTagKV normalizes the provided key to protect it from clobbering +// similarly named tags from non-user input (user inputs are still open +// to collisions amongst eachother). +// Returns the normalized Key plus a default value. If you're embedding a +// key-only tag, the returned default value msut be used instead of an +// empty string. +func MakeTagKV(k string) (string, string) { return userTagPrefix + k, defaultTagValue } @@ -53,12 +59,12 @@ func tagsFromStrings(oc *OwnersCats) map[string]string { res := make(map[string]string, len(oc.ServiceCats)+len(oc.ResourceOwners)) for k := range oc.ServiceCats { - tk, tv := makeTagKV(k) + tk, tv := MakeTagKV(k) res[tk] = tv } for k := range oc.ResourceOwners { - tk, tv := makeTagKV(k) + tk, tv := MakeTagKV(k) res[tk] = tv } @@ -181,13 +187,14 @@ func fetchPrevManifests( return manifestsSinceLastComplete(mans), nil } -// FetchPrevSnapshotManifests returns a set of manifests for complete and maybe +// fetchPrevSnapshotManifests returns a set of manifests for complete and maybe // incomplete snapshots for the given (resource owner, service, category) // tuples. Up to two manifests can be returned per tuple: one complete and one // incomplete. An incomplete manifest may be returned if it is newer than the // newest complete manifest for the tuple. Manifests are deduped such that if // multiple tuples match the same manifest it will only be returned once. -func FetchPrevSnapshotManifests( +// External callers can access this via wrapper.FetchPrevSnapshotManifests(). +func fetchPrevSnapshotManifests( ctx context.Context, sm snapshotManager, oc *OwnersCats, @@ -199,10 +206,10 @@ func FetchPrevSnapshotManifests( // we can pass in. Can be expanded to return more than the most recent // snapshots, but may require more memory at runtime. for serviceCat := range oc.ServiceCats { - serviceTagKey, serviceTagValue := makeTagKV(serviceCat) + serviceTagKey, serviceTagValue := MakeTagKV(serviceCat) for resourceOwner := range oc.ResourceOwners { - resourceOwnerTagKey, resourceOwnerTagValue := makeTagKV(resourceOwner) + resourceOwnerTagKey, resourceOwnerTagValue := MakeTagKV(resourceOwner) tags := map[string]string{ serviceTagKey: serviceTagValue, diff --git a/src/internal/kopia/snapshot_manager_test.go b/src/internal/kopia/snapshot_manager_test.go index c0da2990c..78becc784 100644 --- a/src/internal/kopia/snapshot_manager_test.go +++ b/src/internal/kopia/snapshot_manager_test.go @@ -79,7 +79,7 @@ func newManifestInfo( structTags := make(map[string]struct{}, len(tags)) for _, t := range tags { - tk, _ := makeTagKV(t) + tk, _ := MakeTagKV(t) structTags[tk] = struct{}{} } @@ -442,7 +442,7 @@ func (suite *SnapshotFetchUnitSuite) TestFetchPrevSnapshots() { } } - snaps := FetchPrevSnapshotManifests(ctx, msm, test.input) + snaps := fetchPrevSnapshotManifests(ctx, msm, test.input) expected := make([]*snapshot.Manifest, 0, len(test.expectedIdxs)) for _, i := range test.expectedIdxs { @@ -532,7 +532,7 @@ func (suite *SnapshotFetchUnitSuite) TestFetchPrevSnapshotsWorksWithErrors() { }, } - snaps := FetchPrevSnapshotManifests(ctx, msm, input) + snaps := fetchPrevSnapshotManifests(ctx, msm, input) // Only 1 snapshot should be chosen because the other two attempts fail. // However, which one is returned is non-deterministic because maps are used. diff --git a/src/internal/kopia/wrapper.go b/src/internal/kopia/wrapper.go index 8c5db2008..fc5f24cff 100644 --- a/src/internal/kopia/wrapper.go +++ b/src/internal/kopia/wrapper.go @@ -548,7 +548,7 @@ func (w Wrapper) makeSnapshotWithRoot( ) (*BackupStats, error) { var man *snapshot.Manifest - prevSnaps := FetchPrevSnapshotManifests(ctx, w.c, oc) + prevSnaps := fetchPrevSnapshotManifests(ctx, w.c, oc) bc := &stats.ByteCounter{} @@ -782,3 +782,20 @@ func (w Wrapper) DeleteSnapshot( return nil } + +// FetchPrevSnapshotManifests returns a set of manifests for complete and maybe +// incomplete snapshots for the given (resource owner, service, category) +// tuples. Up to two manifests can be returned per tuple: one complete and one +// incomplete. An incomplete manifest may be returned if it is newer than the +// newest complete manifest for the tuple. Manifests are deduped such that if +// multiple tuples match the same manifest it will only be returned once. +func (w Wrapper) FetchPrevSnapshotManifests( + ctx context.Context, + oc OwnersCats, +) ([]*snapshot.Manifest, error) { + if w.c == nil { + return nil, errors.WithStack(errNotConnected) + } + + return fetchPrevSnapshotManifests(ctx, w.c, &oc), nil +} diff --git a/src/internal/kopia/wrapper_test.go b/src/internal/kopia/wrapper_test.go index d50cc991d..b6e4f8319 100644 --- a/src/internal/kopia/wrapper_test.go +++ b/src/internal/kopia/wrapper_test.go @@ -859,7 +859,7 @@ func (suite *KopiaIntegrationSuite) TestBackupCollections() { expectedTags := map[string]string{} for _, k := range baseTagKeys { - tk, tv := makeTagKV(k) + tk, tv := MakeTagKV(k) expectedTags[tk] = tv }