squashfs

package module
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2021 License: MIT Imports: 18 Imported by: 4

README

squashfs (WIP)

PkgGoDev Go Report Card

A PURE Go library to read and write squashfs.

Currently has support for reading squashfs files and extracting files and folders. Supports all compression types except LZO, but additional compression options are hit or miss.

The only major thing missing from squashfs reading is Xattr parsing.

Special thanks to https://dr-emann.github.io/squashfs/ for some VERY important information in an easy to understand format. Thanks also to distri's squashfs library as I referenced it to figure some things out (and double check others).

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 library and about half a second with unsquashfs)

TODO

Documentation

Index

Constants

View Source
const (
	GzipCompression = 1 + iota
	LzmaCompression
	LzoCompression
	XzCompression
	Lz4Compression
	ZstdCompression
)

The types of compression supported by squashfs.

Variables

View Source
var DefaultFlags = SuperblockFlags{
	RemoveDuplicates: true,
	Exportable:       true,
}

DefaultFlags are the default SuperblockFlags that are used.

View Source
var (

	//ErrOptions is returned when compression options that I haven't tested is set. When this is returned, the Reader is also returned.
	ErrOptions = errors.New("Possibly incompatible compressor options")
)

Functions

This section is empty.

Types

type DirEntry added in v0.4.0

type DirEntry struct {
	// contains filtered or unexported fields
}

DirEntry is a child of a directory.

func (DirEntry) File added in v0.4.0

func (d DirEntry) File() (file *File, err error)

File creates a File from the DirEntry.

func (DirEntry) Info added in v0.4.0

func (d DirEntry) Info() (fs.FileInfo, error)

Info returns the fs.FileInfo for the given DirEntry.

func (DirEntry) IsDir added in v0.4.0

func (d DirEntry) IsDir() bool

IsDir Yep.

func (DirEntry) Name added in v0.4.0

func (d DirEntry) Name() string

Name returns the DirEntry's name

func (DirEntry) Type added in v0.4.0

func (d DirEntry) Type() fs.FileMode

Type returns the type bits of fs.FileMode of the DirEntry.

type ExtractionOptions added in v0.4.0

type ExtractionOptions struct {
	DereferenceSymlink bool        //Replace symlinks with the target file
	UnbreakSymlink     bool        //Try to make sure symlinks remain unbroken when extracted, without changing the symlink
	Verbose            bool        //Prints extra info to log on an error
	FolderPerm         fs.FileMode //The permissions used when creating the extraction folder
	// contains filtered or unexported fields
}

ExtractionOptions are available options on how to extract.

func DefaultOptions added in v0.4.0

func DefaultOptions() ExtractionOptions

DefaultOptions is the default ExtractionOptions.

type FS added in v0.4.0

type FS struct {
	// contains filtered or unexported fields
}

FS is a fs.FS representation of a squashfs directory. Implements fs.GlobFS, fs.ReadDirFS, fs.ReadFileFS, fs.StatFS, and fs.SubFS

func (f FS) ExtractSymlink(folder string) error

ExtractSymlink extracts the File to the folder with the DereferenceSymlink option. It extracts the directory's contents to the folder.

func (FS) ExtractTo added in v0.4.0

func (f FS) ExtractTo(folder string) error

ExtractTo extracts the File to the given folder with the default options. It extracts the directory's contents to the folder.

func (FS) ExtractWithOptions added in v0.4.0

func (f FS) ExtractWithOptions(folder string, op ExtractionOptions) error

ExtractWithOptions extracts the File to the given folder with the given ExtrationOptions. It extracts the directory's contents to the folder.

func (FS) Glob added in v0.4.0

func (f FS) Glob(pattern string) (out []string, err error)

Glob returns the name of the files at the given pattern. All paths are relative to the FS.

func (FS) Open added in v0.4.0

func (f FS) Open(name string) (fs.File, error)

Open opens the file at name. Returns a squashfs.File.

func (FS) ReadDir added in v0.4.0

func (f FS) ReadDir(name string) ([]fs.DirEntry, error)

ReadDir returns all the DirEntry returns all DirEntry's for the directory at name. If name is not a directory, returns an error.

func (FS) ReadFile added in v0.4.0

