-
Notifications
You must be signed in to change notification settings - Fork 26
/
copyobject.go
110 lines (93 loc) · 2.76 KB
/
copyobject.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package allocationchange
import (
"strings"
"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 CopyFileChange struct {
change
ObjectTree fileref.RefEntity
DestPath string
Uuid uuid.UUID
}
func (ch *CopyFileChange) ProcessChange(rootRef *fileref.Ref, fileIDMeta map[string]string) (err error) {
var fields []string
fields, err = common.GetPathFields(ch.DestPath)
if err != nil {
return
}
rootRef.HashToBeComputed = true
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 {
newRef := &fileref.Ref{}
uid := util.GetSHA1Uuid(ch.Uuid, fields[i])
ch.Uuid = uid
newRef.FileID = uid.String()
newRef.Path = "/" + strings.Join(fields[:i+1], "/")
fileIDMeta[newRef.Path] = newRef.FileID
newRef.Type = fileref.DIRECTORY
newRef.AllocationID = dirRef.AllocationID
newRef.Name = fields[i]
dirRef.AddChild(newRef)
dirRef = newRef
}
dirRef.HashToBeComputed = true
}
if dirRef.GetPath() != ch.DestPath || dirRef.GetType() != fileref.DIRECTORY {
err = errors.New("file_not_found", "Object to copy not found in blobber")
return
}
var affectedRef *fileref.Ref
if ch.ObjectTree.GetType() == fileref.FILE {
affectedRef = &(ch.ObjectTree.(*fileref.FileRef)).Ref
} else {
affectedRef = ch.ObjectTree.(*fileref.Ref)
}
affectedRef.Path = pathutil.Join(dirRef.GetPath(), affectedRef.Name)
uid := util.GetSHA1Uuid(ch.Uuid, affectedRef.Name)
ch.Uuid = uid
affectedRef.FileID = uid.String()
affectedRef.HashToBeComputed = true
fileIDMeta[affectedRef.Path] = affectedRef.FileID
ch.processChildren(affectedRef, fileIDMeta)
dirRef.AddChild(ch.ObjectTree)
return
}
func (ch *CopyFileChange) processChildren(curRef *fileref.Ref, fileIDMeta map[string]string) {
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)
uid := util.GetSHA1Uuid(ch.Uuid, childRef.Name)
ch.Uuid = uid
childRef.FileID = uid.String()
fileIDMeta[childRef.Path] = childRef.FileID
if childRefEntity.GetType() == fileref.DIRECTORY {
ch.processChildren(childRef, fileIDMeta)
}
}
}
func (n *CopyFileChange) GetAffectedPath() []string {
return []string{n.DestPath}
}
func (n *CopyFileChange) GetSize() int64 {
return n.ObjectTree.GetSize()
}