Work around root directory Open/Stat corner case leading to panic

See https://github.com/spf13/afero/pull/146#issuecomment-470840725
This commit is contained in:
Hilko Bengen 2019-03-11 19:00:04 +01:00
parent 4f00b06400
commit d5bfeca89b
2 changed files with 18 additions and 1 deletions

View File

@ -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 }

View File

@ -38,6 +38,17 @@ func TestZipFS(t *testing.T) {
t.Errorf("expected to get <aaaabbbb>, 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},