chiark / gitweb /
provide load_games, not called anywhere yet
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 2 Aug 2020 23:44:24 +0000 (00:44 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 2 Aug 2020 23:44:24 +0000 (00:44 +0100)
src/global.rs
src/imports.rs

index c964170b4395cdb255f60090dd69563e0f5dd68d..9b41422c606db929a69a60ad17b6fc92a4da1ca1 100644 (file)
@@ -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);
+        },
       }
-      _ => {
+      <Result<_,anyhow::Error>>::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))?;
       }
     }
-  }
-*/
+    <Result<_,anyhow::Error>>::Ok(())
+  })().context("cleaning up stale files")?;
 }
 
 // ---------- Tokens / TokenTable / AccessId ----------
index 0f69f9d1c2413f7fa65e64028c73f066cb1d3d04..c119eace036c1d8d47942efe8af72151babbf63f 100644 (file)
@@ -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};