Compare commits
85 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e9de9e6ad4 | |||
| ef72408cd0 | |||
| 144805e747 | |||
| bfba5d5b60 | |||
| 17d45eea50 | |||
| d9132ab6a4 | |||
| 5de59627df | |||
| b2a3920c1f | |||
| 0449a03428 | |||
| 0574bbed39 | |||
| 707391baba | |||
| d4d1b2c2b2 | |||
| fcd8c4c85b | |||
| 54d193a3df | |||
| a129b259be | |||
| 87f7533a17 | |||
| 7e1a584e8f | |||
| 942e0f770f | |||
| 7d16990277 | |||
| d2c72f9464 | |||
| 2ba4551fb9 | |||
| 6931075e7e | |||
| 55a25c9d45 | |||
| 94b45c8402 | |||
| 01de43a5ae | |||
| 5b29f4d029 | |||
| 6c7e926649 | |||
| 72d85d7810 | |||
| 67df5f40c6 | |||
| 1ae5593e6c | |||
| 653c4a167b | |||
| 9fe17650b8 | |||
| e9e967f085 | |||
| 187da99dd6 | |||
| 75d2a29319 | |||
| ce2e45ceec | |||
| 089ef53c8c | |||
| 658e5c9e0b | |||
| f2d86aff96 | |||
| f61237a1f0 | |||
| 820e06e792 | |||
| 4f8f5f6928 | |||
| 1b5078c7bd | |||
| 56fdba2f28 | |||
| ffbf4ebc64 | |||
| a015b16293 | |||
| 327781d86e | |||
| 4efd2ee49d | |||
| 392193993c | |||
| 2230a449ec | |||
| 0e50efea64 | |||
| 7a22538623 | |||
| 3bf851852f | |||
| ac89ff7275 | |||
| 83dfa77b7d | |||
| 1b934de04d | |||
| 981f1697ab | |||
| 214419b5c3 | |||
| 8f5e1fef96 | |||
| 49595de3f2 | |||
| 96b38935a6 | |||
| 9ac8fef3b2 | |||
| cde6a265a1 | |||
| 8613e35221 | |||
| 16ef5838c3 | |||
| 0a2ced9072 | |||
| a908d69987 | |||
| 6ada4f3b49 | |||
| 89f28cec6e | |||
| c988309edc | |||
| e8a8c531a9 | |||
| 80ff4466ae | |||
| 64055a8a63 | |||
| 0402b0a2ee | |||
| 305f261d10 | |||
| 70e3d81427 | |||
| 6ad6857d8d | |||
| 7cf15d48c7 | |||
| 28f39cf315 | |||
| 7f5fa3ba1f | |||
| 2a8310a724 | |||
| 8ad4d30bc7 | |||
| b6fbd63ba4 | |||
| 9913b848c6 | |||
| 65bc4a5d78 |
@@ -0,0 +1,3 @@
|
|||||||
|
# These are supported funding model platforms
|
||||||
|
|
||||||
|
github: CalebQ42
|
||||||
@@ -1 +1,2 @@
|
|||||||
testing
|
testing
|
||||||
|
/go-unsquashfs
|
||||||
|
|||||||
@@ -1,18 +1,40 @@
|
|||||||
# 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 if you want this feature).
|
||||||
|
|
||||||
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.
|
The library has two parts with this `github.com/CalebQ42/squashfs` being easy to use as it implements `io/fs` interfaces and doesn't expose unnecessary information. 95% this is the library you want. If you need lower level access to the information, use `github.com/CalebQ42/squashfs/low` where far more information is exposed.
|
||||||
|
|
||||||
The only major thing missing from squashfs reading is Xattr parsing.
|
Currently has support for reading squashfs files and extracting files and folders.
|
||||||
|
|
||||||
Special thanks to <https://dr-emann.github.io/squashfs/> for some VERY important information in an easy to understand format.
|
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).
|
||||||
|
|
||||||
## Performane
|
## FUSE
|
||||||
|
|
||||||
This library, decompressing the Firefox AppImage and using go tests, takes about twice as long as `unsquashfs` on my quad core laptop. (~1 second with the library and about half a second with `unsquashfs`)
|
As of `v1.0`, FUSE capabilities has been moved to [a separate library](https://github.com/CalebQ42/squashfuse).
|
||||||
|
|
||||||
## [TODO](https://github.com/CalebQ42/squashfs/projects/1?fullscreen=true)
|
## Limitations
|
||||||
|
|
||||||
|
* No Xattr parsing.
|
||||||
|
* 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`
|
||||||
|
|
||||||
|
## Issues
|
||||||
|
|
||||||
|
* Significantly slower then `unsquashfs` when extracting folders
|
||||||
|
* This seems to be related to above along with the general optimization of `unsquashfs` and it's compression libraries.
|
||||||
|
* Times seem to be largely dependent on file tree size and compression type.
|
||||||
|
* My main testing image (~100MB) using Zstd takes about 6x longer.
|
||||||
|
* An Arch Linux airootfs image (~780MB) using XZ compression with LZMA filters takes about 32x longer.
|
||||||
|
* A Tensorflow docker image (~3.3GB) using Zstd takes about 12x longer.
|
||||||
|
|
||||||
|
Note: These numbers are using `FastOptions()`. `DefaultOptions()` takes about 2x longer.
|
||||||
|
|
||||||
|
## 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.
|
||||||
|
|||||||
@@ -1,63 +0,0 @@
|
|||||||
package squashfs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
type bufferedBytes struct {
|
|
||||||
data []byte
|
|
||||||
r offsetRange
|
|
||||||
}
|
|
||||||
|
|
||||||
type offsetRange struct {
|
|
||||||
beg int
|
|
||||||
end int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o *offsetRange) offset(off int) {
|
|
||||||
o.beg += off
|
|
||||||
o.end += off
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o offsetRange) within(check int) bool {
|
|
||||||
return check >= o.beg || check <= o.end
|
|
||||||
}
|
|
||||||
|
|
||||||
type bufferedWriter struct {
|
|
||||||
w io.Writer
|
|
||||||
buffer []bufferedBytes
|
|
||||||
mainOffset int
|
|
||||||
}
|
|
||||||
|
|
||||||
func newBufferedWriter(w io.Writer) *bufferedWriter {
|
|
||||||
var out bufferedWriter
|
|
||||||
out.w = w
|
|
||||||
return &out
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *bufferedWriter) WriteTo(data []byte, offset int64) (n int, err error) {
|
|
||||||
if int(offset) == b.mainOffset {
|
|
||||||
n, err = b.Write(data)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
newBuff := bufferedBytes{
|
|
||||||
data: data,
|
|
||||||
r: offsetRange{
|
|
||||||
beg: int(offset),
|
|
||||||
end: int(offset) + len(data),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
b.buffer = append(b.buffer, newBuff)
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *bufferedWriter) Write(data []byte) (int, error) {
|
|
||||||
n, err := b.w.Write(data)
|
|
||||||
b.mainOffset += n
|
|
||||||
if err != nil {
|
|
||||||
return n, err
|
|
||||||
}
|
|
||||||
return n, err
|
|
||||||
}
|
|
||||||
@@ -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))
|
||||||
|
}
|
||||||
-247
@@ -1,247 +0,0 @@
|
|||||||
package squashfs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/CalebQ42/squashfs/internal/inode"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
//ErrInodeNotFile is given when giving an inode, but the function requires a file inode.
|
|
||||||
errInodeNotFile = errors.New("Given inode is NOT a file type")
|
|
||||||
//ErrInodeOnlyFragment is given when trying to make a DataReader from an inode, but the inode only had data in a fragment
|
|
||||||
errInodeOnlyFragment = errors.New("Given inode ONLY has fragment data")
|
|
||||||
)
|
|
||||||
|
|
||||||
//DataReader reads data from data blocks.
|
|
||||||
type dataReader struct {
|
|
||||||
r *Reader
|
|
||||||
curData []byte
|
|
||||||
sizes []uint32
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
//NewDataReader creates a new data reader at the given offset, with the blocks defined by sizes
|
|
||||||
func (r *Reader) newDataReader(offset int64, sizes []uint32) (*dataReader, error) {
|
|
||||||
var dr dataReader
|
|
||||||
dr.r = r
|
|
||||||
dr.offset = offset
|
|
||||||
dr.sizes = sizes
|
|
||||||
err := dr.readCurBlock()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &dr, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//NewDataReaderFromInode creates a new DataReader from a given inode. Inode must be of BasicFile or ExtendedFile types
|
|
||||||
func (r *Reader) newDataReaderFromInode(i *inode.Inode) (*dataReader, error) {
|
|
||||||
var rdr dataReader
|
|
||||||
rdr.r = r
|
|
||||||
switch i.Type {
|
|
||||||
case inode.FileType:
|
|
||||||
fil := i.Info.(inode.File)
|
|
||||||
if fil.BlockStart == 0 {
|
|
||||||
return nil, errInodeOnlyFragment
|
|
||||||
}
|
|
||||||
rdr.offset = int64(fil.BlockStart)
|
|
||||||
for _, sizes := range fil.BlockSizes {
|
|
||||||
rdr.sizes = append(rdr.sizes, sizes)
|
|
||||||
}
|
|
||||||
if fil.Fragmented {
|
|
||||||
rdr.sizes = rdr.sizes[:len(rdr.sizes)-1]
|
|
||||||
}
|
|
||||||
case inode.ExtFileType:
|
|
||||||
fil := i.Info.(inode.ExtFile)
|
|
||||||
if fil.BlockStart == 0 {
|
|
||||||
return nil, errInodeOnlyFragment
|
|
||||||
}
|
|
||||||
rdr.offset = int64(fil.BlockStart)
|
|
||||||
for _, sizes := range fil.BlockSizes {
|
|
||||||
rdr.sizes = append(rdr.sizes, sizes)
|
|
||||||
}
|
|
||||||
if fil.Fragmented {
|
|
||||||
rdr.sizes = rdr.sizes[:len(rdr.sizes)-1]
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return nil, errInodeNotFile
|
|
||||||
}
|
|
||||||
err := rdr.readCurBlock()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &rdr, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//removed the compression bit from a data block size
|
|
||||||
func actualDataSize(size uint32) uint32 {
|
|
||||||
return size &^ (1 << 24)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *dataReader) readNextBlock() error {
|
|
||||||
d.curBlock++
|
|
||||||
if d.curBlock >= len(d.sizes) {
|
|
||||||
d.curBlock--
|
|
||||||
return io.EOF
|
|
||||||
}
|
|
||||||
err := d.readCurBlock()
|
|
||||||
if err != nil {
|
|
||||||
d.curBlock--
|
|
||||||
d.readCurBlock()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *dataReader) readBlockAt(offset int64, size uint32) ([]byte, error) {
|
|
||||||
compressed := size&(1<<24) != (1 << 24)
|
|
||||||
size = size &^ (1 << 24)
|
|
||||||
if d.sizes[d.curBlock] == 0 {
|
|
||||||
return make([]byte, d.r.super.BlockSize), nil
|
|
||||||
}
|
|
||||||
sec := io.NewSectionReader(d.r.r, offset, int64(size))
|
|
||||||
if compressed {
|
|
||||||
btys, err := d.r.decompressor.Decompress(sec)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return btys, nil
|
|
||||||
}
|
|
||||||
var buf bytes.Buffer
|
|
||||||
_, err := io.Copy(&buf, sec)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return buf.Bytes(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *dataReader) offsetForBlock(index int) int64 {
|
|
||||||
out := d.offset
|
|
||||||
for i := 0; i < index; i++ {
|
|
||||||
out += int64(actualDataSize(d.sizes[i]))
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *dataReader) readCurBlock() error {
|
|
||||||
if d.curBlock >= len(d.sizes) {
|
|
||||||
return io.EOF
|
|
||||||
}
|
|
||||||
offset := d.offsetForBlock(d.curBlock)
|
|
||||||
data, err := d.readBlockAt(offset, d.sizes[d.curBlock])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
d.curData = data
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *dataReader) Read(p []byte) (int, error) {
|
|
||||||
if d.curData == nil {
|
|
||||||
err := d.readCurBlock()
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if d.curReadOffset+len(p) <= len(d.curData) {
|
|
||||||
for i := 0; i < len(p); i++ {
|
|
||||||
p[i] = d.curData[d.curReadOffset+i]
|
|
||||||
}
|
|
||||||
d.curReadOffset += len(p)
|
|
||||||
return len(p), nil
|
|
||||||
}
|
|
||||||
read := 0
|
|
||||||
for read < len(p) {
|
|
||||||
if d.curReadOffset == len(d.curData) {
|
|
||||||
err := d.readNextBlock()
|
|
||||||
if err != nil {
|
|
||||||
return read, err
|
|
||||||
}
|
|
||||||
d.curReadOffset = 0
|
|
||||||
}
|
|
||||||
for ; read < len(p); read++ {
|
|
||||||
if d.curReadOffset < len(d.curData) {
|
|
||||||
p[read] = d.curData[d.curReadOffset]
|
|
||||||
} else {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
d.curReadOffset++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if read != len(p) {
|
|
||||||
return read, errors.New("Didn't read enough data")
|
|
||||||
}
|
|
||||||
return read, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteTo writes all the data in the datablock to the writer. MUST BE USED ON A FRESH DATA READER.
|
|
||||||
func (d *dataReader) WriteTo(w io.Writer) (int64, error) {
|
|
||||||
type dataCache struct {
|
|
||||||
err error
|
|
||||||
data []byte
|
|
||||||
index int
|
|
||||||
}
|
|
||||||
dataChan := make(chan *dataCache)
|
|
||||||
for i := range d.sizes {
|
|
||||||
go func(index int, c chan *dataCache) {
|
|
||||||
var cache dataCache
|
|
||||||
cache.index = index
|
|
||||||
defer func() {
|
|
||||||
c <- &cache
|
|
||||||
}()
|
|
||||||
data, err := d.readBlockAt(d.offsetForBlock(index), d.sizes[index])
|
|
||||||
if err != nil {
|
|
||||||
cache.err = err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
cache.data = data
|
|
||||||
return
|
|
||||||
}(i, dataChan)
|
|
||||||
}
|
|
||||||
curIndex := 0
|
|
||||||
totalWrite := int64(0)
|
|
||||||
var backlog []*dataCache
|
|
||||||
mainLoop:
|
|
||||||
for {
|
|
||||||
if curIndex == len(d.sizes) {
|
|
||||||
return totalWrite, nil
|
|
||||||
}
|
|
||||||
if len(backlog) > 0 {
|
|
||||||
for i, cache := range backlog {
|
|
||||||
if cache.index == curIndex {
|
|
||||||
writen, err := w.Write(cache.data)
|
|
||||||
totalWrite += int64(writen)
|
|
||||||
if err != nil {
|
|
||||||
return totalWrite, err
|
|
||||||
}
|
|
||||||
if len(backlog) > 0 {
|
|
||||||
backlog[i] = backlog[len(backlog)-1]
|
|
||||||
backlog = backlog[:len(backlog)-1]
|
|
||||||
} else {
|
|
||||||
backlog = nil
|
|
||||||
}
|
|
||||||
curIndex++
|
|
||||||
continue mainLoop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cache := <-dataChan
|
|
||||||
if cache.err != nil {
|
|
||||||
return totalWrite, cache.err
|
|
||||||
}
|
|
||||||
if cache.index == curIndex {
|
|
||||||
writen, err := w.Write(cache.data)
|
|
||||||
totalWrite += int64(writen)
|
|
||||||
if err != nil {
|
|
||||||
return totalWrite, err
|
|
||||||
}
|
|
||||||
curIndex++
|
|
||||||
} else {
|
|
||||||
backlog = append(backlog, cache)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,137 +0,0 @@
|
|||||||
package squashfs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
"io/fs"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/CalebQ42/squashfs/internal/directory"
|
|
||||||
"github.com/CalebQ42/squashfs/internal/inode"
|
|
||||||
)
|
|
||||||
|
|
||||||
//DirEntry is a child of a directory.
|
|
||||||
type DirEntry struct {
|
|
||||||
en *directory.Entry
|
|
||||||
parent *FS
|
|
||||||
r *Reader
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Reader) newDirEntry(en *directory.Entry, parent *FS) *DirEntry {
|
|
||||||
return &DirEntry{
|
|
||||||
en: en,
|
|
||||||
parent: parent,
|
|
||||||
r: r,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Name returns the DirEntry's name
|
|
||||||
func (d DirEntry) Name() string {
|
|
||||||
return d.en.Name
|
|
||||||
}
|
|
||||||
|
|
||||||
//IsDir Yep.
|
|
||||||
func (d DirEntry) IsDir() bool {
|
|
||||||
return d.en.Type == inode.DirType
|
|
||||||
}
|
|
||||||
|
|
||||||
//Type returns the type bits of fs.FileMode of the DirEntry.
|
|
||||||
func (d DirEntry) Type() fs.FileMode {
|
|
||||||
switch d.en.Type {
|
|
||||||
case inode.DirType:
|
|
||||||
return fs.ModeDir
|
|
||||||
case inode.SymType:
|
|
||||||
return fs.ModeSymlink
|
|
||||||
default:
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Info returns the fs.FileInfo for the given DirEntry.
|
|
||||||
func (d DirEntry) Info() (fs.FileInfo, error) {
|
|
||||||
in, err := d.r.getInodeFromEntry(d.en)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &FileInfo{
|
|
||||||
name: d.en.Name,
|
|
||||||
i: in,
|
|
||||||
parent: d.parent,
|
|
||||||
r: d.r,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//GetInodeFromEntry returns the inode associated with a given directory.Entry
|
|
||||||
func (r *Reader) getInodeFromEntry(en *directory.Entry) (*inode.Inode, error) {
|
|
||||||
br, err := r.newMetadataReader(int64(r.super.InodeTableStart + uint64(en.InodeOffset)))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
_, err = br.Seek(int64(en.InodeBlockOffset), io.SeekStart)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
i, err := inode.ProcessInode(br, r.super.BlockSize)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return i, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//FileInfo is a fs.FileInfo for a file.
|
|
||||||
type FileInfo struct {
|
|
||||||
i *inode.Inode
|
|
||||||
parent *FS
|
|
||||||
r *Reader
|
|
||||||
name string
|
|
||||||
}
|
|
||||||
|
|
||||||
//Name is the file's name.
|
|
||||||
func (f FileInfo) Name() string {
|
|
||||||
return f.name
|
|
||||||
}
|
|
||||||
|
|
||||||
//Size is the file's size if it's a regular file. Otherwise, returns 0.
|
|
||||||
func (f FileInfo) Size() int64 {
|
|
||||||
switch f.i.Type {
|
|
||||||
case inode.FileType:
|
|
||||||
return int64(f.i.Info.(inode.File).Size)
|
|
||||||
case inode.ExtFileType:
|
|
||||||
return int64(f.i.Info.(inode.ExtFile).Size)
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
//Mode returns the fs.FileMode bits of the file.
|
|
||||||
func (f FileInfo) Mode() fs.FileMode {
|
|
||||||
mode := fs.FileMode(f.i.Permissions)
|
|
||||||
switch f.i.Type {
|
|
||||||
case inode.DirType | inode.ExtDirType:
|
|
||||||
return mode | fs.ModeDir
|
|
||||||
case inode.ExtDirType:
|
|
||||||
return mode | fs.ModeDir
|
|
||||||
case inode.SymType:
|
|
||||||
return mode | fs.ModeSymlink
|
|
||||||
case inode.ExtSymType:
|
|
||||||
return mode | fs.ModeSymlink
|
|
||||||
}
|
|
||||||
return mode
|
|
||||||
}
|
|
||||||
|
|
||||||
//ModTime is the last time the file was modified.
|
|
||||||
func (f FileInfo) ModTime() time.Time {
|
|
||||||
return time.Unix(int64(f.i.ModifiedTime), 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
//IsDir yep.
|
|
||||||
func (f FileInfo) IsDir() bool {
|
|
||||||
return f.i.Type == inode.DirType || f.i.Type == inode.ExtDirType
|
|
||||||
}
|
|
||||||
|
|
||||||
//Sys returns the File for the FileInfo. If something goes wrong, nil is returned.
|
|
||||||
func (f FileInfo) Sys() interface{} {
|
|
||||||
fil, err := f.File()
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return fil
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package squashfs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"io/fs"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
|
"github.com/CalebQ42/squashfs/internal/routinemanager"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ExtractionOptions struct {
|
||||||
|
manager *routinemanager.Manager
|
||||||
|
LogOutput io.Writer //Where the verbose 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 file's permissions and instead use Perm.
|
||||||
|
Perm fs.FileMode //Permission to use when IgnorePerm. Defaults to 0777.
|
||||||
|
SimultaneousFiles uint16 //Number of files to process in parallel. Default set based on runtime.NumCPU().
|
||||||
|
ExtractionRoutines uint16 //Number of goroutines to use for each file's extraction. Only applies to regular files. Default set based on runtime.NumCPU().
|
||||||
|
}
|
||||||
|
|
||||||
|
// The default extraction options.
|
||||||
|
func DefaultOptions() *ExtractionOptions {
|
||||||
|
cores := uint16(runtime.NumCPU() / 2)
|
||||||
|
var files, routines uint16
|
||||||
|
if cores <= 4 {
|
||||||
|
files = 1
|
||||||
|
routines = cores
|
||||||
|
} else {
|
||||||
|
files = cores - 4
|
||||||
|
routines = 4
|
||||||
|
}
|
||||||
|
return &ExtractionOptions{
|
||||||
|
Perm: 0777,
|
||||||
|
SimultaneousFiles: files,
|
||||||
|
ExtractionRoutines: routines,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Less limited default options. Can run up 2x faster than DefaultOptions.
|
||||||
|
// Tends to use all available CPU resources.
|
||||||
|
func FastOptions() *ExtractionOptions {
|
||||||
|
return &ExtractionOptions{
|
||||||
|
Perm: 0777,
|
||||||
|
SimultaneousFiles: uint16(runtime.NumCPU()),
|
||||||
|
ExtractionRoutines: uint16(runtime.NumCPU()),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,374 +6,423 @@ import (
|
|||||||
"io/fs"
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/CalebQ42/squashfs/internal/directory"
|
"github.com/CalebQ42/squashfs/internal/routinemanager"
|
||||||
"github.com/CalebQ42/squashfs/internal/inode"
|
squashfslow "github.com/CalebQ42/squashfs/low"
|
||||||
|
"github.com/CalebQ42/squashfs/low/data"
|
||||||
|
"github.com/CalebQ42/squashfs/low/inode"
|
||||||
)
|
)
|
||||||
|
|
||||||
//File represents a file inside a squashfs archive.
|
// File represents a file inside a squashfs archive.
|
||||||
type File struct {
|
type File struct {
|
||||||
i *inode.Inode
|
b *squashfslow.FileBase
|
||||||
|
full *data.FullReader
|
||||||
|
rdr *data.Reader
|
||||||
parent *FS
|
parent *FS
|
||||||
r *Reader
|
r *Reader
|
||||||
reader *fileReader
|
|
||||||
name string
|
|
||||||
dirsRead int
|
dirsRead int
|
||||||
}
|
}
|
||||||
|
|
||||||
//File creates a File from the FileInfo.
|
// Creates a new *File from the given *squashfs.Base
|
||||||
//*File satisfies fs.File and fs.ReadDirFile.
|
func (r *Reader) FileFromBase(b *squashfslow.FileBase, parent *FS) *File {
|
||||||
func (f FileInfo) File() (file *File, err error) {
|
return &File{
|
||||||
file = &File{
|
b: b,
|
||||||
name: f.name,
|
|
||||||
r: f.r,
|
|
||||||
parent: f.parent,
|
|
||||||
i: f.i,
|
|
||||||
}
|
|
||||||
if file.IsRegular() {
|
|
||||||
file.reader, err = f.r.newFileReader(f.i)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//File creates a File from the DirEntry.
|
|
||||||
func (d DirEntry) File() (file *File, err error) {
|
|
||||||
return d.r.newFileFromDirEntry(d.en, d.parent)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r Reader) newFileFromDirEntry(en *directory.Entry, parent *FS) (file *File, err error) {
|
|
||||||
file = &File{
|
|
||||||
name: en.Name,
|
|
||||||
r: &r,
|
|
||||||
parent: parent,
|
parent: parent,
|
||||||
|
r: r,
|
||||||
}
|
}
|
||||||
file.i, err = r.getInodeFromEntry(en)
|
}
|
||||||
|
|
||||||
|
func (f *File) FS() (*FS, error) {
|
||||||
|
if !f.IsDir() {
|
||||||
|
return nil, errors.New("not a directory")
|
||||||
|
}
|
||||||
|
d, err := f.b.ToDir(f.r.Low)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if file.IsRegular() {
|
return &FS{d: d, parent: f.parent, r: f.r}, nil
|
||||||
file.reader, err = r.newFileReader(file.i)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Stat returns the File's fs.FileInfo
|
// Closes the underlying readers.
|
||||||
func (f File) Stat() (fs.FileInfo, error) {
|
// Further calls to Read and WriteTo will re-create the readers.
|
||||||
return &FileInfo{
|
// Never returns an error.
|
||||||
i: f.i,
|
|
||||||
name: f.name,
|
|
||||||
parent: f.parent,
|
|
||||||
r: f.r,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//Read reads the data from the file. Only works if file is a normal file.
|
|
||||||
func (f File) Read(p []byte) (int, error) {
|
|
||||||
if f.i.Type == inode.FileType || f.i.Type == inode.ExtFileType {
|
|
||||||
if f.reader == nil {
|
|
||||||
return 0, fs.ErrClosed
|
|
||||||
}
|
|
||||||
return f.reader.Read(p)
|
|
||||||
}
|
|
||||||
return 0, errors.New("Can only read files")
|
|
||||||
}
|
|
||||||
|
|
||||||
//WriteTo writes all data from the file to the writer. This is multi-threaded.
|
|
||||||
func (f File) WriteTo(w io.Writer) (int64, error) {
|
|
||||||
if f.i.Type == inode.FileType || f.i.Type == inode.ExtFileType {
|
|
||||||
if f.reader == nil {
|
|
||||||
return 0, fs.ErrClosed
|
|
||||||
}
|
|
||||||
return f.reader.WriteTo(w)
|
|
||||||
}
|
|
||||||
return 0, errors.New("Can only read files")
|
|
||||||
}
|
|
||||||
|
|
||||||
//Close simply nils the underlying reader. Here mostly to satisfy fs.File
|
|
||||||
func (f *File) Close() error {
|
func (f *File) Close() error {
|
||||||
f.reader = nil
|
if f.rdr != nil {
|
||||||
|
return f.rdr.Close()
|
||||||
|
}
|
||||||
|
f.rdr = nil
|
||||||
|
f.full = nil
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//ReadDir returns n fs.DirEntry's that's contained in the File (if it's a directory).
|
// Returns the file the symlink points to.
|
||||||
//If n <= 0 all fs.DirEntry's are returned.
|
// If the file isn't a symlink, or points to a file outside the archive, returns nil.
|
||||||
func (f File) ReadDir(n int) ([]fs.DirEntry, error) {
|
func (f *File) GetSymlinkFile() fs.File {
|
||||||
if !f.IsDir() {
|
if !f.IsSymlink() {
|
||||||
return nil, errors.New("File is not a directory")
|
return nil
|
||||||
}
|
}
|
||||||
ffs, err := f.FS()
|
if filepath.IsAbs(f.SymlinkPath()) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
fil, err := f.parent.Open(f.SymlinkPath())
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return fil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns whether the file is a directory.
|
||||||
|
func (f *File) IsDir() bool {
|
||||||
|
return f.b.IsDir()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns whether the file is a regular file.
|
||||||
|
func (f *File) IsRegular() bool {
|
||||||
|
return f.b.IsRegular()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns whether the file is a symlink.
|
||||||
|
func (f *File) IsSymlink() bool {
|
||||||
|
return f.b.Inode.Type == inode.Sym || f.b.Inode.Type == inode.ESym
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *File) Mode() fs.FileMode {
|
||||||
|
return f.b.Inode.Mode()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read reads the data from the file. Only works if file is a normal file.
|
||||||
|
func (f *File) Read(b []byte) (int, error) {
|
||||||
|
if !f.IsRegular() {
|
||||||
|
return 0, errors.New("file is not a regular file")
|
||||||
|
}
|
||||||
|
if f.rdr == nil {
|
||||||
|
err := f.initializeReaders()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return f.rdr.Read(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadDir returns n fs.DirEntry's that's contained in the File (if it's a directory).
|
||||||
|
// If n <= 0 all fs.DirEntry's are returned.
|
||||||
|
func (f *File) ReadDir(n int) ([]fs.DirEntry, error) {
|
||||||
|
if !f.IsDir() {
|
||||||
|
return nil, errors.New("file is not a directory")
|
||||||
|
}
|
||||||
|
d, err := f.b.ToDir(f.r.Low)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var beg, end int
|
start, end := 0, len(d.Entries)
|
||||||
if n <= 0 {
|
if n > 0 {
|
||||||
beg, end = 0, len(ffs.entries)
|
start, end = f.dirsRead, f.dirsRead+n
|
||||||
} else {
|
if end > len(d.Entries) {
|
||||||
beg, end = f.dirsRead, f.dirsRead+n
|
end = len(d.Entries)
|
||||||
if end > len(ffs.entries) {
|
|
||||||
end = len(ffs.entries)
|
|
||||||
err = io.EOF
|
err = io.EOF
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out := make([]fs.DirEntry, end-beg)
|
var out []fs.DirEntry
|
||||||
for i, ent := range ffs.entries[beg:end] {
|
var fi fileInfo
|
||||||
out[i] = f.r.newDirEntry(ent, ffs)
|
for _, e := range d.Entries[start:end] {
|
||||||
|
fi, err = f.r.newFileInfo(e)
|
||||||
|
if err != nil {
|
||||||
|
f.dirsRead += len(out)
|
||||||
|
return out, err
|
||||||
|
}
|
||||||
|
out = append(out, fs.FileInfoToDirEntry(fi))
|
||||||
}
|
}
|
||||||
|
f.dirsRead += len(out)
|
||||||
return out, err
|
return out, err
|
||||||
}
|
}
|
||||||
|
|
||||||
//FS returns the File as a FS.
|
// Returns the file's fs.FileInfo
|
||||||
func (f File) FS() (*FS, error) {
|
func (f *File) Stat() (fs.FileInfo, error) {
|
||||||
if !f.IsDir() {
|
return newFileInfo(f.b.Name, f.b.Inode), nil
|
||||||
return nil, errors.New("File is not a directory")
|
|
||||||
}
|
|
||||||
ents, err := f.r.readDirFromInode(f.i)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &FS{
|
|
||||||
entries: ents,
|
|
||||||
parent: f.parent,
|
|
||||||
r: f.r,
|
|
||||||
name: f.name,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//IsDir Yep.
|
// SymlinkPath returns the symlink's target path. Is the File isn't a symlink, returns an empty string.
|
||||||
func (f File) IsDir() bool {
|
func (f *File) SymlinkPath() string {
|
||||||
return f.i.Type == inode.DirType || f.i.Type == inode.ExtDirType
|
switch f.b.Inode.Type {
|
||||||
}
|
case inode.Sym:
|
||||||
|
return string(f.b.Inode.Data.(inode.Symlink).Target)
|
||||||
func (f File) path() string {
|
case inode.ESym:
|
||||||
if f.name == "/" {
|
return string(f.b.Inode.Data.(inode.ESymlink).Target)
|
||||||
return f.name
|
|
||||||
}
|
|
||||||
return f.parent.path() + "/" + f.name
|
|
||||||
}
|
|
||||||
|
|
||||||
//IsRegular yep.
|
|
||||||
func (f File) IsRegular() bool {
|
|
||||||
return f.i.Type == inode.FileType || f.i.Type == inode.ExtFileType
|
|
||||||
}
|
|
||||||
|
|
||||||
//IsSymlink yep.
|
|
||||||
func (f File) IsSymlink() bool {
|
|
||||||
return f.i.Type == inode.SymType || f.i.Type == inode.ExtSymType
|
|
||||||
}
|
|
||||||
|
|
||||||
//SymlinkPath returns the symlink's target path. Is the File isn't a symlink, returns an empty string.
|
|
||||||
func (f File) SymlinkPath() string {
|
|
||||||
switch f.i.Type {
|
|
||||||
case inode.SymType:
|
|
||||||
return f.i.Info.(inode.Sym).Path
|
|
||||||
case inode.ExtSymType:
|
|
||||||
return f.i.Info.(inode.ExtSym).Path
|
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
//GetSymlinkFile returns the File the symlink is pointing to.
|
// Writes all data from the file to the given writer in a multi-threaded manner.
|
||||||
//If not a symlink, or the target is unobtainable (such as it being outside the archive or it's absolute) returns nil
|
// The underlying reader is separate
|
||||||
func (f File) GetSymlinkFile() *File {
|
func (f *File) WriteTo(w io.Writer) (int64, error) {
|
||||||
if !f.IsSymlink() {
|
if !f.IsRegular() {
|
||||||
return nil
|
return 0, errors.New("file is not a regular file")
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(f.SymlinkPath(), "/") {
|
if f.full == nil {
|
||||||
return nil
|
err := f.initializeReaders()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
sym, err := f.parent.Open(f.SymlinkPath())
|
return f.full.WriteTo(w)
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return sym.(*File)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//ExtractionOptions are available options on how to extract.
|
func (f *File) initializeReaders() error {
|
||||||
type ExtractionOptions struct {
|
var err error
|
||||||
notBase bool
|
f.rdr, f.full, err = f.b.GetRegFileReaders(f.r.Low)
|
||||||
DereferenceSymlink bool //Replace symlinks with the target file
|
return err
|
||||||
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
|
|
||||||
FolderPerm fs.FileMode //The permissions used when creating the extraction folder
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//DefaultOptions is the default ExtractionOptions.
|
func (f *File) deviceDevices() (maj uint32, min uint32) {
|
||||||
func DefaultOptions() ExtractionOptions {
|
var dev uint32
|
||||||
return ExtractionOptions{
|
if f.b.Inode.Type == inode.Char || f.b.Inode.Type == inode.Block {
|
||||||
DereferenceSymlink: false,
|
dev = f.b.Inode.Data.(inode.Device).Dev
|
||||||
UnbreakSymlink: false,
|
} else if f.b.Inode.Type == inode.EChar || f.b.Inode.Type == inode.EBlock {
|
||||||
Verbose: false,
|
dev = f.b.Inode.Data.(inode.EDevice).Dev
|
||||||
FolderPerm: fs.ModePerm,
|
|
||||||
}
|
}
|
||||||
|
return dev >> 8, dev & 0x000FF
|
||||||
}
|
}
|
||||||
|
|
||||||
//ExtractTo extracts the File to the given folder with the default options.
|
func (f *File) path() string {
|
||||||
//If the File is a directory, it instead extracts the directory's contents to the folder.
|
if f.parent == nil {
|
||||||
func (f File) ExtractTo(folder string) error {
|
return f.b.Name
|
||||||
|
}
|
||||||
|
return filepath.Join(f.parent.path(), f.b.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract the file to the given folder. If the file is a folder, the folder's contents will be extracted to the folder.
|
||||||
|
// Uses default extraction options.
|
||||||
|
func (f *File) Extract(folder string) error {
|
||||||
return f.ExtractWithOptions(folder, DefaultOptions())
|
return f.ExtractWithOptions(folder, DefaultOptions())
|
||||||
}
|
}
|
||||||
|
|
||||||
//ExtractSymlink extracts the File to the folder with the DereferenceSymlink option.
|
// Extract the file to the given folder. If the file is a folder, the folder's contents will be extracted to the folder.
|
||||||
//If the File is a directory, it instead extracts the directory's contents to the folder.
|
// Allows setting various extraction options via ExtractionOptions.
|
||||||
func (f File) ExtractSymlink(folder string) error {
|
func (f *File) ExtractWithOptions(path string, op *ExtractionOptions) error {
|
||||||
return f.ExtractWithOptions(folder, ExtractionOptions{
|
if op.manager == nil {
|
||||||
DereferenceSymlink: true,
|
op.manager = routinemanager.NewManager(op.SimultaneousFiles)
|
||||||
FolderPerm: fs.ModePerm,
|
if op.LogOutput != nil {
|
||||||
})
|
log.SetOutput(op.LogOutput)
|
||||||
}
|
}
|
||||||
|
err := os.MkdirAll(path, 0777)
|
||||||
//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 {
|
|
||||||
folder = path.Clean(folder)
|
|
||||||
if !op.notBase {
|
|
||||||
err := os.MkdirAll(folder, op.FolderPerm)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if op.Verbose {
|
||||||
|
log.Println("Failed to create initial directory", path)
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stat, err := f.Stat()
|
switch f.b.Inode.Type {
|
||||||
if f.IsDir() {
|
case inode.Dir, inode.EDir:
|
||||||
if op.notBase {
|
d, err := f.b.ToDir(f.r.Low)
|
||||||
err = os.Mkdir(folder+"/"+f.name, stat.Mode())
|
|
||||||
if err != nil && !os.IsExist(err) {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
op.notBase = true
|
|
||||||
}
|
|
||||||
var ents []fs.DirEntry
|
|
||||||
ents, err = f.ReadDir(0)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if op.Verbose {
|
if op.Verbose {
|
||||||
log.Println("Error while reading children of", f.path())
|
log.Println("Failed to create squashfs.Directory for", path)
|
||||||
}
|
}
|
||||||
return err
|
return errors.Join(errors.New("failed to create squashfs.Directory: "+path), err)
|
||||||
}
|
}
|
||||||
errChan := make(chan error)
|
errChan := make(chan error, len(d.Entries))
|
||||||
for i := 0; i < len(ents); i++ {
|
for i := range d.Entries {
|
||||||
go func(ent *DirEntry) {
|
b, err := f.r.Low.BaseFromEntry(d.Entries[i])
|
||||||
fil, goErr := ent.File()
|
if err != nil {
|
||||||
if goErr != nil {
|
if op.Verbose {
|
||||||
errChan <- goErr
|
log.Println("Failed to get squashfs.Base from entry for", path)
|
||||||
fil.Close()
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
errChan <- fil.ExtractWithOptions(folder+"/"+f.name, op)
|
return errors.Join(errors.New("failed to get base from entry: "+path), err)
|
||||||
fil.Close()
|
}
|
||||||
return
|
go func(b *squashfslow.FileBase, path string) {
|
||||||
}(ents[i].(*DirEntry))
|
i := op.manager.Lock()
|
||||||
|
if b.IsDir() {
|
||||||
|
extDir := filepath.Join(path, b.Name)
|
||||||
|
err = os.Mkdir(extDir, 0777)
|
||||||
|
op.manager.Unlock(i)
|
||||||
|
if err != nil {
|
||||||
|
if op.Verbose {
|
||||||
|
log.Println("Failed to create directory", path)
|
||||||
|
}
|
||||||
|
errChan <- errors.Join(errors.New("failed to create directory: "+path), err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = f.r.FileFromBase(b, f.r.FSFromDirectory(d, f.parent)).ExtractWithOptions(extDir, op)
|
||||||
|
if err != nil {
|
||||||
|
if op.Verbose {
|
||||||
|
log.Println("Failed to extract directory", path)
|
||||||
|
}
|
||||||
|
errChan <- errors.Join(errors.New("failed to extract directory: "+path), err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
errChan <- nil
|
||||||
|
} else {
|
||||||
|
fil := f.r.FileFromBase(b, f.r.FSFromDirectory(d, f.parent))
|
||||||
|
err = fil.ExtractWithOptions(path, op)
|
||||||
|
op.manager.Unlock(i)
|
||||||
|
fil.Close()
|
||||||
|
errChan <- err
|
||||||
|
}
|
||||||
|
}(b, path)
|
||||||
}
|
}
|
||||||
for i := 0; i < len(ents); i++ {
|
var errCache []error
|
||||||
err = <-errChan
|
for i := 0; i < len(d.Entries); i++ {
|
||||||
|
err := <-errChan
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
errCache = append(errCache, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
if len(errCache) > 0 {
|
||||||
} else if f.IsRegular() {
|
return errors.Join(errors.New("failed to extract folder: "+path), errors.Join(errCache...))
|
||||||
var fil *os.File
|
|
||||||
fil, err = os.Create(folder + "/" + f.name)
|
|
||||||
if os.IsExist(err) {
|
|
||||||
os.Remove(folder + "/" + f.name)
|
|
||||||
fil, err = os.Create(folder + "/" + f.name)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Error while creating", folder+"/"+f.name)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
_, err = io.Copy(fil, f)
|
case inode.Fil, inode.EFil:
|
||||||
|
path = filepath.Join(path, f.b.Name)
|
||||||
|
outFil, err := os.Create(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error while copying data to", folder+"/"+f.name)
|
if op.Verbose {
|
||||||
return err
|
log.Println("Failed to create file", path)
|
||||||
|
}
|
||||||
|
return errors.Join(errors.New("failed to create file: "+path), err)
|
||||||
}
|
}
|
||||||
return nil
|
defer outFil.Close()
|
||||||
} else if f.IsSymlink() {
|
full, err := f.b.GetFullReader(f.r.Low)
|
||||||
|
if err != nil {
|
||||||
|
if op.Verbose {
|
||||||
|
log.Println("Failed to create full reader for", path)
|
||||||
|
}
|
||||||
|
return errors.Join(errors.New("failed to create full reader: "+path), err)
|
||||||
|
}
|
||||||
|
full.SetGoroutineLimit(op.ExtractionRoutines)
|
||||||
|
_, err = full.WriteTo(outFil)
|
||||||
|
if err != nil {
|
||||||
|
if op.Verbose {
|
||||||
|
log.Println("Failed to write file", path)
|
||||||
|
}
|
||||||
|
return errors.Join(errors.New("failed to write file: "+path), err)
|
||||||
|
}
|
||||||
|
case inode.Sym, inode.ESym:
|
||||||
symPath := f.SymlinkPath()
|
symPath := f.SymlinkPath()
|
||||||
if op.DereferenceSymlink {
|
if op.DereferenceSymlink {
|
||||||
fil := f.GetSymlinkFile()
|
filTmp := f.GetSymlinkFile()
|
||||||
if fil == nil {
|
if filTmp == nil {
|
||||||
if op.Verbose {
|
if op.Verbose {
|
||||||
log.Println("Symlink path(", symPath, ") is unobtainable:", folder+"/"+f.name)
|
log.Println("Failed to get symlink's file:", f.path())
|
||||||
}
|
}
|
||||||
return errors.New("Cannot get symlink target")
|
return errors.New("failed to get symlink's file")
|
||||||
}
|
}
|
||||||
fil.name = f.name
|
fil := filTmp.(*File)
|
||||||
err = fil.ExtractWithOptions(folder, op)
|
fil.b.Name = f.b.Name
|
||||||
|
err := fil.ExtractWithOptions(path, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if op.Verbose {
|
if op.Verbose {
|
||||||
log.Println("Error while extracting the symlink's file:", folder+"/"+f.name)
|
log.Println("Failed to extract symlink's file:", filepath.Join(path, f.b.Name))
|
||||||
}
|
}
|
||||||
return err
|
return errors.Join(errors.New("failed to extract symlink's file: "+path), err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if op.UnbreakSymlink {
|
||||||
|
filTmp := f.GetSymlinkFile()
|
||||||
|
if filTmp == nil {
|
||||||
|
if op.Verbose {
|
||||||
|
log.Println("Failed to get symlink's file:", f.path())
|
||||||
|
}
|
||||||
|
return errors.New("failed to get symlink's file")
|
||||||
|
}
|
||||||
|
extractLoc := filepath.Join(path, filepath.Dir(symPath))
|
||||||
|
fil := filTmp.(*File)
|
||||||
|
err := fil.ExtractWithOptions(extractLoc, op)
|
||||||
|
if err != nil {
|
||||||
|
if op.Verbose {
|
||||||
|
log.Println("Error while extracting", fil.path(), "to make sure symlink at", f.path(), "is unbroken")
|
||||||
|
}
|
||||||
|
return errors.Join(errors.New("failed to extract symlink's file: "+extractLoc), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
path = filepath.Join(path, f.b.Name)
|
||||||
|
err := os.Symlink(f.SymlinkPath(), path)
|
||||||
|
if err != nil {
|
||||||
|
if op.Verbose {
|
||||||
|
log.Println("Failed to create symlink:", path)
|
||||||
|
}
|
||||||
|
return errors.Join(errors.New("failed to create symlink: "+path), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case inode.Char, inode.EChar, inode.Block, inode.EBlock, inode.Fifo, inode.EFifo:
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
if op.Verbose {
|
||||||
|
log.Println(f.path(), "ignored. A device link and can't be created on Windows.")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
} else if op.UnbreakSymlink {
|
|
||||||
fil := f.GetSymlinkFile()
|
|
||||||
if fil == nil {
|
|
||||||
if op.Verbose {
|
|
||||||
log.Println("Symlink path(", symPath, ") is unobtainable:", folder+"/"+f.name)
|
|
||||||
}
|
|
||||||
return errors.New("Cannot get symlink target")
|
|
||||||
}
|
|
||||||
extractLoc := path.Clean(folder + "/" + path.Dir(symPath))
|
|
||||||
err = fil.ExtractWithOptions(extractLoc, op)
|
|
||||||
if err != nil {
|
|
||||||
if op.Verbose {
|
|
||||||
log.Println("Error while extracting ", folder+"/"+f.name)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
err = os.Symlink(f.SymlinkPath(), folder+"/"+f.name)
|
|
||||||
if os.IsExist(err) {
|
|
||||||
os.Remove(folder + "/" + f.name)
|
|
||||||
err = os.Symlink(f.SymlinkPath(), folder+"/"+f.name)
|
|
||||||
}
|
}
|
||||||
|
_, err := exec.LookPath("mknod")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if op.Verbose {
|
if op.Verbose {
|
||||||
log.Println("Error while making symlink:", folder+"/"+f.name)
|
log.Println("mknot command not found, cannot create device link for", f.path())
|
||||||
}
|
}
|
||||||
return err
|
return errors.Join(errors.New("mknot command not found"), err)
|
||||||
|
}
|
||||||
|
path = filepath.Join(path, f.b.Name)
|
||||||
|
var typ string
|
||||||
|
if f.b.Inode.Type == inode.Char || f.b.Inode.Type == inode.EChar {
|
||||||
|
typ = "c"
|
||||||
|
} else if f.b.Inode.Type == inode.Block || f.b.Inode.Type == inode.EBlock {
|
||||||
|
typ = "b"
|
||||||
|
} else { //Fifo IPC
|
||||||
|
if runtime.GOOS == "darwin" {
|
||||||
|
if op.Verbose {
|
||||||
|
log.Println(f.path(), "ignored. A Fifo file and can't be created on Darwin.")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
typ = "p"
|
||||||
|
}
|
||||||
|
cmd := exec.Command("mknod", path, typ)
|
||||||
|
if typ != "p" {
|
||||||
|
maj, min := f.deviceDevices()
|
||||||
|
cmd.Args = append(cmd.Args, strconv.Itoa(int(maj)), strconv.Itoa(int(min)))
|
||||||
|
}
|
||||||
|
if op.Verbose {
|
||||||
|
cmd.Stdout = op.LogOutput
|
||||||
|
cmd.Stderr = op.LogOutput
|
||||||
|
}
|
||||||
|
err = cmd.Run()
|
||||||
|
if err != nil {
|
||||||
|
if op.Verbose {
|
||||||
|
log.Println("Error while running mknod for", path)
|
||||||
|
}
|
||||||
|
return errors.Join(errors.New("error while running mknod for "+path), err)
|
||||||
|
}
|
||||||
|
case inode.Sock, inode.ESock:
|
||||||
|
if op.Verbose {
|
||||||
|
log.Println(f.path(), "ignored since it's a socket file.")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
return errors.New("Unsupported file type. Inode type: " + strconv.Itoa(int(f.b.Inode.Type)))
|
||||||
|
}
|
||||||
|
if op.Verbose {
|
||||||
|
log.Println(f.path(), "extracted to", path)
|
||||||
|
}
|
||||||
|
if op.IgnorePerm {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
uid, err := f.b.Uid(f.r.Low)
|
||||||
|
if err != nil {
|
||||||
|
if op.Verbose {
|
||||||
|
log.Println("Failed to get uid for", path)
|
||||||
|
log.Println(err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return errors.New("Unsupported file type. Inode type: " + strconv.Itoa(int(f.i.Type)))
|
gid, err := f.b.Gid(f.r.Low)
|
||||||
}
|
|
||||||
|
|
||||||
//ReadDirFromInode returns a fully populated Directory from a given Inode.
|
|
||||||
//If the given inode is not a directory it returns an error.
|
|
||||||
func (r *Reader) readDirFromInode(i *inode.Inode) ([]*directory.Entry, error) {
|
|
||||||
var offset uint32
|
|
||||||
var metaOffset uint16
|
|
||||||
var size uint32
|
|
||||||
switch i.Type {
|
|
||||||
case inode.DirType:
|
|
||||||
offset = i.Info.(inode.Dir).DirectoryIndex
|
|
||||||
metaOffset = i.Info.(inode.Dir).DirectoryOffset
|
|
||||||
size = uint32(i.Info.(inode.Dir).DirectorySize)
|
|
||||||
case inode.ExtDirType:
|
|
||||||
offset = i.Info.(inode.ExtDir).DirectoryIndex
|
|
||||||
metaOffset = i.Info.(inode.ExtDir).DirectoryOffset
|
|
||||||
size = i.Info.(inode.ExtDir).DirectorySize
|
|
||||||
default:
|
|
||||||
return nil, errors.New("Not a directory inode")
|
|
||||||
}
|
|
||||||
br, err := r.newMetadataReader(int64(r.super.DirTableStart + uint64(offset)))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
_, err = br.Seek(int64(metaOffset), io.SeekStart)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
if op.Verbose {
|
||||||
}
|
log.Println("Failed to get gid for", path)
|
||||||
ents, err := directory.NewDirectory(br, size)
|
log.Println(err)
|
||||||
if err != nil {
|
}
|
||||||
return nil, err
|
return nil
|
||||||
}
|
}
|
||||||
return ents, nil
|
os.Chmod(path, f.Mode())
|
||||||
|
os.Chown(path, int(uid), int(gid))
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
package squashfs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/fs"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/CalebQ42/squashfs/low/directory"
|
||||||
|
"github.com/CalebQ42/squashfs/low/inode"
|
||||||
|
)
|
||||||
|
|
||||||
|
type fileInfo struct {
|
||||||
|
name string
|
||||||
|
size int64
|
||||||
|
perm uint32
|
||||||
|
modTime uint32
|
||||||
|
fileType uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Reader) newFileInfo(e directory.Entry) (fileInfo, error) {
|
||||||
|
i, err := r.Low.InodeFromEntry(e)
|
||||||
|
if err != nil {
|
||||||
|
return fileInfo{}, err
|
||||||
|
}
|
||||||
|
return newFileInfo(e.Name, i), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func newFileInfo(name string, i *inode.Inode) fileInfo {
|
||||||
|
var size int64
|
||||||
|
if i.Type == inode.Fil {
|
||||||
|
size = int64(i.Data.(inode.File).Size)
|
||||||
|
} else if i.Type == inode.EFil {
|
||||||
|
size = int64(i.Data.(inode.EFile).Size)
|
||||||
|
}
|
||||||
|
return fileInfo{
|
||||||
|
name: name,
|
||||||
|
size: size,
|
||||||
|
perm: uint32(i.Perm),
|
||||||
|
modTime: i.ModTime,
|
||||||
|
fileType: i.Type,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f fileInfo) Name() string {
|
||||||
|
return f.name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f fileInfo) Size() int64 {
|
||||||
|
return f.size
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f fileInfo) Mode() fs.FileMode {
|
||||||
|
if f.IsDir() {
|
||||||
|
return fs.FileMode(f.perm | uint32(fs.ModeDir))
|
||||||
|
}
|
||||||
|
return fs.FileMode(f.perm)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f fileInfo) ModTime() time.Time {
|
||||||
|
return time.Unix(int64(f.modTime), 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f fileInfo) IsDir() bool {
|
||||||
|
return f.fileType == inode.Dir || f.fileType == inode.EDir
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f fileInfo) Sys() any {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
-101
@@ -1,101 +0,0 @@
|
|||||||
package squashfs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/CalebQ42/squashfs/internal/inode"
|
|
||||||
)
|
|
||||||
|
|
||||||
//FileReader provides a io.Reader interface for files within a squashfs archive
|
|
||||||
type fileReader struct {
|
|
||||||
r *Reader
|
|
||||||
data *dataReader
|
|
||||||
in *inode.Inode
|
|
||||||
fragmentData []byte
|
|
||||||
fragged bool
|
|
||||||
fragOnly bool
|
|
||||||
read int
|
|
||||||
FileSize int //FileSize is the total size of the given file
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
//ErrPathIsNotFile returns when trying to read from a file, but the given path is NOT a file.
|
|
||||||
errPathIsNotFile = errors.New("The given path is not a file")
|
|
||||||
)
|
|
||||||
|
|
||||||
//ReadFile provides a squashfs.FileReader for the file at the given location.
|
|
||||||
func (r *Reader) newFileReader(in *inode.Inode) (*fileReader, error) {
|
|
||||||
var rdr fileReader
|
|
||||||
rdr.in = in
|
|
||||||
if in.Type != inode.FileType && in.Type != inode.ExtFileType {
|
|
||||||
return nil, errPathIsNotFile
|
|
||||||
}
|
|
||||||
switch in.Type {
|
|
||||||
case inode.FileType:
|
|
||||||
fil := in.Info.(inode.File)
|
|
||||||
rdr.fragged = fil.Fragmented
|
|
||||||
rdr.fragOnly = fil.BlockStart == 0
|
|
||||||
rdr.FileSize = int(fil.Size)
|
|
||||||
case inode.ExtFileType:
|
|
||||||
fil := in.Info.(inode.ExtFile)
|
|
||||||
rdr.fragged = fil.Fragmented
|
|
||||||
rdr.fragOnly = fil.BlockStart == 0
|
|
||||||
rdr.FileSize = int(fil.Size)
|
|
||||||
}
|
|
||||||
var err error
|
|
||||||
if rdr.fragged {
|
|
||||||
rdr.fragmentData, err = r.getFragmentDataFromInode(in)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !rdr.fragOnly {
|
|
||||||
rdr.data, err = r.newDataReaderFromInode(in)
|
|
||||||
}
|
|
||||||
return &rdr, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fileReader) Read(p []byte) (int, error) {
|
|
||||||
if f.fragOnly {
|
|
||||||
n, err := bytes.NewBuffer(f.fragmentData[f.read:]).Read(p)
|
|
||||||
f.read += n
|
|
||||||
if err != nil {
|
|
||||||
return n, err
|
|
||||||
}
|
|
||||||
return n, nil
|
|
||||||
}
|
|
||||||
var read int
|
|
||||||
n, err := f.data.Read(p)
|
|
||||||
read += n
|
|
||||||
if f.fragged && err == io.EOF {
|
|
||||||
if f.fragmentData == nil {
|
|
||||||
f.fragmentData, err = f.r.getFragmentDataFromInode(f.in)
|
|
||||||
}
|
|
||||||
n, err = bytes.NewBuffer(f.fragmentData).Read(p[read:])
|
|
||||||
read += n
|
|
||||||
if err != nil {
|
|
||||||
return read, err
|
|
||||||
}
|
|
||||||
} else if err != nil {
|
|
||||||
return read, err
|
|
||||||
}
|
|
||||||
return read, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fileReader) WriteTo(w io.Writer) (int64, error) {
|
|
||||||
if f.fragOnly {
|
|
||||||
n, err := w.Write(f.fragmentData)
|
|
||||||
return int64(n), err
|
|
||||||
}
|
|
||||||
if !f.fragged {
|
|
||||||
return f.data.WriteTo(w)
|
|
||||||
}
|
|
||||||
n, err := f.data.WriteTo(w)
|
|
||||||
if err != nil {
|
|
||||||
return int64(n), err
|
|
||||||
}
|
|
||||||
nn, err := w.Write(f.fragmentData)
|
|
||||||
return int64(nn) + n, err
|
|
||||||
}
|
|
||||||
-80
@@ -1,80 +0,0 @@
|
|||||||
package squashfs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/binary"
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/CalebQ42/squashfs/internal/inode"
|
|
||||||
)
|
|
||||||
|
|
||||||
//FragmentEntry is an entry in the fragment table
|
|
||||||
type fragmentEntry struct {
|
|
||||||
Start uint64
|
|
||||||
Size uint32
|
|
||||||
// Unused uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
//GetFragmentDataFromInode returns the fragment data for a given inode.
|
|
||||||
//If the inode does not have a fragment, harmlessly returns an empty slice without an error.
|
|
||||||
func (r *Reader) getFragmentDataFromInode(in *inode.Inode) ([]byte, error) {
|
|
||||||
var size uint64
|
|
||||||
var fragIndex uint32
|
|
||||||
var fragOffset uint32
|
|
||||||
if in.Type == inode.FileType {
|
|
||||||
bf := in.Info.(inode.File)
|
|
||||||
if !bf.Fragmented {
|
|
||||||
return make([]byte, 0), nil
|
|
||||||
}
|
|
||||||
if bf.BlockStart == 0 {
|
|
||||||
size = uint64(bf.Size)
|
|
||||||
} else {
|
|
||||||
size = uint64(bf.BlockSizes[len(bf.BlockSizes)-1])
|
|
||||||
}
|
|
||||||
fragIndex = bf.FragmentIndex
|
|
||||||
fragOffset = bf.FragmentOffset
|
|
||||||
} else if in.Type == inode.ExtFileType {
|
|
||||||
bf := in.Info.(inode.ExtFile)
|
|
||||||
if !bf.Fragmented {
|
|
||||||
return make([]byte, 0), nil
|
|
||||||
}
|
|
||||||
if bf.BlockStart == 0 {
|
|
||||||
size = bf.Size
|
|
||||||
} else {
|
|
||||||
size = uint64(bf.BlockSizes[len(bf.BlockSizes)-1])
|
|
||||||
}
|
|
||||||
fragIndex = bf.FragmentIndex
|
|
||||||
fragOffset = bf.FragmentOffset
|
|
||||||
} else {
|
|
||||||
return nil, errors.New("Inode type not supported")
|
|
||||||
}
|
|
||||||
//reading the fragment entry first
|
|
||||||
fragEntryRdr, err := r.newMetadataReader(int64(r.fragOffsets[int(fragIndex/512)]))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
_, err = fragEntryRdr.Seek(int64(16*fragIndex), io.SeekStart)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var entry fragmentEntry
|
|
||||||
err = binary.Read(fragEntryRdr, binary.LittleEndian, &entry)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
//now reading the actual fragment
|
|
||||||
dr, err := r.newDataReader(int64(entry.Start), []uint32{entry.Size})
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
_, err = dr.Read(make([]byte, fragOffset))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
tmp := make([]byte, size)
|
|
||||||
err = binary.Read(dr, binary.LittleEndian, &tmp)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return tmp, nil
|
|
||||||
}
|
|
||||||
@@ -1,96 +1,39 @@
|
|||||||
package squashfs
|
package squashfs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"errors"
|
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/CalebQ42/squashfs/internal/directory"
|
squashfslow "github.com/CalebQ42/squashfs/low"
|
||||||
|
"github.com/CalebQ42/squashfs/low/directory"
|
||||||
)
|
)
|
||||||
|
|
||||||
//FS is a fs.FS representation of a squashfs directory.
|
// FS is a fs.FS representation of a squashfs directory.
|
||||||
//Implements fs.GlobFS, fs.ReadDirFS, fs.ReadFileFS, fs.StatFS, and fs.SubFS
|
// Implements fs.GlobFS, fs.ReadDirFS, fs.ReadFileFS, fs.StatFS, and fs.SubFS
|
||||||
type FS struct {
|
type FS struct {
|
||||||
r *Reader
|
d *squashfslow.Directory
|
||||||
parent *FS
|
r *Reader
|
||||||
name string
|
parent *FS
|
||||||
entries []*directory.Entry
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Open opens the file at name. Returns a squashfs.File.
|
// Creates a new *FS from the given squashfs.directory
|
||||||
func (f FS) Open(name string) (fs.File, error) {
|
func (r *Reader) FSFromDirectory(d *squashfslow.Directory, parent *FS) *FS {
|
||||||
if !fs.ValidPath(name) {
|
return &FS{
|
||||||
return nil, &fs.PathError{
|
d: d,
|
||||||
Op: "open",
|
r: r,
|
||||||
Path: name,
|
parent: parent,
|
||||||
Err: fs.ErrInvalid,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
name = path.Clean(strings.TrimPrefix(name, "/"))
|
|
||||||
split := strings.Split(name, "/")
|
|
||||||
if split[0] == ".." {
|
|
||||||
if f.parent == nil {
|
|
||||||
//This should only happen on the root FS
|
|
||||||
return nil, &fs.PathError{
|
|
||||||
Op: "open",
|
|
||||||
Path: name,
|
|
||||||
//TODO: make error clearer
|
|
||||||
Err: errors.New("Trying to get file outside of squashfs"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return f.parent.Open(strings.Join(split[1:], "/"))
|
|
||||||
}
|
|
||||||
for i := 0; i < len(f.entries); i++ {
|
|
||||||
if match, _ := path.Match(split[0], f.entries[i].Name); match {
|
|
||||||
if len(split) == 1 {
|
|
||||||
return f.r.newFileFromDirEntry(f.entries[i], &f)
|
|
||||||
}
|
|
||||||
sub, err := f.Sub(split[0])
|
|
||||||
if err != nil {
|
|
||||||
if pathErr, ok := err.(*fs.PathError); ok {
|
|
||||||
pathErr.Op = "open"
|
|
||||||
pathErr.Path = name
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return nil, &fs.PathError{
|
|
||||||
Op: "open",
|
|
||||||
Path: name,
|
|
||||||
Err: err,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fil, err := sub.Open(strings.Join(split[1:], "/"))
|
|
||||||
if err != nil {
|
|
||||||
if pathErr, ok := err.(*fs.PathError); ok {
|
|
||||||
if pathErr.Err == fs.ErrNotExist {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
pathErr.Op = "open"
|
|
||||||
pathErr.Path = name
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return nil, &fs.PathError{
|
|
||||||
Op: "open",
|
|
||||||
Path: name,
|
|
||||||
Err: err,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return fil, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil, &fs.PathError{
|
|
||||||
Op: "open",
|
|
||||||
Path: name,
|
|
||||||
Err: fs.ErrNotExist,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Glob returns the name of the files at the given pattern.
|
// Glob returns the name of the files at the given pattern.
|
||||||
//All paths are relative to the FS.
|
// All paths are relative to the FS.
|
||||||
func (f FS) Glob(pattern string) (out []string, err error) {
|
// Uses filepath.Match to compare names.
|
||||||
|
func (f *FS) Glob(pattern string) (out []string, err error) {
|
||||||
|
pattern = filepath.Clean(pattern)
|
||||||
if !fs.ValidPath(pattern) {
|
if !fs.ValidPath(pattern) {
|
||||||
return nil, &fs.PathError{
|
return nil, &fs.PathError{
|
||||||
Op: "glob",
|
Op: "glob",
|
||||||
@@ -98,24 +41,11 @@ func (f FS) Glob(pattern string) (out []string, err error) {
|
|||||||
Err: fs.ErrInvalid,
|
Err: fs.ErrInvalid,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pattern = path.Clean(strings.TrimPrefix(pattern, "/"))
|
|
||||||
split := strings.Split(pattern, "/")
|
split := strings.Split(pattern, "/")
|
||||||
if split[0] == ".." {
|
for i := 0; i < len(f.d.Entries); i++ {
|
||||||
if f.parent == nil {
|
if match, _ := path.Match(split[0], f.d.Entries[i].Name); match {
|
||||||
//This should only happen on the root FS
|
|
||||||
return nil, &fs.PathError{
|
|
||||||
Op: "readdir",
|
|
||||||
Path: pattern,
|
|
||||||
//TODO: make error clearer
|
|
||||||
Err: errors.New("Trying to get file outside of squashfs"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return f.parent.Glob(strings.Join(split[1:], "/"))
|
|
||||||
}
|
|
||||||
for i := 0; i < len(f.entries); i++ {
|
|
||||||
if match, _ := path.Match(split[0], f.entries[i].Name); match {
|
|
||||||
if len(split) == 1 {
|
if len(split) == 1 {
|
||||||
out = append(out, f.entries[i].Name)
|
out = append(out, f.d.Entries[i].Name)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
sub, err := f.Sub(split[0])
|
sub, err := f.Sub(split[0])
|
||||||
@@ -134,7 +64,7 @@ func (f FS) Glob(pattern string) (out []string, err error) {
|
|||||||
Err: err,
|
Err: err,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
subGlob, err := sub.(FS).Glob(strings.Join(split[1:], "/"))
|
subGlob, err := sub.(fs.GlobFS).Glob(strings.Join(split[1:], "/"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if pathErr, ok := err.(*fs.PathError); ok {
|
if pathErr, ok := err.(*fs.PathError); ok {
|
||||||
if pathErr.Err == fs.ErrNotExist {
|
if pathErr.Err == fs.ErrNotExist {
|
||||||
@@ -151,7 +81,7 @@ func (f FS) Glob(pattern string) (out []string, err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for i := 0; i < len(subGlob); i++ {
|
for i := 0; i < len(subGlob); i++ {
|
||||||
subGlob[i] = f.name + "/" + subGlob[i]
|
subGlob[i] = f.d.Name + "/" + subGlob[i]
|
||||||
}
|
}
|
||||||
out = append(out, subGlob...)
|
out = append(out, subGlob...)
|
||||||
}
|
}
|
||||||
@@ -159,9 +89,70 @@ func (f FS) Glob(pattern string) (out []string, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//ReadDir returns all the DirEntry returns all DirEntry's for the directory at name.
|
// Opens the file at name. Returns a *File as an fs.File.
|
||||||
//If name is not a directory, returns an error.
|
func (f *FS) Open(name string) (fs.File, error) {
|
||||||
func (f FS) ReadDir(name string) ([]fs.DirEntry, error) {
|
name = filepath.Clean(name)
|
||||||
|
if !fs.ValidPath(name) {
|
||||||
|
return nil, &fs.PathError{
|
||||||
|
Op: "open",
|
||||||
|
Path: name,
|
||||||
|
Err: fs.ErrInvalid,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if name == "." || name == "" {
|
||||||
|
return f.File(), nil
|
||||||
|
}
|
||||||
|
split := strings.Split(name, "/")
|
||||||
|
if split[0] == ".." {
|
||||||
|
if f.parent == nil { // root directory
|
||||||
|
return nil, &fs.PathError{
|
||||||
|
Op: "open",
|
||||||
|
Path: name,
|
||||||
|
Err: fs.ErrNotExist,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return f.parent.Open(strings.Join(split[1:], "/"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i, found := slices.BinarySearchFunc(f.d.Entries, split[0], func(e directory.Entry, name string) int {
|
||||||
|
return strings.Compare(e.Name, name)
|
||||||
|
})
|
||||||
|
if !found {
|
||||||
|
return nil, &fs.PathError{
|
||||||
|
Op: "open",
|
||||||
|
Path: name,
|
||||||
|
Err: fs.ErrNotExist,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
b, err := f.r.Low.BaseFromEntry(f.d.Entries[i])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(split) == 1 {
|
||||||
|
return &File{
|
||||||
|
b: b,
|
||||||
|
r: f.r,
|
||||||
|
parent: f,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
if !b.IsDir() {
|
||||||
|
return nil, &fs.PathError{
|
||||||
|
Op: "open",
|
||||||
|
Path: name,
|
||||||
|
Err: fs.ErrNotExist,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
d, err := b.ToDir(f.r.Low)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return f.r.FSFromDirectory(d, f).Open(strings.Join(split[1:], "/"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns all DirEntry's for the directory at name.
|
||||||
|
// If name is not a directory, returns an error.
|
||||||
|
func (f *FS) ReadDir(name string) ([]fs.DirEntry, error) {
|
||||||
|
name = filepath.Clean(name)
|
||||||
if !fs.ValidPath(name) {
|
if !fs.ValidPath(name) {
|
||||||
return nil, &fs.PathError{
|
return nil, &fs.PathError{
|
||||||
Op: "readdir",
|
Op: "readdir",
|
||||||
@@ -169,115 +160,42 @@ func (f FS) ReadDir(name string) ([]fs.DirEntry, error) {
|
|||||||
Err: fs.ErrInvalid,
|
Err: fs.ErrInvalid,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
name = path.Clean(strings.TrimPrefix(name, "/"))
|
if name == "." || name == "" {
|
||||||
split := strings.Split(name, "/")
|
return f.File().ReadDir(-1)
|
||||||
if split[0] == ".." {
|
|
||||||
if f.parent == nil {
|
|
||||||
//This should only happen on the root FS
|
|
||||||
return nil, &fs.PathError{
|
|
||||||
Op: "readdir",
|
|
||||||
Path: name,
|
|
||||||
//TODO: make error clearer
|
|
||||||
Err: errors.New("Trying to get file outside of squashfs"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return f.parent.ReadDir(strings.Join(split[1:], "/"))
|
|
||||||
}
|
}
|
||||||
for i := 0; i < len(f.entries); i++ {
|
|
||||||
if match, _ := path.Match(split[0], f.entries[i].Name); match {
|
|
||||||
if len(split) == 1 {
|
|
||||||
in, err := f.r.getInodeFromEntry(f.entries[i])
|
|
||||||
if err != nil {
|
|
||||||
return nil, &fs.PathError{
|
|
||||||
Op: "readdir",
|
|
||||||
Path: name,
|
|
||||||
Err: err,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ents, err := f.r.readDirFromInode(in)
|
|
||||||
if err != nil {
|
|
||||||
return nil, &fs.PathError{
|
|
||||||
Op: "readdir",
|
|
||||||
Path: name,
|
|
||||||
Err: err,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
out := make([]fs.DirEntry, len(f.entries))
|
|
||||||
for i, ent := range ents {
|
|
||||||
out[i] = &DirEntry{
|
|
||||||
en: ent,
|
|
||||||
parent: &f,
|
|
||||||
r: f.r,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
sub, err := f.Sub(split[0])
|
|
||||||
if err != nil {
|
|
||||||
if pathErr, ok := err.(*fs.PathError); ok {
|
|
||||||
if pathErr.Err == fs.ErrNotExist {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
pathErr.Op = "readir"
|
|
||||||
pathErr.Path = name
|
|
||||||
return nil, pathErr
|
|
||||||
}
|
|
||||||
return nil, &fs.PathError{
|
|
||||||
Op: "readdir",
|
|
||||||
Path: name,
|
|
||||||
Err: err,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
redDir, err := sub.(FS).ReadDir(strings.Join(split[1:], "/"))
|
|
||||||
if err != nil {
|
|
||||||
if pathErr, ok := err.(*fs.PathError); ok {
|
|
||||||
if pathErr.Err == fs.ErrNotExist {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
pathErr.Op = "readdir"
|
|
||||||
pathErr.Path = name
|
|
||||||
return nil, pathErr
|
|
||||||
}
|
|
||||||
return nil, &fs.PathError{
|
|
||||||
Op: "readdir",
|
|
||||||
Path: name,
|
|
||||||
Err: err,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return redDir, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil, &fs.PathError{
|
|
||||||
Op: "readdir",
|
|
||||||
Path: name,
|
|
||||||
Err: fs.ErrNotExist,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//ReadFile returns the data (in []byte) for the file at name.
|
|
||||||
func (f FS) ReadFile(name string) ([]byte, error) {
|
|
||||||
fil, err := f.Open(name)
|
fil, err := f.Open(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if pathErr, ok := err.(*fs.PathError); ok {
|
return nil, err
|
||||||
pathErr.Op = "readfile"
|
|
||||||
pathErr.Path = name
|
|
||||||
return nil, pathErr
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
var buf bytes.Buffer
|
return fil.(*File).ReadDir(-1)
|
||||||
_, err = io.Copy(&buf, fil)
|
}
|
||||||
if err != nil {
|
|
||||||
|
// Returns the contents of the file at name.
|
||||||
|
func (f *FS) ReadFile(name string) (out []byte, err error) {
|
||||||
|
name = filepath.Clean(name)
|
||||||
|
if !fs.ValidPath(name) {
|
||||||
return nil, &fs.PathError{
|
return nil, &fs.PathError{
|
||||||
Op: "readfile",
|
Op: "readfile",
|
||||||
Path: name,
|
Path: name,
|
||||||
Err: err,
|
Err: fs.ErrInvalid,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return buf.Bytes(), nil
|
if name == "." || name == "" {
|
||||||
|
return nil, fs.ErrInvalid
|
||||||
|
}
|
||||||
|
fil, err := f.Open(name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !fil.(*File).IsRegular() {
|
||||||
|
return nil, fs.ErrInvalid
|
||||||
|
}
|
||||||
|
return io.ReadAll(fil)
|
||||||
}
|
}
|
||||||
|
|
||||||
//Stat returns the fs.FileInfo for the file at name.
|
// Returns the fs.FileInfo for the file at name.
|
||||||
func (f FS) Stat(name string) (fs.FileInfo, error) {
|
func (f *FS) Stat(name string) (fs.FileInfo, error) {
|
||||||
|
name = filepath.Clean(name)
|
||||||
if !fs.ValidPath(name) {
|
if !fs.ValidPath(name) {
|
||||||
return nil, &fs.PathError{
|
return nil, &fs.PathError{
|
||||||
Op: "stat",
|
Op: "stat",
|
||||||
@@ -285,205 +203,67 @@ func (f FS) Stat(name string) (fs.FileInfo, error) {
|
|||||||
Err: fs.ErrInvalid,
|
Err: fs.ErrInvalid,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
name = path.Clean(strings.TrimPrefix(name, "/"))
|
if name == "." || name == "" {
|
||||||
split := strings.Split(name, "/")
|
return f.File().Stat()
|
||||||
if split[0] == ".." {
|
|
||||||
if f.parent == nil {
|
|
||||||
//This should only happen on the root FS
|
|
||||||
return nil, &fs.PathError{
|
|
||||||
Op: "stat",
|
|
||||||
Path: name,
|
|
||||||
//TODO: make error clearer
|
|
||||||
Err: errors.New("Trying to get file outside of squashfs"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return f.parent.Stat(strings.Join(split[1:], "/"))
|
|
||||||
}
|
}
|
||||||
for i := 0; i < len(f.entries); i++ {
|
fil, err := f.Open(name)
|
||||||
if match, _ := path.Match(split[0], f.entries[i].Name); match {
|
if err != nil {
|
||||||
if len(split) == 1 {
|
return nil, err
|
||||||
in, err := f.r.getInodeFromEntry(f.entries[i])
|
|
||||||
if err != nil {
|
|
||||||
return nil, &fs.PathError{
|
|
||||||
Op: "stat",
|
|
||||||
Path: name,
|
|
||||||
Err: err,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return FileInfo{
|
|
||||||
i: in,
|
|
||||||
parent: &f,
|
|
||||||
r: f.r,
|
|
||||||
name: f.entries[i].Name,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
sub, err := f.Sub(split[0])
|
|
||||||
if err != nil {
|
|
||||||
if pathErr, ok := err.(*fs.PathError); ok {
|
|
||||||
if pathErr.Err == fs.ErrNotExist {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
pathErr.Op = "stat"
|
|
||||||
pathErr.Path = name
|
|
||||||
return nil, pathErr
|
|
||||||
}
|
|
||||||
return nil, &fs.PathError{
|
|
||||||
Op: "stat",
|
|
||||||
Path: name,
|
|
||||||
Err: err,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
stat, err := sub.(FS).Stat(strings.Join(split[1:], "/"))
|
|
||||||
if err != nil {
|
|
||||||
if pathErr, ok := err.(*fs.PathError); ok {
|
|
||||||
if pathErr.Err == fs.ErrNotExist {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
pathErr.Op = "stat"
|
|
||||||
pathErr.Path = name
|
|
||||||
return nil, pathErr
|
|
||||||
}
|
|
||||||
return nil, &fs.PathError{
|
|
||||||
Op: "stat",
|
|
||||||
Path: name,
|
|
||||||
Err: err,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return stat, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil, &fs.PathError{
|
|
||||||
Op: "stat",
|
|
||||||
Path: name,
|
|
||||||
Err: fs.ErrNotExist,
|
|
||||||
}
|
}
|
||||||
|
return fil.(*File).Stat()
|
||||||
}
|
}
|
||||||
|
|
||||||
//Sub returns the FS at dir
|
// Returns the FS at dir
|
||||||
func (f FS) Sub(dir string) (fs.FS, error) {
|
func (f *FS) Sub(dir string) (fs.FS, error) {
|
||||||
|
dir = filepath.Clean(dir)
|
||||||
if !fs.ValidPath(dir) {
|
if !fs.ValidPath(dir) {
|
||||||
return nil, &fs.PathError{
|
return nil, &fs.PathError{
|
||||||
Op: "sub",
|
Op: "dir",
|
||||||
Path: dir,
|
Path: dir,
|
||||||
Err: fs.ErrInvalid,
|
Err: fs.ErrInvalid,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dir = path.Clean(strings.TrimPrefix(dir, "/"))
|
if dir == "." || dir == "" {
|
||||||
split := strings.Split(dir, "/")
|
return f, nil
|
||||||
if split[0] == ".." {
|
|
||||||
if f.parent == nil {
|
|
||||||
//This should only happen on the root FS
|
|
||||||
return nil, &fs.PathError{
|
|
||||||
Op: "sub",
|
|
||||||
Path: dir,
|
|
||||||
//TODO: make error clearer
|
|
||||||
Err: errors.New("Trying to get file outside of squashfs"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return f.parent.Sub(strings.Join(split[1:], "/"))
|
|
||||||
}
|
}
|
||||||
for i := 0; i < len(f.entries); i++ {
|
fil, err := f.Open(dir)
|
||||||
if match, _ := path.Match(split[0], f.entries[i].Name); match {
|
|
||||||
if len(split) == 1 {
|
|
||||||
in, err := f.r.getInodeFromEntry(f.entries[i])
|
|
||||||
if err != nil {
|
|
||||||
return nil, &fs.PathError{
|
|
||||||
Op: "sub",
|
|
||||||
Path: dir,
|
|
||||||
Err: err,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ents, err := f.r.readDirFromInode(in)
|
|
||||||
if err != nil {
|
|
||||||
return nil, &fs.PathError{
|
|
||||||
Op: "sub",
|
|
||||||
Path: dir,
|
|
||||||
Err: err,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return &FS{
|
|
||||||
r: f.r,
|
|
||||||
parent: &f,
|
|
||||||
name: f.entries[i].Name,
|
|
||||||
entries: ents,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
sub, err := f.Sub(strings.Join(split[1:], "/"))
|
|
||||||
if err != nil {
|
|
||||||
if pathErr, ok := err.(*fs.PathError); ok {
|
|
||||||
if pathErr.Err == fs.ErrNotExist {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
pathErr.Op = "sub"
|
|
||||||
pathErr.Path = dir
|
|
||||||
return nil, pathErr
|
|
||||||
}
|
|
||||||
return nil, &fs.PathError{
|
|
||||||
Op: "sub",
|
|
||||||
Path: dir,
|
|
||||||
Err: err,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return sub, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil, &fs.PathError{
|
|
||||||
Op: "sub",
|
|
||||||
Path: dir,
|
|
||||||
Err: fs.ErrNotExist,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f FS) path() string {
|
|
||||||
return f.parent.path() + "/" + f.name
|
|
||||||
}
|
|
||||||
|
|
||||||
//ExtractTo extracts the File to the given folder with the default options.
|
|
||||||
//It extracts the directory's contents to the folder.
|
|
||||||
func (f FS) ExtractTo(folder string) error {
|
|
||||||
return f.ExtractWithOptions(folder, DefaultOptions())
|
|
||||||
}
|
|
||||||
|
|
||||||
//ExtractSymlink extracts the File to the folder with the DereferenceSymlink option.
|
|
||||||
//It extracts the directory's contents to the folder.
|
|
||||||
func (f FS) ExtractSymlink(folder string) error {
|
|
||||||
return f.ExtractWithOptions(folder, ExtractionOptions{
|
|
||||||
DereferenceSymlink: true,
|
|
||||||
FolderPerm: fs.ModePerm,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
//ExtractWithOptions extracts the File to the given folder with the given ExtrationOptions.
|
|
||||||
//It extracts the directory's contents to the folder.
|
|
||||||
func (f FS) ExtractWithOptions(folder string, op ExtractionOptions) error {
|
|
||||||
op.notBase = true
|
|
||||||
folder = path.Clean(folder)
|
|
||||||
err := os.MkdirAll(folder, op.FolderPerm)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
errChan := make(chan error)
|
if !fil.(*File).IsDir() {
|
||||||
for i := 0; i < len(f.entries); i++ {
|
return nil, &fs.PathError{
|
||||||
go func(ent *DirEntry) {
|
Op: "dir",
|
||||||
fil, goErr := ent.File()
|
Path: dir,
|
||||||
if goErr != nil {
|
Err: fs.ErrInvalid,
|
||||||
errChan <- goErr
|
|
||||||
return
|
|
||||||
}
|
|
||||||
errChan <- fil.ExtractWithOptions(folder, op)
|
|
||||||
fil.Close()
|
|
||||||
return
|
|
||||||
}(&DirEntry{
|
|
||||||
en: f.entries[i],
|
|
||||||
parent: &f,
|
|
||||||
r: f.r,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
for i := 0; i < len(f.entries); i++ {
|
|
||||||
err := <-errChan
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return fil.(*File).FS()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract the FS to the given folder. If the file is a folder, the folder's contents will be extracted to the folder.
|
||||||
|
// Uses default extraction options.
|
||||||
|
func (f *FS) Extract(folder string) error {
|
||||||
|
return f.File().Extract(folder)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract the FS to the given folder. If the file is a folder, the folder's contents will be extracted to the folder.
|
||||||
|
// Allows setting various extraction options via ExtractionOptions.
|
||||||
|
func (f *FS) ExtractWithOptions(folder string, op *ExtractionOptions) error {
|
||||||
|
return f.File().ExtractWithOptions(folder, op)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the FS as a *File
|
||||||
|
func (f *FS) File() *File {
|
||||||
|
return &File{
|
||||||
|
b: &f.d.FileBase,
|
||||||
|
parent: f.parent,
|
||||||
|
r: f.r,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *FS) path() string {
|
||||||
|
if f.parent == nil {
|
||||||
|
return f.d.Name
|
||||||
|
}
|
||||||
|
return filepath.Join(f.parent.path(), f.d.Name)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,11 @@
|
|||||||
module github.com/CalebQ42/squashfs
|
module github.com/CalebQ42/squashfs
|
||||||
|
|
||||||
go 1.15
|
go 1.21.5
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/CalebQ42/GoAppImage v0.5.0
|
github.com/pierrec/lz4/v4 v4.1.19
|
||||||
github.com/adrg/xdg v0.2.3 // indirect
|
github.com/ulikunitz/xz v0.5.11
|
||||||
github.com/google/go-cmp v0.5.4 // indirect
|
github.com/klauspost/compress v1.17.4
|
||||||
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect
|
github.com/rasky/go-lzo v0.0.0-20200203143853-96a758eda86e
|
||||||
github.com/klauspost/compress v1.11.6
|
github.com/therootcompany/xz v1.0.1
|
||||||
github.com/kr/text v0.2.0 // indirect
|
|
||||||
github.com/pierrec/lz4/v4 v4.1.3
|
|
||||||
github.com/smartystreets/assertions v1.2.0 // indirect
|
|
||||||
github.com/stretchr/testify v1.7.0 // indirect
|
|
||||||
github.com/ulikunitz/xz v0.5.9
|
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
|
||||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,76 +1,10 @@
|
|||||||
github.com/CalebQ42/GoAppImage v0.5.0 h1:znoKNXtliH754tS9sYwyOIg/0wFDjFN5Twc7PAh1rSM=
|
github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4=
|
||||||
github.com/CalebQ42/GoAppImage v0.5.0/go.mod h1:qHudJKAn/dlkNWNnH4h1YKXp29EZ7Bppsn7sNP2HuvU=
|
github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
|
||||||
github.com/adrg/xdg v0.2.2 h1:A7ZHKRz5KGOLJX/bg7IPzStryhvCzAE1wX+KWawPiAo=
|
github.com/pierrec/lz4/v4 v4.1.19 h1:tYLzDnjDXh9qIxSTKHwXwOYmm9d887Y7Y1ZkyXYHAN4=
|
||||||
github.com/adrg/xdg v0.2.2/go.mod h1:7I2hH/IT30IsupOpKZ5ue7/qNi3CoKzD6tL3HwpaRMQ=
|
github.com/pierrec/lz4/v4 v4.1.19/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
|
||||||
github.com/adrg/xdg v0.2.3 h1:GxXngdYxNDkoUvZXjNJGwqZxWXi43MKbOOlA/00qZi4=
|
github.com/rasky/go-lzo v0.0.0-20200203143853-96a758eda86e h1:dCWirM5F3wMY+cmRda/B1BiPsFtmzXqV9b0hLWtVBMs=
|
||||||
github.com/adrg/xdg v0.2.3/go.mod h1:7I2hH/IT30IsupOpKZ5ue7/qNi3CoKzD6tL3HwpaRMQ=
|
github.com/rasky/go-lzo v0.0.0-20200203143853-96a758eda86e/go.mod h1:9leZcVcItj6m9/CfHY5Em/iBrCz7js8LcRQGTKEEv2M=
|
||||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
github.com/therootcompany/xz v1.0.1 h1:CmOtsn1CbtmyYiusbfmhmkpAAETj0wBIH6kCYaX+xzw=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/therootcompany/xz v1.0.1/go.mod h1:3K3UH1yCKgBneZYhuQUvJ9HPD19UEXEI0BWbMn8qNMY=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/ulikunitz/xz v0.5.11/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
|
||||||
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
|
|
||||||
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
|
||||||
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
|
|
||||||
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
|
||||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
|
|
||||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
|
|
||||||
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 h1:l5lAOZEym3oK3SQ2HBHWsJUfbNBiTXJDeW2QDxw9AQ0=
|
|
||||||
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/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
|
||||||
github.com/klauspost/compress v1.11.6 h1:EgWPCW6O3n1D5n99Zq3xXBt9uCwRGvpwGOusOLNBRSQ=
|
|
||||||
github.com/klauspost/compress v1.11.6/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
|
|
||||||
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
|
||||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
|
||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
|
||||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
|
||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
|
||||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
|
||||||
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/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
|
||||||
github.com/pierrec/lz4/v4 v4.1.3 h1:/dvQpkb0o1pVlSgKNQqfkavlnXaIK+hJ0LXsKRUN9D4=
|
|
||||||
github.com/pierrec/lz4/v4 v4.1.3/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0/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/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
|
|
||||||
github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs=
|
|
||||||
github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=
|
|
||||||
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
|
|
||||||
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
|
|
||||||
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
|
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
|
||||||
github.com/stretchr/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.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
|
||||||
github.com/ulikunitz/xz v0.5.9 h1:RsKRIA2MO8x56wkkcd3LbtcE/uMszhb6DpRf+3uwa3I=
|
|
||||||
github.com/ulikunitz/xz v0.5.9/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
|
|
||||||
go.lsp.dev/uri v0.3.0 h1:KcZJmh6nFIBeJzTugn5JTU6OOyG0lDOo3R9KwTxTYbo=
|
|
||||||
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/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
|
||||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
|
|
||||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
|
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
|
||||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
|
||||||
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384 h1:TFlARGu6Czu1z7q93HTxcP1P+/ZFC/IKythI5RzrnRg=
|
|
||||||
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
|
||||||
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b h1:QRR6H1YWRnHb4Y/HeNFCTJLFVxaq6wH4YuVdsUOr75U=
|
|
||||||
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
|
||||||
gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU=
|
|
||||||
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
||||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
|
||||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
||||||
|
|||||||
@@ -1,75 +0,0 @@
|
|||||||
package compression
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"compress/zlib"
|
|
||||||
"encoding/binary"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
type gzipInit struct {
|
|
||||||
CompressionLevel int32
|
|
||||||
WindowSize int16
|
|
||||||
Strategies int16
|
|
||||||
}
|
|
||||||
|
|
||||||
//Gzip is a decompressor for gzip type compression. Uses zlib for compression and decompression
|
|
||||||
type Gzip struct {
|
|
||||||
wrt *zlib.Writer
|
|
||||||
gzipInit
|
|
||||||
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
|
|
||||||
err := binary.Read(r, binary.LittleEndian, &gzip.gzipInit)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
//TODO: proper support for window size and strategies
|
|
||||||
gzip.HasCustomWindow = gzip.WindowSize != 15
|
|
||||||
gzip.HasStrategies = gzip.Strategies != 0 && gzip.Strategies != 1
|
|
||||||
return &gzip, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//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) {
|
|
||||||
rdr, err := zlib.NewReader(r)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var data bytes.Buffer
|
|
||||||
_, err = io.Copy(&data, rdr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return data.Bytes(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//Compress compresses the given data (as a byte array) and returns the compressed data.
|
|
||||||
func (g *Gzip) Compress(data []byte) ([]byte, error) {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
var err error
|
|
||||||
if g.wrt == nil {
|
|
||||||
if g.CompressionLevel == 0 {
|
|
||||||
g.wrt = zlib.NewWriter(&buf)
|
|
||||||
} else {
|
|
||||||
g.wrt, err = zlib.NewWriterLevel(&buf, int(g.CompressionLevel))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
wrt, err := zlib.NewWriterLevel(&buf, int(g.CompressionLevel))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
_, err = wrt.Write(data)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
wrt.Close()
|
|
||||||
return buf.Bytes(), nil
|
|
||||||
}
|
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
//Compress implements compression.Compress
|
|
||||||
func (l *Lz4) Compress(data []byte) ([]byte, error) {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
w := lz4.NewWriter(&buf)
|
|
||||||
if l.HC {
|
|
||||||
err := w.Apply(lz4.CompressionLevelOption(lz4.Level9))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_, err := w.Write(data)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
w.Close()
|
|
||||||
return buf.Bytes(), nil
|
|
||||||
}
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
package compression
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/ulikunitz/xz/lzma"
|
|
||||||
)
|
|
||||||
|
|
||||||
//Lzma is a lzma decompressor
|
|
||||||
type Lzma struct{}
|
|
||||||
|
|
||||||
//Decompress decompresses all the data in the given reader and returns the uncompressed bytes.
|
|
||||||
func (l *Lzma) Decompress(rdr io.Reader) ([]byte, error) {
|
|
||||||
r, err := lzma.NewReader(rdr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var buf bytes.Buffer
|
|
||||||
_, err = io.Copy(&buf, r)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return buf.Bytes(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//Compress implements compression.Compress
|
|
||||||
func (l *Lzma) Compress(data []byte) ([]byte, error) {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
w, err := lzma.NewWriter(&buf)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
_, err = w.Write(data)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
w.Close()
|
|
||||||
return buf.Bytes(), nil
|
|
||||||
}
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
package compression
|
|
||||||
|
|
||||||
import "io"
|
|
||||||
|
|
||||||
//Compressor is a squashfs decompressor interface. Allows for easy compression.
|
|
||||||
type Compressor interface {
|
|
||||||
Compress([]byte) ([]byte, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
//Decompressor is a squashfs decompressor interface. Allows for easy decompression no matter the type of compression.
|
|
||||||
type Decompressor interface {
|
|
||||||
Decompress(io.Reader) ([]byte, error)
|
|
||||||
}
|
|
||||||
@@ -1,75 +0,0 @@
|
|||||||
package compression
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/binary"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/ulikunitz/xz"
|
|
||||||
)
|
|
||||||
|
|
||||||
type xzInit struct {
|
|
||||||
DictionarySize int32
|
|
||||||
Filters int32
|
|
||||||
}
|
|
||||||
|
|
||||||
//Xz is a Xz decompressor.
|
|
||||||
type Xz struct {
|
|
||||||
DictionarySize int32
|
|
||||||
HasFilters bool
|
|
||||||
}
|
|
||||||
|
|
||||||
//NewXzCompressorWithOptions creates a new Xz compressor/decompressor that reads the compressor options from the given reader.
|
|
||||||
func NewXzCompressorWithOptions(rdr io.Reader) (*Xz, error) {
|
|
||||||
var x Xz
|
|
||||||
var init xzInit
|
|
||||||
err := binary.Read(rdr, binary.LittleEndian, &init)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
x.DictionarySize = init.DictionarySize
|
|
||||||
//TODO: When I can do filters, parse the filters
|
|
||||||
if init.Filters != 0 {
|
|
||||||
x.HasFilters = true
|
|
||||||
}
|
|
||||||
return &x, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//Decompress decompresses all the data from the rdr and returns the uncompressed bytes.
|
|
||||||
func (x *Xz) Decompress(rdr io.Reader) ([]byte, error) {
|
|
||||||
r, err := xz.NewReader(rdr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
r.DictCap = int(x.DictionarySize)
|
|
||||||
err = r.Verify()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var buf bytes.Buffer
|
|
||||||
_, err = io.Copy(&buf, r)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return buf.Bytes(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//Compress implements compression.Compress
|
|
||||||
func (x *Xz) Compress(data []byte) ([]byte, error) {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
w, err := xz.NewWriter(&buf)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
w.DictCap = int(x.DictionarySize)
|
|
||||||
err = w.Verify()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
_, err = w.Write(data)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
w.Close()
|
|
||||||
return buf.Bytes(), nil
|
|
||||||
}
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
//Compress impelements compression.Compress
|
|
||||||
func (z *Zstd) Compress(data []byte) ([]byte, error) {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
w, err := zstd.NewWriter(&buf, zstd.WithEncoderLevel(zstd.EncoderLevel(z.CompressionLevel)))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
_, err = w.Write(data)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
w.Close()
|
|
||||||
return buf.Bytes(), nil
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package decompress
|
||||||
|
|
||||||
|
type Decompressor interface {
|
||||||
|
Decompress([]byte) ([]byte, error)
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package decompress
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/pierrec/lz4/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Lz4 struct{}
|
||||||
|
|
||||||
|
func (l Lz4) Decompress(data []byte) ([]byte, error) {
|
||||||
|
rdr := lz4.NewReader(bytes.NewReader(data))
|
||||||
|
return io.ReadAll(rdr)
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package decompress
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/ulikunitz/xz/lzma"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Lzma struct{}
|
||||||
|
|
||||||
|
func (l Lzma) Decompress(data []byte) ([]byte, error) {
|
||||||
|
rdr, err := lzma.NewReader(bytes.NewReader(data))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return io.ReadAll(rdr)
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package decompress
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
|
||||||
|
"github.com/rasky/go-lzo"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Lzo struct{}
|
||||||
|
|
||||||
|
func (l Lzo) Decompress(data []byte) ([]byte, error) {
|
||||||
|
return lzo.Decompress1X(bytes.NewReader(data), len(data), 0)
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package decompress
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/therootcompany/xz"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Xz struct{}
|
||||||
|
|
||||||
|
func (x Xz) Decompress(data []byte) ([]byte, error) {
|
||||||
|
rdr, err := xz.NewReader(bytes.NewReader(data), 0)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return io.ReadAll(rdr)
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package decompress
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"compress/zlib"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Zlib struct{}
|
||||||
|
|
||||||
|
func (z Zlib) Decompress(data []byte) ([]byte, error) {
|
||||||
|
rdr, err := zlib.NewReader(bytes.NewReader(data))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rdr.Close()
|
||||||
|
return io.ReadAll(rdr)
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package decompress
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/klauspost/compress/zstd"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Zstd struct{}
|
||||||
|
|
||||||
|
func (z Zstd) Decompress(data []byte) ([]byte, error) {
|
||||||
|
rdr, err := zstd.NewReader(bytes.NewReader(data))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rdr.Close()
|
||||||
|
return io.ReadAll(rdr)
|
||||||
|
}
|
||||||
@@ -1,87 +0,0 @@
|
|||||||
package directory
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/binary"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
//Header is the header for a directory in the directory table
|
|
||||||
type Header struct {
|
|
||||||
Count uint32
|
|
||||||
InodeOffset uint32
|
|
||||||
InodeNumber uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
//EntryRaw is the values that can be easily decoded
|
|
||||||
type EntryRaw struct {
|
|
||||||
Offset uint16
|
|
||||||
InodeOffset int16
|
|
||||||
Type uint16
|
|
||||||
NameSize uint16
|
|
||||||
}
|
|
||||||
|
|
||||||
//Entry is an entry in a directory.
|
|
||||||
type Entry struct {
|
|
||||||
Name string
|
|
||||||
InodeOffset uint32
|
|
||||||
InodeBlockOffset uint16
|
|
||||||
Type uint16
|
|
||||||
}
|
|
||||||
|
|
||||||
//NewEntry creates a new directory entry
|
|
||||||
func NewEntry(rdr io.Reader) (*Entry, error) {
|
|
||||||
var raw EntryRaw
|
|
||||||
err := binary.Read(rdr, binary.LittleEndian, &raw)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
tmp := make([]byte, raw.NameSize+1)
|
|
||||||
err = binary.Read(rdr, binary.LittleEndian, &tmp)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &Entry{
|
|
||||||
InodeBlockOffset: raw.Offset,
|
|
||||||
Type: raw.Type,
|
|
||||||
Name: string(tmp),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//NewDirectory reads the directory from rdr
|
|
||||||
func NewDirectory(base io.Reader, size uint32) (entries []*Entry, err error) {
|
|
||||||
tmp := make([]byte, size)
|
|
||||||
base.Read(tmp)
|
|
||||||
rdr := bytes.NewBuffer(tmp)
|
|
||||||
for {
|
|
||||||
var hdr Header
|
|
||||||
err = binary.Read(rdr, binary.LittleEndian, &hdr)
|
|
||||||
if err == io.ErrUnexpectedEOF {
|
|
||||||
err = nil
|
|
||||||
break
|
|
||||||
} else if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
hdr.Count++
|
|
||||||
headers := hdr.Count / 256
|
|
||||||
if hdr.Count%256 > 0 {
|
|
||||||
headers++
|
|
||||||
}
|
|
||||||
for i := uint32(0); i < hdr.Count; i++ {
|
|
||||||
if i != 0 && i%256 == 0 {
|
|
||||||
err = binary.Read(rdr, binary.LittleEndian, &hdr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var ent *Entry
|
|
||||||
ent, err = NewEntry(rdr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
ent.InodeOffset = hdr.InodeOffset
|
|
||||||
entries = append(entries, ent)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
@@ -1,257 +0,0 @@
|
|||||||
package inode
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/binary"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
//The different types of inodes as defined by inodetype
|
|
||||||
const (
|
|
||||||
DirType = iota + 1
|
|
||||||
FileType
|
|
||||||
SymType
|
|
||||||
BlockDevType
|
|
||||||
CharDevType
|
|
||||||
FifoType
|
|
||||||
SocketType
|
|
||||||
ExtDirType
|
|
||||||
ExtFileType
|
|
||||||
ExtSymType
|
|
||||||
ExtBlockDeviceType
|
|
||||||
ExtCharDeviceType
|
|
||||||
ExtFifoType
|
|
||||||
ExtSocketType
|
|
||||||
)
|
|
||||||
|
|
||||||
//Header is the common header for all inodes
|
|
||||||
type Header struct {
|
|
||||||
Type uint16
|
|
||||||
Permissions uint16
|
|
||||||
UID uint16
|
|
||||||
GID uint16
|
|
||||||
ModifiedTime uint32
|
|
||||||
Number uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
//Dir is self explainatory
|
|
||||||
type Dir struct {
|
|
||||||
DirectoryIndex uint32
|
|
||||||
HardLinks uint32
|
|
||||||
DirectorySize uint16
|
|
||||||
DirectoryOffset uint16
|
|
||||||
ParentInodeNumber uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
//ExtDirInit is the information that can be directoy decoded
|
|
||||||
type ExtDirInit struct {
|
|
||||||
HardLinks uint32
|
|
||||||
DirectorySize uint32
|
|
||||||
DirectoryIndex uint32
|
|
||||||
ParentInodeNumber uint32
|
|
||||||
IndexCount uint16 //one less then directory indexes following structure
|
|
||||||
DirectoryOffset uint16
|
|
||||||
XattrIndex uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
//ExtDir is a directory with extra info
|
|
||||||
type ExtDir struct {
|
|
||||||
Indexes []DirIndex
|
|
||||||
ExtDirInit
|
|
||||||
}
|
|
||||||
|
|
||||||
//NewExtendedDirectory creates a new ExtendedDirectory
|
|
||||||
func NewExtendedDirectory(rdr io.Reader) (ExtDir, error) {
|
|
||||||
var inode ExtDir
|
|
||||||
err := binary.Read(rdr, binary.LittleEndian, &inode.ExtDirInit)
|
|
||||||
if err != nil {
|
|
||||||
return inode, err
|
|
||||||
}
|
|
||||||
for i := uint16(0); i < inode.IndexCount; i++ {
|
|
||||||
var tmp DirIndex
|
|
||||||
tmp, err = NewDirectoryIndex(rdr)
|
|
||||||
if err != nil {
|
|
||||||
return inode, err
|
|
||||||
}
|
|
||||||
inode.Indexes = append(inode.Indexes, tmp)
|
|
||||||
}
|
|
||||||
return inode, err
|
|
||||||
}
|
|
||||||
|
|
||||||
//DirIndexInit holds the values that can be easily decoded
|
|
||||||
type DirIndexInit struct {
|
|
||||||
Offset uint32
|
|
||||||
DirTableOffset uint32
|
|
||||||
NameSize uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
//DirIndex is a quick lookup provided by an ExtendedDirectory
|
|
||||||
type DirIndex struct {
|
|
||||||
Name string
|
|
||||||
DirIndexInit
|
|
||||||
}
|
|
||||||
|
|
||||||
//NewDirectoryIndex return a new DirectoryIndex
|
|
||||||
func NewDirectoryIndex(rdr io.Reader) (DirIndex, error) {
|
|
||||||
var index DirIndex
|
|
||||||
err := binary.Read(rdr, binary.LittleEndian, &index.DirIndexInit)
|
|
||||||
if err != nil {
|
|
||||||
return index, err
|
|
||||||
}
|
|
||||||
tmp := make([]byte, index.NameSize+1, index.NameSize+1)
|
|
||||||
err = binary.Read(rdr, binary.LittleEndian, &tmp)
|
|
||||||
if err != nil {
|
|
||||||
return index, err
|
|
||||||
}
|
|
||||||
index.Name = string(tmp)
|
|
||||||
return index, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//FileInit is the information that can be directly decoded
|
|
||||||
type FileInit struct {
|
|
||||||
BlockStart uint32
|
|
||||||
FragmentIndex uint32
|
|
||||||
FragmentOffset uint32
|
|
||||||
Size uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
//File is self explainatory
|
|
||||||
type File struct {
|
|
||||||
BlockSizes []uint32
|
|
||||||
Fragmented bool
|
|
||||||
FileInit
|
|
||||||
}
|
|
||||||
|
|
||||||
//NewFile creates a new File
|
|
||||||
func NewFile(rdr io.Reader, blockSize uint32) (File, error) {
|
|
||||||
var inode File
|
|
||||||
err := binary.Read(rdr, binary.LittleEndian, &inode.FileInit)
|
|
||||||
if err != nil {
|
|
||||||
return inode, err
|
|
||||||
}
|
|
||||||
inode.Fragmented = inode.FragmentIndex != 0xFFFFFFFF
|
|
||||||
blocks := inode.Size / blockSize
|
|
||||||
if inode.Size%blockSize > 0 {
|
|
||||||
blocks++
|
|
||||||
}
|
|
||||||
inode.BlockSizes = make([]uint32, blocks, blocks)
|
|
||||||
err = binary.Read(rdr, binary.LittleEndian, &inode.BlockSizes)
|
|
||||||
return inode, err
|
|
||||||
}
|
|
||||||
|
|
||||||
//ExtFileInit is the information that can be directly decoded
|
|
||||||
type ExtFileInit struct {
|
|
||||||
BlockStart uint64
|
|
||||||
Size uint64
|
|
||||||
Sparse uint64
|
|
||||||
HardLinks uint32
|
|
||||||
FragmentIndex uint32
|
|
||||||
FragmentOffset uint32
|
|
||||||
XattrIndex uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
//ExtFile is a file with more information
|
|
||||||
type ExtFile struct {
|
|
||||||
BlockSizes []uint32
|
|
||||||
Fragmented bool
|
|
||||||
ExtFileInit
|
|
||||||
}
|
|
||||||
|
|
||||||
//NewExtendedFile creates a new ExtendedFile
|
|
||||||
func NewExtendedFile(rdr io.Reader, blockSize uint32) (ExtFile, error) {
|
|
||||||
var inode ExtFile
|
|
||||||
err := binary.Read(rdr, binary.LittleEndian, &inode.ExtFileInit)
|
|
||||||
if err != nil {
|
|
||||||
return inode, err
|
|
||||||
}
|
|
||||||
inode.Fragmented = inode.FragmentIndex != 0xFFFFFFFF
|
|
||||||
blocks := inode.Size / uint64(blockSize)
|
|
||||||
if inode.Size%uint64(blockSize) > 0 {
|
|
||||||
blocks++
|
|
||||||
}
|
|
||||||
inode.BlockSizes = make([]uint32, blocks, blocks)
|
|
||||||
err = binary.Read(rdr, binary.LittleEndian, &inode.BlockSizes)
|
|
||||||
return inode, err
|
|
||||||
}
|
|
||||||
|
|
||||||
//SymInit is all the values that can be directly decoded
|
|
||||||
type SymInit struct {
|
|
||||||
HardLinks uint32
|
|
||||||
TargetPathSize uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
//Sym is a symlink
|
|
||||||
type Sym struct {
|
|
||||||
Path string
|
|
||||||
targetPath []byte //len is TargetPathSize
|
|
||||||
SymInit
|
|
||||||
}
|
|
||||||
|
|
||||||
//NewSymlink creates a new Symlink
|
|
||||||
func NewSymlink(rdr io.Reader) (Sym, error) {
|
|
||||||
var inode Sym
|
|
||||||
err := binary.Read(rdr, binary.LittleEndian, &inode.SymInit)
|
|
||||||
if err != nil {
|
|
||||||
return inode, err
|
|
||||||
}
|
|
||||||
inode.targetPath = make([]byte, inode.TargetPathSize, inode.TargetPathSize)
|
|
||||||
err = binary.Read(rdr, binary.LittleEndian, &inode.targetPath)
|
|
||||||
if err != nil {
|
|
||||||
return inode, err
|
|
||||||
}
|
|
||||||
inode.Path = string(inode.targetPath)
|
|
||||||
return inode, err
|
|
||||||
}
|
|
||||||
|
|
||||||
//ExtSymInit is all the values that can be directly decoded
|
|
||||||
type ExtSymInit struct {
|
|
||||||
HardLinks uint32
|
|
||||||
TargetPathSize uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
//ExtSym is a symlink with extra information
|
|
||||||
type ExtSym struct {
|
|
||||||
Path string
|
|
||||||
targetPath []uint8
|
|
||||||
ExtSymInit
|
|
||||||
XattrIndex uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
//NewExtendedSymlink creates a new ExtendedSymlink
|
|
||||||
func NewExtendedSymlink(rdr io.Reader) (ExtSym, error) {
|
|
||||||
var inode ExtSym
|
|
||||||
err := binary.Read(rdr, binary.LittleEndian, &inode.ExtSymInit)
|
|
||||||
if err != nil {
|
|
||||||
return inode, err
|
|
||||||
}
|
|
||||||
inode.targetPath = make([]uint8, inode.TargetPathSize, inode.TargetPathSize)
|
|
||||||
err = binary.Read(rdr, binary.LittleEndian, &inode.targetPath)
|
|
||||||
if err != nil {
|
|
||||||
return inode, err
|
|
||||||
}
|
|
||||||
inode.Path = string(inode.targetPath)
|
|
||||||
err = binary.Read(rdr, binary.LittleEndian, &inode.XattrIndex)
|
|
||||||
return inode, err
|
|
||||||
}
|
|
||||||
|
|
||||||
//Device is a device
|
|
||||||
type Device struct {
|
|
||||||
HardLinks uint32
|
|
||||||
Device uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
//ExtDevice is a device with more info
|
|
||||||
type ExtDevice struct {
|
|
||||||
Device
|
|
||||||
XattrIndex uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
//IPC is a Fifo or Socket device
|
|
||||||
type IPC struct {
|
|
||||||
HardLink uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
//ExtIPC is a IPC device with extra info
|
|
||||||
type ExtIPC struct {
|
|
||||||
IPC
|
|
||||||
XattrIndex uint32
|
|
||||||
}
|
|
||||||
@@ -1,128 +0,0 @@
|
|||||||
package inode
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/binary"
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
"strconv"
|
|
||||||
)
|
|
||||||
|
|
||||||
//Inode holds an inode. Header is the header that's common for all inodes.
|
|
||||||
//
|
|
||||||
//Info holds the actual Inode. Due to each inode type being a different type, it's store as an interface{}
|
|
||||||
type Inode struct {
|
|
||||||
Info interface{} //Info is the parsed specific data. It's type is defined by Type.
|
|
||||||
Header
|
|
||||||
}
|
|
||||||
|
|
||||||
//ProcessInode tries to read an inode from the BlockReader
|
|
||||||
func ProcessInode(br io.Reader, blockSize uint32) (*Inode, error) {
|
|
||||||
var in Inode
|
|
||||||
err := binary.Read(br, binary.LittleEndian, &in.Header)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
switch in.Type {
|
|
||||||
case DirType:
|
|
||||||
var inode Dir
|
|
||||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
in.Info = inode
|
|
||||||
case FileType:
|
|
||||||
var inode File
|
|
||||||
inode, err = NewFile(br, blockSize)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
in.Info = inode
|
|
||||||
case SymType:
|
|
||||||
var inode Sym
|
|
||||||
inode, err = NewSymlink(br)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
in.Info = inode
|
|
||||||
case BlockDevType:
|
|
||||||
var inode Device
|
|
||||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
in.Info = inode
|
|
||||||
case CharDevType:
|
|
||||||
var inode Device
|
|
||||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
in.Info = inode
|
|
||||||
case FifoType:
|
|
||||||
var inode IPC
|
|
||||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
in.Info = inode
|
|
||||||
case SocketType:
|
|
||||||
var inode IPC
|
|
||||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
in.Info = inode
|
|
||||||
case ExtDirType:
|
|
||||||
var inode ExtDir
|
|
||||||
inode, err = NewExtendedDirectory(br)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
in.Info = inode
|
|
||||||
case ExtFileType:
|
|
||||||
var inode ExtFile
|
|
||||||
inode, err = NewExtendedFile(br, blockSize)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
in.Info = inode
|
|
||||||
case ExtSymType:
|
|
||||||
var inode ExtSym
|
|
||||||
inode, err = NewExtendedSymlink(br)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
in.Info = inode
|
|
||||||
case ExtBlockDeviceType:
|
|
||||||
var inode ExtDevice
|
|
||||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
in.Info = inode
|
|
||||||
case ExtCharDeviceType:
|
|
||||||
var inode ExtDevice
|
|
||||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
in.Info = inode
|
|
||||||
case ExtFifoType:
|
|
||||||
var inode ExtIPC
|
|
||||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
in.Info = inode
|
|
||||||
case ExtSocketType:
|
|
||||||
var inode ExtIPC
|
|
||||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
in.Info = inode
|
|
||||||
default:
|
|
||||||
return nil, errors.New("Unsupported inode type: " + strconv.Itoa(int(in.Type)))
|
|
||||||
}
|
|
||||||
return &in, nil
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package metadata
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/CalebQ42/squashfs/internal/decompress"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Reader struct {
|
||||||
|
r io.Reader
|
||||||
|
d decompress.Decompressor
|
||||||
|
dat []byte
|
||||||
|
curOffset uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewReader(r io.Reader, d decompress.Decompressor) *Reader {
|
||||||
|
return &Reader{
|
||||||
|
r: r,
|
||||||
|
d: d,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reader) advance() error {
|
||||||
|
r.curOffset = 0
|
||||||
|
var size uint16
|
||||||
|
err := binary.Read(r.r, binary.LittleEndian, &size)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
realSize := size &^ 0x8000
|
||||||
|
r.dat = make([]byte, realSize)
|
||||||
|
err = binary.Read(r.r, binary.LittleEndian, &r.dat)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if size != realSize {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
r.dat, err = r.d.Decompress(r.dat)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reader) Read(b []byte) (int, error) {
|
||||||
|
curRead := 0
|
||||||
|
var toRead int
|
||||||
|
for curRead < len(b) {
|
||||||
|
if r.curOffset >= uint16(len(r.dat)) {
|
||||||
|
if err := r.advance(); err != nil {
|
||||||
|
return curRead, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
toRead = len(b) - curRead
|
||||||
|
if toRead > len(r.dat)-int(r.curOffset) {
|
||||||
|
toRead = len(r.dat) - int(r.curOffset)
|
||||||
|
}
|
||||||
|
copy(b[curRead:], r.dat[r.curOffset:int(r.curOffset)+toRead])
|
||||||
|
r.curOffset += uint16(toRead)
|
||||||
|
curRead += toRead
|
||||||
|
}
|
||||||
|
return curRead, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reader) Close() error {
|
||||||
|
r.dat = nil
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package routinemanager
|
||||||
|
|
||||||
|
type Manager struct {
|
||||||
|
channel chan uint16
|
||||||
|
maxRoutines uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewManager(maxRoutines uint16) *Manager {
|
||||||
|
m := &Manager{
|
||||||
|
maxRoutines: maxRoutines,
|
||||||
|
channel: make(chan uint16, maxRoutines),
|
||||||
|
}
|
||||||
|
for i := uint16(0); i < maxRoutines; i++ {
|
||||||
|
m.channel <- i
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Manager) Lock() uint16 {
|
||||||
|
return <-m.channel
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Manager) Unlock(i uint16) {
|
||||||
|
m.channel <- i
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package toreader
|
||||||
|
|
||||||
|
import "io"
|
||||||
|
|
||||||
|
type Reader struct {
|
||||||
|
r io.ReaderAt
|
||||||
|
offset int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewReader(r io.ReaderAt, start int64) *Reader {
|
||||||
|
return &Reader{
|
||||||
|
r: r,
|
||||||
|
offset: start,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reader) Read(b []byte) (int, error) {
|
||||||
|
n, err := r.r.ReadAt(b, r.offset)
|
||||||
|
r.offset += int64(n)
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# Lower-Level Squashfs
|
||||||
|
|
||||||
|
This library is a lower level version of the main [squashfs](https://github.com/CalebQ42/squashfs) library that doesn't try to be easy to use and exposes a lot of information that is not necesary for must use cases.
|
||||||
@@ -0,0 +1,174 @@
|
|||||||
|
package data
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"math"
|
||||||
|
"runtime"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/CalebQ42/squashfs/internal/decompress"
|
||||||
|
"github.com/CalebQ42/squashfs/internal/toreader"
|
||||||
|
)
|
||||||
|
|
||||||
|
type FragReaderConstructor func() (io.Reader, error)
|
||||||
|
|
||||||
|
type FullReader struct {
|
||||||
|
r io.ReaderAt
|
||||||
|
d decompress.Decompressor
|
||||||
|
frag FragReaderConstructor
|
||||||
|
retPool *sync.Pool
|
||||||
|
sizes []uint32
|
||||||
|
initialOffset int64
|
||||||
|
finalBlockSize uint64
|
||||||
|
blockSize uint32
|
||||||
|
goroutineLimit uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFullReader(r io.ReaderAt, initialOffset int64, d decompress.Decompressor, sizes []uint32, finalBlockSize uint64, blockSize uint32) *FullReader {
|
||||||
|
return &FullReader{
|
||||||
|
r: r,
|
||||||
|
d: d,
|
||||||
|
sizes: sizes,
|
||||||
|
initialOffset: initialOffset,
|
||||||
|
goroutineLimit: uint16(runtime.NumCPU()),
|
||||||
|
finalBlockSize: finalBlockSize,
|
||||||
|
blockSize: blockSize,
|
||||||
|
retPool: &sync.Pool{
|
||||||
|
New: func() any {
|
||||||
|
return &retValue{}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *FullReader) AddFrag(frag FragReaderConstructor) {
|
||||||
|
r.frag = frag
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *FullReader) SetGoroutineLimit(limit uint16) {
|
||||||
|
r.goroutineLimit = limit
|
||||||
|
}
|
||||||
|
|
||||||
|
type retValue struct {
|
||||||
|
err error
|
||||||
|
data []byte
|
||||||
|
index uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *FullReader) process(index uint64, fileOffset uint64, retChan chan *retValue) {
|
||||||
|
ret := r.retPool.Get().(*retValue)
|
||||||
|
ret.index = index
|
||||||
|
realSize := r.sizes[index] &^ (1 << 24)
|
||||||
|
if realSize == 0 {
|
||||||
|
if index == uint64(len(r.sizes))-1 && r.frag == nil {
|
||||||
|
ret.data = make([]byte, r.finalBlockSize)
|
||||||
|
} else {
|
||||||
|
ret.data = make([]byte, r.blockSize)
|
||||||
|
}
|
||||||
|
ret.err = nil
|
||||||
|
retChan <- ret
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ret.data = make([]byte, realSize)
|
||||||
|
ret.err = binary.Read(toreader.NewReader(r.r, int64(r.initialOffset)+int64(fileOffset)), binary.LittleEndian, &ret.data)
|
||||||
|
if r.sizes[index] == realSize {
|
||||||
|
ret.data, ret.err = r.d.Decompress(ret.data)
|
||||||
|
}
|
||||||
|
retChan <- ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *FullReader) WriteTo(w io.Writer) (int64, error) {
|
||||||
|
var curIndex uint64
|
||||||
|
var curOffset uint64
|
||||||
|
var toProcess uint16
|
||||||
|
var wrote int64
|
||||||
|
cache := make(map[uint64]*retValue)
|
||||||
|
var errCache []error
|
||||||
|
retChan := make(chan *retValue, r.goroutineLimit)
|
||||||
|
for i := uint64(0); i < uint64(math.Ceil(float64(len(r.sizes))/float64(r.goroutineLimit))); i++ {
|
||||||
|
toProcess = uint16(len(r.sizes)) - (uint16(i) * r.goroutineLimit)
|
||||||
|
if toProcess > r.goroutineLimit {
|
||||||
|
toProcess = r.goroutineLimit
|
||||||
|
}
|
||||||
|
// Start all the goroutines
|
||||||
|
for j := uint16(0); j < toProcess; j++ {
|
||||||
|
go r.process((i*uint64(r.goroutineLimit))+uint64(j), curOffset, retChan)
|
||||||
|
curOffset += uint64(r.sizes[(i*uint64(r.goroutineLimit))+uint64(j)]) &^ (1 << 24)
|
||||||
|
}
|
||||||
|
// Then consume the results on retChan
|
||||||
|
for j := uint16(0); j < toProcess; j++ {
|
||||||
|
res := <-retChan
|
||||||
|
// If there's an error, we don't care about the results.
|
||||||
|
if res.err != nil {
|
||||||
|
errCache = append(errCache, res.err)
|
||||||
|
if len(cache) > 0 {
|
||||||
|
clear(cache)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// If there has been an error previously, we don't care about the results.
|
||||||
|
// We still want to wait for all the goroutines to prevent resources being wasted.
|
||||||
|
if len(errCache) > 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// If we don't need the data yet, we cache it and move on
|
||||||
|
if res.index != curIndex {
|
||||||
|
cache[res.index] = res
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// If we do need the data, we write it
|
||||||
|
wr, err := w.Write(res.data)
|
||||||
|
wrote += int64(wr)
|
||||||
|
if err != nil {
|
||||||
|
errCache = append(errCache, err)
|
||||||
|
if len(cache) > 0 {
|
||||||
|
clear(cache)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
r.retPool.Put(res)
|
||||||
|
curIndex++
|
||||||
|
// Now we recursively try to clear the cache
|
||||||
|
for len(cache) > 0 {
|
||||||
|
res, ok := cache[curIndex]
|
||||||
|
if !ok {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
wr, err := w.Write(res.data)
|
||||||
|
wrote += int64(wr)
|
||||||
|
if err != nil {
|
||||||
|
errCache = append(errCache, err)
|
||||||
|
if len(cache) > 0 {
|
||||||
|
clear(cache)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
delete(cache, curIndex)
|
||||||
|
r.retPool.Put(res)
|
||||||
|
curIndex++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(errCache) > 0 {
|
||||||
|
return wrote, errors.Join(errCache...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if r.frag != nil {
|
||||||
|
rdr, err := r.frag()
|
||||||
|
if err != nil {
|
||||||
|
return wrote, err
|
||||||
|
}
|
||||||
|
wr, err := io.Copy(w, rdr)
|
||||||
|
wrote += wr
|
||||||
|
if l, ok := rdr.(*io.LimitedReader); ok {
|
||||||
|
if cl, ok := l.R.(io.Closer); ok {
|
||||||
|
cl.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return wrote, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return wrote, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,97 @@
|
|||||||
|
package data
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/CalebQ42/squashfs/internal/decompress"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Reader struct {
|
||||||
|
r io.Reader
|
||||||
|
d decompress.Decompressor
|
||||||
|
frag io.Reader
|
||||||
|
sizes []uint32
|
||||||
|
dat []byte
|
||||||
|
curOffset int
|
||||||
|
curIndex uint64
|
||||||
|
finalBlockSize uint64
|
||||||
|
blockSize uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewReader(r io.Reader, d decompress.Decompressor, sizes []uint32, finalBlockSize uint64, blockSize uint32) *Reader {
|
||||||
|
return &Reader{
|
||||||
|
r: r,
|
||||||
|
d: d,
|
||||||
|
sizes: sizes,
|
||||||
|
finalBlockSize: finalBlockSize,
|
||||||
|
blockSize: blockSize,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reader) AddFrag(fragRdr io.Reader) {
|
||||||
|
r.frag = fragRdr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reader) advance() error {
|
||||||
|
r.curOffset = 0
|
||||||
|
defer func() { r.curIndex++ }()
|
||||||
|
var err error
|
||||||
|
if r.curIndex == uint64(len(r.sizes)) && r.frag != nil {
|
||||||
|
r.dat, err = io.ReadAll(r.frag)
|
||||||
|
return err
|
||||||
|
} else if r.curIndex >= uint64(len(r.sizes)) {
|
||||||
|
return io.EOF
|
||||||
|
}
|
||||||
|
realSize := r.sizes[r.curIndex] &^ (1 << 24)
|
||||||
|
if realSize == 0 {
|
||||||
|
if r.curIndex == uint64(len(r.sizes))-1 && r.frag == nil {
|
||||||
|
r.dat = make([]byte, r.finalBlockSize)
|
||||||
|
} else {
|
||||||
|
r.dat = make([]byte, r.blockSize)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
r.dat = make([]byte, realSize)
|
||||||
|
err = binary.Read(r.r, binary.LittleEndian, &r.dat)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if r.sizes[r.curIndex] != realSize {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
r.dat, err = r.d.Decompress(r.dat)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reader) Read(b []byte) (int, error) {
|
||||||
|
curRead := 0
|
||||||
|
var toRead int
|
||||||
|
for curRead < len(b) {
|
||||||
|
if r.curOffset >= len(r.dat) {
|
||||||
|
if err := r.advance(); err != nil {
|
||||||
|
return curRead, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
toRead = len(b) - curRead
|
||||||
|
if toRead > len(r.dat)-r.curOffset {
|
||||||
|
toRead = len(r.dat) - r.curOffset
|
||||||
|
}
|
||||||
|
toRead = copy(b[curRead:], r.dat[r.curOffset:r.curOffset+toRead])
|
||||||
|
r.curOffset += toRead
|
||||||
|
curRead += toRead
|
||||||
|
}
|
||||||
|
return curRead, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reader) Close() error {
|
||||||
|
if r.frag != nil {
|
||||||
|
if l, ok := r.frag.(*io.LimitedReader); ok {
|
||||||
|
if cl, ok := l.R.(io.Closer); ok {
|
||||||
|
cl.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
r.dat = nil
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
package squashfslow
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"io/fs"
|
||||||
|
"path/filepath"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/CalebQ42/squashfs/internal/metadata"
|
||||||
|
"github.com/CalebQ42/squashfs/internal/toreader"
|
||||||
|
"github.com/CalebQ42/squashfs/low/directory"
|
||||||
|
"github.com/CalebQ42/squashfs/low/inode"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Directory struct {
|
||||||
|
FileBase
|
||||||
|
Entries []directory.Entry
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reader) directoryFromRef(ref uint64, name string) (*Directory, error) {
|
||||||
|
i, err := r.InodeFromRef(ref)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var blockStart uint32
|
||||||
|
var size uint32
|
||||||
|
var offset uint16
|
||||||
|
switch i.Type {
|
||||||
|
case inode.Dir:
|
||||||
|
blockStart = i.Data.(inode.Directory).BlockStart
|
||||||
|
size = uint32(i.Data.(inode.Directory).Size)
|
||||||
|
offset = i.Data.(inode.Directory).Offset
|
||||||
|
case inode.EDir:
|
||||||
|
blockStart = i.Data.(inode.EDirectory).BlockStart
|
||||||
|
size = i.Data.(inode.EDirectory).Size
|
||||||
|
offset = i.Data.(inode.EDirectory).Offset
|
||||||
|
default:
|
||||||
|
return nil, errors.New("not a directory")
|
||||||
|
}
|
||||||
|
dirRdr := metadata.NewReader(toreader.NewReader(r.r, int64(r.Superblock.DirTableStart)+int64(blockStart)), r.d)
|
||||||
|
defer dirRdr.Close()
|
||||||
|
_, err = dirRdr.Read(make([]byte, offset))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
entries, err := directory.ReadDirectory(dirRdr, size)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Directory{
|
||||||
|
FileBase: *r.BaseFromInode(i, name),
|
||||||
|
Entries: entries,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Directory) Open(r *Reader, path string) (*FileBase, error) {
|
||||||
|
path = filepath.Clean(path)
|
||||||
|
if path == "." || path == "" {
|
||||||
|
return &d.FileBase, nil
|
||||||
|
}
|
||||||
|
split := strings.Split(path, "/")
|
||||||
|
i, found := slices.BinarySearchFunc(d.Entries, split[0], func(e directory.Entry, name string) int {
|
||||||
|
return strings.Compare(e.Name, name)
|
||||||
|
})
|
||||||
|
if !found {
|
||||||
|
return nil, fs.ErrNotExist
|
||||||
|
}
|
||||||
|
b, err := r.BaseFromEntry(d.Entries[i])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(split) == 1 {
|
||||||
|
return b, nil
|
||||||
|
} else if !b.IsDir() {
|
||||||
|
return nil, fs.ErrNotExist
|
||||||
|
}
|
||||||
|
dir, err := b.ToDir(r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return dir.Open(r, strings.Join(split[1:], "/"))
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package directory
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
type header struct {
|
||||||
|
Count uint32
|
||||||
|
BlockStart uint32
|
||||||
|
Num uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
type decEntry struct {
|
||||||
|
Offset uint16
|
||||||
|
NumOffset int16
|
||||||
|
InodeType uint16
|
||||||
|
NameSize uint16
|
||||||
|
// Name []byte (not decoded along with decEntry)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Entry struct {
|
||||||
|
Name string
|
||||||
|
BlockStart uint32
|
||||||
|
Offset uint16
|
||||||
|
InodeType uint16
|
||||||
|
Num uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadDirectory(r io.Reader, size uint32) (out []Entry, err error) {
|
||||||
|
size -= 3
|
||||||
|
var curRead uint32
|
||||||
|
var h header
|
||||||
|
var de decEntry
|
||||||
|
for curRead < size {
|
||||||
|
err = binary.Read(r, binary.LittleEndian, &h)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
curRead += 12
|
||||||
|
for i := uint32(0); i < h.Count+1 && curRead < size; i++ {
|
||||||
|
err = binary.Read(r, binary.LittleEndian, &de)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
nameTmp := make([]byte, de.NameSize+1)
|
||||||
|
err = binary.Read(r, binary.LittleEndian, &nameTmp)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
curRead += 8 + uint32(de.NameSize) + 1
|
||||||
|
out = append(out, Entry{
|
||||||
|
BlockStart: h.BlockStart,
|
||||||
|
Offset: de.Offset,
|
||||||
|
Name: string(nameTmp),
|
||||||
|
InodeType: de.InodeType,
|
||||||
|
Num: h.Num + uint32(de.NumOffset),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -0,0 +1,203 @@
|
|||||||
|
package squashfslow
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/CalebQ42/squashfs/internal/metadata"
|
||||||
|
"github.com/CalebQ42/squashfs/internal/toreader"
|
||||||
|
"github.com/CalebQ42/squashfs/low/data"
|
||||||
|
"github.com/CalebQ42/squashfs/low/directory"
|
||||||
|
"github.com/CalebQ42/squashfs/low/inode"
|
||||||
|
)
|
||||||
|
|
||||||
|
type FileBase struct {
|
||||||
|
Inode *inode.Inode
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reader) BaseFromInode(i *inode.Inode, name string) *FileBase {
|
||||||
|
return &FileBase{Inode: i, Name: name}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reader) BaseFromEntry(e directory.Entry) (*FileBase, error) {
|
||||||
|
in, err := r.InodeFromEntry(e)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &FileBase{Inode: in, Name: e.Name}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reader) BaseFromRef(ref uint64, name string) (*FileBase, error) {
|
||||||
|
in, err := r.InodeFromRef(ref)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &FileBase{Inode: in, Name: name}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *FileBase) Uid(r *Reader) (uint32, error) {
|
||||||
|
return r.Id(b.Inode.UidInd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *FileBase) Gid(r *Reader) (uint32, error) {
|
||||||
|
return r.Id(b.Inode.GidInd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *FileBase) IsDir() bool {
|
||||||
|
return b.Inode.Type == inode.Dir || b.Inode.Type == inode.EDir
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *FileBase) ToDir(r *Reader) (*Directory, error) {
|
||||||
|
var blockStart uint32
|
||||||
|
var size uint32
|
||||||
|
var offset uint16
|
||||||
|
switch b.Inode.Type {
|
||||||
|
case inode.Dir:
|
||||||
|
blockStart = b.Inode.Data.(inode.Directory).BlockStart
|
||||||
|
size = uint32(b.Inode.Data.(inode.Directory).Size)
|
||||||
|
offset = b.Inode.Data.(inode.Directory).Offset
|
||||||
|
case inode.EDir:
|
||||||
|
blockStart = b.Inode.Data.(inode.EDirectory).BlockStart
|
||||||
|
size = b.Inode.Data.(inode.EDirectory).Size
|
||||||
|
offset = b.Inode.Data.(inode.EDirectory).Offset
|
||||||
|
default:
|
||||||
|
return nil, errors.New("not a directory")
|
||||||
|
}
|
||||||
|
dirRdr := metadata.NewReader(toreader.NewReader(r.r, int64(r.Superblock.DirTableStart)+int64(blockStart)), r.d)
|
||||||
|
defer dirRdr.Close()
|
||||||
|
_, err := dirRdr.Read(make([]byte, offset))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
entries, err := directory.ReadDirectory(dirRdr, size)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Directory{
|
||||||
|
FileBase: *b,
|
||||||
|
Entries: entries,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *FileBase) IsRegular() bool {
|
||||||
|
return b.Inode.Type == inode.Fil || b.Inode.Type == inode.EFil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *FileBase) GetRegFileReaders(r *Reader) (*data.Reader, *data.FullReader, error) {
|
||||||
|
if !b.IsRegular() {
|
||||||
|
return nil, nil, errors.New("not a regular file")
|
||||||
|
}
|
||||||
|
var blockStart uint64
|
||||||
|
var fragIndex uint32
|
||||||
|
var fragOffset uint32
|
||||||
|
var fragSize uint64
|
||||||
|
var sizes []uint32
|
||||||
|
if b.Inode.Type == inode.Fil {
|
||||||
|
blockStart = uint64(b.Inode.Data.(inode.File).BlockStart)
|
||||||
|
fragIndex = b.Inode.Data.(inode.File).FragInd
|
||||||
|
fragOffset = b.Inode.Data.(inode.File).FragOffset
|
||||||
|
sizes = b.Inode.Data.(inode.File).BlockSizes
|
||||||
|
fragSize = uint64(b.Inode.Data.(inode.File).Size % r.Superblock.BlockSize)
|
||||||
|
} else {
|
||||||
|
blockStart = b.Inode.Data.(inode.EFile).BlockStart
|
||||||
|
fragIndex = b.Inode.Data.(inode.EFile).FragInd
|
||||||
|
fragOffset = b.Inode.Data.(inode.EFile).FragOffset
|
||||||
|
sizes = b.Inode.Data.(inode.EFile).BlockSizes
|
||||||
|
fragSize = b.Inode.Data.(inode.EFile).Size % uint64(r.Superblock.BlockSize)
|
||||||
|
}
|
||||||
|
frag := func() (io.Reader, error) {
|
||||||
|
ent, err := r.fragEntry(fragIndex)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
frag := data.NewReader(toreader.NewReader(r.r, int64(ent.Start)), r.d, []uint32{ent.Size}, uint64(r.Superblock.BlockSize), r.Superblock.BlockSize)
|
||||||
|
frag.Read(make([]byte, fragOffset))
|
||||||
|
return io.LimitReader(frag, int64(fragSize)), nil
|
||||||
|
}
|
||||||
|
outRdr := data.NewReader(toreader.NewReader(r.r, int64(blockStart)), r.d, sizes, fragSize, r.Superblock.BlockSize)
|
||||||
|
if fragIndex != 0xffffffff {
|
||||||
|
f, err := frag()
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
outRdr.AddFrag(f)
|
||||||
|
}
|
||||||
|
outFull := data.NewFullReader(r.r, int64(blockStart), r.d, sizes, fragSize, r.Superblock.BlockSize)
|
||||||
|
if fragIndex != 0xffffffff {
|
||||||
|
outFull.AddFrag(frag)
|
||||||
|
}
|
||||||
|
return outRdr, outFull, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *FileBase) GetFullReader(r *Reader) (*data.FullReader, error) {
|
||||||
|
if !b.IsRegular() {
|
||||||
|
return nil, errors.New("not a regular file")
|
||||||
|
}
|
||||||
|
var blockStart uint64
|
||||||
|
var fragIndex uint32
|
||||||
|
var fragOffset uint32
|
||||||
|
var fragSize uint64
|
||||||
|
var sizes []uint32
|
||||||
|
if b.Inode.Type == inode.Fil {
|
||||||
|
blockStart = uint64(b.Inode.Data.(inode.File).BlockStart)
|
||||||
|
fragIndex = b.Inode.Data.(inode.File).FragInd
|
||||||
|
fragOffset = b.Inode.Data.(inode.File).FragOffset
|
||||||
|
sizes = b.Inode.Data.(inode.File).BlockSizes
|
||||||
|
fragSize = uint64(b.Inode.Data.(inode.File).Size % r.Superblock.BlockSize)
|
||||||
|
} else {
|
||||||
|
blockStart = b.Inode.Data.(inode.EFile).BlockStart
|
||||||
|
fragIndex = b.Inode.Data.(inode.EFile).FragInd
|
||||||
|
fragOffset = b.Inode.Data.(inode.EFile).FragOffset
|
||||||
|
sizes = b.Inode.Data.(inode.EFile).BlockSizes
|
||||||
|
fragSize = b.Inode.Data.(inode.EFile).Size % uint64(r.Superblock.BlockSize)
|
||||||
|
}
|
||||||
|
outFull := data.NewFullReader(r.r, int64(blockStart), r.d, sizes, fragSize, r.Superblock.BlockSize)
|
||||||
|
if fragIndex != 0xffffffff {
|
||||||
|
outFull.AddFrag(func() (io.Reader, error) {
|
||||||
|
ent, err := r.fragEntry(fragIndex)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
frag := data.NewReader(toreader.NewReader(r.r, int64(ent.Start)), r.d, []uint32{ent.Size}, uint64(r.Superblock.BlockSize), r.Superblock.BlockSize)
|
||||||
|
frag.Read(make([]byte, fragOffset))
|
||||||
|
return io.LimitReader(frag, int64(fragSize)), nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return outFull, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *FileBase) GetReader(r *Reader) (*data.Reader, error) {
|
||||||
|
if !b.IsRegular() {
|
||||||
|
return nil, errors.New("not a regular file")
|
||||||
|
}
|
||||||
|
var blockStart uint64
|
||||||
|
var fragIndex uint32
|
||||||
|
var fragOffset uint32
|
||||||
|
var fragSize uint64
|
||||||
|
var sizes []uint32
|
||||||
|
if b.Inode.Type == inode.Fil {
|
||||||
|
blockStart = uint64(b.Inode.Data.(inode.File).BlockStart)
|
||||||
|
fragIndex = b.Inode.Data.(inode.File).FragInd
|
||||||
|
fragOffset = b.Inode.Data.(inode.File).FragOffset
|
||||||
|
sizes = b.Inode.Data.(inode.File).BlockSizes
|
||||||
|
fragSize = uint64(b.Inode.Data.(inode.File).Size % r.Superblock.BlockSize)
|
||||||
|
} else {
|
||||||
|
blockStart = b.Inode.Data.(inode.EFile).BlockStart
|
||||||
|
fragIndex = b.Inode.Data.(inode.EFile).FragInd
|
||||||
|
fragOffset = b.Inode.Data.(inode.EFile).FragOffset
|
||||||
|
sizes = b.Inode.Data.(inode.EFile).BlockSizes
|
||||||
|
fragSize = b.Inode.Data.(inode.EFile).Size % uint64(r.Superblock.BlockSize)
|
||||||
|
}
|
||||||
|
outRdr := data.NewReader(toreader.NewReader(r.r, int64(blockStart)), r.d, sizes, fragSize, r.Superblock.BlockSize)
|
||||||
|
if fragIndex != 0xffffffff {
|
||||||
|
ent, err := r.fragEntry(fragIndex)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
frag := data.NewReader(toreader.NewReader(r.r, int64(ent.Start)), r.d, []uint32{ent.Size}, uint64(r.Superblock.BlockSize), r.Superblock.BlockSize)
|
||||||
|
frag.Read(make([]byte, fragOffset))
|
||||||
|
outRdr.AddFrag(io.LimitReader(frag, int64(fragSize)))
|
||||||
|
}
|
||||||
|
return outRdr, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package squashfslow
|
||||||
|
|
||||||
|
type fragEntry struct {
|
||||||
|
Start uint64
|
||||||
|
Size uint32
|
||||||
|
_ uint32
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package squashfslow
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/CalebQ42/squashfs/internal/metadata"
|
||||||
|
"github.com/CalebQ42/squashfs/internal/toreader"
|
||||||
|
"github.com/CalebQ42/squashfs/low/directory"
|
||||||
|
"github.com/CalebQ42/squashfs/low/inode"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (r *Reader) InodeFromRef(ref uint64) (*inode.Inode, error) {
|
||||||
|
offset, meta := (ref>>16)+r.Superblock.InodeTableStart, ref&0xFFFF
|
||||||
|
rdr := metadata.NewReader(toreader.NewReader(r.r, int64(offset)), r.d)
|
||||||
|
defer rdr.Close()
|
||||||
|
_, err := rdr.Read(make([]byte, meta))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return inode.Read(rdr, r.Superblock.BlockSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reader) InodeFromEntry(e directory.Entry) (*inode.Inode, error) {
|
||||||
|
rdr := metadata.NewReader(toreader.NewReader(r.r, int64(r.Superblock.InodeTableStart)+int64(e.BlockStart)), r.d)
|
||||||
|
defer rdr.Close()
|
||||||
|
rdr.Read(make([]byte, e.Offset))
|
||||||
|
return inode.Read(rdr, r.Superblock.BlockSize)
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
package inode
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Directory struct {
|
||||||
|
BlockStart uint32
|
||||||
|
LinkCount uint32
|
||||||
|
Size uint16
|
||||||
|
Offset uint16
|
||||||
|
ParentNum uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
type eDirectoryInit struct {
|
||||||
|
LinkCount uint32
|
||||||
|
Size uint32
|
||||||
|
BlockStart uint32
|
||||||
|
ParentNum uint32
|
||||||
|
IndCount uint16
|
||||||
|
Offset uint16
|
||||||
|
XattrInd uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
type EDirectory struct {
|
||||||
|
eDirectoryInit
|
||||||
|
Indexes []DirectoryIndex
|
||||||
|
}
|
||||||
|
|
||||||
|
type directoryIndexInit struct {
|
||||||
|
Ind uint32
|
||||||
|
Start uint32
|
||||||
|
NameSize uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
type DirectoryIndex struct {
|
||||||
|
directoryIndexInit
|
||||||
|
Name []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadDir(r io.Reader) (d Directory, err error) {
|
||||||
|
err = binary.Read(r, binary.LittleEndian, &d)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadEDir(r io.Reader) (d EDirectory, err error) {
|
||||||
|
err = binary.Read(r, binary.LittleEndian, &d.eDirectoryInit)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
d.Indexes = make([]DirectoryIndex, d.IndCount)
|
||||||
|
for i := range d.Indexes {
|
||||||
|
err = binary.Read(r, binary.LittleEndian, &d.Indexes[i].directoryIndexInit)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
d.Indexes[i].Name = make([]byte, d.Indexes[i].NameSize+1)
|
||||||
|
err = binary.Read(r, binary.LittleEndian, &d.Indexes[i].Name)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package inode
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"io"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
type fileInit struct {
|
||||||
|
BlockStart uint32
|
||||||
|
FragInd uint32
|
||||||
|
FragOffset uint32
|
||||||
|
Size uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
type File struct {
|
||||||
|
fileInit
|
||||||
|
BlockSizes []uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
type eFileInit struct {
|
||||||
|
BlockStart uint64
|
||||||
|
Size uint64
|
||||||
|
Sparse uint64
|
||||||
|
LinkCount uint32
|
||||||
|
FragInd uint32
|
||||||
|
FragOffset uint32
|
||||||
|
XattrInd uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
type EFile struct {
|
||||||
|
eFileInit
|
||||||
|
BlockSizes []uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadFile(r io.Reader, blockSize uint32) (f File, err error) {
|
||||||
|
err = binary.Read(r, binary.LittleEndian, &f.fileInit)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
toRead := int(math.Floor(float64(f.Size) / float64(blockSize)))
|
||||||
|
if f.FragInd == 0xFFFFFFFF && f.Size%blockSize > 0 {
|
||||||
|
toRead++
|
||||||
|
}
|
||||||
|
f.BlockSizes = make([]uint32, toRead)
|
||||||
|
err = binary.Read(r, binary.LittleEndian, &f.BlockSizes)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadEFile(r io.Reader, blockSize uint32) (f EFile, err error) {
|
||||||
|
err = binary.Read(r, binary.LittleEndian, &f.eFileInit)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
toRead := int(math.Floor(float64(f.Size) / float64(blockSize)))
|
||||||
|
if f.FragInd == 0xFFFFFFFF && f.Size%uint64(blockSize) > 0 {
|
||||||
|
toRead++
|
||||||
|
}
|
||||||
|
f.BlockSizes = make([]uint32, toRead)
|
||||||
|
err = binary.Read(r, binary.LittleEndian, &f.BlockSizes)
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -0,0 +1,144 @@
|
|||||||
|
package inode
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"io/fs"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Dir = uint16(iota + 1)
|
||||||
|
Fil
|
||||||
|
Sym
|
||||||
|
Block
|
||||||
|
Char
|
||||||
|
Fifo
|
||||||
|
Sock
|
||||||
|
EDir
|
||||||
|
EFil
|
||||||
|
ESym
|
||||||
|
EBlock
|
||||||
|
EChar
|
||||||
|
EFifo
|
||||||
|
ESock
|
||||||
|
)
|
||||||
|
|
||||||
|
type Header struct {
|
||||||
|
Type uint16
|
||||||
|
Perm uint16
|
||||||
|
UidInd uint16
|
||||||
|
GidInd uint16
|
||||||
|
ModTime uint32
|
||||||
|
Num uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
type Inode struct {
|
||||||
|
Header
|
||||||
|
Data any
|
||||||
|
}
|
||||||
|
|
||||||
|
func Read(r io.Reader, blockSize uint32) (i *Inode, err error) {
|
||||||
|
i = new(Inode)
|
||||||
|
err = binary.Read(r, binary.LittleEndian, &i.Header)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch i.Type {
|
||||||
|
case Dir:
|
||||||
|
i.Data, err = ReadDir(r)
|
||||||
|
case Fil:
|
||||||
|
i.Data, err = ReadFile(r, blockSize)
|
||||||
|
case Sym:
|
||||||
|
i.Data, err = ReadSym(r)
|
||||||
|
case Block:
|
||||||
|
fallthrough
|
||||||
|
case Char:
|
||||||
|
i.Data, err = ReadDevice(r)
|
||||||
|
case Fifo:
|
||||||
|
fallthrough
|
||||||
|
case Sock:
|
||||||
|
i.Data, err = ReadIPC(r)
|
||||||
|
case EDir:
|
||||||
|
i.Data, err = ReadEDir(r)
|
||||||
|
case EFil:
|
||||||
|
i.Data, err = ReadEFile(r, blockSize)
|
||||||
|
case ESym:
|
||||||
|
i.Data, err = ReadESym(r)
|
||||||
|
case EBlock:
|
||||||
|
fallthrough
|
||||||
|
case EChar:
|
||||||
|
i.Data, err = ReadEDevice(r)
|
||||||
|
case EFifo:
|
||||||
|
fallthrough
|
||||||
|
case ESock:
|
||||||
|
i.Data, err = ReadEIPC(r)
|
||||||
|
default:
|
||||||
|
return i, errors.New("invalid inode type " + strconv.Itoa(int(i.Type)))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i Inode) Mode() (out fs.FileMode) {
|
||||||
|
out = fs.FileMode(i.Perm)
|
||||||
|
switch i.Data.(type) {
|
||||||
|
case Directory:
|
||||||
|
out |= fs.ModeDir
|
||||||
|
case EDirectory:
|
||||||
|
out |= fs.ModeDir
|
||||||
|
case Symlink:
|
||||||
|
out |= fs.ModeSymlink
|
||||||
|
case ESymlink:
|
||||||
|
out |= fs.ModeSymlink
|
||||||
|
case Device:
|
||||||
|
out |= fs.ModeDevice
|
||||||
|
case EDevice:
|
||||||
|
out |= fs.ModeDevice
|
||||||
|
case IPC:
|
||||||
|
out |= fs.ModeNamedPipe
|
||||||
|
case EIPC:
|
||||||
|
out |= fs.ModeNamedPipe
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i Inode) LinkCount() uint32 {
|
||||||
|
switch i.Data.(type) {
|
||||||
|
case EFile:
|
||||||
|
return i.Data.(EFile).LinkCount
|
||||||
|
case Directory:
|
||||||
|
return i.Data.(Directory).LinkCount
|
||||||
|
case EDirectory:
|
||||||
|
return i.Data.(EDirectory).LinkCount
|
||||||
|
case Device:
|
||||||
|
return i.Data.(Device).LinkCount
|
||||||
|
case EDevice:
|
||||||
|
return i.Data.(EDevice).LinkCount
|
||||||
|
case IPC:
|
||||||
|
return i.Data.(IPC).LinkCount
|
||||||
|
case EIPC:
|
||||||
|
return i.Data.(EIPC).LinkCount
|
||||||
|
case Symlink:
|
||||||
|
return i.Data.(Symlink).LinkCount
|
||||||
|
case ESymlink:
|
||||||
|
return i.Data.(ESymlink).LinkCount
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i Inode) Size() uint64 {
|
||||||
|
switch i.Data.(type) {
|
||||||
|
case File:
|
||||||
|
return uint64(i.Data.(File).Size)
|
||||||
|
case EFile:
|
||||||
|
return i.Data.(EFile).Size
|
||||||
|
// case Directory:
|
||||||
|
// return uint64(i.Data.(Directory).Size)
|
||||||
|
// case EDirectory:
|
||||||
|
// return uint64(i.Data.(EDirectory).Size)
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package inode
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Device struct {
|
||||||
|
LinkCount uint32
|
||||||
|
Dev uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
type EDevice struct {
|
||||||
|
Device
|
||||||
|
XattrInd uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadDevice(r io.Reader) (d Device, err error) {
|
||||||
|
err = binary.Read(r, binary.LittleEndian, &d)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadEDevice(r io.Reader) (d EDevice, err error) {
|
||||||
|
err = binary.Read(r, binary.LittleEndian, &d)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
type IPC struct {
|
||||||
|
LinkCount uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
type EIPC struct {
|
||||||
|
IPC
|
||||||
|
XattrInd uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadIPC(r io.Reader) (i IPC, err error) {
|
||||||
|
err = binary.Read(r, binary.LittleEndian, &i)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadEIPC(r io.Reader) (i EIPC, err error) {
|
||||||
|
err = binary.Read(r, binary.LittleEndian, &i)
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package inode
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
type symlinkInit struct {
|
||||||
|
LinkCount uint32
|
||||||
|
TargetSize uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
type Symlink struct {
|
||||||
|
symlinkInit
|
||||||
|
Target []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
type ESymlink struct {
|
||||||
|
symlinkInit
|
||||||
|
Target []byte
|
||||||
|
XattrInd uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadSym(r io.Reader) (s Symlink, err error) {
|
||||||
|
err = binary.Read(r, binary.LittleEndian, &s.symlinkInit)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s.Target = make([]byte, s.TargetSize)
|
||||||
|
err = binary.Read(r, binary.LittleEndian, &s.Target)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadESym(r io.Reader) (s ESymlink, err error) {
|
||||||
|
err = binary.Read(r, binary.LittleEndian, &s.symlinkInit)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s.Target = make([]byte, s.TargetSize)
|
||||||
|
err = binary.Read(r, binary.LittleEndian, &s.Target)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = binary.Read(r, binary.LittleEndian, &s.XattrInd)
|
||||||
|
return
|
||||||
|
}
|
||||||
+219
@@ -0,0 +1,219 @@
|
|||||||
|
package squashfslow
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"math"
|
||||||
|
|
||||||
|
"github.com/CalebQ42/squashfs/internal/decompress"
|
||||||
|
"github.com/CalebQ42/squashfs/internal/metadata"
|
||||||
|
"github.com/CalebQ42/squashfs/internal/toreader"
|
||||||
|
"github.com/CalebQ42/squashfs/low/inode"
|
||||||
|
)
|
||||||
|
|
||||||
|
// The types of compression supported by squashfs
|
||||||
|
const (
|
||||||
|
ZlibCompression = uint16(iota + 1)
|
||||||
|
LZMACompression
|
||||||
|
LZOCompression
|
||||||
|
XZCompression
|
||||||
|
LZ4Compression
|
||||||
|
ZSTDCompression
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrorMagic = errors.New("magic incorrect. probably not reading squashfs archive or archive is corrupted")
|
||||||
|
ErrorLog = errors.New("block log is incorrect. possible corrupted archive")
|
||||||
|
ErrorVersion = errors.New("squashfs version of archive is not 4.0. may be corrupted")
|
||||||
|
ErrorNotExportable = errors.New("archive does not have an export table")
|
||||||
|
)
|
||||||
|
|
||||||
|
type Reader struct {
|
||||||
|
r io.ReaderAt
|
||||||
|
d decompress.Decompressor
|
||||||
|
Root *Directory
|
||||||
|
fragTable []fragEntry
|
||||||
|
idTable []uint32
|
||||||
|
exportTable []uint64
|
||||||
|
Superblock superblock
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewReader(r io.ReaderAt) (rdr *Reader, err error) {
|
||||||
|
rdr = new(Reader)
|
||||||
|
rdr.r = r
|
||||||
|
err = binary.Read(toreader.NewReader(r, 0), binary.LittleEndian, &rdr.Superblock)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Join(errors.New("failed to read superblock"), err)
|
||||||
|
}
|
||||||
|
if !rdr.Superblock.ValidMagic() {
|
||||||
|
return nil, ErrorMagic
|
||||||
|
}
|
||||||
|
if !rdr.Superblock.ValidBlockLog() {
|
||||||
|
return nil, ErrorLog
|
||||||
|
}
|
||||||
|
if !rdr.Superblock.ValidVersion() {
|
||||||
|
return nil, ErrorVersion
|
||||||
|
}
|
||||||
|
switch rdr.Superblock.CompType {
|
||||||
|
case ZlibCompression:
|
||||||
|
rdr.d = decompress.Zlib{}
|
||||||
|
case LZMACompression:
|
||||||
|
rdr.d = decompress.Lzma{}
|
||||||
|
case LZOCompression:
|
||||||
|
rdr.d = decompress.Lzo{}
|
||||||
|
case XZCompression:
|
||||||
|
rdr.d = decompress.Xz{}
|
||||||
|
case LZ4Compression:
|
||||||
|
rdr.d = decompress.Lz4{}
|
||||||
|
case ZSTDCompression:
|
||||||
|
rdr.d = &decompress.Zstd{}
|
||||||
|
default:
|
||||||
|
return nil, errors.New("invalid compression type. possible corrupted archive")
|
||||||
|
}
|
||||||
|
rdr.Root, err = rdr.directoryFromRef(rdr.Superblock.RootInodeRef, "")
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Join(errors.New("failed to read root directory"), err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get a uid/gid at the given index. Lazily populates the reader's Id table as necessary.
|
||||||
|
func (r *Reader) Id(i uint16) (uint32, error) {
|
||||||
|
if len(r.idTable) > int(i) {
|
||||||
|
return r.idTable[i], nil
|
||||||
|
} else if i >= r.Superblock.IdCount {
|
||||||
|
return 0, errors.New("id out of bounds")
|
||||||
|
}
|
||||||
|
// Populate the id table as needed
|
||||||
|
var blockNum uint32
|
||||||
|
if i != 0 { // If i == 0, we go negatives causing issues with uint32s
|
||||||
|
blockNum = uint32(math.Ceil(float64(i)/2048)) - 1
|
||||||
|
} else {
|
||||||
|
blockNum = 0
|
||||||
|
}
|
||||||
|
blocksRead := len(r.idTable) / 2048
|
||||||
|
blocksToRead := int(blockNum) - blocksRead + 1
|
||||||
|
|
||||||
|
var offset uint64
|
||||||
|
var idsToRead uint16
|
||||||
|
var idsTmp []uint32
|
||||||
|
var err error
|
||||||
|
var rdr *metadata.Reader
|
||||||
|
for i := blocksRead; i < int(blocksRead)+blocksToRead; i++ {
|
||||||
|
err = binary.Read(toreader.NewReader(r.r, int64(r.Superblock.IdTableStart)+int64(8*i)), binary.LittleEndian, &offset)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
idsToRead = r.Superblock.IdCount - uint16(len(r.idTable))
|
||||||
|
if idsToRead > 2048 {
|
||||||
|
idsToRead = 2048
|
||||||
|
}
|
||||||
|
idsTmp = make([]uint32, idsToRead)
|
||||||
|
rdr = metadata.NewReader(toreader.NewReader(r.r, int64(offset)), r.d)
|
||||||
|
err = binary.Read(rdr, binary.LittleEndian, &idsTmp)
|
||||||
|
rdr.Close()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
r.idTable = append(r.idTable, idsTmp...)
|
||||||
|
}
|
||||||
|
return r.idTable[i], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get a fragment entry at the given index. Lazily populates the reader's fragment table as necessary.
|
||||||
|
func (r *Reader) fragEntry(i uint32) (fragEntry, error) {
|
||||||
|
if len(r.fragTable) > int(i) {
|
||||||
|
return r.fragTable[i], nil
|
||||||
|
} else if i >= r.Superblock.FragCount {
|
||||||
|
return fragEntry{}, errors.New("fragment out of bounds")
|
||||||
|
}
|
||||||
|
// Populate the fragment table as needed
|
||||||
|
var blockNum uint32
|
||||||
|
if i != 0 { // If i == 0, we go negatives causing issues with uint32s
|
||||||
|
blockNum = uint32(math.Ceil(float64(i)/512)) - 1
|
||||||
|
} else {
|
||||||
|
blockNum = 0
|
||||||
|
}
|
||||||
|
blocksRead := len(r.fragTable) / 512
|
||||||
|
blocksToRead := int(blockNum) - blocksRead + 1
|
||||||
|
|
||||||
|
var offset uint64
|
||||||
|
var fragsToRead uint32
|
||||||
|
var fragsTmp []fragEntry
|
||||||
|
var err error
|
||||||
|
var rdr *metadata.Reader
|
||||||
|
for i := blocksRead; i < int(blocksRead)+blocksToRead; i++ {
|
||||||
|
err = binary.Read(toreader.NewReader(r.r, int64(r.Superblock.FragTableStart)+int64(8*i)), binary.LittleEndian, &offset)
|
||||||
|
if err != nil {
|
||||||
|
return fragEntry{}, err
|
||||||
|
}
|
||||||
|
fragsToRead = r.Superblock.FragCount - uint32(len(r.fragTable))
|
||||||
|
if fragsToRead > 512 {
|
||||||
|
fragsToRead = 512
|
||||||
|
}
|
||||||
|
fragsTmp = make([]fragEntry, fragsToRead)
|
||||||
|
rdr = metadata.NewReader(toreader.NewReader(r.r, int64(offset)), r.d)
|
||||||
|
err = binary.Read(rdr, binary.LittleEndian, &fragsTmp)
|
||||||
|
rdr.Close()
|
||||||
|
if err != nil {
|
||||||
|
return fragEntry{}, err
|
||||||
|
}
|
||||||
|
r.fragTable = append(r.fragTable, fragsTmp...)
|
||||||
|
}
|
||||||
|
return r.fragTable[i], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get an inode reference at the given index. Lazily populates the reader's export table as necessary.
|
||||||
|
func (r *Reader) inodeRef(i uint32) (uint64, error) {
|
||||||
|
if !r.Superblock.Exportable() {
|
||||||
|
return 0, ErrorNotExportable
|
||||||
|
}
|
||||||
|
if len(r.exportTable) > int(i) {
|
||||||
|
return r.exportTable[i], nil
|
||||||
|
} else if i >= r.Superblock.InodeCount {
|
||||||
|
return 0, errors.New("inode out of bounds")
|
||||||
|
}
|
||||||
|
// Populate the export table as needed
|
||||||
|
var blockNum uint32
|
||||||
|
if i != 0 { // If i == 0, we go negatives causing issues with uint32s
|
||||||
|
blockNum = uint32(math.Ceil(float64(i)/1024)) - 1
|
||||||
|
} else {
|
||||||
|
blockNum = 0
|
||||||
|
}
|
||||||
|
blocksRead := len(r.exportTable) / 1024
|
||||||
|
blocksToRead := int(blockNum) - blocksRead + 1
|
||||||
|
|
||||||
|
var offset uint64
|
||||||
|
var refsToRead uint32
|
||||||
|
var refsTmp []uint64
|
||||||
|
var err error
|
||||||
|
var rdr *metadata.Reader
|
||||||
|
for i := blocksRead; i < int(blocksRead)+blocksToRead; i++ {
|
||||||
|
err = binary.Read(toreader.NewReader(r.r, int64(r.Superblock.ExportTableStart)+int64(8*i)), binary.LittleEndian, &offset)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
refsToRead = r.Superblock.InodeCount - uint32(len(r.exportTable))
|
||||||
|
if refsToRead > 1024 {
|
||||||
|
refsToRead = 1024
|
||||||
|
}
|
||||||
|
refsTmp = make([]uint64, refsToRead)
|
||||||
|
rdr = metadata.NewReader(toreader.NewReader(r.r, int64(offset)), r.d)
|
||||||
|
err = binary.Read(rdr, binary.LittleEndian, &refsTmp)
|
||||||
|
rdr.Close()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
r.exportTable = append(r.exportTable, refsTmp...)
|
||||||
|
}
|
||||||
|
return r.exportTable[i], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reader) Inode(i uint32) (*inode.Inode, error) {
|
||||||
|
ref, err := r.inodeRef(i)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r.InodeFromRef(ref)
|
||||||
|
}
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
package squashfslow_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
squashfslow "github.com/CalebQ42/squashfs/low"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
squashfsURL = "https://darkstorm.tech/files/LinuxPATest.sfs"
|
||||||
|
squashfsName = "LinuxPATest.sfs"
|
||||||
|
)
|
||||||
|
|
||||||
|
func preTest(dir string) (fil *os.File, err error) {
|
||||||
|
fil, err = os.Open(filepath.Join(dir, squashfsName))
|
||||||
|
if err != nil {
|
||||||
|
_, err = os.Open(dir)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
err = os.Mkdir(dir, 0755)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
os.Remove(filepath.Join(dir, squashfsName))
|
||||||
|
fil, err = os.Create(filepath.Join(dir, squashfsName))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var resp *http.Response
|
||||||
|
resp, err = http.DefaultClient.Get(squashfsURL)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err = io.Copy(fil, resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_, err = exec.LookPath("unsquashfs")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err = exec.LookPath("mksquashfs")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReader(t *testing.T) {
|
||||||
|
tmpDir := "../testing"
|
||||||
|
fil, err := preTest(tmpDir)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer fil.Close()
|
||||||
|
rdr, err := squashfslow.NewReader(fil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
path := filepath.Join(tmpDir, "extractTest")
|
||||||
|
os.RemoveAll(path)
|
||||||
|
os.MkdirAll(path, 0777)
|
||||||
|
err = extractToDir(rdr, &rdr.Root.FileBase, path)
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var singleFile = "PortableApps/CPU-X/CPU-X-v4.2.0-x86_64.AppImage"
|
||||||
|
|
||||||
|
func TestSingleFile(t *testing.T) {
|
||||||
|
tmpDir := "../testing"
|
||||||
|
fil, err := preTest(tmpDir)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer fil.Close()
|
||||||
|
rdr, err := squashfslow.NewReader(fil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
path := filepath.Join(tmpDir, "extractTest")
|
||||||
|
os.RemoveAll(path)
|
||||||
|
os.MkdirAll(path, 0777)
|
||||||
|
b, err := rdr.Root.Open(rdr, singleFile)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
err = extractToDir(rdr, b, path)
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func extractToDir(rdr *squashfslow.Reader, b *squashfslow.FileBase, folder string) error {
|
||||||
|
path := filepath.Join(folder, b.Name)
|
||||||
|
if b.IsDir() {
|
||||||
|
d, err := b.ToDir(rdr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = os.MkdirAll(path, 0777)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var nestBast *squashfslow.FileBase
|
||||||
|
for _, e := range d.Entries {
|
||||||
|
nestBast, err = rdr.BaseFromEntry(e)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = extractToDir(rdr, nestBast, path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if b.IsRegular() {
|
||||||
|
_, full, err := b.GetRegFileReaders(rdr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fil, err := os.Create(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = full.WriteTo(fil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Println("Successfully extracted file:", b.Name)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
package squashfslow
|
||||||
|
|
||||||
|
import "math"
|
||||||
|
|
||||||
|
type superblock struct {
|
||||||
|
Magic uint32
|
||||||
|
InodeCount uint32
|
||||||
|
ModTime uint32
|
||||||
|
BlockSize uint32
|
||||||
|
FragCount uint32
|
||||||
|
CompType uint16
|
||||||
|
BlockLog uint16
|
||||||
|
Flags uint16
|
||||||
|
IdCount uint16
|
||||||
|
VerMaj uint16
|
||||||
|
VerMin uint16
|
||||||
|
RootInodeRef uint64
|
||||||
|
Size uint64
|
||||||
|
IdTableStart uint64
|
||||||
|
XattrTableStart uint64
|
||||||
|
InodeTableStart uint64
|
||||||
|
DirTableStart uint64
|
||||||
|
FragTableStart uint64
|
||||||
|
ExportTableStart uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s superblock) ValidMagic() bool {
|
||||||
|
return s.Magic == 0x73717368
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s superblock) ValidBlockLog() bool {
|
||||||
|
return s.BlockLog == uint16(math.Log2(float64(s.BlockSize)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s superblock) ValidVersion() bool {
|
||||||
|
return s.VerMaj == 4 && s.VerMin == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s superblock) UncompressedInodes() bool {
|
||||||
|
return s.Flags&0x1 == 0x1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s superblock) UncompressedData() bool {
|
||||||
|
return s.Flags&0x2 == 0x2
|
||||||
|
}
|
||||||
|
func (s superblock) UncompressedFragments() bool {
|
||||||
|
return s.Flags&0x8 == 0x8
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s superblock) NoFragments() bool {
|
||||||
|
return s.Flags&0x10 == 0x10
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s superblock) AlwaysFragment() bool {
|
||||||
|
return s.Flags&0x20 == 0x20
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s superblock) Duplicates() bool {
|
||||||
|
return s.Flags&0x40 == 0x40
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s superblock) Exportable() bool {
|
||||||
|
return s.Flags&0x80 == 0x80
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s superblock) UncompressedXattrs() bool {
|
||||||
|
return s.Flags&0x100 == 0x100
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s superblock) NoXattrs() bool {
|
||||||
|
return s.Flags&0x200 == 0x200
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s superblock) CompressionOptions() bool {
|
||||||
|
return s.Flags&0x400 == 0x400
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s superblock) UncompressedIDs() bool {
|
||||||
|
return s.Flags&0x800 == 0x800
|
||||||
|
}
|
||||||
-188
@@ -1,188 +0,0 @@
|
|||||||
package squashfs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/binary"
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
type metadata struct {
|
|
||||||
raw uint16
|
|
||||||
size uint16
|
|
||||||
compressed bool
|
|
||||||
}
|
|
||||||
|
|
||||||
//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 {
|
|
||||||
s *Reader
|
|
||||||
headers []*metadata
|
|
||||||
data []byte
|
|
||||||
offset int64
|
|
||||||
readOffset int
|
|
||||||
}
|
|
||||||
|
|
||||||
//NewMetadataReader creates a new MetadataReader, beginning to read at the given offset. It will automatically cache the first block at the location.
|
|
||||||
func (s *Reader) newMetadataReader(offset int64) (*metadataReader, error) {
|
|
||||||
var br metadataReader
|
|
||||||
br.s = s
|
|
||||||
br.offset = offset
|
|
||||||
err := br.parseMetadata()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
err = br.readNextDataBlock()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &br, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//NewMetadataReaderFromInodeRef creates a new MetadataReader with the offsets set by the given inode reference.
|
|
||||||
func (s *Reader) newMetadataReaderFromInodeRef(ref uint64) (*metadataReader, error) {
|
|
||||||
offset, metaOffset := processInodeRef(ref)
|
|
||||||
br, err := s.newMetadataReader(int64(s.super.InodeTableStart + offset))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
_, err = br.Seek(int64(metaOffset), io.SeekStart)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return br, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//ProcessInodeRef processes an inode reference and returns two values
|
|
||||||
//
|
|
||||||
//The first value is the inode table offset. AKA, it's where the metadata block of the inode STARTS relative to the inode table.
|
|
||||||
//
|
|
||||||
//The second value is the offset of the inode, INSIDE of the metadata.
|
|
||||||
func processInodeRef(inodeRef uint64) (tableOffset uint64, metaOffset uint64) {
|
|
||||||
tableOffset = inodeRef >> 16
|
|
||||||
metaOffset = inodeRef &^ 0xFFFFFFFF0000
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (br *metadataReader) parseMetadata() error {
|
|
||||||
var raw uint16
|
|
||||||
err := binary.Read(io.NewSectionReader(br.s.r, br.offset, 2), binary.LittleEndian, &raw)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
br.offset += 2
|
|
||||||
compressed := raw&0x8000 != 0x8000
|
|
||||||
size := raw &^ 0x8000
|
|
||||||
br.headers = append(br.headers, &metadata{
|
|
||||||
raw: raw,
|
|
||||||
size: size,
|
|
||||||
compressed: compressed,
|
|
||||||
})
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (br *metadataReader) readNextDataBlock() error {
|
|
||||||
meta := br.headers[len(br.headers)-1]
|
|
||||||
r := io.NewSectionReader(br.s.r, br.offset, int64(meta.size))
|
|
||||||
if meta.compressed {
|
|
||||||
byts, err := br.s.decompressor.Decompress(r)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
br.offset += int64(meta.size)
|
|
||||||
br.data = append(br.data, byts...)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
var buf bytes.Buffer
|
|
||||||
_, err := io.Copy(&buf, r)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
br.offset += int64(meta.size)
|
|
||||||
br.data = append(br.data, buf.Bytes()...)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//Read reads bytes into the given byte slice. Returns the amount of data read.
|
|
||||||
func (br *metadataReader) Read(p []byte) (int, error) {
|
|
||||||
if br.readOffset+len(p) <= len(br.data) {
|
|
||||||
for i := 0; i < len(p); i++ {
|
|
||||||
p[i] = br.data[br.readOffset+i]
|
|
||||||
}
|
|
||||||
br.readOffset += len(p)
|
|
||||||
return len(p), nil
|
|
||||||
}
|
|
||||||
read := 0
|
|
||||||
for read < len(p) {
|
|
||||||
err := br.parseMetadata()
|
|
||||||
if err != nil {
|
|
||||||
br.readOffset += read
|
|
||||||
return read, err
|
|
||||||
}
|
|
||||||
err = br.readNextDataBlock()
|
|
||||||
if err != nil {
|
|
||||||
br.readOffset += read
|
|
||||||
return read, err
|
|
||||||
}
|
|
||||||
for ; read < len(p); read++ {
|
|
||||||
if br.readOffset+read < len(br.data) {
|
|
||||||
p[read] = br.data[br.readOffset+read]
|
|
||||||
} else {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
br.readOffset += read
|
|
||||||
if read != len(p) {
|
|
||||||
return read, errors.New("Didn't read enough data")
|
|
||||||
}
|
|
||||||
return read, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//Seek will seek to the specified location (if possible). Seeking is relative to the uncompressed data.
|
|
||||||
//When io.SeekCurrent or io.SeekStart is set, if seeking would put the offset beyond the current cached data, it will try to read the next data blocks to accomodate. On a failure it will seek to the end of the data.
|
|
||||||
//When io.SeekEnd is set, it wil seek reletive to the currently cached data.
|
|
||||||
func (br *metadataReader) Seek(offset int64, whence int) (int64, error) {
|
|
||||||
switch whence {
|
|
||||||
case io.SeekCurrent:
|
|
||||||
br.readOffset += int(offset)
|
|
||||||
for {
|
|
||||||
if br.readOffset < len(br.data) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
err := br.parseMetadata()
|
|
||||||
if err != nil {
|
|
||||||
br.readOffset = len(br.data)
|
|
||||||
return int64(br.readOffset), err
|
|
||||||
}
|
|
||||||
err = br.readNextDataBlock()
|
|
||||||
if err != nil {
|
|
||||||
br.readOffset = len(br.data)
|
|
||||||
return int64(br.readOffset), err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case io.SeekStart:
|
|
||||||
br.readOffset = int(offset)
|
|
||||||
for {
|
|
||||||
if br.readOffset < len(br.data) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
err := br.parseMetadata()
|
|
||||||
if err != nil {
|
|
||||||
br.readOffset = len(br.data)
|
|
||||||
return int64(br.readOffset), err
|
|
||||||
}
|
|
||||||
err = br.readNextDataBlock()
|
|
||||||
if err != nil {
|
|
||||||
br.readOffset = len(br.data)
|
|
||||||
return int64(br.readOffset), err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case io.SeekEnd:
|
|
||||||
br.readOffset = len(br.data) - int(offset)
|
|
||||||
if br.readOffset < 0 {
|
|
||||||
br.readOffset = 0
|
|
||||||
return int64(br.readOffset), errors.New("Trying to seek to a negative value")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return int64(br.readOffset), nil
|
|
||||||
}
|
|
||||||
@@ -1,179 +1,32 @@
|
|||||||
package squashfs
|
package squashfs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"errors"
|
|
||||||
"io"
|
"io"
|
||||||
"math"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/CalebQ42/squashfs/internal/compression"
|
squashfslow "github.com/CalebQ42/squashfs/low"
|
||||||
"github.com/CalebQ42/squashfs/internal/inode"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
magic uint32 = 0x73717368
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
//ErrNoMagic is returned if the magic number in the superblock isn't correct.
|
|
||||||
errNoMagic = errors.New("Magic number doesn't match. Either isn't a squashfs or corrupted")
|
|
||||||
//ErrIncompatibleCompression is returned if the compression type in the superblock doesn't work.
|
|
||||||
errIncompatibleCompression = errors.New("Compression type unsupported")
|
|
||||||
//ErrCompressorOptions is returned if compressor options is present. It's 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")
|
|
||||||
)
|
|
||||||
|
|
||||||
//TODO: implement fs.FS, possibly more FS types for compatibility. Most of this work will mostly be handed off to root anyway so this shouldn't be too difficult.
|
|
||||||
|
|
||||||
//Reader processes and reads a squashfs archive.
|
|
||||||
type Reader struct {
|
type Reader struct {
|
||||||
FS
|
*FS
|
||||||
r *io.SectionReader
|
Low *squashfslow.Reader
|
||||||
decompressor compression.Decompressor
|
|
||||||
root *File
|
|
||||||
fragOffsets []uint64
|
|
||||||
idTable []uint32
|
|
||||||
super superblock
|
|
||||||
flags SuperblockFlags
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//NewSquashfsReader returns a new squashfs.Reader from an io.ReaderAt
|
func NewReader(r io.ReaderAt) (*Reader, error) {
|
||||||
func NewSquashfsReader(r io.ReaderAt) (*Reader, error) {
|
rdr, err := squashfslow.NewReader(r)
|
||||||
var rdr Reader
|
|
||||||
err := binary.Read(io.NewSectionReader(r, 0, int64(binary.Size(rdr.super))), binary.LittleEndian, &rdr.super)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
rdr.r = io.NewSectionReader(r, 0, int64(rdr.super.BytesUsed))
|
out := &Reader{
|
||||||
if rdr.super.Magic != magic {
|
Low: rdr,
|
||||||
return nil, errNoMagic
|
|
||||||
}
|
}
|
||||||
if rdr.super.BlockLog != uint16(math.Log2(float64(rdr.super.BlockSize))) {
|
out.FS = &FS{
|
||||||
return nil, errors.New("BlockSize and BlockLog doesn't match. The archive is probably corrupt")
|
d: rdr.Root,
|
||||||
|
r: out,
|
||||||
}
|
}
|
||||||
rdr.r.Seek(96, io.SeekStart)
|
return out, nil
|
||||||
hasUnsupportedOptions := false
|
|
||||||
rdr.flags = rdr.super.GetFlags()
|
|
||||||
if rdr.flags.compressorOptions {
|
|
||||||
switch rdr.super.CompressionType {
|
|
||||||
case GzipCompression:
|
|
||||||
var gzip *compression.Gzip
|
|
||||||
gzip, err = compression.NewGzipCompressorWithOptions(rdr.r)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if gzip.HasCustomWindow || gzip.HasStrategies {
|
|
||||||
hasUnsupportedOptions = true
|
|
||||||
}
|
|
||||||
rdr.decompressor = gzip
|
|
||||||
case XzCompression:
|
|
||||||
var xz *compression.Xz
|
|
||||||
xz, err = compression.NewXzCompressorWithOptions(rdr.r)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if xz.HasFilters {
|
|
||||||
return nil, errors.New("XZ compression options has filters. These are not yet supported")
|
|
||||||
}
|
|
||||||
rdr.decompressor = xz
|
|
||||||
case Lz4Compression:
|
|
||||||
var lz4 *compression.Lz4
|
|
||||||
lz4, err = compression.NewLz4CompressorWithOptions(rdr.r)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
rdr.decompressor = lz4
|
|
||||||
case ZstdCompression:
|
|
||||||
var zstd *compression.Zstd
|
|
||||||
zstd, err = compression.NewZstdCompressorWithOptions(rdr.r)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
rdr.decompressor = zstd
|
|
||||||
default:
|
|
||||||
return nil, errIncompatibleCompression
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
switch rdr.super.CompressionType {
|
|
||||||
case GzipCompression:
|
|
||||||
rdr.decompressor = &compression.Gzip{}
|
|
||||||
case LzmaCompression:
|
|
||||||
rdr.decompressor = &compression.Lzma{}
|
|
||||||
case XzCompression:
|
|
||||||
rdr.decompressor = &compression.Xz{}
|
|
||||||
case Lz4Compression:
|
|
||||||
rdr.decompressor = &compression.Lz4{}
|
|
||||||
case ZstdCompression:
|
|
||||||
rdr.decompressor = &compression.Zstd{}
|
|
||||||
default:
|
|
||||||
//TODO: all compression types.
|
|
||||||
return nil, errIncompatibleCompression
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fragBlocks := int(math.Ceil(float64(rdr.super.FragCount) / 512))
|
|
||||||
if fragBlocks > 0 {
|
|
||||||
offset := int64(rdr.super.FragTableStart)
|
|
||||||
for i := 0; i < fragBlocks; i++ {
|
|
||||||
tmp := make([]byte, 8)
|
|
||||||
_, err = r.ReadAt(tmp, offset)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
rdr.fragOffsets = append(rdr.fragOffsets, binary.LittleEndian.Uint64(tmp))
|
|
||||||
offset += 8
|
|
||||||
}
|
|
||||||
}
|
|
||||||
unread := rdr.super.IDCount
|
|
||||||
blockOffsets := make([]uint64, int(math.Ceil(float64(rdr.super.IDCount)/2048)))
|
|
||||||
rdr.r.Seek(int64(rdr.super.IDTableStart), io.SeekStart)
|
|
||||||
for i := range blockOffsets {
|
|
||||||
err = binary.Read(rdr.r, binary.LittleEndian, &blockOffsets[i])
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var idRdr *metadataReader
|
|
||||||
idRdr, err = rdr.newMetadataReader(int64(blockOffsets[i]))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
read := uint16(math.Min(float64(unread), 2048))
|
|
||||||
for i := uint16(0); i < read; i++ {
|
|
||||||
var tmp uint32
|
|
||||||
err = binary.Read(idRdr, binary.LittleEndian, &tmp)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
rdr.idTable = append(rdr.idTable, tmp)
|
|
||||||
}
|
|
||||||
unread -= read
|
|
||||||
}
|
|
||||||
metaRdr, err := rdr.newMetadataReaderFromInodeRef(rdr.super.RootInodeRef)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
i, err := inode.ProcessInode(metaRdr, rdr.super.BlockSize)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
entries, err := rdr.readDirFromInode(i)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
rdr.FS = FS{
|
|
||||||
r: &rdr,
|
|
||||||
name: "/",
|
|
||||||
entries: entries,
|
|
||||||
}
|
|
||||||
if hasUnsupportedOptions {
|
|
||||||
return &rdr, ErrOptions
|
|
||||||
}
|
|
||||||
return &rdr, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//ModTime is the last time the file was modified/created.
|
|
||||||
func (r *Reader) ModTime() time.Time {
|
func (r *Reader) ModTime() time.Time {
|
||||||
return time.Unix(int64(r.super.CreationTime), 0)
|
return time.Unix(int64(r.Low.Superblock.ModTime), 0)
|
||||||
}
|
}
|
||||||
|
|||||||
-223
@@ -1,223 +0,0 @@
|
|||||||
package squashfs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"strconv"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
goappimage "github.com/CalebQ42/GoAppImage"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
downloadURL = "https://github.com/srevinsaju/Firefox-Appimage/releases/download/firefox-v84.0.r20201221152838/firefox-84.0.r20201221152838-x86_64.AppImage"
|
|
||||||
appImageName = "firefox-84.0.r20201221152838-x86_64.AppImage"
|
|
||||||
squashfsName = "balenaEtcher-1.5.113-x64.AppImage.sfs"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestSquashfs(t *testing.T) {
|
|
||||||
wd, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
squashFil, err := os.Open(wd + "/testing/" + squashfsName)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
rdr, err := NewSquashfsReader(squashFil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
fmt.Println("stuff", rdr.super.CompressionType)
|
|
||||||
// fil := rdr.GetFileAtPath("*.desktop")
|
|
||||||
// if fil == nil {
|
|
||||||
// t.Fatal("Can't find desktop fil")
|
|
||||||
// }
|
|
||||||
// errs := fil.ExtractTo(wd + "/testing")
|
|
||||||
// if len(errs) > 0 {
|
|
||||||
// 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) {
|
|
||||||
t.Parallel()
|
|
||||||
wd, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
aiFil, err := os.Open(wd + "/testing/" + appImageName)
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
err = downloadTestAppImage(wd + "/testing")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
aiFil, err = os.Open(wd + "/testing/" + appImageName)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
} else if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer aiFil.Close()
|
|
||||||
stat, _ := aiFil.Stat()
|
|
||||||
ai := goappimage.NewAppImage(wd + "/testing/" + appImageName)
|
|
||||||
rdr, err := NewSquashfsReader(io.NewSectionReader(aiFil, ai.Offset, stat.Size()-ai.Offset))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
os.RemoveAll(wd + "/testing/firefox")
|
|
||||||
err = rdr.ExtractTo(wd + "/testing/firefox")
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestUnsquashfs(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
wd, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
aiFil, err := os.Open(wd + "/testing/" + appImageName)
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
err = downloadTestAppImage(wd + "/testing")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
aiFil, err = os.Open(wd + "/testing/" + appImageName)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
} else if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
os.RemoveAll(wd + "/testing/unsquashFirefox")
|
|
||||||
os.RemoveAll(wd + "/testing/firefox")
|
|
||||||
ai := goappimage.NewAppImage(wd + "/testing/" + appImageName)
|
|
||||||
fmt.Println("Command:", "unsquashfs", "-d", wd+"/testing/unsquashFirefox", "-o", strconv.Itoa(int(ai.Offset)), aiFil.Name())
|
|
||||||
cmd := exec.Command("unsquashfs", "-d", wd+"/testing/unsquashFirefox", "-o", strconv.Itoa(int(ai.Offset)), aiFil.Name())
|
|
||||||
start := time.Now()
|
|
||||||
err = cmd.Run()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
fmt.Println(time.Since(start))
|
|
||||||
t.Fatal("HI")
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkDragRace(b *testing.B) {
|
|
||||||
wd, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
b.Fatal(err)
|
|
||||||
}
|
|
||||||
aiFil, err := os.Open(wd + "/testing/" + appImageName)
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
err = downloadTestAppImage(wd + "/testing")
|
|
||||||
if err != nil {
|
|
||||||
b.Fatal(err)
|
|
||||||
}
|
|
||||||
aiFil, err = os.Open(wd + "/testing/" + appImageName)
|
|
||||||
if err != nil {
|
|
||||||
b.Fatal(err)
|
|
||||||
}
|
|
||||||
} else if err != nil {
|
|
||||||
b.Fatal(err)
|
|
||||||
}
|
|
||||||
stat, _ := aiFil.Stat()
|
|
||||||
ai := goappimage.NewAppImage(wd + "/testing/" + appImageName)
|
|
||||||
os.RemoveAll(wd + "/testing/unsquashFirefox")
|
|
||||||
os.RemoveAll(wd + "/testing/firefox")
|
|
||||||
cmd := exec.Command("unsquashfs", "-d", wd+"/testing/unsquashFirefox", "-o", strconv.Itoa(int(ai.Offset)), aiFil.Name())
|
|
||||||
start := time.Now()
|
|
||||||
err = cmd.Run()
|
|
||||||
if err != nil {
|
|
||||||
b.Fatal(err)
|
|
||||||
}
|
|
||||||
unsquashTime := time.Since(start)
|
|
||||||
start = time.Now()
|
|
||||||
rdr, err := NewSquashfsReader(io.NewSectionReader(aiFil, ai.Offset, stat.Size()-ai.Offset))
|
|
||||||
if err != nil {
|
|
||||||
b.Fatal(err)
|
|
||||||
}
|
|
||||||
err = rdr.ExtractTo(wd + "/testing/firefox")
|
|
||||||
if err != nil {
|
|
||||||
b.Fatal(err)
|
|
||||||
}
|
|
||||||
libTime := time.Since(start)
|
|
||||||
b.Log("Unsqushfs:", unsquashTime.Round(time.Millisecond))
|
|
||||||
b.Log("Library:", libTime.Round(time.Millisecond))
|
|
||||||
b.Log("unsquashfs is", strconv.FormatFloat(float64(libTime.Milliseconds())/float64(unsquashTime.Milliseconds()), 'f', 2, 64)+"x faster")
|
|
||||||
}
|
|
||||||
|
|
||||||
func downloadTestAppImage(dir string) error {
|
|
||||||
//seems to time out on slow connections. Might fix that at some point... or not. It's just a test...
|
|
||||||
os.Mkdir(dir, os.ModePerm)
|
|
||||||
appImage, err := os.Create(dir + "/" + appImageName)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer appImage.Close()
|
|
||||||
check := http.Client{
|
|
||||||
CheckRedirect: func(r *http.Request, _ []*http.Request) error {
|
|
||||||
r.URL.Opaque = r.URL.Path
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
}
|
|
||||||
resp, err := check.Get(downloadURL)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
_, err = io.Copy(appImage, resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCreateSquashFromAppImage(t *testing.T) {
|
|
||||||
wd, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
err = os.Mkdir(wd+"/testing", 0777)
|
|
||||||
if err != nil && !os.IsExist(err) {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
_, err = os.Open(wd + "/testing/" + appImageName)
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
err = downloadTestAppImage(wd + "/testing")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
_, err = os.Open(wd + "/testing/" + appImageName)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
} else if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
ai := goappimage.NewAppImage(wd + "/testing/" + appImageName)
|
|
||||||
aiFil, err := os.Open(wd + "/testing/" + appImageName)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer aiFil.Close()
|
|
||||||
aiFil.Seek(ai.Offset, 0)
|
|
||||||
os.Remove(wd + "/testing/" + appImageName + ".squashfs")
|
|
||||||
aiSquash, err := os.Create(wd + "/testing/" + appImageName + ".squashfs")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
_, err = io.Copy(aiSquash, aiFil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,193 @@
|
|||||||
|
package squashfs_test
|
||||||
|
|
||||||
|
//Actually proper tests go here.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"io/fs"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/CalebQ42/squashfs"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
squashfsURL = "https://darkstorm.tech/files/LinuxPATest.sfs"
|
||||||
|
squashfsName = "airootfs.sfs"
|
||||||
|
)
|
||||||
|
|
||||||
|
func preTest(dir string) (fil *os.File, err error) {
|
||||||
|
fil, err = os.Open(filepath.Join(dir, squashfsName))
|
||||||
|
if err != nil {
|
||||||
|
_, err = os.Open(dir)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
err = os.Mkdir(dir, 0755)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
os.Remove(filepath.Join(dir, squashfsName))
|
||||||
|
fil, err = os.Create(filepath.Join(dir, squashfsName))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var resp *http.Response
|
||||||
|
resp, err = http.DefaultClient.Get(squashfsURL)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err = io.Copy(fil, resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_, err = exec.LookPath("unsquashfs")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err = exec.LookPath("mksquashfs")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMisc(t *testing.T) {
|
||||||
|
tmpDir := "testing"
|
||||||
|
fil, err := preTest(tmpDir)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
rdr, err := squashfs.NewReader(fil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
_ = rdr
|
||||||
|
// Put testing here
|
||||||
|
t.Fatal("UM")
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkRace(b *testing.B) {
|
||||||
|
tmpDir := "testing"
|
||||||
|
fil, err := preTest(tmpDir)
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
libPath := filepath.Join(tmpDir, "ExtractLib")
|
||||||
|
unsquashPath := filepath.Join(tmpDir, "ExtractSquashfs")
|
||||||
|
os.RemoveAll(libPath)
|
||||||
|
os.RemoveAll(unsquashPath)
|
||||||
|
var libTime, unsquashTime time.Duration
|
||||||
|
op := squashfs.FastOptions()
|
||||||
|
start := time.Now()
|
||||||
|
rdr, err := squashfs.NewReader(fil)
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
err = rdr.ExtractWithOptions(libPath, op)
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
libTime = time.Since(start)
|
||||||
|
cmd := exec.Command("unsquashfs", "-d", unsquashPath, fil.Name())
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
start = time.Now()
|
||||||
|
err = cmd.Run()
|
||||||
|
if err != nil {
|
||||||
|
b.Log("Unsquashfs error:", err)
|
||||||
|
}
|
||||||
|
unsquashTime = time.Since(start)
|
||||||
|
b.Log("Library took:", libTime.Round(time.Millisecond))
|
||||||
|
b.Log("unsquashfs took:", unsquashTime.Round(time.Millisecond))
|
||||||
|
b.Log("unsquashfs is", strconv.FormatFloat(float64(libTime.Milliseconds())/float64(unsquashTime.Milliseconds()), 'f', 2, 64), "times faster")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExtractQuick(t *testing.T) {
|
||||||
|
//First, setup everything and extract the archive using the library and unsquashfs
|
||||||
|
|
||||||
|
// tmpDir := b.TempDir()
|
||||||
|
tmpDir := "testing"
|
||||||
|
fil, err := preTest(tmpDir)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
libPath := filepath.Join(tmpDir, "ExtractLib")
|
||||||
|
unsquashPath := filepath.Join(tmpDir, "ExtractSquashfs")
|
||||||
|
os.RemoveAll(libPath)
|
||||||
|
os.RemoveAll(unsquashPath)
|
||||||
|
rdr, err := squashfs.NewReader(fil)
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
cmd := exec.Command("unsquashfs", "-d", unsquashPath, fil.Name())
|
||||||
|
err = cmd.Run()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
//Then compare the sizes and existance between the two (using unsquashfs as a reference).
|
||||||
|
//If the file doesn't exist, or the size is different, we exit.
|
||||||
|
//TODO: Add long test that checks contents.
|
||||||
|
|
||||||
|
squashFils := os.DirFS(unsquashPath)
|
||||||
|
err = fs.WalkDir(squashFils, ".", func(path string, _ fs.DirEntry, _ error) error {
|
||||||
|
libFil, e := os.Open(filepath.Join(libPath, path))
|
||||||
|
if e != nil {
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
sfsFile, e := os.Open(filepath.Join(unsquashPath, path))
|
||||||
|
if e != nil {
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
sfsStat, _ := sfsFile.Stat()
|
||||||
|
libStat, _ := libFil.Stat()
|
||||||
|
if sfsStat.Size() != libStat.Size() {
|
||||||
|
t.Log(libFil.Name(), "not the same size between library and unsquashfs")
|
||||||
|
t.Log("File is", libStat.Size())
|
||||||
|
t.Log("Should be", sfsStat.Size())
|
||||||
|
return errors.New("file not the correct size")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var filePath = "bin"
|
||||||
|
|
||||||
|
func TestSingleFile(t *testing.T) {
|
||||||
|
tmpDir := "testing"
|
||||||
|
fil, err := preTest(tmpDir)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
os.Remove(filepath.Join(tmpDir, filePath))
|
||||||
|
rdr, err := squashfs.NewReader(fil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
f, err := rdr.Open(filePath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
err = f.(*squashfs.File).ExtractWithOptions("testing", &squashfs.ExtractionOptions{Verbose: true})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Fatal("HI")
|
||||||
|
}
|
||||||
-126
@@ -1,126 +0,0 @@
|
|||||||
package squashfs
|
|
||||||
|
|
||||||
//The types of compression supported by squashfs.
|
|
||||||
const (
|
|
||||||
GzipCompression = 1 + iota
|
|
||||||
LzmaCompression
|
|
||||||
LzoCompression
|
|
||||||
XzCompression
|
|
||||||
Lz4Compression
|
|
||||||
ZstdCompression
|
|
||||||
)
|
|
||||||
|
|
||||||
//Superblock contains important information about a squashfs file. Located at the very front of the archive.
|
|
||||||
type superblock struct {
|
|
||||||
Magic uint32
|
|
||||||
InodeCount uint32
|
|
||||||
CreationTime uint32
|
|
||||||
BlockSize uint32
|
|
||||||
FragCount uint32
|
|
||||||
CompressionType uint16
|
|
||||||
BlockLog uint16
|
|
||||||
Flags uint16
|
|
||||||
IDCount uint16
|
|
||||||
MajorVersion uint16
|
|
||||||
MinorVersion uint16
|
|
||||||
RootInodeRef uint64
|
|
||||||
BytesUsed uint64
|
|
||||||
IDTableStart uint64
|
|
||||||
XattrTableStart uint64
|
|
||||||
InodeTableStart uint64
|
|
||||||
DirTableStart uint64
|
|
||||||
FragTableStart uint64
|
|
||||||
ExportTableStart uint64
|
|
||||||
}
|
|
||||||
|
|
||||||
//SuperblockFlags is the series of flags describing how a squashfs archive is packed.
|
|
||||||
type SuperblockFlags struct {
|
|
||||||
//If true, inodes are stored uncompressed.
|
|
||||||
UncompressedInodes bool
|
|
||||||
//If true, data is stored uncompressed.
|
|
||||||
UncompressedData bool
|
|
||||||
check bool
|
|
||||||
//If true, fragments are stored uncompressed.
|
|
||||||
UncompressedFragments bool
|
|
||||||
//If true, ALL data is stored in sequential data blocks instead of utilizing fragments.
|
|
||||||
NoFragments bool
|
|
||||||
//If true, the last block of data will always be stored as a fragment if it's less then the block size.
|
|
||||||
AlwaysFragment bool
|
|
||||||
//If true, duplicate files are only stored once. (Currently unsupported)
|
|
||||||
RemoveDuplicates bool
|
|
||||||
//If true, the export table is populated. (Currently unsupported)
|
|
||||||
Exportable bool
|
|
||||||
//If true, the xattr table is uncompressed. (Currently unsupported)
|
|
||||||
UncompressedXattr bool
|
|
||||||
//If true, the xattr table is not populated. (Currently unsupported)
|
|
||||||
NoXattr bool
|
|
||||||
compressorOptions bool
|
|
||||||
//If true, the UID/GID table is stored uncompressed.
|
|
||||||
UncompressedIDs bool
|
|
||||||
}
|
|
||||||
|
|
||||||
//DefaultFlags are the default SuperblockFlags that are used.
|
|
||||||
var DefaultFlags = SuperblockFlags{
|
|
||||||
RemoveDuplicates: true,
|
|
||||||
Exportable: true,
|
|
||||||
}
|
|
||||||
|
|
||||||
//GetFlags returns a SuperblockFlags for a given superblock.
|
|
||||||
func (s *superblock) GetFlags() SuperblockFlags {
|
|
||||||
return SuperblockFlags{
|
|
||||||
UncompressedInodes: s.Flags&0x1 == 0x1,
|
|
||||||
UncompressedData: s.Flags&0x2 == 0x2,
|
|
||||||
check: s.Flags&0x4 == 0x4,
|
|
||||||
UncompressedFragments: s.Flags&0x8 == 0x8,
|
|
||||||
NoFragments: s.Flags&0x10 == 0x10,
|
|
||||||
AlwaysFragment: s.Flags&0x20 == 0x20,
|
|
||||||
RemoveDuplicates: s.Flags&0x40 == 0x40,
|
|
||||||
Exportable: s.Flags&0x80 == 0x80,
|
|
||||||
UncompressedXattr: s.Flags&0x100 == 0x100,
|
|
||||||
NoXattr: s.Flags&0x200 == 0x200,
|
|
||||||
compressorOptions: s.Flags&0x400 == 0x400,
|
|
||||||
UncompressedIDs: s.Flags&0x800 == 0x800,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//ToUint returns the uint16 representation of the given SuperblockFlags
|
|
||||||
func (s *SuperblockFlags) ToUint() uint16 {
|
|
||||||
var out uint16
|
|
||||||
if s.UncompressedInodes {
|
|
||||||
out = out | 0x1
|
|
||||||
}
|
|
||||||
if s.UncompressedData {
|
|
||||||
out = out | 0x2
|
|
||||||
}
|
|
||||||
if s.check {
|
|
||||||
out = out | 0x4
|
|
||||||
}
|
|
||||||
if s.UncompressedFragments {
|
|
||||||
out = out | 0x8
|
|
||||||
}
|
|
||||||
if s.NoFragments {
|
|
||||||
out = out | 0x10
|
|
||||||
}
|
|
||||||
if s.AlwaysFragment {
|
|
||||||
out = out | 0x20
|
|
||||||
}
|
|
||||||
if s.RemoveDuplicates {
|
|
||||||
out = out | 0x40
|
|
||||||
}
|
|
||||||
if s.Exportable {
|
|
||||||
out = out | 0x80
|
|
||||||
}
|
|
||||||
if s.UncompressedXattr {
|
|
||||||
out = out | 0x100
|
|
||||||
}
|
|
||||||
if s.NoXattr {
|
|
||||||
out = out | 0x200
|
|
||||||
}
|
|
||||||
if s.compressorOptions {
|
|
||||||
out = out | 0x400
|
|
||||||
}
|
|
||||||
if s.UncompressedIDs {
|
|
||||||
out = out | 0x800
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
@@ -1,390 +0,0 @@
|
|||||||
package squashfs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
"io/fs"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
"syscall"
|
|
||||||
|
|
||||||
"github.com/CalebQ42/squashfs/internal/compression"
|
|
||||||
)
|
|
||||||
|
|
||||||
//Writer is used to creaste squashfs archives. Currently unusable.
|
|
||||||
//TODO: Make usable
|
|
||||||
type Writer struct {
|
|
||||||
compressor compression.Compressor
|
|
||||||
structure map[string][]*fileHolder
|
|
||||||
symlinkTable map[string]string
|
|
||||||
folders []string
|
|
||||||
uidGUIDTable []int
|
|
||||||
frags []fragment
|
|
||||||
superblock superblock
|
|
||||||
compressionType int
|
|
||||||
//BlockSize is how large the data blocks are. Can be between 4096 (4KB) and 1048576 (1 MB).
|
|
||||||
//If BlockSize is not inside that range, it will be set to within the range before writing.
|
|
||||||
//Default is 1048576.
|
|
||||||
BlockSize uint32
|
|
||||||
//Flags are the SuperblockFlags used when writing the archive.
|
|
||||||
//Currently Duplicates, Exportable, UncompressedXattr, NoXattr values are ignored
|
|
||||||
Flags SuperblockFlags
|
|
||||||
allowErrors bool
|
|
||||||
}
|
|
||||||
|
|
||||||
//NewWriter creates a new with the default options (Gzip compression and allow errors)
|
|
||||||
func NewWriter() (*Writer, error) {
|
|
||||||
return NewWriterWithOptions(GzipCompression, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
//NewWriterWithOptions creates a new squashfs.Writer with the given options.
|
|
||||||
//compressionType can be of any types, except LZO (which this library doesn't have support for yet)
|
|
||||||
//allowErrors determines if, when adding folders, it allows errors encountered with it's sub-directories and instead logs the errors.
|
|
||||||
func NewWriterWithOptions(compressionType int, allowErrors bool) (*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")
|
|
||||||
}
|
|
||||||
writer := &Writer{
|
|
||||||
structure: map[string][]*fileHolder{
|
|
||||||
"/": make([]*fileHolder, 0),
|
|
||||||
},
|
|
||||||
folders: []string{
|
|
||||||
"/",
|
|
||||||
},
|
|
||||||
symlinkTable: make(map[string]string),
|
|
||||||
compressionType: compressionType,
|
|
||||||
allowErrors: allowErrors,
|
|
||||||
BlockSize: uint32(1048576),
|
|
||||||
Flags: DefaultFlags,
|
|
||||||
}
|
|
||||||
switch compressionType {
|
|
||||||
case 1:
|
|
||||||
writer.compressor = &compression.Gzip{}
|
|
||||||
case 2:
|
|
||||||
writer.compressor = &compression.Lzma{}
|
|
||||||
case 4:
|
|
||||||
writer.compressor = &compression.Xz{}
|
|
||||||
case 5:
|
|
||||||
writer.compressor = &compression.Lz4{}
|
|
||||||
case 6:
|
|
||||||
writer.compressor = &compression.Zstd{}
|
|
||||||
}
|
|
||||||
return writer, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//fileHolder holds the necessary information about a given file inside of a squashfs
|
|
||||||
type fileHolder struct {
|
|
||||||
reader io.Reader
|
|
||||||
path string
|
|
||||||
name string
|
|
||||||
symLocation string
|
|
||||||
blockSizes []uint32
|
|
||||||
GUID int
|
|
||||||
perm int
|
|
||||||
size uint64
|
|
||||||
UID int
|
|
||||||
folder bool
|
|
||||||
symlink bool
|
|
||||||
|
|
||||||
fragIndex int
|
|
||||||
fragOffset int
|
|
||||||
}
|
|
||||||
|
|
||||||
//AddFile attempts to add an os.File to the archive's root directory.
|
|
||||||
func (w *Writer) AddFile(file *os.File) error {
|
|
||||||
return w.AddFileToFolder("/", file)
|
|
||||||
}
|
|
||||||
|
|
||||||
//AddFileToFolder adds the given file to the squashfs archive, placing it inside the given folder.
|
|
||||||
func (w *Writer) AddFileToFolder(folder string, file *os.File) error {
|
|
||||||
name := path.Base(file.Name())
|
|
||||||
if !strings.HasSuffix(folder, "/") {
|
|
||||||
folder += "/"
|
|
||||||
}
|
|
||||||
return w.AddFileTo(folder+name, file)
|
|
||||||
}
|
|
||||||
|
|
||||||
//AddFileTo adds the given file to the squashfs archive at the given filepath.
|
|
||||||
func (w *Writer) AddFileTo(filepath string, file *os.File) error {
|
|
||||||
filepath = path.Clean(filepath)
|
|
||||||
if !strings.HasPrefix(filepath, "/") {
|
|
||||||
filepath = "/" + filepath
|
|
||||||
}
|
|
||||||
if w.Contains(filepath) {
|
|
||||||
return errors.New("File already exists at " + filepath)
|
|
||||||
}
|
|
||||||
var holder fileHolder
|
|
||||||
holder.path = path.Dir(filepath)
|
|
||||||
holder.name = path.Base(filepath)
|
|
||||||
holder.reader = file
|
|
||||||
stat, err := file.Stat()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
holder.folder = stat.IsDir()
|
|
||||||
holder.symlink = (stat.Mode()&os.ModeSymlink == os.ModeSymlink)
|
|
||||||
holder.perm = int(stat.Mode().Perm())
|
|
||||||
//Thanks to https://stackoverflow.com/questions/58179647/getting-uid-and-gid-of-a-file for uid and guid getting
|
|
||||||
if stat, ok := stat.Sys().(*syscall.Stat_t); ok {
|
|
||||||
holder.UID = int(stat.Uid)
|
|
||||||
holder.GUID = int(stat.Gid)
|
|
||||||
}
|
|
||||||
if sort.SearchInts(w.uidGUIDTable, holder.UID) == len(w.uidGUIDTable) {
|
|
||||||
w.uidGUIDTable = append(w.uidGUIDTable, holder.UID)
|
|
||||||
sort.Ints(w.uidGUIDTable)
|
|
||||||
}
|
|
||||||
if sort.SearchInts(w.uidGUIDTable, holder.GUID) == len(w.uidGUIDTable) {
|
|
||||||
w.uidGUIDTable = append(w.uidGUIDTable, holder.GUID)
|
|
||||||
sort.Ints(w.uidGUIDTable)
|
|
||||||
}
|
|
||||||
if holder.symlink {
|
|
||||||
holder.reader = file
|
|
||||||
target, err := os.Readlink(file.Name())
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
holder.symLocation = target
|
|
||||||
} else if holder.folder {
|
|
||||||
subDirNames, err := file.Readdirnames(-1)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
dirsAdded := make([]string, 0)
|
|
||||||
for _, subDir := range subDirNames {
|
|
||||||
fil, err := os.Open(file.Name() + subDir)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = w.AddFileToFolder(holder.path+"/"+holder.name, fil)
|
|
||||||
if err != nil && !w.allowErrors {
|
|
||||||
for _, dir := range dirsAdded {
|
|
||||||
w.Remove(dir)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
} else if err != nil {
|
|
||||||
log.Println("Error while adding", fil.Name())
|
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
if !w.allowErrors {
|
|
||||||
dirsAdded = append(dirsAdded, holder.path+"/"+holder.name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if stat.Mode().IsRegular() {
|
|
||||||
holder.reader = file
|
|
||||||
} else {
|
|
||||||
return errors.New("Unsupported file type " + file.Name())
|
|
||||||
}
|
|
||||||
if _, ok := w.structure[holder.path]; ok {
|
|
||||||
w.folders = append(w.folders, holder.path)
|
|
||||||
sort.Strings(w.folders)
|
|
||||||
}
|
|
||||||
w.structure[holder.path] = append(w.structure[holder.path], &holder)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//AddReaderTo adds the data from the given reader to the archive as a file located at the given filepath.
|
|
||||||
//Data from the reader is not read until the squashfs archive is writen.
|
|
||||||
//If the given reader implements io.Closer, it will be closed after it is fully read.
|
|
||||||
func (w *Writer) AddReaderTo(filepath string, reader io.Reader, size uint64) error {
|
|
||||||
filepath = path.Clean(filepath)
|
|
||||||
if !strings.HasPrefix(filepath, "/") {
|
|
||||||
filepath = "/" + filepath
|
|
||||||
}
|
|
||||||
if w.Contains(filepath) {
|
|
||||||
return errors.New("File already exists at " + filepath)
|
|
||||||
}
|
|
||||||
var holder fileHolder
|
|
||||||
holder.path = path.Dir(filepath)
|
|
||||||
holder.name = path.Base(filepath)
|
|
||||||
holder.size = size
|
|
||||||
holder.reader = reader
|
|
||||||
if _, ok := w.structure[holder.path]; ok {
|
|
||||||
w.folders = append(w.folders, holder.path)
|
|
||||||
sort.Strings(w.folders)
|
|
||||||
}
|
|
||||||
w.structure[holder.path] = append(w.structure[holder.path], &holder)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//AddFolderTo adds a folder at the given path. IF the folder is already present, it sets the folder's permissions.
|
|
||||||
//If the path points to a non-folder (such as a file or symlink), an error is returned
|
|
||||||
func (w *Writer) AddFolderTo(folderpath string, permission fs.FileMode) error {
|
|
||||||
folderpath = path.Clean(folderpath)
|
|
||||||
tmp := w.holderAt(folderpath)
|
|
||||||
if tmp != nil {
|
|
||||||
if !tmp.folder {
|
|
||||||
return errors.New("Path is not a folder: " + folderpath)
|
|
||||||
}
|
|
||||||
tmp.perm = int(permission.Perm())
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
file := fileHolder{
|
|
||||||
path: path.Dir(folderpath),
|
|
||||||
name: path.Base(folderpath),
|
|
||||||
perm: int(permission | fs.ModePerm),
|
|
||||||
folder: true,
|
|
||||||
}
|
|
||||||
w.structure[file.path] = append(w.structure[file.path], &file)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//Remove tries to remove the file(s) at the given filepath. If wildcards are used, it will remove all files that match.
|
|
||||||
//Returns true if one or more files are removed.
|
|
||||||
func (w *Writer) Remove(filepath string) bool {
|
|
||||||
var matchFound bool
|
|
||||||
filepath = path.Clean(filepath)
|
|
||||||
if !strings.HasPrefix(filepath, "/") {
|
|
||||||
filepath = "/" + filepath
|
|
||||||
}
|
|
||||||
dir, name := path.Split(filepath)
|
|
||||||
for structDir, files := range w.structure {
|
|
||||||
if match, _ := path.Match(dir, structDir); match {
|
|
||||||
for i, fil := range files {
|
|
||||||
if match, _ = path.Match(name, fil.name); match {
|
|
||||||
matchFound = true
|
|
||||||
if len(w.structure[structDir]) > 1 {
|
|
||||||
w.structure[structDir][i] = w.structure[structDir][len(w.structure[structDir])-1]
|
|
||||||
w.structure[structDir] = w.structure[structDir][:len(w.structure[structDir])-1]
|
|
||||||
} else {
|
|
||||||
w.structure[structDir] = nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return matchFound
|
|
||||||
}
|
|
||||||
|
|
||||||
//FixSymlinks will scan through the squashfs archive and try to find broken symlinks and fix them.
|
|
||||||
//This done by replacing the symlink with the target file and then pointing other symlinks to that file.
|
|
||||||
//If all symlinks can be resolved, the error slice will be nil, and the bool false, otherwise all errors occured will be in the slice.
|
|
||||||
func (w *Writer) FixSymlinks() (errs []error, problems bool) {
|
|
||||||
for dir, holderSlice := range w.structure {
|
|
||||||
for i, holder := range holderSlice {
|
|
||||||
if !holder.symlink {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
sym := holder.symLocation
|
|
||||||
if !path.IsAbs(holder.symLocation) {
|
|
||||||
sym = path.Join(dir, holder.symLocation)
|
|
||||||
}
|
|
||||||
if path, ok := w.symlinkTable[sym]; ok {
|
|
||||||
w.structure[dir][i].symLocation = path
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if path.IsAbs(sym) || strings.HasPrefix(sym, "../") {
|
|
||||||
var symFil *os.File
|
|
||||||
var err error
|
|
||||||
if strings.HasPrefix(sym, "../") {
|
|
||||||
holderFil, ok := holder.reader.(*os.File)
|
|
||||||
if !ok {
|
|
||||||
problems = true
|
|
||||||
errs = append(errs, errors.New("Cannot resolve symlink at "+dir+holder.name))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
symFilPath := path.Dir(holderFil.Name())
|
|
||||||
symFilPath = path.Join(symFilPath, holder.symLocation)
|
|
||||||
symFil, err = os.Open(symFilPath)
|
|
||||||
} else {
|
|
||||||
symFil, err = os.Open(sym)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
problems = true
|
|
||||||
errs = append(errs, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
suc := w.Remove(dir + holder.name)
|
|
||||||
if !suc {
|
|
||||||
problems = true
|
|
||||||
errs = append(errs, errors.New("Cannot resolve symlink at "+dir+holder.name))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
err = w.AddFileTo(dir+holder.name, symFil)
|
|
||||||
if err != nil {
|
|
||||||
w.structure[dir] = append(w.structure[dir], holder)
|
|
||||||
problems = true
|
|
||||||
errs = append(errs, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
w.symlinkTable[sym] = dir + holder.name
|
|
||||||
} else {
|
|
||||||
symHolder := w.holderAt(sym)
|
|
||||||
if symHolder != nil {
|
|
||||||
w.symlinkTable[sym] = sym
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
holderFil, ok := holder.reader.(*os.File)
|
|
||||||
if !ok {
|
|
||||||
problems = true
|
|
||||||
errs = append(errs, errors.New("Cannot resolve symlink at "+dir+holder.name))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
symFilPath := path.Dir(holderFil.Name())
|
|
||||||
symFilPath = path.Join(symFilPath, holder.symLocation)
|
|
||||||
symFil, err := os.Open(symFilPath)
|
|
||||||
if err != nil {
|
|
||||||
problems = true
|
|
||||||
errs = append(errs, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
err = w.AddFileTo(sym, symFil)
|
|
||||||
if err != nil {
|
|
||||||
problems = true
|
|
||||||
errs = append(errs, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
w.symlinkTable[sym] = sym
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *Writer) holderAt(filepath string) *fileHolder {
|
|
||||||
filepath = path.Clean(filepath)
|
|
||||||
if !strings.HasPrefix(filepath, "/") {
|
|
||||||
filepath = "/" + filepath
|
|
||||||
}
|
|
||||||
dir, name := path.Split(filepath)
|
|
||||||
if holderSlice, ok := w.structure[dir]; ok {
|
|
||||||
for _, holder := range holderSlice {
|
|
||||||
if holder.name == name {
|
|
||||||
return holder
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//Contains returns whether a file is present at the given filepath
|
|
||||||
func (w *Writer) Contains(filepath string) bool {
|
|
||||||
filepath = path.Clean(filepath)
|
|
||||||
if !strings.HasPrefix(filepath, "/") {
|
|
||||||
filepath = "/" + filepath
|
|
||||||
}
|
|
||||||
dir, name := path.Split(filepath)
|
|
||||||
if holderSlice, ok := w.structure[dir]; ok {
|
|
||||||
for _, holder := range holderSlice {
|
|
||||||
if holder.name == name {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
//WriteToFilename creates the squashfs archive with the given filepath.
|
|
||||||
func (w *Writer) WriteToFilename(filepath string) error {
|
|
||||||
newFil, err := os.Create(filepath)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err = w.WriteTo(newFil)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
package squashfs
|
|
||||||
|
|
||||||
import "reflect"
|
|
||||||
|
|
||||||
func (w *Writer) compressData(data []byte) ([]byte, error) {
|
|
||||||
if reflect.DeepEqual(data, make([]byte, len(data))) {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
compressedData, err := w.compressor.Compress(data)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if len(data) <= len(compressedData) {
|
|
||||||
return data, nil
|
|
||||||
}
|
|
||||||
return compressedData, nil
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
package squashfs
|
|
||||||
|
|
||||||
//TODO: Allow settings the options
|
|
||||||
|
|
||||||
// func (w *Writer) SetGzipOptions() error {}
|
|
||||||
|
|
||||||
// func (w *Writer) SetLzmaOptions() error {}
|
|
||||||
|
|
||||||
// func (w *Writer) SetLzoOptions() error {}
|
|
||||||
|
|
||||||
// func (w *Writer) SetXzOptions() error {}
|
|
||||||
|
|
||||||
// func (w *Writer) SetLz4Options() error {}
|
|
||||||
|
|
||||||
// func (w *Writer) SetZstdOptions() error {}
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
package squashfs
|
|
||||||
|
|
||||||
type fragment struct {
|
|
||||||
w *Writer
|
|
||||||
files []*fileHolder
|
|
||||||
sizes []uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fragment) SizeLeft() uint32 {
|
|
||||||
totalSize := uint32(0)
|
|
||||||
for _, siz := range f.sizes {
|
|
||||||
totalSize += siz
|
|
||||||
}
|
|
||||||
return f.w.BlockSize - uint32(totalSize)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fragment) AddFragment(fil *fileHolder) {
|
|
||||||
//SizeLeft should already be checked
|
|
||||||
fil.fragOffset = len(f.files)
|
|
||||||
f.files = append(f.files, fil)
|
|
||||||
f.sizes = append(f.sizes, fil.blockSizes[len(fil.blockSizes)-1])
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *Writer) addToFragments(fil *fileHolder) {
|
|
||||||
fragSize := fil.blockSizes[len(fil.blockSizes)-1]
|
|
||||||
//only fragment if the final block is less then 80% of a full block or AlwaysFragment
|
|
||||||
if w.Flags.AlwaysFragment || fragSize < uint32(float32(w.BlockSize)*0.8) {
|
|
||||||
var possibleFrags []int
|
|
||||||
for i := range w.frags {
|
|
||||||
left := w.frags[i].SizeLeft()
|
|
||||||
if left == fragSize {
|
|
||||||
fil.fragIndex = i
|
|
||||||
w.frags[i].AddFragment(fil)
|
|
||||||
return
|
|
||||||
} else if left > fragSize {
|
|
||||||
possibleFrags = append(possibleFrags, i)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(possibleFrags) > 0 {
|
|
||||||
fil.fragIndex = possibleFrags[0]
|
|
||||||
} else {
|
|
||||||
fil.fragIndex = len(w.frags)
|
|
||||||
w.frags = append(w.frags, fragment{
|
|
||||||
w: w,
|
|
||||||
files: []*fileHolder{fil},
|
|
||||||
sizes: []uint32{fragSize},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *Writer) calculateFragsAndBlockSizes() {
|
|
||||||
for _, files := range w.structure {
|
|
||||||
for i := range files {
|
|
||||||
files[i].fragIndex = -1
|
|
||||||
files[i].blockSizes = make([]uint32, files[i].size/uint64(w.BlockSize))
|
|
||||||
for j := range files[i].blockSizes {
|
|
||||||
files[i].blockSizes[j] = w.BlockSize
|
|
||||||
}
|
|
||||||
fragSize := uint32(files[i].size % uint64(w.BlockSize))
|
|
||||||
if fragSize > 0 {
|
|
||||||
files[i].blockSizes = append(files[i].blockSizes, fragSize)
|
|
||||||
if !w.Flags.NoFragments {
|
|
||||||
w.addToFragments(files[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
package squashfs
|
|
||||||
|
|
||||||
func (w *Writer) countInodes() (out uint32) {
|
|
||||||
for _, files := range w.structure {
|
|
||||||
out++
|
|
||||||
out += uint32(len(files))
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
package squashfs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
"io/fs"
|
|
||||||
"math"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (w *Writer) fixFolders() error {
|
|
||||||
for folder := range w.structure {
|
|
||||||
if folder == "/" || w.Contains(folder) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
err := w.AddFolderTo(folder, fs.ModePerm)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//WriteTo attempts to write the archive to the given io.Writer.
|
|
||||||
//Folder that aren't present (such as if you add a file at /folder/file, but not the folder /folder)
|
|
||||||
//are added with full permission (777).
|
|
||||||
//
|
|
||||||
//Not working. Yet.
|
|
||||||
func (w *Writer) WriteTo(write io.Writer) (int64, error) {
|
|
||||||
err := w.fixFolders()
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
if w.BlockSize > 1048576 {
|
|
||||||
w.BlockSize = 1048576
|
|
||||||
} else if w.BlockSize < 4096 {
|
|
||||||
w.BlockSize = 4096
|
|
||||||
}
|
|
||||||
w.Flags.RemoveDuplicates = false
|
|
||||||
w.Flags.Exportable = false
|
|
||||||
w.Flags.NoXattr = true
|
|
||||||
w.calculateFragsAndBlockSizes()
|
|
||||||
w.superblock = superblock{
|
|
||||||
Magic: magic,
|
|
||||||
InodeCount: w.countInodes(),
|
|
||||||
CreationTime: uint32(time.Now().Unix()),
|
|
||||||
BlockSize: w.BlockSize,
|
|
||||||
CompressionType: uint16(w.compressionType),
|
|
||||||
BlockLog: uint16(math.Log2(float64(w.BlockSize))),
|
|
||||||
Flags: w.Flags.ToUint(),
|
|
||||||
IDCount: uint16(len(w.uidGUIDTable)),
|
|
||||||
MajorVersion: 4,
|
|
||||||
MinorVersion: 0,
|
|
||||||
}
|
|
||||||
return 0, errors.New("I SAID DON'T")
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user