Compare commits

...

6 Commits

Author SHA1 Message Date
Caleb Gardner 80946f58e7 Fixed issue with Extended Symlinks
Removed some shadowed err's
2021-01-16 01:32:00 -06:00
Caleb Gardner 4187598783 A couple of fixes.
GetChildrenRecursively is no longer threaded so it's more consistent
Fixed GetFileAtPath, specifically when getting the root dir
2021-01-15 10:57:03 -06:00
Caleb Gardner 9cf92c4916 Removed some shadowed values (thanks gopls) 2021-01-13 11:45:10 -06:00
Caleb Gardner 407d649b3d Finished FixSymlinks (in theory) 2021-01-13 06:02:15 -06:00
Caleb Gardner dcf59a4261 The root inode is now only initialized once.
Privated File.Reader because it really shouldn't be public.
2021-01-12 01:43:03 -06:00
Caleb Gardner 1506ca0ac3 updated dependencies 2021-01-10 04:39:19 -06:00
9 changed files with 257 additions and 125 deletions
+43 -57
View File
@@ -28,14 +28,18 @@ var (
//File can be either a file or folder. When reading from a squashfs, it reads from the datablocks. //File can be either a file or folder. When reading from a squashfs, it reads from the datablocks.
//When writing, this holds the information on WHERE the file will be placed inside the archive. //When writing, this holds the information on WHERE the file will be placed inside the archive.
// //
//Implements os.FileInfo and io.ReadCloser //If copying data from a squashfs, the returned reader from io.Sys() implements io.WriterTo which
//will be significantly faster then calling Read directly.
//Ex: use io.Sys().(io.Reader) for io.Copy instead of using the File directly.
//
//Implements os.FileInfo and io.Reader
type File struct { type File struct {
Reader io.Reader reader io.Reader
Parent *File Parent *File
r *Reader //Underlying reader. When writing, will probably be an os.File. When reading this is kept nil UNTIL reading to save memory. r *Reader //Underlying reader. When writing, will probably be an os.File. When reading this is kept nil UNTIL reading to save memory.
in *inode.Inode in *inode.Inode
name string name string
path string dir string
filType int //The file's type, using inode types. filType int //The file's type, using inode types.
} }
@@ -75,20 +79,20 @@ func (f *File) ModTime() time.Time {
return time.Unix(int64(f.in.Header.ModifiedTime), 0) return time.Unix(int64(f.in.Header.ModifiedTime), 0)
} }
//Sys returns the underlying reader, file.Reader. If the reader isn't initialized, it will initialize it. //Sys returns the underlying reader. If the reader isn't initialized, it will initialize it.
//If called on something other then a file, returns nil. //If called on something other then a file, returns nil.
func (f *File) Sys() interface{} { func (f *File) Sys() interface{} {
if f.IsFile() { if !f.IsFile() {
if f.Reader == nil && f.r != nil { return nil
var err error
f.Reader, err = f.r.newFileReader(f.in)
if err != nil {
return nil
}
}
return f.Reader
} }
return nil if f.reader == nil && f.r != nil {
var err error
f.reader, err = f.r.newFileReader(f.in)
if err != nil {
return nil
}
}
return f.reader
} }
//GetChildren returns a *squashfs.File slice of every direct child of the directory. If the File is not a directory, will return ErrNotDirectory //GetChildren returns a *squashfs.File slice of every direct child of the directory. If the File is not a directory, will return ErrNotDirectory
@@ -112,7 +116,7 @@ func (f *File) GetChildren() (children []*File, err error) {
} }
fil.Parent = f fil.Parent = f
if f.name != "" { if f.name != "" {
fil.path = f.Path() fil.dir = f.Path()
} }
children = append(children, fil) children = append(children, fil)
} }
@@ -138,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, err := fil.GetChildrenRecursively() childs, err = folds.GetChildrenRecursively()
errChan <- err
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
} }
@@ -160,26 +157,23 @@ func (f *File) GetChildrenRecursively() (children []*File, err error) {
//Path returns the path of the file within the archive. //Path returns the path of the file within the archive.
func (f *File) Path() string { func (f *File) Path() string {
if f.name == "" { if f.name == "" {
return f.path return f.dir
} }
return f.path + "/" + f.name return f.dir + "/" + f.name
} }
//GetFileAtPath tries to return the File at the given path, relative to the file. //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. //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 ?). //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 == "" { dirPath = path.Clean(dirPath)
dirPath = strings.TrimPrefix(dirPath, "/")
if dirPath == "" || dirPath == "." {
return f return f
} }
dirPath = strings.TrimSuffix(strings.TrimPrefix(dirPath, "/"), "/") if dirPath != "." && !f.IsDir() {
if dirPath != "" && !f.IsDir() {
return nil return nil
} }
for strings.HasSuffix(dirPath, "./") {
//since you can TECHNICALLY have an infinite amount of ./ and it would still be valid.
dirPath = strings.TrimPrefix(dirPath, "./")
}
split := strings.Split(dirPath, "/") split := strings.Split(dirPath, "/")
if split[0] == ".." && f.name == "" { if split[0] == ".." && f.name == "" {
return nil return nil
@@ -209,7 +203,7 @@ func (f *File) IsDir() bool {
//IsSymlink returns if the file is a symlink. //IsSymlink returns if the file is a symlink.
func (f *File) IsSymlink() bool { func (f *File) IsSymlink() bool {
return f.filType == inode.SymType || f.filType == inode.ExtSymlinkType return f.filType == inode.SymType || f.filType == inode.ExtSymType
} }
//IsFile returns if the file is a file. //IsFile returns if the file is a file.
@@ -223,8 +217,8 @@ func (f *File) SymlinkPath() string {
switch f.filType { switch f.filType {
case inode.SymType: case inode.SymType:
return f.in.Info.(inode.Sym).Path return f.in.Info.(inode.Sym).Path
case inode.ExtSymlinkType: case inode.ExtSymType:
return f.in.Info.(inode.Sym).Path return f.in.Info.(inode.ExtSym).Path
default: default:
return "" return ""
} }
@@ -319,7 +313,8 @@ func (f *File) ExtractWithOptions(path string, dereferenceSymlink, unbreakSymlin
errs = append(errs, err) errs = append(errs, err)
return return
} }
fil, err := os.Open(path + "/" + f.name) var fil *os.File
fil, err = os.Open(path + "/" + f.name)
if err != nil { if err != nil {
if verbose { if verbose {
fmt.Println("Error while opening:", path+"/"+f.name) fmt.Println("Error while opening:", path+"/"+f.name)
@@ -346,7 +341,8 @@ func (f *File) ExtractWithOptions(path string, dereferenceSymlink, unbreakSymlin
errs = append(errs, err) errs = append(errs, err)
} }
} }
children, err := f.GetChildren() var children []*File
children, err = f.GetChildren()
if err != nil { if err != nil {
if verbose { if verbose {
fmt.Println("Error getting children for:", f.Path()) fmt.Println("Error getting children for:", f.Path())
@@ -370,7 +366,8 @@ func (f *File) ExtractWithOptions(path string, dereferenceSymlink, unbreakSymlin
} }
return return
case f.IsFile(): case f.IsFile():
fil, err := os.Create(path + "/" + f.name) var fil *os.File
fil, err = os.Create(path + "/" + f.name)
if os.IsExist(err) { if os.IsExist(err) {
err = os.Remove(path + "/" + f.name) err = os.Remove(path + "/" + f.name)
if err != nil { if err != nil {
@@ -398,18 +395,7 @@ func (f *File) ExtractWithOptions(path string, dereferenceSymlink, unbreakSymlin
errs = append(errs, err) errs = append(errs, err)
return return
} //Since we will be reading from the file } //Since we will be reading from the file
if f.Reader == nil && f.r != nil { _, err = io.Copy(fil, f.Sys().(io.Reader))
f.Reader, err = f.r.newFileReader(f.in)
if err != nil {
if verbose {
fmt.Println("Error while Copying data to:", path+"/"+f.name)
fmt.Println(err)
}
errs = append(errs, err)
return
}
}
_, err = io.Copy(fil, f.Reader)
if err != nil { if err != nil {
if verbose { if verbose {
fmt.Println("Error while Copying data to:", path+"/"+f.name) fmt.Println("Error while Copying data to:", path+"/"+f.name)
@@ -495,13 +481,13 @@ func (f *File) Read(p []byte) (int, error) {
return 0, io.EOF return 0, io.EOF
} }
var err error var err error
if f.Reader == nil && f.r != nil { if f.reader == nil && f.r != nil {
f.Reader, err = f.r.newFileReader(f.in) f.reader, err = f.r.newFileReader(f.in)
if err != nil { if err != nil {
return 0, err return 0, err
} }
} }
return f.Reader.Read(p) return f.reader.Read(p)
} }
//ReadDirFromInode returns a fully populated Directory from a given Inode. //ReadDirFromInode returns a fully populated Directory from a given Inode.
+3 -3
View File
@@ -10,9 +10,9 @@ import (
//FragmentEntry is an entry in the fragment table //FragmentEntry is an entry in the fragment table
type fragmentEntry struct { type fragmentEntry struct {
Start uint64 Start uint64
Size uint32 Size uint32
Unused uint32 // Unused uint32
} }
//GetFragmentDataFromInode returns the fragment data for a given inode. //GetFragmentDataFromInode returns the fragment data for a given inode.
+5 -4
View File
@@ -3,16 +3,17 @@ module github.com/CalebQ42/squashfs
go 1.15 go 1.15
require ( require (
github.com/CalebQ42/GoAppImage v0.4.0 github.com/CalebQ42/GoAppImage v0.5.0
github.com/adrg/xdg v0.2.3 // indirect github.com/adrg/xdg v0.2.3 // indirect
github.com/google/go-cmp v0.5.4 // indirect github.com/google/go-cmp v0.5.4 // indirect
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect
github.com/klauspost/compress v1.11.4 github.com/klauspost/compress v1.11.6
github.com/kr/text v0.2.0 // indirect github.com/kr/text v0.2.0 // indirect
github.com/pierrec/lz4/v4 v4.1.2 github.com/pierrec/lz4/v4 v4.1.3
github.com/smartystreets/assertions v1.2.0 // indirect github.com/smartystreets/assertions v1.2.0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/ulikunitz/xz v0.5.9 github.com/ulikunitz/xz v0.5.9
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
) )
+11 -8
View File
@@ -1,5 +1,5 @@
github.com/CalebQ42/GoAppImage v0.4.0 h1:aF+Y/vyo/RGhoyZEW1CMY6WyRWrZZO4ydsRFAtIGnaY= github.com/CalebQ42/GoAppImage v0.5.0 h1:znoKNXtliH754tS9sYwyOIg/0wFDjFN5Twc7PAh1rSM=
github.com/CalebQ42/GoAppImage v0.4.0/go.mod h1:qHudJKAn/dlkNWNnH4h1YKXp29EZ7Bppsn7sNP2HuvU= github.com/CalebQ42/GoAppImage v0.5.0/go.mod h1:qHudJKAn/dlkNWNnH4h1YKXp29EZ7Bppsn7sNP2HuvU=
github.com/adrg/xdg v0.2.2 h1:A7ZHKRz5KGOLJX/bg7IPzStryhvCzAE1wX+KWawPiAo= github.com/adrg/xdg v0.2.2 h1:A7ZHKRz5KGOLJX/bg7IPzStryhvCzAE1wX+KWawPiAo=
github.com/adrg/xdg v0.2.2/go.mod h1:7I2hH/IT30IsupOpKZ5ue7/qNi3CoKzD6tL3HwpaRMQ= github.com/adrg/xdg v0.2.2/go.mod h1:7I2hH/IT30IsupOpKZ5ue7/qNi3CoKzD6tL3HwpaRMQ=
github.com/adrg/xdg v0.2.3 h1:GxXngdYxNDkoUvZXjNJGwqZxWXi43MKbOOlA/00qZi4= github.com/adrg/xdg v0.2.3 h1:GxXngdYxNDkoUvZXjNJGwqZxWXi43MKbOOlA/00qZi4=
@@ -18,8 +18,8 @@ github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 h1:l5lAOZEym3oK3
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/klauspost/compress v1.11.4 h1:kz40R/YWls3iqT9zX9AHN3WoVsrAWVyui5sxuLqiXqU= github.com/klauspost/compress v1.11.6 h1:EgWPCW6O3n1D5n99Zq3xXBt9uCwRGvpwGOusOLNBRSQ=
github.com/klauspost/compress v1.11.4/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= github.com/klauspost/compress v1.11.6/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@@ -29,8 +29,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pierrec/lz4/v4 v4.1.2 h1:qvY3YFXRQE/XB8MlLzJH7mSzBs74eA2gg52YTk6jUPM= github.com/pierrec/lz4/v4 v4.1.3 h1:/dvQpkb0o1pVlSgKNQqfkavlnXaIK+hJ0LXsKRUN9D4=
github.com/pierrec/lz4/v4 v4.1.2/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= github.com/pierrec/lz4/v4 v4.1.3/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
@@ -39,9 +39,12 @@ github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N
github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/ulikunitz/xz v0.5.9 h1:RsKRIA2MO8x56wkkcd3LbtcE/uMszhb6DpRf+3uwa3I= github.com/ulikunitz/xz v0.5.9 h1:RsKRIA2MO8x56wkkcd3LbtcE/uMszhb6DpRf+3uwa3I=
github.com/ulikunitz/xz v0.5.9/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= github.com/ulikunitz/xz v0.5.9/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
go.lsp.dev/uri v0.3.0 h1:KcZJmh6nFIBeJzTugn5JTU6OOyG0lDOo3R9KwTxTYbo= go.lsp.dev/uri v0.3.0 h1:KcZJmh6nFIBeJzTugn5JTU6OOyG0lDOo3R9KwTxTYbo=
@@ -69,5 +72,5 @@ gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU=
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+3 -2
View File
@@ -16,7 +16,7 @@ const (
SocketType SocketType
ExtDirType ExtDirType
ExtFileType ExtFileType
ExtSymlinkType ExtSymType
ExtBlockDeviceType ExtBlockDeviceType
ExtCharDeviceType ExtCharDeviceType
ExtFifoType ExtFifoType
@@ -67,7 +67,8 @@ func NewExtendedDirectory(rdr io.Reader) (ExtDir, error) {
return inode, err return inode, err
} }
for i := uint16(0); i < inode.IndexCount; i++ { for i := uint16(0); i < inode.IndexCount; i++ {
tmp, err := NewDirectoryIndex(rdr) var tmp DirIndex
tmp, err = NewDirectoryIndex(rdr)
if err != nil { if err != nil {
return inode, err return inode, err
} }
+11 -6
View File
@@ -31,13 +31,15 @@ func ProcessInode(br io.Reader, blockSize uint32) (*Inode, error) {
} }
info = inode info = inode
case FileType: case FileType:
inode, err := NewFile(br, blockSize) var inode File
inode, err = NewFile(br, blockSize)
if err != nil { if err != nil {
return nil, err return nil, err
} }
info = inode info = inode
case SymType: case SymType:
inode, err := NewSymlink(br) var inode Sym
inode, err = NewSymlink(br)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -71,19 +73,22 @@ func ProcessInode(br io.Reader, blockSize uint32) (*Inode, error) {
} }
info = inode info = inode
case ExtDirType: case ExtDirType:
inode, err := NewExtendedDirectory(br) var inode ExtDir
inode, err = NewExtendedDirectory(br)
if err != nil { if err != nil {
return nil, err return nil, err
} }
info = inode info = inode
case ExtFileType: case ExtFileType:
inode, err := NewExtendedFile(br, blockSize) var inode ExtFile
inode, err = NewExtendedFile(br, blockSize)
if err != nil { if err != nil {
return nil, err return nil, err
} }
info = inode info = inode
case ExtSymlinkType: case ExtSymType:
inode, err := NewExtendedSymlink(br) var inode ExtSym
inode, err = NewExtendedSymlink(br)
if err != nil { if err != nil {
return nil, err return nil, err
} }
+48 -29
View File
@@ -30,6 +30,7 @@ var (
type Reader struct { type Reader struct {
r io.ReaderAt r io.ReaderAt
decompressor compression.Decompressor decompressor compression.Decompressor
root *File
fragOffsets []uint64 fragOffsets []uint64
idTable []uint32 idTable []uint32
super superblock super superblock
@@ -55,7 +56,8 @@ func NewSquashfsReader(r io.ReaderAt) (*Reader, error) {
if rdr.flags.CompressorOptions { if rdr.flags.CompressorOptions {
switch rdr.super.CompressionType { switch rdr.super.CompressionType {
case GzipCompression: case GzipCompression:
gzip, err := compression.NewGzipCompressorWithOptions(io.NewSectionReader(rdr.r, int64(binary.Size(rdr.super)), 8)) var gzip *compression.Gzip
gzip, err = compression.NewGzipCompressorWithOptions(io.NewSectionReader(rdr.r, int64(binary.Size(rdr.super)), 8))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -64,7 +66,8 @@ func NewSquashfsReader(r io.ReaderAt) (*Reader, error) {
} }
rdr.decompressor = gzip rdr.decompressor = gzip
case XzCompression: case XzCompression:
xz, err := compression.NewXzCompressorWithOptions(io.NewSectionReader(rdr.r, int64(binary.Size(rdr.super)), 8)) var xz *compression.Xz
xz, err = compression.NewXzCompressorWithOptions(io.NewSectionReader(rdr.r, int64(binary.Size(rdr.super)), 8))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -73,13 +76,15 @@ func NewSquashfsReader(r io.ReaderAt) (*Reader, error) {
} }
rdr.decompressor = xz rdr.decompressor = xz
case Lz4Compression: case Lz4Compression:
lz4, err := compression.NewLz4CompressorWithOptions(io.NewSectionReader(rdr.r, int64(binary.Size(rdr.super)), 8)) var lz4 *compression.Lz4
lz4, err = compression.NewLz4CompressorWithOptions(io.NewSectionReader(rdr.r, int64(binary.Size(rdr.super)), 8))
if err != nil { if err != nil {
return nil, err return nil, err
} }
rdr.decompressor = lz4 rdr.decompressor = lz4
case ZstdCompression: case ZstdCompression:
zstd, err := compression.NewZstdCompressorWithOptions(io.NewSectionReader(rdr.r, int64(binary.Size(rdr.super)), 4)) var zstd *compression.Zstd
zstd, err = compression.NewZstdCompressorWithOptions(io.NewSectionReader(rdr.r, int64(binary.Size(rdr.super)), 4))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -153,46 +158,56 @@ 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 {
root, err := r.GetRootFolder() if r.root == nil {
if err != nil { _, err := r.GetRootFolder()
return []error{err} if err != nil {
return []error{err}
}
} }
return 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) {
root = new(File) if r.root != nil {
return r.root, nil
}
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
} }
root.path = "/" 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.
func (r *Reader) GetAllFiles() (fils []*File, err error) { func (r *Reader) GetAllFiles() (fils []*File, err error) {
root, err := r.GetRootFolder() if r.root == nil {
if err != nil { _, err := r.GetRootFolder()
return nil, err if err != nil {
return nil, err
}
} }
return root.GetChildrenRecursively() return r.root.GetChildrenRecursively()
} }
//FindFile returns the first file (in the same order as Reader.GetAllFiles) that the given function returns true for. Returns nil if nothing is found. //FindFile returns the first file (in the same order as Reader.GetAllFiles) that the given function returns true for. Returns nil if nothing is found.
func (r *Reader) FindFile(query func(*File) bool) *File { func (r *Reader) FindFile(query func(*File) bool) *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.GetChildren() fils, err := r.root.GetChildren()
if err != nil { if err != nil {
return nil return nil
} }
@@ -228,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
} }
@@ -245,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)
} }
+8 -8
View File
@@ -16,7 +16,7 @@ import (
const ( const (
downloadURL = "https://github.com/srevinsaju/Firefox-Appimage/releases/download/firefox-v84.0.r20201221152838/firefox-84.0.r20201221152838-x86_64.AppImage" downloadURL = "https://github.com/srevinsaju/Firefox-Appimage/releases/download/firefox-v84.0.r20201221152838/firefox-84.0.r20201221152838-x86_64.AppImage"
appImageName = "firefox-84.0.r20201221152838-x86_64.AppImage" appImageName = "firefox-84.0.r20201221152838-x86_64.AppImage"
squashfsName = "balenaEtcher-1.5.113-x64.AppImage.sfs" //testing with a ArchLinux root fs from the live img squashfsName = "balenaEtcher-1.5.113-x64.AppImage.sfs"
) )
func TestSquashfs(t *testing.T) { func TestSquashfs(t *testing.T) {
@@ -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!")
} }
+125 -8
View File
@@ -33,6 +33,7 @@ type Writer struct {
symlinkTable map[string]string //[oldpath]newpath symlinkTable map[string]string //[oldpath]newpath
uidGUIDTable []int uidGUIDTable []int
compressionType int compressionType int
Flags superblockFlags
allowErrors bool allowErrors bool
} }
@@ -81,6 +82,9 @@ func (w *Writer) AddFileTo(filepath string, file *os.File) error {
if !strings.HasPrefix(filepath, "/") { if !strings.HasPrefix(filepath, "/") {
filepath = "/" + filepath filepath = "/" + filepath
} }
if w.Contains(filepath) {
return errors.New("File already exists at " + filepath)
}
var holder fileHolder var holder fileHolder
holder.path, holder.name = path.Split(filepath) holder.path, holder.name = path.Split(filepath)
holder.reader = file holder.reader = file
@@ -150,6 +154,9 @@ func (w *Writer) AddReaderTo(filepath string, reader io.Reader) error {
if !strings.HasPrefix(filepath, "/") { if !strings.HasPrefix(filepath, "/") {
filepath = "/" + filepath filepath = "/" + filepath
} }
if w.Contains(filepath) {
return errors.New("File already exists at " + filepath)
}
var holder fileHolder var holder fileHolder
holder.path, holder.name = path.Split(filepath) holder.path, holder.name = path.Split(filepath)
holder.reader = reader holder.reader = reader
@@ -171,10 +178,11 @@ func (w *Writer) Remove(filepath string) bool {
for i, fil := range files { for i, fil := range files {
if match, _ = path.Match(name, fil.name); match { if match, _ = path.Match(name, fil.name); match {
matchFound = true matchFound = true
if i != len(files)-1 { if len(w.structure[structDir]) > 1 {
w.structure[structDir] = append(w.structure[structDir][:i], w.structure[structDir][i+1:]...) w.structure[structDir][i] = w.structure[structDir][len(w.structure[structDir])-1]
w.structure[structDir] = w.structure[structDir][:len(w.structure[structDir])-1]
} else { } else {
w.structure[structDir] = w.structure[structDir][:i] w.structure[structDir] = nil
} }
} }
} }
@@ -185,11 +193,120 @@ func (w *Writer) Remove(filepath string) bool {
//FixSymlinks will scan through the squashfs archive and try to find broken symlinks and fix them. //FixSymlinks will scan through the squashfs archive and try to find broken symlinks and fix them.
//This done by replacing the symlink with the target file and then pointing other symlinks to that file. //This done by replacing the symlink with the target file and then pointing other symlinks to that file.
// //If all symlinks can be resolved, the error slice will be nil, and the bool false, otherwise all errors occured will be in the slice.
//If this is not run before writing, you may end up with broken symlinks. func (w *Writer) FixSymlinks() (errs []error, problems bool) {
func (w *Writer) FixSymlinks() error { for dir, holderSlice := range w.structure {
//TODO for i, holder := range holderSlice {
return errors.New("DON'T") if !holder.symlink {
continue
}
sym := holder.symLocation
if !path.IsAbs(holder.symLocation) {
sym = path.Join(dir, holder.symLocation)
}
if path, ok := w.symlinkTable[sym]; ok {
w.structure[dir][i].symLocation = path
continue
}
if path.IsAbs(sym) || strings.HasPrefix(sym, "../") {
var symFil *os.File
var err error
if strings.HasPrefix(sym, "../") {
holderFil, ok := holder.reader.(*os.File)
if !ok {
problems = true
errs = append(errs, errors.New("Cannot resolve symlink at "+dir+holder.name))
continue
}
symFilPath := path.Dir(holderFil.Name())
symFilPath = path.Join(symFilPath, holder.symLocation)
symFil, err = os.Open(symFilPath)
} else {
symFil, err = os.Open(sym)
}
if err != nil {
problems = true
errs = append(errs, err)
continue
}
suc := w.Remove(dir + holder.name)
if !suc {
problems = true
errs = append(errs, errors.New("Cannot resolve symlink at "+dir+holder.name))
continue
}
err = w.AddFileTo(dir+holder.name, symFil)
if err != nil {
w.structure[dir] = append(w.structure[dir], holder)
problems = true
errs = append(errs, err)
continue
}
w.symlinkTable[sym] = dir + holder.name
} else {
symHolder := w.holderAt(sym)
if symHolder != nil {
w.symlinkTable[sym] = sym
continue
}
holderFil, ok := holder.reader.(*os.File)
if !ok {
problems = true
errs = append(errs, errors.New("Cannot resolve symlink at "+dir+holder.name))
continue
}
symFilPath := path.Dir(holderFil.Name())
symFilPath = path.Join(symFilPath, holder.symLocation)
symFil, err := os.Open(symFilPath)
if err != nil {
problems = true
errs = append(errs, err)
continue
}
err = w.AddFileTo(sym, symFil)
if err != nil {
problems = true
errs = append(errs, err)
continue
}
w.symlinkTable[sym] = sym
}
}
}
return
}
func (w *Writer) holderAt(filepath string) *fileHolder {
filepath = path.Clean(filepath)
if !strings.HasPrefix(filepath, "/") {
filepath = "/" + filepath
}
dir, name := path.Split(filepath)
if holderSlice, ok := w.structure[dir]; ok {
for _, holder := range holderSlice {
if holder.name == name {
return holder
}
}
}
return nil
}
//Contains returns whether a file is present at the given filepath
func (w *Writer) Contains(filepath string) bool {
filepath = path.Clean(filepath)
if !strings.HasPrefix(filepath, "/") {
filepath = "/" + filepath
}
dir, name := path.Split(filepath)
if holderSlice, ok := w.structure[dir]; ok {
for _, holder := range holderSlice {
if holder.name == name {
return true
}
}
}
return false
} }
//WriteToFilename creates the squashfs archive with the given filepath. //WriteToFilename creates the squashfs archive with the given filepath.