Skip to content

Commit b44ab55

Browse files
Create the output directory if it does not exist (#118) (#141)
* Change to create the output directory if it does not exist (#118) * Switch file system API from std::fs to tokio::fs to * refactor: improve error message, use anyhow::ensure * chore: add rustfmt.toml, lint --------- Co-authored-by: Victor Adossi <vadossi@cosmonic.com>
1 parent 6a15c8e commit b44ab55

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

crates/wkg/src/main.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -358,23 +358,34 @@ impl GetArgs {
358358
tracing::debug!(?release, "Fetched release details");
359359

360360
let output_trailing_slash = self.output.as_os_str().to_string_lossy().ends_with('/');
361-
let parent_dir = if output_trailing_slash {
362-
self.output.as_path()
361+
let parent_dir_path = if output_trailing_slash {
362+
// TODO(fix): TOCTOU on directory, requires platform open dir flags
363+
let parent_dir_path = self.output.as_path();
364+
if !tokio::fs::try_exists(parent_dir_path).await? {
365+
tokio::fs::create_dir_all(parent_dir_path)
366+
.await
367+
.with_context(|| {
368+
format!(
369+
"failed to create output dir @ [{}]",
370+
parent_dir_path.display()
371+
)
372+
})?;
373+
}
374+
parent_dir_path
363375
} else {
364376
self.output
365377
.parent()
366-
.context("Failed to resolve output parent dir")?
378+
.context("Failed to resolve non-trailing-slash output parent dir")?
367379
};
368380

369-
if !parent_dir.exists() {
370-
anyhow::bail!(
371-
"output directory {:?} does not exist; create it first or choose a different path",
372-
parent_dir
373-
);
374-
}
381+
ensure!(
382+
parent_dir_path.exists(),
383+
"output directory {:?} does not exist; create it first or choose a different path",
384+
parent_dir_path
385+
);
375386

376387
let (tmp_file, tmp_path) =
377-
tempfile::NamedTempFile::with_prefix_in(".wkg-get", parent_dir)?.into_parts();
388+
tempfile::NamedTempFile::with_prefix_in(".wkg-get", parent_dir_path)?.into_parts();
378389
tracing::debug!(?tmp_path, "Created temporary file");
379390

380391
let mut content_stream = client.get_content(&package, &release).await?;

rustfmt.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
edition = "2024"

0 commit comments

Comments
 (0)