Eliminate another root-path-related corner case

This commit is contained in:
Hilko Bengen 2019-03-11 19:14:41 +01:00
parent d5bfeca89b
commit 72cabd552e
2 changed files with 10 additions and 5 deletions

View File

@ -103,16 +103,18 @@ func (f *File) Write(p []byte) (n int, err error) { return 0, syscall.EPERM }
func (f *File) WriteAt(p []byte, off int64) (n int, err error) { return 0, syscall.EPERM } func (f *File) WriteAt(p []byte, off int64) (n int, err error) { return 0, syscall.EPERM }
func (f *File) Name() string { return f.zipfile.Name } func (f *File) Name() string {
if f.zipfile == nil {
return string(filepath.Separator)
}
return filepath.Join(splitpath(f.zipfile.Name))
}
func (f *File) getDirEntries() (map[string]*zip.File, error) { func (f *File) getDirEntries() (map[string]*zip.File, error) {
if !f.isdir { if !f.isdir {
return nil, syscall.ENOTDIR return nil, syscall.ENOTDIR
} }
name := string(filepath.Separator) name := f.Name()
if f.zipfile != nil {
name = filepath.Join(splitpath(f.zipfile.Name))
}
entries, ok := f.fs.files[name] entries, ok := f.fs.files[name]
if !ok { if !ok {
return nil, &os.PathError{Op: "readdir", Path: name, Err: syscall.ENOENT} return nil, &os.PathError{Op: "readdir", Path: name, Err: syscall.ENOENT}

View File

@ -48,6 +48,9 @@ func TestZipFS(t *testing.T) {
if s, _ := d.Stat(); !s.IsDir() { if s, _ := d.Stat(); !s.IsDir() {
t.Error(`expected root ("/") to be a directory`) t.Error(`expected root ("/") to be a directory`)
} }
if n := d.Name(); n != "/" {
t.Errorf("Wrong Name() of root directory: Expected: '/', got '%s'", n)
}
buf = make([]byte, 8192) buf = make([]byte, 8192)
if n, err := f.Read(buf); err != nil { if n, err := f.Read(buf); err != nil {