Skip to content

Commit

Permalink
Fix broken parentids when files are restored to differen locations
Browse files Browse the repository at this point in the history
  • Loading branch information
aduffeck committed Jul 30, 2024
1 parent bcf02b3 commit fac559e
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions pkg/storage/fs/posix/trashbin/trashbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package trashbin

import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -213,22 +214,37 @@ func (tb *Trashbin) RestoreRecycleItem(ctx context.Context, ref *provider.Refere

trashRoot := trashRootForNode(n)
trashPath := filepath.Clean(filepath.Join(trashRoot, "files", key+".trashitem", relativePath))
originalPath, _, err := tb.readInfoFile(trashRoot, key)

restoreBaseNode, err := tb.lu.NodeFromID(ctx, restoreRef.GetResourceId())
if err != nil {
return err
}
restorePath := filepath.Join(n.SpaceRoot.InternalPath(), originalPath)
restorePath := filepath.Join(restoreBaseNode.InternalPath(), restoreRef.GetPath())

id, err := tb.lu.MetadataBackend().Get(ctx, trashPath, prefixes.IDAttr)
if err != nil {
return err
}

err = os.Rename(trashPath, restorePath)
// update parent id in case it was restored to a different location
parentID, err := tb.lu.MetadataBackend().Get(ctx, filepath.Dir(restorePath), prefixes.IDAttr)
if err != nil {
return err
}
if len(parentID) == 0 {
return fmt.Errorf("trashbin: parent id not found for %s", restorePath)
}

err = tb.lu.MetadataBackend().Set(ctx, trashPath, prefixes.ParentidAttr, parentID)
if err != nil {
return err
}

// restore the item
err = os.Rename(trashPath, restorePath)
if err != nil {
return err
}
tb.lu.CacheID(ctx, n.SpaceID, string(id), restorePath)

// cleanup trash info
Expand Down

0 comments on commit fac559e

Please sign in to comment.