Merge branch 'errors-from-backend'

This commit is contained in:
Matthias Beyer 2015-12-04 14:17:45 +01:00
commit 406348bc34
2 changed files with 79 additions and 56 deletions

View file

@ -39,7 +39,7 @@ pub fn list_command(module: &Module, env: CommandEnv) -> CommandResult {
let files = get_filtered_files_from_backend(module, &env); let files = get_filtered_files_from_backend(module, &env);
debug!("Printing files now"); debug!("Printing files now");
printer.print_files(files); files.map(|f| printer.print_files(f));
Ok(()) Ok(())
} }
@ -66,34 +66,35 @@ pub fn remove_command(module: &Module, env: CommandEnv) -> CommandResult {
} else { } else {
debug!("Remove more than one file"); debug!("Remove more than one file");
let files = get_filtered_files_from_backend(module, &env); get_filtered_files_from_backend(module, &env).and_then(|files| {
let nfiles = files.len(); let nfiles = files.len();
info!("Removing {} Files", nfiles); info!("Removing {} Files", nfiles);
let errs = files.map(|file| { let errs = files.map(|file| {
debug!("Remove file: {:?}", file); debug!("Remove file: {:?}", file);
env.bk.remove_file(module, file, checked) env.bk.remove_file(module, file, checked)
}) })
.filter(|e| e.is_err()) .filter(|e| e.is_err())
.map(|e| { .map(|e| {
let err = e.err().unwrap(); let err = e.err().unwrap();
warn!("Error occured in Filesystem operation: {}", err); warn!("Error occured in Filesystem operation: {}", err);
err err
}) })
.collect::<Vec<StorageBackendError>>(); .collect::<Vec<StorageBackendError>>();
let nerrs = errs.len(); let nerrs = errs.len();
if nerrs != 0 { if nerrs != 0 {
warn!("{} Errors occured while removing {} files", nerrs, nfiles); warn!("{} Errors occured while removing {} files", nerrs, nfiles);
let moderr = ModuleError::new("File removal failed"); let moderr = ModuleError::new("File removal failed");
// TODO : Collect StorageBackendErrors // TODO : Collect StorageBackendErrors
Err(moderr) Err(moderr)
} else { } else {
Ok(()) Ok(())
} }
})
} }
} }
@ -104,28 +105,34 @@ pub fn remove_command(module: &Module, env: CommandEnv) -> CommandResult {
*/ */
fn get_filtered_files_from_backend<'a>(module: &'a Module, fn get_filtered_files_from_backend<'a>(module: &'a Module,
env: &CommandEnv) -> IntoIter<File<'a>> env: &CommandEnv)
-> Result<IntoIter<File<'a>>, ModuleError>
{ {
let parser = Parser::new(JsonHeaderParser::new(None)); let parser = Parser::new(JsonHeaderParser::new(None));
let tags = get_tags(env.rt, env.matches); let tags = get_tags(env.rt, env.matches);
debug!("Tags: {:?}", tags); debug!("Tags: {:?}", tags);
env.bk.iter_files(module, &parser).and_then(|files| { env.bk.iter_files(module, &parser)
let f = files.filter(|file| { .map(|files| {
if tags.len() != 0 { let f = files.filter(|file| {
debug!("Checking tags of: {:?}", file.id()); if tags.len() != 0 {
get_tags_from_header(&file.header()).iter() debug!("Checking tags of: {:?}", file.id());
.any(|t| tags.contains(t)) get_tags_from_header(&file.header()).iter()
} else { .any(|t| tags.contains(t))
true } else {
} true
}).filter(|file| { }
debug!("Checking matches of: {:?}", file.id()); }).filter(|file| {
get_matcher(env.rt, env.matches) debug!("Checking matches of: {:?}", file.id());
.and_then(|r| Some(file.matches_with(&r))) get_matcher(env.rt, env.matches)
.unwrap_or(true) .and_then(|r| Some(file.matches_with(&r)))
}).collect::<Vec<File>>(); .unwrap_or(true)
Some(f) }).collect::<Vec<File>>();
}).unwrap_or(Vec::<File>::new()).into_iter() f.into_iter()
}).map_err(|e| {
let mut merr = ModuleError::new("Could not filter files");
merr.caused_by = Some(Box::new(e));
merr
})
} }
fn get_tags<'a>(rt: &Runtime, sub: &ArgMatches<'a, 'a>) -> Vec<String> { fn get_tags<'a>(rt: &Runtime, sub: &ArgMatches<'a, 'a>) -> Vec<String> {

View file

@ -72,26 +72,42 @@ impl StorageBackend {
} }
} }
pub fn iter_ids(&self, m: &Module) -> Option<IntoIter<FileID>> pub fn iter_ids(&self, m: &Module) -> Result<IntoIter<FileID>, StorageBackendError>
{ {
glob(&self.prefix_of_files_for_module(m)[..]).and_then(|globlist| { glob(&self.prefix_of_files_for_module(m)[..])
let v = globlist.filter_map(Result::ok) .and_then(|globlist| {
.map(|pbuf| FileID::from(&pbuf)) let v = globlist.filter_map(Result::ok)
.collect::<Vec<FileID>>() .map(|pbuf| FileID::from(&pbuf))
.into_iter(); .collect::<Vec<FileID>>()
Ok(v) .into_iter();
}).ok() Ok(v)
})
.map_err(|e| {
let serr = StorageBackendError::build(
"iter_ids()",
"Cannot iter on file ids",
None);
serr
})
} }
pub fn iter_files<'a, HP>(&self, m: &'a Module, p: &Parser<HP>) pub fn iter_files<'a, HP>(&self, m: &'a Module, p: &Parser<HP>)
-> Option<IntoIter<File<'a>>> -> Result<IntoIter<File<'a>>, StorageBackendError>
where HP: FileHeaderParser where HP: FileHeaderParser
{ {
self.iter_ids(m).and_then(|ids| { self.iter_ids(m)
Some(ids.filter_map(|id| self.get_file_by_id(m, &id, p)) .and_then(|ids| {
.collect::<Vec<File>>() Ok(ids.filter_map(|id| self.get_file_by_id(m, &id, p))
.into_iter()) .collect::<Vec<File>>()
}) .into_iter())
})
.map_err(|e| {
let serr = StorageBackendError::build(
"iter_files()",
"Cannot iter on files",
None);
serr
})
} }
/* /*