From 84bb05035b51b05734eef44657cffd1fdb2950db Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 23 Nov 2019 20:09:40 +0100 Subject: Fix: Ignore broken pipe when reporting ids This fixes a bug where a broken pipe in Runtime::report_touched() resulted in an Err(_) raised up to main() which then reported this. But as report_touched() should ignore a broken pipe (because the program will exit anyways shortly after the call), we can safely ignore this error. This also results in `ExitCode` removed from the function signature, which pushes us forward to the removal of custom error-handling implementations! Signed-off-by: Matthias Beyer --- lib/core/libimagrt/src/runtime.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/core/libimagrt/src/runtime.rs b/lib/core/libimagrt/src/runtime.rs index d8997a8b..a1a27c6c 100644 --- a/lib/core/libimagrt/src/runtime.rs +++ b/lib/core/libimagrt/src/runtime.rs @@ -24,7 +24,6 @@ use std::process::exit; use std::io::Stdin; use std::io::StdoutLock; use std::borrow::Borrow; -use std::result::Result as RResult; pub use clap::App; use clap::AppSettings; @@ -41,10 +40,8 @@ use crate::configuration::{fetch_config, override_config, InternalConfiguration} use crate::logger::ImagLogger; use crate::io::OutputProxy; -use libimagerror::exit::ExitCode; use libimagerror::errors::ErrorMsg as EM; use libimagerror::trace::*; -use libimagerror::io::ToExitCode; use libimagstore::store::Store; use libimagstore::storeid::StoreId; use libimagutil::debug_result::DebugResult; @@ -522,14 +519,14 @@ impl<'a> Runtime<'a> { .map_err(Error::from) } - pub fn report_touched(&self, id: &StoreId) -> RResult<(), ExitCode> { + pub fn report_touched(&self, id: &StoreId) -> Result<()> { let out = ::std::io::stdout(); let mut lock = out.lock(); - self.report_touched_id(id, &mut lock) + self.report_touched_id(id, &mut lock).map(|_| ()) } - pub fn report_all_touched(&self, ids: I) -> RResult<(), ExitCode> + pub fn report_all_touched(&self, ids: I) -> Result<()> where ID: Borrow + Sized, I: Iterator { @@ -537,22 +534,30 @@ impl<'a> Runtime<'a> { let mut lock = out.lock(); for id in ids { - self.report_touched_id(id.borrow(), &mut lock)?; + if !self.report_touched_id(id.borrow(), &mut lock)? { + break + } } Ok(()) } #[inline] - fn report_touched_id(&self, id: &StoreId, output: &mut StdoutLock) -> RResult<(), ExitCode> { + fn report_touched_id(&self, id: &StoreId, output: &mut StdoutLock) -> Result { use std::io::Write; if self.output_is_pipe() && !self.ignore_ids { trace!("Reporting: {} to {:?}", id, output); - writeln!(output, "{}", id).to_exit_code() - } else { - Ok(()) + if let Err(e) = writeln!(output, "{}", id) { + return if e.kind() == std::io::ErrorKind::BrokenPipe { + Ok(false) + } else { + Err(failure::Error::from(e)) + } + } } + + Ok(true) } } -- cgit v1.2.3 From 0464bfba53255814aadc35d9927c8d0fff2eb00d Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 23 Nov 2019 20:22:31 +0100 Subject: Remove error conversion which is not necessary anymore. Signed-off-by: Matthias Beyer --- bin/core/imag-category/src/lib.rs | 2 +- bin/core/imag-gps/src/lib.rs | 6 +++--- bin/core/imag-grep/src/lib.rs | 3 +-- bin/core/imag-header/src/lib.rs | 12 ++++++------ bin/core/imag-id-in-collection/src/lib.rs | 3 +-- bin/core/imag-ids/src/lib.rs | 3 +-- bin/core/imag-link/src/lib.rs | 11 +++++------ bin/core/imag-ref/src/lib.rs | 4 ++-- bin/core/imag-store/src/create.rs | 3 +-- bin/core/imag-store/src/get.rs | 3 +-- bin/core/imag-store/src/retrieve.rs | 3 +-- bin/core/imag-store/src/update.rs | 3 +-- bin/core/imag-tag/src/lib.rs | 9 ++++----- bin/core/imag-view/src/lib.rs | 5 ++--- bin/domain/imag-calendar/src/lib.rs | 6 +++--- bin/domain/imag-todo/src/lib.rs | 8 ++++---- 16 files changed, 37 insertions(+), 47 deletions(-) diff --git a/bin/core/imag-category/src/lib.rs b/bin/core/imag-category/src/lib.rs index 27fd2d6b..8e70d722 100644 --- a/bin/core/imag-category/src/lib.rs +++ b/bin/core/imag-category/src/lib.rs @@ -165,7 +165,7 @@ fn create_category(rt: &Runtime) -> Result<()> { let name = scmd.value_of("create-category-name").map(String::from).unwrap(); // safed by clap rt.store() .create_category(&name) - .and_then(|e| rt.report_touched(e.get_location()).map_err(Error::from)) + .and_then(|e| rt.report_touched(e.get_location())) } fn delete_category(rt: &Runtime) -> Result<()> { diff --git a/bin/core/imag-gps/src/lib.rs b/bin/core/imag-gps/src/lib.rs index 20c3b495..c58a71de 100644 --- a/bin/core/imag-gps/src/lib.rs +++ b/bin/core/imag-gps/src/lib.rs @@ -144,7 +144,7 @@ fn add(rt: &Runtime) -> Result<()> { .ok_or_else(|| format_err!("No such entry: {}", id))? .set_coordinates(c.clone())?; - rt.report_touched(&id).map_err(Error::from) + rt.report_touched(&id) }) .collect() } @@ -170,7 +170,7 @@ fn remove(rt: &Runtime) -> Result<()> { writeln!(rt.stdout(), "{}", removed_value)?; } - rt.report_touched(&id).map_err(Error::from) + rt.report_touched(&id) }) .collect() } @@ -194,7 +194,7 @@ fn get(rt: &Runtime) -> Result<()> { writeln!(stdout, "{}", value)?; - rt.report_touched(&id).map_err(Error::from) + rt.report_touched(&id) }) .collect() } diff --git a/bin/core/imag-grep/src/lib.rs b/bin/core/imag-grep/src/lib.rs index 8a04eaf3..be5d4e2a 100644 --- a/bin/core/imag-grep/src/lib.rs +++ b/bin/core/imag-grep/src/lib.rs @@ -48,7 +48,6 @@ use std::io::Write; use regex::Regex; use clap::App; -use failure::Error; use failure::Fallible as Result; use failure::err_msg; use resiter::AndThen; @@ -154,6 +153,6 @@ fn show(rt: &Runtime, e: &Entry, re: &Regex, opts: &Options, count: &mut usize) *count += 1; } - rt.report_touched(e.get_location()).map_err(Error::from) + rt.report_touched(e.get_location()) } diff --git a/bin/core/imag-header/src/lib.rs b/bin/core/imag-header/src/lib.rs index 243cd939..3fbd9bd5 100644 --- a/bin/core/imag-header/src/lib.rs +++ b/bin/core/imag-header/src/lib.rs @@ -180,7 +180,7 @@ fn has<'a, 'e, I>(rt: &Runtime, mtch: &ArgMatches<'a>, iter: I) -> Result<()> if !rt.output_is_pipe() { writeln!(output, "{}", entry.get_location())?; } - rt.report_touched(entry.get_location()).map_err(Error::from) + rt.report_touched(entry.get_location()) } else { Ok(()) } @@ -203,7 +203,7 @@ fn hasnt<'a, 'e, I>(rt: &Runtime, mtch: &ArgMatches<'a>, iter: I) -> Result<()> if !rt.output_is_pipe() { writeln!(output, "{}", entry.get_location())?; } - rt.report_touched(entry.get_location()).map_err(Error::from) + rt.report_touched(entry.get_location()) } }) .collect() @@ -256,7 +256,7 @@ fn int<'a, 'e, I>(rt: &Runtime, mtch: &ArgMatches<'a>, iter: I) -> Result<()> } }) .filter_map_ok(|(b, e)| if b { Some(e) } else { None }) - .and_then_ok(|entry| rt.report_touched(entry.get_location()).map_err(Error::from)) + .and_then_ok(|entry| rt.report_touched(entry.get_location())) .collect() } @@ -294,7 +294,7 @@ fn float<'a, 'e, I>(rt: &Runtime, mtch: &ArgMatches<'a>, iter: I) -> Result<()> } }) .filter_map_ok(|(b, e)| if b { Some(e) } else { None }) - .and_then_ok(|entry| rt.report_touched(entry.get_location()).map_err(Error::from)) + .and_then_ok(|entry| rt.report_touched(entry.get_location())) .collect() } @@ -320,7 +320,7 @@ fn string<'a, 'e, I>(rt: &Runtime, mtch: &ArgMatches<'a>, iter: I) -> Result<()> } }) .filter_map_ok(|(b, e)| if b { Some(e) } else { None }) - .and_then_ok(|entry| rt.report_touched(entry.get_location()).map_err(Error::from)) + .and_then_ok(|entry| rt.report_touched(entry.get_location())) .collect() } @@ -342,7 +342,7 @@ fn boolean<'a, 'e, I>(rt: &Runtime, mtch: &ArgMatches<'a>, iter: I) -> Result<() } }) .filter_map_ok(|(b, e)| if b { Some(e) } else { None }) - .and_then_ok(|entry| rt.report_touched(entry.get_location()).map_err(Error::from)) + .and_then_ok(|entry| rt.report_touched(entry.get_location())) .collect() } diff --git a/bin/core/imag-id-in-collection/src/lib.rs b/bin/core/imag-id-in-collection/src/lib.rs index 9c75f54a..72a590bd 100644 --- a/bin/core/imag-id-in-collection/src/lib.rs +++ b/bin/core/imag-id-in-collection/src/lib.rs @@ -52,7 +52,6 @@ use std::io::Write; use filters::filter::Filter; use failure::Fallible as Result; -use failure::Error; use failure::err_msg; use clap::App; @@ -113,7 +112,7 @@ impl ImagApplication for ImagIdInCollection { writeln!(stdout, "{}", id)?; } - rt.report_touched(&id).map_err(Error::from) + rt.report_touched(&id) }) .collect() } diff --git a/bin/core/imag-ids/src/lib.rs b/bin/core/imag-ids/src/lib.rs index 43849462..621b1358 100644 --- a/bin/core/imag-ids/src/lib.rs +++ b/bin/core/imag-ids/src/lib.rs @@ -52,7 +52,6 @@ use std::io::Write; use failure::Fallible as Result; use failure::err_msg; -use failure::Error; use resiter::Map; use resiter::AndThen; use clap::App; @@ -92,7 +91,7 @@ impl ImagApplication for ImagIds { } } - rt.report_touched(&id).map_err(Error::from) + rt.report_touched(&id) }) .collect::>() }; diff --git a/bin/core/imag-link/src/lib.rs b/bin/core/imag-link/src/lib.rs index bba51892..47d75c93 100644 --- a/bin/core/imag-link/src/lib.rs +++ b/bin/core/imag-link/src/lib.rs @@ -72,7 +72,6 @@ use libimagstore::storeid::StoreId; use url::Url; use failure::Fallible as Result; -use failure::Error; use clap::App; mod ui; @@ -179,7 +178,7 @@ fn link_from_to<'a, I>(rt: &'a Runtime, from: &'a str, to: I) -> Result<()> info!("Ok: {} -> {}", from, entry); } - rt.report_touched(from_entry.get_location()).map_err(Error::from) + rt.report_touched(from_entry.get_location()) } fn remove_linking(rt: &Runtime) -> Result<()> { @@ -198,7 +197,7 @@ fn remove_linking(rt: &Runtime) -> Result<()> { .map(|id| match rt.store().get(id.clone())? { Some(mut to_entry) => { to_entry.remove_link(&mut from)?; - rt.report_touched(to_entry.get_location()).map_err(Error::from) + rt.report_touched(to_entry.get_location()) }, None => { @@ -217,7 +216,7 @@ fn remove_linking(rt: &Runtime) -> Result<()> { }) .collect::>>()?; - rt.report_touched(from.get_location()).map_err(Error::from) + rt.report_touched(from.get_location()) } fn unlink(rt: &Runtime) -> Result<()> { @@ -231,7 +230,7 @@ fn unlink(rt: &Runtime) -> Result<()> { .ok_or_else(|| format_err!("No entry for {}", id))? .unlink(rt.store())?; - rt.report_touched(&id).map_err(Error::from) + rt.report_touched(&id) }) .collect() } @@ -280,7 +279,7 @@ fn list_linkings(rt: &Runtime) -> Result<()> { .collect::>>()?; } - rt.report_touched(entry.get_location()).map_err(Error::from) + rt.report_touched(entry.get_location()) }) .collect::>>()?; diff --git a/bin/core/imag-ref/src/lib.rs b/bin/core/imag-ref/src/lib.rs index c5b5f982..c4b24db1 100644 --- a/bin/core/imag-ref/src/lib.rs +++ b/bin/core/imag-ref/src/lib.rs @@ -131,7 +131,7 @@ fn deref(rt: &Runtime) -> Result<()> { .ok_or_else(|| Error::from(::libimagerror::errors::ErrorMsg::UTF8Error)) .and_then(|s| writeln!(outlock, "{}", s).map_err(Error::from))?; - rt.report_touched(&id).map_err(Error::from) + rt.report_touched(&id) }, None => Err(format_err!("No entry for id '{}' found", id)) } @@ -193,7 +193,7 @@ fn list_dead(rt: &Runtime) -> Result<()> { unimplemented!() }?; - rt.report_touched(entry.get_location()).map_err(Error::from) + rt.report_touched(entry.get_location()) } else { Ok(()) } diff --git a/bin/core/imag-store/src/create.rs b/bin/core/imag-store/src/create.rs index a8989a28..3bb5681e 100644 --- a/bin/core/imag-store/src/create.rs +++ b/bin/core/imag-store/src/create.rs @@ -26,7 +26,6 @@ use std::io::Read; use clap::ArgMatches; use toml::Value; use failure::Fallible as Result; -use failure::Error; use failure::err_msg; use libimagrt::runtime::Runtime; @@ -59,7 +58,7 @@ pub fn create(rt: &Runtime) -> Result<()> { create_with_content_and_header(rt, &path, String::new(), Entry::default_header())?; } - rt.report_touched(&path).map_err(Error::from) + rt.report_touched(&path) } fn create_from_cli_spec(rt: &Runtime, matches: &ArgMatches, path: &StoreId) -> Result<()> { diff --git a/bin/core/imag-store/src/get.rs b/bin/core/imag-store/src/get.rs index b47fabc3..aa060fd1 100644 --- a/bin/core/imag-store/src/get.rs +++ b/bin/core/imag-store/src/get.rs @@ -20,7 +20,6 @@ use std::path::PathBuf; use failure::Fallible as Result; -use failure::Error; use failure::err_msg; use libimagrt::runtime::Runtime; @@ -40,7 +39,7 @@ pub fn get(rt: &Runtime) -> Result<()> { None => Err(err_msg("No entry found")), Some(entry) => { print_entry(rt, scmd, entry)?; - rt.report_touched(&path).map_err(Error::from) + rt.report_touched(&path) }, } } diff --git a/bin/core/imag-store/src/retrieve.rs b/bin/core/imag-store/src/retrieve.rs index 7ae516d7..511c9348 100644 --- a/bin/core/imag-store/src/retrieve.rs +++ b/bin/core/imag-store/src/retrieve.rs @@ -21,7 +21,6 @@ use std::path::PathBuf; use std::io::Write; use failure::Fallible as Result; -use failure::Error; use clap::ArgMatches; use libimagstore::store::FileLockEntry; @@ -39,7 +38,7 @@ pub fn retrieve(rt: &Runtime) -> Result<()> { .retrieve(path.clone()) .and_then(|e| print_entry(rt, scmd, e))?; - rt.report_touched(&path).map_err(Error::from) + rt.report_touched(&path) } pub fn print_entry(rt: &Runtime, scmd: &ArgMatches, e: FileLockEntry) -> Result<()> { diff --git a/bin/core/imag-store/src/update.rs b/bin/core/imag-store/src/update.rs index 8f2479fd..c1e2eecc 100644 --- a/bin/core/imag-store/src/update.rs +++ b/bin/core/imag-store/src/update.rs @@ -21,7 +21,6 @@ use std::ops::DerefMut; use std::path::PathBuf; use failure::Fallible as Result; -use failure::Error; use libimagrt::runtime::Runtime; use libimagstore::storeid::StoreId; @@ -49,7 +48,7 @@ pub fn update(rt: &Runtime) -> Result<()> { debug!("New header set"); } - rt.report_touched(locked_e.get_location()).map_err(Error::from) + rt.report_touched(locked_e.get_location()) }) } diff --git a/bin/core/imag-tag/src/lib.rs b/bin/core/imag-tag/src/lib.rs index 874ebf5a..3e931609 100644 --- a/bin/core/imag-tag/src/lib.rs +++ b/bin/core/imag-tag/src/lib.rs @@ -62,7 +62,6 @@ extern crate env_logger; use std::io::Write; use failure::Fallible as Result; -use failure::Error; use failure::err_msg; use resiter::AndThen; use resiter::Map; @@ -142,7 +141,7 @@ impl ImagApplication for ImagTag { } Ok(e) }) - .and_then_ok(|e| rt.report_touched(e.get_location()).map_err(Error::from)) + .and_then_ok(|e| rt.report_touched(e.get_location())) .collect::>>() .map(|_| ()) }, @@ -181,7 +180,7 @@ impl ImagApplication for ImagTag { } Ok(e) }) - .and_then_ok(|e| rt.report_touched(e.get_location()).map_err(Error::from)) + .and_then_ok(|e| rt.report_touched(e.get_location())) .collect::>>() .map(|_| ()) }, @@ -251,7 +250,7 @@ fn alter(rt: &Runtime, path: StoreId, add: Option>, rem: Option Result<()> { @@ -288,7 +287,7 @@ fn list(path: StoreId, rt: &Runtime) -> Result<()> { writeln!(rt.stdout(), "{}", tags.join(", "))?; } - rt.report_touched(&path).map_err(Error::from) + rt.report_touched(&path) } /// Get the tags which should be added from the commandline diff --git a/bin/core/imag-view/src/lib.rs b/bin/core/imag-view/src/lib.rs index d97d8711..b44f0283 100644 --- a/bin/core/imag-view/src/lib.rs +++ b/bin/core/imag-view/src/lib.rs @@ -56,7 +56,6 @@ use std::process::Command; use handlebars::Handlebars; use toml_query::read::TomlValueReadTypeExt; -use failure::Error; use failure::err_msg; use failure::Fallible as Result; use resiter::AndThen; @@ -186,7 +185,7 @@ impl ImagApplication for ImagView { viewer.view_entry(&entry, &mut outlock)?; i += 1; - rt.report_touched(entry.get_location()).map_err(Error::from) + rt.report_touched(entry.get_location()) }) .collect() } else { @@ -217,7 +216,7 @@ impl ImagApplication for ImagView { viewer.view_entry(&entry, &mut outlock)?; i += 1; - rt.report_touched(entry.get_location()).map_err(Error::from) + rt.report_touched(entry.get_location()) }) .collect() } diff --git a/bin/domain/imag-calendar/src/lib.rs b/bin/domain/imag-calendar/src/lib.rs index ff479757..ae0c8fdb 100644 --- a/bin/domain/imag-calendar/src/lib.rs +++ b/bin/domain/imag-calendar/src/lib.rs @@ -168,7 +168,7 @@ fn import(rt: &Runtime) -> Result<()> { .collect::>>()? .into_iter() .flatten() - .and_then_ok(|fle| rt.report_touched(fle.get_location()).map_err(Error::from)); + .and_then_ok(|fle| rt.report_touched(fle.get_location())); if do_fail { iter.collect() @@ -256,7 +256,7 @@ fn list(rt: &Runtime) -> Result<()> { }) .collect::>>()?; - rt.report_touched(parsed_entry.get_entry().get_location()).map_err(Error::from) + rt.report_touched(parsed_entry.get_entry().get_location()) }) .collect() } @@ -303,7 +303,7 @@ fn show(rt: &Runtime) -> Result<()> { }) .collect::>>()?; - rt.report_touched(parsed_entry.get_entry().get_location()).map_err(Error::from) + rt.report_touched(parsed_entry.get_entry().get_location()) }) .collect() } diff --git a/bin/domain/imag-todo/src/lib.rs b/bin/domain/imag-todo/src/lib.rs index 96ff913f..9af20181 100644 --- a/bin/domain/imag-todo/src/lib.rs +++ b/bin/domain/imag-todo/src/lib.rs @@ -222,7 +222,7 @@ fn create(rt: &Runtime) -> Result<()> { entry.edit_content(&rt)?; } - rt.report_touched(entry.get_location()).map_err(Error::from) + rt.report_touched(entry.get_location()) } fn mark(rt: &Runtime) -> Result<()> { @@ -233,7 +233,7 @@ fn mark(rt: &Runtime) -> Result<()> { .map(Ok) .into_get_iter(rt.store()) .map_inner_ok_or_else(|| err_msg("Did not find one entry")) - .and_then_ok(|e| rt.report_touched(e.get_location()).map_err(Error::from).map(|_| e)) + .and_then_ok(|e| rt.report_touched(e.get_location()).map(|_| e)) .and_then_ok(|mut e| e.set_status(status.clone())) .collect() } @@ -339,7 +339,7 @@ fn list_todos(rt: &Runtime, matcher: &StatusMatcher, show_hidden: bool) -> Resul } } - rt.report_touched(entry.get_location()).map_err(Error::from) + rt.report_touched(entry.get_location()) }) .collect() }; @@ -453,7 +453,7 @@ fn show(rt: &Runtime) -> Result<()> { .map(Ok) .into_get_iter(rt.store()) .map_inner_ok_or_else(|| err_msg("Did not find one entry")) - .and_then_ok(|e| rt.report_touched(e.get_location()).map_err(Error::from).map(|_| e)) + .and_then_ok(|e| rt.report_touched(e.get_location()).map(|_| e)) .collect::>>()? .into_iter(); -- cgit v1.2.3