diff options
| author | Shulhan <m.shulhan@gmail.com> | 2020-05-06 21:08:35 +0700 |
|---|---|---|
| committer | Shulhan <m.shulhan@gmail.com> | 2020-05-07 12:47:58 +0700 |
| commit | d13c3783ccbebe18d42348c4df14c212bb1923e9 (patch) | |
| tree | 2697d2b3d546bbb02346ecbd2639dde7fa18d175 | |
| parent | 52c75cf627fa5acf5ad63152382a22e7d7e59481 (diff) | |
| download | pakakeh.go-d13c3783ccbebe18d42348c4df14c212bb1923e9.tar.xz | |
memfs: simplify the API, add directory parameter to New
This changes add parameter directory to be mounted in New(), so user
did not need to call Mount manually
This cause the Mount method and its sibling, Unmount and IsMounted
become unneeded, so we remove them.
| -rw-r--r-- | lib/http/server.go | 8 | ||||
| -rw-r--r-- | lib/io/dirwatcher.go | 17 | ||||
| -rw-r--r-- | lib/memfs/generate_test.go | 9 | ||||
| -rw-r--r-- | lib/memfs/generate_test/gen_test.go | 87 | ||||
| -rw-r--r-- | lib/memfs/generate_test/memfs_generate_test.go | 8 | ||||
| -rw-r--r-- | lib/memfs/memfs.go | 46 | ||||
| -rw-r--r-- | lib/memfs/memfs_example_test.go | 14 | ||||
| -rw-r--r-- | lib/memfs/memfs_test.go | 33 | ||||
| -rw-r--r-- | lib/memfs/node_test.go | 7 | ||||
| -rw-r--r-- | lib/memfs/template.go | 2 |
10 files changed, 77 insertions, 154 deletions
diff --git a/lib/http/server.go b/lib/http/server.go index 5a265492..3f6d92ca 100644 --- a/lib/http/server.go +++ b/lib/http/server.go @@ -78,12 +78,8 @@ func NewServer(opts *ServerOptions) (srv *Server, err error) { memfs.Development = opts.Development if len(opts.Root) > 0 { - srv.Memfs, err = memfs.New(opts.Includes, opts.Excludes, true) - if err != nil { - return nil, err - } - - err = srv.Memfs.Mount(opts.Root) + srv.Memfs, err = memfs.New(opts.Root, opts.Includes, + opts.Excludes, true) if err != nil { return nil, err } diff --git a/lib/io/dirwatcher.go b/lib/io/dirwatcher.go index db17e9fb..badcac33 100644 --- a/lib/io/dirwatcher.go +++ b/lib/io/dirwatcher.go @@ -70,12 +70,7 @@ func (dw *DirWatcher) Start() (err error) { return fmt.Errorf("lib/io: NewDirWatcher: %q is not a directory", dw.Path) } - dw.fs, err = memfs.New(dw.Includes, dw.Excludes, false) - if err != nil { - return fmt.Errorf("lib/io: NewDirWatcher: " + err.Error()) - } - - err = dw.fs.Mount(dw.Path) + dw.fs, err = memfs.New(dw.Path, dw.Includes, dw.Excludes, false) if err != nil { return fmt.Errorf("lib/io: NewDirWatcher: " + err.Error()) } @@ -239,7 +234,9 @@ func (dw *DirWatcher) onContentChange(node *memfs.Node) { // recursively. // func (dw *DirWatcher) onRootCreated() { - err := dw.fs.Mount(dw.Path) + var err error + + dw.fs, err = memfs.New(dw.Path, dw.Includes, dw.Excludes, false) if err != nil { log.Println("lib/io: DirWatcher.onRootCreated: " + err.Error()) return @@ -277,7 +274,7 @@ func (dw *DirWatcher) onRootDeleted() { State: FileStateDeleted, } - dw.fs.Unmount() + dw.fs = nil dw.root = nil dw.dirs = nil @@ -317,12 +314,12 @@ func (dw *DirWatcher) start() { log.Println("lib/io: DirWatcher: " + err.Error()) continue } - if dw.fs.IsMounted() { + if dw.fs != nil { dw.onRootDeleted() } continue } - if !dw.fs.IsMounted() { + if dw.fs == nil { dw.onRootCreated() continue } diff --git a/lib/memfs/generate_test.go b/lib/memfs/generate_test.go index 8ec53aa2..9a476fd8 100644 --- a/lib/memfs/generate_test.go +++ b/lib/memfs/generate_test.go @@ -7,14 +7,7 @@ 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") + mfs, err := New("testdata", nil, nil, true) 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 44cbc543..1f9b5d27 100644 --- a/lib/memfs/generate_test/gen_test.go +++ b/lib/memfs/generate_test/gen_test.go @@ -8,20 +8,20 @@ import ( func generate_() *memfs.Node { node := &memfs.Node{ - SysPath: "xxx/testdata", + SysPath: "testdata", Path: "/", ContentType: "", ContentEncoding: "", } node.SetMode(2147484141) node.SetName("/") - node.SetSize(4096) + node.SetSize(0) return node } func generate_direct() *memfs.Node { node := &memfs.Node{ - SysPath: "xxx/testdata/direct", + SysPath: "testdata/direct", Path: "/direct", ContentType: "", ContentEncoding: "", @@ -34,7 +34,7 @@ func generate_direct() *memfs.Node { func generate_direct_add() *memfs.Node { node := &memfs.Node{ - SysPath: "xxx/testdata/direct/add", + SysPath: "testdata/direct/add", Path: "/direct/add", ContentType: "", ContentEncoding: "", @@ -47,14 +47,11 @@ func generate_direct_add() *memfs.Node { func generate_direct_add_file() *memfs.Node { node := &memfs.Node{ - SysPath: "xxx/testdata/direct/add/file", + SysPath: "testdata/direct/add/file", Path: "/direct/add/file", ContentType: "text/plain; charset=utf-8", ContentEncoding: "", - V: []byte{ - 84, 101, 115, 116, 32, 100, 105, 114, 101, 99, 116, 32, 97, 100, 100, 32, - 102, 105, 108, 101, 46, 10, - }, + V: []byte("\x54\x65\x73\x74\x20\x64\x69\x72\x65\x63\x74\x20\x61\x64\x64\x20\x66\x69\x6C\x65\x2E\x0A"), } node.SetMode(420) node.SetName("file") @@ -64,14 +61,11 @@ func generate_direct_add_file() *memfs.Node { func generate_direct_add_file2() *memfs.Node { node := &memfs.Node{ - SysPath: "xxx/testdata/direct/add/file2", + SysPath: "testdata/direct/add/file2", Path: "/direct/add/file2", ContentType: "text/plain; charset=utf-8", ContentEncoding: "", - 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, - }, + V: []byte("\x54\x65\x73\x74\x20\x64\x69\x72\x65\x63\x74\x20\x61\x64\x64\x20\x66\x69\x6C\x65\x20\x32\x2E\x0A"), } node.SetMode(420) node.SetName("file2") @@ -81,7 +75,7 @@ func generate_direct_add_file2() *memfs.Node { func generate_exclude() *memfs.Node { node := &memfs.Node{ - SysPath: "xxx/testdata/exclude", + SysPath: "testdata/exclude", Path: "/exclude", ContentType: "", ContentEncoding: "", @@ -94,13 +88,11 @@ func generate_exclude() *memfs.Node { func generate_exclude_index_css() *memfs.Node { node := &memfs.Node{ - SysPath: "xxx/testdata/exclude/index.css", + SysPath: "testdata/exclude/index.css", Path: "/exclude/index.css", ContentType: "text/css; charset=utf-8", ContentEncoding: "", - V: []byte{ - 98, 111, 100, 121, 32, 123, 10, 125, 10, - }, + V: []byte("\x62\x6F\x64\x79\x20\x7B\x0A\x7D\x0A"), } node.SetMode(420) node.SetName("index.css") @@ -110,13 +102,11 @@ func generate_exclude_index_css() *memfs.Node { func generate_exclude_index_html() *memfs.Node { node := &memfs.Node{ - SysPath: "xxx/testdata/exclude/index.html", + SysPath: "testdata/exclude/index.html", Path: "/exclude/index.html", ContentType: "text/html; charset=utf-8", ContentEncoding: "", - V: []byte{ - 60, 104, 116, 109, 108, 62, 60, 47, 104, 116, 109, 108, 62, 10, - }, + V: []byte("\x3C\x68\x74\x6D\x6C\x3E\x3C\x2F\x68\x74\x6D\x6C\x3E\x0A"), } node.SetMode(420) node.SetName("index.html") @@ -126,13 +116,11 @@ func generate_exclude_index_html() *memfs.Node { func generate_exclude_index_js() *memfs.Node { node := &memfs.Node{ - SysPath: "xxx/testdata/exclude/index.js", + SysPath: "testdata/exclude/index.js", Path: "/exclude/index.js", ContentType: "application/javascript", ContentEncoding: "", - V: []byte{ - 102, 117, 110, 99, 116, 105, 111, 110, 32, 88, 40, 41, 32, 123, 125, 10, - }, + V: []byte("\x66\x75\x6E\x63\x74\x69\x6F\x6E\x20\x58\x28\x29\x20\x7B\x7D\x0A"), } node.SetMode(420) node.SetName("index.js") @@ -142,7 +130,7 @@ func generate_exclude_index_js() *memfs.Node { func generate_include() *memfs.Node { node := &memfs.Node{ - SysPath: "xxx/testdata/include", + SysPath: "testdata/include", Path: "/include", ContentType: "", ContentEncoding: "", @@ -155,13 +143,11 @@ func generate_include() *memfs.Node { func generate_include_index_css() *memfs.Node { node := &memfs.Node{ - SysPath: "xxx/testdata/include/index.css", + SysPath: "testdata/include/index.css", Path: "/include/index.css", ContentType: "text/css; charset=utf-8", ContentEncoding: "", - V: []byte{ - 98, 111, 100, 121, 32, 123, 10, 125, 10, - }, + V: []byte("\x62\x6F\x64\x79\x20\x7B\x0A\x7D\x0A"), } node.SetMode(420) node.SetName("index.css") @@ -171,13 +157,11 @@ func generate_include_index_css() *memfs.Node { func generate_include_index_html() *memfs.Node { node := &memfs.Node{ - SysPath: "xxx/testdata/include/index.html", + SysPath: "testdata/include/index.html", Path: "/include/index.html", ContentType: "text/html; charset=utf-8", ContentEncoding: "", - V: []byte{ - 60, 104, 116, 109, 108, 62, 60, 47, 104, 116, 109, 108, 62, 10, - }, + V: []byte("\x3C\x68\x74\x6D\x6C\x3E\x3C\x2F\x68\x74\x6D\x6C\x3E\x0A"), } node.SetMode(420) node.SetName("index.html") @@ -187,13 +171,11 @@ func generate_include_index_html() *memfs.Node { func generate_include_index_js() *memfs.Node { node := &memfs.Node{ - SysPath: "xxx/testdata/include/index.js", + SysPath: "testdata/include/index.js", Path: "/include/index.js", ContentType: "application/javascript", ContentEncoding: "", - V: []byte{ - 102, 117, 110, 99, 116, 105, 111, 110, 32, 88, 40, 41, 32, 123, 125, 10, - }, + V: []byte("\x66\x75\x6E\x63\x74\x69\x6F\x6E\x20\x58\x28\x29\x20\x7B\x7D\x0A"), } node.SetMode(420) node.SetName("index.js") @@ -203,13 +185,11 @@ func generate_include_index_js() *memfs.Node { func generate_index_css() *memfs.Node { node := &memfs.Node{ - SysPath: "xxx/testdata/index.css", + SysPath: "testdata/index.css", Path: "/index.css", ContentType: "text/css; charset=utf-8", ContentEncoding: "", - V: []byte{ - 98, 111, 100, 121, 32, 123, 10, 125, 10, - }, + V: []byte("\x62\x6F\x64\x79\x20\x7B\x0A\x7D\x0A"), } node.SetMode(420) node.SetName("index.css") @@ -219,13 +199,11 @@ func generate_index_css() *memfs.Node { func generate_index_html() *memfs.Node { node := &memfs.Node{ - SysPath: "xxx/testdata/index.html", + SysPath: "testdata/index.html", Path: "/index.html", ContentType: "text/html; charset=utf-8", ContentEncoding: "", - V: []byte{ - 60, 104, 116, 109, 108, 62, 60, 47, 104, 116, 109, 108, 62, 10, - }, + V: []byte("\x3C\x68\x74\x6D\x6C\x3E\x3C\x2F\x68\x74\x6D\x6C\x3E\x0A"), } node.SetMode(420) node.SetName("index.html") @@ -235,13 +213,11 @@ func generate_index_html() *memfs.Node { func generate_index_js() *memfs.Node { node := &memfs.Node{ - SysPath: "xxx/testdata/index.js", + SysPath: "testdata/index.js", Path: "/index.js", ContentType: "application/javascript", ContentEncoding: "", - V: []byte{ - 102, 117, 110, 99, 116, 105, 111, 110, 32, 88, 40, 41, 32, 123, 125, 10, - }, + V: []byte("\x66\x75\x6E\x63\x74\x69\x6F\x6E\x20\x58\x28\x29\x20\x7B\x7D\x0A"), } node.SetMode(420) node.SetName("index.js") @@ -251,14 +227,11 @@ func generate_index_js() *memfs.Node { func generate_plain() *memfs.Node { node := &memfs.Node{ - SysPath: "xxx/testdata/plain", + SysPath: "testdata/plain", Path: "/plain", ContentType: "text/plain; charset=utf-8", ContentEncoding: "", - V: []byte{ - 84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 112, 108, 97, 105, 110, 32, - 116, 101, 120, 116, 46, 10, - }, + V: []byte("\x54\x68\x69\x73\x20\x69\x73\x20\x61\x20\x70\x6C\x61\x69\x6E\x20\x74\x65\x78\x74\x2E\x0A"), } node.SetMode(420) node.SetName("plain") diff --git a/lib/memfs/generate_test/memfs_generate_test.go b/lib/memfs/generate_test/memfs_generate_test.go index 020d0cfa..17dc249e 100644 --- a/lib/memfs/generate_test/memfs_generate_test.go +++ b/lib/memfs/generate_test/memfs_generate_test.go @@ -23,15 +23,15 @@ func TestGeneratePathNode(t *testing.T) { wd = strings.TrimSuffix(wd, "generate_test") expRoot := &memfs.Node{ - SysPath: filepath.Join(wd, "testdata"), + SysPath: "testdata", Path: "/", } expRoot.SetMode(2147484141) expRoot.SetName("/") - expRoot.SetSize(4096) + expRoot.SetSize(0) expExcludeIndexHTML := &memfs.Node{ - SysPath: filepath.Join(wd, "testdata", "exclude", "index.html"), + SysPath: filepath.Join("testdata", "exclude", "index.html"), Path: "/exclude/index.html", ContentType: "text/html; charset=utf-8", V: []byte("<html></html>\n"), @@ -56,7 +56,7 @@ func TestGeneratePathNode(t *testing.T) { exp: expExcludeIndexHTML, }} - mfs, err := memfs.New(nil, nil, true) + mfs, err := memfs.New("", nil, nil, true) if err != nil { t.Fatal(err) } diff --git a/lib/memfs/memfs.go b/lib/memfs/memfs.go index 9f595dee..c7f00787 100644 --- a/lib/memfs/memfs.go +++ b/lib/memfs/memfs.go @@ -7,7 +7,6 @@ package memfs import ( "bytes" "compress/gzip" - "errors" "fmt" "io" "log" @@ -60,8 +59,8 @@ type MemFS struct { } // -// New create and initialize new memory file system using list of regular -// expresssion for including or excluding files. +// New create and initialize new memory file system from directory dir using +// list of regular expresssion for including or excluding files. // // The includes and excludes pattern applied to path of file in file system, // not to the path in memory. @@ -74,7 +73,9 @@ type MemFS struct { // excludes does not have any effect, since the content of path and nodes will // be overwritten by GeneratedPathNode. // -func New(includes, excludes []string, withContent bool) (mfs *MemFS, err error) { +func New(dir string, includes, excludes []string, withContent bool) ( + mfs *MemFS, err error, +) { if GeneratedPathNode != nil { if !Development { mfs = &MemFS{ @@ -108,6 +109,11 @@ func New(includes, excludes []string, withContent bool) (mfs *MemFS, err error) mfs.excRE = append(mfs.excRE, re) } + err = mfs.mount(dir) + if err != nil { + return nil, fmt.Errorf("memfs.New: %w", err) + } + return mfs, nil } @@ -274,22 +280,14 @@ func (mfs *MemFS) ListNames() (paths []string) { } // -// IsMounted will return true if a directory in file system has been mounted -// to memory; otherwise it will return false. -// -func (mfs *MemFS) IsMounted() bool { - return mfs.root != nil -} - -// -// Mount the directory recursively into the memory as root directory. +// mount the directory recursively into the memory as root directory. // For example, if we mount directory "/tmp" and "/tmp" contains file "a", to // access file "a" we call Get("/a"), not Get("/tmp/a"). // -// Mount does not have any effect if current directory contains ".go" +// mount does not have any effect if current directory contains ".go" // file generated from GoGenerate(). // -func (mfs *MemFS) Mount(dir string) error { +func (mfs *MemFS) mount(dir string) error { if len(dir) == 0 { return nil } @@ -308,18 +306,18 @@ func (mfs *MemFS) Mount(dir string) error { f, err := os.Open(dir) if err != nil { - return fmt.Errorf("memfs.Mount: %w", err) + return fmt.Errorf("mount: %w", err) } err = mfs.createRoot(dir, f) if err != nil { - return fmt.Errorf("memfs.Mount: %w", err) + return fmt.Errorf("mount: %w", err) } err = mfs.scanDir(mfs.root, f) _ = f.Close() if err != nil { - return fmt.Errorf("memfs.Mount: %w", err) + return fmt.Errorf("mount: %w", err) } if mfs.withContent { @@ -330,14 +328,6 @@ func (mfs *MemFS) Mount(dir string) error { } // -// Unmount the root directory from memory. -// -func (mfs *MemFS) Unmount() { - mfs.root = nil - mfs.pn = nil -} - -// // Update the node content and information in memory based on new file // information. // This method only check if the node name is equal with file name, but it's @@ -362,7 +352,7 @@ func (mfs *MemFS) createRoot(dir string, f *os.File) error { } if !fi.IsDir() { - return errors.New("mount must be a directory") + return fmt.Errorf("%q must be a directory", dir) } mfs.root = &Node{ @@ -371,7 +361,7 @@ func (mfs *MemFS) createRoot(dir string, f *os.File) error { name: "/", modTime: fi.ModTime(), mode: fi.Mode(), - size: fi.Size(), + size: 0, V: nil, Parent: nil, } diff --git a/lib/memfs/memfs_example_test.go b/lib/memfs/memfs_example_test.go index da414faf..c2b18074 100644 --- a/lib/memfs/memfs_example_test.go +++ b/lib/memfs/memfs_example_test.go @@ -14,12 +14,7 @@ func ExampleNew() { `.*/exclude`, } - mfs, err := New(incs, excs, true) - if err != nil { - log.Fatal(err) - } - - err = mfs.Mount("./testdata") + mfs, err := New("./testdata", incs, excs, true) if err != nil { log.Fatal(err) } @@ -35,12 +30,7 @@ func ExampleNew() { } func ExampleMemFS_Search() { - mfs, err := New(nil, nil, true) - if err != nil { - log.Fatal(err) - } - - err = mfs.Mount("./testdata") + mfs, err := New("./testdata", nil, nil, true) if err != nil { log.Fatal(err) } diff --git a/lib/memfs/memfs_test.go b/lib/memfs/memfs_test.go index 2accfb9a..ccaabaa2 100644 --- a/lib/memfs/memfs_test.go +++ b/lib/memfs/memfs_test.go @@ -1,6 +1,7 @@ package memfs import ( + "fmt" "log" "os" "path/filepath" @@ -51,12 +52,7 @@ func TestAddFile(t *testing.T) { }, }} - mfs, err := New(nil, nil, true) - if err != nil { - t.Fatal(err) - } - - err = mfs.Mount("testdata") + mfs, err := New("testdata", nil, nil, true) if err != nil { t.Fatal(err) } @@ -156,12 +152,8 @@ func TestGet(t *testing.T) { expContentType: "application/octet-stream", }} - mfs, err := New(nil, nil, true) - if err != nil { - t.Fatal(err) - } - - err = mfs.Mount(filepath.Join(_testWD, "/testdata")) + dir := filepath.Join(_testWD, "/testdata") + mfs, err := New(dir, nil, nil, true) if err != nil { t.Fatal(err) } @@ -184,7 +176,9 @@ func TestGet(t *testing.T) { } } -func TestMount(t *testing.T) { +func TestMemFS_mount(t *testing.T) { + afile := filepath.Join(_testWD, "testdata/index.html") + cases := []struct { desc string incs []string @@ -198,8 +192,8 @@ func TestMount(t *testing.T) { expMapKeys: make([]string, 0), }, { desc: "With file", - dir: filepath.Join(_testWD, "testdata/index.html"), - expErr: "memfs.Mount: mount must be a directory", + dir: afile, + expErr: fmt.Sprintf("memfs.New: mount: %q must be a directory", afile), }, { desc: "With directory", excs: []string{ @@ -265,12 +259,7 @@ func TestMount(t *testing.T) { for _, c := range cases { t.Log(c.desc) - mfs, err := New(c.incs, c.excs, true) - if err != nil { - t.Fatal(err) - } - - err = mfs.Mount(c.dir) + mfs, err := New(c.dir, c.incs, c.excs, true) if err != nil { test.Assert(t, "error", c.expErr, err.Error(), true) continue @@ -393,7 +382,7 @@ func TestFilter(t *testing.T) { for _, c := range cases { t.Log(c.desc) - mfs, err := New(c.inc, c.exc, true) + mfs, err := New("", c.inc, c.exc, true) if err != nil { t.Fatal(err) } diff --git a/lib/memfs/node_test.go b/lib/memfs/node_test.go index e63562e7..ea1920d8 100644 --- a/lib/memfs/node_test.go +++ b/lib/memfs/node_test.go @@ -64,12 +64,7 @@ func TestNode_Read(t *testing.T) { } func TestNode_Readdir(t *testing.T) { - mfs, err := New(nil, nil, true) - if err != nil { - t.Fatal(err) - } - - err = mfs.Mount("testdata") + mfs, err := New("testdata", nil, nil, true) if err != nil { t.Fatal(err) } diff --git a/lib/memfs/template.go b/lib/memfs/template.go index e250d013..f244041b 100644 --- a/lib/memfs/template.go +++ b/lib/memfs/template.go @@ -59,7 +59,7 @@ func generate{{ funcname .Path}}() *memfs.Node { ContentType: "{{.ContentType}}", ContentEncoding: "{{.ContentEncoding}}", {{- if .V }} - V: []byte("{{range $x, $c := .V}}{{ printf "\\x%x" $c }}{{end}}"), + V: []byte("{{range $x, $c := .V}}{{ printf "\\x%02X" $c }}{{end}}"), {{- end }} } node.SetMode({{printf "%d" .Mode}}) |
