From 21a9552b0bc927b6007eea7dd6aca3e9a13217e3 Mon Sep 17 00:00:00 2001 From: Cole Mickens Date: Thu, 29 Aug 2024 14:34:42 -0700 Subject: [PATCH] startup notification: create_dir_all parent dir, add context for io ops --- magic-nix-cache/src/main.rs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/magic-nix-cache/src/main.rs b/magic-nix-cache/src/main.rs index e5fbdfa..661dd11 100644 --- a/magic-nix-cache/src/main.rs +++ b/magic-nix-cache/src/main.rs @@ -392,8 +392,33 @@ async fn main_cli() -> Result<()> { tracing::debug!("Startup notification via file at {startup_notification_file_path:?}"); - let mut notification_file = File::create(&startup_notification_file_path).await?; - notification_file.write_all(file_contents).await?; + if let Some(parent_dir) = startup_notification_file_path.parent() { + tokio::fs::create_dir_all(parent_dir) + .await + .with_context(|| { + format!( + "failed to create parent directory for startup notification file path: {}", + startup_notification_file_path.display() + ) + })?; + } + let mut notification_file = File::create(&startup_notification_file_path) + .await + .with_context(|| { + format!( + "failed to create startup notification file to path: {}", + startup_notification_file_path.display() + ) + })?; + notification_file + .write_all(file_contents) + .await + .with_context(|| { + format!( + "failed to write startup notification file to path: {}", + startup_notification_file_path.display() + ) + })?; tracing::debug!("Created startup notification file at {startup_notification_file_path:?}"); }