diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/persistence.rs | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/src/persistence.rs b/src/persistence.rs index f1ed845..2903ec6 100644 --- a/src/persistence.rs +++ b/src/persistence.rs @@ -9,7 +9,8 @@ use crate::{ RoomId, savestate::SaveState, playerstate::PlayerState, - Result + Result, + aerr }; pub trait PersistentStorage { @@ -103,7 +104,15 @@ impl PersistentStorage for FileStorage { } fn write_file_safe<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> Result<()> { - let temppath = path.as_ref().with_file_name(format!("tempfile_{}.tmp", rand::random::<u64>())); + let temppath = path + .as_ref() + .with_file_name( + format!( + "tempfile_{}_{}.tmp", + path.as_ref().file_name().ok_or(aerr!("writing to directory"))?.to_str().unwrap_or("invalid"), + rand::random::<u64>() + ) + ); fs::write(&temppath, contents)?; fs::rename(&temppath, path)?; Ok(()) |
