Rename squashfs/squashfs to squashfs/low
squashfs/low library name is now squashfslow
This commit is contained in:
@@ -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.
|
||||
+168
@@ -0,0 +1,168 @@
|
||||
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 Base struct {
|
||||
Inode *inode.Inode
|
||||
Name string
|
||||
}
|
||||
|
||||
func (r *Reader) BaseFromInode(i *inode.Inode, name string) *Base {
|
||||
return &Base{Inode: i, Name: name}
|
||||
}
|
||||
|
||||
func (r *Reader) BaseFromEntry(e directory.Entry) (*Base, error) {
|
||||
in, err := r.InodeFromEntry(e)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Base{Inode: in, Name: e.Name}, nil
|
||||
}
|
||||
|
||||
func (r *Reader) BaseFromRef(ref uint64, name string) (*Base, error) {
|
||||
in, err := r.InodeFromRef(ref)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Base{Inode: in, Name: name}, nil
|
||||
}
|
||||
|
||||
func (b *Base) Uid(r *Reader) (uint32, error) {
|
||||
return r.Id(b.Inode.UidInd)
|
||||
}
|
||||
|
||||
func (b *Base) Gid(r *Reader) (uint32, error) {
|
||||
return r.Id(b.Inode.GidInd)
|
||||
}
|
||||
|
||||
func (b *Base) IsDir() bool {
|
||||
return b.Inode.Type == inode.Dir || b.Inode.Type == inode.EDir
|
||||
}
|
||||
|
||||
func (b *Base) 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{
|
||||
Base: *b,
|
||||
Entries: entries,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (b *Base) IsRegular() bool {
|
||||
return b.Inode.Type == inode.Fil || b.Inode.Type == inode.EFil
|
||||
}
|
||||
|
||||
func (b *Base) 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 *Base) 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
|
||||
}
|
||||
@@ -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 {
|
||||
Base
|
||||
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{
|
||||
Base: *r.BaseFromInode(i, name),
|
||||
Entries: entries,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (d *Directory) Open(r *Reader, path string) (*Base, error) {
|
||||
path = filepath.Clean(path)
|
||||
if path == "." || path == "" {
|
||||
return &d.Base, 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,60 @@
|
||||
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
|
||||
}
|
||||
|
||||
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,
|
||||
})
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -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.Base, 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.Base, 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.Base
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user