diff --git a/changelog/unreleased/proper-space-delete.md b/changelog/unreleased/proper-space-delete.md new file mode 100644 index 0000000000..b0f07a02f8 --- /dev/null +++ b/changelog/unreleased/proper-space-delete.md @@ -0,0 +1,5 @@ +Bugfix: Delete Blobs when Space is deleted + +Delete all blobs of a space when the space is deleted. + +https://github.com/cs3org/reva/pull/5026 diff --git a/pkg/storage/utils/decomposedfs/spaces.go b/pkg/storage/utils/decomposedfs/spaces.go index 760ad96552..0a369a61f9 100644 --- a/pkg/storage/utils/decomposedfs/spaces.go +++ b/pkg/storage/utils/decomposedfs/spaces.go @@ -50,6 +50,7 @@ import ( "github.com/cs3org/reva/v2/pkg/storagespace" "github.com/cs3org/reva/v2/pkg/utils" "github.com/pkg/errors" + "github.com/shamaton/msgpack/v2" "golang.org/x/sync/errgroup" ) @@ -744,12 +745,58 @@ func (fs *Decomposedfs) DeleteStorageSpace(ctx context.Context, req *provider.De return err } + root := fs.getSpaceRoot(spaceID) + + // walkfn will delete the blob if the node has one + walkfn := func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if filepath.Ext(path) != ".mpk" { + return nil + } + + b, err := os.ReadFile(path) + if err != nil { + return err + } + + m := map[string][]byte{} + if err := msgpack.Unmarshal(b, &m); err != nil { + return err + } + + bid := m["user.ocis.blobid"] + if string(bid) == "" { + return nil + } + + if err := fs.tp.DeleteBlob(&node.Node{ + BlobID: string(bid), + SpaceID: spaceID, + }); err != nil { + return err + } + + // remove .mpk file so subsequent attempts will not try to delete the blob again + return os.Remove(path) + } + + // This is deletes all blobs of the space + // NOTE: This isn't needed when no s3 is used, but we can't differentiate that here... + if err := filepath.Walk(root, walkfn); err != nil { + return err + } + // remove space metadata - if err := os.RemoveAll(fs.getSpaceRoot(spaceID)); err != nil { + if err := os.RemoveAll(root); err != nil { return err } - // TODO remove space blobs with s3 backend by adding a purge method to the Blobstore interface + // try removing the space root node + // Note that this will fail when there are other spaceids starting with the same two digits. + _ = os.Remove(filepath.Dir(root)) return nil }