Skip to content

Commit

Permalink
Handle symlink zip files (stashapp#5249)
Browse files Browse the repository at this point in the history
  • Loading branch information
WithoutPants authored Sep 11, 2024
1 parent d1c207e commit a17199b
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 17 deletions.
9 changes: 2 additions & 7 deletions pkg/file/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,8 @@ func (f *OsFS) Open(name string) (fs.ReadDirFile, error) {
return os.Open(name)
}

func (f *OsFS) OpenZip(name string) (models.ZipFS, error) {
info, err := f.Lstat(name)
if err != nil {
return nil, err
}

return newZipFS(f, name, info)
func (f *OsFS) OpenZip(name string, size int64) (models.ZipFS, error) {
return newZipFS(f, name, size)
}

func (f *OsFS) IsPathCaseSensitive(path string) (bool, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/file/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ func (s *scanJob) acceptEntry(ctx context.Context, path string, info fs.FileInfo
}

func (s *scanJob) scanZipFile(ctx context.Context, f scanFile) error {
zipFS, err := f.fs.OpenZip(f.Path)
zipFS, err := f.fs.OpenZip(f.Path, f.Size)
if err != nil {
if errors.Is(err, errNotReaderAt) {
// can't walk the zip file
Expand Down Expand Up @@ -838,7 +838,7 @@ func (s *scanJob) getFileFS(f *models.BaseFile) (models.FS, error) {
}

zipPath := f.ZipFile.Base().Path
return fs.OpenZip(zipPath)
return fs.OpenZip(zipPath, f.Size)
}

func (s *scanJob) handleRename(ctx context.Context, f models.File, fp []models.Fingerprint) (models.File, error) {
Expand Down
8 changes: 3 additions & 5 deletions pkg/file/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ var (
type zipFS struct {
*zip.Reader
zipFileCloser io.Closer
zipInfo fs.FileInfo
zipPath string
}

func newZipFS(fs models.FS, path string, info fs.FileInfo) (*zipFS, error) {
func newZipFS(fs models.FS, path string, size int64) (*zipFS, error) {
reader, err := fs.Open(path)
if err != nil {
return nil, err
Expand All @@ -42,7 +41,7 @@ func newZipFS(fs models.FS, path string, info fs.FileInfo) (*zipFS, error) {
return nil, errNotReaderAt
}

zipReader, err := zip.NewReader(asReaderAt, info.Size())
zipReader, err := zip.NewReader(asReaderAt, size)
if err != nil {
reader.Close()
return nil, err
Expand Down Expand Up @@ -89,7 +88,6 @@ func newZipFS(fs models.FS, path string, info fs.FileInfo) (*zipFS, error) {
return &zipFS{
Reader: zipReader,
zipFileCloser: reader,
zipInfo: info,
zipPath: path,
}, nil
}
Expand Down Expand Up @@ -125,7 +123,7 @@ func (f *zipFS) Lstat(name string) (fs.FileInfo, error) {
return f.Stat(name)
}

func (f *zipFS) OpenZip(name string) (models.ZipFS, error) {
func (f *zipFS) OpenZip(name string, size int64) (models.ZipFS, error) {
return nil, errZipFSOpenZip
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/models/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type FS interface {
Stat(name string) (fs.FileInfo, error)
Lstat(name string) (fs.FileInfo, error)
Open(name string) (fs.ReadDirFile, error)
OpenZip(name string) (ZipFS, error)
OpenZip(name string, size int64) (ZipFS, error)
IsPathCaseSensitive(path string) (bool, error)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/models/model_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ type DirEntry struct {
func (e *DirEntry) info(fs FS, path string) (fs.FileInfo, error) {
if e.ZipFile != nil {
zipPath := e.ZipFile.Base().Path
zfs, err := fs.OpenZip(zipPath)
zfs, err := fs.OpenZip(zipPath, e.ZipFile.Base().Size)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -163,7 +163,7 @@ func (f *BaseFile) Base() *BaseFile {
func (f *BaseFile) Open(fs FS) (io.ReadCloser, error) {
if f.ZipFile != nil {
zipPath := f.ZipFile.Base().Path
zfs, err := fs.OpenZip(zipPath)
zfs, err := fs.OpenZip(zipPath, f.ZipFile.Base().Size)
if err != nil {
return nil, err
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/sqlite/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ type fileQueryRow struct {

ZipBasename null.String `db:"zip_basename"`
ZipFolderPath null.String `db:"zip_folder_path"`
ZipSize null.Int `db:"zip_size"`

FolderPath null.String `db:"parent_folder_path"`
fingerprintQueryRow
Expand Down Expand Up @@ -205,6 +206,7 @@ func (r *fileQueryRow) resolve() models.File {
ID: *basic.ZipFileID,
Path: filepath.Join(r.ZipFolderPath.String, r.ZipBasename.String),
Basename: r.ZipBasename.String,
Size: r.ZipSize.Int64,
}
}

Expand Down Expand Up @@ -461,6 +463,8 @@ func (qb *FileStore) selectDataset() *goqu.SelectDataset {
fingerprintTable.Col("fingerprint"),
zipFileTable.Col("basename").As("zip_basename"),
zipFolderTable.Col("path").As("zip_folder_path"),
// size is needed to open containing zip files
zipFileTable.Col("size").As("zip_size"),
}

cols = append(cols, videoFileQueryColumns()...)
Expand Down
4 changes: 4 additions & 0 deletions pkg/sqlite/folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type folderQueryRow struct {

ZipBasename null.String `db:"zip_basename"`
ZipFolderPath null.String `db:"zip_folder_path"`
ZipSize null.Int `db:"zip_size"`
}

func (r *folderQueryRow) resolve() *models.Folder {
Expand All @@ -61,6 +62,7 @@ func (r *folderQueryRow) resolve() *models.Folder {
ID: *ret.ZipFileID,
Path: filepath.Join(r.ZipFolderPath.String, r.ZipBasename.String),
Basename: r.ZipBasename.String,
Size: r.ZipSize.Int64,
}
}

Expand Down Expand Up @@ -148,6 +150,8 @@ func (qb *FolderStore) selectDataset() *goqu.SelectDataset {
table.Col("updated_at"),
zipFileTable.Col("basename").As("zip_basename"),
zipFolderTable.Col("path").As("zip_folder_path"),
// size is needed to open containing zip files
zipFileTable.Col("size").As("zip_size"),
}

ret := dialect.From(table).Select(cols...)
Expand Down

0 comments on commit a17199b

Please sign in to comment.