Skip to content

Commit

Permalink
lib/memfs: include empty directory
Browse files Browse the repository at this point in the history
Even thought empty directory does not contains file, from the parent
node _it is_ part of their content.

Also, there is a use case where memfs use as virtual file system (VFS),
as a layer with file system, where user can view list of directory,
create a directory or file on the fly.
If we skip scanning empty directory, that directory will not be visible.
  • Loading branch information
shuLhan committed Nov 5, 2023
1 parent d714934 commit 3dbed6f
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 20 deletions.
43 changes: 41 additions & 2 deletions lib/memfs/internal/test/embed/embed_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/memfs/internal/test/embed/memfs_embed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,15 @@ func TestNode_Readdir(t *testing.T) {
}, {
path: "/exclude",
exp: []string{
"dir",
"index-link.css",
"index-link.html",
"index-link.js",
},
}, {
path: "/include",
exp: []string{
"dir",
"index.css",
"index.html",
"index.js",
Expand Down
41 changes: 39 additions & 2 deletions lib/memfs/internal/test/embed_disable_modtime/embed_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,15 @@ func TestNode_Readdir(t *testing.T) {
}, {
path: "/exclude",
exp: []string{
"dir",
"index-link.css",
"index-link.html",
"index-link.js",
},
}, {
path: "/include",
exp: []string{
"dir",
"index.css",
"index.html",
"index.js",
Expand Down
23 changes: 8 additions & 15 deletions lib/memfs/memfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ func (mfs *MemFS) mount() (err error) {
return nil
}
if mfs.Root != nil {
// The directory has been initialized by embedded.
// The Root node may has been initialized through embedding.
return nil
}

Expand Down Expand Up @@ -568,17 +568,15 @@ func (mfs *MemFS) Remount() (err error) {
return mfs.mount()
}

// scanDir scan the directory node for files and add them to memory file
// system.
// scanDir scan the content of node directory and add them to mfs.
// It returns number of childs added to the node or an error.
func (mfs *MemFS) scanDir(node *Node) (n int, err error) {
var (
logp = "scanDir"
child *Node
nchilds int
f *os.File
fi os.FileInfo
fis []os.FileInfo
logp = "scanDir"
child *Node
f *os.File
fi os.FileInfo
fis []os.FileInfo
)

f, err = os.Open(node.SysPath)
Expand Down Expand Up @@ -613,16 +611,11 @@ func (mfs *MemFS) scanDir(node *Node) (n int, err error) {
continue
}

nchilds, err = mfs.scanDir(child)
_, err = mfs.scanDir(child)
if err != nil {
err = fmt.Errorf(`%s %q: %w`, logp, node.SysPath, err)
goto out
}
if nchilds == 0 {
// No childs added, remove it from node.
mfs.RemoveChild(node, child)
n--
}
}
out:
errClose := f.Close()
Expand Down
2 changes: 1 addition & 1 deletion lib/memfs/memfs_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func ExampleNew() {

// Output:
// Content of /index.html: <html></html>
// List of embedded files: [/ /include /include/index.css /include/index.html /include/index.js /index.css /index.html /index.js]
// List of embedded files: [/ /direct /direct/add /include /include/dir /include/index.css /include/index.html /include/index.js /index.css /index.html /index.js]
// Error on Get /exclude/index.html: file does not exist
}

Expand Down
8 changes: 8 additions & 0 deletions lib/memfs/memfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,12 @@ func TestNew(t *testing.T) {
expMapKeys: []string{
"/",
"/exclude",
"/exclude/dir",
"/exclude/index-link.css",
"/exclude/index-link.html",
"/exclude/index-link.js",
"/include",
"/include/dir",
"/include/index.css",
"/include/index.html",
"/include/index.js",
Expand All @@ -141,9 +143,11 @@ func TestNew(t *testing.T) {
expMapKeys: []string{
"/",
"/exclude",
"/exclude/dir",
"/exclude/index-link.css",
"/exclude/index-link.html",
"/include",
"/include/dir",
"/include/index.css",
"/include/index.html",
"/index.css",
Expand All @@ -166,8 +170,10 @@ func TestNew(t *testing.T) {
expMapKeys: []string{
"/",
"/exclude",
"/exclude/dir",
"/exclude/index-link.js",
"/include",
"/include/dir",
"/include/index.js",
"/index.js",
},
Expand Down Expand Up @@ -669,6 +675,7 @@ func TestMerge(t *testing.T) {
Path: "/",
Childs: []*Node{
mfsDirect.MustGet("/add"),
mfsInclude.MustGet("/dir"),
mfsInclude.MustGet("/index.css"),
mfsInclude.MustGet("/index.html"),
mfsInclude.MustGet("/index.js"),
Expand All @@ -678,6 +685,7 @@ func TestMerge(t *testing.T) {
"/add": mfsDirect.MustGet("/add"),
"/add/file": mfsDirect.MustGet("/add/file"),
"/add/file2": mfsDirect.MustGet("/add/file2"),
"/dir": mfsInclude.MustGet("/dir"),
"/index.css": mfsInclude.MustGet("/index.css"),
"/index.html": mfsInclude.MustGet("/index.html"),
"/index.js": mfsInclude.MustGet("/index.js"),
Expand Down

0 comments on commit 3dbed6f

Please sign in to comment.