diff options
| author | Shulhan <m.shulhan@gmail.com> | 2021-01-08 19:59:26 +0700 |
|---|---|---|
| committer | Shulhan <m.shulhan@gmail.com> | 2021-01-08 19:59:26 +0700 |
| commit | e16cf97cbe7394e39355055adbb8e1e2ad957692 (patch) | |
| tree | ce95f2cbf7594dc1478c9cbfe4e803129b80b376 /lib | |
| parent | 27ff5bc5b3ea87d8795996eba10d20678c61c0f2 (diff) | |
| download | pakakeh.go-e16cf97cbe7394e39355055adbb8e1e2ad957692.tar.xz | |
memfs: remove field WithContent
The field WithContent is not necessary if we set MaxFileSize to negative
value.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/http/http_test.go | 1 | ||||
| -rw-r--r-- | lib/io/dirwatcher.go | 14 | ||||
| -rw-r--r-- | lib/io/watcher.go | 2 | ||||
| -rw-r--r-- | lib/memfs/generate_test.go | 3 | ||||
| -rw-r--r-- | lib/memfs/memfs.go | 10 | ||||
| -rw-r--r-- | lib/memfs/memfs_example_test.go | 4 | ||||
| -rw-r--r-- | lib/memfs/memfs_test.go | 15 | ||||
| -rw-r--r-- | lib/memfs/node.go | 16 | ||||
| -rw-r--r-- | lib/memfs/node_test.go | 3 | ||||
| -rw-r--r-- | lib/memfs/options.go | 8 |
10 files changed, 31 insertions, 45 deletions
diff --git a/lib/http/http_test.go b/lib/http/http_test.go index ff80b972..c0e1ecee 100644 --- a/lib/http/http_test.go +++ b/lib/http/http_test.go @@ -53,7 +53,6 @@ func TestMain(m *testing.M) { Options: memfs.Options{ Root: "./testdata", MaxFileSize: 30, - WithContent: true, }, Address: "127.0.0.1:8080", } diff --git a/lib/io/dirwatcher.go b/lib/io/dirwatcher.go index 31dc1c94..26487a79 100644 --- a/lib/io/dirwatcher.go +++ b/lib/io/dirwatcher.go @@ -71,9 +71,10 @@ func (dw *DirWatcher) Start() (err error) { } memfsOpts := &memfs.Options{ - Root: dw.Path, - Includes: dw.Includes, - Excludes: dw.Excludes, + Root: dw.Path, + Includes: dw.Includes, + Excludes: dw.Excludes, + MaxFileSize: -1, } dw.fs, err = memfs.New(memfsOpts) if err != nil { @@ -242,9 +243,10 @@ func (dw *DirWatcher) onRootCreated() { var err error memfsOpts := &memfs.Options{ - Root: dw.Path, - Includes: dw.Includes, - Excludes: dw.Excludes, + Root: dw.Path, + Includes: dw.Includes, + Excludes: dw.Excludes, + MaxFileSize: -1, } dw.fs, err = memfs.New(memfsOpts) if err != nil { diff --git a/lib/io/watcher.go b/lib/io/watcher.go index ac718a7e..e1d3669c 100644 --- a/lib/io/watcher.go +++ b/lib/io/watcher.go @@ -58,7 +58,7 @@ func NewWatcher(path string, d time.Duration, cb WatchCallback) (w *Watcher, err return nil, fmt.Errorf("lib/io: NewWatcher: path is directory") } - node, err := memfs.NewNode(nil, fi, 0, false) + node, err := memfs.NewNode(nil, fi, -1) if err != nil { log.Printf("lib/io: NewWatcher %s: %s", fi.Name(), err.Error()) return nil, nil diff --git a/lib/memfs/generate_test.go b/lib/memfs/generate_test.go index c7029fef..d567e338 100644 --- a/lib/memfs/generate_test.go +++ b/lib/memfs/generate_test.go @@ -8,8 +8,7 @@ import "testing" func TestGenerate(t *testing.T) { opts := &Options{ - Root: "testdata", - WithContent: true, + Root: "testdata", } mfs, err := New(opts) if err != nil { diff --git a/lib/memfs/memfs.go b/lib/memfs/memfs.go index 74eff7e1..69e97413 100644 --- a/lib/memfs/memfs.go +++ b/lib/memfs/memfs.go @@ -125,7 +125,7 @@ func (mfs *MemFS) AddFile(path string) (*Node, error) { return nil, fmt.Errorf("memfs.AddFile: %w", err) } - node, err = NewNode(parent, fi, mfs.opts.MaxFileSize, mfs.opts.WithContent) + node, err = NewNode(parent, fi, mfs.opts.MaxFileSize) if err != nil { return nil, fmt.Errorf("memfs.AddFile: %w", err) } @@ -219,7 +219,7 @@ func (mfs *MemFS) Get(path string) (node *Node, err error) { } if mfs.opts.Development { - err = node.update(nil, mfs.opts.MaxFileSize, mfs.opts.WithContent) + err = node.update(nil, mfs.opts.MaxFileSize) if err != nil { return nil, fmt.Errorf("memfs.Get: %w", err) } @@ -299,7 +299,7 @@ func (mfs *MemFS) mount(dir string) error { return fmt.Errorf("mount: %w", err) } - if mfs.opts.WithContent { + if mfs.opts.MaxFileSize > 0 { mfs.pruneEmptyDirs() } @@ -318,7 +318,7 @@ func (mfs *MemFS) Update(node *Node, newInfo os.FileInfo) { return } - err := node.update(newInfo, mfs.opts.MaxFileSize, mfs.opts.WithContent) + err := node.update(newInfo, mfs.opts.MaxFileSize) if err != nil { log.Println("lib/memfs: Update: " + err.Error()) } @@ -411,7 +411,7 @@ func (mfs *MemFS) AddChild(parent *Node, fi os.FileInfo) (child *Node, err error return nil, nil } - child, err = parent.addChild(sysPath, fi, mfs.opts.MaxFileSize, mfs.opts.WithContent) + child, err = parent.addChild(sysPath, fi, mfs.opts.MaxFileSize) if err != nil { log.Printf("AddChild %s: %s", fi.Name(), err.Error()) return nil, nil diff --git a/lib/memfs/memfs_example_test.go b/lib/memfs/memfs_example_test.go index 2f7f8a05..5b21602d 100644 --- a/lib/memfs/memfs_example_test.go +++ b/lib/memfs/memfs_example_test.go @@ -15,7 +15,6 @@ func ExampleNew() { Excludes: []string{ `.*/exclude`, }, - WithContent: true, } mfs, err := New(opts) if err != nil { @@ -34,8 +33,7 @@ func ExampleNew() { func ExampleMemFS_Search() { opts := &Options{ - Root: "./testdata", - WithContent: true, + Root: "./testdata", } mfs, err := New(opts) if err != nil { diff --git a/lib/memfs/memfs_test.go b/lib/memfs/memfs_test.go index e077e008..f40212c0 100644 --- a/lib/memfs/memfs_test.go +++ b/lib/memfs/memfs_test.go @@ -54,8 +54,7 @@ func TestAddFile(t *testing.T) { }} opts := &Options{ - Root: "testdata", - WithContent: true, + Root: "testdata", } mfs, err := New(opts) if err != nil { @@ -161,7 +160,6 @@ func TestGet(t *testing.T) { Root: dir, // Limit file size to allow testing Get from disk on file "index.js". MaxFileSize: 15, - WithContent: true, } mfs, err := New(opts) @@ -202,8 +200,7 @@ func TestMemFS_mount(t *testing.T) { }, { desc: "With file", opts: Options{ - Root: afile, - WithContent: true, + Root: afile, }, expErr: fmt.Sprintf("memfs.New: mount: %q must be a directory", afile), }, { @@ -214,7 +211,6 @@ func TestMemFS_mount(t *testing.T) { "memfs_generate.go$", "direct$", }, - WithContent: true, }, expMapKeys: []string{ "/", @@ -240,7 +236,6 @@ func TestMemFS_mount(t *testing.T) { "memfs_generate.go$", "direct$", }, - WithContent: true, }, expMapKeys: []string{ "/", @@ -265,7 +260,6 @@ func TestMemFS_mount(t *testing.T) { "memfs_generate.go$", "direct$", }, - WithContent: true, }, expMapKeys: []string{ "/", @@ -404,9 +398,8 @@ func TestFilter(t *testing.T) { t.Log(c.desc) opts := &Options{ - Includes: c.inc, - Excludes: c.exc, - WithContent: true, + Includes: c.inc, + Excludes: c.exc, } mfs, err := New(opts) if err != nil { diff --git a/lib/memfs/node.go b/lib/memfs/node.go index 96196e17..e5b60a59 100644 --- a/lib/memfs/node.go +++ b/lib/memfs/node.go @@ -53,10 +53,10 @@ type Node struct { // // 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. +// If maxFileSize is greater than zero, the file content and its type will be +// saved in node as V and ContentType. // -func NewNode(parent *Node, fi os.FileInfo, maxFileSize int64, withContent bool) (node *Node, err error) { +func NewNode(parent *Node, fi os.FileInfo, maxFileSize int64) (node *Node, err error) { if fi == nil { return nil, nil } @@ -87,7 +87,7 @@ func NewNode(parent *Node, fi os.FileInfo, maxFileSize int64, withContent bool) } node.generateFuncName() - if node.mode.IsDir() || !withContent { + if node.mode.IsDir() || maxFileSize <= 0 { node.size = 0 return node, nil } @@ -305,9 +305,9 @@ func (leaf *Node) Sys() interface{} { // addChild add new node as sub-directory or file of this node. // func (leaf *Node) addChild( - sysPath string, fi os.FileInfo, maxFileSize int64, withContent bool, + sysPath string, fi os.FileInfo, maxFileSize int64, ) (child *Node, err error) { - child, err = NewNode(leaf, fi, maxFileSize, withContent) + child, err = NewNode(leaf, fi, maxFileSize) if err != nil { return nil, err } @@ -359,7 +359,7 @@ func (leaf *Node) removeChild(child *Node) *Node { // mode or change on content (size and modtime). // Change on mode will not affect the content of node. // -func (leaf *Node) update(newInfo os.FileInfo, maxFileSize int64, withContent bool) (err error) { +func (leaf *Node) update(newInfo os.FileInfo, maxFileSize int64) (err error) { if newInfo == nil { newInfo, err = os.Stat(leaf.SysPath) if err != nil { @@ -376,7 +376,7 @@ func (leaf *Node) update(newInfo os.FileInfo, maxFileSize int64, withContent boo leaf.modTime = newInfo.ModTime() leaf.size = newInfo.Size() - if !withContent || newInfo.IsDir() { + if newInfo.IsDir() || maxFileSize <= 0 { return nil } diff --git a/lib/memfs/node_test.go b/lib/memfs/node_test.go index 78c6abc3..f2f899a8 100644 --- a/lib/memfs/node_test.go +++ b/lib/memfs/node_test.go @@ -65,8 +65,7 @@ func TestNode_Read(t *testing.T) { func TestNode_Readdir(t *testing.T) { opts := &Options{ - Root: "testdata", - WithContent: true, + Root: "testdata", } mfs, err := New(opts) if err != nil { diff --git a/lib/memfs/options.go b/lib/memfs/options.go index 92fba58b..11e097c2 100644 --- a/lib/memfs/options.go +++ b/lib/memfs/options.go @@ -25,14 +25,10 @@ type Options struct { // MaxFileSize define maximum file size that can be stored on memory. // The default value is 5 MB. + // If its value is negative, the content of file will not be mapped to + // memory, the MemFS will behave as directory tree. MaxFileSize int64 - // WithContent parameter tell the MemFS to read the content of file - // and detect its content type. - // If false, the content of file will not be mapped to memory, the - // MemFS will behave as directory tree. - WithContent bool - // Development define a flag to bypass file in memory. // If its true, any call to Get will result in direct read to file // system. |
