From 4187598783c6a503a81ec110ffb5ebbdbaef8ccc Mon Sep 17 00:00:00 2001 From: Caleb Gardner Date: Fri, 15 Jan 2021 10:57:03 -0600 Subject: [PATCH] A couple of fixes. GetChildrenRecursively is no longer threaded so it's more consistent Fixed GetFileAtPath, specifically when getting the root dir --- file.go | 21 +++++++-------------- reader.go | 32 ++++++++++++++++++-------------- reader_test.go | 14 +++++++------- 3 files changed, 32 insertions(+), 35 deletions(-) diff --git a/file.go b/file.go index 608d80b..9fc920f 100644 --- a/file.go +++ b/file.go @@ -142,21 +142,14 @@ func (f *File) GetChildrenRecursively() (children []*File, err error) { childFolders = append(childFolders, child) } } - foldChil := make(chan []*File) - errChan := make(chan error) for _, folds := range childFolders { - go func(fil *File) { - childs, childsErr := fil.GetChildrenRecursively() - errChan <- childsErr - foldChil <- childs - }(folds) - } - for range childFolders { - err = <-errChan + var childs []*File + childs, err = folds.GetChildrenRecursively() if err != nil { + fmt.Println(err) return } - children = append(children, <-foldChil...) + children = append(children, childs...) } return } @@ -173,11 +166,11 @@ func (f *File) Path() string { //Returns nil if called on something other then a folder, OR if the path goes oustide the archive. //Allows wildcards supported by path.Match (namely * and ?) and will return the FIRST file that matches. func (f *File) GetFileAtPath(dirPath string) *File { - if dirPath == "" { - return f - } dirPath = path.Clean(dirPath) dirPath = strings.TrimPrefix(dirPath, "/") + if dirPath == "" || dirPath == "." { + return f + } if dirPath != "." && !f.IsDir() { return nil } diff --git a/reader.go b/reader.go index 4446353..05202f3 100644 --- a/reader.go +++ b/reader.go @@ -159,25 +159,24 @@ func (r *Reader) ModTime() time.Time { //ExtractTo tries to extract ALL files to the given path. This is the same as getting the root folder and extracting that. func (r *Reader) ExtractTo(path string) []error { if r.root == nil { - root, err := r.GetRootFolder() + _, err := r.GetRootFolder() if err != nil { return []error{err} } - r.root = root } return r.root.ExtractTo(path) } //GetRootFolder returns a squashfs.File that references the root directory of the squashfs archive. -func (r *Reader) GetRootFolder() (root *File, err error) { +func (r *Reader) GetRootFolder() (*File, error) { if r.root != nil { return r.root, nil } - root = new(File) mr, err := r.newMetadataReaderFromInodeRef(r.super.RootInodeRef) if err != nil { return nil, err } + var root File root.in, err = inode.ProcessInode(mr, r.super.BlockSize) if err != nil { return nil, err @@ -185,7 +184,8 @@ func (r *Reader) GetRootFolder() (root *File, err error) { root.dir = "/" root.filType = root.in.Type root.r = r - return root, nil + r.root = &root + return r.root, nil } //GetAllFiles returns a slice of ALL files and folders contained in the squashfs. @@ -243,11 +243,13 @@ func (r *Reader) FindFile(query func(*File) bool) *File { //FindAll returns all files where the given function returns true. func (r *Reader) FindAll(query func(*File) bool) (all []*File) { - root, err := r.GetRootFolder() - if err != nil { - return nil + if r.root == nil { + _, err := r.GetRootFolder() + if err != nil { + return nil + } } - fils, err := root.GetChildrenRecursively() + fils, err := r.root.GetChildrenRecursively() if err != nil { return nil } @@ -260,10 +262,12 @@ func (r *Reader) FindAll(query func(*File) bool) (all []*File) { } //GetFileAtPath will return the file at the given path. If the file cannot be found, will return nil. -func (r *Reader) GetFileAtPath(path string) *File { - dir, err := r.GetRootFolder() - if err != nil { - return nil +func (r *Reader) GetFileAtPath(filepath string) *File { + if r.root == nil { + _, err := r.GetRootFolder() + if err != nil { + return nil + } } - return dir.GetFileAtPath(path) + return r.root.GetFileAtPath(filepath) } diff --git a/reader_test.go b/reader_test.go index 285d4ed..5b4d6ff 100644 --- a/reader_test.go +++ b/reader_test.go @@ -76,14 +76,14 @@ func TestAppImage(t *testing.T) { if err != nil { t.Fatal(err) } - errs := rdr.ExtractTo(wd + "/testing/firefox") - if len(errs) > 0 { - t.Fatal(errs) + rt := rdr.GetFileAtPath("/") + fils, err := rt.GetChildrenRecursively() + if err != nil { + t.Fatal(err) + } + for _, fil := range fils { + fmt.Println(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) fmt.Println(time.Since(start)) t.Fatal("No problemo!") }