diff options
| author | Shulhan <ms@kilabit.info> | 2021-08-22 20:52:43 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2021-08-22 20:52:43 +0700 |
| commit | c1bd3574758c7070eb405922b56f0dfa056cad01 (patch) | |
| tree | 0a041234bac4e1cadb82260dda519eb76d3d0fe0 /lib | |
| parent | 884220a03acc6da626dd97f6f9682e5041e315e6 (diff) | |
| download | pakakeh.go-c1bd3574758c7070eb405922b56f0dfa056cad01.tar.xz | |
lib/memfs: fix empty file not being added to tree
Previously, we did not check if the file size is 0 before reading the
content or updating the content type, which cause the read on file
return io.EOF and the file not added to caches.
This commit fix this issue by checking for zero file size and for
io.EOF when reading the file content.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/memfs/memfs.go | 2 | ||||
| -rw-r--r-- | lib/memfs/memfs_test.go | 8 | ||||
| -rw-r--r-- | lib/memfs/node.go | 18 |
3 files changed, 23 insertions, 5 deletions
diff --git a/lib/memfs/memfs.go b/lib/memfs/memfs.go index 70cae220..6d0c523c 100644 --- a/lib/memfs/memfs.go +++ b/lib/memfs/memfs.go @@ -29,6 +29,8 @@ import ( // const ( EncodingGzip = "gzip" + + defContentType = "application/octet-stream" ) // diff --git a/lib/memfs/memfs_test.go b/lib/memfs/memfs_test.go index 998913a3..18dc1eab 100644 --- a/lib/memfs/memfs_test.go +++ b/lib/memfs/memfs_test.go @@ -198,11 +198,9 @@ func TestMemFS_AddFile(t *testing.T) { } for _, c := range cases { - t.Log(c.desc) - got, err := mfs.AddFile(c.intPath, c.extPath) if err != nil { - test.Assert(t, "error", c.expError, err.Error()) + test.Assert(t, c.desc+": error", c.expError, err.Error()) continue } @@ -213,7 +211,7 @@ func TestMemFS_AddFile(t *testing.T) { got.Childs = nil } - test.Assert(t, "AddFile", c.exp, got) + test.Assert(t, c.desc+": AddFile", c.exp, got) if c.exp == nil { continue @@ -231,7 +229,7 @@ func TestMemFS_AddFile(t *testing.T) { got.Childs = nil } - test.Assert(t, "Get", c.exp, got) + test.Assert(t, c.desc+": Get", c.exp, got) } } diff --git a/lib/memfs/node.go b/lib/memfs/node.go index 12d90340..c958ea90 100644 --- a/lib/memfs/node.go +++ b/lib/memfs/node.go @@ -454,9 +454,16 @@ func (leaf *Node) updateContent(maxFileSize int64) (err error) { if leaf.size > maxFileSize { return nil } + if leaf.size == 0 { + leaf.V = nil + return nil + } leaf.V, err = ioutil.ReadFile(leaf.SysPath) if err != nil { + if errors.Is(err, io.EOF) { + return nil + } return err } @@ -473,11 +480,22 @@ func (leaf *Node) updateContentType() error { leaf.ContentType = http.DetectContentType(leaf.V) return nil } + if leaf.size == 0 { + // The actual file size is zero, we set the content type to + // default. + leaf.ContentType = defContentType + return nil + } data := make([]byte, 512) f, err := os.Open(leaf.SysPath) if err != nil { + if errors.Is(err, io.EOF) { + // File is empty. + leaf.ContentType = defContentType + return nil + } return err } |
