github.com/0chain/gosdk@v1.17.11/zboxcore/allocationchange/updatefile.go (about) 1 package allocationchange 2 3 import ( 4 "fmt" 5 6 "github.com/0chain/errors" 7 "github.com/0chain/gosdk/core/common" 8 "github.com/0chain/gosdk/core/pathutil" 9 "github.com/0chain/gosdk/zboxcore/client" 10 "github.com/0chain/gosdk/zboxcore/fileref" 11 ) 12 13 type UpdateFileChange struct { 14 change 15 OldFile *fileref.FileRef 16 NewFile *fileref.FileRef 17 } 18 19 func (ch *UpdateFileChange) ProcessChange(rootRef *fileref.Ref, _ map[string]string) (err error) { 20 21 if ch.NewFile.ActualFileHash == "" { 22 err = fmt.Errorf("empty actual file hash field") 23 return 24 } 25 26 if ch.NewFile.ValidationRoot == "" { 27 err = fmt.Errorf("empty validation root field") 28 return 29 } 30 31 fileHashSign, err := client.Sign(ch.NewFile.ActualFileHash) 32 if err != nil { 33 return 34 } 35 36 validationRootSign, err := client.Sign(fileHashSign + ch.NewFile.ValidationRoot) 37 if err != nil { 38 return 39 } 40 41 ch.NewFile.ActualFileHashSignature = fileHashSign 42 ch.NewFile.ValidationRootSignature = validationRootSign 43 44 fields, err := common.GetPathFields(pathutil.Dir(ch.NewFile.Path)) 45 46 if err != nil { 47 return 48 } 49 50 rootRef.HashToBeComputed = true 51 dirRef := rootRef 52 for i := 0; i < len(fields); i++ { 53 found := false 54 for _, child := range dirRef.Children { 55 if child.GetName() == fields[i] { 56 var ok bool 57 dirRef, ok = child.(*fileref.Ref) 58 if !ok { 59 err = errors.New("invalid_reference_path", "Invalid reference path from the blobber") 60 return 61 } 62 dirRef.HashToBeComputed = true 63 found = true 64 break 65 } 66 } 67 68 if !found { 69 err = errors.New("invalid_reference_path", "Invalid reference path from the blobber") 70 return 71 } 72 } 73 idx := -1 74 for i, child := range dirRef.Children { 75 if child.GetType() == fileref.FILE && child.GetPath() == ch.NewFile.Path { 76 ch.OldFile = child.(*fileref.FileRef) 77 idx = i 78 break 79 } 80 } 81 if idx < 0 || ch.OldFile == nil { 82 err = errors.New("file_not_found", "File to update not found in blobber") 83 return 84 } 85 86 ch.NewFile.HashToBeComputed = true 87 ch.NewFile.FileID = ch.OldFile.FileID 88 dirRef.Children[idx] = ch.NewFile 89 return 90 } 91 92 func (n *UpdateFileChange) GetAffectedPath() []string { 93 if n.NewFile != nil { 94 return []string{n.NewFile.Path} 95 } 96 return nil 97 } 98 99 func (n *UpdateFileChange) GetSize() int64 { 100 if n.NewFile != nil && n.OldFile != nil { 101 return n.NewFile.Size - n.OldFile.Size 102 } 103 return int64(0) 104 }