From afaa3bf73708e5ce877170331e72c75c834ad14b Mon Sep 17 00:00:00 2001 From: Christian Richter Date: Wed, 21 Feb 2024 15:29:30 +0100 Subject: [PATCH 1/2] Fix recursive trashcan purge MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jörn Friedrich Dreyer Signed-off-by: Christian Richter --- .../unreleased/fix-recusrive-trashcan-purge | 6 ++ pkg/storage/utils/decomposedfs/tree/tree.go | 59 ++++++++++++++----- 2 files changed, 50 insertions(+), 15 deletions(-) create mode 100644 changelog/unreleased/fix-recusrive-trashcan-purge diff --git a/changelog/unreleased/fix-recusrive-trashcan-purge b/changelog/unreleased/fix-recusrive-trashcan-purge new file mode 100644 index 0000000000..4e7b222253 --- /dev/null +++ b/changelog/unreleased/fix-recusrive-trashcan-purge @@ -0,0 +1,6 @@ +Bugfix: Fix recursive trashcan purge + +We have fixed a bug in the trashcan purge process that did not delete folder structures recursively. + +https://github.com/cs3org/reva/pull/4533 +https://github.com/owncloud/ocis/issues/8473 \ No newline at end of file diff --git a/pkg/storage/utils/decomposedfs/tree/tree.go b/pkg/storage/utils/decomposedfs/tree/tree.go index e6d108c0e9..7a709ee278 100644 --- a/pkg/storage/utils/decomposedfs/tree/tree.go +++ b/pkg/storage/utils/decomposedfs/tree/tree.go @@ -24,7 +24,6 @@ import ( "fmt" "io" "io/fs" - iofs "io/fs" "os" "path/filepath" "regexp" @@ -42,7 +41,6 @@ import ( "github.com/cs3org/reva/v2/pkg/utils" "github.com/google/uuid" "github.com/pkg/errors" - "github.com/rs/zerolog/log" "go-micro.dev/v4/store" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/trace" @@ -172,7 +170,7 @@ func (t *Tree) TouchFile(ctx context.Context, n *node.Node, markprocessing bool, return errors.Wrap(err, "Decomposedfs: could not remove symlink child entry") } } - if errors.Is(err, iofs.ErrNotExist) || link != "../"+n.ID { + if errors.Is(err, fs.ErrNotExist) || link != "../"+n.ID { relativeNodePath := filepath.Join("../../../../../", lookup.Pathify(n.ID, 4, 2)) if err = os.Symlink(relativeNodePath, childNameLink); err != nil { return errors.Wrap(err, "Decomposedfs: could not symlink child entry") @@ -529,6 +527,8 @@ func (t *Tree) Delete(ctx context.Context, n *node.Node) (err error) { // RestoreRecycleItemFunc returns a node and a function to restore it from the trash. func (t *Tree) RestoreRecycleItemFunc(ctx context.Context, spaceid, key, trashPath string, targetNode *node.Node) (*node.Node, *node.Node, func() error, error) { + logger := appctx.GetLogger(ctx) + recycleNode, trashItem, deletedNodePath, origin, err := t.readRecycleItem(ctx, spaceid, key, trashPath) if err != nil { return nil, nil, nil, err @@ -604,7 +604,7 @@ func (t *Tree) RestoreRecycleItemFunc(ctx context.Context, spaceid, key, trashPa deletePath = filepath.Join(resolvedTrashRoot, trashPath) } if err = os.Remove(deletePath); err != nil { - log.Error().Err(err).Str("trashItem", trashItem).Msg("error deleting trash item") + logger.Error().Err(err).Str("trashItem", trashItem).Msg("error deleting trash item") } var sizeDiff int64 @@ -624,13 +624,21 @@ func (t *Tree) RestoreRecycleItemFunc(ctx context.Context, spaceid, key, trashPa // PurgeRecycleItemFunc returns a node and a function to purge it from the trash func (t *Tree) PurgeRecycleItemFunc(ctx context.Context, spaceid, key string, path string) (*node.Node, func() error, error) { + logger := appctx.GetLogger(ctx) + rn, trashItem, deletedNodePath, _, err := t.readRecycleItem(ctx, spaceid, key, path) if err != nil { return nil, nil, err } + timeSuffix := strings.SplitN(filepath.Base(deletedNodePath), node.TrashIDDelimiter, 2) + if len(timeSuffix) != 2 { + return nil, nil, errtypes.InternalError("invalid trash path") + } + fn := func() error { - if err := t.removeNode(ctx, deletedNodePath, rn); err != nil { + + if err := t.removeNode(ctx, deletedNodePath, timeSuffix[1], rn); err != nil { return err } @@ -644,7 +652,7 @@ func (t *Tree) PurgeRecycleItemFunc(ctx context.Context, spaceid, key string, pa deletePath = filepath.Join(resolvedTrashRoot, path) } if err = os.Remove(deletePath); err != nil { - log.Error().Err(err).Str("deletePath", deletePath).Msg("error deleting trash item") + logger.Error().Err(err).Str("deletePath", deletePath).Msg("error deleting trash item") return err } @@ -654,22 +662,41 @@ func (t *Tree) PurgeRecycleItemFunc(ctx context.Context, spaceid, key string, pa return rn, fn, nil } -func (t *Tree) removeNode(ctx context.Context, path string, n *node.Node) error { +func (t *Tree) removeNode(ctx context.Context, path, timeSuffix string, n *node.Node) error { + logger := appctx.GetLogger(ctx) + + if timeSuffix != "" { + n.ID = n.ID + node.TrashIDDelimiter + timeSuffix + } + + if n.IsDir(ctx) { + item, err := t.ListFolder(ctx, n) + if err != nil { + logger.Error().Err(err).Str("path", path).Msg("error listing folder") + } else { + for _, child := range item { + if err := t.removeNode(ctx, child.InternalPath(), "", child); err != nil { + return err + } + } + } + } + // delete the actual node if err := utils.RemoveItem(path); err != nil { - log.Error().Err(err).Str("path", path).Msg("error purging node") + logger.Error().Err(err).Str("path", path).Msg("error purging node") return err } if err := t.lookup.MetadataBackend().Purge(path); err != nil { - log.Error().Err(err).Str("path", t.lookup.MetadataBackend().MetadataPath(path)).Msg("error purging node metadata") + logger.Error().Err(err).Str("path", t.lookup.MetadataBackend().MetadataPath(path)).Msg("error purging node metadata") return err } // delete blob from blobstore if n.BlobID != "" { if err := t.DeleteBlob(n); err != nil { - log.Error().Err(err).Str("blobID", n.BlobID).Msg("error purging nodes blob") + logger.Error().Err(err).Str("blobID", n.BlobID).Msg("error purging nodes blob") return err } } @@ -677,7 +704,7 @@ func (t *Tree) removeNode(ctx context.Context, path string, n *node.Node) error // delete revisions revs, err := filepath.Glob(n.InternalPath() + node.RevisionIDDelimiter + "*") if err != nil { - log.Error().Err(err).Str("path", n.InternalPath()+node.RevisionIDDelimiter+"*").Msg("glob failed badly") + logger.Error().Err(err).Str("path", n.InternalPath()+node.RevisionIDDelimiter+"*").Msg("glob failed badly") return err } for _, rev := range revs { @@ -687,18 +714,18 @@ func (t *Tree) removeNode(ctx context.Context, path string, n *node.Node) error bID, err := t.lookup.ReadBlobIDAttr(ctx, rev) if err != nil { - log.Error().Err(err).Str("revision", rev).Msg("error reading blobid attribute") + logger.Error().Err(err).Str("revision", rev).Msg("error reading blobid attribute") return err } if err := utils.RemoveItem(rev); err != nil { - log.Error().Err(err).Str("revision", rev).Msg("error removing revision node") + logger.Error().Err(err).Str("revision", rev).Msg("error removing revision node") return err } if bID != "" { if err := t.DeleteBlob(&node.Node{SpaceID: n.SpaceID, BlobID: bID}); err != nil { - log.Error().Err(err).Str("revision", rev).Str("blobID", bID).Msg("error removing revision node blob") + logger.Error().Err(err).Str("revision", rev).Str("blobID", bID).Msg("error removing revision node blob") return err } } @@ -761,6 +788,8 @@ var nodeIDRegep = regexp.MustCompile(`.*/nodes/([^.]*).*`) // TODO refactor the returned params into Node properties? would make all the path transformations go away... func (t *Tree) readRecycleItem(ctx context.Context, spaceID, key, path string) (recycleNode *node.Node, trashItem string, deletedNodePath string, origin string, err error) { + logger := appctx.GetLogger(ctx) + if key == "" { return nil, "", "", "", errtypes.InternalError("key is empty") } @@ -823,7 +852,7 @@ func (t *Tree) readRecycleItem(ctx context.Context, spaceID, key, path string) ( if attrBytes, err = backend.Get(ctx, resolvedTrashItem, prefixes.TrashOriginAttr); err == nil { origin = filepath.Join(string(attrBytes), path) } else { - log.Error().Err(err).Str("trashItem", trashItem).Str("deletedNodePath", deletedNodePath).Msg("could not read origin path, restoring to /") + logger.Error().Err(err).Str("trashItem", trashItem).Str("deletedNodePath", deletedNodePath).Msg("could not read origin path, restoring to /") } return From 3cf3e5c3d51c1ce72fde5566963240f72bedf36e Mon Sep 17 00:00:00 2001 From: Christian Richter Date: Wed, 21 Feb 2024 15:47:09 +0100 Subject: [PATCH 2/2] fix bug when deletion timestamp is missing Signed-off-by: Christian Richter --- pkg/storage/utils/decomposedfs/tree/tree.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/storage/utils/decomposedfs/tree/tree.go b/pkg/storage/utils/decomposedfs/tree/tree.go index 7a709ee278..6fc5176af3 100644 --- a/pkg/storage/utils/decomposedfs/tree/tree.go +++ b/pkg/storage/utils/decomposedfs/tree/tree.go @@ -631,14 +631,15 @@ func (t *Tree) PurgeRecycleItemFunc(ctx context.Context, spaceid, key string, pa return nil, nil, err } + ts := "" timeSuffix := strings.SplitN(filepath.Base(deletedNodePath), node.TrashIDDelimiter, 2) - if len(timeSuffix) != 2 { - return nil, nil, errtypes.InternalError("invalid trash path") + if len(timeSuffix) == 2 { + ts = timeSuffix[1] } fn := func() error { - if err := t.removeNode(ctx, deletedNodePath, timeSuffix[1], rn); err != nil { + if err := t.removeNode(ctx, deletedNodePath, ts, rn); err != nil { return err }