Skip to content

Commit e60fb6b

Browse files
committed
fix restore
Signed-off-by: Jörn Friedrich Dreyer <[email protected]>
1 parent 15db9ad commit e60fb6b

File tree

1 file changed

+27
-22
lines changed
  • pkg/storage/utils/decomposedfs/tree

1 file changed

+27
-22
lines changed

Diff for: pkg/storage/utils/decomposedfs/tree/tree.go

+27-22
Original file line numberDiff line numberDiff line change
@@ -817,26 +817,40 @@ func (t *Tree) createNode(n *node.Node, owner *userpb.UserId) (err error) {
817817
return n.WriteAllNodeMetadata(owner)
818818
}
819819

820+
// readTrashLink returns nodeID and timestamp
821+
func readTrashLink(path string) (string, string, error) {
822+
link, err := os.Readlink(path)
823+
if err != nil {
824+
return "", "", err
825+
}
826+
// ../../../../../nodes/e5/6c/75/a8/-d235-4cbb-8b4e-48b6fd0f2094.T.2022-02-16T14:38:11.769917408Z
827+
// TODO use filepath.Separator to support windows
828+
link = strings.ReplaceAll(link, "/", "")
829+
// ..........nodese56c75a8-d235-4cbb-8b4e-48b6fd0f2094.T.2022-02-16T14:38:11.769917408Z
830+
if link[0:15] != "..........nodes" || link[51:54] != ".T." {
831+
return "", "", errtypes.InternalError("malformed trash link")
832+
}
833+
return link[15:51], link[54:], nil
834+
}
835+
820836
// TODO refactor the returned params into Node properties? would make all the path transformations go away...
821837
func (t *Tree) readRecycleItem(ctx context.Context, spaceID, key, path string) (recycleNode *node.Node, trashItem string, deletedNodePath string, origin string, err error) {
822838
if key == "" {
823839
return nil, "", "", "", errtypes.InternalError("key is empty")
824840
}
825841

826-
trashItem = filepath.Join(t.lookup.InternalRoot(), "trash", spaceID, key, path)
842+
trashItem = filepath.Join(t.lookup.InternalRoot(), "spaces", Pathify(spaceID, 1, 2), "trash", Pathify(key, 4, 2), path)
827843

828-
var link string
829-
link, err = os.Readlink(trashItem)
844+
nodeID, timeSuffix, err := readTrashLink(trashItem)
830845
if err != nil {
831846
appctx.GetLogger(ctx).Error().Err(err).Str("trashItem", trashItem).Msg("error reading trash link")
832847
return
833848
}
834849

835-
var attrStr string
836-
trashNodeID := filepath.Base(link)
837-
deletedNodePath = t.lookup.InternalPath(spaceID, trashNodeID)
850+
deletedNodePath = t.lookup.InternalPath(spaceID, nodeID) + node.TrashIDDelimiter + timeSuffix
838851

839852
owner := &userpb.UserId{}
853+
var attrStr string
840854
// lookup ownerId in extended attributes
841855
if attrStr, err = xattrs.Get(deletedNodePath, xattrs.OwnerIDAttr); err == nil {
842856
owner.OpaqueId = attrStr
@@ -856,7 +870,7 @@ func (t *Tree) readRecycleItem(ctx context.Context, spaceID, key, path string) (
856870
return
857871
}
858872

859-
recycleNode = node.New(spaceID, trashNodeID, "", "", 0, "", owner, t.lookup)
873+
recycleNode = node.New(spaceID, nodeID, "", "", 0, "", owner, t.lookup)
860874
// lookup blobID in extended attributes
861875
if attrStr, err = xattrs.Get(deletedNodePath, xattrs.BlobIDAttr); err == nil {
862876
recycleNode.BlobID = attrStr
@@ -879,37 +893,28 @@ func (t *Tree) readRecycleItem(ctx context.Context, spaceID, key, path string) (
879893
}
880894

881895
// look up space root from the trashed node
882-
err = recycleNode.FindStorageSpaceRoot()
883-
884-
if path == "" || path == "/" {
885-
parts := strings.SplitN(filepath.Base(link), node.TrashIDDelimiter, 2)
886-
if len(parts) != 2 {
887-
appctx.GetLogger(ctx).Error().Err(err).Str("trashItem", trashItem).Interface("parts", parts).Msg("malformed trash link")
888-
return
889-
}
890-
// update the node id, drop the `.T.{timestamp}` suffix
891-
recycleNode.ID = parts[0]
896+
if err = recycleNode.FindStorageSpaceRoot(); err != nil {
897+
return
892898
}
893899

894900
// get origin node, is relative to space root
895901
origin = "/"
896902

897903
deletedNodeRootPath := deletedNodePath
898904
if path != "" && path != "/" {
899-
trashItemRoot := filepath.Join(t.lookup.InternalRoot(), "trash", spaceID, key)
900-
var rootLink string
901-
rootLink, err = os.Readlink(trashItemRoot)
905+
trashItemRoot := filepath.Join(t.lookup.InternalRoot(), "spaces", Pathify(spaceID, 1, 2), "trash", Pathify(key, 4, 2))
906+
nodeID, _, err = readTrashLink(trashItemRoot)
902907
if err != nil {
903908
appctx.GetLogger(ctx).Error().Err(err).Str("trashItem", trashItem).Msg("error reading trash link")
904909
return
905910
}
906-
deletedNodeRootPath = t.lookup.InternalPath(spaceID, filepath.Base(rootLink))
911+
deletedNodeRootPath = t.lookup.InternalPath(spaceID, nodeID)
907912
}
908913
// lookup origin path in extended attributes
909914
if attrStr, err = xattrs.Get(deletedNodeRootPath, xattrs.TrashOriginAttr); err == nil {
910915
origin = filepath.Join(attrStr, path)
911916
} else {
912-
log.Error().Err(err).Str("trashItem", trashItem).Str("link", link).Str("deletedNodePath", deletedNodePath).Msg("could not read origin path, restoring to /")
917+
log.Error().Err(err).Str("trashItem", trashItem).Str("deletedNodePath", deletedNodePath).Msg("could not read origin path, restoring to /")
913918
}
914919

915920
return

0 commit comments

Comments
 (0)