From 5b4d7d9ca3b158bf1bf7945c6937d11a45c05d79 Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 19 Sep 2025 17:57:53 -0400 Subject: [PATCH] ensure output dir exists --- src/bin/subbub.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/bin/subbub.rs b/src/bin/subbub.rs index ced2221..412fd2e 100644 --- a/src/bin/subbub.rs +++ b/src/bin/subbub.rs @@ -727,12 +727,14 @@ fn write_to_output(output: &Path, files: &Vec<(&Path, Vec)>) -> Result<()> { return Err(anyhow!("no files to write to output")); } else if files.len() == 1 { // if there's only one file, write it directly to the output path + create_parent_directory(output)?; let mut file = fs::File::create(output).context("could not create output file")?; file.write_all(&files[0].1) .context("could not write to output file")?; return Ok(()); } else { // if there are multiple files, write them to the output directory + std::fs::create_dir_all(output)?; for (original_file, bytes) in files { let destination_file = output.join(original_file.file_name().context("file has no name")?); @@ -744,3 +746,15 @@ fn write_to_output(output: &Path, files: &Vec<(&Path, Vec)>) -> Result<()> { } Ok(()) } + +fn create_parent_directory(output: &Path) -> Result<()> { + match output.parent() { + Some(parent) => std::fs::create_dir_all(parent) + .context("could not create parent directory for output file"), + None => { + return Err(anyhow!( + "output path has no parent, cannot create parent directory" + )); + } + } +}