Skip to content

Commit

Permalink
Finished wildcard support.
Browse files Browse the repository at this point in the history
Realized that path.Match works perfectly for my wildcard needs.
  • Loading branch information
CalebQ42 committed Dec 7, 2020
1 parent c0f3695 commit e5d4d09
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
30 changes: 11 additions & 19 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"path"
"strings"

"github.com/CalebQ42/squashfs/internal/directory"
Expand Down Expand Up @@ -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] == ".." {
Expand All @@ -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:], "/"))
}
}
Expand Down
15 changes: 11 additions & 4 deletions squash_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package squashfs

import (
"fmt"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit e5d4d09

Please sign in to comment.