-
Notifications
You must be signed in to change notification settings - Fork 26
/
renameobject.go
95 lines (83 loc) · 2.19 KB
/
renameobject.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package allocationchange
import (
"path"
"github.com/0chain/errors"
"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/core/pathutil"
"github.com/0chain/gosdk/zboxcore/fileref"
)
type RenameFileChange struct {
change
ObjectTree fileref.RefEntity
NewName string
}
func (ch *RenameFileChange) ProcessChange(rootRef *fileref.Ref, _ map[string]string) (err error) {
parentPath := path.Dir(ch.ObjectTree.GetPath())
fields, err := common.GetPathFields(parentPath)
if err != nil {
return
}
dirRef := rootRef
for i := 0; i < len(fields); i++ {
found := false
for _, child := range dirRef.Children {
if child.GetName() == fields[i] {
dirRef = child.(*fileref.Ref)
found = true
break
}
}
if !found {
err = errors.New("invalid_reference_path", "Invalid reference path from the blobber")
return
}
}
found := false
var affectedRef *fileref.Ref
for i, child := range dirRef.Children {
if child.GetPath() == ch.ObjectTree.GetPath() {
dirRef.RemoveChild(i)
if ch.ObjectTree.GetType() == fileref.FILE {
affectedRef = &(ch.ObjectTree.(*fileref.FileRef)).Ref
} else {
affectedRef = ch.ObjectTree.(*fileref.Ref)
}
affectedRef.Path = pathutil.Join(parentPath, ch.NewName)
affectedRef.Name = ch.NewName
affectedRef.HashToBeComputed = true
dirRef.AddChild(ch.ObjectTree)
found = true
break
}
}
if !found {
err = errors.New("file_not_found", "Object to rename not found in blobber")
return
}
ch.processChildren(affectedRef)
return
}
func (ch *RenameFileChange) processChildren(curRef *fileref.Ref) {
for _, childRefEntity := range curRef.Children {
var childRef *fileref.Ref
if childRefEntity.GetType() == fileref.FILE {
childRef = &(childRefEntity.(*fileref.FileRef)).Ref
} else {
childRef = childRefEntity.(*fileref.Ref)
}
childRef.HashToBeComputed = true
childRef.Path = pathutil.Join(curRef.Path, childRef.Name)
if childRefEntity.GetType() == fileref.DIRECTORY {
ch.processChildren(childRef)
}
}
}
func (n *RenameFileChange) GetAffectedPath() []string {
if n.ObjectTree != nil {
return []string{n.ObjectTree.GetPath()}
}
return nil
}
func (n *RenameFileChange) GetSize() int64 {
return int64(0)
}