diff --git a/internal/vaultik/remove_snapshot_test.go b/internal/vaultik/remove_snapshot_test.go index 919f989..df0f202 100644 --- a/internal/vaultik/remove_snapshot_test.go +++ b/internal/vaultik/remove_snapshot_test.go @@ -504,6 +504,36 @@ func TestRemoveSnapshot_ByRemoteKey(t *testing.T) { } } +// TestRemoveSnapshot_ByRemoteKeyInLocalIndex checks that a snapshot the +// local index holds, named by its remote key, is removed from the local +// index as well as from the destination store. +func TestRemoveSnapshot_ByRemoteKeyInLocalIndex(t *testing.T) { + log.Initialize(log.Config{}) + t.Parallel() + + store := newTestStorer() + addManifest(t, store, "snapshot-001", nil) + + // A backup wrote to this destination, so it carries the marker. + require.NoError(t, store.Put(context.Background(), markerFile, + bytes.NewReader(nil))) + + tv := vaultik.NewForTesting(store) + tv.Repositories = newLocalIndex(t, "snapshot-001") + + result, err := tv.RemoveSnapshot(snapshot.RemoteSnapshotKey("snapshot-001"), + &vaultik.RemoveOptions{Force: true}) + + require.NoError(t, err) + assert.True(t, result.RemoteRemoved) + assert.False(t, store.hasKey(remoteKeyPath("snapshot-001"))) + + snap, err := tv.Repositories.Snapshots.GetByID(context.Background(), + "snapshot-001") + require.NoError(t, err) + assert.Nil(t, snap) +} + // TestRemoveSnapshot_NotInLocalIndexDeleteFails checks that removing a // snapshot only the destination store holds fails when its metadata // cannot be deleted there, under --force and under --json: nothing was diff --git a/internal/vaultik/snapshot.go b/internal/vaultik/snapshot.go index 96267df..ae95c93 100644 --- a/internal/vaultik/snapshot.go +++ b/internal/vaultik/snapshot.go @@ -1467,7 +1467,7 @@ func (v *Vaultik) RemoveSnapshot( return result, err } - remoteKey, inLocalIndex, err := v.findSnapshotToRemove( + snapshotID, remoteKey, inLocalIndex, err := v.findSnapshotToRemove( snapshotID, opts.LocalOnly) if err != nil { return result, err @@ -1531,22 +1531,39 @@ func (v *Vaultik) RemoveSnapshot( return result, nil } -// findSnapshotToRemove returns the remote key of the snapshot that -// identifier names, and whether the local index holds it. A remote key, -// or the leading part of one, names a snapshot only on the destination -// store, so the local index never holds it. +// findSnapshotToRemove returns the ID of the snapshot that identifier +// names, its remote key, and whether the local index holds it. A remote +// key, or the leading part of one, is resolved on the destination store; +// when the local index holds the snapshot with that key, its ID is +// returned, so it is removed from both. func (v *Vaultik) findSnapshotToRemove( identifier string, localOnly bool, -) (string, bool, error) { - if isRemoteKeyOrPrefix(identifier) && !localOnly { - remoteKey, err := v.resolveSnapshotRemoteKey(identifier) +) (string, string, bool, error) { + if !isRemoteKeyOrPrefix(identifier) || localOnly { + inLocalIndex, err := v.checkSnapshotExists(identifier, localOnly) - return remoteKey, false, err + return identifier, snapshot.RemoteSnapshotKey(identifier), + inLocalIndex, err } - inLocalIndex, err := v.checkSnapshotExists(identifier, localOnly) + remoteKey, err := v.resolveSnapshotRemoteKey(identifier) + if err != nil { + return "", "", false, err + } - return snapshot.RemoteSnapshotKey(identifier), inLocalIndex, err + localIDs, err := v.localSnapshotIDs() + if err != nil { + return "", "", false, fmt.Errorf( + "looking up snapshot in local index: %w", err) + } + + for _, id := range localIDs { + if snapshot.RemoteSnapshotKey(id) == remoteKey { + return id, remoteKey, true, nil + } + } + + return identifier, remoteKey, false, nil } // checkSnapshotExists returns errSnapshotNotFound when the local index