2017-11-15 18:50:29 +03:00
|
|
|
package zipfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
|
|
|
type File struct {
|
|
|
|
fs *Fs
|
|
|
|
zipfile *zip.File
|
|
|
|
reader io.ReadCloser
|
|
|
|
offset int64
|
|
|
|
isdir, closed bool
|
|
|
|
buf []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) fillBuffer(offset int64) (err error) {
|
|
|
|
if f.reader == nil {
|
|
|
|
if f.reader, err = f.zipfile.Open(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if offset > int64(f.zipfile.UncompressedSize64) {
|
|
|
|
offset = int64(f.zipfile.UncompressedSize64)
|
|
|
|
err = io.EOF
|
|
|
|
}
|
|
|
|
if len(f.buf) >= int(offset) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
buf := make([]byte, int(offset)-len(f.buf))
|
2020-03-30 16:28:04 +03:00
|
|
|
if n, readErr := io.ReadFull(f.reader, buf); n > 0 {
|
2017-11-15 18:50:29 +03:00
|
|
|
f.buf = append(f.buf, buf[:n]...)
|
2020-03-30 16:28:04 +03:00
|
|
|
} else if readErr != nil {
|
|
|
|
err = readErr
|
2017-11-15 18:50:29 +03:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) Close() (err error) {
|
|
|
|
f.zipfile = nil
|
|
|
|
f.closed = true
|
|
|
|
f.buf = nil
|
|
|
|
if f.reader != nil {
|
|
|
|
err = f.reader.Close()
|
|
|
|
f.reader = nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) Read(p []byte) (n int, err error) {
|
|
|
|
if f.isdir {
|
|
|
|
return 0, syscall.EISDIR
|
|
|
|
}
|
|
|
|
if f.closed {
|
|
|
|
return 0, afero.ErrFileClosed
|
|
|
|
}
|
|
|
|
err = f.fillBuffer(f.offset + int64(len(p)))
|
|
|
|
n = copy(p, f.buf[f.offset:])
|
2020-06-26 07:27:17 +03:00
|
|
|
f.offset += int64(n)
|
2017-11-15 18:50:29 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) ReadAt(p []byte, off int64) (n int, err error) {
|
|
|
|
if f.isdir {
|
|
|
|
return 0, syscall.EISDIR
|
|
|
|
}
|
|
|
|
if f.closed {
|
|
|
|
return 0, afero.ErrFileClosed
|
|
|
|
}
|
|
|
|
err = f.fillBuffer(off + int64(len(p)))
|
|
|
|
n = copy(p, f.buf[int(off):])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) Seek(offset int64, whence int) (int64, error) {
|
|
|
|
if f.isdir {
|
|
|
|
return 0, syscall.EISDIR
|
|
|
|
}
|
|
|
|
if f.closed {
|
|
|
|
return 0, afero.ErrFileClosed
|
|
|
|
}
|
|
|
|
switch whence {
|
|
|
|
case os.SEEK_SET:
|
|
|
|
case os.SEEK_CUR:
|
|
|
|
offset += f.offset
|
|
|
|
case os.SEEK_END:
|
|
|
|
offset += int64(f.zipfile.UncompressedSize64)
|
|
|
|
default:
|
|
|
|
return 0, syscall.EINVAL
|
|
|
|
}
|
|
|
|
if offset < 0 || offset > int64(f.zipfile.UncompressedSize64) {
|
|
|
|
return 0, afero.ErrOutOfRange
|
|
|
|
}
|
|
|
|
f.offset = offset
|
|
|
|
return offset, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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 }
|
|
|
|
|
2019-03-11 21:14:41 +03:00
|
|
|
func (f *File) Name() string {
|
|
|
|
if f.zipfile == nil {
|
|
|
|
return string(filepath.Separator)
|
|
|
|
}
|
|
|
|
return filepath.Join(splitpath(f.zipfile.Name))
|
|
|
|
}
|
2017-11-15 18:50:29 +03:00
|
|
|
|
|
|
|
func (f *File) getDirEntries() (map[string]*zip.File, error) {
|
|
|
|
if !f.isdir {
|
|
|
|
return nil, syscall.ENOTDIR
|
|
|
|
}
|
2019-03-11 21:14:41 +03:00
|
|
|
name := f.Name()
|
2017-11-15 18:50:29 +03:00
|
|
|
entries, ok := f.fs.files[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, &os.PathError{Op: "readdir", Path: name, Err: syscall.ENOENT}
|
|
|
|
}
|
|
|
|
return entries, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) Readdir(count int) (fi []os.FileInfo, err error) {
|
|
|
|
zipfiles, err := f.getDirEntries()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, zipfile := range zipfiles {
|
|
|
|
fi = append(fi, zipfile.FileInfo())
|
2020-04-14 23:36:17 +03:00
|
|
|
if count > 0 && len(fi) >= count {
|
2017-11-15 18:50:29 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) Readdirnames(count int) (names []string, err error) {
|
|
|
|
zipfiles, err := f.getDirEntries()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-11 00:50:34 +03:00
|
|
|
for filename := range zipfiles {
|
2017-11-15 18:50:29 +03:00
|
|
|
names = append(names, filename)
|
2020-04-14 23:36:17 +03:00
|
|
|
if count > 0 && len(names) >= count {
|
2017-11-15 18:50:29 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-11 21:00:04 +03:00
|
|
|
func (f *File) Stat() (os.FileInfo, error) {
|
|
|
|
if f.zipfile == nil {
|
|
|
|
return &pseudoRoot{}, nil
|
|
|
|
}
|
|
|
|
return f.zipfile.FileInfo(), nil
|
|
|
|
}
|
2017-11-15 18:50:29 +03:00
|
|
|
|
|
|
|
func (f *File) Sync() error { return nil }
|
|
|
|
|
|
|
|
func (f *File) Truncate(size int64) error { return syscall.EPERM }
|
|
|
|
|
|
|
|
func (f *File) WriteString(s string) (ret int, err error) { return 0, syscall.EPERM }
|