-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
98 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use self::args::{ManifestSubcommand, ModuleSubcommand, ToolArgs, ToolSubcommand}; | ||
use clap::Parser; | ||
use color_eyre::{eyre::bail, Result}; | ||
use std::{ | ||
ffi::OsString, | ||
fs::{self, File}, | ||
io::Write, | ||
path::Path, | ||
}; | ||
|
||
mod args; | ||
|
||
pub fn read_manifest<W>(sink: &mut W, module: &[u8]) -> Result<()> | ||
where | ||
W: Write, | ||
{ | ||
let Some((manifest, _section_range)) = mrf_manifest::decode(module)? else { | ||
bail!("missing manifest in module"); | ||
}; | ||
|
||
let prettified = serde_json::to_string_pretty(&manifest)?; | ||
writeln!(sink, "{prettified}")?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn remove_manifest(module_path: &Path, output_path: &Path) -> Result<()> { | ||
let module = fs::read(module_path)?; | ||
let Some((_manifest, section_range)) = mrf_manifest::decode(&module)? else { | ||
bail!("missing manifest in module"); | ||
}; | ||
|
||
let mut module_file = File::options() | ||
.create(true) | ||
.truncate(true) | ||
.write(true) | ||
.open(output_path)?; | ||
|
||
module_file.write_all(&module[..section_range.start])?; | ||
module_file.write_all(&module[section_range.end..])?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn write_manifest(manifest: &[u8], module_path: &Path) -> Result<()> { | ||
// Parse the manifest and re-encode it in canonical JSON | ||
let parsed_manifest = serde_json::from_slice(manifest)?; | ||
let custom_section = mrf_manifest::encode(&parsed_manifest)?; | ||
|
||
let mut file = File::options().append(true).open(module_path)?; | ||
file.write_all(&custom_section)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn handle<I, W>(sink: &mut W, input: I) -> Result<()> | ||
where | ||
I: IntoIterator, | ||
<I as IntoIterator>::Item: Into<OsString> + Clone, | ||
W: Write, | ||
{ | ||
let args = ToolArgs::try_parse_from(input)?; | ||
match args.command { | ||
ToolSubcommand::Manifest(ManifestSubcommand::Add(args)) => { | ||
let manifest = fs::read(args.manifest_path)?; | ||
|
||
// Only copy if the paths are distinct | ||
if args.module_path != args.output { | ||
fs::copy(&args.module_path, &args.output)?; | ||
} | ||
|
||
write_manifest(&manifest, &args.output)?; | ||
} | ||
ToolSubcommand::Manifest(ManifestSubcommand::Read(args)) => { | ||
let data = fs::read(args.module_path)?; | ||
read_manifest(sink, &data)?; | ||
} | ||
ToolSubcommand::Manifest(ManifestSubcommand::Remove(args)) => { | ||
remove_manifest(&args.module_path, &args.output)?; | ||
} | ||
ToolSubcommand::Module(ModuleSubcommand::Validate(args)) => { | ||
let data = fs::read(args.module_path)?; | ||
wasmparser::validate(&data)?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters