From 8c70500675b919cb490f9f9eb6b151f5c9f9a84f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com> Date: Fri, 10 May 2024 13:52:14 +0100 Subject: [PATCH] chore: force flush of file in state saving Without flush we would might be loosing some bytes in writing. This is explicitly stated in https://doc.rust-lang.org/std/fs/struct.File.html. Also adds more explicit error handling in state related code. --- src/state.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/state.rs b/src/state.rs index 2e0c27d9..b13eea0a 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1472,7 +1472,10 @@ impl StateManager { let mut file = File::create(file_path) .map_err(|_| Error::CustomError(format!("Failed to create file: {}", file_path)))?; let data = Self::save(gb, format)?; - file.write_all(&data).unwrap(); + file.write_all(&data) + .map_err(|_| Error::CustomError(format!("Failed to write to file: {}", file_path)))?; + file.flush() + .map_err(|_| Error::CustomError(format!("Failed to flush file: {}", file_path)))?; Ok(()) } @@ -1484,7 +1487,8 @@ impl StateManager { let mut file = File::open(file_path) .map_err(|_| Error::CustomError(format!("Failed to open file: {}", file_path)))?; let mut data = vec![]; - file.read_to_end(&mut data).unwrap(); + file.read_to_end(&mut data) + .map_err(|_| Error::CustomError(format!("Failed to read from file: {}", file_path)))?; Self::load(&data, gb, format)?; Ok(()) } -- GitLab