aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2019-04-18 21:05:53 +0700
committerShulhan <ms@kilabit.info>2019-04-19 00:39:57 +0700
commitc9d79294435284c95dbc06601c7e11d7ef977a06 (patch)
tree474f650e7eb23addfd44fdbad86c62f9a73362a7
parent093b48d6d778ed56082ff186f98ab04dce7fd09c (diff)
downloadpakakeh.go-c9d79294435284c95dbc06601c7e11d7ef977a06.tar.xz
memfs: add method to remove child from any node
The child will be removed if and only if child is part of parent node's childs; otherwise the method will return nil.
-rw-r--r--lib/memfs/memfs.go12
-rw-r--r--lib/memfs/node.go10
2 files changed, 21 insertions, 1 deletions
diff --git a/lib/memfs/memfs.go b/lib/memfs/memfs.go
index fcae45d5..b2754ad6 100644
--- a/lib/memfs/memfs.go
+++ b/lib/memfs/memfs.go
@@ -315,6 +315,18 @@ func (mfs *MemFS) AddChild(parent *Node, fi os.FileInfo) (*Node, error) {
}
//
+// RemoveChild remove a child on parent, including its map on PathNode.
+// If child is not part if node's childrens it will return nil.
+//
+func (mfs *MemFS) RemoveChild(parent *Node, child *Node) (removed *Node) {
+ removed = parent.removeChild(child)
+ if removed != nil {
+ delete(mfs.pn.v, removed.Path)
+ }
+ return
+}
+
+//
// isIncluded will return true if the child node pass the included filter or
// excluded filter; otherwise it will return false.
//
diff --git a/lib/memfs/node.go b/lib/memfs/node.go
index 2f114203..caad37c2 100644
--- a/lib/memfs/node.go
+++ b/lib/memfs/node.go
@@ -72,7 +72,11 @@ func newNode(parent *Node, fi os.FileInfo, withContent bool) (node *Node, err er
return node, nil
}
-func (leaf *Node) removeChild(child *Node) {
+//
+// removeChild remove a children node from list. If child is not exist, it
+// will return nil.
+//
+func (leaf *Node) removeChild(child *Node) *Node {
for x := 0; x < len(leaf.Childs); x++ {
if leaf.Childs[x] != child {
continue
@@ -85,7 +89,11 @@ func (leaf *Node) removeChild(child *Node) {
child.Parent = nil
child.Childs = nil
+
+ return child
}
+
+ return nil
}
//