From 45e4e691dd9d1726de53ae7c3e0d092de0d7d1ad Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Sat, 30 Sep 2023 14:33:22 +0530 Subject: [PATCH] fix move when srcPath is same as destPath --- zboxcore/allocationchange/moveobject.go | 86 +++++++++++++------------ zboxcore/sdk/moveworker.go | 1 + 2 files changed, 47 insertions(+), 40 deletions(-) diff --git a/zboxcore/allocationchange/moveobject.go b/zboxcore/allocationchange/moveobject.go index a8119b760..5f7ed9b63 100644 --- a/zboxcore/allocationchange/moveobject.go +++ b/zboxcore/allocationchange/moveobject.go @@ -7,18 +7,58 @@ import ( "github.com/0chain/errors" "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/pathutil" + "github.com/0chain/gosdk/core/util" "github.com/0chain/gosdk/zboxcore/fileref" + "github.com/google/uuid" ) type MoveFileChange struct { change ObjectTree fileref.RefEntity DestPath string + Uuid uuid.UUID } -func (ch *MoveFileChange) ProcessChange(rootRef *fileref.Ref, _ map[string]string) (err error) { +func (ch *MoveFileChange) ProcessChange(rootRef *fileref.Ref, fileIDMeta map[string]string) (err error) { + oldParentPath, oldFileName := pathutil.Split(ch.ObjectTree.GetPath()) + fields, err := common.GetPathFields(oldParentPath) + if err != nil { + return + } - fields, err := common.GetPathFields(ch.DestPath) + delRef := rootRef + for i := 0; i < len(fields); i++ { + found := false + for _, child := range delRef.Children { + if child.GetName() == fields[i] { + delRef = child.(*fileref.Ref) + delRef.HashToBeComputed = true + found = true + break + } + } + + if !found { + err = errors.New("invalid_reference_path", "Ref not found in root reference object") + return + } + } + + var removed bool + for i, child := range delRef.Children { + if child.GetName() == oldFileName { + delRef.RemoveChild(i) + removed = true + break + } + } + + if !removed { + err = errors.New("incomplete_move", "could not remove ref from source path") + return + } + + fields, err = common.GetPathFields(ch.DestPath) if err != nil { return } @@ -39,12 +79,16 @@ func (ch *MoveFileChange) ProcessChange(rootRef *fileref.Ref, _ map[string]strin } if !found { + uid := util.GetSHA1Uuid(ch.Uuid, fields[i]) + ch.Uuid = uid newRef := &fileref.Ref{ Type: fileref.DIRECTORY, AllocationID: dirRef.AllocationID, Path: path.Join("/", strings.Join(fields[:i+1], "/")), Name: fields[i], + FileID: uid.String(), } + fileIDMeta[newRef.Path] = newRef.FileID dirRef.AddChild(newRef) dirRef = newRef } @@ -63,49 +107,11 @@ func (ch *MoveFileChange) ProcessChange(rootRef *fileref.Ref, _ map[string]strin affectedRef = ch.ObjectTree.(*fileref.Ref) } - oldParentPath, oldFileName := pathutil.Split(ch.ObjectTree.GetPath()) affectedRef.Path = pathutil.Join(dirRef.GetPath(), affectedRef.Name) ch.processChildren(affectedRef) dirRef.AddChild(ch.ObjectTree) - fields, err = common.GetPathFields(oldParentPath) - if err != nil { - return - } - - delRef := rootRef - for i := 0; i < len(fields); i++ { - found := false - for _, child := range delRef.Children { - if child.GetName() == fields[i] { - delRef = child.(*fileref.Ref) - delRef.HashToBeComputed = true - found = true - break - } - } - - if !found { - err = errors.New("invalid_reference_path", "Ref not found in root reference object") - return - } - } - - var removed bool - for i, child := range delRef.Children { - if child.GetName() == oldFileName { - delRef.RemoveChild(i) - removed = true - break - } - } - - if !removed { - err = errors.New("incomplete_move", "could not remove ref from source path") - return - } - rootRef.CalculateHash() return } diff --git a/zboxcore/sdk/moveworker.go b/zboxcore/sdk/moveworker.go index 4487437b7..2380cd542 100644 --- a/zboxcore/sdk/moveworker.go +++ b/zboxcore/sdk/moveworker.go @@ -345,6 +345,7 @@ func (mo *MoveOperation) buildChange(refs []fileref.RefEntity, uid uuid.UUID) [] moveChange.NumBlocks = 0 moveChange.Operation = constants.FileOperationMove moveChange.Size = 0 + moveChange.Uuid = uid changes[idx] = moveChange } return changes