1*11be35a1SLionel Sambuc // Copyright 2010 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc // documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc // may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc // without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc
29*11be35a1SLionel Sambuc #include "utils/process/systembuf.hpp"
30*11be35a1SLionel Sambuc
31*11be35a1SLionel Sambuc extern "C" {
32*11be35a1SLionel Sambuc #include <unistd.h>
33*11be35a1SLionel Sambuc }
34*11be35a1SLionel Sambuc
35*11be35a1SLionel Sambuc #include "utils/auto_array.ipp"
36*11be35a1SLionel Sambuc #include "utils/sanity.hpp"
37*11be35a1SLionel Sambuc
38*11be35a1SLionel Sambuc using utils::process::systembuf;
39*11be35a1SLionel Sambuc
40*11be35a1SLionel Sambuc
41*11be35a1SLionel Sambuc /// Private implementation fields for systembuf.
42*11be35a1SLionel Sambuc struct systembuf::impl {
43*11be35a1SLionel Sambuc /// File descriptor attached to the systembuf.
44*11be35a1SLionel Sambuc int _fd;
45*11be35a1SLionel Sambuc
46*11be35a1SLionel Sambuc /// Size of the _read_buf and _write_buf buffers.
47*11be35a1SLionel Sambuc std::size_t _bufsize;
48*11be35a1SLionel Sambuc
49*11be35a1SLionel Sambuc /// In-memory buffer for read operations.
50*11be35a1SLionel Sambuc utils::auto_array< char > _read_buf;
51*11be35a1SLionel Sambuc
52*11be35a1SLionel Sambuc /// In-memory buffer for write operations.
53*11be35a1SLionel Sambuc utils::auto_array< char > _write_buf;
54*11be35a1SLionel Sambuc
55*11be35a1SLionel Sambuc /// Initializes private implementation data.
56*11be35a1SLionel Sambuc ///
57*11be35a1SLionel Sambuc /// \param fd The file descriptor.
58*11be35a1SLionel Sambuc /// \param bufsize The size of the created read and write buffers.
implsystembuf::impl59*11be35a1SLionel Sambuc impl(const int fd, const std::size_t bufsize) :
60*11be35a1SLionel Sambuc _fd(fd),
61*11be35a1SLionel Sambuc _bufsize(bufsize),
62*11be35a1SLionel Sambuc _read_buf(new char[bufsize]),
63*11be35a1SLionel Sambuc _write_buf(new char[bufsize])
64*11be35a1SLionel Sambuc {
65*11be35a1SLionel Sambuc }
66*11be35a1SLionel Sambuc };
67*11be35a1SLionel Sambuc
68*11be35a1SLionel Sambuc
69*11be35a1SLionel Sambuc /// Constructs a new systembuf based on an open file descriptor.
70*11be35a1SLionel Sambuc ///
71*11be35a1SLionel Sambuc /// This grabs ownership of the file descriptor.
72*11be35a1SLionel Sambuc ///
73*11be35a1SLionel Sambuc /// \param fd The file descriptor to wrap. Must be open and valid.
74*11be35a1SLionel Sambuc /// \param bufsize The size to use for the internal read/write buffers.
systembuf(const int fd,std::size_t bufsize)75*11be35a1SLionel Sambuc systembuf::systembuf(const int fd, std::size_t bufsize) :
76*11be35a1SLionel Sambuc _pimpl(new impl(fd, bufsize))
77*11be35a1SLionel Sambuc {
78*11be35a1SLionel Sambuc setp(_pimpl->_write_buf.get(), _pimpl->_write_buf.get() + _pimpl->_bufsize);
79*11be35a1SLionel Sambuc }
80*11be35a1SLionel Sambuc
81*11be35a1SLionel Sambuc
82*11be35a1SLionel Sambuc /// Destroys a systembuf object.
83*11be35a1SLionel Sambuc ///
84*11be35a1SLionel Sambuc /// \post The file descriptor attached to this systembuf is closed.
~systembuf(void)85*11be35a1SLionel Sambuc systembuf::~systembuf(void)
86*11be35a1SLionel Sambuc {
87*11be35a1SLionel Sambuc ::close(_pimpl->_fd);
88*11be35a1SLionel Sambuc }
89*11be35a1SLionel Sambuc
90*11be35a1SLionel Sambuc
91*11be35a1SLionel Sambuc /// Reads new data when the systembuf read buffer underflows.
92*11be35a1SLionel Sambuc ///
93*11be35a1SLionel Sambuc /// \return The new character to be read, or EOF if no more.
94*11be35a1SLionel Sambuc systembuf::int_type
underflow(void)95*11be35a1SLionel Sambuc systembuf::underflow(void)
96*11be35a1SLionel Sambuc {
97*11be35a1SLionel Sambuc PRE(gptr() >= egptr());
98*11be35a1SLionel Sambuc
99*11be35a1SLionel Sambuc bool ok;
100*11be35a1SLionel Sambuc ssize_t cnt = ::read(_pimpl->_fd, _pimpl->_read_buf.get(),
101*11be35a1SLionel Sambuc _pimpl->_bufsize);
102*11be35a1SLionel Sambuc ok = (cnt != -1 && cnt != 0);
103*11be35a1SLionel Sambuc
104*11be35a1SLionel Sambuc if (!ok)
105*11be35a1SLionel Sambuc return traits_type::eof();
106*11be35a1SLionel Sambuc else {
107*11be35a1SLionel Sambuc setg(_pimpl->_read_buf.get(), _pimpl->_read_buf.get(),
108*11be35a1SLionel Sambuc _pimpl->_read_buf.get() + cnt);
109*11be35a1SLionel Sambuc return traits_type::to_int_type(*gptr());
110*11be35a1SLionel Sambuc }
111*11be35a1SLionel Sambuc }
112*11be35a1SLionel Sambuc
113*11be35a1SLionel Sambuc
114*11be35a1SLionel Sambuc /// Writes data to the file descriptor when the write buffer overflows.
115*11be35a1SLionel Sambuc ///
116*11be35a1SLionel Sambuc /// \param c The character that causes the overflow.
117*11be35a1SLionel Sambuc ///
118*11be35a1SLionel Sambuc /// \return EOF if error, some other value for success.
119*11be35a1SLionel Sambuc ///
120*11be35a1SLionel Sambuc /// \throw something TODO(jmmv): According to the documentation, it is OK for
121*11be35a1SLionel Sambuc /// this method to throw in case of errors. Revisit this code to see if we
122*11be35a1SLionel Sambuc /// can do better.
123*11be35a1SLionel Sambuc systembuf::int_type
overflow(int c)124*11be35a1SLionel Sambuc systembuf::overflow(int c)
125*11be35a1SLionel Sambuc {
126*11be35a1SLionel Sambuc PRE(pptr() >= epptr());
127*11be35a1SLionel Sambuc if (sync() == -1)
128*11be35a1SLionel Sambuc return traits_type::eof();
129*11be35a1SLionel Sambuc if (!traits_type::eq_int_type(c, traits_type::eof())) {
130*11be35a1SLionel Sambuc traits_type::assign(*pptr(), c);
131*11be35a1SLionel Sambuc pbump(1);
132*11be35a1SLionel Sambuc }
133*11be35a1SLionel Sambuc return traits_type::not_eof(c);
134*11be35a1SLionel Sambuc }
135*11be35a1SLionel Sambuc
136*11be35a1SLionel Sambuc
137*11be35a1SLionel Sambuc /// Synchronizes the stream with the file descriptor.
138*11be35a1SLionel Sambuc ///
139*11be35a1SLionel Sambuc /// \return 0 on success, -1 on error.
140*11be35a1SLionel Sambuc int
sync(void)141*11be35a1SLionel Sambuc systembuf::sync(void)
142*11be35a1SLionel Sambuc {
143*11be35a1SLionel Sambuc ssize_t cnt = pptr() - pbase();
144*11be35a1SLionel Sambuc
145*11be35a1SLionel Sambuc bool ok;
146*11be35a1SLionel Sambuc ok = ::write(_pimpl->_fd, pbase(), cnt) == cnt;
147*11be35a1SLionel Sambuc
148*11be35a1SLionel Sambuc if (ok)
149*11be35a1SLionel Sambuc pbump(-cnt);
150*11be35a1SLionel Sambuc return ok ? 0 : -1;
151*11be35a1SLionel Sambuc }
152