Skip to content

Commit

Permalink
tarfs: remove path/filepath usage
Browse files Browse the repository at this point in the history
This was done out of a desire to use `filepath.Rel`, and force of habit,
but had the unfortunate side-effect of mangling paths on Windows.

Closes: #1327
Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Jun 12, 2024
1 parent ecc9d8d commit e31accc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pkg/tarfs/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"archive/tar"
"io"
"io/fs"
"path/filepath"
"path"
"strings"
)

Expand Down Expand Up @@ -67,7 +67,7 @@ type dirent struct{ *tar.Header }

var _ fs.DirEntry = dirent{}

func (d dirent) Name() string { return filepath.Base(d.Header.Name) }
func (d dirent) Name() string { return path.Base(d.Header.Name) }
func (d dirent) IsDir() bool { return d.Header.FileInfo().IsDir() }
func (d dirent) Type() fs.FileMode { return d.Header.FileInfo().Mode() & fs.ModeType }
func (d dirent) Info() (fs.FileInfo, error) { return d.FileInfo(), nil }
Expand Down
20 changes: 11 additions & 9 deletions pkg/tarfs/tarfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"io/fs"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
Expand All @@ -34,7 +33,13 @@ type inode struct {
//
// This is needed any time a name is pulled from the archive.
func normPath(p string) string {
s, _ := filepath.Rel("/", filepath.Join("/", p))
// This is OK because [path.Join] is documented to call [path.Clean], which
// will remove any parent ("..") elements, and will always return a string
// of at least length 1, because the static component is length 1.
s := path.Join("/", p)[1:]
if len(s) == 0 {
return "."
}
if utf8.ValidString(s) {
return s
}
Expand Down Expand Up @@ -207,7 +212,7 @@ Again:
f.lookup[name] = i

cycle := make(map[*inode]struct{})
dir := filepath.Dir(name)
dir := path.Dir(name)
AddEnt:
switch dir {
case name:
Expand Down Expand Up @@ -530,15 +535,12 @@ func (f *FS) Sub(dir string) (fs.FS, error) {
lookup: make(map[string]int),
}
for n, i := range f.lookup {
rel, err := filepath.Rel(bp, n)
if err != nil {
// Can't be made relative.
continue
}
if strings.HasPrefix(rel, "..") {
if !strings.HasPrefix(n, bp) {
// Not in this subtree.
continue
}
// NormPath handles the root condition cleanly.
rel := normPath(strings.TrimPrefix(n, bp))
ret.lookup[rel] = i
}
return &ret, nil
Expand Down

0 comments on commit e31accc

Please sign in to comment.