diff options
author | 2006-07-24 17:47:58 +0000 | |
---|---|---|
committer | 2006-07-24 17:47:58 +0000 | |
commit | 52112ae6d0542c1d77e306ea0418565575bc20c6 (patch) | |
tree | d917a123c421a159a6d34166094172d917ea14b7 | |
parent | 1c1bda20ea6669dc22cbbd48a68309d7366b5502 (diff) | |
download | paludis-52112ae6d0542c1d77e306ea0418565575bc20c6.tar.gz paludis-52112ae6d0542c1d77e306ea0418565575bc20c6.tar.xz |
Unify fd output streams and pipe handling
-rw-r--r-- | doc/doc_main.doxygen | 10 | ||||
-rw-r--r-- | ebuild/utils/merge.cc | 71 | ||||
-rw-r--r-- | paludis/util/fd_output_stream.hh | 102 | ||||
-rw-r--r-- | paludis/util/files.m4 | 2 | ||||
-rw-r--r-- | paludis/util/pipe.cc | 37 | ||||
-rw-r--r-- | paludis/util/pipe.hh | 62 | ||||
-rw-r--r-- | src/gtkpaludis/message_window.cc | 136 |
7 files changed, 235 insertions, 185 deletions
diff --git a/doc/doc_main.doxygen b/doc/doc_main.doxygen index d165bc5..a23f767 100644 --- a/doc/doc_main.doxygen +++ b/doc/doc_main.doxygen @@ -221,6 +221,16 @@ * \ingroup grplibpaludisutil */ +/** \defgroup grppipe Pipes + * + * \ingroup grplibpaludisutil + */ + +/** \defgroup grpfdotputstream FD Output + * + * \ingroup grplibpaludisutil + */ + /** \defgroup grppointers Pointers * * \ingroup grplibpaludisutil diff --git a/ebuild/utils/merge.cc b/ebuild/utils/merge.cc index 23cb64a..af8083b 100644 --- a/ebuild/utils/merge.cc +++ b/ebuild/utils/merge.cc @@ -21,6 +21,7 @@ #include <paludis/digests/md5.hh> #include <paludis/util/dir_iterator.hh> +#include <paludis/util/fd_output_stream.hh> #include <paludis/util/fs_entry.hh> #include <paludis/util/log.hh> #include <paludis/util/pstream.hh> @@ -96,76 +97,6 @@ namespace } /** - * Output stream buffer class that's opened via an FD, so that we can avoid race - * conditions that would be present on the non-Unix-specific standard file - * stream classes. - * - * See \ref TCppSL Ch. 13.13 for what we're doing here. The buffer code is - * based upon the "io/outbuf2.hpp" example in section 13.13.3. - */ - class FDOutputStreamBuf : - public std::streambuf - { - protected: - int fd; - - virtual int_type - overflow(int_type c) - { - if (c != EOF) - { - char z = c; - if (1 != write(fd, &z, 1)) - return EOF; - } - return c; - } - - virtual std::streamsize - xsputn(const char * s, std::streamsize num) - { - return write(fd, s, num); - } - - public: - FDOutputStreamBuf(const int f) : - fd(f) - { - } - }; - - /** - * Member from base initialisation for FDOutputStream. - */ - class FDOutputStreamBase - { - protected: - FDOutputStreamBuf buf; - - public: - FDOutputStreamBase(const int fd) : - buf(fd) - { - } - }; - /** - * Output stream buffer class that's opened via an FD, so that we can avoid race - * conditions that would be present on the non-Unix-specific standard file - * stream classes. - */ - class FDOutputStream : - protected FDOutputStreamBase, - public std::ostream - { - public: - FDOutputStream(const int fd) : - FDOutputStreamBase(fd), - std::ostream(&buf) - { - } - }; - - /** * RAII holder for a file descriptor. */ class FDHolder diff --git a/paludis/util/fd_output_stream.hh b/paludis/util/fd_output_stream.hh new file mode 100644 index 0000000..48babfe --- /dev/null +++ b/paludis/util/fd_output_stream.hh @@ -0,0 +1,102 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2006 Ciaran McCreesh <ciaran.mccreesh@blueyonder.co.uk> + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef PALUDIS_GUARD_PALUDIS_UTIL_FD_OUTPUT_STREAM_HH +#define PALUDIS_GUARD_PALUDIS_UTIL_FD_OUTPUT_STREAM_HH 1 + +#include <ostream> +#include <unistd.h> + +namespace paludis +{ + /** + * Output stream buffer class that's opened via an FD. + * + * See \ref TCppSL Ch. 13.13 for what we're doing here. The buffer code is + * based upon the "io/outbuf2.hpp" example in section 13.13.3. + * + * \ingroup grpfdotputstream + */ + class FDOutputStreamBuf : + public std::streambuf + { + protected: + int fd; + + virtual int_type + overflow(int_type c) + { + if (c != EOF) + { + char z = c; + if (1 != write(fd, &z, 1)) + return EOF; + } + return c; + } + + virtual std::streamsize + xsputn(const char * s, std::streamsize num) + { + return write(fd, s, num); + } + + public: + FDOutputStreamBuf(const int f) : + fd(f) + { + } + }; + + /** + * Member from base initialisation for FDOutputStream. + * + * \ingroup grpfdotputstream + */ + class FDOutputStreamBase + { + protected: + FDOutputStreamBuf buf; + + public: + FDOutputStreamBase(const int fd) : + buf(fd) + { + } + }; + + /** + * Output stream buffer class that's opened via an FD. + * + * \ingroup grpfdotputstream + */ + class FDOutputStream : + protected FDOutputStreamBase, + public std::ostream + { + public: + FDOutputStream(const int fd) : + FDOutputStreamBase(fd), + std::ostream(&buf) + { + } + }; +} + +#endif diff --git a/paludis/util/files.m4 b/paludis/util/files.m4 index 495514d..91e14e2 100644 --- a/paludis/util/files.m4 +++ b/paludis/util/files.m4 @@ -17,12 +17,14 @@ add(`counted_ptr', `hh', `cc', `test') add(`destringify', `hh', `cc', `test') add(`dir_iterator', `hh', `cc', `test', `testscript') add(`exception', `hh', `cc') +add(`fd_output_stream', `hh') add(`fs_entry', `hh', `cc', `test', `testscript') add(`iterator', `hh', `test') add(`instantiation_policy', `hh', `test') add(`is_file_with_extension', `hh', `cc', `test', `testscript') add(`join', `hh', `test') add(`log', `hh', `cc', `test') +add(`pipe', `hh', `cc') add(`private_implementation_pattern', `hh') add(`pstream', `hh', `cc', `test') add(`random', `hh', `cc', `test') diff --git a/paludis/util/pipe.cc b/paludis/util/pipe.cc new file mode 100644 index 0000000..5cb0c33 --- /dev/null +++ b/paludis/util/pipe.cc @@ -0,0 +1,37 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2006 Ciaran McCreesh <ciaran.mccreesh@blueyonder.co.uk> + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "pipe.hh" +#include <paludis/util/exception.hh> +#include <unistd.h> + +using namespace paludis; + +Pipe::Pipe() +{ + if (-1 == pipe(_fds)) + throw InternalError(PALUDIS_HERE, "pipe(2) failed"); +} + +Pipe::~Pipe() +{ + close(_fds[0]); + close(_fds[1]); +} + diff --git a/paludis/util/pipe.hh b/paludis/util/pipe.hh new file mode 100644 index 0000000..931dca8 --- /dev/null +++ b/paludis/util/pipe.hh @@ -0,0 +1,62 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2006 Ciaran McCreesh <ciaran.mccreesh@blueyonder.co.uk> + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef PALUDIS_GUARD_PALUDIS_UTIL_PIPE_HH +#define PALUDIS_GUARD_PALUDIS_UTIL_PIPE_HH 1 + +#include <paludis/util/instantiation_policy.hh> + +/** \file + * Declaration for the Pipe class. + * + * \ingroup grppipe + */ + +namespace paludis +{ + /** + * Wrapper around pipe file descriptors. + * + * \ingroup grppipe + */ + class Pipe : + InstantiationPolicy<Pipe, instantiation_method::NonCopyableTag> + { + private: + int _fds[2]; + + public: + Pipe(); + + ~Pipe(); + + int read_fd() const + { + return _fds[0]; + } + + int write_fd() const + { + return _fds[1]; + } + }; + +} + +#endif diff --git a/src/gtkpaludis/message_window.cc b/src/gtkpaludis/message_window.cc index 667e69b..f8dc2ef 100644 --- a/src/gtkpaludis/message_window.cc +++ b/src/gtkpaludis/message_window.cc @@ -19,112 +19,19 @@ #include "message_window.hh" #include <gtkmm/main.h> +#include <paludis/util/fd_output_stream.hh> #include <paludis/util/log.hh> +#include <paludis/util/pipe.hh> +#include <paludis/util/system.hh> #include <unistd.h> #include <fcntl.h> +#include <sys/utsname.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <errno.h> using namespace paludis; -namespace -{ - class Pipe : - InstantiationPolicy<Pipe, instantiation_method::NonCopyableTag> - { - private: - int _fds[2]; - - public: - Pipe() - { - if (-1 == pipe(_fds)) - throw InternalError(PALUDIS_HERE, "pipe(2) failed"); - } - - ~Pipe() - { - close(_fds[0]); - close(_fds[1]); - } - - int read_fd() const - { - return _fds[0]; - } - - int write_fd() const - { - return _fds[1]; - } - }; - - /** - * Output stream buffer class that's opened via an FD. - * - * See \ref TCppSL Ch. 13.13 for what we're doing here. The buffer code is - * based upon the "io/outbuf2.hpp" example in section 13.13.3. - */ - class FDOutputStreamBuf : - public std::streambuf - { - protected: - int fd; - - virtual int_type - overflow(int_type c) - { - if (c != EOF) - { - char z = c; - if (1 != write(fd, &z, 1)) - return EOF; - } - return c; - } - - virtual std::streamsize - xsputn(const char * s, std::streamsize num) - { - return write(fd, s, num); - } - - public: - FDOutputStreamBuf(const int f) : - fd(f) - { - } - }; - - /** - * Member from base initialisation for FDOutputStream. - */ - class FDOutputStreamBase - { - protected: - FDOutputStreamBuf buf; - - public: - FDOutputStreamBase(const int fd) : - buf(fd) - { - } - }; - - /** - * Output stream buffer class that's opened via an FD. - */ - class FDOutputStream : - protected FDOutputStreamBase, - public std::ostream - { - public: - FDOutputStream(const int fd) : - FDOutputStreamBase(fd), - std::ostream(&buf) - { - } - }; -} - namespace paludis { template<> @@ -132,21 +39,25 @@ namespace paludis InternalCounted<Implementation<MessageWindow> > { Pipe log_pipe; + MessageWindow * const owner; Glib::RefPtr<Glib::IOChannel> log_connection; FDOutputStream stream; - Implementation(MessageWindow * const o) : - owner(o), - log_connection(Glib::IOChannel::create_from_fd(log_pipe.read_fd())), - stream(log_pipe.write_fd()) - { - Glib::signal_io().connect(sigc::mem_fun(*owner, &MessageWindow::on_log_read), - log_pipe.read_fd(), Glib::IO_IN); - Log::get_instance()->set_log_stream(&stream); - Log::get_instance()->message(ll_debug, lc_no_context, "Message window initialised"); - } + Implementation(MessageWindow * const o); }; + + Implementation<MessageWindow>::Implementation(MessageWindow * const o) : + owner(o), + log_connection(Glib::IOChannel::create_from_fd(log_pipe.read_fd())), + stream(log_pipe.write_fd()) + { + Glib::signal_io().connect(sigc::mem_fun(*owner, &MessageWindow::on_log_read), + log_pipe.read_fd(), Glib::IO_IN); + + Log::get_instance()->set_log_stream(&stream); + Log::get_instance()->message(ll_debug, lc_no_context, "Message window initialised"); + } } MessageWindow::MessageWindow() : @@ -166,11 +77,6 @@ MessageWindow::on_log_read(Glib::IOCondition io_condition) return false; Glib::ustring buf; - -#ifndef GLIBMM_EXCEPTIONS_ENABLED -# error Your glibcc hates us -#endif - _imp->log_connection->read_line(buf); get_buffer()->insert(get_buffer()->end(), buf); |