(partly) Rewrite StorageBackend::get_file_by_id(), so we can get a file with a partially available ID

This commit is contained in:
Matthias Beyer 2015-12-04 22:49:00 +01:00
parent 253658ece5
commit dac4911ea2

View file

@ -189,7 +189,25 @@ impl StorageBackend {
pub fn get_file_by_id<'a, HP>(&self, m: &'a Module, id: &FileID, p: &Parser<HP>) -> Option<File<'a>> pub fn get_file_by_id<'a, HP>(&self, m: &'a Module, id: &FileID, p: &Parser<HP>) -> Option<File<'a>>
where HP: FileHeaderParser where HP: FileHeaderParser
{ {
use std::ops::Index;
debug!("Searching for file with id '{}'", id); debug!("Searching for file with id '{}'", id);
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<File>>();
vec.reverse();
vec.pop()
}).map_err(|e| e).unwrap()
} else {
// 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())) { if let Ok(mut fs) = FSFile::open(self.build_filepath_with_id(m, id.clone())) {
let mut s = String::new(); let mut s = String::new();
fs.read_to_string(&mut s); fs.read_to_string(&mut s);
@ -201,6 +219,7 @@ impl StorageBackend {
None None
} }
} }
}
pub fn remove_file(&self, m: &Module, file: File, checked: bool) -> BackendOperationResult { pub fn remove_file(&self, m: &Module, file: File, checked: bool) -> BackendOperationResult {
if checked { if checked {