Skip to content

Commit

Permalink
First version of File interface.
Browse files Browse the repository at this point in the history
This will allow you to easily find and extract files.
Extraction of whole folders coming next. (Maybe)
  • Loading branch information
CalebQ42 committed Nov 27, 2020
1 parent 8358cb2 commit 23ec7ea
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
10 changes: 7 additions & 3 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,20 @@ type File struct {

//get a File from a directory.entry
func (r *Reader) newFileFromDirEntry(entry *directory.Entry) (fil *File, err error) {
fil = new(File)
fil.in, err = r.getInodeFromEntry(entry)
if err != nil {
return nil, err
}
fil.Name = entry.Name
fil.r = r
fil.filType = fil.in.Type

return
}

//GetChildren returns a *squashfs.File slice of every direct child of the directory. If the File is not a directory, will return ErrNotDirectory
func (f *File) GetChildren() (children []*File, err error) {
children = make([]*File, 0)
if f.r == nil {
return nil, ErrNotReading
}
Expand All @@ -62,14 +63,17 @@ func (f *File) GetChildren() (children []*File, err error) {
return
}
fil.Parent = f
fil.Path = f.Path + "/" + f.Name
if f.Name != "" {
fil.Path = f.Path + "/" + f.Name
}
children = append(children, fil)
}
return
}

//GetChildrenRecursively returns ALL children. Goes down ALL folder paths.
func (f *File) GetChildrenRecursively() (children []*File, err error) {
children = make([]*File, 0)
if f.r == nil {
return nil, ErrNotReading
}
Expand Down Expand Up @@ -104,7 +108,7 @@ func (f *File) IsDir() bool {
}

//Close frees up the memory held up by the underlying reader. Should NOT be called when writing.
//When reading, Close is safe to use, as any subsequent Read calls reinitialize the reader.
//When reading, Close is safe to use, but any subsequent Read calls resets to the beginning of the file.
func (f *File) Close() error {
if f.IsDir() {
return ErrNotFile
Expand Down
4 changes: 3 additions & 1 deletion filereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ func (r *Reader) newFileReader(in *inode.Inode) (*fileReader, error) {

//Close runs Close on the data reader and frees the fragmentdata
func (f *fileReader) Close() error {
f.data.Close()
if f.data != nil {
f.data.Close()
}
f.fragmentData = nil
return nil
}
Expand Down
16 changes: 11 additions & 5 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func NewSquashfsReader(r io.ReaderAt) (*Reader, error) {

//GetRootFolder returns a squashfs.File that references the root directory of the squashfs archive.
func (r *Reader) GetRootFolder() (root *File, err error) {
root = new(File)
mr, err := r.newMetadataReaderFromInodeRef(r.super.RootInodeRef)
if err != nil {
return nil, err
Expand All @@ -86,6 +87,7 @@ func (r *Reader) GetRootFolder() (root *File, err error) {
}
root.Path = "/"
root.filType = root.in.Type
root.r = r
return root, nil
}

Expand Down Expand Up @@ -158,7 +160,7 @@ 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 {
path = "/" + strings.TrimSuffix(strings.TrimPrefix(path, "/"), "/")
path = strings.TrimSuffix(strings.TrimPrefix(path, "/"), "/")
pathDirs := strings.Split(path, "/")
dir, err := r.GetRootFolder()
if err != nil {
Expand All @@ -172,15 +174,19 @@ func (r *Reader) GetFileAtPath(path string) *File {
for _, child := range children {
if child.Name == folder {
dir = child
children, err = dir.GetChildren()
if err != nil {
return nil
if dir.IsDir() {
children, err = dir.GetChildren()
if err != nil {
return nil
}
} else {
children = make([]*File, 0)
}
break
}
}
}
if dir.Path+"/"+dir.Name == path {
if dir.Path+"/"+dir.Name == "/"+path {
return dir
}
return nil
Expand Down
37 changes: 24 additions & 13 deletions squash_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package squashfs

import (
"fmt"
"io"
"net/http"
"os"
"strings"
"testing"

goappimage "github.com/CalebQ42/GoAppImage"
Expand All @@ -27,7 +29,7 @@ func TestMain(t *testing.T) {
if err != nil {
t.Fatal(err)
}
} else {
} else if err != nil {
t.Fatal(err)
}
defer aiFil.Close()
Expand All @@ -37,22 +39,31 @@ func TestMain(t *testing.T) {
if err != nil {
t.Fatal(err)
}
extractionFil := "code-oss.desktop"
os.Remove(wd + "/testing/" + extractionFil)
desk, err := os.Create(wd + "/testing/" + extractionFil)
if err != nil {
t.Fatal(err)
}
ext := rdr.FindFile(func(fil *File) bool {
return fil.Name == extractionFil
rdr.FindAll(func(fil *File) bool {
return strings.HasSuffix(fil.Name, ".desktop")
})
if ext == nil {
t.Fatal("Cannot find file")
}
_, err = io.Copy(desk, ext)
fils, err := rdr.GetAllFiles()
if err != nil {
t.Fatal(err)
}
for _, fil := range fils {
fmt.Println(fil.Path + "/" + fil.Name)
}
// extractionFil := "code-oss.desktop"
// os.Remove(wd + "/testing/" + extractionFil)
// desk, err := os.Create(wd + "/testing/" + extractionFil)
// if err != nil {
// t.Fatal(err)
// }
// ext := rdr.GetFileAtPath(extractionFil)
// if ext == nil {
// t.Fatal("Cannot find file")
// }
// defer ext.Close()
// _, err = io.Copy(desk, ext)
// if err != nil {
// t.Fatal(err)
// }
t.Fatal("No problems here!")
}

Expand Down

0 comments on commit 23ec7ea

Please sign in to comment.