Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

Commit

Permalink
Handle relative paths more correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
epsilon-phase committed Oct 13, 2023
1 parent 7a1fcf5 commit cff8d36
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
15 changes: 13 additions & 2 deletions pkg/fs/apkfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,18 @@ func (a *APKFS) Stat(path string) (fs.FileInfo, error) {
}
return &apkFSFileInfo{file: file, name: file.name[strings.LastIndex(file.name, "/"):]}, nil
}

func correctApkFSPath(path string) string {
if path == "." {
path = "/"
}
if len(path) > 3 && path[:2] == "./" {
path = path[1:]
}
path = filepath.Clean(path)
return path
}
func (a *APKFS) ReadDir(path string) ([]fs.DirEntry, error) {
path = correctApkFSPath(path)
file, ok := a.files[path]
if !ok {
return nil, fs.ErrNotExist
Expand All @@ -261,11 +271,12 @@ func (a *APKFS) ReadDir(path string) ([]fs.DirEntry, error) {
}

func (a *APKFS) Open(path string) (fs.File, error) {
path = filepath.Clean(path)
path = correctApkFSPath(path)
file, ok := a.files[path]
if !ok {
return nil, os.ErrNotExist
}

fileCopy := file.acquireCopy()
var err error
fileCopy.fileDescriptor, fileCopy.tarReader, err = a.getTarReader()
Expand Down
12 changes: 12 additions & 0 deletions pkg/fs/apkfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ func TestReadAPKFile(t *testing.T) {
require.Nil(t, err)
require.Equal(t, info.Name(), "hello")
})
t.Run("stat-alternate-workaround", func(t *testing.T) {
apkfs, err := NewAPKFS(context.Background(), "testdata/hello-2.12-r0.apk", APKFSPackage)
require.Nil(t, err)
defer apkfs.Close()
require.NotNil(t, apkfs)
file, err := apkfs.Open("./usr/bin/hello")
require.Nil(t, err)
defer file.Close()
info, err := file.Stat()
require.Nil(t, err)
require.Equal(t, info.Name(), "hello")
})
t.Run("stat-control", func(t *testing.T) {
apkfs, err := NewAPKFS(context.Background(), "testdata/hello-2.12-r0.apk", APKFSControl)
require.Nil(t, err)
Expand Down

0 comments on commit cff8d36

Please sign in to comment.