func (f FS) ReadFile(name string) ([]byte, error)

ReadFile returns the data (in []byte) for the file at name.

func (FS) Stat added in v0.4.0

func (f FS) Stat(name string) (fs.FileInfo, error)

Stat returns the fs.FileInfo for the file at name.

func (FS) Sub added in v0.4.0

func (f FS) Sub(dir string) (fs.FS, error)

Sub returns the FS at dir

type File added in v0.2.0

type File struct {
	// contains filtered or unexported fields
}

File represents a file inside a squashfs archive.

func (*File) Close added in v0.2.0

func (f *File) Close() error

Close simply nils the underlying reader. Here mostly to satisfy fs.File

func (f File) ExtractSymlink(folder string) error

ExtractSymlink extracts the File to the folder with the DereferenceSymlink option. If the File is a directory, it instead extracts the directory's contents to the folder.

func (File) ExtractTo added in v0.3.0

func (f File) ExtractTo(folder string) error

ExtractTo extracts the File to the given folder with the default options. If the File is a directory, it instead extracts the directory's contents to the folder.

func (File) ExtractWithOptions added in v0.3.0

func (f File) ExtractWithOptions(folder string, op ExtractionOptions) error

ExtractWithOptions extracts the File to the given folder with the given ExtrationOptions. If the File is a directory, it instead extracts the directory's contents to the folder.

func (File) FS added in v0.4.0

func (f File) FS() (*FS, error)

FS returns the File as a FS.

func (File) GetSymlinkFile added in v0.2.1

func (f File) GetSymlinkFile() *File

