mirror of
https://github.com/kopia/kopia.git
synced 2026-10-23 06:38:42 -04:00
cli: fixed snapshot delete to support deleting file (not directory) snapshots by object ID (#613)
This commit is contained in:
1 parent
f0b97b960b
commit
9faf6b33d0
3 files changed
+20
-12
No files matched your search
@@ -3,7 +3,6 @@ package cli
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
@@ -21,19 +20,16 @@ var (
|
||||
|
||||
func runDeleteCommand(ctx context.Context, rep repo.Repository) error {
|
||||
for _, id := range *snapshotDeleteIDs {
|
||||
if strings.HasPrefix(id, "k") {
|
||||
if err := deleteSnapshotsByRootObjectID(ctx, rep, object.ID(id)); err != nil {
|
||||
return errors.Wrapf(err, "error deleting snapshots by root ID %v", id)
|
||||
}
|
||||
} else {
|
||||
m, err := snapshot.LoadSnapshot(ctx, rep, manifest.ID(id))
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "error loading snapshot %v", id)
|
||||
}
|
||||
|
||||
if err := deleteSnapshot(ctx, rep, m); err != nil {
|
||||
m, err := snapshot.LoadSnapshot(ctx, rep, manifest.ID(id))
|
||||
if err == nil {
|
||||
// snapshot found by manifest ID, delete it directly.
|
||||
if err = deleteSnapshot(ctx, rep, m); err != nil {
|
||||
return errors.Wrapf(err, "error deleting %v", id)
|
||||
}
|
||||
} else if !errors.Is(err, snapshot.ErrSnapshotNotFound) {
|
||||
return errors.Wrapf(err, "error loading snapshot %v", id)
|
||||
} else if err := deleteSnapshotsByRootObjectID(ctx, rep, object.ID(id)); err != nil {
|
||||
return errors.Wrapf(err, "error deleting snapshots by root ID %v", id)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,9 @@ import (
|
||||
// ManifestType is the value of the "type" label for snapshot manifests.
|
||||
const ManifestType = "snapshot"
|
||||
|
||||
// ErrSnapshotNotFound is returned when a snapshot is not found.
|
||||
var ErrSnapshotNotFound = errors.Errorf("snapshot not found")
|
||||
|
||||
const (
|
||||
typeKey = manifest.TypeLabelKey
|
||||
|
||||
@@ -73,6 +76,10 @@ func LoadSnapshot(ctx context.Context, rep repo.Repository, manifestID manifest.
|
||||
|
||||
em, err := rep.GetManifest(ctx, manifestID, sm)
|
||||
if err != nil {
|
||||
if errors.Is(err, manifest.ErrNotFound) {
|
||||
return nil, ErrSnapshotNotFound
|
||||
}
|
||||
|
||||
return nil, errors.Wrap(err, "unable to find manifest entries")
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package snapshot_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
@@ -32,6 +33,10 @@ func TestSnapshotsAPI(t *testing.T) {
|
||||
Path: "/some/other/path",
|
||||
}
|
||||
|
||||
if _, err := snapshot.LoadSnapshot(ctx, env.Repository, "no-such-manifest-id"); !errors.Is(err, snapshot.ErrSnapshotNotFound) {
|
||||
t.Errorf("unexpected error when deleting snapshot for manifest that does not exist: %v", err)
|
||||
}
|
||||
|
||||
verifySnapshotManifestIDs(t, env.Repository, nil, nil)
|
||||
verifySnapshotManifestIDs(t, env.Repository, &src1, nil)
|
||||
verifySnapshotManifestIDs(t, env.Repository, &src2, nil)
|
||||
|
||||
Reference in new issue
Block a user