From: Ian Jackson Date: Sun, 2 Aug 2020 23:44:24 +0000 (+0100) Subject: provide load_games, not called anywhere yet X-Git-Tag: otter-0.2.0~1191 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=acc088bd2cdc5d50509f4254381134b77686126a;p=otter.git provide load_games, not called anywhere yet --- diff --git a/src/global.rs b/src/global.rs index c964170b..9b41422c 100644 --- a/src/global.rs +++ b/src/global.rs @@ -667,30 +667,47 @@ impl InstanceGuard<'_> { } } -#[throws(ServerFailure)] +#[throws(anyhow::Error)] pub fn load_games() { -/* /// xxx take a lock - enum A_State { Found, Used }; + enum AFState { Found(PathBuf), Used }; + use AFState::*; + use SavefilenameParseResult::*; let mut a_leaves = HashMap::new(); for de in fs::read_dir(SAVE_DIRECTORY)? { - let leaf = de.file_name().as_bytes(); - if leaf.starts_with("a-") { - a_leaves.entry(leaf.to_owned()).or_insert(A_State::Found); - } else if leaf.starts_with("g-") { - - a_leaves.insert(leaf.to_owned(),A_State::Used); - - } - - match { - &[b"g-"..] => { + let de = de?; + let leaf = de.file_name(); + (||{ + let leaf = leaf.as_bytes(); + match savefilename_parse(leaf)? { + NotGameFile => { + }, + TempToDelete => { + fs::remove_file(de.path()) + .context("stale temporary file")?; + }, + AccessFile => { + a_leaves.entry(leaf.to_owned()).or_insert_with( + || Found(de.path()) + ); + }, + GameFile { access_leaf, name } => { + InstanceGuard::load(name)?; + a_leaves.insert(access_leaf, Used); + }, } - _ => { + >::Ok(()) + })().with_context(|| format!("leaf={:?}", leaf))?; + } + (||{ + for (leaf, state) in &a_leaves { + if let Found(path) = state { + fs::remove_file(&path) + .with_context(|| format!("leaf={:?}", leaf))?; } } - } -*/ + >::Ok(()) + })().context("cleaning up stale files")?; } // ---------- Tokens / TokenTable / AccessId ---------- diff --git a/src/imports.rs b/src/imports.rs index 0f69f9d1..c119eace 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -26,7 +26,9 @@ pub use std::fs::File; pub use std::mem; pub use std::os::unix; pub use std::time::Instant; - +pub use std::path::PathBuf; +pub use std::os::unix::ffi::OsStrExt; + pub use thiserror::Error; pub use anyhow::{Context,anyhow}; pub use fehler::{throws,throw};