Install the backhand library
masterTo use backhand as a library in your Rust project, add it via cargo. Note that it requires rustc 1.90+.
$ cargo add backhandrepository·master·Indexed 41 days ago
https://github.com/wcampbell0x2a/backhandA Rust library and set of CLI tools for reading, creating, and modifying SquashFS file systems. It supports standard Linux kernel formats and vendor-specific formats via a configurable 'Kind' system. Includes tools such as unsquashfs-backhand for extraction and listing, add-backhand for inserting files or directories, and replace-backhand for swapping files within images. Requires rustc 1.90+.
To use backhand as a library in your Rust project, add it via cargo. Note that it requires rustc 1.90+.
$ cargo add backhandRun the project's linting suite using just lint to check for code quality and style issues.
$ just lintTo test the backhand library and the backhand-cli tool, you must first build the binaries and run the unit tests.
cargo build --release --binscargo test --workspace --release --all-features$ cargo build --release --bins
$ cargo test --workspace --release --all-featuresTo develop with backhand, you need to install the following tools:
justfile to manage development tasks. Install it from github.com/casey/just.Use the just build command to compile the project.
$ just buildTo test the project on different architectures, use cargo-cross. The project is verified in CI for the following targets:
x86_64-unknown-linux-muslaarch64-unknown-linux-muslarm-unknown-linux-musleabiarmv7-unknown-linux-musleabiTo run the comparison benchmarks (which compare backhand against squashfs-tools), use the bench.bash script. Note that these benchmarks are generated from bench.bash and typically require the dl binary from the test-assets-ureq repository.
$ ./bench.bashUse cargo llvm-cov to generate coverage reports. You can run coverage for specific binaries or for the entire workspace.
To generate a full HTML coverage report for the workspace with all features enabled (skipping slow tests), use the following command:
$ cargo llvm-cov run --bin replace --no-clean --release
$ cargo llvm-cov run --bin add --no-clean --release
$ cargo llvm-cov run --bin unsquashfs --no-clean --release
$ cargo llvm-cov --html --workspace --all-features --release --no-clean -- --skip slowRun the test suite using just test. Note that this requires squashfs-tools to be installed on your system to verify compatibility.
$ just testThe backhand library provides FilesystemReader for analyzing images and FilesystemWriter for creating or modifying them. You can convert a reader into a writer to perform modifications on an existing image.
Common operations include:
FilesystemReader::from_reader.push_file with either a std::io::Read type (like a File) or a std::io::Seek type (like a Cursor) and a NodeHeader.replace_file to swap an existing path with new data.write to save the modified filesystem to an output stream.use std::fs::File;
use std::io::{Cursor, BufReader};
use backhand::{FilesystemReader, FilesystemWriter, NodeHeader};
// read
let file = BufReader::new(File::open("file.squashfs").unwrap());
let read_filesystem = FilesystemReader::from_reader(file).unwrap();
// convert to writer
let mut write_filesystem = FilesystemWriter::from_fs_reader(&read_filesystem).unwrap();
// add file with data from slice
let d = NodeHeader::default();
let bytes = Cursor::new(b"Fear is the mind-killer.");
write_filesystem.push_file(bytes, "a/d/e/new_file", d);
// add file with data from file
let new_file = File::open("dune").unwrap();
write_filesystem.push_file(new_file, "/root/dune", d);
// modify file
let bytes = Cursor::new(b"The sleeper must awaken.\n");
write_filesystem.replace_file("/a/b/c/d/e/first_file", bytes).unwrap();
// write into a new file
let mut output = File::create("modified.squashfs").unwrap();
write_filesystem.write(&mut output).unwrap();To run the internal library benchmarks for the backhand project, use the standard cargo bench command.
$ cargo benchTo measure and compare the peak heap memory consumption of backhand against squashfs-tools, you can use heaptrack.
First, build the backhand-cli with the dist profile:
$ cargo +stable build -p backhand-cli --bins --locked --profile=distThen, run heaptrack on the resulting binary against a target squashfs file. Example for 870D97.squashfs:
# Measuring backhand
$ heaptrack ./target/dist/unsquashfs-backhand --quiet -f -d $(mktemp -d) backhand-test/test-assets/test_re815_xev160/870D97.squashfs
# Measuring squashfs-tools
$ heaptrack unsquashfs -quiet -no-progress -d $(mktemp -d) backhand-test/test-assets/test_re815_xev160/870D97.squashfs$ cargo +stable build -p backhand-cli --bins --locked --profile=dist
$ heaptrack ./target/dist/unsquashfs-backhand --quiet -f -d $(mktemp -d) backhand-test/test-assets/test_re815_xev160/870D97.squashfs
$ heaptrack unsquashfs -quiet -no-progress -d $(mktemp -d) backhand-test/test-assets/test_re815_xev160/870D97.squashfs