aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorShulhan <m.shulhan@gmail.com>2019-12-09 22:25:58 +0700
committerShulhan <m.shulhan@gmail.com>2019-12-09 22:48:52 +0700
commita7363bf547e68adc223cac856020ed53cedb26ab (patch)
treeec4fb6a77c24bdff09d6b1e42b537c36d1650a54 /lib
parent8de9e95e1ba715233eb3cad0a6625a0be342beca (diff)
downloadpakakeh.go-a7363bf547e68adc223cac856020ed53cedb26ab.tar.xz
memfs: implement os.FileInfo on Node
Diffstat (limited to 'lib')
-rw-r--r--lib/http/server.go4
-rw-r--r--lib/io/dirwatcher.go20
-rw-r--r--lib/io/watcher.go12
-rw-r--r--lib/io/watcher_test.go8
-rw-r--r--lib/memfs/generate_test.go26
-rw-r--r--lib/memfs/generate_test/gen_test.go104
-rw-r--r--lib/memfs/generate_test/memfs_generate_test.go38
-rw-r--r--lib/memfs/memfs.go18
-rw-r--r--lib/memfs/memfs_test.go18
-rw-r--r--lib/memfs/node.go90
-rw-r--r--lib/memfs/node_test.go4
-rw-r--r--lib/memfs/template.go6
12 files changed, 216 insertions, 132 deletions
diff --git a/lib/http/server.go b/lib/http/server.go
index ec6ed701..8fbd5628 100644
--- a/lib/http/server.go
+++ b/lib/http/server.go
@@ -324,7 +324,7 @@ func (srv *Server) getFSNode(reqPath string) (node *memfs.Node) {
}
}
- if node.Mode.IsDir() {
+ if node.IsDir() {
indexHTML := path.Join(reqPath, "index.html")
node, e = srv.Memfs.Get(indexHTML)
if e != nil {
@@ -366,7 +366,7 @@ func (srv *Server) handleFS(
}
if method == RequestMethodHead {
- res.Header().Set("Content-Length", strconv.FormatInt(node.Size, 10))
+ res.Header().Set("Content-Length", strconv.FormatInt(node.Size(), 10))
res.WriteHeader(http.StatusOK)
return
}
diff --git a/lib/io/dirwatcher.go b/lib/io/dirwatcher.go
index 341c3bed..db17e9fb 100644
--- a/lib/io/dirwatcher.go
+++ b/lib/io/dirwatcher.go
@@ -104,7 +104,7 @@ func (dw *DirWatcher) Stop() {
//
func (dw *DirWatcher) mapSubdirs(node *memfs.Node) {
for _, child := range node.Childs {
- if !child.Mode.IsDir() {
+ if !child.IsDir() {
_, err := NewWatcher(child.SysPath, dw.Delay, dw.Callback)
if err != nil {
log.Println(err)
@@ -123,7 +123,7 @@ func (dw *DirWatcher) mapSubdirs(node *memfs.Node) {
//
func (dw *DirWatcher) unmapSubdirs(node *memfs.Node) {
for _, child := range node.Childs {
- if !child.Mode.IsDir() {
+ if !child.IsDir() {
continue
}
@@ -164,7 +164,7 @@ func (dw *DirWatcher) onContentChange(node *memfs.Node) {
for x := 0; x < len(node.Childs); x++ {
found := false
for _, newInfo := range fis {
- if node.Childs[x].Name == newInfo.Name() {
+ if node.Childs[x].Name() == newInfo.Name() {
found = true
break
}
@@ -174,7 +174,7 @@ func (dw *DirWatcher) onContentChange(node *memfs.Node) {
fmt.Printf("lib/io: DirWatcher.onContentChange: deleted %+v\n", node.Childs[x])
}
- if node.Childs[x].Mode.IsDir() {
+ if node.Childs[x].IsDir() {
dw.unmapSubdirs(node.Childs[x])
}
@@ -187,7 +187,7 @@ func (dw *DirWatcher) onContentChange(node *memfs.Node) {
for _, newInfo := range fis {
found := false
for _, child := range node.Childs {
- if newInfo.Name() == child.Name {
+ if newInfo.Name() == child.Name() {
found = true
break
}
@@ -217,7 +217,7 @@ func (dw *DirWatcher) onContentChange(node *memfs.Node) {
dw.Callback(ns)
- if newChild.Mode.IsDir() {
+ if newChild.IsDir() {
dw.dirs[newChild.Path] = newChild
dw.mapSubdirs(newChild)
dw.onContentChange(newChild)
@@ -326,11 +326,11 @@ func (dw *DirWatcher) start() {
dw.onRootCreated()
continue
}
- if dw.root.Mode != newDirInfo.Mode() {
+ if dw.root.Mode() != newDirInfo.Mode() {
dw.onModified(dw.root, newDirInfo)
continue
}
- if dw.root.ModTime.Equal(newDirInfo.ModTime()) {
+ if dw.root.ModTime().Equal(newDirInfo.ModTime()) {
dw.processSubdirs()
continue
}
@@ -356,11 +356,11 @@ func (dw *DirWatcher) processSubdirs() {
dw.unmapSubdirs(node)
continue
}
- if node.Mode != newDirInfo.Mode() {
+ if node.Mode() != newDirInfo.Mode() {
dw.onModified(node, newDirInfo)
continue
}
- if node.ModTime.Equal(newDirInfo.ModTime()) {
+ if node.ModTime().Equal(newDirInfo.ModTime()) {
continue
}
diff --git a/lib/io/watcher.go b/lib/io/watcher.go
index 48c5a40e..169e06d8 100644
--- a/lib/io/watcher.go
+++ b/lib/io/watcher.go
@@ -54,7 +54,7 @@ func NewWatcher(path string, d time.Duration, cb WatchCallback) (w *Watcher, err
if err != nil {
return nil, fmt.Errorf("lib/io: NewWatcher: " + err.Error())
}
- if fi.Mode().IsDir() {
+ if fi.IsDir() {
return nil, fmt.Errorf("lib/io: NewWatcher: path is directory")
}
@@ -108,12 +108,12 @@ func (w *Watcher) start() {
w.node = nil
return
}
- if w.node.Mode != newInfo.Mode() {
+ if w.node.Mode() != newInfo.Mode() {
if debug.Value >= 2 {
fmt.Printf("lib/io: Watcher: mode modified %q\n", w.node.SysPath)
}
- w.node.Mode = newInfo.Mode()
+ w.node.SetMode(newInfo.Mode())
ns := &NodeState{
Node: w.node,
State: FileStateModified,
@@ -121,7 +121,7 @@ func (w *Watcher) start() {
w.cb(ns)
continue
}
- if w.node.ModTime.Equal(newInfo.ModTime()) {
+ if w.node.ModTime().Equal(newInfo.ModTime()) {
continue
}
@@ -129,8 +129,8 @@ func (w *Watcher) start() {
fmt.Printf("lib/io: Watcher: content modified %q\n", w.node.SysPath)
}
- w.node.ModTime = newInfo.ModTime()
- w.node.Size = newInfo.Size()
+ w.node.SetModTime(newInfo.ModTime())
+ w.node.SetSize(newInfo.Size())
ns := &NodeState{
Node: w.node,
diff --git a/lib/io/watcher_test.go b/lib/io/watcher_test.go
index acf9c9d5..b5c2166b 100644
--- a/lib/io/watcher_test.go
+++ b/lib/io/watcher_test.go
@@ -45,11 +45,11 @@ func TestWatcher(t *testing.T) {
if exps[x].state != ns.State {
log.Fatalf("Got state %d, want %d\n", ns.State, exps[x].state)
}
- if exps[x].mode != ns.Node.Mode {
- log.Fatalf("Got mode %d, want %d\n", ns.Node.Mode, exps[x].mode)
+ if exps[x].mode != ns.Node.Mode() {
+ log.Fatalf("Got mode %d, want %d\n", ns.Node.Mode(), exps[x].mode)
}
- if exps[x].size != ns.Node.Size {
- log.Fatalf("Got size %d, want %d\n", ns.Node.Size, exps[x].size)
+ if exps[x].size != ns.Node.Size() {
+ log.Fatalf("Got size %d, want %d\n", ns.Node.Size(), exps[x].size)
}
x++
wg.Done()
diff --git a/lib/memfs/generate_test.go b/lib/memfs/generate_test.go
new file mode 100644
index 00000000..8ec53aa2
--- /dev/null
+++ b/lib/memfs/generate_test.go
@@ -0,0 +1,26 @@
+// Copyright 2019, Shulhan <ms@kilabit.info>. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package memfs
+
+import "testing"
+
+func TestGenerate(t *testing.T) {
+ t.Skip()
+
+ mfs, err := New(nil, nil, true)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ err = mfs.Mount("testdata")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ err = mfs.GoGenerate("test", "./generate_test/gen_test.go", "")
+ if err != nil {
+ t.Fatal(err)
+ }
+}
diff --git a/lib/memfs/generate_test/gen_test.go b/lib/memfs/generate_test/gen_test.go
index 3829e7e2..44cbc543 100644
--- a/lib/memfs/generate_test/gen_test.go
+++ b/lib/memfs/generate_test/gen_test.go
@@ -10,12 +10,12 @@ func generate_() *memfs.Node {
node := &memfs.Node{
SysPath: "xxx/testdata",
Path: "/",
- Name: "/",
ContentType: "",
ContentEncoding: "",
- Mode: 2147484141,
- Size: 4096,
}
+ node.SetMode(2147484141)
+ node.SetName("/")
+ node.SetSize(4096)
return node
}
@@ -23,12 +23,12 @@ func generate_direct() *memfs.Node {
node := &memfs.Node{
SysPath: "xxx/testdata/direct",
Path: "/direct",
- Name: "direct",
ContentType: "",
ContentEncoding: "",
- Mode: 2147484141,
- Size: 0,
}
+ node.SetMode(2147484141)
+ node.SetName("direct")
+ node.SetSize(0)
return node
}
@@ -36,12 +36,12 @@ func generate_direct_add() *memfs.Node {
node := &memfs.Node{
SysPath: "xxx/testdata/direct/add",
Path: "/direct/add",
- Name: "add",
ContentType: "",
ContentEncoding: "",
- Mode: 2147484141,
- Size: 0,
}
+ node.SetMode(2147484141)
+ node.SetName("add")
+ node.SetSize(0)
return node
}
@@ -49,16 +49,16 @@ func generate_direct_add_file() *memfs.Node {
node := &memfs.Node{
SysPath: "xxx/testdata/direct/add/file",
Path: "/direct/add/file",
- Name: "file",
ContentType: "text/plain; charset=utf-8",
ContentEncoding: "",
- Mode: 420,
- Size: 22,
V: []byte{
84, 101, 115, 116, 32, 100, 105, 114, 101, 99, 116, 32, 97, 100, 100, 32,
102, 105, 108, 101, 46, 10,
},
}
+ node.SetMode(420)
+ node.SetName("file")
+ node.SetSize(22)
return node
}
@@ -66,29 +66,29 @@ func generate_direct_add_file2() *memfs.Node {
node := &memfs.Node{
SysPath: "xxx/testdata/direct/add/file2",
Path: "/direct/add/file2",
- Name: "file2",
ContentType: "text/plain; charset=utf-8",
ContentEncoding: "",
- Mode: 420,
- Size: 24,
V: []byte{
84, 101, 115, 116, 32, 100, 105, 114, 101, 99, 116, 32, 97, 100, 100, 32,
102, 105, 108, 101, 32, 50, 46, 10,
},
}
+ node.SetMode(420)
+ node.SetName("file2")
+ node.SetSize(24)
return node
}
func generate_exclude() *memfs.Node {
node := &memfs.Node{
- SysPath: "/testdata/exclude",
+ SysPath: "xxx/testdata/exclude",
Path: "/exclude",
- Name: "exclude",
ContentType: "",
ContentEncoding: "",
- Mode: 2147484141,
- Size: 0,
}
+ node.SetMode(2147484141)
+ node.SetName("exclude")
+ node.SetSize(0)
return node
}
@@ -96,15 +96,15 @@ func generate_exclude_index_css() *memfs.Node {
node := &memfs.Node{
SysPath: "xxx/testdata/exclude/index.css",
Path: "/exclude/index.css",
- Name: "index.css",
ContentType: "text/css; charset=utf-8",
ContentEncoding: "",
- Mode: 420,
- Size: 9,
V: []byte{
98, 111, 100, 121, 32, 123, 10, 125, 10,
},
}
+ node.SetMode(420)
+ node.SetName("index.css")
+ node.SetSize(9)
return node
}
@@ -112,15 +112,15 @@ func generate_exclude_index_html() *memfs.Node {
node := &memfs.Node{
SysPath: "xxx/testdata/exclude/index.html",
Path: "/exclude/index.html",
- Name: "index.html",
ContentType: "text/html; charset=utf-8",
ContentEncoding: "",
- Mode: 420,
- Size: 14,
V: []byte{
60, 104, 116, 109, 108, 62, 60, 47, 104, 116, 109, 108, 62, 10,
},
}
+ node.SetMode(420)
+ node.SetName("index.html")
+ node.SetSize(14)
return node
}
@@ -128,15 +128,15 @@ func generate_exclude_index_js() *memfs.Node {
node := &memfs.Node{
SysPath: "xxx/testdata/exclude/index.js",
Path: "/exclude/index.js",
- Name: "index.js",
ContentType: "application/javascript",
ContentEncoding: "",
- Mode: 420,
- Size: 16,
V: []byte{
102, 117, 110, 99, 116, 105, 111, 110, 32, 88, 40, 41, 32, 123, 125, 10,
},
}
+ node.SetMode(420)
+ node.SetName("index.js")
+ node.SetSize(16)
return node
}
@@ -144,12 +144,12 @@ func generate_include() *memfs.Node {
node := &memfs.Node{
SysPath: "xxx/testdata/include",
Path: "/include",
- Name: "include",
ContentType: "",
ContentEncoding: "",
- Mode: 2147484141,
- Size: 0,
}
+ node.SetMode(2147484141)
+ node.SetName("include")
+ node.SetSize(0)
return node
}
@@ -157,15 +157,15 @@ func generate_include_index_css() *memfs.Node {
node := &memfs.Node{
SysPath: "xxx/testdata/include/index.css",
Path: "/include/index.css",
- Name: "index.css",
ContentType: "text/css; charset=utf-8",
ContentEncoding: "",
- Mode: 420,
- Size: 9,
V: []byte{
98, 111, 100, 121, 32, 123, 10, 125, 10,
},
}
+ node.SetMode(420)
+ node.SetName("index.css")
+ node.SetSize(9)
return node
}
@@ -173,15 +173,15 @@ func generate_include_index_html() *memfs.Node {
node := &memfs.Node{
SysPath: "xxx/testdata/include/index.html",
Path: "/include/index.html",
- Name: "index.html",
ContentType: "text/html; charset=utf-8",
ContentEncoding: "",
- Mode: 420,
- Size: 14,
V: []byte{
60, 104, 116, 109, 108, 62, 60, 47, 104, 116, 109, 108, 62, 10,
},
}
+ node.SetMode(420)
+ node.SetName("index.html")
+ node.SetSize(14)
return node
}
@@ -189,15 +189,15 @@ func generate_include_index_js() *memfs.Node {
node := &memfs.Node{
SysPath: "xxx/testdata/include/index.js",
Path: "/include/index.js",
- Name: "index.js",
ContentType: "application/javascript",
ContentEncoding: "",
- Mode: 420,
- Size: 16,
V: []byte{
102, 117, 110, 99, 116, 105, 111, 110, 32, 88, 40, 41, 32, 123, 125, 10,
},
}
+ node.SetMode(420)
+ node.SetName("index.js")
+ node.SetSize(16)
return node
}
@@ -205,15 +205,15 @@ func generate_index_css() *memfs.Node {
node := &memfs.Node{
SysPath: "xxx/testdata/index.css",
Path: "/index.css",
- Name: "index.css",
ContentType: "text/css; charset=utf-8",
ContentEncoding: "",
- Mode: 420,
- Size: 9,
V: []byte{
98, 111, 100, 121, 32, 123, 10, 125, 10,
},
}
+ node.SetMode(420)
+ node.SetName("index.css")
+ node.SetSize(9)
return node
}
@@ -221,15 +221,15 @@ func generate_index_html() *memfs.Node {
node := &memfs.Node{
SysPath: "xxx/testdata/index.html",
Path: "/index.html",
- Name: "index.html",
ContentType: "text/html; charset=utf-8",
ContentEncoding: "",
- Mode: 420,
- Size: 14,
V: []byte{
60, 104, 116, 109, 108, 62, 60, 47, 104, 116, 109, 108, 62, 10,
},
}
+ node.SetMode(420)
+ node.SetName("index.html")
+ node.SetSize(14)
return node
}
@@ -237,15 +237,15 @@ func generate_index_js() *memfs.Node {
node := &memfs.Node{
SysPath: "xxx/testdata/index.js",
Path: "/index.js",
- Name: "index.js",
ContentType: "application/javascript",
ContentEncoding: "",
- Mode: 420,
- Size: 16,
V: []byte{
102, 117, 110, 99, 116, 105, 111, 110, 32, 88, 40, 41, 32, 123, 125, 10,
},
}
+ node.SetMode(420)
+ node.SetName("index.js")
+ node.SetSize(16)
return node
}
@@ -253,16 +253,16 @@ func generate_plain() *memfs.Node {
node := &memfs.Node{
SysPath: "xxx/testdata/plain",
Path: "/plain",
- Name: "plain",
ContentType: "text/plain; charset=utf-8",
ContentEncoding: "",
- Mode: 420,
- Size: 22,
V: []byte{
84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 112, 108, 97, 105, 110, 32,
116, 101, 120, 116, 46, 10,
},
}
+ node.SetMode(420)
+ node.SetName("plain")
+ node.SetSize(22)
return node
}
diff --git a/lib/memfs/generate_test/memfs_generate_test.go b/lib/memfs/generate_test/memfs_generate_test.go
index 213d5a0d..020d0cfa 100644
--- a/lib/memfs/generate_test/memfs_generate_test.go
+++ b/lib/memfs/generate_test/memfs_generate_test.go
@@ -22,6 +22,25 @@ func TestGeneratePathNode(t *testing.T) {
wd = strings.TrimSuffix(wd, "generate_test")
+ expRoot := &memfs.Node{
+ SysPath: filepath.Join(wd, "testdata"),
+ Path: "/",
+ }
+ expRoot.SetMode(2147484141)
+ expRoot.SetName("/")
+ expRoot.SetSize(4096)
+
+ expExcludeIndexHTML := &memfs.Node{
+ SysPath: filepath.Join(wd, "testdata", "exclude", "index.html"),
+ Path: "/exclude/index.html",
+ ContentType: "text/html; charset=utf-8",
+ V: []byte("<html></html>\n"),
+ }
+
+ expExcludeIndexHTML.SetMode(420) //nolint: staticcheck
+ expExcludeIndexHTML.SetName("index.html")
+ expExcludeIndexHTML.SetSize(14)
+
cases := []struct {
path string
exp *memfs.Node
@@ -31,25 +50,10 @@ func TestGeneratePathNode(t *testing.T) {
expError: "file does not exist",
}, {
path: "/",
- exp: &memfs.Node{
- SysPath: filepath.Join(wd, "testdata"),
- Path: "/",
- Name: "/",
- ContentType: "",
- Mode: 2147484141,
- Size: 4096,
- },
+ exp: expRoot,
}, {
path: "/exclude/index.html",
- exp: &memfs.Node{
- SysPath: filepath.Join(wd, "testdata", "exclude", "index.html"),
- Path: "/exclude/index.html",
- Name: "index.html",
- ContentType: "text/html; charset=utf-8",
- Mode: 420,
- Size: 14,
- V: []byte("<html></html>\n"),
- },
+ exp: expExcludeIndexHTML,
}}
mfs, err := memfs.New(nil, nil, true)
diff --git a/lib/memfs/memfs.go b/lib/memfs/memfs.go
index 72384d96..e6f38629 100644
--- a/lib/memfs/memfs.go
+++ b/lib/memfs/memfs.go
@@ -181,7 +181,7 @@ func (mfs *MemFS) ContentEncode(encoding string) (err error) {
}
for _, node := range mfs.pn.v {
- if node.Mode.IsDir() || len(node.V) == 0 {
+ if node.mode.IsDir() || len(node.V) == 0 {
continue
}
@@ -199,7 +199,7 @@ func (mfs *MemFS) ContentEncode(encoding string) (err error) {
copy(node.V, buf.Bytes())
node.ContentEncoding = encoding
- node.Size = int64(len(node.V))
+ node.size = int64(len(node.V))
buf.Reset()
@@ -357,10 +357,10 @@ func (mfs *MemFS) createRoot(dir string, f *os.File) error {
mfs.root = &Node{
SysPath: dir,
Path: "/",
- Name: "/",
- ModTime: fi.ModTime(),
- Mode: fi.Mode(),
- Size: fi.Size(),
+ name: "/",
+ modTime: fi.ModTime(),
+ mode: fi.Mode(),
+ size: fi.Size(),
V: nil,
Parent: nil,
}
@@ -384,7 +384,7 @@ func (mfs *MemFS) scanDir(parent *Node, f *os.File) error {
if leaf == nil {
continue
}
- if !leaf.Mode.IsDir() {
+ if !leaf.mode.IsDir() {
continue
}
@@ -483,7 +483,7 @@ func (mfs *MemFS) isIncluded(sysPath string, mode os.FileMode) bool {
//
func (mfs *MemFS) pruneEmptyDirs() {
for k, node := range mfs.pn.v {
- if !node.Mode.IsDir() {
+ if !node.mode.IsDir() {
continue
}
if len(node.Childs) != 0 {
@@ -550,7 +550,7 @@ func (mfs *MemFS) Search(words []string, snippetLen int) (results []SearchResult
}
for _, node := range mfs.pn.v {
- if node.Mode.IsDir() {
+ if node.mode.IsDir() {
continue
}
diff --git a/lib/memfs/memfs_test.go b/lib/memfs/memfs_test.go
index a289ef32..9a1a0657 100644
--- a/lib/memfs/memfs_test.go
+++ b/lib/memfs/memfs_test.go
@@ -33,9 +33,9 @@ func TestAddFile(t *testing.T) {
exp: &Node{
SysPath: "testdata/direct/add/file",
Path: "testdata/direct/add/file",
- Name: "file",
+ name: "file",
ContentType: "text/plain; charset=utf-8",
- Size: 22,
+ size: 22,
V: []byte("Test direct add file.\n"),
},
}, {
@@ -44,9 +44,9 @@ func TestAddFile(t *testing.T) {
exp: &Node{
SysPath: "testdata/direct/add/file2",
Path: "testdata/direct/add/file2",
- Name: "file2",
+ name: "file2",
ContentType: "text/plain; charset=utf-8",
- Size: 24,
+ size: 24,
V: []byte("Test direct add file 2.\n"),
},
}}
@@ -71,8 +71,8 @@ func TestAddFile(t *testing.T) {
}
if got != nil {
- got.ModTime = time.Time{}
- got.Mode = 0
+ got.modTime = time.Time{}
+ got.mode = 0
got.Parent = nil
got.Childs = nil
}
@@ -86,8 +86,8 @@ func TestAddFile(t *testing.T) {
}
if got != nil {
- got.ModTime = time.Time{}
- got.Mode = 0
+ got.modTime = time.Time{}
+ got.mode = 0
got.Parent = nil
got.Childs = nil
}
@@ -175,7 +175,7 @@ func TestGet(t *testing.T) {
continue
}
- if got.Size <= MaxFileSize {
+ if got.size <= MaxFileSize {
test.Assert(t, "node.V", c.expV, got.V, true)
}
diff --git a/lib/memfs/node.go b/lib/memfs/node.go
index ff3925c2..d7a600ce 100644
--- a/lib/memfs/node.go
+++ b/lib/memfs/node.go
@@ -28,14 +28,16 @@ var (
// Node represent a single file.
//
type Node struct {
+ os.FileInfo
+
SysPath string // The original file path in system.
Path string // Absolute file path in memory.
- Name string // File name.
+ name string // File name.
ContentType string // File type per MIME, for example "application/json".
ContentEncoding string // File type encoding, for example "gzip".
- ModTime time.Time // ModTime contains file modification time.
- Mode os.FileMode // File mode.
- Size int64 // Size of file.
+ modTime time.Time // ModTime contains file modification time.
+ mode os.FileMode // File mode.
+ size int64 // Size of file.
V []byte // Content of file.
Parent *Node // Pointer to parent directory.
Childs []*Node // List of files in directory.
@@ -70,17 +72,17 @@ func NewNode(parent *Node, fi os.FileInfo, withContent bool) (node *Node, err er
node = &Node{
SysPath: sysPath,
Path: absPath,
- Name: fi.Name(),
- ModTime: fi.ModTime(),
- Mode: fi.Mode(),
- Size: fi.Size(),
+ name: fi.Name(),
+ modTime: fi.ModTime(),
+ mode: fi.Mode(),
+ size: fi.Size(),
V: nil,
Parent: parent,
Childs: make([]*Node, 0),
}
- if node.Mode.IsDir() || !withContent {
- node.Size = 0
+ if node.mode.IsDir() || !withContent {
+ node.size = 0
return node, nil
}
@@ -142,6 +144,22 @@ func (leaf *Node) Decode() ([]byte, error) {
return leaf.plainv, nil
}
+func (leaf *Node) IsDir() bool {
+ return leaf.mode.IsDir()
+}
+
+func (leaf *Node) ModTime() time.Time {
+ return leaf.modTime
+}
+
+func (leaf *Node) Mode() os.FileMode {
+ return leaf.mode
+}
+
+func (leaf *Node) Name() string {
+ return leaf.name
+}
+
//
// Read the content of node into p.
//
@@ -151,7 +169,7 @@ func (leaf *Node) Read(p []byte) (n int, err error) {
if len(p) == 0 {
return 0, nil
}
- if leaf.off >= leaf.Size {
+ if leaf.off >= leaf.size {
return 0, io.EOF
}
n = copy(p, leaf.V[leaf.off:])
@@ -172,7 +190,7 @@ func (leaf *Node) Seek(offset int64, whence int) (int64, error) {
case io.SeekCurrent:
offset += leaf.off
case io.SeekEnd:
- offset += leaf.Size
+ offset += leaf.size
default:
return 0, errWhence
}
@@ -184,6 +202,42 @@ func (leaf *Node) Seek(offset int64, whence int) (int64, error) {
}
//
+// SetModTime set the file modification time.
+//
+func (leaf *Node) SetModTime(modTime time.Time) {
+ leaf.modTime = modTime
+}
+
+//
+// SetMode set the mode of file.
+//
+func (leaf *Node) SetMode(mode os.FileMode) {
+ leaf.mode = mode
+}
+
+//
+// SetName set the name of file.
+//
+func (leaf *Node) SetName(name string) {
+ leaf.name = name
+}
+
+//
+// SetSize set the file size.
+//
+func (leaf *Node) SetSize(size int64) {
+ leaf.size = size
+}
+
+func (leaf *Node) Size() int64 {
+ return leaf.size
+}
+
+func (leaf *Node) Sys() interface{} {
+ return leaf
+}
+
+//
// removeChild remove a children node from list. If child is not exist, it
// will return nil.
//
@@ -226,13 +280,13 @@ func (leaf *Node) update(newInfo os.FileInfo, withContent bool) (err error) {
}
}
- if leaf.Mode != newInfo.Mode() {
- leaf.Mode = newInfo.Mode()
+ if leaf.mode != newInfo.Mode() {
+ leaf.mode = newInfo.Mode()
return nil
}
- leaf.ModTime = newInfo.ModTime()
- leaf.Size = newInfo.Size()
+ leaf.modTime = newInfo.ModTime()
+ leaf.size = newInfo.Size()
if !withContent || newInfo.IsDir() {
return nil
@@ -245,7 +299,7 @@ func (leaf *Node) update(newInfo os.FileInfo, withContent bool) (err error) {
// updateContent read the content of file.
//
func (leaf *Node) updateContent() (err error) {
- if leaf.Size > MaxFileSize {
+ if leaf.size > MaxFileSize {
return nil
}
@@ -258,7 +312,7 @@ func (leaf *Node) updateContent() (err error) {
}
func (leaf *Node) updateContentType() error {
- leaf.ContentType = mime.TypeByExtension(path.Ext(leaf.Name))
+ leaf.ContentType = mime.TypeByExtension(path.Ext(leaf.name))
if len(leaf.ContentType) > 0 {
return nil
}
diff --git a/lib/memfs/node_test.go b/lib/memfs/node_test.go
index 22acec80..e2eb89fd 100644
--- a/lib/memfs/node_test.go
+++ b/lib/memfs/node_test.go
@@ -13,7 +13,7 @@ import (
func TestNode_Read(t *testing.T) {
node := &Node{
- Size: 3,
+ size: 3,
V: []byte("123"),
}
@@ -63,7 +63,7 @@ func TestNode_Read(t *testing.T) {
func TestNode_Seek(t *testing.T) {
node := &Node{
- Size: 3,
+ size: 3,
V: []byte("123"),
}
diff --git a/lib/memfs/template.go b/lib/memfs/template.go
index b96fa48d..fa4c46d9 100644
--- a/lib/memfs/template.go
+++ b/lib/memfs/template.go
@@ -50,17 +50,17 @@ func generate{{ funcname .Path | printf "%s"}}() *memfs.Node {
node := &memfs.Node{
SysPath: "{{.SysPath}}",
Path: "{{.Path}}",
- Name: "{{.Name}}",
ContentType: "{{.ContentType}}",
ContentEncoding: "{{.ContentEncoding}}",
- Mode: {{printf "%d" .Mode}},
- Size: {{.Size}},
{{- if .V }}
V: []byte{
{{range $x, $c := .V}}{{ if maxline $x }}{{ printf "\n\t\t\t" }}{{else if $x}} {{end}}{{ printf "%d," $c }}{{end}}
},
{{- end }}
}
+ node.SetMode({{printf "%d" .Mode}})
+ node.SetName("{{.Name}}")
+ node.SetSize({{.Size}})
return node
}
{{end}}