From 0ec4cd15a07d33ba754360728ae424f5f4a15db1 Mon Sep 17 00:00:00 2001 From: Dmytro Manchynskyi Date: Fri, 27 May 2022 16:39:53 +0300 Subject: [PATCH 1/2] Fix #317 --- zipfs/file.go | 15 +++++++- zipfs/fs.go | 36 ++++++++++++++++++ zipfs/testdata/no_dir_entry.zip | Bin 0 -> 344 bytes zipfs/zipfs_test.go | 64 +++++++++++++++++++++++++++++++- 4 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 zipfs/testdata/no_dir_entry.zip diff --git a/zipfs/file.go b/zipfs/file.go index 355f5f45..ccc1a7e5 100644 --- a/zipfs/file.go +++ b/zipfs/file.go @@ -13,6 +13,7 @@ import ( type File struct { fs *Fs zipfile *zip.File + pseudodir *pseudoDir reader io.ReadCloser offset int64 isdir, closed bool @@ -106,6 +107,9 @@ func (f *File) WriteAt(p []byte, off int64) (n int, err error) { return 0, sysca func (f *File) Name() string { if f.zipfile == nil { + if f.pseudodir != nil { + return f.pseudodir.path + } return string(filepath.Separator) } return filepath.Join(splitpath(f.zipfile.Name)) @@ -128,8 +132,12 @@ func (f *File) Readdir(count int) (fi []os.FileInfo, err error) { if err != nil { return nil, err } - for _, zipfile := range zipfiles { - fi = append(fi, zipfile.FileInfo()) + for d, zipfile := range zipfiles { + if zipfile == nil { + fi = append(fi, pseudoDir{d}) + } else { + fi = append(fi, zipfile.FileInfo()) + } if count > 0 && len(fi) >= count { break } @@ -153,6 +161,9 @@ func (f *File) Readdirnames(count int) (names []string, err error) { func (f *File) Stat() (os.FileInfo, error) { if f.zipfile == nil { + if f.pseudodir != nil { + return f.pseudodir, nil + } return &pseudoRoot{}, nil } return f.zipfile.FileInfo(), nil diff --git a/zipfs/fs.go b/zipfs/fs.go index d08b7f5d..953044e6 100644 --- a/zipfs/fs.go +++ b/zipfs/fs.go @@ -42,6 +42,21 @@ func New(r *zip.Reader) afero.Fs { fs.files[dirname] = make(map[string]*zip.File) } } + + dv := d + for { + d, f = splitpath(dv) + if _, ok := fs.files[d]; !ok { + fs.files[d] = make(map[string]*zip.File) + } + if f == "" { + break + } + if _, ok := fs.files[d][f]; !ok { + fs.files[d][f] = nil + } + dv = d + } } return fs } @@ -64,6 +79,13 @@ func (fs *Fs) Open(name string) (afero.File, error) { if !ok { return nil, &os.PathError{Op: "stat", Path: name, Err: syscall.ENOENT} } + if file == nil { + return &File{ + fs: fs, + pseudodir: &pseudoDir{path: filepath.Join(d, f)}, + isdir: true, + }, nil + } return &File{fs: fs, zipfile: file, isdir: file.FileInfo().IsDir()}, nil } @@ -89,6 +111,17 @@ func (p *pseudoRoot) ModTime() time.Time { return time.Now() } func (p *pseudoRoot) IsDir() bool { return true } func (p *pseudoRoot) Sys() interface{} { return nil } +type pseudoDir struct { + path string +} + +func (fi pseudoDir) Name() string { return filepath.Base(fi.path) } +func (fi pseudoDir) Size() int64 { return 0 } +func (fi pseudoDir) IsDir() bool { return true } +func (fi pseudoDir) ModTime() time.Time { return time.Now() } +func (fi pseudoDir) Mode() os.FileMode { return os.ModeDir | os.ModePerm } +func (fi pseudoDir) Sys() interface{} { return nil } + func (fs *Fs) Stat(name string) (os.FileInfo, error) { d, f := splitpath(name) if f == "" { @@ -101,6 +134,9 @@ func (fs *Fs) Stat(name string) (os.FileInfo, error) { if !ok { return nil, &os.PathError{Op: "stat", Path: name, Err: syscall.ENOENT} } + if file == nil { + return pseudoDir{name}, nil + } return file.FileInfo(), nil } diff --git a/zipfs/testdata/no_dir_entry.zip b/zipfs/testdata/no_dir_entry.zip new file mode 100644 index 0000000000000000000000000000000000000000..04345bec4bb3e1ffb55b3957e340f952dd6fabd9 GIT binary patch literal 344 zcmWIWW@Zs#-~d8&APN*HE=|%eNi8mM$t*GgGu$$BQr{eC6kuTBVK%&RcK$-S`jd=6 zg_Zn3;)yN;!~g#Q-s~J~&*GBgK?W)?Fa+SVK@g|_YPun&>DSNR$6>nc8j 0 { + t.Errorf("Files %v is not found in zip", expected) + } + + // Test ReadDir + dir, err := afero.ReadDir(zfs, "/") + if err != nil { + t.Fatal(err) + } + expected = map[string]bool{ + "sub": true, + "testDir1": true, + } + for _, d := range dir { + if isDir, ok := expected[d.Name()]; ok { + if isDir != d.IsDir() { + t.Error("file", d.Name(), "isDir:", d.IsDir(), "but expected:", isDir) + } + delete(expected, d.Name()) + } else { + t.Error("Unexpected file", d.Name(), "isDir:", d.IsDir()) + } + } + if len(expected) > 0 { + t.Errorf("Files %v is not found in zip", expected) + } +} From 76d71183000e12eb5230a731cf9f2405a3ba4d80 Mon Sep 17 00:00:00 2001 From: Dmytro Manchynskyi Date: Fri, 27 May 2022 16:55:44 +0300 Subject: [PATCH 2/2] Fix appveyor tests --- zipfs/zipfs_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zipfs/zipfs_test.go b/zipfs/zipfs_test.go index ed97a06c..dfea1524 100644 --- a/zipfs/zipfs_test.go +++ b/zipfs/zipfs_test.go @@ -2,7 +2,7 @@ package zipfs import ( "archive/zip" - "io/fs" + "os" "path/filepath" "reflect" "testing" @@ -119,7 +119,7 @@ func TestZipFsNoDirEntry(t *testing.T) { "testDir1": true, "testDir1/testFile": false, } - err = afero.Walk(zfs, "", func(path string, info fs.FileInfo, err error) error { + err = afero.Walk(zfs, "", func(path string, info os.FileInfo, err error) error { path = filepath.ToSlash(path) if isDir, ok := expected[path]; ok { if isDir != info.IsDir() {