From 7ce74ce206065c3af0035583330b3c773086f21c Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Thu, 3 Nov 2016 00:44:53 +0300 Subject: Invent auto_fd, make use of it in fdstreams and process --- butl/process.cxx | 92 +++++++++++--------------------------------------------- 1 file changed, 18 insertions(+), 74 deletions(-) (limited to 'butl/process.cxx') diff --git a/butl/process.cxx b/butl/process.cxx index 3af42cf..d8b10be 100644 --- a/butl/process.cxx +++ b/butl/process.cxx @@ -37,10 +37,11 @@ #include #include // size_t #include // strlen(), strchr() +#include // move() #include #include // casecmp() -#include // fdnull(), fdclose() +#include // fdnull() using namespace std; @@ -50,49 +51,6 @@ using namespace butl::win32; namespace butl { - class auto_fd - { - public: - explicit - auto_fd (int fd = -1) noexcept: fd_ (fd) {} - - auto_fd (const auto_fd&) = delete; - auto_fd& operator= (const auto_fd&) = delete; - - ~auto_fd () noexcept {reset ();} - - int - get () const noexcept {return fd_;} - - int - release () noexcept - { - int r (fd_); - fd_ = -1; - return r; - } - - void - reset (int fd = -1) noexcept - { - if (fd_ != -1) - { - bool r (fdclose (fd_)); - - // The valid file descriptor that has no IO operations being - // performed on it should close successfully, unless something is - // severely damaged. - // - assert (r); - } - - fd_ = fd; - } - - private: - int fd_; - }; - static process_path path_search (const char*, const dir_path&); @@ -158,6 +116,16 @@ namespace butl } while (*p != nullptr); } + process:: + process (const char* cwd, + const process_path& pp, const char* args[], + process& in, int out, int err) + : process (cwd, pp, args, in.in_ofd.get (), out, err) + { + assert (in.in_ofd.get () != -1); // Should be a pipe. + in.in_ofd.reset (); // Close it on our side. + } + #ifndef _WIN32 static process_path @@ -365,19 +333,9 @@ namespace butl assert (handle != 0); // Shouldn't get here unless in the parent process. - this->out_fd = out_fd[1].release (); - this->in_ofd = in_ofd[0].release (); - this->in_efd = in_efd[0].release (); - } - - process:: - process (const char* cwd, - const process_path& pp, const char* args[], - process& in, int out, int err) - : process (cwd, pp, args, in.in_ofd, out, err) - { - assert (in.in_ofd != -1); // Should be a pipe. - close (in.in_ofd); // Close it on our side. + this->out_fd = move (out_fd[1]); + this->in_ofd = move (in_ofd[0]); + this->in_efd = move (in_efd[0]); } bool process:: @@ -849,13 +807,9 @@ namespace butl return fd; }; - auto_fd out_fd (in == -1 ? open_osfhandle (out_h[1]) : -1); - auto_fd in_ofd (out == -1 ? open_osfhandle (in_oh[0]) : -1); - auto_fd in_efd (err == -1 ? open_osfhandle (in_eh[0]) : -1); - - this->out_fd = out_fd.release (); - this->in_ofd = in_ofd.release (); - this->in_efd = in_efd.release (); + this->out_fd.reset (in == -1 ? open_osfhandle (out_h[1]) : -1); + this->in_ofd.reset (out == -1 ? open_osfhandle (in_oh[0]) : -1); + this->in_efd.reset (err == -1 ? open_osfhandle (in_eh[0]) : -1); this->handle = process.release (); @@ -864,16 +818,6 @@ namespace butl assert (this->handle != 0 && this->handle != INVALID_HANDLE_VALUE); } - process:: - process (const char* cwd, - const process_path& pp, const char* args[], - process& in, int out, int err) - : process (cwd, pp, args, in.in_ofd, out, err) - { - assert (in.in_ofd != -1); // Should be a pipe. - _close (in.in_ofd); // Close it on our side. - } - bool process:: wait (bool ie) { -- cgit v1.1