Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f242de2710 | |||
| 88315ee384 | |||
| 1e2a8f4b75 | |||
| 863b03fb19 | |||
| d3f84344d1 | |||
| ad24995b7b | |||
| 638355ab71 | |||
| 04d914d403 | |||
| 7323fe56f6 | |||
| 6286da31e1 | |||
| 77c87a9653 | |||
| e6b0b83dcb | |||
| cef9090210 | |||
| 24a9457c6b | |||
| e0c1309ed4 | |||
| 8b475b6cc4 |
@@ -11,6 +11,10 @@ Currently has support for reading squashfs files and extracting files and folder
|
||||
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).
|
||||
|
||||
## Build tags
|
||||
|
||||
As of `v1.1.0` this library has two optional build tags: `no_gpl` and `no_obsolete`. `no_gpl` disables the ability to read archives with lzo compression due to the library's gpl license. `no_obsolete` removes "obsolete" compression types for a reduced compilation size; currently this only disable lzma compression since it's superseded by xz.
|
||||
|
||||
## FUSE
|
||||
|
||||
As of `v1.0`, FUSE capabilities has been moved to [a separate library](https://github.com/CalebQ42/squashfuse).
|
||||
|
||||
@@ -3,14 +3,62 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/CalebQ42/squashfs"
|
||||
)
|
||||
|
||||
func userName(uid int, numeric bool) string {
|
||||
us := strconv.Itoa(uid)
|
||||
if numeric {
|
||||
return us
|
||||
}
|
||||
if u, err := user.LookupId(us); err == nil {
|
||||
return u.Username
|
||||
}
|
||||
return us
|
||||
}
|
||||
|
||||
func groupName(gid int, numeric bool) string {
|
||||
gs := strconv.Itoa(gid)
|
||||
if numeric {
|
||||
return gs
|
||||
}
|
||||
if g, err := user.LookupGroupId(gs); err == nil {
|
||||
return g.Name
|
||||
}
|
||||
return gs
|
||||
}
|
||||
|
||||
func printEntry(root, path string, d fs.DirEntry, numeric bool) {
|
||||
fi, _ := d.Info()
|
||||
sfi := fi.(squashfs.FileInfo)
|
||||
owner := fmt.Sprintf("%s/%s",
|
||||
userName(sfi.Uid(), numeric),
|
||||
groupName(sfi.Gid(), numeric))
|
||||
link := ""
|
||||
if sfi.IsSymlink() {
|
||||
link = " -> " + sfi.SymlinkPath()
|
||||
}
|
||||
fmt.Printf("%s %s %*d %s %s%s\n",
|
||||
strings.ToLower(fi.Mode().String()),
|
||||
owner, 26-len(owner), fi.Size(),
|
||||
fi.ModTime().Format("2006-01-02 15:04"),
|
||||
filepath.Join(root, path), link)
|
||||
}
|
||||
|
||||
func main() {
|
||||
verbose := flag.Bool("v", false, "Verbose")
|
||||
list := flag.Bool("l", false, "List")
|
||||
long := flag.Bool("ll", false, "List with attributes")
|
||||
numeric := flag.Bool("lln", false, "List with attributes and numeric ids")
|
||||
offset := flag.Int64("o", 0, "Offset")
|
||||
ignore := flag.Bool("ip", false, "Ignore Permissions and extract all files/folders with 0755")
|
||||
flag.Parse()
|
||||
if len(flag.Args()) < 2 {
|
||||
@@ -21,10 +69,25 @@ func main() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
r, err := squashfs.NewReader(f)
|
||||
r, err := squashfs.NewReaderAtOffset(f, *offset)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if *list || *long || *numeric {
|
||||
root := flag.Arg(1)
|
||||
fs.WalkDir(r, ".", func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if *long || *numeric {
|
||||
printEntry(root, path, d, *numeric)
|
||||
} else {
|
||||
fmt.Println(filepath.Join(root, path))
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return
|
||||
}
|
||||
op := squashfs.DefaultOptions()
|
||||
op.Verbose = *verbose
|
||||
op.IgnorePerm = *ignore
|
||||
|
||||
@@ -127,7 +127,7 @@ func (f *File) ReadDir(n int) ([]fs.DirEntry, error) {
|
||||
}
|
||||
}
|
||||
var out []fs.DirEntry
|
||||
var fi fileInfo
|
||||
var fi FileInfo
|
||||
for _, e := range d.Entries[start:end] {
|
||||
fi, err = f.r.newFileInfo(e)
|
||||
if err != nil {
|
||||
@@ -142,7 +142,15 @@ func (f *File) ReadDir(n int) ([]fs.DirEntry, error) {
|
||||
|
||||
// Returns the file's fs.FileInfo
|
||||
func (f *File) Stat() (fs.FileInfo, error) {
|
||||
return newFileInfo(f.b.Name, &f.b.Inode), nil
|
||||
uid, err := f.b.Uid(&f.r.Low)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
gid, err := f.b.Gid(&f.r.Low)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newFileInfo(f.b.Name, uid, gid, &f.b.Inode), nil
|
||||
}
|
||||
|
||||
// SymlinkPath returns the symlink's target path. Is the File isn't a symlink, returns an empty string.
|
||||
|
||||
+48
-17
@@ -8,48 +8,79 @@ import (
|
||||
"github.com/CalebQ42/squashfs/low/inode"
|
||||
)
|
||||
|
||||
type fileInfo struct {
|
||||
type FileInfo struct {
|
||||
name string
|
||||
uid uint32
|
||||
gid uint32
|
||||
size int64
|
||||
target string
|
||||
perm uint32
|
||||
modTime uint32
|
||||
fileType uint16
|
||||
}
|
||||
|
||||
func (r Reader) newFileInfo(e directory.Entry) (fileInfo, error) {
|
||||
i, err := r.Low.InodeFromEntry(e)
|
||||
func (r Reader) newFileInfo(e directory.Entry) (FileInfo, error) {
|
||||
b, err := r.Low.BaseFromEntry(e)
|
||||
if err != nil {
|
||||
return fileInfo{}, err
|
||||
return FileInfo{}, err
|
||||
}
|
||||
return newFileInfo(e.Name, &i), nil
|
||||
uid, err := b.Uid(&r.Low)
|
||||
if err != nil {
|
||||
return FileInfo{}, err
|
||||
}
|
||||
gid, err := b.Gid(&r.Low)
|
||||
if err != nil {
|
||||
return FileInfo{}, err
|
||||
}
|
||||
return newFileInfo(e.Name, uid, gid, &b.Inode), nil
|
||||
}
|
||||
|
||||
func newFileInfo(name string, i *inode.Inode) fileInfo {
|
||||
func newFileInfo(name string, uid, gid uint32, i *inode.Inode) FileInfo {
|
||||
var size int64
|
||||
var target string
|
||||
switch i.Type {
|
||||
case inode.Fil:
|
||||
size = int64(i.Data.(inode.File).Size)
|
||||
case inode.EFil:
|
||||
size = int64(i.Data.(inode.EFile).Size)
|
||||
case inode.Sym:
|
||||
target = string(i.Data.(inode.Symlink).Target)
|
||||
case inode.ESym:
|
||||
target = string(i.Data.(inode.ESymlink).Target)
|
||||
}
|
||||
return fileInfo{
|
||||
return FileInfo{
|
||||
name: name,
|
||||
uid: uid,
|
||||
gid: gid,
|
||||
size: size,
|
||||
target: target,
|
||||
perm: uint32(i.Perm),
|
||||
modTime: i.ModTime,
|
||||
fileType: i.Type,
|
||||
}
|
||||
}
|
||||
|
||||
func (f fileInfo) Name() string {
|
||||
func (f FileInfo) Name() string {
|
||||
return f.name
|
||||
}
|
||||
|
||||
func (f fileInfo) Size() int64 {
|
||||
func (f FileInfo) Uid() int {
|
||||
return int(f.uid)
|
||||
}
|
||||
|
||||
func (f FileInfo) Gid() int {
|
||||
return int(f.gid)
|
||||
}
|
||||
|
||||
func (f FileInfo) Size() int64 {
|
||||
return f.size
|
||||
}
|
||||
|
||||
func (f fileInfo) Mode() fs.FileMode {
|
||||
func (f FileInfo) SymlinkPath() string {
|
||||
return f.target
|
||||
}
|
||||
|
||||
func (f FileInfo) Mode() fs.FileMode {
|
||||
switch f.fileType {
|
||||
case inode.Dir, inode.EDir:
|
||||
return fs.FileMode(f.perm | uint32(fs.ModeDir))
|
||||
@@ -65,31 +96,31 @@ func (f fileInfo) Mode() fs.FileMode {
|
||||
return fs.FileMode(f.perm)
|
||||
}
|
||||
|
||||
func (f fileInfo) ModTime() time.Time {
|
||||
func (f FileInfo) ModTime() time.Time {
|
||||
return time.Unix(int64(f.modTime), 0)
|
||||
}
|
||||
|
||||
func (f fileInfo) IsDir() bool {
|
||||
func (f FileInfo) IsDir() bool {
|
||||
return f.fileType == inode.Dir || f.fileType == inode.EDir
|
||||
}
|
||||
|
||||
func (f fileInfo) IsSymlink() bool {
|
||||
func (f FileInfo) IsSymlink() bool {
|
||||
return f.fileType == inode.Sym || f.fileType == inode.ESym
|
||||
}
|
||||
|
||||
func (f fileInfo) IsDevice() bool {
|
||||
func (f FileInfo) IsDevice() bool {
|
||||
return f.fileType == inode.Block || f.fileType == inode.EBlock ||
|
||||
f.fileType == inode.Char || f.fileType == inode.EChar
|
||||
}
|
||||
|
||||
func (f fileInfo) IsFifo() bool {
|
||||
func (f FileInfo) IsFifo() bool {
|
||||
return f.fileType == inode.Fifo || f.fileType == inode.EFifo
|
||||
}
|
||||
|
||||
func (f fileInfo) IsSocket() bool {
|
||||
func (f FileInfo) IsSocket() bool {
|
||||
return f.fileType == inode.Sock || f.fileType == inode.ESock
|
||||
}
|
||||
|
||||
func (f fileInfo) Sys() any {
|
||||
func (f FileInfo) Sys() any {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
|
||||
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
|
||||
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
|
||||
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
|
||||
github.com/pierrec/lz4/v4 v4.1.21 h1:yOVMLb6qSIDP67pl/5F7RepeKYu/VmTyEXvuMI5d9mQ=
|
||||
github.com/pierrec/lz4/v4 v4.1.21/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
|
||||
github.com/pierrec/lz4/v4 v4.1.22 h1:cKFw6uJDK+/gfw5BcDL0JL5aBsAFdsIT18eRtLj7VIU=
|
||||
github.com/pierrec/lz4/v4 v4.1.22/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
|
||||
github.com/rasky/go-lzo v0.0.0-20200203143853-96a758eda86e h1:dCWirM5F3wMY+cmRda/B1BiPsFtmzXqV9b0hLWtVBMs=
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !no_obsolete
|
||||
|
||||
package decompress
|
||||
|
||||
import (
|
||||
@@ -9,6 +11,10 @@ import (
|
||||
|
||||
type Lzma struct{}
|
||||
|
||||
func NewLzma() (Lzma, error) {
|
||||
return Lzma{}, nil
|
||||
}
|
||||
|
||||
func (l Lzma) Decompress(data []byte) ([]byte, error) {
|
||||
rdr, err := lzma.NewReader(bytes.NewReader(data))
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
//go:build no_obsolete
|
||||
|
||||
package decompress
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
type Lzma struct{}
|
||||
|
||||
func NewLzma() (Lzma, error) {
|
||||
return Lzma{}, errors.New("lzma compression is disable in this build with no_obsolete")
|
||||
}
|
||||
|
||||
func (l Lzma) Decompress(data []byte) ([]byte, error) {
|
||||
return nil, errors.New("lzma compression is disable in this build with no_obsolete")
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build !no_gpl
|
||||
|
||||
package decompress
|
||||
|
||||
import (
|
||||
@@ -8,6 +10,10 @@ import (
|
||||
|
||||
type Lzo struct{}
|
||||
|
||||
func NewLzo() (Lzo, error) {
|
||||
return Lzo{}, nil
|
||||
}
|
||||
|
||||
func (l Lzo) Decompress(data []byte) ([]byte, error) {
|
||||
return lzo.Decompress1X(bytes.NewReader(data), len(data), 0)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
//go:build no_gpl
|
||||
|
||||
package decompress
|
||||
|
||||
import "errors"
|
||||
|
||||
type Lzo struct{}
|
||||
|
||||
func NewLzo() (Lzo, error) {
|
||||
return Lzo{}, errors.New("lzo compression is disable in this build with no_gpl")
|
||||
}
|
||||
|
||||
func (l Lzo) Decompress(data []byte) ([]byte, error) {
|
||||
return nil, errors.New("lzo compression is disable in this build with no_gpl")
|
||||
}
|
||||
+9
-3
@@ -59,15 +59,21 @@ func NewReader(r io.ReaderAt) (rdr *Reader, err error) {
|
||||
case ZlibCompression:
|
||||
rdr.d = decompress.Zlib{}
|
||||
case LZMACompression:
|
||||
rdr.d = decompress.Lzma{}
|
||||
rdr.d, err = decompress.NewLzma()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case LZOCompression:
|
||||
rdr.d = decompress.Lzo{}
|
||||
rdr.d, err = decompress.NewLzo()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case XZCompression:
|
||||
rdr.d = decompress.Xz{}
|
||||
case LZ4Compression:
|
||||
rdr.d = decompress.Lz4{}
|
||||
case ZSTDCompression:
|
||||
rdr.d = &decompress.Zstd{}
|
||||
rdr.d = decompress.Zstd{}
|
||||
default:
|
||||
return nil, errors.New("invalid compression type. possible corrupted archive")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user