Compare commits

...

6 Commits

Author SHA1 Message Date
Caleb Gardner 942e0f770f Set main folder permission 2023-04-17 10:38:44 -05:00
Caleb Gardner 7d16990277 Updated README 2023-04-17 10:33:27 -05:00
Caleb Gardner d2c72f9464 Limit number of simultaneous file extractions to prevent hardlock
Added helper extraction functions
chmod & chown is now set after a folder's extraction to prevent permission issues
2023-04-17 10:22:10 -05:00
Caleb Gardner 2ba4551fb9 Fixed stupid errors 2023-04-17 08:01:20 -05:00
Caleb Gardner 6931075e7e Testing better large file support 2023-04-17 07:51:08 -05:00
Caleb Gardner 55a25c9d45 Updated README 2023-04-12 08:44:48 -05:00
6 changed files with 215 additions and 84 deletions
+2 -1
View File
@@ -1 +1,2 @@
testing
testing
/go-unsquashfs
+13 -2
View File
@@ -14,7 +14,18 @@ Thanks also to [distri's squashfs library](https://github.com/distr1/distri/tree
## Limitations
* No Xattr parsing. This is simply because I haven't done any research on it and how to apply these in a pure go way.
* Socket files are not extracted.
* From my research, it seems like a socket file would be useless if it could be created.
* Fifo files are ignored on `darwin`
## Performance
## Issues
Testing on a zstd compressed file, my library is anywhere from 5x ~ 7x slower then `unsquashfs`
* Significantly slower then `unsquashfs` when extracting folders (about 5 ~ 7 times slower on a ~100MB archive using zstd compression)
* This seems to be related to above along with the general optimization of `unsquashfs` and it's compression libraries.
* The larger the file's tree, the slower the extraction will be. Arch Linux's Live USB's airootfs.sfs takes ~35x longer for a full extraction.
## Recommendations on Usage
Due to the above performance consideration, this library should only be used to access files within the archive without extraction, or to mount it via Fuse.
* Neither of these use cases are largely effected by the issue above.
+37
View File
@@ -0,0 +1,37 @@
package main
import (
"flag"
"fmt"
"os"
"time"
"github.com/CalebQ42/squashfs"
)
func main() {
verbose := flag.Bool("v", false, "Verbose")
ignore := flag.Bool("ip", false, "Ignore Permissions and extract all files/folders with 0755")
flag.Parse()
if len(flag.Args()) < 2 {
fmt.Println("Please provide a file name and extraction path")
os.Exit(0)
}
f, err := os.Open(flag.Arg(0))
if err != nil {
panic(err)
}
r, err := squashfs.NewReader(f)
if err != nil {
panic(err)
}
op := squashfs.DefaultOptions()
op.Verbose = *verbose
op.IgnorePerm = *ignore
n := time.Now()
err = r.ExtractWithOptions(flag.Arg(1), op)
if err != nil {
panic(err)
}
fmt.Println("Took:", time.Since(n))
}
+23
View File
@@ -0,0 +1,23 @@
package threadmanager
type Manager struct {
c chan int
}
func NewManager(maxRoutines int) *Manager {
m := &Manager{
c: make(chan int, maxRoutines),
}
for i := 0; i < maxRoutines; i++ {
m.c <- i
}
return m
}
func (m *Manager) Lock() int {
return <-m.c
}
func (m *Manager) Unlock(n int) {
m.c <- n
}
+134 -76
View File
@@ -15,6 +15,7 @@ import (
"github.com/CalebQ42/squashfs/internal/data"
"github.com/CalebQ42/squashfs/internal/directory"
"github.com/CalebQ42/squashfs/internal/inode"
"github.com/CalebQ42/squashfs/internal/threadmanager"
)
// File represents a file inside a squashfs archive.
@@ -219,25 +220,40 @@ func (f File) GetSymlinkFile() *File {
// ExtractionOptions are available options on how to extract.
type ExtractionOptions struct {
LogOutput io.Writer //Where error log should write. If nil, uses os.Stdout. Has no effect if verbose is false.
manager *threadmanager.Manager
LogOutput io.Writer //Where error log should write.
DereferenceSymlink bool //Replace symlinks with the target file.
UnbreakSymlink bool //Try to make sure symlinks remain unbroken when extracted, without changing the symlink.
Verbose bool //Prints extra info to log on an error.
IgnorePerm bool //ignore the file's permission and instead use FolderPerm.
FolderPerm fs.FileMode //The permissions used when creating the extraction folder. Defaults to 0755.
IgnorePerm bool //Ignore file's permissions and instead use Perm.
Perm fs.FileMode //Permission to use when IgnorePerm. Defaults to 0755.
notFirst bool
}
// DefaultOptions is the default ExtractionOptions.
func DefaultOptions() ExtractionOptions {
return ExtractionOptions{
FolderPerm: 0755,
func DefaultOptions() *ExtractionOptions {
return &ExtractionOptions{
Perm: 0755,
}
}
// ExtractTo extracts the File to the given folder with the default options.
// If the File is a directory, it instead extracts the directory's contents to the folder.
func (f File) ExtractTo(folder string) error {
return f.ExtractWithOptions(folder, DefaultOptions())
return f.realExtract(folder, DefaultOptions())
}
// ExtractVerbose extracts the File to the folder with the Verbose option.
func (f File) ExtractVerbose(folder string) error {
op := DefaultOptions()
op.Verbose = true
return f.realExtract(folder, op)
}
// ExtractIgnorePermissions extracts the File to the folder with the IgnorePerm option.
func (f File) ExtractIgnorePermissions(folder string) error {
op := DefaultOptions()
op.IgnorePerm = true
return f.realExtract(folder, op)
}
// ExtractSymlink extracts the File to the folder with the DereferenceSymlink option.
@@ -245,73 +261,112 @@ func (f File) ExtractTo(folder string) error {
func (f File) ExtractSymlink(folder string) error {
op := DefaultOptions()
op.DereferenceSymlink = true
return f.ExtractWithOptions(folder, op)
return f.realExtract(folder, op)
}
// ExtractWithOptions extracts the File to the given folder with the given ExtrationOptions.
// If the File is a directory, it instead extracts the directory's contents to the folder.
func (f File) ExtractWithOptions(folder string, op ExtractionOptions) error {
if op.Verbose {
if op.LogOutput == nil {
op.LogOutput = os.Stdout
}
func (f File) ExtractWithOptions(folder string, op *ExtractionOptions) error {
if op.Verbose && op.LogOutput != nil {
log.SetOutput(op.LogOutput)
}
return f.realExtract(folder, op)
}
func (f File) realExtract(folder string, op ExtractionOptions) error {
err := os.MkdirAll(folder, op.FolderPerm)
folder = filepath.Clean(folder)
if err != nil && !os.IsExist(err) {
if op.Verbose {
log.Println("Error while creating extraction folder")
func (f File) realExtract(folder string, op *ExtractionOptions) (err error) {
if op.manager == nil {
op.manager = threadmanager.NewManager(runtime.NumCPU())
}
extDir := folder + "/" + f.e.Name
if !op.notFirst {
op.notFirst = true
if f.IsDir() {
extDir = folder
_, err = os.Open(folder)
if err != nil && os.IsNotExist(err) {
err = os.Mkdir(extDir, op.Perm)
}
if err != nil {
if op.Verbose {
log.Println("Error while making", folder)
}
return
}
if !op.IgnorePerm {
defer os.Chmod(extDir, f.Mode())
defer os.Chown(extDir, int(f.r.ids[f.i.UidInd]), int(f.r.ids[f.i.GidInd]))
}
}
return err
}
switch {
case f.IsDir():
filFS, _ := f.FS()
var ents []directory.Entry
ents, err = f.r.readDirectory(f.i)
if folder != extDir && f.e.Name != "" {
//First extract it with a permisive permission.
err = os.Mkdir(extDir, op.Perm)
if err != nil {
if op.Verbose {
log.Println("Error while making directory", extDir)
}
return
}
//Then set it to it's actual permissions once we're done with it
if !op.IgnorePerm {
defer os.Chmod(extDir, f.Mode())
defer os.Chown(extDir, int(f.r.ids[f.i.UidInd]), int(f.r.ids[f.i.GidInd]))
}
}
var filFS *FS
filFS, err = f.FS()
if err != nil {
if op.Verbose {
log.Println("Error while reading children of", f.path())
log.Println("Error while converting", f.path(), "to FS")
}
return err
}
errChan := make(chan error)
for i := 0; i < len(ents); i++ {
go func(ent directory.Entry) {
fil, goErr := f.r.newFile(ent, filFS)
errChan := make(chan error, len(filFS.e))
files := make([]directory.Entry, 0)
//Focus on making the folder tree first...
var i int
for i = 0; i < len(filFS.e); i++ {
if filFS.e[i].Type == inode.Fil {
files = append(files, filFS.e[i])
} else {
go func(index int) {
subF, goErr := f.r.newFile(filFS.e[index], filFS)
if goErr != nil {
if op.Verbose {
log.Println("Error while resolving", extDir)
}
errChan <- goErr
return
}
errChan <- subF.ExtractWithOptions(extDir, op)
}(i)
}
}
for i = 0; i < len(filFS.e)-len(files); i++ {
err = <-errChan
if err != nil {
return err
}
}
//Then we extract the files.
for i = 0; i < len(files); i++ {
go func(index int) {
n := op.manager.Lock()
defer op.manager.Unlock(n)
subF, goErr := f.r.newFile(files[index], filFS)
if goErr != nil {
if op.Verbose {
log.Println("Error while reading info for", filepath.Join(f.path(), ent.Name))
log.Println("Error while resolving", extDir)
}
errChan <- goErr
return
}
if fil.IsDir() {
perm := f.Mode()
if op.IgnorePerm {
perm = (op.FolderPerm & fs.ModePerm) | (perm & fs.ModeType)
}
err = os.Mkdir(filepath.Join(folder, fil.e.Name), perm)
if err != nil {
if op.Verbose {
log.Println("Error while creating", filepath.Join(folder, fil.e.Name))
}
errChan <- err
return
}
errChan <- fil.realExtract(filepath.Join(folder, fil.e.Name), op)
} else {
errChan <- fil.realExtract(folder, op)
}
fil.Close()
}(ents[i])
errChan <- subF.ExtractWithOptions(extDir, op)
}(i)
}
for i := 0; i < len(ents); i++ {
for i = 0; i < len(files); i++ {
err = <-errChan
if err != nil {
return err
@@ -319,19 +374,19 @@ func (f File) realExtract(folder string, op ExtractionOptions) error {
}
case f.IsRegular():
var fil *os.File
fil, err = os.Create(folder + "/" + f.e.Name)
fil, err = os.Create(extDir)
if os.IsExist(err) {
os.Remove(folder + "/" + f.e.Name)
fil, err = os.Create(folder + "/" + f.e.Name)
os.Remove(extDir)
fil, err = os.Create(extDir)
if err != nil {
if op.Verbose {
log.Println("Error while creating", folder+"/"+f.e.Name)
log.Println("Error while creating", extDir)
}
return err
}
} else if err != nil {
if op.Verbose {
log.Println("Error while creating", folder+"/"+f.e.Name)
log.Println("Error while creating", extDir)
}
return err
}
@@ -339,14 +394,15 @@ func (f File) realExtract(folder string, op ExtractionOptions) error {
_, err = io.Copy(fil, f)
if err != nil {
if op.Verbose {
log.Println("Error while copying data to", folder+"/"+f.e.Name)
log.Println("Error while copying data to", extDir)
}
return err
}
if op.IgnorePerm {
os.Chmod(fil.Name(), op.FolderPerm)
os.Chmod(extDir, op.Perm|(f.Mode()&fs.ModeType))
} else {
os.Chmod(fil.Name(), f.Mode())
os.Chmod(extDir, f.Mode())
os.Chown(extDir, int(f.r.ids[f.i.UidInd]), int(f.r.ids[f.i.GidInd]))
}
case f.IsSymlink():
symPath := f.SymlinkPath()
@@ -354,7 +410,7 @@ func (f File) realExtract(folder string, op ExtractionOptions) error {
fil := f.GetSymlinkFile()
if fil == nil {
if op.Verbose {
log.Println("Symlink path(", symPath, ") is unobtainable:", folder+"/"+f.e.Name)
log.Println("Symlink path(", symPath, ") is unobtainable:", extDir)
}
return errors.New("cannot get symlink target")
}
@@ -362,7 +418,7 @@ func (f File) realExtract(folder string, op ExtractionOptions) error {
err = fil.realExtract(folder, op)
if err != nil {
if op.Verbose {
log.Println("Error while extracting the symlink's file:", folder+"/"+f.e.Name)
log.Println("Error while extracting the symlink's file:", extDir)
}
return err
}
@@ -371,39 +427,40 @@ func (f File) realExtract(folder string, op ExtractionOptions) error {
fil := f.GetSymlinkFile()
if fil == nil {
if op.Verbose {
log.Println("Symlink path(", symPath, ") is unobtainable:", folder+"/"+f.e.Name)
log.Println("Symlink path(", symPath, ") is unobtainable:", extDir)
}
return errors.New("cannot get symlink target")
}
extractLoc := filepath.Clean(folder + "/" + filepath.Dir(symPath))
extractLoc := filepath.Join(folder, filepath.Dir(symPath))
err = fil.realExtract(extractLoc, op)
if err != nil {
if op.Verbose {
log.Println("Error while extracting ", folder+"/"+f.e.Name)
log.Println("Error while extracting ", extDir)
}
return err
}
}
err = os.Symlink(f.SymlinkPath(), folder+"/"+f.e.Name)
err = os.Symlink(f.SymlinkPath(), extDir)
if os.IsExist(err) {
os.Remove(folder + "/" + f.e.Name)
err = os.Symlink(f.SymlinkPath(), folder+"/"+f.e.Name)
os.Remove(extDir)
err = os.Symlink(f.SymlinkPath(), extDir)
}
if err != nil {
if op.Verbose {
log.Println("Error while making symlink:", folder+"/"+f.e.Name)
log.Println("Error while making symlink:", extDir)
}
return err
}
if op.IgnorePerm {
os.Chmod(folder+"/"+f.e.Name, op.FolderPerm)
os.Chmod(extDir, op.Perm|(f.Mode()&fs.ModeType))
} else {
os.Chmod(folder+"/"+f.e.Name, f.Mode())
os.Chmod(extDir, f.Mode())
os.Chown(extDir, int(f.r.ids[f.i.UidInd]), int(f.r.ids[f.i.GidInd]))
}
case f.isDeviceOrFifo():
if runtime.GOOS == "windows" {
if op.Verbose {
log.Println(folder+"/"+f.e.Name, "ignored since it's a device link and can't be created on Windows.")
log.Println(extDir, "ignored since it's a device link and can't be created on Windows.")
}
return nil
}
@@ -422,13 +479,13 @@ func (f File) realExtract(folder string, op ExtractionOptions) error {
} else { //Fifo IPC
if runtime.GOOS == "darwin" {
if op.Verbose {
log.Println(folder+"/"+f.e.Name, "ignored since it's a Fifo file and can't be created on Darwin.")
log.Println(extDir, "ignored since it's a Fifo file and can't be created on Darwin.")
}
return nil
}
typ = "p"
}
cmd := exec.Command("mknod", folder+"/"+f.e.Name, typ)
cmd := exec.Command("mknod", extDir, typ)
if typ != "p" {
maj, min := f.deviceDevices()
cmd.Args = append(cmd.Args, strconv.Itoa(int(maj)), strconv.Itoa(int(min)))
@@ -440,18 +497,19 @@ func (f File) realExtract(folder string, op ExtractionOptions) error {
err = cmd.Run()
if err != nil {
if op.Verbose {
log.Println("Error while running mknod for", folder+"/"+f.e.Name)
log.Println("Error while running mknod for", extDir)
}
return err
}
if op.IgnorePerm {
os.Chmod(folder+"/"+f.e.Name, op.FolderPerm)
os.Chmod(extDir, op.Perm|(f.Mode()&fs.ModeType))
} else {
os.Chmod(folder+"/"+f.e.Name, f.Mode())
os.Chmod(extDir, f.Mode())
os.Chown(extDir, int(f.r.ids[f.i.UidInd]), int(f.r.ids[f.i.GidInd]))
}
case f.e.Type == inode.Sock:
if op.Verbose {
log.Println(folder+"/"+f.e.Name, "ignored since it's a socket file.")
log.Println(extDir, "ignored since it's a socket file.")
}
default:
return errors.New("Unsupported file type. Inode type: " + strconv.Itoa(int(f.i.Type)))
+6 -5
View File
@@ -18,7 +18,7 @@ import (
)
const (
squashfsURL = "https://darkstorm.tech/LinuxPATest.sfs"
squashfsURL = "https://darkstorm.tech/files/LinuxPATest.sfs"
squashfsName = "LinuxPATest.sfs"
filePath = "PortableApps/Notepad++Portable/App/DefaultData/Config/contextMenu.xml"
@@ -106,7 +106,6 @@ func BenchmarkRace(b *testing.B) {
}
func TestExtractQuick(t *testing.T) {
//First, setup everything and extract the archive using the library and unsquashfs
// tmpDir := b.TempDir()
@@ -123,8 +122,12 @@ func TestExtractQuick(t *testing.T) {
if err != nil {
t.Fatal(err)
}
os.RemoveAll(filepath.Join(tmpDir, "testLog.txt"))
logFil, _ := os.Create(filepath.Join(tmpDir, "testLog.txt"))
op := squashfs.DefaultOptions()
op.Verbose = true
op.IgnorePerm = true
op.LogOutput = logFil
err = rdr.ExtractWithOptions(libPath, op)
if err != nil {
t.Fatal(err)
@@ -176,9 +179,7 @@ func TestSingleFile(t *testing.T) {
if err != nil {
t.Fatal(err)
}
op := squashfs.DefaultOptions()
op.Verbose = true
err = f.(*squashfs.File).ExtractWithOptions("testing", op)
err = f.(*squashfs.File).ExtractWithOptions("testing", &squashfs.ExtractionOptions{Verbose: true})
if err != nil {
t.Fatal(err)
}