-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for deleting files/folders, renaming files/folders to sdk
- Loading branch information
1 parent
18d2350
commit d83b66f
Showing
14 changed files
with
475 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package allocationchange | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"github.com/0chain/gosdk/core/common" | ||
"github.com/0chain/gosdk/zboxcore/fileref" | ||
) | ||
|
||
type RenameFileChange struct { | ||
change | ||
ObjectTree fileref.RefEntity | ||
NewName string | ||
} | ||
|
||
func (ch *RenameFileChange) ProcessChange(rootRef *fileref.Ref) error { | ||
path, _ := filepath.Split(ch.ObjectTree.GetPath()) | ||
tSubDirs := getSubDirs(path) | ||
dirRef := rootRef | ||
treelevel := 0 | ||
for treelevel < len(tSubDirs) { | ||
found := false | ||
for _, child := range dirRef.Children { | ||
if child.GetType() == fileref.DIRECTORY && treelevel < len(tSubDirs) { | ||
if (child.(*fileref.Ref)).Name == tSubDirs[treelevel] { | ||
dirRef = child.(*fileref.Ref) | ||
found = true | ||
break | ||
} | ||
} | ||
} | ||
if found { | ||
treelevel++ | ||
} else { | ||
return common.NewError("invalid_reference_path", "Invalid reference path from the blobber") | ||
} | ||
} | ||
idx := -1 | ||
for i, child := range dirRef.Children { | ||
if child.GetPath() == ch.ObjectTree.GetPath() && child.GetHash() == ch.ObjectTree.GetHash() { | ||
idx = i | ||
break | ||
} | ||
} | ||
if idx < 0 { | ||
return common.NewError("file_not_found", "File to update not found in blobber") | ||
} | ||
dirRef.Children[idx] = ch.ObjectTree | ||
// Logger.Info("Old name: " + dirRef.Children[idx].GetName()) | ||
var affectedRef *fileref.Ref | ||
if ch.ObjectTree.GetType() == fileref.FILE { | ||
affectedRef = &(ch.ObjectTree.(*fileref.FileRef)).Ref | ||
} else { | ||
affectedRef = ch.ObjectTree.(*fileref.Ref) | ||
} | ||
|
||
path, _ = filepath.Split(affectedRef.Path) | ||
path = filepath.Clean(path) | ||
affectedRef.Name = ch.NewName | ||
affectedRef.Path = filepath.Join(path, ch.NewName) | ||
|
||
// Logger.Info("Changed name: " + dirRef.Children[idx].GetName()) | ||
|
||
ch.processChildren(affectedRef) | ||
// Logger.Info("Process hash for renaming") | ||
rootRef.CalculateHash() | ||
return nil | ||
} | ||
|
||
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.Path = filepath.Join(curRef.Path, childRef.Name) | ||
if childRefEntity.GetType() == fileref.DIRECTORY { | ||
ch.processChildren(childRef) | ||
} | ||
} | ||
} | ||
|
||
func (n *RenameFileChange) GetAffectedPath() string { | ||
if n.ObjectTree != nil { | ||
return n.ObjectTree.GetPath() | ||
} | ||
return "" | ||
} | ||
|
||
func (n *RenameFileChange) GetSize() int64 { | ||
return int64(0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.