Merge branch 'fix-bm-remove'

This commit is contained in:
Matthias Beyer 2015-12-04 23:21:32 +01:00
commit a005d4209b
3 changed files with 56 additions and 17 deletions

View file

@ -60,7 +60,13 @@ pub fn remove_command(module: &Module, env: CommandEnv) -> CommandResult {
debug!("Remove by id: {}", id); debug!("Remove by id: {}", id);
let parser = Parser::new(JsonHeaderParser::new(None)); let parser = Parser::new(JsonHeaderParser::new(None));
let file = env.bk.get_file_by_id(module, &id.into(), &parser).unwrap(); let file = env.bk
.get_file_by_id(module, &id.into(), &parser)
.unwrap_or({
info!("No files found");
return Ok(())
});
debug!("Remove file: {:?}", file); debug!("Remove file: {:?}", file);
if let Err(e) = env.bk.remove_file(module, file, checked) { if let Err(e) = env.bk.remove_file(module, file, checked) {
@ -69,7 +75,7 @@ pub fn remove_command(module: &Module, env: CommandEnv) -> CommandResult {
err.caused_by = Some(Box::new(e)); err.caused_by = Some(Box::new(e));
Err(err) Err(err)
} else { } else {
debug!("Remove worked"); info!("Remove worked");
Ok(()) Ok(())
} }
} else { } else {

View file

@ -59,10 +59,7 @@ impl StorageBackend {
glob(&globstr[..]) glob(&globstr[..])
.and_then(|globlist| { .and_then(|globlist| {
debug!("Iterating over globlist"); debug!("Iterating over globlist");
Ok(globlist.filter_map(Result::ok) Ok(globlist_to_file_id_vec(globlist).into_iter())
.map(|pbuf| FileID::from(&pbuf))
.collect::<Vec<FileID>>()
.into_iter())
}) })
.map_err(|e| { .map_err(|e| {
debug!("glob() returned error: {:?}", e); debug!("glob() returned error: {:?}", e);
@ -189,7 +186,31 @@ 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.
debug!("Having FileIDType::NONE, so we glob() for the raw ID");
let id_str = id.get_id().unwrap_or(String::from("INVALID"));
let globstr = self.prefix_of_files_for_module(m) + "*" + &id_str[..] + ".imag";
debug!("Globbing with globstr = '{}'", globstr);
glob(&globstr[..]).map(|globlist| {
let mut vec = globlist_to_file_id_vec(globlist).into_iter()
.filter_map(|id| self.get_file_by_id(m, &id, p))
.collect::<Vec<File>>();
vec.reverse();
vec.pop()
}).unwrap_or({
debug!("No glob matches, actually. We can't do anything at this point");
None
})
} else {
// The (hash)type is already in the FileID object, so we can just
// build a path from the information we already have
debug!("We know FileIDType, so we build the path directly now");
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 +222,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 {
@ -307,3 +329,9 @@ fn write_with_parser<'a, HP>(f: &File, p: &Parser<HP>) -> Result<String, Storage
Err(serr) Err(serr)
}) })
} }
fn globlist_to_file_id_vec(globlist: Paths) -> Vec<FileID> {
globlist.filter_map(Result::ok)
.map(|pbuf| FileID::from(&pbuf))
.collect::<Vec<FileID>>()
}

View file

@ -73,6 +73,10 @@ impl FileID {
self.id_type.clone() self.id_type.clone()
} }
pub fn get_id(&self) -> Option<String> {
self.id.clone()
}
} }
impl Debug for FileID { impl Debug for FileID {
@ -127,7 +131,7 @@ impl From<String> for FileID {
impl<'a> From<&'a String> for FileID { impl<'a> From<&'a String> for FileID {
fn from(string: &'a String) -> FileID { fn from(string: &'a String) -> FileID {
// we assume that it is an path
let regex = Regex::new(r"([:alnum:]*)-([:upper:]*)-([A-Za-z0-9-_]*)\.(.*)").unwrap(); let regex = Regex::new(r"([:alnum:]*)-([:upper:]*)-([A-Za-z0-9-_]*)\.(.*)").unwrap();
let s = string.split("/").last().unwrap_or(""); let s = string.split("/").last().unwrap_or("");
@ -160,9 +164,10 @@ impl<'a> From<&'a String> for FileID {
Some(FileID::new(idtype, String::from(hash))) Some(FileID::new(idtype, String::from(hash)))
}).unwrap_or({ }).unwrap_or({
debug!("Did not match"); debug!("Did not match");
debug!("It is no path, actually. So we assume it is an ID already");
FileID { FileID {
id_type: FileIDType::NONE, id_type: FileIDType::NONE,
id: None, id: Some(string.clone()),
} }
}) })
} }