Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c9d451e24c | |||
| ae5ade0683 | |||
| a7b6801d2b | |||
| a123935250 | |||
| 8a91e1a1c1 | |||
| 07962426b2 | |||
| d89153c3e2 | |||
| 3f1b2a8d1e | |||
| 3691e1f486 | |||
| 8ab566521d | |||
| dd08d3516d | |||
| 8dba30e24f | |||
| d4e2577075 | |||
| 23371163c0 | |||
| 69f56d6951 | |||
| 17e1d65488 | |||
| 80946f58e7 | |||
| 4187598783 | |||
| 9cf92c4916 | |||
| 407d649b3d | |||
| dcf59a4261 | |||
| 1506ca0ac3 | |||
| fe9344b633 |
@@ -13,6 +13,6 @@ Thanks also to [distri's squashfs library](https://github.com/distr1/distri/tree
|
|||||||
|
|
||||||
## Performane
|
## Performane
|
||||||
|
|
||||||
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 libarary and about half a second with `unsquashfs`)
|
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`)
|
||||||
|
|
||||||
## [TODO](https://github.com/CalebQ42/squashfs/projects/1?fullscreen=true)
|
## [TODO](https://github.com/CalebQ42/squashfs/projects/1?fullscreen=true)
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
+1
-1
@@ -239,9 +239,9 @@ mainLoop:
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return totalWrite, err
|
return totalWrite, err
|
||||||
}
|
}
|
||||||
|
curIndex++
|
||||||
} else {
|
} else {
|
||||||
backlog = append(backlog, cache)
|
backlog = append(backlog, cache)
|
||||||
}
|
}
|
||||||
curIndex++
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,137 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
@@ -2,511 +2,352 @@ package squashfs
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
|
"io/fs"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/CalebQ42/squashfs/internal/directory"
|
"github.com/CalebQ42/squashfs/internal/directory"
|
||||||
"github.com/CalebQ42/squashfs/internal/inode"
|
"github.com/CalebQ42/squashfs/internal/inode"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
//File represents a file inside a squashfs archive.
|
||||||
//ErrNotDirectory is returned when you're trying to do directory things with a non-directory
|
|
||||||
errNotDirectory = errors.New("File is not a directory")
|
|
||||||
//ErrNotFile is returned when you're trying to do file things with a directory
|
|
||||||
errNotFile = errors.New("File is not a file")
|
|
||||||
//ErrNotReading is returned when running functions that are only meant to be used when reading a squashfs
|
|
||||||
errNotReading = errors.New("Function only supported when reading a squashfs")
|
|
||||||
//ErrBrokenSymlink is returned when using ExtractWithOptions with the unbreakSymlink set to true, but the symlink's file cannot be extracted.
|
|
||||||
ErrBrokenSymlink = errors.New("Extracted symlink is probably broken")
|
|
||||||
)
|
|
||||||
|
|
||||||
//File is the main way to interact with files within squashfs, or when putting files into a squashfs.
|
|
||||||
//File can be either a file or folder. When reading from a squashfs, it reads from the datablocks.
|
|
||||||
//When writing, this holds the information on WHERE the file will be placed inside the archive.
|
|
||||||
//
|
|
||||||
//Implements os.FileInfo and io.ReadCloser
|
|
||||||
type File struct {
|
type File struct {
|
||||||
Reader io.Reader
|
i *inode.Inode
|
||||||
Parent *File
|
parent *FS
|
||||||
r *Reader //Underlying reader. When writing, will probably be an os.File. When reading this is kept nil UNTIL reading to save memory.
|
r *Reader
|
||||||
in *inode.Inode
|
reader *fileReader
|
||||||
name string
|
name string
|
||||||
path string
|
dirsRead int
|
||||||
filType int //The file's type, using inode types.
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//get a File from a directory.entry
|
//File creates a File from the FileInfo.
|
||||||
func (r *Reader) newFileFromDirEntry(entry *directory.Entry) (fil *File, err error) {
|
//*File satisfies fs.File and fs.ReadDirFile.
|
||||||
fil = new(File)
|
func (f FileInfo) File() (file *File, err error) {
|
||||||
fil.in, err = r.getInodeFromEntry(entry)
|
file = &File{
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
file.i, err = r.getInodeFromEntry(en)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
fil.name = entry.Name
|
if file.IsRegular() {
|
||||||
fil.r = r
|
file.reader, err = r.newFileReader(file.i)
|
||||||
fil.filType = fil.in.Type
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//Name is the file's name
|
//Stat returns the File's fs.FileInfo
|
||||||
func (f *File) Name() string {
|
func (f File) Stat() (fs.FileInfo, error) {
|
||||||
|
return &FileInfo{
|
||||||
|
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 {
|
||||||
|
f.reader = nil
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//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")
|
||||||
|
}
|
||||||
|
ffs, err := f.FS()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var beg, end int
|
||||||
|
if n <= 0 {
|
||||||
|
beg, end = 0, len(ffs.entries)
|
||||||
|
} else {
|
||||||
|
beg, end = f.dirsRead, f.dirsRead+n
|
||||||
|
if end > len(ffs.entries) {
|
||||||
|
end = len(ffs.entries)
|
||||||
|
err = io.EOF
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out := make([]fs.DirEntry, end-beg)
|
||||||
|
for i, ent := range ffs.entries[beg:end] {
|
||||||
|
out[i] = f.r.newDirEntry(ent, ffs)
|
||||||
|
}
|
||||||
|
return out, err
|
||||||
|
}
|
||||||
|
|
||||||
|
//FS returns the File as a FS.
|
||||||
|
func (f File) FS() (*FS, error) {
|
||||||
|
if !f.IsDir() {
|
||||||
|
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.
|
||||||
|
func (f File) IsDir() bool {
|
||||||
|
return f.i.Type == inode.DirType || f.i.Type == inode.ExtDirType
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f File) path() string {
|
||||||
|
if f.name == "/" {
|
||||||
return f.name
|
return f.name
|
||||||
}
|
}
|
||||||
|
return f.parent.path() + "/" + f.name
|
||||||
//Size is the complete size of the file. Zero if it's not a file.
|
|
||||||
func (f *File) Size() int64 {
|
|
||||||
switch f.filType {
|
|
||||||
case inode.FileType:
|
|
||||||
return int64(f.in.Info.(inode.File).Size)
|
|
||||||
case inode.ExtFileType:
|
|
||||||
return int64(f.in.Info.(inode.ExtFile).Size)
|
|
||||||
default:
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//ModTime is the time of last modification.
|
//IsRegular yep.
|
||||||
func (f *File) ModTime() time.Time {
|
func (f File) IsRegular() bool {
|
||||||
return time.Unix(int64(f.in.Header.ModifiedTime), 0)
|
return f.i.Type == inode.FileType || f.i.Type == inode.ExtFileType
|
||||||
}
|
}
|
||||||
|
|
||||||
//Sys returns the underlying reader, file.Reader. If the reader isn't initialized, it will initialize it.
|
//IsSymlink yep.
|
||||||
//If called on something other then a file, returns nil.
|
func (f File) IsSymlink() bool {
|
||||||
func (f *File) Sys() interface{} {
|
return f.i.Type == inode.SymType || f.i.Type == inode.ExtSymType
|
||||||
if f.IsFile() {
|
|
||||||
if f.Reader == nil && f.r != nil {
|
|
||||||
var err error
|
|
||||||
f.Reader, err = f.r.newFileReader(f.in)
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return f.Reader
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//GetChildren returns a *squashfs.File slice of every direct child of the directory. If the File is not a directory, will return ErrNotDirectory
|
//SymlinkPath returns the symlink's target path. Is the File isn't a symlink, returns an empty string.
|
||||||
func (f *File) GetChildren() (children []*File, err error) {
|
func (f File) SymlinkPath() string {
|
||||||
children = make([]*File, 0)
|
switch f.i.Type {
|
||||||
if f.r == nil {
|
|
||||||
return nil, errNotReading
|
|
||||||
}
|
|
||||||
if !f.IsDir() {
|
|
||||||
return nil, errNotDirectory
|
|
||||||
}
|
|
||||||
dir, err := f.r.readDirFromInode(f.in)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var fil *File
|
|
||||||
for _, entry := range dir.Entries {
|
|
||||||
fil, err = f.r.newFileFromDirEntry(&entry)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fil.Parent = f
|
|
||||||
if f.name != "" {
|
|
||||||
fil.path = f.Path()
|
|
||||||
}
|
|
||||||
children = append(children, fil)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//GetChildrenRecursively returns ALL children. Goes down ALL folder paths.
|
|
||||||
func (f *File) GetChildrenRecursively() (children []*File, err error) {
|
|
||||||
children = make([]*File, 0)
|
|
||||||
if f.r == nil {
|
|
||||||
return nil, errNotReading
|
|
||||||
}
|
|
||||||
if !f.IsDir() {
|
|
||||||
return nil, errNotDirectory
|
|
||||||
}
|
|
||||||
children, err = f.GetChildren()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var childFolders []*File
|
|
||||||
for _, child := range children {
|
|
||||||
if child.IsDir() {
|
|
||||||
childFolders = append(childFolders, child)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
foldChil := make(chan []*File)
|
|
||||||
errChan := make(chan error)
|
|
||||||
for _, folds := range childFolders {
|
|
||||||
go func(fil *File) {
|
|
||||||
childs, err := fil.GetChildrenRecursively()
|
|
||||||
errChan <- err
|
|
||||||
foldChil <- childs
|
|
||||||
}(folds)
|
|
||||||
}
|
|
||||||
for range childFolders {
|
|
||||||
err = <-errChan
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
children = append(children, <-foldChil...)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//Path returns the path of the file within the archive.
|
|
||||||
func (f *File) Path() string {
|
|
||||||
if f.name == "" {
|
|
||||||
return f.path
|
|
||||||
}
|
|
||||||
return f.path + "/" + f.name
|
|
||||||
}
|
|
||||||
|
|
||||||
//GetFileAtPath tries to return the File at the given path, relative to the file.
|
|
||||||
//Returns nil if called on something other then a folder, OR if the path goes oustide the archive.
|
|
||||||
//Allows wildcards supported by path.Match (namely * and ?).
|
|
||||||
func (f *File) GetFileAtPath(dirPath string) *File {
|
|
||||||
if dirPath == "" {
|
|
||||||
return f
|
|
||||||
}
|
|
||||||
dirPath = strings.TrimSuffix(strings.TrimPrefix(dirPath, "/"), "/")
|
|
||||||
if dirPath != "" && !f.IsDir() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
for strings.HasSuffix(dirPath, "./") {
|
|
||||||
//since you can TECHNICALLY have an infinite amount of ./ and it would still be valid.
|
|
||||||
dirPath = strings.TrimPrefix(dirPath, "./")
|
|
||||||
}
|
|
||||||
split := strings.Split(dirPath, "/")
|
|
||||||
if split[0] == ".." && f.name == "" {
|
|
||||||
return nil
|
|
||||||
} else if split[0] == ".." {
|
|
||||||
if f.Parent != nil {
|
|
||||||
return f.Parent.GetFileAtPath(strings.Join(split[1:], "/"))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
children, err := f.GetChildren()
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
for _, child := range children {
|
|
||||||
eq, _ := path.Match(split[0], child.name)
|
|
||||||
if eq {
|
|
||||||
return child.GetFileAtPath(strings.Join(split[1:], "/"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//IsDir returns if the file is a directory.
|
|
||||||
func (f *File) IsDir() bool {
|
|
||||||
return f.filType == inode.DirType || f.filType == inode.ExtDirType
|
|
||||||
}
|
|
||||||
|
|
||||||
//IsSymlink returns if the file is a symlink.
|
|
||||||
func (f *File) IsSymlink() bool {
|
|
||||||
return f.filType == inode.SymType || f.filType == inode.ExtSymlinkType
|
|
||||||
}
|
|
||||||
|
|
||||||
//IsFile returns if the file is a file.
|
|
||||||
func (f *File) IsFile() bool {
|
|
||||||
return f.filType == inode.FileType || f.filType == inode.ExtFileType
|
|
||||||
}
|
|
||||||
|
|
||||||
//SymlinkPath returns the path the symlink is pointing to. If the file ISN'T a symlink, will return an empty string.
|
|
||||||
//If a path begins with "/" then the symlink is pointing to an absolute path (starting from root, and not a file inside the archive)
|
|
||||||
func (f *File) SymlinkPath() string {
|
|
||||||
switch f.filType {
|
|
||||||
case inode.SymType:
|
case inode.SymType:
|
||||||
return f.in.Info.(inode.Sym).Path
|
return f.i.Info.(inode.Sym).Path
|
||||||
case inode.ExtSymlinkType:
|
case inode.ExtSymType:
|
||||||
return f.in.Info.(inode.Sym).Path
|
return f.i.Info.(inode.ExtSym).Path
|
||||||
default:
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
//GetSymlinkFile tries to return the squashfs.File associated with the symlink. If the file isn't a symlink
|
//GetSymlinkFile returns the File the symlink is pointing to.
|
||||||
//or the symlink points to a location outside the archive, nil is returned.
|
//If not a symlink, or the target is unobtainable (such as it being outside the archive or it's absolute) returns nil
|
||||||
func (f *File) GetSymlinkFile() *File {
|
func (f File) GetSymlinkFile() *File {
|
||||||
if !f.IsSymlink() {
|
if !f.IsSymlink() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if strings.HasSuffix(f.SymlinkPath(), "/") {
|
if strings.HasPrefix(f.SymlinkPath(), "/") {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return f.Parent.GetFileAtPath(f.SymlinkPath())
|
sym, err := f.parent.Open(f.SymlinkPath())
|
||||||
}
|
if err != nil {
|
||||||
|
|
||||||
//GetSymlinkFileRecursive tries to return the squasfs.File associated with the symlink. It will recursively
|
|
||||||
//try to get the symlink's file. This will return either a non-symlink File, or nil.
|
|
||||||
func (f *File) GetSymlinkFileRecursive() *File {
|
|
||||||
if !f.IsSymlink() {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if strings.HasSuffix(f.SymlinkPath(), "/") {
|
return sym.(*File)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
sym := f
|
|
||||||
for {
|
//ExtractionOptions are available options on how to extract.
|
||||||
sym = sym.GetSymlinkFile()
|
type ExtractionOptions struct {
|
||||||
if sym == nil {
|
notBase bool
|
||||||
return nil
|
DereferenceSymlink bool //Replace symlinks with the target file
|
||||||
}
|
UnbreakSymlink bool //Try to make sure symlinks remain unbroken when extracted, without changing the symlink
|
||||||
if !sym.IsSymlink() {
|
Verbose bool //Prints extra info to log on an error
|
||||||
return sym
|
FolderPerm fs.FileMode //The permissions used when creating the extraction folder
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//DefaultOptions is the default ExtractionOptions.
|
||||||
|
func DefaultOptions() ExtractionOptions {
|
||||||
|
return ExtractionOptions{
|
||||||
|
DereferenceSymlink: false,
|
||||||
|
UnbreakSymlink: false,
|
||||||
|
Verbose: false,
|
||||||
|
FolderPerm: fs.ModePerm,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Mode returns the os.FileMode of the File. Sets mode bits for directories and symlinks.
|
//ExtractTo extracts the File to the given folder with the default options.
|
||||||
func (f *File) Mode() os.FileMode {
|
//If the File is a directory, it instead extracts the directory's contents to the folder.
|
||||||
mode := os.FileMode(f.in.Header.Permissions)
|
func (f File) ExtractTo(folder string) error {
|
||||||
switch {
|
return f.ExtractWithOptions(folder, DefaultOptions())
|
||||||
case f.IsDir():
|
|
||||||
mode = mode | os.ModeDir
|
|
||||||
case f.IsSymlink():
|
|
||||||
mode = mode | os.ModeSymlink
|
|
||||||
}
|
|
||||||
return mode
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//ExtractTo extracts the file to the given path. This is the same as ExtractWithOptions(path, false, false, os.ModePerm, false).
|
//ExtractSymlink extracts the File to the folder with the DereferenceSymlink option.
|
||||||
//Will NOT try to keep symlinks valid, folders extracted will have the permissions set by the squashfs, but the folder to make path will have full permissions (777).
|
//If the File is a directory, it instead extracts the directory's contents to the folder.
|
||||||
//
|
func (f File) ExtractSymlink(folder string) error {
|
||||||
//Will try it's best to extract all files, and if any errors come up, they will be appended to the error slice that's returned.
|
return f.ExtractWithOptions(folder, ExtractionOptions{
|
||||||
func (f *File) ExtractTo(path string) []error {
|
DereferenceSymlink: true,
|
||||||
return f.ExtractWithOptions(path, false, false, os.ModePerm, false)
|
FolderPerm: fs.ModePerm,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//ExtractSymlink is similar to ExtractTo, but when it extracts a symlink, it instead extracts the file associated with the symlink in it's place.
|
//ExtractWithOptions extracts the File to the given folder with the given ExtrationOptions.
|
||||||
//This is the same as ExtractWithOptions(path, true, false, os.ModePerm, false)
|
//If the File is a directory, it instead extracts the directory's contents to the folder.
|
||||||
func (f *File) ExtractSymlink(path string) []error {
|
func (f File) ExtractWithOptions(folder string, op ExtractionOptions) error {
|
||||||
return f.ExtractWithOptions(path, true, false, os.ModePerm, false)
|
folder = path.Clean(folder)
|
||||||
}
|
if !op.notBase {
|
||||||
|
err := os.MkdirAll(folder, op.FolderPerm)
|
||||||
//ExtractWithOptions will extract the file to the given path, while allowing customization on how it works. ExtractTo is the "default" options.
|
|
||||||
//Will try it's best to extract all files, and if any errors come up, they will be appended to the error slice that's returned.
|
|
||||||
//Should only return multiple errors if extracting a folder.
|
|
||||||
//
|
|
||||||
//If dereferenceSymlink is set, instead of extracting a symlink, it will extract the file the symlink is pointed to in it's place.
|
|
||||||
//If both dereferenceSymlink and unbreakSymlink is set, dereferenceSymlink takes precendence.
|
|
||||||
//
|
|
||||||
//If unbreakSymlink is set, it will also try to extract the symlink's associated file. WARNING: the symlink's file may have to go up the directory to work.
|
|
||||||
//If unbreakSymlink is set and the file cannot be extracted, a ErrBrokenSymlink will be appended to the returned error slice.
|
|
||||||
//
|
|
||||||
//folderPerm only applies to the folders created to get to path. Folders from the archive are given the correct permissions defined by the archive.
|
|
||||||
func (f *File) ExtractWithOptions(path string, dereferenceSymlink, unbreakSymlink bool, folderPerm os.FileMode, verbose bool) (errs []error) {
|
|
||||||
errs = make([]error, 0)
|
|
||||||
err := os.MkdirAll(path, folderPerm)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []error{err}
|
return err
|
||||||
}
|
|
||||||
switch {
|
|
||||||
case f.IsDir():
|
|
||||||
if f.name != "" {
|
|
||||||
//TODO: check if folder is present, and if so, try to set it's permission
|
|
||||||
err = os.Mkdir(path+"/"+f.name, os.ModePerm)
|
|
||||||
if err != nil {
|
|
||||||
if verbose {
|
|
||||||
fmt.Println("Error while making: ", path+"/"+f.name)
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
errs = append(errs, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fil, err := os.Open(path + "/" + f.name)
|
|
||||||
if err != nil {
|
|
||||||
if verbose {
|
|
||||||
fmt.Println("Error while opening:", path+"/"+f.name)
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
errs = append(errs, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fil.Chown(int(f.r.idTable[f.in.Header.UID]), int(f.r.idTable[f.in.Header.GID]))
|
|
||||||
//don't mention anything when it fails. Because it fails often. Probably has something to do about uid & gid 0
|
|
||||||
// if err != nil {
|
|
||||||
// if verbose {
|
|
||||||
// fmt.Println("Error while changing owner:", path+"/"+f.Name)
|
|
||||||
// fmt.Println(err)
|
|
||||||
// }
|
|
||||||
// errs = append(errs, err)
|
|
||||||
// }
|
|
||||||
err = fil.Chmod(f.Mode())
|
|
||||||
if err != nil {
|
|
||||||
if verbose {
|
|
||||||
fmt.Println("Error while changing owner:", path+"/"+f.name)
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
errs = append(errs, err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
children, err := f.GetChildren()
|
stat, err := f.Stat()
|
||||||
if err != nil {
|
if f.IsDir() {
|
||||||
if verbose {
|
if op.notBase {
|
||||||
fmt.Println("Error getting children for:", f.Path())
|
err = os.Mkdir(folder+"/"+f.name, stat.Mode())
|
||||||
fmt.Println(err)
|
if err != nil && !os.IsExist(err) {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
errs = append(errs, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
finishChan := make(chan []error)
|
|
||||||
for _, child := range children {
|
|
||||||
go func(child *File) {
|
|
||||||
if f.name == "" {
|
|
||||||
finishChan <- child.ExtractWithOptions(path, dereferenceSymlink, unbreakSymlink, folderPerm, verbose)
|
|
||||||
} else {
|
} else {
|
||||||
finishChan <- child.ExtractWithOptions(path+"/"+f.name, dereferenceSymlink, unbreakSymlink, folderPerm, verbose)
|
op.notBase = true
|
||||||
}
|
}
|
||||||
}(child)
|
var ents []fs.DirEntry
|
||||||
|
ents, err = f.ReadDir(0)
|
||||||
|
if err != nil {
|
||||||
|
if op.Verbose {
|
||||||
|
log.Println("Error while reading children of", f.path())
|
||||||
}
|
}
|
||||||
for range children {
|
return err
|
||||||
errs = append(errs, (<-finishChan)...)
|
|
||||||
}
|
}
|
||||||
|
errChan := make(chan error)
|
||||||
|
for i := 0; i < len(ents); i++ {
|
||||||
|
go func(ent *DirEntry) {
|
||||||
|
fil, goErr := ent.File()
|
||||||
|
if goErr != nil {
|
||||||
|
errChan <- goErr
|
||||||
|
fil.Close()
|
||||||
return
|
return
|
||||||
case f.IsFile():
|
}
|
||||||
fil, err := os.Create(path + "/" + f.name)
|
errChan <- fil.ExtractWithOptions(folder+"/"+f.name, op)
|
||||||
|
fil.Close()
|
||||||
|
return
|
||||||
|
}(ents[i].(*DirEntry))
|
||||||
|
}
|
||||||
|
for i := 0; i < len(ents); i++ {
|
||||||
|
err = <-errChan
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
} else if f.IsRegular() {
|
||||||
|
var fil *os.File
|
||||||
|
fil, err = os.Create(folder + "/" + f.name)
|
||||||
if os.IsExist(err) {
|
if os.IsExist(err) {
|
||||||
err = os.Remove(path + "/" + f.name)
|
os.Remove(folder + "/" + f.name)
|
||||||
|
fil, err = os.Create(folder + "/" + f.name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if verbose {
|
log.Println("Error while creating", folder+"/"+f.name)
|
||||||
fmt.Println("Error while making:", path+"/"+f.name)
|
return err
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
errs = append(errs, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fil, err = os.Create(path + "/" + f.name)
|
|
||||||
if err != nil {
|
|
||||||
if verbose {
|
|
||||||
fmt.Println("Error while making:", path+"/"+f.name)
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
errs = append(errs, err)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
if verbose {
|
return err
|
||||||
fmt.Println("Error while making:", path+"/"+f.name)
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
}
|
||||||
errs = append(errs, err)
|
_, err = io.Copy(fil, f)
|
||||||
return
|
|
||||||
} //Since we will be reading from the file
|
|
||||||
if f.Reader == nil && f.r != nil {
|
|
||||||
f.Reader, err = f.r.newFileReader(f.in)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if verbose {
|
log.Println("Error while copying data to", folder+"/"+f.name)
|
||||||
fmt.Println("Error while Copying data to:", path+"/"+f.name)
|
return err
|
||||||
fmt.Println(err)
|
|
||||||
}
|
}
|
||||||
errs = append(errs, err)
|
return nil
|
||||||
return
|
} else if f.IsSymlink() {
|
||||||
}
|
|
||||||
}
|
|
||||||
_, err = io.Copy(fil, f.Reader)
|
|
||||||
if err != nil {
|
|
||||||
if verbose {
|
|
||||||
fmt.Println("Error while Copying data to:", path+"/"+f.name)
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
errs = append(errs, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fil.Chown(int(f.r.idTable[f.in.Header.UID]), int(f.r.idTable[f.in.Header.GID]))
|
|
||||||
//don't mention anything when it fails. Because it fails often. Probably has something to do about uid & gid 0
|
|
||||||
// if err != nil {
|
|
||||||
// if verbose {
|
|
||||||
// fmt.Println("Error while changing owner:", path+"/"+f.Name)
|
|
||||||
// fmt.Println(err)
|
|
||||||
// }
|
|
||||||
// errs = append(errs, err)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
err = fil.Chmod(f.Mode())
|
|
||||||
if err != nil {
|
|
||||||
if verbose {
|
|
||||||
fmt.Println("Error while setting permissions for:", path+"/"+f.name)
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
errs = append(errs, err)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
case f.IsSymlink():
|
|
||||||
symPath := f.SymlinkPath()
|
symPath := f.SymlinkPath()
|
||||||
if dereferenceSymlink {
|
if op.DereferenceSymlink {
|
||||||
fil := f.GetSymlinkFile()
|
fil := f.GetSymlinkFile()
|
||||||
if fil == nil {
|
if fil == nil {
|
||||||
if verbose {
|
if op.Verbose {
|
||||||
fmt.Println("Symlink path(", symPath, ") is outside the archive:"+path+"/"+f.name)
|
log.Println("Symlink path(", symPath, ") is unobtainable:", folder+"/"+f.name)
|
||||||
}
|
}
|
||||||
return
|
return errors.New("Cannot get symlink target")
|
||||||
}
|
}
|
||||||
fil.name = f.name
|
fil.name = f.name
|
||||||
extracSymErrs := fil.ExtractWithOptions(path, dereferenceSymlink, unbreakSymlink, folderPerm, verbose)
|
err = fil.ExtractWithOptions(folder, op)
|
||||||
if len(extracSymErrs) > 0 {
|
if err != nil {
|
||||||
if verbose {
|
if op.Verbose {
|
||||||
fmt.Println("Error(s) while extracting the symlink's file:", path+"/"+f.name)
|
log.Println("Error while extracting the symlink's file:", folder+"/"+f.name)
|
||||||
fmt.Println(extracSymErrs)
|
|
||||||
}
|
}
|
||||||
errs = append(errs, extracSymErrs...)
|
return err
|
||||||
}
|
}
|
||||||
return
|
return nil
|
||||||
} else if unbreakSymlink {
|
} else if op.UnbreakSymlink {
|
||||||
fil := f.GetSymlinkFile()
|
fil := f.GetSymlinkFile()
|
||||||
if fil != nil {
|
if fil == nil {
|
||||||
symPath = path + "/" + symPath
|
if op.Verbose {
|
||||||
paths := strings.Split(symPath, "/")
|
log.Println("Symlink path(", symPath, ") is unobtainable:", folder+"/"+f.name)
|
||||||
extracSymErrs := fil.ExtractWithOptions(strings.Join(paths[:len(paths)-1], "/"), dereferenceSymlink, unbreakSymlink, folderPerm, verbose)
|
|
||||||
if len(extracSymErrs) > 0 {
|
|
||||||
if verbose {
|
|
||||||
fmt.Println("Error(s) while extracting the symlink's file:", path+"/"+f.name)
|
|
||||||
fmt.Println(extracSymErrs)
|
|
||||||
}
|
}
|
||||||
errs = append(errs, extracSymErrs...)
|
return errors.New("Cannot get symlink target")
|
||||||
}
|
}
|
||||||
} else {
|
extractLoc := path.Clean(folder + "/" + path.Dir(symPath))
|
||||||
if verbose {
|
err = fil.ExtractWithOptions(extractLoc, op)
|
||||||
fmt.Println("Symlink path(", symPath, ") is outside the archive:"+path+"/"+f.name)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
err = os.Symlink(f.SymlinkPath(), path+"/"+f.name)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if verbose {
|
if op.Verbose {
|
||||||
fmt.Println("Error while making symlink:", path+"/"+f.name)
|
log.Println("Error while extracting ", folder+"/"+f.name)
|
||||||
fmt.Println(err)
|
|
||||||
}
|
}
|
||||||
errs = append(errs, err)
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
//Read from the file. Doesn't do anything fancy, just pases it to the underlying io.Reader. If a directory, return io.EOF.
|
|
||||||
func (f *File) Read(p []byte) (int, error) {
|
|
||||||
if !f.IsFile() {
|
|
||||||
return 0, io.EOF
|
|
||||||
}
|
|
||||||
var err error
|
|
||||||
if f.Reader == nil && f.r != nil {
|
|
||||||
f.Reader, err = f.r.newFileReader(f.in)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
if op.Verbose {
|
||||||
|
log.Println("Error while making symlink:", folder+"/"+f.name)
|
||||||
}
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
return f.Reader.Read(p)
|
return nil
|
||||||
|
}
|
||||||
|
return errors.New("Unsupported file type. Inode type: " + strconv.Itoa(int(f.i.Type)))
|
||||||
}
|
}
|
||||||
|
|
||||||
//ReadDirFromInode returns a fully populated Directory from a given Inode.
|
//ReadDirFromInode returns a fully populated Directory from a given Inode.
|
||||||
//If the given inode is not a directory it returns an error.
|
//If the given inode is not a directory it returns an error.
|
||||||
func (r *Reader) readDirFromInode(i *inode.Inode) (*directory.Directory, error) {
|
func (r *Reader) readDirFromInode(i *inode.Inode) ([]*directory.Entry, error) {
|
||||||
var offset uint32
|
var offset uint32
|
||||||
var metaOffset uint16
|
var metaOffset uint16
|
||||||
var size uint32
|
var size uint32
|
||||||
@@ -530,26 +371,9 @@ func (r *Reader) readDirFromInode(i *inode.Inode) (*directory.Directory, error)
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
dir, err := directory.NewDirectory(br, size)
|
ents, err := directory.NewDirectory(br, size)
|
||||||
if err != nil {
|
|
||||||
return dir, err
|
|
||||||
}
|
|
||||||
return dir, 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.Header.InodeOffset)))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
_, err = br.Seek(int64(en.Offset), io.SeekStart)
|
return ents, nil
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
i, err := inode.ProcessInode(br, r.super.BlockSize)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return i, nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-5
@@ -12,13 +12,13 @@ import (
|
|||||||
type fragmentEntry struct {
|
type fragmentEntry struct {
|
||||||
Start uint64
|
Start uint64
|
||||||
Size uint32
|
Size uint32
|
||||||
Unused uint32
|
// Unused uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
//GetFragmentDataFromInode returns the fragment data for a given inode.
|
//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.
|
//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) {
|
func (r *Reader) getFragmentDataFromInode(in *inode.Inode) ([]byte, error) {
|
||||||
var size uint32
|
var size uint64
|
||||||
var fragIndex uint32
|
var fragIndex uint32
|
||||||
var fragOffset uint32
|
var fragOffset uint32
|
||||||
if in.Type == inode.FileType {
|
if in.Type == inode.FileType {
|
||||||
@@ -27,9 +27,9 @@ func (r *Reader) getFragmentDataFromInode(in *inode.Inode) ([]byte, error) {
|
|||||||
return make([]byte, 0), nil
|
return make([]byte, 0), nil
|
||||||
}
|
}
|
||||||
if bf.BlockStart == 0 {
|
if bf.BlockStart == 0 {
|
||||||
size = bf.Size
|
size = uint64(bf.Size)
|
||||||
} else {
|
} else {
|
||||||
size = bf.BlockSizes[len(bf.BlockSizes)-1]
|
size = uint64(bf.BlockSizes[len(bf.BlockSizes)-1])
|
||||||
}
|
}
|
||||||
fragIndex = bf.FragmentIndex
|
fragIndex = bf.FragmentIndex
|
||||||
fragOffset = bf.FragmentOffset
|
fragOffset = bf.FragmentOffset
|
||||||
@@ -41,7 +41,7 @@ func (r *Reader) getFragmentDataFromInode(in *inode.Inode) ([]byte, error) {
|
|||||||
if bf.BlockStart == 0 {
|
if bf.BlockStart == 0 {
|
||||||
size = bf.Size
|
size = bf.Size
|
||||||
} else {
|
} else {
|
||||||
size = bf.BlockSizes[len(bf.BlockSizes)-1]
|
size = uint64(bf.BlockSizes[len(bf.BlockSizes)-1])
|
||||||
}
|
}
|
||||||
fragIndex = bf.FragmentIndex
|
fragIndex = bf.FragmentIndex
|
||||||
fragOffset = bf.FragmentOffset
|
fragOffset = bf.FragmentOffset
|
||||||
|
|||||||
@@ -0,0 +1,489 @@
|
|||||||
|
package squashfs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"io/fs"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/CalebQ42/squashfs/internal/directory"
|
||||||
|
)
|
||||||
|
|
||||||
|
//FS is a fs.FS representation of a squashfs directory.
|
||||||
|
//Implements fs.GlobFS, fs.ReadDirFS, fs.ReadFileFS, fs.StatFS, and fs.SubFS
|
||||||
|
type FS struct {
|
||||||
|
r *Reader
|
||||||
|
parent *FS
|
||||||
|
name string
|
||||||
|
entries []*directory.Entry
|
||||||
|
}
|
||||||
|
|
||||||
|
//Open opens the file at name. Returns a squashfs.File.
|
||||||
|
func (f FS) Open(name string) (fs.File, error) {
|
||||||
|
if !fs.ValidPath(name) {
|
||||||
|
return nil, &fs.PathError{
|
||||||
|
Op: "open",
|
||||||
|
Path: name,
|
||||||
|
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.
|
||||||
|
//All paths are relative to the FS.
|
||||||
|
func (f FS) Glob(pattern string) (out []string, err error) {
|
||||||
|
if !fs.ValidPath(pattern) {
|
||||||
|
return nil, &fs.PathError{
|
||||||
|
Op: "glob",
|
||||||
|
Path: pattern,
|
||||||
|
Err: fs.ErrInvalid,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pattern = path.Clean(strings.TrimPrefix(pattern, "/"))
|
||||||
|
split := strings.Split(pattern, "/")
|
||||||
|
if split[0] == ".." {
|
||||||
|
if f.parent == nil {
|
||||||
|
//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 {
|
||||||
|
out = append(out, f.entries[i].Name)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
sub, err := f.Sub(split[0])
|
||||||
|
if err != nil {
|
||||||
|
if pathErr, ok := err.(*fs.PathError); ok {
|
||||||
|
if pathErr.Err == fs.ErrNotExist {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
pathErr.Op = "glob"
|
||||||
|
pathErr.Path = pattern
|
||||||
|
return nil, pathErr
|
||||||
|
}
|
||||||
|
return nil, &fs.PathError{
|
||||||
|
Op: "glob",
|
||||||
|
Path: pattern,
|
||||||
|
Err: err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
subGlob, err := sub.(FS).Glob(strings.Join(split[1:], "/"))
|
||||||
|
if err != nil {
|
||||||
|
if pathErr, ok := err.(*fs.PathError); ok {
|
||||||
|
if pathErr.Err == fs.ErrNotExist {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
pathErr.Op = "glob"
|
||||||
|
pathErr.Path = pattern
|
||||||
|
return nil, pathErr
|
||||||
|
}
|
||||||
|
return nil, &fs.PathError{
|
||||||
|
Op: "glob",
|
||||||
|
Path: pattern,
|
||||||
|
Err: err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i := 0; i < len(subGlob); i++ {
|
||||||
|
subGlob[i] = f.name + "/" + subGlob[i]
|
||||||
|
}
|
||||||
|
out = append(out, subGlob...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//ReadDir returns all the DirEntry 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) {
|
||||||
|
if !fs.ValidPath(name) {
|
||||||
|
return nil, &fs.PathError{
|
||||||
|
Op: "readdir",
|
||||||
|
Path: name,
|
||||||
|
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: "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)
|
||||||
|
if err != nil {
|
||||||
|
if pathErr, ok := err.(*fs.PathError); ok {
|
||||||
|
pathErr.Op = "readfile"
|
||||||
|
pathErr.Path = name
|
||||||
|
return nil, pathErr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var buf bytes.Buffer
|
||||||
|
_, err = io.Copy(&buf, fil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, &fs.PathError{
|
||||||
|
Op: "readfile",
|
||||||
|
Path: name,
|
||||||
|
Err: err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return buf.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//Stat returns the fs.FileInfo for the file at name.
|
||||||
|
func (f FS) Stat(name string) (fs.FileInfo, error) {
|
||||||
|
if !fs.ValidPath(name) {
|
||||||
|
return nil, &fs.PathError{
|
||||||
|
Op: "stat",
|
||||||
|
Path: name,
|
||||||
|
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: "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++ {
|
||||||
|
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: "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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Sub returns the FS at dir
|
||||||
|
func (f FS) Sub(dir string) (fs.FS, error) {
|
||||||
|
if !fs.ValidPath(dir) {
|
||||||
|
return nil, &fs.PathError{
|
||||||
|
Op: "sub",
|
||||||
|
Path: dir,
|
||||||
|
Err: fs.ErrInvalid,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dir = path.Clean(strings.TrimPrefix(dir, "/"))
|
||||||
|
split := strings.Split(dir, "/")
|
||||||
|
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++ {
|
||||||
|
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 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
errChan := make(chan error)
|
||||||
|
for i := 0; i < len(f.entries); i++ {
|
||||||
|
go func(ent *DirEntry) {
|
||||||
|
fil, goErr := ent.File()
|
||||||
|
if goErr != nil {
|
||||||
|
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
|
||||||
|
}
|
||||||
@@ -3,16 +3,17 @@ module github.com/CalebQ42/squashfs
|
|||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/CalebQ42/GoAppImage v0.4.0
|
github.com/CalebQ42/GoAppImage v0.5.0
|
||||||
github.com/adrg/xdg v0.2.3 // indirect
|
github.com/adrg/xdg v0.2.3 // indirect
|
||||||
github.com/google/go-cmp v0.5.4 // indirect
|
github.com/google/go-cmp v0.5.4 // indirect
|
||||||
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect
|
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect
|
||||||
github.com/klauspost/compress v1.11.4
|
github.com/klauspost/compress v1.11.6
|
||||||
github.com/kr/text v0.2.0 // indirect
|
github.com/kr/text v0.2.0 // indirect
|
||||||
github.com/pierrec/lz4/v4 v4.1.2
|
github.com/pierrec/lz4/v4 v4.1.3
|
||||||
github.com/smartystreets/assertions v1.2.0 // indirect
|
github.com/smartystreets/assertions v1.2.0 // indirect
|
||||||
|
github.com/stretchr/testify v1.7.0 // indirect
|
||||||
github.com/ulikunitz/xz v0.5.9
|
github.com/ulikunitz/xz v0.5.9
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
github.com/CalebQ42/GoAppImage v0.4.0 h1:aF+Y/vyo/RGhoyZEW1CMY6WyRWrZZO4ydsRFAtIGnaY=
|
github.com/CalebQ42/GoAppImage v0.5.0 h1:znoKNXtliH754tS9sYwyOIg/0wFDjFN5Twc7PAh1rSM=
|
||||||
github.com/CalebQ42/GoAppImage v0.4.0/go.mod h1:qHudJKAn/dlkNWNnH4h1YKXp29EZ7Bppsn7sNP2HuvU=
|
github.com/CalebQ42/GoAppImage v0.5.0/go.mod h1:qHudJKAn/dlkNWNnH4h1YKXp29EZ7Bppsn7sNP2HuvU=
|
||||||
github.com/adrg/xdg v0.2.2 h1:A7ZHKRz5KGOLJX/bg7IPzStryhvCzAE1wX+KWawPiAo=
|
github.com/adrg/xdg v0.2.2 h1:A7ZHKRz5KGOLJX/bg7IPzStryhvCzAE1wX+KWawPiAo=
|
||||||
github.com/adrg/xdg v0.2.2/go.mod h1:7I2hH/IT30IsupOpKZ5ue7/qNi3CoKzD6tL3HwpaRMQ=
|
github.com/adrg/xdg v0.2.2/go.mod h1:7I2hH/IT30IsupOpKZ5ue7/qNi3CoKzD6tL3HwpaRMQ=
|
||||||
github.com/adrg/xdg v0.2.3 h1:GxXngdYxNDkoUvZXjNJGwqZxWXi43MKbOOlA/00qZi4=
|
github.com/adrg/xdg v0.2.3 h1:GxXngdYxNDkoUvZXjNJGwqZxWXi43MKbOOlA/00qZi4=
|
||||||
@@ -18,8 +18,8 @@ github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 h1:l5lAOZEym3oK3
|
|||||||
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
|
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 h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
|
||||||
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
||||||
github.com/klauspost/compress v1.11.4 h1:kz40R/YWls3iqT9zX9AHN3WoVsrAWVyui5sxuLqiXqU=
|
github.com/klauspost/compress v1.11.6 h1:EgWPCW6O3n1D5n99Zq3xXBt9uCwRGvpwGOusOLNBRSQ=
|
||||||
github.com/klauspost/compress v1.11.4/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
|
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 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
||||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
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/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
@@ -29,8 +29,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
|||||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
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 h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
|
||||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
||||||
github.com/pierrec/lz4/v4 v4.1.2 h1:qvY3YFXRQE/XB8MlLzJH7mSzBs74eA2gg52YTk6jUPM=
|
github.com/pierrec/lz4/v4 v4.1.3 h1:/dvQpkb0o1pVlSgKNQqfkavlnXaIK+hJ0LXsKRUN9D4=
|
||||||
github.com/pierrec/lz4/v4 v4.1.2/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
|
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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
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 h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
|
||||||
@@ -39,9 +39,12 @@ github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N
|
|||||||
github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=
|
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 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
|
||||||
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
|
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/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 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
|
||||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
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 h1:RsKRIA2MO8x56wkkcd3LbtcE/uMszhb6DpRf+3uwa3I=
|
||||||
github.com/ulikunitz/xz v0.5.9/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
|
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 h1:KcZJmh6nFIBeJzTugn5JTU6OOyG0lDOo3R9KwTxTYbo=
|
||||||
@@ -69,5 +72,5 @@ gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU=
|
|||||||
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
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 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-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
@@ -23,38 +23,33 @@ type EntryRaw struct {
|
|||||||
|
|
||||||
//Entry is an entry in a directory.
|
//Entry is an entry in a directory.
|
||||||
type Entry struct {
|
type Entry struct {
|
||||||
*Header
|
|
||||||
Name string
|
Name string
|
||||||
EntryRaw
|
InodeOffset uint32
|
||||||
|
InodeBlockOffset uint16
|
||||||
|
Type uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
//NewEntry creates a new directory entry
|
//NewEntry creates a new directory entry
|
||||||
func NewEntry(rdr io.Reader) (Entry, error) {
|
func NewEntry(rdr io.Reader) (*Entry, error) {
|
||||||
var entry Entry
|
var raw EntryRaw
|
||||||
err := binary.Read(rdr, binary.LittleEndian, &entry.EntryRaw)
|
err := binary.Read(rdr, binary.LittleEndian, &raw)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Entry{}, err
|
return nil, err
|
||||||
}
|
}
|
||||||
tmp := make([]byte, entry.EntryRaw.NameSize+1)
|
tmp := make([]byte, raw.NameSize+1)
|
||||||
err = binary.Read(rdr, binary.LittleEndian, &tmp)
|
err = binary.Read(rdr, binary.LittleEndian, &tmp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Entry{}, err
|
return nil, err
|
||||||
}
|
}
|
||||||
entry.Name = string(tmp)
|
return &Entry{
|
||||||
return entry, err
|
InodeBlockOffset: raw.Offset,
|
||||||
}
|
Type: raw.Type,
|
||||||
|
Name: string(tmp),
|
||||||
//Directory is an entry in the directory table of a squashfs.
|
}, nil
|
||||||
//Will only have multiple headers if there are more then 256 entries
|
|
||||||
type Directory struct {
|
|
||||||
Headers []Header
|
|
||||||
Entries []Entry
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//NewDirectory reads the directory from rdr
|
//NewDirectory reads the directory from rdr
|
||||||
func NewDirectory(base io.Reader, size uint32) (*Directory, error) {
|
func NewDirectory(base io.Reader, size uint32) (entries []*Entry, err error) {
|
||||||
var dir Directory
|
|
||||||
var err error
|
|
||||||
tmp := make([]byte, size)
|
tmp := make([]byte, size)
|
||||||
base.Read(tmp)
|
base.Read(tmp)
|
||||||
rdr := bytes.NewBuffer(tmp)
|
rdr := bytes.NewBuffer(tmp)
|
||||||
@@ -62,6 +57,7 @@ func NewDirectory(base io.Reader, size uint32) (*Directory, error) {
|
|||||||
var hdr Header
|
var hdr Header
|
||||||
err = binary.Read(rdr, binary.LittleEndian, &hdr)
|
err = binary.Read(rdr, binary.LittleEndian, &hdr)
|
||||||
if err == io.ErrUnexpectedEOF {
|
if err == io.ErrUnexpectedEOF {
|
||||||
|
err = nil
|
||||||
break
|
break
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -71,24 +67,21 @@ func NewDirectory(base io.Reader, size uint32) (*Directory, error) {
|
|||||||
if hdr.Count%256 > 0 {
|
if hdr.Count%256 > 0 {
|
||||||
headers++
|
headers++
|
||||||
}
|
}
|
||||||
dir.Headers = append(dir.Headers, hdr)
|
|
||||||
for i := uint32(0); i < hdr.Count; i++ {
|
for i := uint32(0); i < hdr.Count; i++ {
|
||||||
if i != 0 && i%256 == 0 {
|
if i != 0 && i%256 == 0 {
|
||||||
var newHdr Header
|
err = binary.Read(rdr, binary.LittleEndian, &hdr)
|
||||||
err = binary.Read(rdr, binary.LittleEndian, &newHdr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
dir.Headers = append(dir.Headers, newHdr)
|
|
||||||
}
|
}
|
||||||
var ent Entry
|
var ent *Entry
|
||||||
ent, err = NewEntry(rdr)
|
ent, err = NewEntry(rdr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
ent.Header = &dir.Headers[len(dir.Headers)-1]
|
ent.InodeOffset = hdr.InodeOffset
|
||||||
dir.Entries = append(dir.Entries, ent)
|
entries = append(entries, ent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return &dir, nil
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ const (
|
|||||||
SocketType
|
SocketType
|
||||||
ExtDirType
|
ExtDirType
|
||||||
ExtFileType
|
ExtFileType
|
||||||
ExtSymlinkType
|
ExtSymType
|
||||||
ExtBlockDeviceType
|
ExtBlockDeviceType
|
||||||
ExtCharDeviceType
|
ExtCharDeviceType
|
||||||
ExtFifoType
|
ExtFifoType
|
||||||
@@ -25,7 +25,7 @@ const (
|
|||||||
|
|
||||||
//Header is the common header for all inodes
|
//Header is the common header for all inodes
|
||||||
type Header struct {
|
type Header struct {
|
||||||
InodeType uint16
|
Type uint16
|
||||||
Permissions uint16
|
Permissions uint16
|
||||||
UID uint16
|
UID uint16
|
||||||
GID uint16
|
GID uint16
|
||||||
@@ -67,7 +67,8 @@ func NewExtendedDirectory(rdr io.Reader) (ExtDir, error) {
|
|||||||
return inode, err
|
return inode, err
|
||||||
}
|
}
|
||||||
for i := uint16(0); i < inode.IndexCount; i++ {
|
for i := uint16(0); i < inode.IndexCount; i++ {
|
||||||
tmp, err := NewDirectoryIndex(rdr)
|
var tmp DirIndex
|
||||||
|
tmp, err = NewDirectoryIndex(rdr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return inode, err
|
return inode, err
|
||||||
}
|
}
|
||||||
@@ -139,8 +140,8 @@ func NewFile(rdr io.Reader, blockSize uint32) (File, error) {
|
|||||||
|
|
||||||
//ExtFileInit is the information that can be directly decoded
|
//ExtFileInit is the information that can be directly decoded
|
||||||
type ExtFileInit struct {
|
type ExtFileInit struct {
|
||||||
BlockStart uint32
|
BlockStart uint64
|
||||||
Size uint32
|
Size uint64
|
||||||
Sparse uint64
|
Sparse uint64
|
||||||
HardLinks uint32
|
HardLinks uint32
|
||||||
FragmentIndex uint32
|
FragmentIndex uint32
|
||||||
@@ -163,8 +164,8 @@ func NewExtendedFile(rdr io.Reader, blockSize uint32) (ExtFile, error) {
|
|||||||
return inode, err
|
return inode, err
|
||||||
}
|
}
|
||||||
inode.Fragmented = inode.FragmentIndex != 0xFFFFFFFF
|
inode.Fragmented = inode.FragmentIndex != 0xFFFFFFFF
|
||||||
blocks := inode.Size / blockSize
|
blocks := inode.Size / uint64(blockSize)
|
||||||
if inode.Size%blockSize > 0 {
|
if inode.Size%uint64(blockSize) > 0 {
|
||||||
blocks++
|
blocks++
|
||||||
}
|
}
|
||||||
inode.BlockSizes = make([]uint32, blocks, blocks)
|
inode.BlockSizes = make([]uint32, blocks, blocks)
|
||||||
|
|||||||
+33
-30
@@ -2,7 +2,9 @@ package inode
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
//Inode holds an inode. Header is the header that's common for all inodes.
|
//Inode holds an inode. Header is the header that's common for all inodes.
|
||||||
@@ -10,116 +12,117 @@ import (
|
|||||||
//Info holds the actual Inode. Due to each inode type being a different type, it's store as an interface{}
|
//Info holds the actual Inode. Due to each inode type being a different type, it's store as an interface{}
|
||||||
type Inode struct {
|
type Inode struct {
|
||||||
Info interface{} //Info is the parsed specific data. It's type is defined by Type.
|
Info interface{} //Info is the parsed specific data. It's type is defined by Type.
|
||||||
Type int //Type the inode type defined in the header. Here so it's easy to access
|
|
||||||
Header
|
Header
|
||||||
}
|
}
|
||||||
|
|
||||||
//ProcessInode tries to read an inode from the BlockReader
|
//ProcessInode tries to read an inode from the BlockReader
|
||||||
func ProcessInode(br io.Reader, blockSize uint32) (*Inode, error) {
|
func ProcessInode(br io.Reader, blockSize uint32) (*Inode, error) {
|
||||||
var head Header
|
var in Inode
|
||||||
err := binary.Read(br, binary.LittleEndian, &head)
|
err := binary.Read(br, binary.LittleEndian, &in.Header)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var info interface{}
|
switch in.Type {
|
||||||
switch head.InodeType {
|
|
||||||
case DirType:
|
case DirType:
|
||||||
var inode Dir
|
var inode Dir
|
||||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
err = binary.Read(br, binary.LittleEndian, &inode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
info = inode
|
in.Info = inode
|
||||||
case FileType:
|
case FileType:
|
||||||
inode, err := NewFile(br, blockSize)
|
var inode File
|
||||||
|
inode, err = NewFile(br, blockSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
info = inode
|
in.Info = inode
|
||||||
case SymType:
|
case SymType:
|
||||||
inode, err := NewSymlink(br)
|
var inode Sym
|
||||||
|
inode, err = NewSymlink(br)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
info = inode
|
in.Info = inode
|
||||||
case BlockDevType:
|
case BlockDevType:
|
||||||
var inode Device
|
var inode Device
|
||||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
err = binary.Read(br, binary.LittleEndian, &inode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
info = inode
|
in.Info = inode
|
||||||
case CharDevType:
|
case CharDevType:
|
||||||
var inode Device
|
var inode Device
|
||||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
err = binary.Read(br, binary.LittleEndian, &inode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
info = inode
|
in.Info = inode
|
||||||
case FifoType:
|
case FifoType:
|
||||||
var inode IPC
|
var inode IPC
|
||||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
err = binary.Read(br, binary.LittleEndian, &inode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
info = inode
|
in.Info = inode
|
||||||
case SocketType:
|
case SocketType:
|
||||||
var inode IPC
|
var inode IPC
|
||||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
err = binary.Read(br, binary.LittleEndian, &inode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
info = inode
|
in.Info = inode
|
||||||
case ExtDirType:
|
case ExtDirType:
|
||||||
inode, err := NewExtendedDirectory(br)
|
var inode ExtDir
|
||||||
|
inode, err = NewExtendedDirectory(br)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
info = inode
|
in.Info = inode
|
||||||
case ExtFileType:
|
case ExtFileType:
|
||||||
inode, err := NewExtendedFile(br, blockSize)
|
var inode ExtFile
|
||||||
|
inode, err = NewExtendedFile(br, blockSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
info = inode
|
in.Info = inode
|
||||||
case ExtSymlinkType:
|
case ExtSymType:
|
||||||
inode, err := NewExtendedSymlink(br)
|
var inode ExtSym
|
||||||
|
inode, err = NewExtendedSymlink(br)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
info = inode
|
in.Info = inode
|
||||||
case ExtBlockDeviceType:
|
case ExtBlockDeviceType:
|
||||||
var inode ExtDevice
|
var inode ExtDevice
|
||||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
err = binary.Read(br, binary.LittleEndian, &inode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
info = inode
|
in.Info = inode
|
||||||
case ExtCharDeviceType:
|
case ExtCharDeviceType:
|
||||||
var inode ExtDevice
|
var inode ExtDevice
|
||||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
err = binary.Read(br, binary.LittleEndian, &inode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
info = inode
|
in.Info = inode
|
||||||
case ExtFifoType:
|
case ExtFifoType:
|
||||||
var inode ExtIPC
|
var inode ExtIPC
|
||||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
err = binary.Read(br, binary.LittleEndian, &inode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
info = inode
|
in.Info = inode
|
||||||
case ExtSocketType:
|
case ExtSocketType:
|
||||||
var inode ExtIPC
|
var inode ExtIPC
|
||||||
err = binary.Read(br, binary.LittleEndian, &inode)
|
err = binary.Read(br, binary.LittleEndian, &inode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
info = inode
|
in.Info = inode
|
||||||
|
default:
|
||||||
|
return nil, errors.New("Unsupported inode type: " + strconv.Itoa(int(in.Type)))
|
||||||
}
|
}
|
||||||
return &Inode{
|
return &in, nil
|
||||||
Type: int(head.InodeType),
|
|
||||||
Header: head,
|
|
||||||
Info: info,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
magic = 0x73717368
|
magic uint32 = 0x73717368
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -26,36 +26,42 @@ var (
|
|||||||
ErrOptions = errors.New("Possibly incompatible compressor options")
|
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.
|
//Reader processes and reads a squashfs archive.
|
||||||
type Reader struct {
|
type Reader struct {
|
||||||
r io.ReaderAt
|
FS
|
||||||
|
r *io.SectionReader
|
||||||
decompressor compression.Decompressor
|
decompressor compression.Decompressor
|
||||||
|
root *File
|
||||||
fragOffsets []uint64
|
fragOffsets []uint64
|
||||||
idTable []uint32
|
idTable []uint32
|
||||||
super superblock
|
super superblock
|
||||||
flags superblockFlags
|
flags SuperblockFlags
|
||||||
}
|
}
|
||||||
|
|
||||||
//NewSquashfsReader returns a new squashfs.Reader from an io.ReaderAt
|
//NewSquashfsReader returns a new squashfs.Reader from an io.ReaderAt
|
||||||
func NewSquashfsReader(r io.ReaderAt) (*Reader, error) {
|
func NewSquashfsReader(r io.ReaderAt) (*Reader, error) {
|
||||||
hasUnsupportedOptions := false
|
|
||||||
var rdr Reader
|
var rdr Reader
|
||||||
rdr.r = r
|
err := binary.Read(io.NewSectionReader(r, 0, int64(binary.Size(rdr.super))), binary.LittleEndian, &rdr.super)
|
||||||
err := binary.Read(io.NewSectionReader(rdr.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))
|
||||||
if rdr.super.Magic != magic {
|
if rdr.super.Magic != magic {
|
||||||
return nil, errNoMagic
|
return nil, errNoMagic
|
||||||
}
|
}
|
||||||
// if rdr.super.BlockLog == uint16(math.Log2(float64(rdr.super.BlockSize))) {
|
if rdr.super.BlockLog != uint16(math.Log2(float64(rdr.super.BlockSize))) {
|
||||||
// return nil, errors.New("BlockSize and BlockLog doesn't match. The archive is probably corrupt")
|
return nil, errors.New("BlockSize and BlockLog doesn't match. The archive is probably corrupt")
|
||||||
// }
|
}
|
||||||
|
rdr.r.Seek(96, io.SeekStart)
|
||||||
|
hasUnsupportedOptions := false
|
||||||
rdr.flags = rdr.super.GetFlags()
|
rdr.flags = rdr.super.GetFlags()
|
||||||
if rdr.flags.CompressorOptions {
|
if rdr.flags.compressorOptions {
|
||||||
switch rdr.super.CompressionType {
|
switch rdr.super.CompressionType {
|
||||||
case GzipCompression:
|
case GzipCompression:
|
||||||
gzip, err := compression.NewGzipCompressorWithOptions(io.NewSectionReader(rdr.r, int64(binary.Size(rdr.super)), 8))
|
var gzip *compression.Gzip
|
||||||
|
gzip, err = compression.NewGzipCompressorWithOptions(rdr.r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -64,7 +70,8 @@ func NewSquashfsReader(r io.ReaderAt) (*Reader, error) {
|
|||||||
}
|
}
|
||||||
rdr.decompressor = gzip
|
rdr.decompressor = gzip
|
||||||
case XzCompression:
|
case XzCompression:
|
||||||
xz, err := compression.NewXzCompressorWithOptions(io.NewSectionReader(rdr.r, int64(binary.Size(rdr.super)), 8))
|
var xz *compression.Xz
|
||||||
|
xz, err = compression.NewXzCompressorWithOptions(rdr.r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -73,13 +80,15 @@ func NewSquashfsReader(r io.ReaderAt) (*Reader, error) {
|
|||||||
}
|
}
|
||||||
rdr.decompressor = xz
|
rdr.decompressor = xz
|
||||||
case Lz4Compression:
|
case Lz4Compression:
|
||||||
lz4, err := compression.NewLz4CompressorWithOptions(io.NewSectionReader(rdr.r, int64(binary.Size(rdr.super)), 8))
|
var lz4 *compression.Lz4
|
||||||
|
lz4, err = compression.NewLz4CompressorWithOptions(rdr.r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
rdr.decompressor = lz4
|
rdr.decompressor = lz4
|
||||||
case ZstdCompression:
|
case ZstdCompression:
|
||||||
zstd, err := compression.NewZstdCompressorWithOptions(io.NewSectionReader(rdr.r, int64(binary.Size(rdr.super)), 4))
|
var zstd *compression.Zstd
|
||||||
|
zstd, err = compression.NewZstdCompressorWithOptions(rdr.r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -119,13 +128,14 @@ func NewSquashfsReader(r io.ReaderAt) (*Reader, error) {
|
|||||||
}
|
}
|
||||||
unread := rdr.super.IDCount
|
unread := rdr.super.IDCount
|
||||||
blockOffsets := make([]uint64, int(math.Ceil(float64(rdr.super.IDCount)/2048)))
|
blockOffsets := make([]uint64, int(math.Ceil(float64(rdr.super.IDCount)/2048)))
|
||||||
|
rdr.r.Seek(int64(rdr.super.IDTableStart), io.SeekStart)
|
||||||
for i := range blockOffsets {
|
for i := range blockOffsets {
|
||||||
secRdr := io.NewSectionReader(r, int64(rdr.super.IDTableStart)+(8*int64(i)), 8)
|
err = binary.Read(rdr.r, binary.LittleEndian, &blockOffsets[i])
|
||||||
err = binary.Read(secRdr, binary.LittleEndian, &blockOffsets[i])
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
idRdr, err := rdr.newMetadataReader(int64(blockOffsets[i]))
|
var idRdr *metadataReader
|
||||||
|
idRdr, err = rdr.newMetadataReader(int64(blockOffsets[i]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -140,6 +150,23 @@ func NewSquashfsReader(r io.ReaderAt) (*Reader, error) {
|
|||||||
}
|
}
|
||||||
unread -= read
|
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 {
|
if hasUnsupportedOptions {
|
||||||
return &rdr, ErrOptions
|
return &rdr, ErrOptions
|
||||||
}
|
}
|
||||||
@@ -150,105 +177,3 @@ func NewSquashfsReader(r io.ReaderAt) (*Reader, error) {
|
|||||||
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.super.CreationTime), 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
//ExtractTo tries to extract ALL files to the given path. This is the same as getting the root folder and extracting that.
|
|
||||||
func (r *Reader) ExtractTo(path string) []error {
|
|
||||||
root, err := r.GetRootFolder()
|
|
||||||
if err != nil {
|
|
||||||
return []error{err}
|
|
||||||
}
|
|
||||||
return root.ExtractTo(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
//GetRootFolder returns a squashfs.File that references the root directory of the squashfs archive.
|
|
||||||
func (r *Reader) GetRootFolder() (root *File, err error) {
|
|
||||||
root = new(File)
|
|
||||||
mr, err := r.newMetadataReaderFromInodeRef(r.super.RootInodeRef)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
root.in, err = inode.ProcessInode(mr, r.super.BlockSize)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
root.path = "/"
|
|
||||||
root.filType = root.in.Type
|
|
||||||
root.r = r
|
|
||||||
return root, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//GetAllFiles returns a slice of ALL files and folders contained in the squashfs.
|
|
||||||
func (r *Reader) GetAllFiles() (fils []*File, err error) {
|
|
||||||
root, err := r.GetRootFolder()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return root.GetChildrenRecursively()
|
|
||||||
}
|
|
||||||
|
|
||||||
//FindFile returns the first file (in the same order as Reader.GetAllFiles) that the given function returns true for. Returns nil if nothing is found.
|
|
||||||
func (r *Reader) FindFile(query func(*File) bool) *File {
|
|
||||||
root, err := r.GetRootFolder()
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
fils, err := root.GetChildren()
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
var childrenDirs []*File
|
|
||||||
for _, fil := range fils {
|
|
||||||
if query(fil) {
|
|
||||||
return fil
|
|
||||||
}
|
|
||||||
if fil.IsDir() {
|
|
||||||
childrenDirs = append(childrenDirs, fil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for len(childrenDirs) != 0 {
|
|
||||||
var tmp []*File
|
|
||||||
for _, dirs := range childrenDirs {
|
|
||||||
chil, err := dirs.GetChildren()
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
for _, child := range chil {
|
|
||||||
if query(child) {
|
|
||||||
return child
|
|
||||||
}
|
|
||||||
if child.IsDir() {
|
|
||||||
tmp = append(tmp, child)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
childrenDirs = tmp
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//FindAll returns all files where the given function returns true.
|
|
||||||
func (r *Reader) FindAll(query func(*File) bool) (all []*File) {
|
|
||||||
root, err := r.GetRootFolder()
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
fils, err := root.GetChildrenRecursively()
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
for _, fil := range fils {
|
|
||||||
if query(fil) {
|
|
||||||
all = append(all, fil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//GetFileAtPath will return the file at the given path. If the file cannot be found, will return nil.
|
|
||||||
func (r *Reader) GetFileAtPath(path string) *File {
|
|
||||||
dir, err := r.GetRootFolder()
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return dir.GetFileAtPath(path)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import (
|
|||||||
const (
|
const (
|
||||||
downloadURL = "https://github.com/srevinsaju/Firefox-Appimage/releases/download/firefox-v84.0.r20201221152838/firefox-84.0.r20201221152838-x86_64.AppImage"
|
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"
|
appImageName = "firefox-84.0.r20201221152838-x86_64.AppImage"
|
||||||
squashfsName = "balenaEtcher-1.5.113-x64.AppImage.sfs" //testing with a ArchLinux root fs from the live img
|
squashfsName = "balenaEtcher-1.5.113-x64.AppImage.sfs"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSquashfs(t *testing.T) {
|
func TestSquashfs(t *testing.T) {
|
||||||
@@ -33,18 +33,18 @@ func TestSquashfs(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
fmt.Println("stuff", rdr.super.CompressionType)
|
fmt.Println("stuff", rdr.super.CompressionType)
|
||||||
fil := rdr.GetFileAtPath("*.desktop")
|
// fil := rdr.GetFileAtPath("*.desktop")
|
||||||
if fil == nil {
|
// if fil == nil {
|
||||||
t.Fatal("Can't find desktop fil")
|
// t.Fatal("Can't find desktop fil")
|
||||||
}
|
// }
|
||||||
errs := fil.ExtractTo(wd + "/testing")
|
// errs := fil.ExtractTo(wd + "/testing")
|
||||||
if len(errs) > 0 {
|
// if len(errs) > 0 {
|
||||||
t.Fatal(errs)
|
// t.Fatal(errs)
|
||||||
}
|
// }
|
||||||
errs = rdr.ExtractTo(wd + "/testing/" + squashfsName + ".d")
|
// errs = rdr.ExtractTo(wd + "/testing/" + squashfsName + ".d")
|
||||||
if len(errs) > 0 {
|
// if len(errs) > 0 {
|
||||||
t.Fatal(errs)
|
// t.Fatal(errs)
|
||||||
}
|
// }
|
||||||
t.Fatal("No Problems")
|
t.Fatal("No Problems")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,23 +69,14 @@ func TestAppImage(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer aiFil.Close()
|
defer aiFil.Close()
|
||||||
stat, _ := aiFil.Stat()
|
stat, _ := aiFil.Stat()
|
||||||
os.RemoveAll(wd + "/testing/firefox")
|
|
||||||
ai := goappimage.NewAppImage(wd + "/testing/" + appImageName)
|
ai := goappimage.NewAppImage(wd + "/testing/" + appImageName)
|
||||||
start := time.Now()
|
|
||||||
rdr, err := NewSquashfsReader(io.NewSectionReader(aiFil, ai.Offset, stat.Size()-ai.Offset))
|
rdr, err := NewSquashfsReader(io.NewSectionReader(aiFil, ai.Offset, stat.Size()-ai.Offset))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
errs := rdr.ExtractTo(wd + "/testing/firefox")
|
os.RemoveAll(wd + "/testing/firefox")
|
||||||
if len(errs) > 0 {
|
err = rdr.ExtractTo(wd + "/testing/firefox")
|
||||||
t.Fatal(errs)
|
t.Fatal(err)
|
||||||
}
|
|
||||||
// os.RemoveAll(wd + "/testing/" + appImageName + ".d")
|
|
||||||
// root, _ := rdr.GetRootFolder()
|
|
||||||
// errs := root.ExtractWithOptions(wd+"/testing/"+appImageName+".d", true, os.ModePerm, true)
|
|
||||||
// t.Fatal(errs)
|
|
||||||
fmt.Println(time.Since(start))
|
|
||||||
t.Fatal("No problemo!")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnsquashfs(t *testing.T) {
|
func TestUnsquashfs(t *testing.T) {
|
||||||
@@ -155,14 +146,14 @@ func BenchmarkDragRace(b *testing.B) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
b.Fatal(err)
|
b.Fatal(err)
|
||||||
}
|
}
|
||||||
errs := rdr.ExtractTo(wd + "/testing/firefox")
|
err = rdr.ExtractTo(wd + "/testing/firefox")
|
||||||
if len(errs) > 0 {
|
if err != nil {
|
||||||
b.Fatal(errs)
|
b.Fatal(err)
|
||||||
}
|
}
|
||||||
libTime := time.Since(start)
|
libTime := time.Since(start)
|
||||||
b.Log("Unsqushfs:", unsquashTime.Round(time.Millisecond))
|
b.Log("Unsqushfs:", unsquashTime.Round(time.Millisecond))
|
||||||
b.Log("Library:", libTime.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")
|
b.Log("unsquashfs is", strconv.FormatFloat(float64(libTime.Milliseconds())/float64(unsquashTime.Milliseconds()), 'f', 2, 64)+"x faster")
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadTestAppImage(dir string) error {
|
func downloadTestAppImage(dir string) error {
|
||||||
+70
-12
@@ -33,36 +33,94 @@ type superblock struct {
|
|||||||
ExportTableStart uint64
|
ExportTableStart uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
//SuperblockFlags is the parsed version of Superblock.Flags
|
//SuperblockFlags is the series of flags describing how a squashfs archive is packed.
|
||||||
type superblockFlags struct {
|
type SuperblockFlags struct {
|
||||||
|
//If true, inodes are stored uncompressed.
|
||||||
UncompressedInodes bool
|
UncompressedInodes bool
|
||||||
|
//If true, data is stored uncompressed.
|
||||||
UncompressedData bool
|
UncompressedData bool
|
||||||
Check bool
|
check bool
|
||||||
|
//If true, fragments are stored uncompressed.
|
||||||
UncompressedFragments bool
|
UncompressedFragments bool
|
||||||
|
//If true, ALL data is stored in sequential data blocks instead of utilizing fragments.
|
||||||
NoFragments bool
|
NoFragments bool
|
||||||
AlwaysFragments bool
|
//If true, the last block of data will always be stored as a fragment if it's less then the block size.
|
||||||
Duplicates bool
|
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
|
Exportable bool
|
||||||
|
//If true, the xattr table is uncompressed. (Currently unsupported)
|
||||||
UncompressedXattr bool
|
UncompressedXattr bool
|
||||||
|
//If true, the xattr table is not populated. (Currently unsupported)
|
||||||
NoXattr bool
|
NoXattr bool
|
||||||
CompressorOptions bool
|
compressorOptions bool
|
||||||
|
//If true, the UID/GID table is stored uncompressed.
|
||||||
UncompressedIDs bool
|
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.
|
//GetFlags returns a SuperblockFlags for a given superblock.
|
||||||
func (s *superblock) GetFlags() superblockFlags {
|
func (s *superblock) GetFlags() SuperblockFlags {
|
||||||
return superblockFlags{
|
return SuperblockFlags{
|
||||||
UncompressedInodes: s.Flags&0x1 == 0x1,
|
UncompressedInodes: s.Flags&0x1 == 0x1,
|
||||||
UncompressedData: s.Flags&0x2 == 0x2,
|
UncompressedData: s.Flags&0x2 == 0x2,
|
||||||
Check: s.Flags&0x4 == 0x4,
|
check: s.Flags&0x4 == 0x4,
|
||||||
UncompressedFragments: s.Flags&0x8 == 0x8,
|
UncompressedFragments: s.Flags&0x8 == 0x8,
|
||||||
NoFragments: s.Flags&0x10 == 0x10,
|
NoFragments: s.Flags&0x10 == 0x10,
|
||||||
AlwaysFragments: s.Flags&0x20 == 0x20,
|
AlwaysFragment: s.Flags&0x20 == 0x20,
|
||||||
Duplicates: s.Flags&0x40 == 0x40,
|
RemoveDuplicates: s.Flags&0x40 == 0x40,
|
||||||
Exportable: s.Flags&0x80 == 0x80,
|
Exportable: s.Flags&0x80 == 0x80,
|
||||||
UncompressedXattr: s.Flags&0x100 == 0x100,
|
UncompressedXattr: s.Flags&0x100 == 0x100,
|
||||||
NoXattr: s.Flags&0x200 == 0x200,
|
NoXattr: s.Flags&0x200 == 0x200,
|
||||||
CompressorOptions: s.Flags&0x400 == 0x400,
|
compressorOptions: s.Flags&0x400 == 0x400,
|
||||||
UncompressedIDs: s.Flags&0x800 == 0x800,
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package squashfs
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@@ -13,26 +14,24 @@ import (
|
|||||||
"github.com/CalebQ42/squashfs/internal/compression"
|
"github.com/CalebQ42/squashfs/internal/compression"
|
||||||
)
|
)
|
||||||
|
|
||||||
type fileHolder struct {
|
//Writer is used to creaste squashfs archives. Currently unusable.
|
||||||
reader io.Reader
|
|
||||||
path string
|
|
||||||
name string
|
|
||||||
symLocation string
|
|
||||||
UID int
|
|
||||||
GUID int
|
|
||||||
perm int
|
|
||||||
folder bool
|
|
||||||
symlink bool
|
|
||||||
}
|
|
||||||
|
|
||||||
//Writer is used to creaste squashfs archives. Currently unusable
|
|
||||||
//TODO: Make usable
|
//TODO: Make usable
|
||||||
type Writer struct {
|
type Writer struct {
|
||||||
compressor compression.Compressor
|
compressor compression.Compressor
|
||||||
structure map[string][]*fileHolder
|
structure map[string][]*fileHolder
|
||||||
symlinkTable map[string]string //[oldpath]newpath
|
symlinkTable map[string]string
|
||||||
|
folders []string
|
||||||
uidGUIDTable []int
|
uidGUIDTable []int
|
||||||
|
frags []fragment
|
||||||
|
superblock superblock
|
||||||
compressionType int
|
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
|
allowErrors bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,19 +48,55 @@ func NewWriterWithOptions(compressionType int, allowErrors bool) (*Writer, error
|
|||||||
return nil, errors.New("Incorrect compression type")
|
return nil, errors.New("Incorrect compression type")
|
||||||
}
|
}
|
||||||
if compressionType == 3 {
|
if compressionType == 3 {
|
||||||
return nil, errors.New("LZO compression is not (currently) supported")
|
return nil, errors.New("Lzo compression is not (currently) supported")
|
||||||
}
|
}
|
||||||
return &Writer{
|
writer := &Writer{
|
||||||
structure: map[string][]*fileHolder{
|
structure: map[string][]*fileHolder{
|
||||||
"/": make([]*fileHolder, 0),
|
"/": make([]*fileHolder, 0),
|
||||||
},
|
},
|
||||||
|
folders: []string{
|
||||||
|
"/",
|
||||||
|
},
|
||||||
symlinkTable: make(map[string]string),
|
symlinkTable: make(map[string]string),
|
||||||
compressionType: compressionType,
|
compressionType: compressionType,
|
||||||
allowErrors: allowErrors,
|
allowErrors: allowErrors,
|
||||||
}, nil
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
//AddFile attempts to add an os.File to the archive at it's root.
|
//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 {
|
func (w *Writer) AddFile(file *os.File) error {
|
||||||
return w.AddFileToFolder("/", file)
|
return w.AddFileToFolder("/", file)
|
||||||
}
|
}
|
||||||
@@ -81,8 +116,12 @@ func (w *Writer) AddFileTo(filepath string, file *os.File) error {
|
|||||||
if !strings.HasPrefix(filepath, "/") {
|
if !strings.HasPrefix(filepath, "/") {
|
||||||
filepath = "/" + filepath
|
filepath = "/" + filepath
|
||||||
}
|
}
|
||||||
|
if w.Contains(filepath) {
|
||||||
|
return errors.New("File already exists at " + filepath)
|
||||||
|
}
|
||||||
var holder fileHolder
|
var holder fileHolder
|
||||||
holder.path, holder.name = path.Split(filepath)
|
holder.path = path.Dir(filepath)
|
||||||
|
holder.name = path.Base(filepath)
|
||||||
holder.reader = file
|
holder.reader = file
|
||||||
stat, err := file.Stat()
|
stat, err := file.Stat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -105,6 +144,7 @@ func (w *Writer) AddFileTo(filepath string, file *os.File) error {
|
|||||||
sort.Ints(w.uidGUIDTable)
|
sort.Ints(w.uidGUIDTable)
|
||||||
}
|
}
|
||||||
if holder.symlink {
|
if holder.symlink {
|
||||||
|
holder.reader = file
|
||||||
target, err := os.Readlink(file.Name())
|
target, err := os.Readlink(file.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -135,9 +175,15 @@ func (w *Writer) AddFileTo(filepath string, file *os.File) error {
|
|||||||
dirsAdded = append(dirsAdded, holder.path+"/"+holder.name)
|
dirsAdded = append(dirsAdded, holder.path+"/"+holder.name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if !stat.Mode().IsRegular() {
|
} else if stat.Mode().IsRegular() {
|
||||||
|
holder.reader = file
|
||||||
|
} else {
|
||||||
return errors.New("Unsupported file type " + file.Name())
|
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)
|
w.structure[holder.path] = append(w.structure[holder.path], &holder)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -145,18 +191,49 @@ func (w *Writer) AddFileTo(filepath string, file *os.File) error {
|
|||||||
//AddReaderTo adds the data from the given reader to the archive as a file located at the given filepath.
|
//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.
|
//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.
|
//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) error {
|
func (w *Writer) AddReaderTo(filepath string, reader io.Reader, size uint64) error {
|
||||||
filepath = path.Clean(filepath)
|
filepath = path.Clean(filepath)
|
||||||
if !strings.HasPrefix(filepath, "/") {
|
if !strings.HasPrefix(filepath, "/") {
|
||||||
filepath = "/" + filepath
|
filepath = "/" + filepath
|
||||||
}
|
}
|
||||||
|
if w.Contains(filepath) {
|
||||||
|
return errors.New("File already exists at " + filepath)
|
||||||
|
}
|
||||||
var holder fileHolder
|
var holder fileHolder
|
||||||
holder.path, holder.name = path.Split(filepath)
|
holder.path = path.Dir(filepath)
|
||||||
|
holder.name = path.Base(filepath)
|
||||||
|
holder.size = size
|
||||||
holder.reader = reader
|
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)
|
w.structure[holder.path] = append(w.structure[holder.path], &holder)
|
||||||
return nil
|
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.
|
//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.
|
//Returns true if one or more files are removed.
|
||||||
func (w *Writer) Remove(filepath string) bool {
|
func (w *Writer) Remove(filepath string) bool {
|
||||||
@@ -171,10 +248,11 @@ func (w *Writer) Remove(filepath string) bool {
|
|||||||
for i, fil := range files {
|
for i, fil := range files {
|
||||||
if match, _ = path.Match(name, fil.name); match {
|
if match, _ = path.Match(name, fil.name); match {
|
||||||
matchFound = true
|
matchFound = true
|
||||||
if i != len(files)-1 {
|
if len(w.structure[structDir]) > 1 {
|
||||||
w.structure[structDir] = append(w.structure[structDir][:i], w.structure[structDir][i+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 {
|
} else {
|
||||||
w.structure[structDir] = w.structure[structDir][:i]
|
w.structure[structDir] = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -185,11 +263,120 @@ func (w *Writer) Remove(filepath string) bool {
|
|||||||
|
|
||||||
//FixSymlinks will scan through the squashfs archive and try to find broken symlinks and fix them.
|
//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.
|
//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.
|
||||||
//If this is not run before writing, you may end up with broken symlinks.
|
func (w *Writer) FixSymlinks() (errs []error, problems bool) {
|
||||||
func (w *Writer) FixSymlinks() error {
|
for dir, holderSlice := range w.structure {
|
||||||
//TODO
|
for i, holder := range holderSlice {
|
||||||
return errors.New("DON'T")
|
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.
|
//WriteToFilename creates the squashfs archive with the given filepath.
|
||||||
@@ -201,9 +388,3 @@ func (w *Writer) WriteToFilename(filepath string) error {
|
|||||||
_, err = w.WriteTo(newFil)
|
_, err = w.WriteTo(newFil)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
//WriteTo attempts to write the archive to the given io.Writer.
|
|
||||||
func (w *Writer) WriteTo(write io.Writer) (int64, error) {
|
|
||||||
//TODO
|
|
||||||
return 0, errors.New("I SAID DON'T")
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,69 @@
|
|||||||
|
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])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package squashfs
|
||||||
|
|
||||||
|
func (w *Writer) countInodes() (out uint32) {
|
||||||
|
for _, files := range w.structure {
|
||||||
|
out++
|
||||||
|
out += uint32(len(files))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
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