- OCaml 86%
- C 13%
- Shell 1%
squashfs #
SquashFS images read and written in pure OCaml.
SquashFS is the compressed read-only filesystem Linux uses for Snap packages
(Canonical's application packages, each a SquashFS image mounted read-only
while the application runs), live CD and USB distributions and most embedded
root filesystems. (An initramfs, the small filesystem the kernel unpacks into
memory at boot to find the real root, is a cpio archive rather than
SquashFS.) Reading or building one usually means shelling out to
unsquashfs and mksquashfs from squashfs-tools. This library reads and
writes SquashFS 4.0 images directly, so a build tool or a unikernel can
produce or inspect an image without those binaries.
The reader opens an image from a string or a bytesrw reader, walks
directories, reads files, symlinks, device numbers and extended attributes,
and decompresses gzip, xz, lz4 and zstd blocks; it refuses lzma and lzo
images. It treats the image as untrusted: every read is bounds-checked, block
sizes above 1 MiB are refused, read_file is capped (100 MB by default) and
safe_read_link refuses a symlink target that leaves the image. 1 MiB is
the largest block size the SquashFS 4.0 format allows (the superblock stores
its base-2 logarithm, at most 20), so a larger value can only come from a
corrupt or hostile image. The 100 MB cap bounds the memory one file read
from an untrusted image can take; a caller raises it with ~max_size.
Squashfs.Writer builds an image in memory from files, directories, symlinks
and device nodes, compressed with gzip or stored uncompressed. The
squashfs-eio library adds a directory tree from disk to a writer.
Installation #
Install with opam:
$ opam install squashfs squashfs-eio
If opam cannot find the package, it may not yet be released in the public
opam-repository. Add the overlay repository, then install it:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install squashfs squashfs-eio
Usage #
let read_image data =
match Squashfs.of_string data with
| Error msg -> Fmt.epr "Error: %s@." msg
| Ok fs -> (
Fmt.pr "%a@." Squashfs.pp_superblock (Squashfs.superblock fs);
match Squashfs.readdir fs (Squashfs.root fs) with
| Error msg -> Fmt.epr "Error: %s@." msg
| Ok entries ->
List.iter
(fun e ->
Fmt.pr "%s (%a)@." e.Squashfs.name Squashfs.pp_file_type
e.Squashfs.file_type)
entries)
API #
Squashfs.of_stringandSquashfs.of_readeropen an image.Squashfs.root,readdir,lookupandresolvewalk the directory tree;foldanditervisit every path.Squashfs.read_fileandread_linkread a file or a symlink target, andsafe_read_linkrefuses a target that escapes the image.Squashfs.inode_type,inode_mode,inode_size,inode_mtime,inode_uidandinode_gidread inode properties;xattrandlist_xattrsread extended attributes.Squashfs.Writer.v,add_file,add_directory,add_symlink,add_deviceandfinalizebuild an image.
Related work #
- squashfs-tools is the reference C implementation.
- go-squashfs is a Go implementation.
- backhand is a Rust implementation.
References #
Licence #
ISC License. See LICENSE.md for details.