From d5bfeca89b128cf5061264419543ee1c120e0173 Mon Sep 17 00:00:00 2001 From: Hilko Bengen Date: Mon, 11 Mar 2019 19:00:04 +0100 Subject: [PATCH] Work around root directory Open/Stat corner case leading to panic See https://github.com/spf13/afero/pull/146#issuecomment-470840725 --- zipfs/file.go | 7 ++++++- zipfs/zipfs_test.go | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/zipfs/file.go b/zipfs/file.go index 2841227..36c4ce9 100644 --- a/zipfs/file.go +++ b/zipfs/file.go @@ -148,7 +148,12 @@ func (f *File) Readdirnames(count int) (names []string, err error) { return } -func (f *File) Stat() (os.FileInfo, error) { return f.zipfile.FileInfo(), nil } +func (f *File) Stat() (os.FileInfo, error) { + if f.zipfile == nil { + return &pseudoRoot{}, nil + } + return f.zipfile.FileInfo(), nil +} func (f *File) Sync() error { return nil } diff --git a/zipfs/zipfs_test.go b/zipfs/zipfs_test.go index 02ac6b6..54db4aa 100644 --- a/zipfs/zipfs_test.go +++ b/zipfs/zipfs_test.go @@ -38,6 +38,17 @@ func TestZipFS(t *testing.T) { t.Errorf("expected to get , got <%s>", string(buf)) } + d, err := a.Open("/") + if d == nil { + t.Error(`Open("/") returns nil`) + } + if err != nil { + t.Errorf(`Open("/"): err = %v`, err) + } + if s, _ := d.Stat(); !s.IsDir() { + t.Error(`expected root ("/") to be a directory`) + } + buf = make([]byte, 8192) if n, err := f.Read(buf); err != nil { t.Error(err) @@ -51,6 +62,7 @@ func TestZipFS(t *testing.T) { path string dir bool }{ + {"/", true}, {"testDir1", true}, {"testDir1/testFile", false}, {"testFile", false},