Documentation
¶
Index ¶
- Constants
- Variables
- func RegisterDecompressor(method Compression, dcomp Decompressor)
- type Compression
- type Decompressor
- type DirIndexEntry
- type File
- type FileDir
- type Flags
- type Inode
- func (i *Inode) AddRef(count uint64) uint64
- func (i *Inode) DelRef(count uint64) uint64
- func (i *Inode) GetGid() uint32
- func (i *Inode) GetUid() uint32
- func (i *Inode) IsDir() bool
- func (i *Inode) Mode() fs.FileMode
- func (ino *Inode) OpenFile(name string) fs.File
- func (i *Inode) ReadAt(p []byte, off int64) (int, error)
- func (i *Inode) Readlink() ([]byte, error)
- type Option
- type Superblock
- func (sb *Superblock) Close() error
- func (s *Superblock) FindInode(name string, followSymlinks bool) (*Inode, error)
- func (s *Superblock) FindInodeUnder(cur *Inode, name string, followSymlinks bool) (*Inode, error)
- func (sb *Superblock) GetInode(ino uint64) (*Inode, error)
- func (sb *Superblock) GetInodeRef(inor inodeRef) (*Inode, error)
- func (sb *Superblock) Lstat(name string) (fs.FileInfo, error)
- func (sb *Superblock) Open(name string) (fs.File, error)
- func (sb *Superblock) ReadDir(name string) ([]fs.DirEntry, error)
- func (sb *Superblock) Readlink(name string) (string, error)
- func (s *Superblock) SetInodeOffset(offt uint64)
- func (sb *Superblock) Stat(name string) (fs.FileInfo, error)
- func (s *Superblock) UnmarshalBinary(data []byte) error
- type Type
Constants ¶
const SuperblockSize = 96
Variables ¶
var ( // ErrInvalidFile is returned when the file format is not recognized as SquashFS ErrInvalidFile = errors.New("invalid file, squashfs signature not found") // ErrInvalidSuper is returned when the superblock data is corrupted or invalid ErrInvalidSuper = errors.New("invalid squashfs superblock") // ErrInvalidVersion is returned when the SquashFS version is not 4.0 // This library only supports SquashFS 4.0 format ErrInvalidVersion = errors.New("invalid file version, expected squashfs 4.0") // ErrInodeNotExported is returned when trying to access an inode that isn't in the export table ErrInodeNotExported = errors.New("unknown squashfs inode and no NFS export table") // ErrNotDirectory is returned when attempting to perform directory operations on a non-directory ErrNotDirectory = errors.New("not a directory") // ErrTooManySymlinks is returned when symlink resolution exceeds the maximum depth // This prevents infinite loops in symlink resolution ErrTooManySymlinks = errors.New("too many levels of symbolic links") )
Package-specific error variables that can be used with errors.Is() for error handling.
Functions ¶
func RegisterDecompressor ¶
func RegisterDecompressor(method Compression, dcomp Decompressor)
RegisterDecompressor can be used to register a decompressor for squashfs. By default GZip is supported. The method shall take a buffer and return a decompressed buffer.
Types ¶
type Compression ¶ added in v0.1.4
type Compression uint16
Compression represents the compression algorithm used in a SquashFS filesystem. Different compression methods can be used to optimize for size or decompression speed. By default, only GZip compression is supported. Additional compression formats can be enabled through build tags or manually registering decompressors.
const ( GZip Compression = iota + 1 // GZip compression (zlib, always supported) LZMA // LZMA compression (currently not implemented via build tag) LZO // LZO compression (currently not implemented via build tag) XZ // XZ compression (enabled with "xz" build tag) LZ4 // LZ4 compression (currently not implemented via build tag) ZSTD // Zstandard compression (enabled with "zstd" build tag) )
func (Compression) String ¶ added in v0.1.4
func (s Compression) String() string
type Decompressor ¶
func MakeDecompressor ¶
func MakeDecompressor(dec func(r io.Reader) io.ReadCloser) Decompressor
MakeDecompressor allows using a decompressor made for archive/zip with SquashFs. It has some overhead as instead of simply dealing with buffer this uses the reader/writer API, but should allow to easily handle some formats.
Example use: * squashfs.RegisterDecompressor(squashfs.ZSTD, squashfs.MakeDecompressor(zstd.ZipDecompressor())) * squashfs.RegisterDecompressor(squashfs.LZ4, squashfs.MakeDecompressor(lz4.NewReader)))
func MakeDecompressorErr ¶ added in v0.1.4
func MakeDecompressorErr(dec func(r io.Reader) (io.ReadCloser, error)) Decompressor
MakeDecompressorErr is similar to MakeDecompressor but the factory method also returns an error.
Example use: * squashfs.RegisterDecompressor(squashfs.LZMA, squashfs.MakeDecompressorErr(lzma.NewReader)) * squashfs.RegisterDecompressor(squashfs.XZ, squashfs.MakeDecompressorErr(xz.NewReader))
type DirIndexEntry ¶ added in v1.1.0
type DirIndexEntry struct { Index uint32 // position in the directory data Start uint32 // block offset Name string // filename at this index point }
DirIndexEntry represents an entry in a directory index. Directory indices allow fast lookups in large directories by providing a sorted index of filenames and their positions in the directory data.
type File ¶
type File struct { *io.SectionReader // contains filtered or unexported fields }
File is a convience object allowing using an inode as if it was a regular file
type FileDir ¶
type FileDir struct {
// contains filtered or unexported fields
}
FileDir is a convenience object allowing using a dir inode as if it was a regular file
type Inode ¶
type Inode struct { Type Type // file type (regular file, directory, symlink, etc.) Perm uint16 // permission bits UidIdx uint16 // user ID index (in the ID table) GidIdx uint16 // group ID index (in the ID table) ModTime int32 // modification time (Unix timestamp) Ino uint32 // inode number (unique identifier) StartBlock uint64 // start of data blocks NLink uint32 // number of hard links Size uint64 // file size in bytes (interpretation varies by type) Offset uint32 // offset within block (uint16 for directories) ParentIno uint32 // parent directory's inode number (for directories) SymTarget []byte // target path for symlinks IdxCount uint16 // count of directory index entries (for extended directories) XattrIdx uint32 // extended attribute index if present Sparse uint64 // sparse file information // fragment information for file data that doesn't fill a complete block FragBlock uint32 // fragment block index FragOfft uint32 // offset within fragment block // data block information Blocks []uint32 // block sizes, possibly with compression flags BlocksOfft []uint64 // offsets for each block from StartBlock DirIndex []*DirIndexEntry // directory index entries for fast lookups // contains filtered or unexported fields }
Inode represents a file, directory, or other filesystem object in a SquashFS filesystem. It contains all the metadata and references to data blocks that make up the file's contents. Inodes implement various interfaces like io.ReaderAt to provide file-like access to their contents.
func (*Inode) AddRef ¶
AddRef atomatically increments the inode's refcount and returns the new value. This is mainly useful when using fuse and can be safely ignored.
func (*Inode) DelRef ¶
DelRef atomatically decrements the inode's refcount and returns the new value. This is mainly useful when using fuse and can be safely ignored.
func (*Inode) GetUid ¶ added in v0.1.5
GetUid returns inode's owner uid, or zero if an error happens
type Option ¶ added in v0.1.5
type Option func(sb *Superblock) error
func InodeOffset ¶ added in v0.1.5
type Superblock ¶
type Superblock struct { Magic uint32 // magic identifier InodeCnt uint32 // number of inodes in filesystem ModTime int32 // creation unix time as int32 (will stop working in 2038) BlockSize uint32 // size of a single data block, must match 1<<BlockLog FragCount uint32 Comp Compression // Compression used, usually GZip BlockLog uint16 Flags Flags // squashfs flags IdCount uint16 VMajor uint16 VMinor uint16 RootInode inodeRef // inode number/reference of root BytesUsed uint64 IdTableStart uint64 XattrIdTableStart uint64 InodeTableStart uint64 DirTableStart uint64 FragTableStart uint64 ExportTableStart uint64 // contains filtered or unexported fields }
Superblock is the main object representing a squashfs image, and exposes various information about the file system. It implements Go's standard fs.FS, fs.ReadDirFS, and fs.StatFS interfaces, allowing it to be used as a drop-in replacement for any code that works with the standard filesystem interfaces.
The Superblock provides access to all the internal data structures of the SquashFS image, including inodes, compression information, and directory structures. For most use cases, you can use the standard fs methods (Open, ReadDir, etc.) without needing to understand the underlying implementation.
For advanced use cases, the Superblock also provides direct access to internal structures like inodes and directory entries.
func New ¶
func New(fs io.ReaderAt, options ...Option) (*Superblock, error)
New returns a new instance of superblock for a given io.ReaderAt that can be used to access files inside squashfs.
func Open ¶ added in v0.1.5
func Open(file string, options ...Option) (*Superblock, error)
Open returns a new instance of superblock for a given file that can be used to access files inside squashfs. The file will be closed by the garbage collector or when Close() is called on the superblock.
func (*Superblock) Close ¶ added in v0.1.5
func (sb *Superblock) Close() error
Close will close the underlying file when a filesystem was open with Open()
func (*Superblock) FindInode ¶ added in v0.1.5
func (s *Superblock) FindInode(name string, followSymlinks bool) (*Inode, error)
FindInode returns the inode for a given path. If followSymlink is false and a symlink is found in the path, it will be followed anyway. If however the target file is a symlink, then its inode will be returned.
func (*Superblock) FindInodeUnder ¶ added in v1.0.0
FindInodeUnder returns an inode for a path starting at a given different inode
Note that it is not possible to access directories outside the given path, including using symlinks, as this effectively acts as a chroot. This can be useful to implement fs.Sub
func (*Superblock) GetInodeRef ¶
func (sb *Superblock) GetInodeRef(inor inodeRef) (*Inode, error)
func (*Superblock) Lstat ¶ added in v0.1.5
func (sb *Superblock) Lstat(name string) (fs.FileInfo, error)
Lstat will return stats for a given path inside the sqhashfs archive. If the target is a symbolic link, data on the link itself will be returned.
func (*Superblock) Open ¶
func (sb *Superblock) Open(name string) (fs.File, error)
Open returns a fs.File for a given path, which can be a different object depending if the file is a regular file or a directory.
func (*Superblock) ReadDir ¶
func (sb *Superblock) ReadDir(name string) ([]fs.DirEntry, error)
ReadDir implements fs.ReadDirFS and allows listing any directory inside the archive
func (*Superblock) Readlink ¶
func (sb *Superblock) Readlink(name string) (string, error)
Readlink allows reading the value of a symbolic link inside the archive.
func (*Superblock) SetInodeOffset ¶
func (s *Superblock) SetInodeOffset(offt uint64)
SetInodeOffset allows setting the inode offset used for interacting with fuse. This can be safely ignored if not using fuse or when mounting only a single squashfs via fuse.
func (*Superblock) Stat ¶
func (sb *Superblock) Stat(name string) (fs.FileInfo, error)
Stat will return stats for a given path inside the squashfs archive
func (*Superblock) UnmarshalBinary ¶
func (s *Superblock) UnmarshalBinary(data []byte) error
UnmarshalBinary reads a binary header values into Superblock
type Type ¶ added in v0.1.5
type Type uint16
Type represents the type of a file or directory in a SquashFS filesystem. SquashFS has both basic and extended types, where extended types provide additional information or capabilities.
const ( DirType Type = iota + 1 // Basic directory FileType // Basic regular file SymlinkType // Symbolic link BlockDevType // Block device CharDevType // Character device FifoType // Named pipe (FIFO) SocketType // UNIX domain socket XDirType // Extended directory with optional index information XFileType // Extended file with additional metadata XSymlinkType // Extended symbolic link XBlockDevType // Extended block device XCharDevType // Extended character device XFifoType // Extended named pipe XSocketType // Extended UNIX domain socket )