Skip to main content

greentic_bundle/bundle_fs/
native_mksquashfs_writer.rs

1use std::io::ErrorKind;
2use std::path::Path;
3use std::process::Command;
4
5use anyhow::{Result, bail};
6
7use super::{BundleFsWriter, WRITER_ENV};
8
9pub struct MksquashfsBundleFsWriter;
10
11impl BundleFsWriter for MksquashfsBundleFsWriter {
12    fn write_bundle(&self, input_dir: &Path, output_file: &Path) -> Result<()> {
13        if let Some(parent) = output_file.parent() {
14            std::fs::create_dir_all(parent)?;
15        }
16        if output_file.exists() {
17            std::fs::remove_file(output_file)?;
18        }
19        super::assert_no_dev_secret_paths(input_dir)?;
20        let output = Command::new("mksquashfs")
21            .arg(input_dir)
22            .arg(output_file)
23            .args([
24                "-noappend",
25                "-all-root",
26                "-no-progress",
27                "-quiet",
28                "-processors",
29                "1",
30                "-fstime",
31                "0",
32                "-mkfs-time",
33                "0",
34                "-all-time",
35                "0",
36                "-sort",
37                "/dev/null",
38            ])
39            .output()
40            .map_err(|error| match error.kind() {
41                ErrorKind::NotFound => anyhow::anyhow!(
42                    "{WRITER_ENV}=mksquashfs was requested, but mksquashfs was not found. Install squashfs-tools or unset {WRITER_ENV} to use the Rust-native writer."
43                ),
44                _ => anyhow::Error::new(error).context("spawn mksquashfs"),
45            })?;
46        if !output.status.success() {
47            bail!(
48                "mksquashfs failed: {}",
49                String::from_utf8_lossy(&output.stderr).trim()
50            );
51        }
52        Ok(())
53    }
54}