The world's most clever kitty cat
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Atomic saves

Ben C cad5a22e dd94b59a

+10 -2
+10 -2
src/main.rs
··· 110 110 } 111 111 112 112 async fn save_brain(ctx: Arc<BotContext>) -> Result { 113 - // TODO: Atomic saves 114 - let mut file = File::create(&ctx.brain_file_path).context("Failed to open brain file")?; 113 + let scratch_path = ctx.brain_file_path.with_file_name(format!( 114 + "~{}", 115 + ctx.brain_file_path.file_name().unwrap().to_str().unwrap() 116 + )); 117 + let mut file = File::create(&scratch_path).context("Failed to open brain file")?; 115 118 let mut brotli_writer = 116 119 brotli::CompressorWriter::with_params(&mut file, BROTLI_BUF_SIZE, &get_brotli_params()); 120 + 117 121 let brain = ctx.brain_handle.read().await; 118 122 rmp_serde::encode::write(&mut brotli_writer, &*brain) 119 123 .context("Failed to write serialized brain")?; 124 + 125 + std::fs::rename(&scratch_path, &ctx.brain_file_path) 126 + .context("Failed to override scratch file")?; 127 + 120 128 debug!("Saved brain file"); 121 129 Ok(()) 122 130 }