From dac4911ea225b7eb3da0e9071585071b03024a88 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 4 Dec 2015 22:49:00 +0100 Subject: [PATCH] (partly) Rewrite StorageBackend::get_file_by_id(), so we can get a file with a partially available ID --- src/storage/backend.rs | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/storage/backend.rs b/src/storage/backend.rs index 9021517b..74edc34b 100644 --- a/src/storage/backend.rs +++ b/src/storage/backend.rs @@ -189,16 +189,35 @@ impl StorageBackend { pub fn get_file_by_id<'a, HP>(&self, m: &'a Module, id: &FileID, p: &Parser) -> Option> where HP: FileHeaderParser { + use std::ops::Index; + debug!("Searching for file with id '{}'", id); - if let Ok(mut fs) = FSFile::open(self.build_filepath_with_id(m, id.clone())) { - let mut s = String::new(); - fs.read_to_string(&mut s); - debug!("Success opening file with id '{}'", id); - debug!("Parsing to internal structure now"); - p.read(s).and_then(|(h, d)| Ok(File::from_parser_result(m, id.clone(), h, d))).ok() + + if id.get_type() == FileIDType::NONE { + // We don't know the hash type, so we glob() around a bit. + + let globstr = self.prefix_of_files_for_module(m) + "*" + ".imag"; + glob(&globstr[..]).map(|globlist| { + let mut vec = globlist.filter_map(Result::ok) + .map(|pbuf| FileID::from(&pbuf)) + .filter_map(|id| self.get_file_by_id(m, &id, p)) + .collect::>(); + vec.reverse(); + vec.pop() + }).map_err(|e| e).unwrap() } else { - debug!("No file with id '{}'", id); - None + // The (hash)type is already in the FileID object, so we can just + // build a path from the information we already have + if let Ok(mut fs) = FSFile::open(self.build_filepath_with_id(m, id.clone())) { + let mut s = String::new(); + fs.read_to_string(&mut s); + debug!("Success opening file with id '{}'", id); + debug!("Parsing to internal structure now"); + p.read(s).and_then(|(h, d)| Ok(File::from_parser_result(m, id.clone(), h, d))).ok() + } else { + debug!("No file with id '{}'", id); + None + } } }