From 28cc0c29ad88e723c3029c06a1f759eebb294d4e Mon Sep 17 00:00:00 2001 From: Chris Fotiadis Date: Fri, 1 Dec 2023 22:32:19 +0100 Subject: [PATCH] fix: ensure target dir for replay exists --- crates/client/src/lan/game/game.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/crates/client/src/lan/game/game.rs b/crates/client/src/lan/game/game.rs index 4bdf04e..1ca597c 100644 --- a/crates/client/src/lan/game/game.rs +++ b/crates/client/src/lan/game/game.rs @@ -22,6 +22,8 @@ use flo_w3gs::protocol::leave::LeaveAck; use flo_w3gs::protocol::ping::PingFromHost; use parking_lot::Mutex; use std::collections::BTreeSet; +use std::fs; +use std::path::Path; use std::time::Duration; use tokio::sync::mpsc::{Receiver, Sender}; use tokio::sync::watch::Receiver as WatchReceiver; @@ -195,12 +197,23 @@ impl<'a> GameHandler<'a> { let now = chrono::Utc::now(); let now_timestamp_str = format!("w3c-{}.w3g", now.format("%Y%m%d%H%M%S")); user_replay_path.push_str(&now_timestamp_str); - let the_file = match std::fs::File::create(&user_replay_path) { - Ok(file) => Some(file), - Err(err) => { - tracing::error!("Could not open file: {}", err); + let the_file = { + let path = Path::new(&user_replay_path); + if let Some(parent) = path.parent() { + if let Err(err) = fs::create_dir_all(parent) { + tracing::error!("Could not create directory: {}", err); + None + } else { + match std::fs::File::create(path) { + Ok(file) => Some(file), + Err(err) => { + tracing::error!("Could not open file: {}", err); + None + } + } + } + } else { None - //return; } }; if let Some(the_file) = the_file {