Skip to content

Commit

Permalink
fix move when srcPath is same as destPath
Browse files Browse the repository at this point in the history
  • Loading branch information
Hitenjain14 committed Sep 30, 2023
1 parent 0ffbf8a commit 45e4e69
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 40 deletions.
86 changes: 46 additions & 40 deletions zboxcore/allocationchange/moveobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions zboxcore/sdk/moveworker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 45e4e69

Please sign in to comment.