From 4ff7fb88780d5bfa83a548853ad7b63527cdedde Mon Sep 17 00:00:00 2001 From: Zero Ekkusu <94782988+ZeroEkkusu@users.noreply.github.com> Date: Fri, 27 Jan 2023 21:18:22 +0100 Subject: [PATCH 1/2] fix(eas): allow `path/to/output` & add clean-up --- etk-asm/src/bin/eas.rs | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/etk-asm/src/bin/eas.rs b/etk-asm/src/bin/eas.rs index cf3285cd..bb10dc82 100644 --- a/etk-asm/src/bin/eas.rs +++ b/etk-asm/src/bin/eas.rs @@ -3,9 +3,9 @@ use etk_cli::io::HexWrite; use etk_asm::ingest::{Error, Ingest}; -use std::fs::File; +use std::fs::{create_dir_all, File}; use std::io::prelude::*; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use clap::StructOpt; @@ -19,6 +19,11 @@ struct Opt { } fn create(path: PathBuf) -> File { + match create_dir_all(path.parent().unwrap()) { + Ok(_) => (), + Err(why) => panic!("couldn't create parent directories: {}", why), + } + match File::create(&path) { Err(why) => panic!("couldn't create `{}`: {}", path.display(), why), Ok(file) => file, @@ -38,15 +43,41 @@ fn main() { fn run() -> Result<(), Error> { let opt: Opt = clap::Parser::parse(); + let mut out_file_exists = false; + let mut out_file_content = vec![]; + if let Some(o) = &opt.out { + if Path::new(o).exists() { + out_file_exists = true; + match std::fs::read(o) { + Ok(content) => out_file_content = content, + Err(e) => panic!("couldn't backup existing file: {}", e), + } + } + } + let mut out: Box = match opt.out { - Some(o) => Box::new(create(o)), + Some(ref o) => Box::new(create(o.to_path_buf())), None => Box::new(std::io::stdout()), }; let hex_out = HexWrite::new(&mut out); let mut ingest = Ingest::new(hex_out); - ingest.ingest_file(opt.input)?; + + if let Err(e) = ingest.ingest_file(opt.input) { + if out_file_exists { + match std::fs::write(&opt.out.unwrap(), &out_file_content) { + Ok(_) => (), + Err(e) => panic!("couldn't restore existing file.\n{}", e), + }; + } else if let Some(o) = &opt.out { + match std::fs::remove_file(o) { + Ok(_) => (), + Err(e) => panic!("couldn't clean up.\n{}", e), + } + } + return Err(e); + } out.write_all(b"\n").unwrap(); From 8682c1159dd8c04400b374ed7ec5b9a9c196b99c Mon Sep 17 00:00:00 2001 From: Zero Ekkusu <94782988+ZeroEkkusu@users.noreply.github.com> Date: Fri, 27 Jan 2023 21:26:39 +0100 Subject: [PATCH 2/2] chore: format --- etk-asm/src/bin/eas.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/etk-asm/src/bin/eas.rs b/etk-asm/src/bin/eas.rs index bb10dc82..6618e16f 100644 --- a/etk-asm/src/bin/eas.rs +++ b/etk-asm/src/bin/eas.rs @@ -51,7 +51,7 @@ fn run() -> Result<(), Error> { match std::fs::read(o) { Ok(content) => out_file_content = content, Err(e) => panic!("couldn't backup existing file: {}", e), - } + } } }