From 0a62fe61acdb5dda4e28322ed96c074240bbdc44 Mon Sep 17 00:00:00 2001 From: Vika Date: Sun, 1 May 2022 04:51:12 +0300 Subject: FileStorage: fix writing settings on empty file --- src/database/file/mod.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'src/database/file/mod.rs') diff --git a/src/database/file/mod.rs b/src/database/file/mod.rs index 0336a80..53dea04 100644 --- a/src/database/file/mod.rs +++ b/src/database/file/mod.rs @@ -596,15 +596,19 @@ impl Storage for FileStorage { .open(&temppath) .await?; - let mut settings: HashMap = { - let mut f = File::open(&path).await?; - let mut content = String::new(); - f.read_to_string(&mut content).await?; - if content.is_empty() { - HashMap::default() - } else { - serde_json::from_str(&content)? + let mut settings: HashMap = match File::open(&path).await { + Ok(mut f) => { + let mut content = String::new(); + f.read_to_string(&mut content).await?; + if content.is_empty() { + HashMap::default() + } else { + serde_json::from_str(&content)? + } } + Err(err) => if err.kind() == IOErrorKind::NotFound { + HashMap::default() + } else { Err(err)? } }; settings.insert(setting, value); tempfile.write_all(serde_json::to_string(&settings)?.as_bytes()).await?; -- cgit 1.4.1