From 668132237008e097c67df03e470a3b058f2cd421 Mon Sep 17 00:00:00 2001
From: Antoine Gersant <antoine.gersant@lesforges.org>
Date: Fri, 31 Jan 2025 17:38:42 -0800
Subject: [PATCH] Fixed a bug where config file would fail to be created when
 parent directory does not exist

---
 src/app/config.rs | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/src/app/config.rs b/src/app/config.rs
index e3e85ff..60f86b5 100644
--- a/src/app/config.rs
+++ b/src/app/config.rs
@@ -76,7 +76,20 @@ pub struct Manager {
 
 impl Manager {
 	pub async fn new(config_file_path: &Path, auth_secret: auth::Secret) -> Result<Self, Error> {
-		tokio::fs::File::create_new(config_file_path).await.ok();
+		if let Some(parent) = config_file_path.parent() {
+			tokio::fs::create_dir_all(parent)
+				.await
+				.map_err(|e| Error::Io(parent.to_owned(), e))?;
+		}
+
+		match tokio::fs::File::create_new(config_file_path).await {
+			Ok(_) => (),
+			Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => (),
+			Err(e) => {
+				error!("Failed to create config file at {config_file_path:#?}: {e}");
+				return Err(Error::Io(config_file_path.to_owned(), e));
+			}
+		};
 
 		let notify = Arc::new(Notify::new());
 		let mut debouncer = notify_debouncer_full::new_debouncer(Duration::from_secs(1), None, {