From b8526f8f0cb24c457da8c88877e516943e3e1902 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Mon, 24 Apr 2017 13:51:56 +0300 Subject: Make fdnull() to return auto_fd --- butl/process.cxx | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) (limited to 'butl/process.cxx') diff --git a/butl/process.cxx b/butl/process.cxx index 850ba9b..c427aa8 100644 --- a/butl/process.cxx +++ b/butl/process.cxx @@ -286,13 +286,26 @@ namespace butl } }; - auto open_null = [&fail] () -> auto_fd + auto open_null = [] () -> auto_fd { - auto_fd fd (fdnull ()); - if (fd.get () == -1) - fail (false); + try + { + return fdnull (); + } + catch (const ios_base::failure& e) + { + // Translate to process_error. + // + // For old versions of g++ (as of 4.9) ios_base::failure is not derived + // from system_error and so we cannot recover the errno value. Lets use + // EIO in this case. This is a temporary code after all. + // + const system_error* se (dynamic_cast (&e)); - return fd; + throw process_error (se != nullptr + ? se->code ().value () + : EIO); + } }; // If we are asked to open null (-2) then open "half-pipe". @@ -940,17 +953,30 @@ namespace butl throw process_error (m == nullptr ? last_error_msg () : m); }; - auto open_null = [&fail] () -> auto_fd + auto open_null = [] () -> auto_fd { // Note that we are using a faster, temporary file-based emulation of // NUL since we have no way of making sure the child buffers things // properly (and by default they seem no to). // - auto_fd fd (fdnull (true)); - if (fd.get () == -1) - fail (); + try + { + return fdnull (true); + } + catch (const ios_base::failure& e) + { + // Translate to process_error. + // + // For old versions of g++ (as of 4.9) ios_base::failure is not derived + // from system_error and so we cannot recover the errno value. Lets use + // EIO in this case. This is a temporary code after all. + // + const system_error* se (dynamic_cast (&e)); - return fd; + throw process_error (se != nullptr + ? se->code ().value () + : EIO); + } }; // If we are asked to open null (-2) then open "half-pipe". -- cgit v1.1