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)
}
}
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
}
+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.
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)
}
+7 -7
View File
@@ -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!")
}