#squashfs #linux #deku #linux-kernel #data-file

backhand

SquashFS 文件系统的读取、创建和修改库

24 个版本 (破坏性更新)

0.18.0 2024 年 5 月 25 日
0.16.0 2024 年 4 月 26 日
0.15.0 2024 年 3 月 24 日
0.13.0 2023 年 6 月 19 日
0.11.0 2023 年 3 月 15 日

#62 in 文件系统

Download history 562/week @ 2024-05-02 376/week @ 2024-05-09 657/week @ 2024-05-16 355/week @ 2024-05-23 466/week @ 2024-05-30 300/week @ 2024-06-06 289/week @ 2024-06-13 1257/week @ 2024-06-20 1246/week @ 2024-06-27 1232/week @ 2024-07-04 1374/week @ 2024-07-11 1400/week @ 2024-07-18 1088/week @ 2024-07-25 1056/week @ 2024-08-01 1372/week @ 2024-08-08 1232/week @ 2024-08-15

4,836 每月下载量
6 个 Crates 中使用 (5 个直接使用)

MIT/Apache 和可能 GPL-2.0

175KB
3.5K SLoC

backhand

github crates.io docs.rs build status Codecov

用于读取、创建和修改 SquashFS 文件系统的库和二进制文件。

  • — Backhand 提供了一种简单的方法来对 Squashfs 4.0 图像进行程序化分析,包括图像的提取和修改。
  • 功能标志 — 支持的压缩和解压缩功能带有功能标志,因此您的最终二进制文件(或 unsquashfs)只需要包含提取一种类型图像的代码。
  • 非常规支持 — 除了支持正常的 Linux 内核 SquashFS 4.0,我们还使用 Kind 结构支持“令人惊叹的供应商格式世界”。这允许更改魔术字节、自定义压缩算法以及数据或元数据字段的大端/小端。

编译器支持:需要 rustc 1.72.1+

将以下内容添加到您的 Cargo.toml 文件中

[dependencies]
backhand = "0.18.0"

读取/写入/修改固件

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();

二进制文件

编译器支持:需要 rustc 1.74+

这些目前处于开发中,功能不完整,欢迎提交 MR!

安装,请运行 cargo install backhand-cli --locked,或从 最新 GitHub 发布版 下载。

有关更多信息,请参阅 --help

unsquashfs-backhand

tool to uncompress, extract and list squashfs filesystems

Usage: unsquashfs-backhand [OPTIONS] [FILESYSTEM]

Arguments:
  [FILESYSTEM]  Squashfs file

Options:
  -o, --offset <BYTES>             Skip BYTES at the start of FILESYSTEM [default: 0]
  -a, --auto-offset                Find first instance of squashfs --kind magic
  -l, --list                       List filesystem, do not write to DEST (ignores --quiet)
  -d, --dest <PATHNAME>            Extract to [PATHNAME] [default: squashfs-root]
  -i, --info                       Print files as they are extracted
      --path-filter <PATH_FILTER>  Limit filesystem extraction [default: /]
  -f, --force                      If file already exists then overwrite
  -s, --stat                       Display filesystem superblock information (ignores --quiet)
  -k, --kind <KIND>                Kind(type of image) to parse [default: le_v4_0] [possible
                                   values: be_v4_0, le_v4_0, avm_be_v4_0]
      --completions <COMPLETIONS>  Emit shell completion scripts [possible values: bash, elvish,
                                   fish, powershell, zsh]
      --quiet                      Silence all progress bar and RUST_LOG output
  -h, --help                       Print help (see more with '--help')
  -V, --version                    Print version

add-backhand

tool to add a file or directory to squashfs filesystems

Usage: add-backhand [OPTIONS] <IMAGE> <FILE_PATH_IN_IMAGE>

Arguments:
  <IMAGE>               Squashfs input image
  <FILE_PATH_IN_IMAGE>  Path of file once inserted into squashfs

Options:
  -d, --dir            Create empty directory
  -f, --file <FILE>    Path of file to read, to write into squashfs
  -o, --out <OUT>      Squashfs output image [default: added.squashfs]
      --mode <MODE>    Override mode read from <FILE>
      --uid <UID>      Override uid read from <FILE>
      --gid <GID>      Override gid read from <FILE>
      --mtime <MTIME>  Override mtime read from <FILE>
  -h, --help           Print help
  -V, --version        Print version

replace-backhand

tool to replace files in squashfs filesystems

Usage: replace-backhand [OPTIONS] <IMAGE> <FILE> <FILE_PATH_IN_IMAGE>

Arguments:
  <IMAGE>               Squashfs input image
  <FILE>                Path of file to read, to write into squashfs
  <FILE_PATH_IN_IMAGE>  Path of file replaced in image

Options:
  -o, --out <OUT>  Squashfs output image [default: replaced.squashfs]
  -h, --help       Print help
  -V, --version    Print version

性能

请参阅 BENCHMARK.md

测试

请参阅 backhand-test

依赖关系

~6.5MB
~122K SLoC