diff --git a/exec.cpp b/exec.cpp index 1ade232e4..e4dd5f61c 100644 --- a/exec.cpp +++ b/exec.cpp @@ -332,20 +332,11 @@ static int has_fd(const io_chain_t &d, int fd) } /** - Free a transmogrified io chain. Only the chain itself and resources - used by a transmogrified IO_FILE redirection are freed, since the - original chain may still be needed. + Close a list of fds. */ -static void io_cleanup_chains(io_chain_t &chains, const std::vector &opened_fds) +static void io_cleanup_fds(const std::vector &opened_fds) { - /* Close all the fds */ - for (size_t idx = 0; idx < opened_fds.size(); idx++) - { - close(opened_fds.at(idx)); - } - - /* Then delete all of the redirections we made */ - chains.destroy(); + std::for_each(opened_fds.begin(), opened_fds.end(), close); } /** @@ -446,7 +437,8 @@ static bool io_transmogrify(const io_chain_t &in_chain, io_chain_t &out_chain, s else { /* No dice - clean up */ - io_cleanup_chains(result_chain, opened_fds); + result_chain.clear(); + io_cleanup_fds(opened_fds); } return success; } @@ -488,7 +480,8 @@ static void internal_exec_helper(parser_t &parser, signal_block(); - io_cleanup_chains(morphed_chain, opened_fds); + morphed_chain.clear(); + io_cleanup_fds(opened_fds); job_reap(0); is_block=is_block_old; } @@ -563,7 +556,7 @@ void exec(parser_t &parser, job_t *j) if (! parser.block_io.empty()) { - io_duplicate_prepend(parser.block_io, j->io); + j->io.insert(j->io.begin(), parser.block_io.begin(), parser.block_io.end()); } const io_buffer_t *input_redirect = 0; diff --git a/io.cpp b/io.cpp index dfda6e55a..f6947cb21 100644 --- a/io.cpp +++ b/io.cpp @@ -135,7 +135,6 @@ io_buffer_t *io_buffer_t::create(bool is_input) { bool success = true; io_buffer_t *buffer_redirect = new io_buffer_t(is_input ? 0 : 1, is_input); - buffer_redirect->out_buffer_create(); if (exec_pipe(buffer_redirect->pipe_fd) == -1) { @@ -194,17 +193,6 @@ void io_chain_t::remove(const shared_ptr &element) } } -void io_chain_t::duplicate_prepend(const io_chain_t &src) -{ - /* Prepend a duplicate of src before this. */ - this->insert(this->begin(), src.begin(), src.end()); -} - -void io_chain_t::destroy() -{ - this->clear(); -} - void io_remove(io_chain_t &list, const shared_ptr &element) { list.remove(element); @@ -227,16 +215,6 @@ void io_print(const io_chain_t &chain) } } -void io_duplicate_prepend(const io_chain_t &src, io_chain_t &dst) -{ - return dst.duplicate_prepend(src); -} - -void io_chain_destroy(io_chain_t &chain) -{ - chain.destroy(); -} - /* Return the last IO for the given fd */ shared_ptr io_chain_t::get_io_for_fd(int fd) const { diff --git a/io.h b/io.h index bfe910e33..8cb33d428 100644 --- a/io.h +++ b/io.h @@ -71,22 +71,15 @@ class io_file_t : public io_data_t { public: /** Filename, malloc'd. This needs to be used after fork, so don't use wcstring here. */ - const char *filename_cstr; + const char * const filename_cstr; /** file creation flags to send to open */ int flags; - - /** Convenience to set filename_cstr via wcstring */ - void set_filename(const wcstring &str) - { - free((void *)filename_cstr); - filename_cstr = wcs2str(str.c_str()); - } - + virtual void print() const; - io_file_t(int f, const char *fname = NULL, int fl = 0) : + io_file_t(int f, const wcstring &fname, int fl = 0) : io_data_t(IO_FILE, f), - filename_cstr(fname ? strdup(fname) : NULL), + filename_cstr(wcs2str(fname)), flags(fl) { } @@ -125,7 +118,7 @@ class io_buffer_t : public io_pipe_t { private: /** buffer to save output in */ - shared_ptr > out_buffer; + std::vector out_buffer; io_buffer_t(int f, bool i): io_pipe_t(IO_BUFFER, f, i), @@ -138,37 +131,27 @@ public: virtual ~io_buffer_t(); - /** Function to create the output buffer */ - void out_buffer_create() - { - out_buffer.reset(new std::vector); - } - /** Function to append to the buffer */ void out_buffer_append(const char *ptr, size_t count) { - assert(out_buffer.get() != NULL); - out_buffer->insert(out_buffer->end(), ptr, ptr + count); + out_buffer.insert(out_buffer.end(), ptr, ptr + count); } /** Function to get a pointer to the buffer */ char *out_buffer_ptr(void) { - assert(out_buffer.get() != NULL); - return out_buffer->empty() ? NULL : &out_buffer->at(0); + return out_buffer.empty() ? NULL : &out_buffer.at(0); } const char *out_buffer_ptr(void) const { - assert(out_buffer.get() != NULL); - return out_buffer->empty() ? NULL : &out_buffer->at(0); + return out_buffer.empty() ? NULL : &out_buffer.at(0); } /** Function to get the size of the buffer */ size_t out_buffer_size(void) const { - assert(out_buffer.get() != NULL); - return out_buffer->size(); + return out_buffer.size(); } /** @@ -195,9 +178,6 @@ public: io_chain_t(const shared_ptr &); void remove(const shared_ptr &element); - io_chain_t duplicate() const; - void duplicate_prepend(const io_chain_t &src); - void destroy(); shared_ptr get_io_for_fd(int fd) const; shared_ptr get_io_for_fd(int fd); @@ -209,12 +189,6 @@ public: */ void io_remove(io_chain_t &list, const shared_ptr &element); -/** Return a shallow copy of the specified chain of redirections that contains only the applicable redirections. That is, if there's multiple redirections for the same fd, only the second one is included. */ -io_chain_t io_unique(const io_chain_t &chain); - -/** Prepends a copy of the specified 'src' chain of redirections to 'dst.' Uses operator new. */ -void io_duplicate_prepend(const io_chain_t &src, io_chain_t &dst); - /** Destroys an io_chain */ void io_chain_destroy(io_chain_t &chain); diff --git a/parser.cpp b/parser.cpp index f2ed3f2d7..11b3745f9 100644 --- a/parser.cpp +++ b/parser.cpp @@ -1619,8 +1619,7 @@ void parser_t::parse_job_argument_list(process_t *p, break; } - io_file_t *new_io_file = new io_file_t(fd, NULL, flags); - new_io_file->set_filename(target); + io_file_t *new_io_file = new io_file_t(fd, target, flags); new_io.reset(new_io_file); } } diff --git a/proc.cpp b/proc.cpp index 87c8f362f..7f1b86a94 100644 --- a/proc.cpp +++ b/proc.cpp @@ -544,7 +544,6 @@ job_t::~job_t() { if (first_process != NULL) delete first_process; - io_chain_destroy(this->io); release_job_id(job_id); }