-
Notifications
You must be signed in to change notification settings - Fork 26
/
deletefile.go
70 lines (57 loc) · 1.32 KB
/
deletefile.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
package allocationchange
import (
"path"
"github.com/0chain/errors"
"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/zboxcore/fileref"
)
type DeleteFileChange struct {
change
ObjectTree fileref.RefEntity
}
func (ch *DeleteFileChange) ProcessChange(rootRef *fileref.Ref, _ map[string]string) (err error) {
if ch.ObjectTree.GetPath() == "/" {
rootRef.Children = nil
return
}
parentPath := path.Dir(ch.ObjectTree.GetPath())
fields, err := common.GetPathFields(parentPath)
if err != nil {
return
}
dirRef := rootRef
for _, name := range fields {
found := false
for _, child := range dirRef.Children {
if child.GetName() == name {
dirRef = child.(*fileref.Ref)
found = true
break
}
}
if !found {
err = errors.New("invalid_reference_path", "Invalid reference path from the blobber")
return
}
}
for i, child := range dirRef.Children {
if child.GetName() == ch.ObjectTree.GetName() {
dirRef.RemoveChild(i)
return
}
}
err = errors.New("file_not_found", "File to delete not found in blobber")
return
}
func (n *DeleteFileChange) GetAffectedPath() []string {
if n.ObjectTree != nil {
return []string{n.ObjectTree.GetPath()}
}
return nil
}
func (n *DeleteFileChange) GetSize() int64 {
if n.ObjectTree != nil {
return -n.ObjectTree.GetSize()
}
return int64(0)
}