Finished wildcard support.

Realized that path.Match works perfectly for my wildcard needs.
This commit is contained in:
Caleb Gardner
2020-12-07 10:41:45 -06:00
parent c0f3695cca
commit e5d4d0902f
2 changed files with 22 additions and 23 deletions
+11 -19
View File
@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"path"
"strings" "strings"
"github.com/CalebQ42/squashfs/internal/directory" "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. //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. //Returns nil if called on something other then a folder, OR if the path goes oustide the archive.
//Allows * wildcards. //Allows wildcards supported by path.Match (namely * and ?).
func (f *File) GetFileAtPath(path string) *File { func (f *File) GetFileAtPath(dirPath string) *File {
if path == "" { if dirPath == "" {
return f return f
} }
path = strings.TrimSuffix(strings.TrimPrefix(path, "/"), "/") dirPath = strings.TrimSuffix(strings.TrimPrefix(dirPath, "/"), "/")
if path != "" && !f.IsDir() { if dirPath != "" && !f.IsDir() {
return nil 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. //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 == "" { if split[0] == ".." && f.Name == "" {
return nil return nil
} else if split[0] == ".." { } else if split[0] == ".." {
@@ -150,18 +151,9 @@ func (f *File) GetFileAtPath(path string) *File {
if err != nil { if err != nil {
return nil return nil
} }
outer:
for _, child := range children { for _, child := range children {
if strings.Contains(split[0], "*") { eq, _ := path.Match(split[0], child.Name)
wilds := strings.Split(split[0], "*") if eq {
curIndex := 0
for i, section := range wilds {
ind := strings.Index(child.Name, section)
if ind == -1 {
continue outer
}
}
} else if child.Name == split[0] {
return child.GetFileAtPath(strings.Join(split[1:], "/")) return child.GetFileAtPath(strings.Join(split[1:], "/"))
} }
} }
+11 -4
View File
@@ -1,6 +1,7 @@
package squashfs package squashfs
import ( import (
"fmt"
"io" "io"
"net/http" "net/http"
"os" "os"
@@ -58,10 +59,16 @@ func TestAppImage(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
os.RemoveAll(wd + "/testing/" + appImageName + ".d") fil := rdr.GetFileAtPath("usr/q*/QtQ*k/Extras/Priv*/q*")
root, _ := rdr.GetRootFolder() if fil == nil {
errs := root.ExtractWithOptions(wd+"/testing/"+appImageName+".d", true, os.ModePerm, true) t.Fatal("Can't find desktop file")
t.Fatal(errs) }
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) { func downloadTestAppImage(t *testing.T, dir string) {