Skip to content

Commit

Permalink
fix #7: dump to a tmp file and then rename it
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieugouel committed Feb 12, 2025
1 parent fb25722 commit 1b9e55d
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ type RouterPeerUpdate = (IpAddr, IpAddr, TimedPrefix);
pub fn new_state(state_config: &StateConfig) -> AsyncState {
Arc::new(Mutex::new(State::new(state_config)))
}

pub fn dump(state: AsyncState) {
let state = state.lock().unwrap();
let file = std::fs::File::create(state.config.path.clone()).unwrap();
let temp_path = format!("{}.tmp", state.config.path);
let file = std::fs::File::create(&temp_path).unwrap();
let mut writer = std::io::BufWriter::new(file);
serde_json::to_writer(&mut writer, &state.store).unwrap();
std::fs::rename(temp_path, state.config.path.clone()).unwrap();
}

pub fn load(state: AsyncState) {
Expand Down

0 comments on commit 1b9e55d

Please sign in to comment.