aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2019-04-19 00:03:27 +0700
committerShulhan <ms@kilabit.info>2019-04-19 00:39:57 +0700
commit3d52d4647343dbdfceedf5f1b9418be9e7a7022f (patch)
tree8755f8fcdcf542f42769cb3c8e3850b04553dc86
parentbdb5e1e58e6ce0ad06c01647a4be805f5602df55 (diff)
downloadpakakeh.go-3d52d4647343dbdfceedf5f1b9418be9e7a7022f.tar.xz
memfs: export the NewNode function
The implication for exporting NewNode function is the parent parameter can be nil, and if its nil the SysPath and Path will be derived from FileInfo.Name(). The reason for exporting this function is to allow creating independent node from FileInfo.
-rw-r--r--lib/memfs/memfs.go2
-rw-r--r--lib/memfs/node.go21
2 files changed, 17 insertions, 6 deletions
diff --git a/lib/memfs/memfs.go b/lib/memfs/memfs.go
index b2754ad6..cb196a6f 100644
--- a/lib/memfs/memfs.go
+++ b/lib/memfs/memfs.go
@@ -302,7 +302,7 @@ func (mfs *MemFS) AddChild(parent *Node, fi os.FileInfo) (*Node, error) {
return nil, nil
}
- child, err := newNode(parent, fi, mfs.withContent)
+ child, err := NewNode(parent, fi, mfs.withContent)
if err != nil {
return nil, err
}
diff --git a/lib/memfs/node.go b/lib/memfs/node.go
index caad37c2..c4b7d1c2 100644
--- a/lib/memfs/node.go
+++ b/lib/memfs/node.go
@@ -32,20 +32,31 @@ type Node struct {
}
//
-// newNode create a new node based on file information "fi".
+// NewNode create a new node based on file information "fi".
// If withContent is true, the file content and its type will be saved in
// node as V and ContentType.
//
-func newNode(parent *Node, fi os.FileInfo, withContent bool) (node *Node, err error) {
- if parent == nil || fi == nil {
+func NewNode(parent *Node, fi os.FileInfo, withContent bool) (node *Node, err error) {
+ if fi == nil {
return nil, nil
}
- sysPath := filepath.Join(parent.SysPath, fi.Name())
+ var (
+ sysPath string
+ absPath string
+ )
+
+ if parent != nil {
+ sysPath = filepath.Join(parent.SysPath, fi.Name())
+ absPath = path.Join(parent.Path, fi.Name())
+ } else {
+ sysPath = fi.Name()
+ absPath = fi.Name()
+ }
node = &Node{
SysPath: sysPath,
- Path: path.Join(parent.Path, fi.Name()),
+ Path: absPath,
Name: fi.Name(),
ModTime: fi.ModTime(),
Mode: fi.Mode(),