Merge pull request #6 from stffabi/feature/support-reading-dot

Support reading "." for fs.FS
This commit is contained in:
Caleb Gardner
2021-12-02 07:40:27 -06:00
committed by GitHub
3 changed files with 23 additions and 4 deletions
+1
View File
@@ -189,6 +189,7 @@ func (r *Reader) Init() error {
return err return err
} }
r.FS = FS{ r.FS = FS{
i: i,
r: r, r: r,
name: "/", name: "/",
entries: entries, entries: entries,
+3 -2
View File
@@ -135,10 +135,11 @@ func (f File) FS() (*FS, error) {
return nil, err return nil, err
} }
return &FS{ return &FS{
entries: ents, i: f.i,
parent: f.parent,
r: f.r, r: f.r,
parent: f.parent,
name: f.name, name: f.name,
entries: ents,
}, nil }, nil
} }
+19 -2
View File
@@ -10,11 +10,13 @@ import (
"strings" "strings"
"github.com/CalebQ42/squashfs/internal/directory" "github.com/CalebQ42/squashfs/internal/directory"
"github.com/CalebQ42/squashfs/internal/inode"
) )
//FS is a fs.FS representation of a squashfs directory. //FS is a fs.FS representation of a squashfs directory.
//Implements fs.GlobFS, fs.ReadDirFS, fs.ReadFileFS, fs.StatFS, and fs.SubFS //Implements fs.GlobFS, fs.ReadDirFS, fs.ReadFileFS, fs.StatFS, and fs.SubFS
type FS struct { type FS struct {
i *inode.Inode
r *Reader r *Reader
parent *FS parent *FS
name string name string
@@ -44,6 +46,9 @@ func (f FS) Open(name string) (fs.File, error) {
} }
return f.parent.Open(strings.Join(split[1:], "/")) return f.parent.Open(strings.Join(split[1:], "/"))
} }
if split[0] == "." {
return &File{i: f.i, r: f.r, parent: f.parent, name: f.name}, nil
}
for i := 0; i < len(f.entries); i++ { for i := 0; i < len(f.entries); i++ {
if match, _ := path.Match(split[0], f.entries[i].Name); match { if match, _ := path.Match(split[0], f.entries[i].Name); match {
if len(split) == 1 { if len(split) == 1 {
@@ -183,6 +188,10 @@ func (f FS) ReadDir(name string) ([]fs.DirEntry, error) {
} }
return f.parent.ReadDir(strings.Join(split[1:], "/")) return f.parent.ReadDir(strings.Join(split[1:], "/"))
} }
if split[0] == "." {
f := &File{i: f.i, r: f.r, parent: f.parent, name: f.name}
return f.ReadDir(-1)
}
for i := 0; i < len(f.entries); i++ { for i := 0; i < len(f.entries); i++ {
if match, _ := path.Match(split[0], f.entries[i].Name); match { if match, _ := path.Match(split[0], f.entries[i].Name); match {
if len(split) == 1 { if len(split) == 1 {
@@ -202,7 +211,7 @@ func (f FS) ReadDir(name string) ([]fs.DirEntry, error) {
Err: err, Err: err,
} }
} }
out := make([]fs.DirEntry, len(f.entries)) out := make([]fs.DirEntry, len(ents))
for i, ent := range ents { for i, ent := range ents {
out[i] = &DirEntry{ out[i] = &DirEntry{
en: ent, en: ent,
@@ -299,6 +308,10 @@ func (f FS) Stat(name string) (fs.FileInfo, error) {
} }
return f.parent.Stat(strings.Join(split[1:], "/")) return f.parent.Stat(strings.Join(split[1:], "/"))
} }
if split[0] == "." {
f := &File{i: f.i, r: f.r, parent: f.parent, name: f.name}
return f.Stat()
}
for i := 0; i < len(f.entries); i++ { for i := 0; i < len(f.entries); i++ {
if match, _ := path.Match(split[0], f.entries[i].Name); match { if match, _ := path.Match(split[0], f.entries[i].Name); match {
if len(split) == 1 { if len(split) == 1 {
@@ -382,6 +395,9 @@ func (f FS) Sub(dir string) (fs.FS, error) {
} }
return f.parent.Sub(strings.Join(split[1:], "/")) return f.parent.Sub(strings.Join(split[1:], "/"))
} }
if split[0] == "." {
return f, nil
}
for i := 0; i < len(f.entries); i++ { for i := 0; i < len(f.entries); i++ {
if match, _ := path.Match(split[0], f.entries[i].Name); match { if match, _ := path.Match(split[0], f.entries[i].Name); match {
if len(split) == 1 { if len(split) == 1 {
@@ -401,7 +417,8 @@ func (f FS) Sub(dir string) (fs.FS, error) {
Err: err, Err: err,
} }
} }
return &FS{ return FS{
i: in,
r: f.r, r: f.r,
parent: &f, parent: &f,
name: f.entries[i].Name, name: f.entries[i].Name,