initfs: Simplify archive cli
This commit is contained in:
Binary file not shown.
@@ -1,6 +1,6 @@
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use anyhow::Result;
|
||||
use clap::{Arg, Command};
|
||||
|
||||
use redox_initfs_tools::{self as archive, Args, DEFAULT_MAX_SIZE};
|
||||
@@ -10,13 +10,6 @@ fn main() -> Result<()> {
|
||||
.about("create an initfs image from a directory")
|
||||
.version(clap::crate_version!())
|
||||
.author(clap::crate_authors!())
|
||||
.arg(
|
||||
Arg::new("MAX_SIZE")
|
||||
.long("max-size")
|
||||
.short('m')
|
||||
.required(false)
|
||||
.help("Set the upper limit for how large the image can become (default 64 MiB)."),
|
||||
)
|
||||
// TODO: support non-utf8 paths (applies to other paths as well)
|
||||
.arg(
|
||||
Arg::new("SOURCE")
|
||||
@@ -25,7 +18,7 @@ fn main() -> Result<()> {
|
||||
)
|
||||
.arg(
|
||||
Arg::new("BOOTSTRAP_CODE")
|
||||
.required(false)
|
||||
.required(true)
|
||||
.help("Specify the bootstrap ELF file to include in the image."),
|
||||
)
|
||||
.arg(
|
||||
@@ -39,19 +32,13 @@ fn main() -> Result<()> {
|
||||
|
||||
env_logger::init();
|
||||
|
||||
let max_size = if let Some(max_size_str) = matches.get_one::<String>("MAX_SIZE") {
|
||||
max_size_str
|
||||
.parse::<u64>()
|
||||
.context("expected an integer for MAX_SIZE")?
|
||||
} else {
|
||||
DEFAULT_MAX_SIZE
|
||||
};
|
||||
|
||||
let source = matches
|
||||
.get_one::<String>("SOURCE")
|
||||
.expect("expected the required arg SOURCE to exist");
|
||||
|
||||
let bootstrap_code = matches.get_one::<String>("BOOTSTRAP_CODE");
|
||||
let bootstrap_code = matches
|
||||
.get_one::<String>("BOOTSTRAP_CODE")
|
||||
.expect("expected the required arg BOOTSTRAP_CODE to exist");
|
||||
|
||||
let destination = matches
|
||||
.get_one::<String>("OUTPUT")
|
||||
@@ -59,9 +46,9 @@ fn main() -> Result<()> {
|
||||
|
||||
let args = Args {
|
||||
source: Path::new(source),
|
||||
bootstrap_code: bootstrap_code.map(Path::new),
|
||||
bootstrap_code: Path::new(bootstrap_code),
|
||||
destination_path: Path::new(destination),
|
||||
max_size,
|
||||
max_size: DEFAULT_MAX_SIZE,
|
||||
};
|
||||
archive::archive(&args)
|
||||
}
|
||||
|
||||
+14
-18
@@ -418,7 +418,7 @@ pub struct Args<'a> {
|
||||
pub destination_path: &'a Path,
|
||||
pub max_size: u64,
|
||||
pub source: &'a Path,
|
||||
pub bootstrap_code: Option<&'a Path>,
|
||||
pub bootstrap_code: &'a Path,
|
||||
}
|
||||
pub fn archive(
|
||||
&Args {
|
||||
@@ -479,26 +479,22 @@ pub fn archive(
|
||||
let header_offset = bump_alloc(&mut state, 4096, "allocate header")?;
|
||||
assert_eq!(header_offset, 0);
|
||||
|
||||
let bootstrap_entry = if let Some(bootstrap_code) = bootstrap_code {
|
||||
allocate_and_write_file(
|
||||
&mut state,
|
||||
&File::open(bootstrap_code).with_context(|| {
|
||||
anyhow!(
|
||||
"failed to open bootstrap code file `{}`",
|
||||
bootstrap_code.to_string_lossy(),
|
||||
)
|
||||
})?,
|
||||
)?;
|
||||
let bootstrap_data = std::fs::read(bootstrap_code).with_context(|| {
|
||||
allocate_and_write_file(
|
||||
&mut state,
|
||||
&File::open(bootstrap_code).with_context(|| {
|
||||
anyhow!(
|
||||
"failed to read bootstrap code file `{}`",
|
||||
"failed to open bootstrap code file `{}`",
|
||||
bootstrap_code.to_string_lossy(),
|
||||
)
|
||||
})?;
|
||||
elf_entry(&bootstrap_data)
|
||||
} else {
|
||||
u64::MAX
|
||||
};
|
||||
})?,
|
||||
)?;
|
||||
let bootstrap_data = std::fs::read(bootstrap_code).with_context(|| {
|
||||
anyhow!(
|
||||
"failed to read bootstrap code file `{}`",
|
||||
bootstrap_code.to_string_lossy(),
|
||||
)
|
||||
})?;
|
||||
let bootstrap_entry = elf_entry(&bootstrap_data);
|
||||
|
||||
let inode_table_length = {
|
||||
let inode_entry_size: u64 = std::mem::size_of::<initfs::InodeHeader>()
|
||||
|
||||
@@ -77,7 +77,7 @@ fn archive_and_read() -> Result<()> {
|
||||
let args = redox_initfs_tools::Args {
|
||||
destination_path: &Path::new(env!("CARGO_TARGET_TMPDIR")).join("out.img"),
|
||||
source: Path::new("data"),
|
||||
bootstrap_code: None,
|
||||
bootstrap_code: Path::new("data/foo/bootstrap.elf"),
|
||||
max_size: redox_initfs_tools::DEFAULT_MAX_SIZE,
|
||||
};
|
||||
redox_initfs_tools::archive(&args).context("failed to archive")?;
|
||||
@@ -94,6 +94,10 @@ fn archive_and_read() -> Result<()> {
|
||||
let reference_tree = Node::dir([(
|
||||
b"foo",
|
||||
Node::dir([
|
||||
(
|
||||
b"bootstrap.elf".as_slice(),
|
||||
Node::file("\x7FELF\x01\x01\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"),
|
||||
),
|
||||
(b"file-link.txt".as_slice(), Node::link(b"file.txt")),
|
||||
(
|
||||
b"file.txt".as_slice(),
|
||||
|
||||
Reference in New Issue
Block a user