From 72cabd552e74467703e3f90b84a48939a7bae1c7 Mon Sep 17 00:00:00 2001 From: Hilko Bengen Date: Mon, 11 Mar 2019 19:14:41 +0100 Subject: [PATCH] Eliminate another root-path-related corner case --- zipfs/file.go | 12 +++++++----- zipfs/zipfs_test.go | 3 +++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/zipfs/file.go b/zipfs/file.go index 36c4ce9..a81ea06 100644 --- a/zipfs/file.go +++ b/zipfs/file.go @@ -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) 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) { if !f.isdir { return nil, syscall.ENOTDIR } - name := string(filepath.Separator) - if f.zipfile != nil { - name = filepath.Join(splitpath(f.zipfile.Name)) - } + name := f.Name() entries, ok := f.fs.files[name] if !ok { return nil, &os.PathError{Op: "readdir", Path: name, Err: syscall.ENOENT} diff --git a/zipfs/zipfs_test.go b/zipfs/zipfs_test.go index 54db4aa..7a141cb 100644 --- a/zipfs/zipfs_test.go +++ b/zipfs/zipfs_test.go @@ -48,6 +48,9 @@ func TestZipFS(t *testing.T) { if s, _ := d.Stat(); !s.IsDir() { 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) if n, err := f.Read(buf); err != nil {