Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 35d22b4bd0 | |||
| 97b12090c6 | |||
| 43fe4f91a2 | |||
| 9524a2c192 | |||
| 162b228881 | |||
| 7f87999a8f | |||
| 47c28baf87 | |||
| a298a3d7b5 | |||
| 89ec7eb0fb | |||
| d63ba47818 | |||
| 495d2345a4 | |||
| 6dfcb1cf80 | |||
| c7593eaff3 | |||
| 135403032f | |||
| 5c3bf8d528 | |||
| 1da97137a5 |
@@ -1,14 +1,14 @@
|
|||||||
# squashfs [](https://pkg.go.dev/github.com/CalebQ42/squashfs)
|
# squashfs (WIP)
|
||||||
|
|
||||||
|
[](https://pkg.go.dev/github.com/CalebQ42/squashfs) [](https://goreportcard.com/report/github.com/CalebQ42/squashfs)
|
||||||
|
|
||||||
A PURE Go library to read and write squashfs.
|
A PURE Go library to read and write squashfs.
|
||||||
|
|
||||||
Currently, you can read a squashfs and extract files (folder extraction not supported. Yet).
|
Currently has support for reading squashfs files and extracting files and folders. Supports all compression types except LZO, but additional compression options are hit or miss.
|
||||||
|
|
||||||
Special thanks to https://dr-emann.github.io/squashfs/ for some VERY important information in an easy to understand format.
|
The only major thing missing from squashfs reading is Xattr parsing.
|
||||||
|
|
||||||
|
Special thanks to <https://dr-emann.github.io/squashfs/> for some VERY important information in an easy to understand format.
|
||||||
Thanks also to [distri's squashfs library](https://github.com/distr1/distri/tree/master/internal/squashfs) as I referenced it to figure some things out (and double check others).
|
Thanks also to [distri's squashfs library](https://github.com/distr1/distri/tree/master/internal/squashfs) as I referenced it to figure some things out (and double check others).
|
||||||
|
|
||||||
# [TODO](https://github.com/CalebQ42/squashfs/projects/1?fullscreen=true)
|
## [TODO](https://github.com/CalebQ42/squashfs/projects/1?fullscreen=true)
|
||||||
|
|
||||||
# Where I'm at
|
|
||||||
|
|
||||||
* Working on the File interface that should make it easier to deal with squashfs files. I'm also trying to make them capable for when I get squashing working.
|
|
||||||
|
|||||||
+3
-9
@@ -18,10 +18,10 @@ var (
|
|||||||
//DataReader reads data from data blocks.
|
//DataReader reads data from data blocks.
|
||||||
type dataReader struct {
|
type dataReader struct {
|
||||||
r *Reader
|
r *Reader
|
||||||
offset int64 //offset relative to the beginning of the squash file
|
|
||||||
blocks []dataBlock
|
blocks []dataBlock
|
||||||
curBlock int //Which block in sizes is currently cached
|
|
||||||
curData []byte
|
curData []byte
|
||||||
|
offset int64 //offset relative to the beginning of the squash file
|
||||||
|
curBlock int //Which block in sizes is currently cached
|
||||||
curReadOffset int //offset relative to the currently cached data
|
curReadOffset int //offset relative to the currently cached data
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,12 +145,6 @@ func (d *dataReader) readCurBlock() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
//Close frees up the curData from memory
|
|
||||||
func (d *dataReader) Close() error {
|
|
||||||
d.curData = nil
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *dataReader) Read(p []byte) (int, error) {
|
func (d *dataReader) Read(p []byte) (int, error) {
|
||||||
if d.curData == nil {
|
if d.curData == nil {
|
||||||
err := d.readCurBlock()
|
err := d.readCurBlock()
|
||||||
@@ -175,12 +169,12 @@ func (d *dataReader) Read(p []byte) (int, error) {
|
|||||||
d.curReadOffset = 0
|
d.curReadOffset = 0
|
||||||
}
|
}
|
||||||
for ; read < len(p); read++ {
|
for ; read < len(p); read++ {
|
||||||
d.curReadOffset++
|
|
||||||
if d.curReadOffset < len(d.curData) {
|
if d.curReadOffset < len(d.curData) {
|
||||||
p[read] = d.curData[d.curReadOffset]
|
p[read] = d.curData[d.curReadOffset]
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
d.curReadOffset++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if read != len(p) {
|
if read != len(p) {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/CalebQ42/squashfs/internal/directory"
|
"github.com/CalebQ42/squashfs/internal/directory"
|
||||||
"github.com/CalebQ42/squashfs/internal/inode"
|
"github.com/CalebQ42/squashfs/internal/inode"
|
||||||
@@ -26,14 +27,17 @@ var (
|
|||||||
//File is the main way to interact with files within squashfs, or when putting files into a squashfs.
|
//File is the main way to interact with files within squashfs, or when putting files into a squashfs.
|
||||||
//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
|
||||||
type File struct {
|
type File struct {
|
||||||
Name string //The name of the file or folder. Root folder will not have a name ("")
|
Reader io.Reader
|
||||||
Parent *File //The parent directory. Should ALWAYS be a folder. If it's the root directory, will be nil
|
Parent *File
|
||||||
Reader io.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.
|
||||||
path string //The path to the folder the File is located in.
|
in *inode.Inode
|
||||||
r *Reader //The squashfs.Reader where this file is contained.
|
name string
|
||||||
in *inode.Inode //Underlyting inode when reading.
|
path string
|
||||||
filType int //The file's type, using inode types.
|
filType int //The file's type, using inode types.
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//get a File from a directory.entry
|
//get a File from a directory.entry
|
||||||
@@ -43,12 +47,50 @@ func (r *Reader) newFileFromDirEntry(entry *directory.Entry) (fil *File, err err
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
fil.Name = entry.Name
|
fil.name = entry.Name
|
||||||
fil.r = r
|
fil.r = r
|
||||||
fil.filType = fil.in.Type
|
fil.filType = fil.in.Type
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Name is the file's name
|
||||||
|
func (f *File) Name() string {
|
||||||
|
return f.name
|
||||||
|
}
|
||||||
|
|
||||||
|
//Size is the complete size of the file. Zero if it's not a file.
|
||||||
|
func (f *File) Size() int64 {
|
||||||
|
switch f.filType {
|
||||||
|
case inode.BasicFileType:
|
||||||
|
return int64(f.in.Info.(inode.BasicFile).Init.Size)
|
||||||
|
case inode.ExtFileType:
|
||||||
|
return int64(f.in.Info.(inode.ExtendedFile).Init.Size)
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//ModTime is the time of last modification.
|
||||||
|
func (f *File) ModTime() time.Time {
|
||||||
|
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.
|
||||||
|
//If called on something other then a file, returns nil.
|
||||||
|
func (f *File) Sys() interface{} {
|
||||||
|
if f.IsFile() {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
//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
|
||||||
func (f *File) GetChildren() (children []*File, err error) {
|
func (f *File) GetChildren() (children []*File, err error) {
|
||||||
children = make([]*File, 0)
|
children = make([]*File, 0)
|
||||||
@@ -69,7 +111,7 @@ func (f *File) GetChildren() (children []*File, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
fil.Parent = f
|
fil.Parent = f
|
||||||
if f.Name != "" {
|
if f.name != "" {
|
||||||
fil.path = f.Path()
|
fil.path = f.Path()
|
||||||
}
|
}
|
||||||
children = append(children, fil)
|
children = append(children, fil)
|
||||||
@@ -117,10 +159,10 @@ 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.path
|
||||||
}
|
}
|
||||||
return f.path + "/" + f.Name
|
return f.path + "/" + 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.
|
||||||
@@ -139,7 +181,7 @@ func (f *File) GetFileAtPath(dirPath string) *File {
|
|||||||
dirPath = strings.TrimPrefix(dirPath, "./")
|
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
|
||||||
} else if split[0] == ".." {
|
} else if split[0] == ".." {
|
||||||
if f.Parent != nil {
|
if f.Parent != nil {
|
||||||
@@ -152,7 +194,7 @@ func (f *File) GetFileAtPath(dirPath string) *File {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
for _, child := range children {
|
for _, child := range children {
|
||||||
eq, _ := path.Match(split[0], child.Name)
|
eq, _ := path.Match(split[0], child.name)
|
||||||
if eq {
|
if eq {
|
||||||
return child.GetFileAtPath(strings.Join(split[1:], "/"))
|
return child.GetFileAtPath(strings.Join(split[1:], "/"))
|
||||||
}
|
}
|
||||||
@@ -188,7 +230,8 @@ func (f *File) SymlinkPath() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//GetSymlinkFile tries to return the squashfs.File associated with the symlink
|
//GetSymlinkFile tries to return the squashfs.File associated with the symlink. If the file isn't a symlink
|
||||||
|
//or the symlink points to a location outside the archive, nil is returned.
|
||||||
func (f *File) GetSymlinkFile() *File {
|
func (f *File) GetSymlinkFile() *File {
|
||||||
if !f.IsSymlink() {
|
if !f.IsSymlink() {
|
||||||
return nil
|
return nil
|
||||||
@@ -196,11 +239,32 @@ func (f *File) GetSymlinkFile() *File {
|
|||||||
if strings.HasSuffix(f.SymlinkPath(), "/") {
|
if strings.HasSuffix(f.SymlinkPath(), "/") {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return f.r.GetFileAtPath(f.SymlinkPath())
|
return f.Parent.GetFileAtPath(f.SymlinkPath())
|
||||||
}
|
}
|
||||||
|
|
||||||
//Permission returns the os.FileMode of the File. Sets mode bits for directories and symlinks.
|
//GetSymlinkFileRecursive tries to return the squasfs.File associated with the symlink. It will recursively
|
||||||
func (f *File) Permission() os.FileMode {
|
//try to get the symlink's file. This will return either a non-symlink File, or nil.
|
||||||
|
func (f *File) GetSymlinkFileRecursive() *File {
|
||||||
|
if !f.IsSymlink() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(f.SymlinkPath(), "/") {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
sym := f
|
||||||
|
for {
|
||||||
|
sym = sym.GetSymlinkFile()
|
||||||
|
if sym == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if !sym.IsSymlink() {
|
||||||
|
return sym
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Mode returns the os.FileMode of the File. Sets mode bits for directories and symlinks.
|
||||||
|
func (f *File) Mode() os.FileMode {
|
||||||
mode := os.FileMode(f.in.Header.Permissions)
|
mode := os.FileMode(f.in.Header.Permissions)
|
||||||
switch {
|
switch {
|
||||||
case f.IsDir():
|
case f.IsDir():
|
||||||
@@ -211,23 +275,32 @@ func (f *File) Permission() os.FileMode {
|
|||||||
return mode
|
return mode
|
||||||
}
|
}
|
||||||
|
|
||||||
//ExtractTo extracts the file to the given path. This is the same as ExtractWithOptions(path, false, os.ModePerm, false).
|
//ExtractTo extracts the file to the given path. This is the same as ExtractWithOptions(path, false, false, os.ModePerm, false).
|
||||||
//Will NOT try to keep symlinks valid, folders extracted will have the permissions set by the squashfs, but the folder to make path will have full permissions (777).
|
//Will NOT try to keep symlinks valid, folders extracted will have the permissions set by the squashfs, but the folder to make path will have full permissions (777).
|
||||||
//
|
//
|
||||||
//Will try it's best to extract all files, and if any errors come up, they will be appended to the error slice that's returned.
|
//Will try it's best to extract all files, and if any errors come up, they will be appended to the error slice that's returned.
|
||||||
func (f *File) ExtractTo(path string) []error {
|
func (f *File) ExtractTo(path string) []error {
|
||||||
return f.ExtractWithOptions(path, false, os.ModePerm, false)
|
return f.ExtractWithOptions(path, false, false, os.ModePerm, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
//ExtractSymlink is similar to ExtractTo, but when it extracts a symlink, it instead extracts the file associated with the symlink in it's place.
|
||||||
|
//This is the same as ExtractWithOptions(path, true, false, os.ModePerm, false)
|
||||||
|
func (f *File) ExtractSymlink(path string) []error {
|
||||||
|
return f.ExtractWithOptions(path, true, false, os.ModePerm, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
//ExtractWithOptions will extract the file to the given path, while allowing customization on how it works. ExtractTo is the "default" options.
|
//ExtractWithOptions will extract the file to the given path, while allowing customization on how it works. ExtractTo is the "default" options.
|
||||||
//Will try it's best to extract all files, and if any errors come up, they will be appended to the error slice that's returned.
|
//Will try it's best to extract all files, and if any errors come up, they will be appended to the error slice that's returned.
|
||||||
//Should only return multiple errors if extracting a folder.
|
//Should only return multiple errors if extracting a folder.
|
||||||
//
|
//
|
||||||
|
//If dereferenceSymlink is set, instead of extracting a symlink, it will extract the file the symlink is pointed to in it's place.
|
||||||
|
//If both dereferenceSymlink and unbreakSymlink is set, dereferenceSymlink takes precendence.
|
||||||
|
//
|
||||||
//If unbreakSymlink is set, it will also try to extract the symlink's associated file. WARNING: the symlink's file may have to go up the directory to work.
|
//If unbreakSymlink is set, it will also try to extract the symlink's associated file. WARNING: the symlink's file may have to go up the directory to work.
|
||||||
//If unbreakSymlink is set and the file cannot be extracted, a ErrBrokenSymlink will be appended to the returned error slice.
|
//If unbreakSymlink is set and the file cannot be extracted, a ErrBrokenSymlink will be appended to the returned error slice.
|
||||||
//
|
//
|
||||||
//folderPerm only applies to the folders created to get to path. Folders from the archive are given the correct permissions defined by the archive.
|
//folderPerm only applies to the folders created to get to path. Folders from the archive are given the correct permissions defined by the archive.
|
||||||
func (f *File) ExtractWithOptions(path string, unbreakSymlink bool, folderPerm os.FileMode, verbose bool) (errs []error) {
|
func (f *File) ExtractWithOptions(path string, dereferenceSymlink, unbreakSymlink bool, folderPerm os.FileMode, verbose bool) (errs []error) {
|
||||||
errs = make([]error, 0)
|
errs = make([]error, 0)
|
||||||
err := os.MkdirAll(path, folderPerm)
|
err := os.MkdirAll(path, folderPerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -235,21 +308,21 @@ func (f *File) ExtractWithOptions(path string, unbreakSymlink bool, folderPerm o
|
|||||||
}
|
}
|
||||||
switch {
|
switch {
|
||||||
case f.IsDir():
|
case f.IsDir():
|
||||||
if f.Name != "" {
|
if f.name != "" {
|
||||||
//TODO: check if folder is present, and if so, try to set it's permission
|
//TODO: check if folder is present, and if so, try to set it's permission
|
||||||
err = os.Mkdir(path+"/"+f.Name, os.ModePerm)
|
err = os.Mkdir(path+"/"+f.name, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if verbose {
|
if verbose {
|
||||||
fmt.Println("Error while making: ", path+"/"+f.Name)
|
fmt.Println("Error while making: ", path+"/"+f.name)
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fil, err := os.Open(path + "/" + f.Name)
|
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)
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
@@ -264,10 +337,10 @@ func (f *File) ExtractWithOptions(path string, unbreakSymlink bool, folderPerm o
|
|||||||
// }
|
// }
|
||||||
// errs = append(errs, err)
|
// errs = append(errs, err)
|
||||||
// }
|
// }
|
||||||
err = fil.Chmod(f.Permission())
|
err = fil.Chmod(f.Mode())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if verbose {
|
if verbose {
|
||||||
fmt.Println("Error while changing owner:", path+"/"+f.Name)
|
fmt.Println("Error while changing owner:", path+"/"+f.name)
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
@@ -283,13 +356,12 @@ func (f *File) ExtractWithOptions(path string, unbreakSymlink bool, folderPerm o
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
finishChan := make(chan []error)
|
finishChan := make(chan []error)
|
||||||
defer close(finishChan)
|
|
||||||
for _, child := range children {
|
for _, child := range children {
|
||||||
go func(child *File) {
|
go func(child *File) {
|
||||||
if f.Name == "" {
|
if f.name == "" {
|
||||||
finishChan <- child.ExtractWithOptions(path, unbreakSymlink, folderPerm, verbose)
|
finishChan <- child.ExtractWithOptions(path, dereferenceSymlink, unbreakSymlink, folderPerm, verbose)
|
||||||
} else {
|
} else {
|
||||||
finishChan <- child.ExtractWithOptions(path+"/"+f.Name, unbreakSymlink, folderPerm, verbose)
|
finishChan <- child.ExtractWithOptions(path+"/"+f.name, dereferenceSymlink, unbreakSymlink, folderPerm, verbose)
|
||||||
}
|
}
|
||||||
}(child)
|
}(child)
|
||||||
}
|
}
|
||||||
@@ -298,21 +370,21 @@ func (f *File) ExtractWithOptions(path string, unbreakSymlink bool, folderPerm o
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
case f.IsFile():
|
case f.IsFile():
|
||||||
fil, err := os.Create(path + "/" + f.Name)
|
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 {
|
||||||
if verbose {
|
if verbose {
|
||||||
fmt.Println("Error while making:", path+"/"+f.Name)
|
fmt.Println("Error while making:", path+"/"+f.name)
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fil, err = os.Create(path + "/" + f.Name)
|
fil, err = os.Create(path + "/" + f.name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if verbose {
|
if verbose {
|
||||||
fmt.Println("Error while making:", path+"/"+f.Name)
|
fmt.Println("Error while making:", path+"/"+f.name)
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
@@ -320,7 +392,7 @@ func (f *File) ExtractWithOptions(path string, unbreakSymlink bool, folderPerm o
|
|||||||
}
|
}
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
if verbose {
|
if verbose {
|
||||||
fmt.Println("Error while making:", path+"/"+f.Name)
|
fmt.Println("Error while making:", path+"/"+f.name)
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
@@ -329,13 +401,12 @@ func (f *File) ExtractWithOptions(path string, unbreakSymlink bool, folderPerm o
|
|||||||
_, err = io.Copy(fil, f)
|
_, err = io.Copy(fil, f)
|
||||||
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)
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
f.Close()
|
|
||||||
fil.Chown(int(f.r.idTable[f.in.Header.UID]), int(f.r.idTable[f.in.Header.GID]))
|
fil.Chown(int(f.r.idTable[f.in.Header.UID]), int(f.r.idTable[f.in.Header.GID]))
|
||||||
//don't mention anything when it fails. Because it fails often. Probably has something to do about uid & gid 0
|
//don't mention anything when it fails. Because it fails often. Probably has something to do about uid & gid 0
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
@@ -346,10 +417,10 @@ func (f *File) ExtractWithOptions(path string, unbreakSymlink bool, folderPerm o
|
|||||||
// errs = append(errs, err)
|
// errs = append(errs, err)
|
||||||
// return
|
// return
|
||||||
// }
|
// }
|
||||||
err = fil.Chmod(f.Permission())
|
err = fil.Chmod(f.Mode())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if verbose {
|
if verbose {
|
||||||
fmt.Println("Error while setting permissions for:", path+"/"+f.Name)
|
fmt.Println("Error while setting permissions for:", path+"/"+f.name)
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
@@ -357,27 +428,48 @@ func (f *File) ExtractWithOptions(path string, unbreakSymlink bool, folderPerm o
|
|||||||
return
|
return
|
||||||
case f.IsSymlink():
|
case f.IsSymlink():
|
||||||
symPath := f.SymlinkPath()
|
symPath := f.SymlinkPath()
|
||||||
if unbreakSymlink {
|
if dereferenceSymlink {
|
||||||
fil := f.GetSymlinkFile()
|
fil := f.GetSymlinkFile()
|
||||||
if fil != nil {
|
if fil == nil {
|
||||||
symPath = path + "/" + symPath
|
if verbose {
|
||||||
paths := strings.Split(symPath, "/")
|
fmt.Println("Symlink path(", symPath, ") is outside the archive:"+path+"/"+f.name)
|
||||||
extracSymErrs := fil.ExtractWithOptions(strings.Join(paths[:len(paths)-1], "/"), unbreakSymlink, folderPerm, verbose)
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fil.name = f.name
|
||||||
|
extracSymErrs := fil.ExtractWithOptions(path, dereferenceSymlink, unbreakSymlink, folderPerm, verbose)
|
||||||
if len(extracSymErrs) > 0 {
|
if len(extracSymErrs) > 0 {
|
||||||
if verbose {
|
if verbose {
|
||||||
fmt.Println("Error(s) while extracting the symlink's file:", path+"/"+f.Name)
|
fmt.Println("Error(s) while extracting the symlink's file:", path+"/"+f.name)
|
||||||
fmt.Println(extracSymErrs)
|
fmt.Println(extracSymErrs)
|
||||||
}
|
}
|
||||||
errs = append(errs, extracSymErrs...)
|
errs = append(errs, extracSymErrs...)
|
||||||
}
|
}
|
||||||
} else if verbose {
|
return
|
||||||
fmt.Println("Symlink path(", symPath, ") is outside the archive:"+path+"/"+f.Name)
|
} else if unbreakSymlink {
|
||||||
|
fil := f.GetSymlinkFile()
|
||||||
|
if fil != nil {
|
||||||
|
symPath = path + "/" + symPath
|
||||||
|
paths := strings.Split(symPath, "/")
|
||||||
|
extracSymErrs := fil.ExtractWithOptions(strings.Join(paths[:len(paths)-1], "/"), dereferenceSymlink, unbreakSymlink, folderPerm, verbose)
|
||||||
|
if len(extracSymErrs) > 0 {
|
||||||
|
if verbose {
|
||||||
|
fmt.Println("Error(s) while extracting the symlink's file:", path+"/"+f.name)
|
||||||
|
fmt.Println(extracSymErrs)
|
||||||
|
}
|
||||||
|
errs = append(errs, extracSymErrs...)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if verbose {
|
||||||
|
fmt.Println("Symlink path(", symPath, ") is outside the archive:"+path+"/"+f.name)
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = os.Symlink(f.SymlinkPath(), path+"/"+f.Name)
|
err = os.Symlink(f.SymlinkPath(), path+"/"+f.name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if verbose {
|
if verbose {
|
||||||
fmt.Println("Error while making symlink:", path+"/"+f.Name)
|
fmt.Println("Error while making symlink:", path+"/"+f.name)
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
@@ -386,21 +478,6 @@ func (f *File) ExtractWithOptions(path string, unbreakSymlink bool, folderPerm o
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//Close frees up the memory held up by the underlying reader. Should NOT be called when writing.
|
|
||||||
//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
|
|
||||||
}
|
|
||||||
if f.Reader != nil {
|
|
||||||
if closer, is := f.Reader.(io.Closer); is {
|
|
||||||
closer.Close()
|
|
||||||
}
|
|
||||||
f.Reader = nil
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//Read from the file. Doesn't do anything fancy, just pases it to the underlying io.Reader. If a directory, return io.EOF.
|
//Read from the file. Doesn't do anything fancy, just pases it to the underlying io.Reader. If a directory, return io.EOF.
|
||||||
func (f *File) Read(p []byte) (int, error) {
|
func (f *File) Read(p []byte) (int, error) {
|
||||||
if f.IsDir() {
|
if f.IsDir() {
|
||||||
|
|||||||
@@ -57,15 +57,6 @@ func (r *Reader) newFileReader(in *inode.Inode) (*fileReader, error) {
|
|||||||
return &rdr, nil
|
return &rdr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//Close runs Close on the data reader and frees the fragmentdata
|
|
||||||
func (f *fileReader) Close() error {
|
|
||||||
if f.data != nil {
|
|
||||||
f.data.Close()
|
|
||||||
}
|
|
||||||
f.fragmentData = nil
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fileReader) Read(p []byte) (int, error) {
|
func (f *fileReader) Read(p []byte) (int, error) {
|
||||||
if f.fragOnly {
|
if f.fragOnly {
|
||||||
n, err := bytes.NewBuffer(f.fragmentData[f.read:]).Read(p)
|
n, err := bytes.NewBuffer(f.fragmentData[f.read:]).Read(p)
|
||||||
|
|||||||
@@ -7,9 +7,11 @@ require (
|
|||||||
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/kr/text v0.2.0 // indirect
|
github.com/kr/text v0.2.0 // indirect
|
||||||
|
github.com/pierrec/lz4/v4 v4.1.2
|
||||||
github.com/smartystreets/assertions v1.2.0 // indirect
|
github.com/smartystreets/assertions v1.2.0 // indirect
|
||||||
github.com/ulikunitz/xz v0.5.8
|
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-20200615113413-eeeca48fe776 // indirect
|
||||||
|
|||||||
@@ -18,6 +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.4/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=
|
||||||
@@ -27,6 +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.2/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=
|
||||||
@@ -38,8 +42,8 @@ github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9
|
|||||||
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/ulikunitz/xz v0.5.8 h1:ERv8V6GKqVi23rgu5cj9pVfVzJbOqAY2Ntl88O6c2nQ=
|
github.com/ulikunitz/xz v0.5.9 h1:RsKRIA2MO8x56wkkcd3LbtcE/uMszhb6DpRf+3uwa3I=
|
||||||
github.com/ulikunitz/xz v0.5.8/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=
|
||||||
go.lsp.dev/uri v0.3.0/go.mod h1:P5sbO1IQR+qySTWOCnhnK7phBx+W3zbLqSMDJNTw88I=
|
go.lsp.dev/uri v0.3.0/go.mod h1:P5sbO1IQR+qySTWOCnhnK7phBx+W3zbLqSMDJNTw88I=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
|
||||||
|
|||||||
@@ -3,11 +3,37 @@ package compression
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"compress/zlib"
|
"compress/zlib"
|
||||||
|
"encoding/binary"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type gzipInit struct {
|
||||||
|
CompressionLevel int32
|
||||||
|
WindowSize int16
|
||||||
|
Strategies int16
|
||||||
|
}
|
||||||
|
|
||||||
//Gzip is a decompressor for gzip type compression. Uses zlib for compression and decompression
|
//Gzip is a decompressor for gzip type compression. Uses zlib for compression and decompression
|
||||||
type Gzip struct{}
|
type Gzip struct {
|
||||||
|
CompressionLevel int32
|
||||||
|
HasCustomWindow bool
|
||||||
|
HasStrategies bool
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewGzipCompressorWithOptions creates a new gzip compressor/decompressor with options read from the given reader.
|
||||||
|
func NewGzipCompressorWithOptions(r io.Reader) (*Gzip, error) {
|
||||||
|
var gzip Gzip
|
||||||
|
var init gzipInit
|
||||||
|
err := binary.Read(r, binary.LittleEndian, &init)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
gzip.CompressionLevel = init.CompressionLevel
|
||||||
|
//TODO: proper support for window size and strategies
|
||||||
|
gzip.HasCustomWindow = init.WindowSize != 15
|
||||||
|
gzip.HasStrategies = init.Strategies != 0 && init.Strategies != 1
|
||||||
|
return &gzip, nil
|
||||||
|
}
|
||||||
|
|
||||||
//Decompress reads the entirety of the given reader and returns it uncompressed as a byte slice.
|
//Decompress reads the entirety of the given reader and returns it uncompressed as a byte slice.
|
||||||
func (g *Gzip) Decompress(r io.Reader) ([]byte, error) {
|
func (g *Gzip) Decompress(r io.Reader) ([]byte, error) {
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package compression
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/binary"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/pierrec/lz4/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
//Lz4 is a Lz4 Compressor/Decompressor
|
||||||
|
type Lz4 struct {
|
||||||
|
HC bool
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewLz4CompressorWithOptions creates a new lz4 compressor/decompressor with options read from the given reader.
|
||||||
|
func NewLz4CompressorWithOptions(r io.Reader) (*Lz4, error) {
|
||||||
|
var lz4 Lz4
|
||||||
|
var init struct {
|
||||||
|
Version int32
|
||||||
|
Flags int32
|
||||||
|
}
|
||||||
|
err := binary.Read(r, binary.LittleEndian, &init)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
lz4.HC = init.Flags == 1
|
||||||
|
return &lz4, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//Decompress decompresses all data from r and returns the uncompressed bytes
|
||||||
|
func (l *Lz4) Decompress(r io.Reader) ([]byte, error) {
|
||||||
|
rdr := lz4.NewReader(r)
|
||||||
|
var buf bytes.Buffer
|
||||||
|
_, err := io.Copy(&buf, rdr)
|
||||||
|
return buf.Bytes(), err
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package compression
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/binary"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/klauspost/compress/zstd"
|
||||||
|
)
|
||||||
|
|
||||||
|
//Zstd is a zstd compressor/decompressor
|
||||||
|
type Zstd struct {
|
||||||
|
CompressionLevel int32
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewZstdCompressorWithOptions creates a new Zstd with options read from the given reader
|
||||||
|
func NewZstdCompressorWithOptions(r io.Reader) (*Zstd, error) {
|
||||||
|
var zstd Zstd
|
||||||
|
err := binary.Read(r, binary.LittleEndian, &zstd)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &zstd, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//Decompress decompresses all data from the reader and returns the uncompressed data
|
||||||
|
func (z *Zstd) Decompress(r io.Reader) ([]byte, error) {
|
||||||
|
rdr, err := zstd.NewReader(r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rdr.Close()
|
||||||
|
var buf bytes.Buffer
|
||||||
|
_, err = io.Copy(&buf, rdr)
|
||||||
|
return buf.Bytes(), err
|
||||||
|
}
|
||||||
+1
-1
@@ -16,9 +16,9 @@ type metadata struct {
|
|||||||
//MetadataReader is a block reader for metadata. It will automatically read the next block, when it reaches the end of a block.
|
//MetadataReader is a block reader for metadata. It will automatically read the next block, when it reaches the end of a block.
|
||||||
type metadataReader struct {
|
type metadataReader struct {
|
||||||
s *Reader
|
s *Reader
|
||||||
offset int64
|
|
||||||
headers []*metadata
|
headers []*metadata
|
||||||
data []byte
|
data []byte
|
||||||
|
offset int64
|
||||||
readOffset int
|
readOffset int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/CalebQ42/squashfs/internal/compression"
|
"github.com/CalebQ42/squashfs/internal/compression"
|
||||||
"github.com/CalebQ42/squashfs/internal/inode"
|
"github.com/CalebQ42/squashfs/internal/inode"
|
||||||
@@ -21,20 +22,23 @@ var (
|
|||||||
errIncompatibleCompression = errors.New("Compression type unsupported")
|
errIncompatibleCompression = errors.New("Compression type unsupported")
|
||||||
//ErrCompressorOptions is returned if compressor options is present. It's not currently supported.
|
//ErrCompressorOptions is returned if compressor options is present. It's not currently supported.
|
||||||
errCompressorOptions = errors.New("Compressor options is not currently supported")
|
errCompressorOptions = errors.New("Compressor options is not currently supported")
|
||||||
|
//ErrOptions is returned when compression options that I haven't tested is set. When this is returned, the Reader is also returned.
|
||||||
|
ErrOptions = errors.New("Possibly incompatible compressor options")
|
||||||
)
|
)
|
||||||
|
|
||||||
//Reader processes and reads a squashfs archive.
|
//Reader processes and reads a squashfs archive.
|
||||||
type Reader struct {
|
type Reader struct {
|
||||||
r io.ReaderAt
|
r io.ReaderAt
|
||||||
super superblock
|
|
||||||
flags superblockFlags
|
|
||||||
decompressor compression.Decompressor
|
decompressor compression.Decompressor
|
||||||
fragOffsets []uint64
|
fragOffsets []uint64
|
||||||
idTable []uint32
|
idTable []uint32
|
||||||
|
super superblock
|
||||||
|
flags superblockFlags
|
||||||
}
|
}
|
||||||
|
|
||||||
//NewSquashfsReader returns a new squashfs.Reader from an io.ReaderAt
|
//NewSquashfsReader returns a new squashfs.Reader from an io.ReaderAt
|
||||||
func NewSquashfsReader(r io.ReaderAt) (*Reader, error) {
|
func NewSquashfsReader(r io.ReaderAt) (*Reader, error) {
|
||||||
|
hasUnsupportedOptions := false
|
||||||
var rdr Reader
|
var rdr Reader
|
||||||
rdr.r = r
|
rdr.r = r
|
||||||
err := binary.Read(io.NewSectionReader(rdr.r, 0, int64(binary.Size(rdr.super))), binary.LittleEndian, &rdr.super)
|
err := binary.Read(io.NewSectionReader(rdr.r, 0, int64(binary.Size(rdr.super))), binary.LittleEndian, &rdr.super)
|
||||||
@@ -50,8 +54,17 @@ func NewSquashfsReader(r io.ReaderAt) (*Reader, error) {
|
|||||||
rdr.flags = rdr.super.GetFlags()
|
rdr.flags = rdr.super.GetFlags()
|
||||||
if rdr.flags.CompressorOptions {
|
if rdr.flags.CompressorOptions {
|
||||||
switch rdr.super.CompressionType {
|
switch rdr.super.CompressionType {
|
||||||
case xzCompression:
|
case GzipCompression:
|
||||||
xz, err := compression.NewXzCompressorWithOptions(io.NewSectionReader(rdr.r, int64(binary.Size(rdr.super)), 1000)) //1000 is technically too much, but it's just an easy way to do it.
|
gzip, err := compression.NewGzipCompressorWithOptions(io.NewSectionReader(rdr.r, int64(binary.Size(rdr.super)), 8))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if gzip.HasCustomWindow || gzip.HasStrategies {
|
||||||
|
hasUnsupportedOptions = true
|
||||||
|
}
|
||||||
|
rdr.decompressor = gzip
|
||||||
|
case XzCompression:
|
||||||
|
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
|
||||||
}
|
}
|
||||||
@@ -59,17 +72,36 @@ func NewSquashfsReader(r io.ReaderAt) (*Reader, error) {
|
|||||||
return nil, errors.New("XZ compression options has filters. These are not yet supported")
|
return nil, errors.New("XZ compression options has filters. These are not yet supported")
|
||||||
}
|
}
|
||||||
rdr.decompressor = xz
|
rdr.decompressor = xz
|
||||||
|
case Lz4Compression:
|
||||||
|
lz4, err := compression.NewLz4CompressorWithOptions(io.NewSectionReader(rdr.r, int64(binary.Size(rdr.super)), 8))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if lz4.HC {
|
||||||
|
hasUnsupportedOptions = true
|
||||||
|
}
|
||||||
|
rdr.decompressor = lz4
|
||||||
|
case ZstdCompression:
|
||||||
|
zstd, err := compression.NewZstdCompressorWithOptions(io.NewSectionReader(rdr.r, int64(binary.Size(rdr.super)), 4))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
rdr.decompressor = zstd
|
||||||
default:
|
default:
|
||||||
return nil, errCompressorOptions
|
return nil, errIncompatibleCompression
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
switch rdr.super.CompressionType {
|
switch rdr.super.CompressionType {
|
||||||
case gzipCompression:
|
case GzipCompression:
|
||||||
rdr.decompressor = &compression.Gzip{}
|
rdr.decompressor = &compression.Gzip{}
|
||||||
case lzmaCompression:
|
case LzmaCompression:
|
||||||
rdr.decompressor = &compression.Lzma{}
|
rdr.decompressor = &compression.Lzma{}
|
||||||
case xzCompression:
|
case XzCompression:
|
||||||
rdr.decompressor = &compression.Xz{}
|
rdr.decompressor = &compression.Xz{}
|
||||||
|
case Lz4Compression:
|
||||||
|
rdr.decompressor = &compression.Lz4{}
|
||||||
|
case ZstdCompression:
|
||||||
|
rdr.decompressor = &compression.Zstd{}
|
||||||
default:
|
default:
|
||||||
//TODO: all compression types.
|
//TODO: all compression types.
|
||||||
return nil, errIncompatibleCompression
|
return nil, errIncompatibleCompression
|
||||||
@@ -111,9 +143,26 @@ func NewSquashfsReader(r io.ReaderAt) (*Reader, error) {
|
|||||||
}
|
}
|
||||||
unread -= read
|
unread -= read
|
||||||
}
|
}
|
||||||
|
if hasUnsupportedOptions {
|
||||||
|
return &rdr, ErrOptions
|
||||||
|
}
|
||||||
return &rdr, nil
|
return &rdr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//ModTime is the last time the file was modified/created.
|
||||||
|
func (r *Reader) ModTime() time.Time {
|
||||||
|
return time.Unix(int64(r.super.CreationTime), 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
//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 {
|
||||||
|
root, err := r.GetRootFolder()
|
||||||
|
if err != nil {
|
||||||
|
return []error{err}
|
||||||
|
}
|
||||||
|
return 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() (root *File, err error) {
|
||||||
root = new(File)
|
root = new(File)
|
||||||
|
|||||||
+23
-14
@@ -11,12 +11,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
downloadURL = "https://github.com/Swordfish90/cool-retro-term/releases/download/1.1.1/Cool-Retro-Term-1.1.1-x86_64.AppImage"
|
downloadURL = "https://github.com/srevinsaju/Firefox-Appimage/releases/download/firefox-v84.0.r20201221152838/firefox-84.0.r20201221152838-x86_64.AppImage"
|
||||||
appImageName = "Cool-Retro-Term.AppImage"
|
appImageName = "firefox-84.0.r20201221152838-x86_64.AppImage"
|
||||||
squashfsName = "airootfs.sfs" //testing with a ArchLinux root fs from the live img
|
squashfsName = "balenaEtcher-1.5.113-x64.AppImage.sfs" //testing with a ArchLinux root fs from the live img
|
||||||
)
|
)
|
||||||
|
|
||||||
//Right now, don't use. Arch linux sfs uses XZ compression and when tested, most files just completely fail to extract.
|
|
||||||
func TestSquashfs(t *testing.T) {
|
func TestSquashfs(t *testing.T) {
|
||||||
wd, err := os.Getwd()
|
wd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -30,10 +29,20 @@ func TestSquashfs(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
os.RemoveAll(wd + "/testing/" + squashfsName + ".d")
|
fmt.Println("stuff", rdr.super.CompressionType)
|
||||||
root, _ := rdr.GetRootFolder()
|
fil := rdr.GetFileAtPath("*.desktop")
|
||||||
errs := root.ExtractWithOptions(wd+"/testing/"+squashfsName+".d", false, os.ModePerm, true)
|
if fil == nil {
|
||||||
|
t.Fatal("Can't find desktop fil")
|
||||||
|
}
|
||||||
|
errs := fil.ExtractTo(wd + "/testing")
|
||||||
|
if len(errs) > 0 {
|
||||||
t.Fatal(errs)
|
t.Fatal(errs)
|
||||||
|
}
|
||||||
|
errs = rdr.ExtractTo(wd + "/testing/" + squashfsName + ".d")
|
||||||
|
if len(errs) > 0 {
|
||||||
|
t.Fatal(errs)
|
||||||
|
}
|
||||||
|
t.Fatal("No Problems")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAppImage(t *testing.T) {
|
func TestAppImage(t *testing.T) {
|
||||||
@@ -59,11 +68,11 @@ func TestAppImage(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
fil := rdr.GetFileAtPath("usr/q*/QtQ*k/Extras/Priv*/q*")
|
os.RemoveAll(wd + "testing/firefox")
|
||||||
if fil == nil {
|
errs := rdr.ExtractTo(wd + "/testing/firefox")
|
||||||
t.Fatal("Can't find desktop file")
|
if len(errs) > 0 {
|
||||||
|
t.Fatal(errs)
|
||||||
}
|
}
|
||||||
fmt.Println("Fount:", fil.Path())
|
|
||||||
// os.RemoveAll(wd + "/testing/" + appImageName + ".d")
|
// os.RemoveAll(wd + "/testing/" + appImageName + ".d")
|
||||||
// root, _ := rdr.GetRootFolder()
|
// root, _ := rdr.GetRootFolder()
|
||||||
// errs := root.ExtractWithOptions(wd+"/testing/"+appImageName+".d", true, os.ModePerm, true)
|
// errs := root.ExtractWithOptions(wd+"/testing/"+appImageName+".d", true, os.ModePerm, true)
|
||||||
@@ -72,15 +81,15 @@ func TestAppImage(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func downloadTestAppImage(t *testing.T, dir string) {
|
func downloadTestAppImage(t *testing.T, dir string) {
|
||||||
//seems to time out on slow connections. Might fix that at some point... or not
|
//seems to time out on slow connections. Might fix that at some point... or not. It's just a test...
|
||||||
os.Mkdir(dir, 0777)
|
os.Mkdir(dir, os.ModePerm)
|
||||||
appImage, err := os.Create(dir + "/" + appImageName)
|
appImage, err := os.Create(dir + "/" + appImageName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer appImage.Close()
|
defer appImage.Close()
|
||||||
check := http.Client{
|
check := http.Client{
|
||||||
CheckRedirect: func(r *http.Request, via []*http.Request) error {
|
CheckRedirect: func(r *http.Request, _ []*http.Request) error {
|
||||||
r.URL.Opaque = r.URL.Path
|
r.URL.Opaque = r.URL.Path
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
|
|||||||
+7
-6
@@ -1,12 +1,13 @@
|
|||||||
package squashfs
|
package squashfs
|
||||||
|
|
||||||
|
//The types of compression supported by squashfs.
|
||||||
const (
|
const (
|
||||||
gzipCompression = 1 + iota
|
GzipCompression = 1 + iota
|
||||||
lzmaCompression
|
LzmaCompression
|
||||||
lzoCompression
|
LzoCompression
|
||||||
xzCompression
|
XzCompression
|
||||||
lz4Compression
|
Lz4Compression
|
||||||
zstdCompression
|
ZstdCompression
|
||||||
)
|
)
|
||||||
|
|
||||||
//Superblock contains important information about a squashfs file. Located at the very front of the archive.
|
//Superblock contains important information about a squashfs file. Located at the very front of the archive.
|
||||||
|
|||||||
@@ -0,0 +1,181 @@
|
|||||||
|
package squashfs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/CalebQ42/squashfs/internal/inode"
|
||||||
|
)
|
||||||
|
|
||||||
|
//Writer is an interface to write a squashfs. Doesn't write until you call Write (TODO: maybe not do Write...).
|
||||||
|
//If AllowErrors is true, when errors are encountered, it just prints to the log instead of failing.
|
||||||
|
type Writer struct {
|
||||||
|
files map[string][]*File
|
||||||
|
symlinkTable map[string]string //[oldpath]newpath
|
||||||
|
symTableTemp map[string]string
|
||||||
|
directories []string
|
||||||
|
compression int
|
||||||
|
ResolveSymlinks bool
|
||||||
|
AllowErrors bool
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewWriter creates a new squashfs.Writer with the default settings (gzip compression, autoresolving symlinks, and allowErrors)
|
||||||
|
func NewWriter() (*Writer, error) {
|
||||||
|
return NewWriterWithOptions(true, true, GzipCompression)
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewWriterWithOptions creates a new squashfs.Writer with the given options.
|
||||||
|
//ResolveSymlinks tries to make sure symlinks aren't broken. It will either try to make the link's location work
|
||||||
|
func NewWriterWithOptions(resolveSymlinks, allowErrors bool, compressionType int) (*Writer, error) {
|
||||||
|
if compressionType < 0 || compressionType > 6 {
|
||||||
|
return nil, errors.New("Incorrect compression type")
|
||||||
|
}
|
||||||
|
if compressionType == 3 {
|
||||||
|
return nil, errors.New("Lzo compression is not currently supported")
|
||||||
|
}
|
||||||
|
out := Writer{
|
||||||
|
files: map[string][]*File{
|
||||||
|
"/": make([]*File, 0),
|
||||||
|
},
|
||||||
|
ResolveSymlinks: resolveSymlinks,
|
||||||
|
AllowErrors: allowErrors,
|
||||||
|
compression: compressionType,
|
||||||
|
}
|
||||||
|
if resolveSymlinks {
|
||||||
|
out.symlinkTable = make(map[string]string)
|
||||||
|
}
|
||||||
|
return &out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type fileError struct {
|
||||||
|
err error
|
||||||
|
files []*File
|
||||||
|
}
|
||||||
|
|
||||||
|
//convertFile converts the given os.File to a squashfs.File. Returns the errors and converted file to the channels.
|
||||||
|
func (w *Writer) convertFile(squashfsPath string, file *os.File, subDir bool, fileErrChan chan fileError) {
|
||||||
|
var out fileError
|
||||||
|
var fil File
|
||||||
|
fil.Reader = file
|
||||||
|
fil.path = squashfsPath
|
||||||
|
fil.name = path.Base(file.Name())
|
||||||
|
mode := fil.Mode()
|
||||||
|
|
||||||
|
if mode.IsRegular() {
|
||||||
|
fil.filType = inode.BasicFileType
|
||||||
|
goto successExit
|
||||||
|
} else if mode.IsDir() {
|
||||||
|
fil.filType = inode.BasicSymlinkType
|
||||||
|
subDirs, err := file.Readdirnames(-1)
|
||||||
|
if err != nil {
|
||||||
|
if w.AllowErrors && !subDir {
|
||||||
|
log.Println("Can't get sub-directories for", file.Name())
|
||||||
|
log.Println(err)
|
||||||
|
} else {
|
||||||
|
out.err = err
|
||||||
|
}
|
||||||
|
goto failExit
|
||||||
|
}
|
||||||
|
subDirChan := make(chan fileError)
|
||||||
|
for _, filName := range subDirs {
|
||||||
|
go func(filename string, returnChan chan fileError) {
|
||||||
|
subFil, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
out.err = err
|
||||||
|
returnChan <- fileError{
|
||||||
|
err: err,
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.convertFile(fil.Path(), subFil, true, subDirChan)
|
||||||
|
}(file.Name()+filName, subDirChan)
|
||||||
|
}
|
||||||
|
for range subDirs {
|
||||||
|
filErr := <-subDirChan
|
||||||
|
if filErr.err != nil {
|
||||||
|
if w.AllowErrors && !subDir {
|
||||||
|
log.Println("Error while adding subdirectory of", file.Name())
|
||||||
|
log.Println(filErr.err)
|
||||||
|
} else if subDir {
|
||||||
|
if out.err == nil {
|
||||||
|
out.err = filErr.err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
out.err = err
|
||||||
|
goto failExit
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
out.files = append(out.files, filErr.files...)
|
||||||
|
}
|
||||||
|
goto successExit
|
||||||
|
} else if mode&os.ModeSymlink == os.ModeSymlink {
|
||||||
|
fil.filType = inode.BasicSymlinkType
|
||||||
|
symLocation, err := os.Readlink(file.Name())
|
||||||
|
if err != nil {
|
||||||
|
if w.AllowErrors && !subDir {
|
||||||
|
log.Println("Error while getting symlink's information for", file.Name())
|
||||||
|
log.Println(err)
|
||||||
|
} else {
|
||||||
|
out.err = err
|
||||||
|
}
|
||||||
|
goto failExit
|
||||||
|
}
|
||||||
|
if w.ResolveSymlinks {
|
||||||
|
if val, ok := w.symlinkTable[symLocation]; ok {
|
||||||
|
symLocation = val
|
||||||
|
} else if val, ok := w.symTableTemp[symLocation]; ok {
|
||||||
|
symLocation = val
|
||||||
|
} else {
|
||||||
|
//TODO: either add the file, or place the file in this location. Maybe defer this until after all the other files are added?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//TODO: store the symLocation inside the File somehow....
|
||||||
|
}
|
||||||
|
if w.AllowErrors && !subDir {
|
||||||
|
log.Println("Unsupported file type for", file.Name())
|
||||||
|
} else {
|
||||||
|
out.err = errors.New("Unsupported file type")
|
||||||
|
}
|
||||||
|
failExit: //before this is used, make sure to log or set the error.
|
||||||
|
fileErrChan <- out
|
||||||
|
return
|
||||||
|
successExit:
|
||||||
|
out.files = []*File{&fil}
|
||||||
|
fileErrChan <- out
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//AddFilesToPath adds the give os.Files to the given path within the squashfs archive.
|
||||||
|
//If AllowErrors is true, this will ALWAYS return nil
|
||||||
|
func (w *Writer) AddFilesToPath(squashfsPath string, files ...*os.File) error {
|
||||||
|
squashfsPath = path.Clean(squashfsPath)
|
||||||
|
if strings.HasPrefix(squashfsPath, "/") {
|
||||||
|
squashfsPath = strings.TrimPrefix(squashfsPath, "/")
|
||||||
|
}
|
||||||
|
if squashfsPath == "." {
|
||||||
|
squashfsPath = "/"
|
||||||
|
}
|
||||||
|
fileErrChan := make(chan fileError)
|
||||||
|
for _, fil := range files {
|
||||||
|
go w.convertFile(squashfsPath, fil, false, fileErrChan)
|
||||||
|
}
|
||||||
|
return errors.New("Not yet ready")
|
||||||
|
}
|
||||||
|
|
||||||
|
//AddFiles adds all files given to the root directory
|
||||||
|
//If AllowErrors is true, this will ALWAYS return nil
|
||||||
|
func (w *Writer) AddFiles(files ...*os.File) error {
|
||||||
|
return w.AddFilesToPath("/", files...)
|
||||||
|
}
|
||||||
|
|
||||||
|
//RemoveFileAt removes the file at filepath from the Writer.
|
||||||
|
//If multiple files match the given filepath (such as if there are wildcards), all matching files are removed.
|
||||||
|
//If one or more files are removed, returns true.
|
||||||
|
func (w *Writer) RemoveFileAt(filepath string) bool {
|
||||||
|
//TODO
|
||||||
|
return false
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user