chiark / gitweb /
bundles: Provide Id::open
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 3 May 2021 11:59:12 +0000 (12:59 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 3 May 2021 11:59:12 +0000 (12:59 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/bundles.rs

index 10c5e9be3e1b16d7b57b982613949c7213ef3631..66e80c0beb19bc84bdcd2b5a6b734cb4c45968fb 100644 (file)
@@ -101,6 +101,18 @@ impl Id {
   fn path(&self, instance: &InstanceName) -> String {
     b_file(instance, self.index, self.kind)
   }
+
+  #[throws(IE)]
+  pub fn open(&self, instance_name: &InstanceName) -> Option<fs::File> {
+    let path = self.path(instance_name);
+    match File::open(&path) {
+      Ok(f) => Some(f),
+      Err(e) if e.kind() == ErrorKind::NotFound => None,
+      Err(e) => void::unreachable(
+        Err::<Void,_>(e).context(path).context("open bundle")?
+      ),
+    }
+  }
 }
 
 #[derive(Error,Debug)]