Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 942e0f770f | |||
| 7d16990277 | |||
| d2c72f9464 | |||
| 2ba4551fb9 | |||
| 6931075e7e | |||
| 55a25c9d45 | |||
| 94b45c8402 | |||
| 01de43a5ae | |||
| 5b29f4d029 |
@@ -1 +1,2 @@
|
|||||||
testing
|
testing
|
||||||
|
/go-unsquashfs
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
# squashfs (WIP)
|
# squashfs
|
||||||
|
|
||||||
[](https://pkg.go.dev/github.com/CalebQ42/squashfs) [](https://goreportcard.com/report/github.com/CalebQ42/squashfs)
|
[](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 squashfs. There is currently no plans to add archive creation support as it will almost always be better to just call `mksquashfs`. I could see some possible use cases, but probably won't spend time on it unless it's requested (open a discussion fi you want this feature).
|
||||||
|
|
||||||
Currently has support for reading squashfs files and extracting files and folders.
|
Currently has support for reading squashfs files and extracting files and folders.
|
||||||
|
|
||||||
@@ -14,7 +14,18 @@ Thanks also to [distri's squashfs library](https://github.com/distr1/distri/tree
|
|||||||
## Limitations
|
## 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.
|
* 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.
|
||||||
|
|||||||
@@ -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))
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
}
|
||||||
+186
-71
@@ -8,12 +8,14 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/CalebQ42/squashfs/internal/data"
|
"github.com/CalebQ42/squashfs/internal/data"
|
||||||
"github.com/CalebQ42/squashfs/internal/directory"
|
"github.com/CalebQ42/squashfs/internal/directory"
|
||||||
"github.com/CalebQ42/squashfs/internal/inode"
|
"github.com/CalebQ42/squashfs/internal/inode"
|
||||||
|
"github.com/CalebQ42/squashfs/internal/threadmanager"
|
||||||
)
|
)
|
||||||
|
|
||||||
// File represents a file inside a squashfs archive.
|
// File represents a file inside a squashfs archive.
|
||||||
@@ -59,6 +61,21 @@ func (f File) Stat() (fs.FileInfo, error) {
|
|||||||
return newFileInfo(f.e, f.i), nil
|
return newFileInfo(f.e, f.i), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mode returns the file's fs.FileMode
|
||||||
|
func (f File) Mode() fs.FileMode {
|
||||||
|
switch f.e.Type {
|
||||||
|
case inode.Dir:
|
||||||
|
return fs.FileMode(f.i.Perm) | fs.ModeDir
|
||||||
|
case inode.Char:
|
||||||
|
return fs.FileMode(f.i.Perm) | fs.ModeCharDevice
|
||||||
|
case inode.Block:
|
||||||
|
return fs.FileMode(f.i.Perm) | fs.ModeDevice
|
||||||
|
case inode.Sym:
|
||||||
|
return fs.FileMode(f.i.Perm) | fs.ModeSymlink
|
||||||
|
}
|
||||||
|
return fs.FileMode(f.i.Perm)
|
||||||
|
}
|
||||||
|
|
||||||
// Read reads the data from the file. Only works if file is a normal file.
|
// Read reads the data from the file. Only works if file is a normal file.
|
||||||
func (f File) Read(p []byte) (int, error) {
|
func (f File) Read(p []byte) (int, error) {
|
||||||
if f.i.Type != inode.Fil && f.i.Type != inode.EFil {
|
if f.i.Type != inode.Fil && f.i.Type != inode.EFil {
|
||||||
@@ -71,16 +88,22 @@ func (f File) Read(p []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f File) ReadAt(p []byte, off int64) (int, error) {
|
func (f File) ReadAt(p []byte, off int64) (int, error) {
|
||||||
|
if f.i.Type != inode.Fil && f.i.Type != inode.EFil {
|
||||||
|
return 0, ErrReadNotFile
|
||||||
|
}
|
||||||
return f.fullRdr.ReadAt(p, off)
|
return f.fullRdr.ReadAt(p, off)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteTo writes all data from the file to the writer. This is multi-threaded.
|
// WriteTo writes all data from the file to the writer. This is multi-threaded.
|
||||||
// The underlying reader is seperate from the one used with Read and can be reused.
|
// The underlying reader is seperate from the one used with Read and can be reused.
|
||||||
func (f File) WriteTo(w io.Writer) (int64, error) {
|
func (f File) WriteTo(w io.Writer) (int64, error) {
|
||||||
|
if f.i.Type != inode.Fil && f.i.Type != inode.EFil {
|
||||||
|
return 0, ErrReadNotFile
|
||||||
|
}
|
||||||
return f.fullRdr.WriteTo(w)
|
return f.fullRdr.WriteTo(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close simply nils the underlying reader. Here mostly to satisfy fs.File
|
// Close simply nils the underlying reader.
|
||||||
func (f *File) Close() error {
|
func (f *File) Close() error {
|
||||||
f.rdr = nil
|
f.rdr = nil
|
||||||
return nil
|
return nil
|
||||||
@@ -90,7 +113,7 @@ func (f *File) Close() error {
|
|||||||
// If n <= 0 all fs.DirEntry's are returned.
|
// If n <= 0 all fs.DirEntry's are returned.
|
||||||
func (f *File) ReadDir(n int) (out []fs.DirEntry, err error) {
|
func (f *File) ReadDir(n int) (out []fs.DirEntry, err error) {
|
||||||
if !f.IsDir() {
|
if !f.IsDir() {
|
||||||
return nil, errors.New("File is not a directory")
|
return nil, errors.New("file is not a directory")
|
||||||
}
|
}
|
||||||
ents, err := f.r.readDirectory(f.i)
|
ents, err := f.r.readDirectory(f.i)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -197,96 +220,153 @@ func (f File) GetSymlinkFile() *File {
|
|||||||
|
|
||||||
// ExtractionOptions are available options on how to extract.
|
// ExtractionOptions are available options on how to extract.
|
||||||
type ExtractionOptions struct {
|
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
|
||||||
DereferenceSymlink bool //Replace symlinks with the target file
|
LogOutput io.Writer //Where error log should write.
|
||||||
UnbreakSymlink bool //Try to make sure symlinks remain unbroken when extracted, without changing the symlink
|
DereferenceSymlink bool //Replace symlinks with the target file.
|
||||||
Verbose bool //Prints extra info to log on an error
|
UnbreakSymlink bool //Try to make sure symlinks remain unbroken when extracted, without changing the symlink.
|
||||||
FolderPerm fs.FileMode //The permissions used when creating the extraction folder
|
Verbose bool //Prints extra info to log on an error.
|
||||||
|
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 {
|
||||||
func DefaultOptions() ExtractionOptions {
|
return &ExtractionOptions{
|
||||||
return ExtractionOptions{
|
Perm: 0755,
|
||||||
FolderPerm: 0755,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExtractTo extracts the File to the given folder with the default options.
|
// 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.
|
// If the File is a directory, it instead extracts the directory's contents to the folder.
|
||||||
func (f File) ExtractTo(folder string) error {
|
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.
|
// ExtractSymlink extracts the File to the folder with the DereferenceSymlink option.
|
||||||
// If the File is a directory, it instead extracts the directory's contents to the folder.
|
// If the File is a directory, it instead extracts the directory's contents to the folder.
|
||||||
func (f File) ExtractSymlink(folder string) error {
|
func (f File) ExtractSymlink(folder string) error {
|
||||||
return f.ExtractWithOptions(folder, ExtractionOptions{
|
op := DefaultOptions()
|
||||||
DereferenceSymlink: true,
|
op.DereferenceSymlink = true
|
||||||
FolderPerm: 0755,
|
return f.realExtract(folder, op)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExtractWithOptions extracts the File to the given folder with the given ExtrationOptions.
|
// 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.
|
// 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 {
|
func (f File) ExtractWithOptions(folder string, op *ExtractionOptions) error {
|
||||||
if op.Verbose {
|
if op.Verbose && op.LogOutput != nil {
|
||||||
if op.LogOutput == nil {
|
|
||||||
op.LogOutput = os.Stdout
|
|
||||||
}
|
|
||||||
log.SetOutput(op.LogOutput)
|
log.SetOutput(op.LogOutput)
|
||||||
}
|
}
|
||||||
return f.realExtract(folder, op)
|
return f.realExtract(folder, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f File) realExtract(folder string, op ExtractionOptions) error {
|
func (f File) realExtract(folder string, op *ExtractionOptions) (err error) {
|
||||||
err := os.MkdirAll(folder, op.FolderPerm)
|
if op.manager == nil {
|
||||||
folder = filepath.Clean(folder)
|
op.manager = threadmanager.NewManager(runtime.NumCPU())
|
||||||
if err != nil && !os.IsExist(err) {
|
}
|
||||||
if op.Verbose {
|
extDir := folder + "/" + f.e.Name
|
||||||
log.Println("Error while creating extraction folder")
|
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 {
|
switch {
|
||||||
case f.IsDir():
|
case f.IsDir():
|
||||||
filFS, _ := f.FS()
|
if folder != extDir && f.e.Name != "" {
|
||||||
var ents []directory.Entry
|
//First extract it with a permisive permission.
|
||||||
ents, err = f.r.readDirectory(f.i)
|
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 err != nil {
|
||||||
if op.Verbose {
|
if op.Verbose {
|
||||||
log.Println("Error while reading children of", f.path())
|
log.Println("Error while converting", f.path(), "to FS")
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
errChan := make(chan error)
|
errChan := make(chan error, len(filFS.e))
|
||||||
for i := 0; i < len(ents); i++ {
|
files := make([]directory.Entry, 0)
|
||||||
go func(ent directory.Entry) {
|
//Focus on making the folder tree first...
|
||||||
fil, goErr := f.r.newFile(ent, filFS)
|
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 goErr != nil {
|
||||||
if op.Verbose {
|
if op.Verbose {
|
||||||
log.Println("Error while reading info for", filepath.Join(f.path(), ent.Name))
|
log.Println("Error while resolving", extDir)
|
||||||
}
|
}
|
||||||
errChan <- goErr
|
errChan <- goErr
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if fil.IsDir() {
|
errChan <- subF.ExtractWithOptions(extDir, op)
|
||||||
info, _ := fil.Stat()
|
}(i)
|
||||||
err = os.Mkdir(filepath.Join(folder, fil.e.Name), info.Mode())
|
|
||||||
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])
|
|
||||||
}
|
}
|
||||||
for i := 0; i < len(ents); i++ {
|
for i = 0; i < len(files); i++ {
|
||||||
err = <-errChan
|
err = <-errChan
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -294,36 +374,43 @@ func (f File) realExtract(folder string, op ExtractionOptions) error {
|
|||||||
}
|
}
|
||||||
case f.IsRegular():
|
case f.IsRegular():
|
||||||
var fil *os.File
|
var fil *os.File
|
||||||
fil, err = os.Create(folder + "/" + f.e.Name)
|
fil, err = os.Create(extDir)
|
||||||
if os.IsExist(err) {
|
if os.IsExist(err) {
|
||||||
os.Remove(folder + "/" + f.e.Name)
|
os.Remove(extDir)
|
||||||
fil, err = os.Create(folder + "/" + f.e.Name)
|
fil, err = os.Create(extDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if op.Verbose {
|
if op.Verbose {
|
||||||
log.Println("Error while creating", folder+"/"+f.e.Name)
|
log.Println("Error while creating", extDir)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
if op.Verbose {
|
if op.Verbose {
|
||||||
log.Println("Error while creating", folder+"/"+f.e.Name)
|
log.Println("Error while creating", extDir)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer fil.Close()
|
||||||
_, err = io.Copy(fil, f)
|
_, err = io.Copy(fil, f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if op.Verbose {
|
if op.Verbose {
|
||||||
log.Println("Error while copying data to", folder+"/"+f.e.Name)
|
log.Println("Error while copying data to", extDir)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if op.IgnorePerm {
|
||||||
|
os.Chmod(extDir, op.Perm|(f.Mode()&fs.ModeType))
|
||||||
|
} else {
|
||||||
|
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():
|
case f.IsSymlink():
|
||||||
symPath := f.SymlinkPath()
|
symPath := f.SymlinkPath()
|
||||||
if op.DereferenceSymlink {
|
if op.DereferenceSymlink {
|
||||||
fil := f.GetSymlinkFile()
|
fil := f.GetSymlinkFile()
|
||||||
if fil == nil {
|
if fil == nil {
|
||||||
if op.Verbose {
|
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")
|
return errors.New("cannot get symlink target")
|
||||||
}
|
}
|
||||||
@@ -331,7 +418,7 @@ func (f File) realExtract(folder string, op ExtractionOptions) error {
|
|||||||
err = fil.realExtract(folder, op)
|
err = fil.realExtract(folder, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if op.Verbose {
|
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
|
return err
|
||||||
}
|
}
|
||||||
@@ -340,31 +427,43 @@ func (f File) realExtract(folder string, op ExtractionOptions) error {
|
|||||||
fil := f.GetSymlinkFile()
|
fil := f.GetSymlinkFile()
|
||||||
if fil == nil {
|
if fil == nil {
|
||||||
if op.Verbose {
|
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")
|
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)
|
err = fil.realExtract(extractLoc, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if op.Verbose {
|
if op.Verbose {
|
||||||
log.Println("Error while extracting ", folder+"/"+f.e.Name)
|
log.Println("Error while extracting ", extDir)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = os.Symlink(f.SymlinkPath(), folder+"/"+f.e.Name)
|
err = os.Symlink(f.SymlinkPath(), extDir)
|
||||||
if os.IsExist(err) {
|
if os.IsExist(err) {
|
||||||
os.Remove(folder + "/" + f.e.Name)
|
os.Remove(extDir)
|
||||||
err = os.Symlink(f.SymlinkPath(), folder+"/"+f.e.Name)
|
err = os.Symlink(f.SymlinkPath(), extDir)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if op.Verbose {
|
if op.Verbose {
|
||||||
log.Println("Error while making symlink:", folder+"/"+f.e.Name)
|
log.Println("Error while making symlink:", extDir)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if op.IgnorePerm {
|
||||||
|
os.Chmod(extDir, op.Perm|(f.Mode()&fs.ModeType))
|
||||||
|
} else {
|
||||||
|
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():
|
case f.isDeviceOrFifo():
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
if op.Verbose {
|
||||||
|
log.Println(extDir, "ignored since it's a device link and can't be created on Windows.")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
_, err = exec.LookPath("mknod")
|
_, err = exec.LookPath("mknod")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if op.Verbose {
|
if op.Verbose {
|
||||||
@@ -378,9 +477,15 @@ func (f File) realExtract(folder string, op ExtractionOptions) error {
|
|||||||
} else if f.i.Type == inode.Block || f.i.Type == inode.EBlock {
|
} else if f.i.Type == inode.Block || f.i.Type == inode.EBlock {
|
||||||
typ = "b"
|
typ = "b"
|
||||||
} else { //Fifo IPC
|
} else { //Fifo IPC
|
||||||
|
if runtime.GOOS == "darwin" {
|
||||||
|
if op.Verbose {
|
||||||
|
log.Println(extDir, "ignored since it's a Fifo file and can't be created on Darwin.")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
typ = "p"
|
typ = "p"
|
||||||
}
|
}
|
||||||
cmd := exec.Command("mknod", folder+"/"+f.e.Name, typ)
|
cmd := exec.Command("mknod", extDir, typ)
|
||||||
if typ != "p" {
|
if typ != "p" {
|
||||||
maj, min := f.deviceDevices()
|
maj, min := f.deviceDevices()
|
||||||
cmd.Args = append(cmd.Args, strconv.Itoa(int(maj)), strconv.Itoa(int(min)))
|
cmd.Args = append(cmd.Args, strconv.Itoa(int(maj)), strconv.Itoa(int(min)))
|
||||||
@@ -392,10 +497,20 @@ func (f File) realExtract(folder string, op ExtractionOptions) error {
|
|||||||
err = cmd.Run()
|
err = cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if op.Verbose {
|
if op.Verbose {
|
||||||
log.Println("Error while running mknod for", folder+"/"+f.e.Name)
|
log.Println("Error while running mknod for", extDir)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if op.IgnorePerm {
|
||||||
|
os.Chmod(extDir, op.Perm|(f.Mode()&fs.ModeType))
|
||||||
|
} else {
|
||||||
|
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(extDir, "ignored since it's a socket file.")
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return errors.New("Unsupported file type. Inode type: " + strconv.Itoa(int(f.i.Type)))
|
return errors.New("Unsupported file type. Inode type: " + strconv.Itoa(int(f.i.Type)))
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-5
@@ -18,7 +18,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
squashfsURL = "https://darkstorm.tech/LinuxPATest.sfs"
|
squashfsURL = "https://darkstorm.tech/files/LinuxPATest.sfs"
|
||||||
squashfsName = "LinuxPATest.sfs"
|
squashfsName = "LinuxPATest.sfs"
|
||||||
|
|
||||||
filePath = "PortableApps/Notepad++Portable/App/DefaultData/Config/contextMenu.xml"
|
filePath = "PortableApps/Notepad++Portable/App/DefaultData/Config/contextMenu.xml"
|
||||||
@@ -106,7 +106,6 @@ func BenchmarkRace(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestExtractQuick(t *testing.T) {
|
func TestExtractQuick(t *testing.T) {
|
||||||
|
|
||||||
//First, setup everything and extract the archive using the library and unsquashfs
|
//First, setup everything and extract the archive using the library and unsquashfs
|
||||||
|
|
||||||
// tmpDir := b.TempDir()
|
// tmpDir := b.TempDir()
|
||||||
@@ -123,8 +122,12 @@ func TestExtractQuick(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
os.RemoveAll(filepath.Join(tmpDir, "testLog.txt"))
|
||||||
|
logFil, _ := os.Create(filepath.Join(tmpDir, "testLog.txt"))
|
||||||
op := squashfs.DefaultOptions()
|
op := squashfs.DefaultOptions()
|
||||||
op.Verbose = true
|
op.Verbose = true
|
||||||
|
op.IgnorePerm = true
|
||||||
|
op.LogOutput = logFil
|
||||||
err = rdr.ExtractWithOptions(libPath, op)
|
err = rdr.ExtractWithOptions(libPath, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -176,9 +179,7 @@ func TestSingleFile(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
op := squashfs.DefaultOptions()
|
err = f.(*squashfs.File).ExtractWithOptions("testing", &squashfs.ExtractionOptions{Verbose: true})
|
||||||
op.Verbose = true
|
|
||||||
err = f.(*squashfs.File).ExtractWithOptions("testing", op)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user