GetSymlinkFile returns the File the symlink is pointing to. If not a symlink, or the target is unobtainable (such as it being outside the archive or it's absolute) returns nil

func (File) IsDir added in v0.2.0

func (f File) IsDir() bool

IsDir Yep.

func (File) IsRegular added in v0.4.0

func (f File) IsRegular() bool

IsRegular yep.

func (f File) IsSymlink() bool

IsSymlink yep.

func (File) Read added in v0.2.0

func (f File) Read(p []byte) (int, error)

Read reads the data from the file. Only works if file is a normal file.

func (File) ReadDir added in v0.4.0

func (f File) ReadDir(n int) ([]fs.DirEntry, error)

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 (File) Stat added in v0.4.0

func (f File) Stat() (fs.FileInfo, error)

Stat returns the File's fs.FileInfo

func (File) SymlinkPath added in v0.2.1

func (f File) SymlinkPath() string

SymlinkPath returns the symlink's target path. Is the File isn't a symlink, returns an empty string.

func (File) WriteTo added in v0.4.0

func (f File) WriteTo(w io.Writer) (int64, error)

WriteTo writes all data from the file to the writer. This is multi-threaded.

type FileInfo added in v0.4.0

type FileInfo struct {
	// contains filtered or unexported fields
}

FileInfo is a fs.FileInfo for a file.

func (FileInfo) File added in v0.4.0

func (f FileInfo) File() (file *File, err error)

File creates a File from the FileInfo. *File satisfies fs.File and fs.ReadDirFile.

func (FileInfo) IsDir added in v0.4.0

func (f FileInfo) IsDir() bool

IsDir yep.

func (FileInfo) ModTime added in v0.4.0

func (f FileInfo) ModTime() time.Time

ModTime is the last time the file was modified.

func (FileInfo) Mode added in v0.4.0

func (f FileInfo) Mode() fs.FileMode

Mode returns the fs.FileMode bits of the file.

func (FileInfo) Name added in v0.4.0

func (f FileInfo) Name() string

Name is the file's name.

func (FileInfo) Size added in v0.4.0

func (f FileInfo) Size() int64

Size is the file's size if it's a regular file. Otherwise, returns 0.

func (FileInfo) Sys added in v0.4.0

func (f FileInfo) Sys() interface{}

Sys returns the File for the FileInfo. If something goes wrong, nil is returned.

type Reader

type Reader struct {
	FS
	// contains filtered or unexported fields
}

Reader processes and reads a squashfs archive.

func NewSquashfsReader

func NewSquashfsReader(r io.ReaderAt) (*Reader, error)

NewSquashfsReader returns a new squashfs.Reader from an io.ReaderAt

func (*Reader) ModTime added in v0.3.4

func (r *Reader) ModTime() time.Time

ModTime is the last time the file was modified/created.

type SuperblockFlags added in v0.4.0

type SuperblockFlags struct {
	//If true, inodes are stored uncompressed.
	UncompressedInodes bool
	//If true, data is stored uncompressed.
	UncompressedData bool

	//If true, fragments are stored uncompressed.
	UncompressedFragments bool
	//If true, ALL data is stored in sequential data blocks instead of utilizing fragments.
	NoFragments bool
	//If true, the last block of data will always be stored as a fragment if it's less then the block size.
	AlwaysFragment bool
	//If true, duplicate files are only stored once. (Currently unsupported)
	RemoveDuplicates bool
	//If true, the export table is populated. (Currently unsupported)
	Exportable bool
	//If true, the xattr table is uncompressed. (Currently unsupported)
	UncompressedXattr bool
	//If true, the xattr table is not populated. (Currently unsupported)
	NoXattr bool

	//If true, the UID/GID table is stored uncompressed.
	UncompressedIDs bool
	// contains filtered or unexported fields
}

SuperblockFlags is the series of flags describing how a squashfs archive is packed.

func (*SuperblockFlags) ToUint added in v0.4.0

func (s *SuperblockFlags) ToUint() uint16

ToUint returns the uint16 representation of the given SuperblockFlags

type Writer added in v0.3.7

type Writer struct {

	//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
	// contains filtered or unexported fields
}

Writer is used to creaste squashfs archives. Currently unusable. TODO: Make usable

func NewWriter added in v0.3.7

func NewWriter() (*Writer, error)

NewWriter creates a new with the default options (Gzip compression and allow errors)

func NewWriterWithOptions added in v0.3.7

func NewWriterWithOptions(compressionType int, allowErrors bool) (*Writer, error)

NewWriterWithOptions creates a new squashfs.Writer with the given options. compressionType can be of any types, except LZO (which this library doesn't have support for yet) allowErrors determines if, when adding folders, it allows errors encountered with it's sub-directories and instead logs the errors.

func (*Writer) AddFile added in v0.3.8

func (w *Writer) AddFile(file *os.File) error

AddFile attempts to add an os.File to the archive's root directory.

func (*Writer) AddFileTo added in v0.3.8

func (w *Writer) AddFileTo(filepath string, file *os.File) error

AddFileTo adds the given file to the squashfs archive at the given filepath.

func (*Writer) AddFileToFolder added in v0.3.8

func (w *Writer) AddFileToFolder(folder string, file *os.File) error

AddFileToFolder adds the given file to the squashfs archive, placing it inside the given folder.

func (*Writer) AddFolderTo added in v0.4.0

func (w *Writer) AddFolderTo(folderpath string, permission fs.FileMode) error

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 (*Writer) AddReaderTo added in v0.3.8

func (w *Writer) AddReaderTo(filepath string, reader io.Reader, size uint64) error

AddReaderTo adds the data from the given reader to the archive as a file located at the given filepath. Data from the reader is not read until the squashfs archive is writen. If the given reader implements io.Closer, it will be closed after it is fully read.

func (*Writer) Contains added in v0.3.10

func (w *Writer) Contains(filepath string) bool

Contains returns whether a file is present at the given filepath

func (w *Writer) FixSymlinks() (errs []error, problems bool)

FixSymlinks will scan through the squashfs archive and try to find broken symlinks and fix them. This done by replacing the symlink with the target file and then pointing other symlinks to that file. If all symlinks can be resolved, the error slice will be nil, and the bool false, otherwise all errors occured will be in the slice.

func (*Writer) Remove added in v0.3.8

func (w *Writer) Remove(filepath string) bool

Remove tries to remove the file(s) at the given filepath. If wildcards are used, it will remove all files that match. Returns true if one or more files are removed.

func (*Writer) WriteTo added in v0.3.8

func (w *Writer) WriteTo(write io.Writer) (int64, error)

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 (*Writer) WriteToFilename added in v0.3.8

func (w *Writer) WriteToFilename(filepath string) error

WriteToFilename creates the squashfs archive with the given filepath.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL