From e5d4d0902f8e679f00a372bef313d4ff060afe7f Mon Sep 17 00:00:00 2001 From: Caleb Gardner Date: Mon, 7 Dec 2020 10:41:45 -0600 Subject: [PATCH] Finished wildcard support. Realized that path.Match works perfectly for my wildcard needs. --- file.go | 30 +++++++++++------------------- squash_test.go | 15 +++++++++++---- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/file.go b/file.go index 641e982..c7e11a0 100644 --- a/file.go +++ b/file.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "os" + "path" "strings" "github.com/CalebQ42/squashfs/internal/directory" @@ -124,20 +125,20 @@ func (f *File) Path() string { //GetFileAtPath tries to return the File at the given path, relative to the file. //Returns nil if called on something other then a folder, OR if the path goes oustide the archive. -//Allows * wildcards. -func (f *File) GetFileAtPath(path string) *File { - if path == "" { +//Allows wildcards supported by path.Match (namely * and ?). +func (f *File) GetFileAtPath(dirPath string) *File { + if dirPath == "" { return f } - path = strings.TrimSuffix(strings.TrimPrefix(path, "/"), "/") - if path != "" && !f.IsDir() { + dirPath = strings.TrimSuffix(strings.TrimPrefix(dirPath, "/"), "/") + if dirPath != "" && !f.IsDir() { return nil } - for strings.HasSuffix(path, "./") { + for strings.HasSuffix(dirPath, "./") { //since you can TECHNICALLY have an infinite amount of ./ and it would still be valid. - path = strings.TrimPrefix(path, "./") + dirPath = strings.TrimPrefix(dirPath, "./") } - split := strings.Split(path, "/") + split := strings.Split(dirPath, "/") if split[0] == ".." && f.Name == "" { return nil } else if split[0] == ".." { @@ -150,18 +151,9 @@ func (f *File) GetFileAtPath(path string) *File { if err != nil { return nil } -outer: for _, child := range children { - if strings.Contains(split[0], "*") { - wilds := strings.Split(split[0], "*") - curIndex := 0 - for i, section := range wilds { - ind := strings.Index(child.Name, section) - if ind == -1 { - continue outer - } - } - } else if child.Name == split[0] { + eq, _ := path.Match(split[0], child.Name) + if eq { return child.GetFileAtPath(strings.Join(split[1:], "/")) } } diff --git a/squash_test.go b/squash_test.go index 1614a71..b07be76 100644 --- a/squash_test.go +++ b/squash_test.go @@ -1,6 +1,7 @@ package squashfs import ( + "fmt" "io" "net/http" "os" @@ -58,10 +59,16 @@ func TestAppImage(t *testing.T) { if err != nil { t.Fatal(err) } - os.RemoveAll(wd + "/testing/" + appImageName + ".d") - root, _ := rdr.GetRootFolder() - errs := root.ExtractWithOptions(wd+"/testing/"+appImageName+".d", true, os.ModePerm, true) - t.Fatal(errs) + fil := rdr.GetFileAtPath("usr/q*/QtQ*k/Extras/Priv*/q*") + if fil == nil { + t.Fatal("Can't find desktop file") + } + fmt.Println("Fount:", fil.Path()) + // os.RemoveAll(wd + "/testing/" + appImageName + ".d") + // root, _ := rdr.GetRootFolder() + // errs := root.ExtractWithOptions(wd+"/testing/"+appImageName+".d", true, os.ModePerm, true) + // t.Fatal(errs) + t.Fatal("No problemo!") } func downloadTestAppImage(t *testing.T, dir string) {