src: tarfs assumes forward slashes internally

This commit is contained in:
Luke Kingland 2021-04-26 16:03:34 +09:00
parent c2b216857b
commit 6cc0e67b1c
No known key found for this signature in database
GPG Key ID: 4896F75BAF2E1966
1 changed files with 15 additions and 13 deletions

View File

@ -5,7 +5,6 @@ import (
"bytes"
"io"
"io/fs"
"os"
"path"
"sort"
"strings"
@ -17,6 +16,17 @@ import (
// adapted from testing/fstest.MapFS
type FS map[string]*file
// file can be any file within the FS
type file struct {
Data []byte
Mode fs.FileMode
ModTime time.Time
Sys interface{}
}
var _ fs.FS = FS(nil)
var _ fs.File = (*openFile)(nil)
// New tar FS from a reader attached to a tarball.
func New(r io.Reader) (FS, error) {
mapfs := make(map[string]*file)
@ -72,7 +82,7 @@ func (fsys FS) Open(name string) (fs.File, error) {
if name == "." {
elem = "."
for fname, f := range fsys {
i := strings.Index(fname, string(os.PathSeparator))
i := strings.Index(fname, "/")
if i < 0 {
list = append(list, fileInfo{fname, f})
} else {
@ -80,12 +90,12 @@ func (fsys FS) Open(name string) (fs.File, error) {
}
}
} else {
elem = name[strings.LastIndex(name, string(os.PathSeparator))+1:]
prefix := name + string(os.PathSeparator)
elem = name[strings.LastIndex(name, "/")+1:]
prefix := name + "/"
for fname, f := range fsys {
if strings.HasPrefix(fname, prefix) {
felem := fname[len(prefix):]
i := strings.Index(felem, string(os.PathSeparator))
i := strings.Index(felem, "/")
if i < 0 {
list = append(list, fileInfo{felem, f})
} else {
@ -148,14 +158,6 @@ func (d *dir) ReadDir(count int) (entries []fs.DirEntry, err error) {
return list, nil
}
// file can be any file within the FS
type file struct {
Data []byte
Mode fs.FileMode
ModTime time.Time
Sys interface{}
}
// fileInfo wraps files with metadata
type fileInfo struct {
name string