Working on a better API to interact with squashfs
New API uses a File that can hold more information.
This commit is contained in:
@@ -11,6 +11,10 @@ type decompressor interface {
|
|||||||
Decompress(io.Reader) ([]byte, error)
|
Decompress(io.Reader) ([]byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type compressor interface {
|
||||||
|
Compress(io.Reader) ([]byte, error)
|
||||||
|
}
|
||||||
|
|
||||||
//ZlibDecompressor is a decompressor for gzip type compression
|
//ZlibDecompressor is a decompressor for gzip type compression
|
||||||
type zlibDecompressor struct{}
|
type zlibDecompressor struct{}
|
||||||
|
|
||||||
|
|||||||
+14
-5
@@ -10,9 +10,9 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
//ErrInodeNotFile is given when giving an inode, but the function requires a file inode.
|
//ErrInodeNotFile is given when giving an inode, but the function requires a file inode.
|
||||||
ErrInodeNotFile = errors.New("Given inode is NOT a file type")
|
errInodeNotFile = errors.New("Given inode is NOT a file type")
|
||||||
//ErrInodeOnlyFragment is given when trying to make a DataReader from an inode, but the inode only had data in a fragment
|
//ErrInodeOnlyFragment is given when trying to make a DataReader from an inode, but the inode only had data in a fragment
|
||||||
ErrInodeOnlyFragment = errors.New("Given inode ONLY has fragment data")
|
errInodeOnlyFragment = errors.New("Given inode ONLY has fragment data")
|
||||||
)
|
)
|
||||||
|
|
||||||
//DataReader reads data from data blocks.
|
//DataReader reads data from data blocks.
|
||||||
@@ -66,7 +66,7 @@ func (r *Reader) newDataReaderFromInode(i *inode.Inode) (*dataReader, error) {
|
|||||||
case inode.BasicFileType:
|
case inode.BasicFileType:
|
||||||
fil := i.Info.(inode.BasicFile)
|
fil := i.Info.(inode.BasicFile)
|
||||||
if fil.Init.BlockStart == 0 {
|
if fil.Init.BlockStart == 0 {
|
||||||
return nil, ErrInodeOnlyFragment
|
return nil, errInodeOnlyFragment
|
||||||
}
|
}
|
||||||
rdr.offset = int64(fil.Init.BlockStart)
|
rdr.offset = int64(fil.Init.BlockStart)
|
||||||
for _, sizes := range fil.BlockSizes {
|
for _, sizes := range fil.BlockSizes {
|
||||||
@@ -78,7 +78,7 @@ func (r *Reader) newDataReaderFromInode(i *inode.Inode) (*dataReader, error) {
|
|||||||
case inode.ExtFileType:
|
case inode.ExtFileType:
|
||||||
fil := i.Info.(inode.ExtendedFile)
|
fil := i.Info.(inode.ExtendedFile)
|
||||||
if fil.Init.BlockStart == 0 {
|
if fil.Init.BlockStart == 0 {
|
||||||
return nil, ErrInodeOnlyFragment
|
return nil, errInodeOnlyFragment
|
||||||
}
|
}
|
||||||
rdr.offset = int64(fil.Init.BlockStart)
|
rdr.offset = int64(fil.Init.BlockStart)
|
||||||
for _, sizes := range fil.BlockSizes {
|
for _, sizes := range fil.BlockSizes {
|
||||||
@@ -88,7 +88,7 @@ func (r *Reader) newDataReaderFromInode(i *inode.Inode) (*dataReader, error) {
|
|||||||
rdr.blocks = rdr.blocks[:len(rdr.blocks)-1]
|
rdr.blocks = rdr.blocks[:len(rdr.blocks)-1]
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return nil, ErrInodeNotFile
|
return nil, errInodeNotFile
|
||||||
}
|
}
|
||||||
err := rdr.readCurBlock()
|
err := rdr.readCurBlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -145,7 +145,16 @@ func (d *dataReader) readCurBlock() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Close frees up the curData from memory
|
||||||
|
func (d *dataReader) Close() error {
|
||||||
|
d.curData = nil
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (d *dataReader) Read(p []byte) (int, error) {
|
func (d *dataReader) Read(p []byte) (int, error) {
|
||||||
|
if d.curData == nil {
|
||||||
|
d.readCurBlock()
|
||||||
|
}
|
||||||
if d.curReadOffset+len(p) < len(d.curData) {
|
if d.curReadOffset+len(p) < len(d.curData) {
|
||||||
for i := 0; i < len(p); i++ {
|
for i := 0; i < len(p); i++ {
|
||||||
p[i] = d.curData[d.curReadOffset+i]
|
p[i] = d.curData[d.curReadOffset+i]
|
||||||
|
|||||||
@@ -4,34 +4,98 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
"github.com/CalebQ42/squashfs/internal/directory"
|
||||||
"github.com/CalebQ42/squashfs/internal/inode"
|
"github.com/CalebQ42/squashfs/internal/inode"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
//ErrNotDirectory is returned when you're trying to do directory things with a non-directory
|
//ErrNotDirectory is returned when you're trying to do directory things with a non-directory
|
||||||
ErrNotDirectory = errors.New("File is not a 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")
|
||||||
)
|
)
|
||||||
|
|
||||||
//File is the main way to interact with files within squashfs, or when putting files into a squashfs.
|
//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.
|
//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.
|
//When writing, this holds the information on WHERE the file will be placed inside the archive.
|
||||||
type File struct {
|
type File struct {
|
||||||
Name string //The name of the file or folder.
|
Name string //The name of the file or folder. Root folder will not have a name ("")
|
||||||
Parent *File //The parent directory. If it's the root directory, will be nil
|
Parent *File //The parent directory. If it's the root directory, will be nil
|
||||||
Reader io.Reader //Underlying reader. When writing, will probably be an os.File. When reading will probably be a FileReader
|
Reader io.Reader //Underlying reader. When writing, will probably be an os.File. When reading this is kept nil UNTIL reading to save memory.
|
||||||
path string //When writing, you can set where a file goes in the archive with this. (not yet tho)
|
Path string //The folder the File is located in.
|
||||||
size int //The size of the file. -1 if a directory
|
|
||||||
r *Reader //The squashfs.Reader where this file is contained.
|
r *Reader //The squashfs.Reader where this file is contained.
|
||||||
in *inode.Inode //Underlyting inode when reading.
|
in *inode.Inode //Underlyting inode when reading.
|
||||||
filType int //The file's type, using inode types.
|
filType int //The file's type, using inode types.
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *File) GetChildren() ([]*File, error) {
|
//get a File from a directory.entry
|
||||||
|
func (r *Reader) newFileFromDirEntry(entry *directory.Entry) (fil *File, err error) {
|
||||||
|
fil.in, err = r.getInodeFromEntry(entry)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
fil.Name = entry.Name
|
||||||
|
fil.r = r
|
||||||
|
fil.filType = fil.in.Type
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetChildren returns a *squashfs.File slice of every direct child of the directory. If the File is not a directory, will return ErrNotDirectory
|
||||||
|
func (f *File) GetChildren() (children []*File, err error) {
|
||||||
|
if f.r == nil {
|
||||||
|
return nil, ErrNotReading
|
||||||
|
}
|
||||||
if !f.IsDir() {
|
if !f.IsDir() {
|
||||||
return nil, ErrNotDirectory
|
return nil, ErrNotDirectory
|
||||||
}
|
}
|
||||||
//TODO
|
dir, err := f.r.readDirFromInode(f.in)
|
||||||
return nil, nil
|
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
|
||||||
|
fil.Path = f.Path + "/" + f.Name
|
||||||
|
children = append(children, fil)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetChildrenRecursively returns ALL children. Goes down ALL folder paths.
|
||||||
|
func (f *File) GetChildrenRecursively() (children []*File, err error) {
|
||||||
|
if f.r == nil {
|
||||||
|
return nil, ErrNotReading
|
||||||
|
}
|
||||||
|
if !f.IsDir() {
|
||||||
|
return nil, ErrNotDirectory
|
||||||
|
}
|
||||||
|
chil, err := f.GetChildren()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var childFolders []*File
|
||||||
|
for _, child := range chil {
|
||||||
|
children = append(children, child)
|
||||||
|
if child.IsDir() {
|
||||||
|
childFolders = append(childFolders, child)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, folds := range childFolders {
|
||||||
|
var childs []*File
|
||||||
|
childs, err = folds.GetChildrenRecursively()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
children = append(children, childs...)
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//IsDir returns if the file is a directory.
|
//IsDir returns if the file is a directory.
|
||||||
@@ -39,17 +103,30 @@ func (f *File) IsDir() bool {
|
|||||||
return f.filType == inode.BasicDirectoryType || f.filType == inode.ExtDirType
|
return f.filType == inode.BasicDirectoryType || f.filType == inode.ExtDirType
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//Close frees up the memory held up by the underlying reader. Should NOT be called when writing.
|
||||||
func (f *File) Close() {
|
//When reading, Close is safe to use, as any subsequent Read calls reinitialize the reader.
|
||||||
//nil the reader to free up resources (in theory). Might switch reader to be a readcloser to make it easier.
|
func (f *File) Close() error {
|
||||||
|
if f.IsDir() {
|
||||||
|
return ErrNotFile
|
||||||
|
}
|
||||||
|
if closer, is := f.Reader.(io.Closer); is {
|
||||||
|
closer.Close()
|
||||||
|
}
|
||||||
f.Reader = nil
|
f.Reader = nil
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//Read from the file. Doesn't do anything fancy, just pases it to the underlying io.Reader. If a directory, return io.EOF
|
//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) {
|
func (f *File) Read(p []byte) (int, error) {
|
||||||
if f.IsDir() {
|
if f.IsDir() {
|
||||||
return 0, io.EOF
|
return 0, io.EOF
|
||||||
}
|
}
|
||||||
//Check if reader is nill and create a new one if needed.
|
var err error
|
||||||
|
if f.Reader == nil && f.r != nil {
|
||||||
|
f.Reader, err = f.r.newFileReader(f.in)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
}
|
||||||
return f.Reader.Read(p)
|
return f.Reader.Read(p)
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-9
@@ -9,9 +9,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
//FileReader provides a io.Reader interface for files within a squashfs archive
|
//FileReader provides a io.Reader interface for files within a squashfs archive
|
||||||
type FileReader struct {
|
type fileReader struct {
|
||||||
r *Reader
|
r *Reader
|
||||||
data *dataReader
|
data *dataReader
|
||||||
|
in *inode.Inode
|
||||||
fragmentData []byte
|
fragmentData []byte
|
||||||
fragged bool
|
fragged bool
|
||||||
fragOnly bool
|
fragOnly bool
|
||||||
@@ -25,13 +26,9 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
//ReadFile provides a squashfs.FileReader for the file at the given location.
|
//ReadFile provides a squashfs.FileReader for the file at the given location.
|
||||||
func (r *Reader) ReadFile(location string) (*FileReader, error) {
|
func (r *Reader) newFileReader(in *inode.Inode) (*fileReader, error) {
|
||||||
var rdr FileReader
|
var rdr fileReader
|
||||||
rdr.r = r
|
rdr.in = in
|
||||||
in, err := r.getInodeFromPath(location)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if in.Type != inode.BasicFileType && in.Type != inode.ExtFileType {
|
if in.Type != inode.BasicFileType && in.Type != inode.ExtFileType {
|
||||||
return nil, ErrPathIsNotFile
|
return nil, ErrPathIsNotFile
|
||||||
}
|
}
|
||||||
@@ -47,6 +44,7 @@ func (r *Reader) ReadFile(location string) (*FileReader, error) {
|
|||||||
rdr.fragOnly = fil.Init.BlockStart == 0
|
rdr.fragOnly = fil.Init.BlockStart == 0
|
||||||
rdr.FileSize = int(fil.Init.Size)
|
rdr.FileSize = int(fil.Init.Size)
|
||||||
}
|
}
|
||||||
|
var err error
|
||||||
if rdr.fragged {
|
if rdr.fragged {
|
||||||
rdr.fragmentData, err = r.getFragmentDataFromInode(in)
|
rdr.fragmentData, err = r.getFragmentDataFromInode(in)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -59,7 +57,14 @@ func (r *Reader) ReadFile(location string) (*FileReader, error) {
|
|||||||
return &rdr, nil
|
return &rdr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FileReader) Read(p []byte) (int, error) {
|
//Close runs Close on the data reader and frees the fragmentdata
|
||||||
|
func (f *fileReader) Close() error {
|
||||||
|
f.data.Close()
|
||||||
|
f.fragmentData = nil
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fileReader) Read(p []byte) (int, error) {
|
||||||
if f.fragOnly {
|
if f.fragOnly {
|
||||||
n, err := bytes.NewBuffer(f.fragmentData[f.read:]).Read(p)
|
n, err := bytes.NewBuffer(f.fragmentData[f.read:]).Read(p)
|
||||||
f.read += n
|
f.read += n
|
||||||
@@ -72,6 +77,9 @@ func (f *FileReader) Read(p []byte) (int, error) {
|
|||||||
n, err := f.data.Read(p)
|
n, err := f.data.Read(p)
|
||||||
read += n
|
read += n
|
||||||
if f.fragged && err == io.EOF {
|
if f.fragged && err == io.EOF {
|
||||||
|
if f.fragmentData == nil {
|
||||||
|
f.fragmentData, err = f.r.getFragmentDataFromInode(f.in)
|
||||||
|
}
|
||||||
n, err = bytes.NewBuffer(f.fragmentData).Read(p[read:])
|
n, err = bytes.NewBuffer(f.fragmentData).Read(p[read:])
|
||||||
read += n
|
read += n
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ var (
|
|||||||
ErrCompressorOptions = errors.New("Compressor options is not currently supported")
|
ErrCompressorOptions = errors.New("Compressor options is not currently supported")
|
||||||
//ErrFragmentTableIssues is returned if there's trouble reading the fragment table when creating a reader.
|
//ErrFragmentTableIssues is returned if there's trouble reading the fragment table when creating a reader.
|
||||||
//When this is returned, the reader is still returned.
|
//When this is returned, the reader is still returned.
|
||||||
ErrFragmentTableIssues = errors.New("Trouble while reading the fragment table")
|
errFragmentTableIssues = errors.New("Trouble while reading the fragment table")
|
||||||
)
|
)
|
||||||
|
|
||||||
//Reader processes and reads a squashfs archive.
|
//Reader processes and reads a squashfs archive.
|
||||||
@@ -73,50 +73,35 @@ func NewSquashfsReader(r io.ReaderAt) (*Reader, error) {
|
|||||||
return &rdr, nil
|
return &rdr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//GetFilesList returns a list of ALL files in the squashfs, going down every folder.
|
//GetRootFolder returns a squashfs.File that references the root directory of the squashfs archive.
|
||||||
//Folders end in /
|
func (r *Reader) GetRootFolder() (root *File, err error) {
|
||||||
func (r *Reader) GetFilesList() ([]string, error) {
|
mr, err := r.newMetadataReaderFromInodeRef(r.super.RootInodeRef)
|
||||||
inoderdr, err := r.newMetadataReaderFromInodeRef(r.super.RootInodeRef)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
i, err := inode.ProcessInode(inoderdr, r.super.BlockSize)
|
root.in, err = inode.ProcessInode(mr, r.super.BlockSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
paths, err := r.readDir(i)
|
root.Path = "/"
|
||||||
if err != nil {
|
root.filType = root.in.Type
|
||||||
return nil, err
|
return root, nil
|
||||||
}
|
|
||||||
return paths, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//readDir returns a list of all decendents of a given inode. Inode given MUST be a directory type.
|
//GetAllFiles returns a slice of ALL files and folders contained in the squashfs.
|
||||||
func (r *Reader) readDir(i *inode.Inode) (paths []string, err error) {
|
func (r *Reader) GetAllFiles() (fils []*File, err error) {
|
||||||
dir, err := r.readDirFromInode(i)
|
root, err := r.GetRootFolder()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, entry := range dir.Entries {
|
return root.GetChildrenRecursively()
|
||||||
if entry.Init.Type == inode.BasicDirectoryType {
|
}
|
||||||
paths = append(paths)
|
|
||||||
i, err = r.getInodeFromEntry(&entry)
|
//FindFile returns the first file (in the same order as Reader.GetAllFiles) that the given function returns true for
|
||||||
if err != nil {
|
func (r *Reader) FindFile(query func(*File) bool) *File {
|
||||||
return
|
root, err := r.GetRootFolder()
|
||||||
}
|
if err != nil {
|
||||||
var subPaths []string
|
return nil
|
||||||
subPaths, err = r.readDir(i)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for pathI := range subPaths {
|
|
||||||
subPaths[pathI] = entry.Name + "/" + subPaths[pathI]
|
|
||||||
}
|
|
||||||
paths = append(paths, entry.Name+"/")
|
|
||||||
paths = append(paths, subPaths...)
|
|
||||||
} else {
|
|
||||||
paths = append(paths, entry.Name)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-3
@@ -43,9 +43,11 @@ func TestMain(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
ext, err := rdr.ReadFile(extractionFil)
|
ext := rdr.FindFile(func(fil *File) bool {
|
||||||
if err != nil {
|
return fil.Name == extractionFil
|
||||||
t.Fatal(err)
|
})
|
||||||
|
if ext == nil {
|
||||||
|
t.Fatal("Cannot find file")
|
||||||
}
|
}
|
||||||
_, err = io.Copy(desk, ext)
|
_, err = io.Copy(desk, ext)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user