A couple of fixes.

GetChildrenRecursively is no longer threaded so it's more consistent
Fixed GetFileAtPath, specifically when getting the root dir
This commit is contained in:
Caleb Gardner
2021-01-15 10:57:03 -06:00
parent 9cf92c4916
commit 4187598783
3 changed files with 32 additions and 35 deletions
+7 -14
View File
@@ -142,21 +142,14 @@ func (f *File) GetChildrenRecursively() (children []*File, err error) {
childFolders = append(childFolders, child) childFolders = append(childFolders, child)
} }
} }
foldChil := make(chan []*File)
errChan := make(chan error)
for _, folds := range childFolders { for _, folds := range childFolders {
go func(fil *File) { var childs []*File
childs, childsErr := fil.GetChildrenRecursively() childs, err = folds.GetChildrenRecursively()
errChan <- childsErr
foldChil <- childs
}(folds)
}
for range childFolders {
err = <-errChan
if err != nil { if err != nil {
fmt.Println(err)
return return
} }
children = append(children, <-foldChil...) children = append(children, childs...)
} }
return 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. //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. //Allows wildcards supported by path.Match (namely * and ?) and will return the FIRST file that matches.
func (f *File) GetFileAtPath(dirPath string) *File { func (f *File) GetFileAtPath(dirPath string) *File {
if dirPath == "" {
return f
}
dirPath = path.Clean(dirPath) dirPath = path.Clean(dirPath)
dirPath = strings.TrimPrefix(dirPath, "/") dirPath = strings.TrimPrefix(dirPath, "/")
if dirPath == "" || dirPath == "." {
return f
}
if dirPath != "." && !f.IsDir() { if dirPath != "." && !f.IsDir() {
return nil return nil
} }
+18 -14
View File
@@ -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. //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 { func (r *Reader) ExtractTo(path string) []error {
if r.root == nil { if r.root == nil {
root, err := r.GetRootFolder() _, err := r.GetRootFolder()
if err != nil { if err != nil {
return []error{err} return []error{err}
} }
r.root = root
} }
return r.root.ExtractTo(path) return r.root.ExtractTo(path)
} }
//GetRootFolder returns a squashfs.File that references the root directory of the squashfs archive. //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 { if r.root != nil {
return r.root, nil return r.root, nil
} }
root = new(File)
mr, err := r.newMetadataReaderFromInodeRef(r.super.RootInodeRef) mr, err := r.newMetadataReaderFromInodeRef(r.super.RootInodeRef)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var root File
root.in, err = inode.ProcessInode(mr, r.super.BlockSize) root.in, err = inode.ProcessInode(mr, r.super.BlockSize)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -185,7 +184,8 @@ func (r *Reader) GetRootFolder() (root *File, err error) {
root.dir = "/" root.dir = "/"
root.filType = root.in.Type root.filType = root.in.Type
root.r = r 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. //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. //FindAll returns all files where the given function returns true.
func (r *Reader) FindAll(query func(*File) bool) (all []*File) { func (r *Reader) FindAll(query func(*File) bool) (all []*File) {
root, err := r.GetRootFolder() if r.root == nil {
if err != nil { _, err := r.GetRootFolder()
return nil if err != nil {
return nil
}
} }
fils, err := root.GetChildrenRecursively() fils, err := r.root.GetChildrenRecursively()
if err != nil { if err != nil {
return 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. //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 { func (r *Reader) GetFileAtPath(filepath string) *File {
dir, err := r.GetRootFolder() if r.root == nil {
if err != nil { _, err := r.GetRootFolder()
return nil if err != nil {
return nil
}
} }
return dir.GetFileAtPath(path) return r.root.GetFileAtPath(filepath)
} }
+7 -7
View File
@@ -76,14 +76,14 @@ func TestAppImage(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
errs := rdr.ExtractTo(wd + "/testing/firefox") rt := rdr.GetFileAtPath("/")
if len(errs) > 0 { fils, err := rt.GetChildrenRecursively()
t.Fatal(errs) 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)) fmt.Println(time.Since(start))
t.Fatal("No problemo!") t.Fatal("No problemo!")
} }