1 // Copyright 2010 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "utils/process/systembuf.hpp"
35 #include "utils/auto_array.ipp"
36 #include "utils/noncopyable.hpp"
37 #include "utils/sanity.hpp"
39 using utils::process::systembuf;
42 /// Private implementation fields for systembuf.
43 struct systembuf::impl : utils::noncopyable {
44 /// File descriptor attached to the systembuf.
47 /// Size of the _read_buf and _write_buf buffers.
50 /// In-memory buffer for read operations.
51 utils::auto_array< char > _read_buf;
53 /// In-memory buffer for write operations.
54 utils::auto_array< char > _write_buf;
56 /// Initializes private implementation data.
58 /// \param fd The file descriptor.
59 /// \param bufsize The size of the created read and write buffers.
60 impl(const int fd, const std::size_t bufsize) :
63 _read_buf(new char[bufsize]),
64 _write_buf(new char[bufsize])
70 /// Constructs a new systembuf based on an open file descriptor.
72 /// This grabs ownership of the file descriptor.
74 /// \param fd The file descriptor to wrap. Must be open and valid.
75 /// \param bufsize The size to use for the internal read/write buffers.
76 systembuf::systembuf(const int fd, std::size_t bufsize) :
77 _pimpl(new impl(fd, bufsize))
79 setp(_pimpl->_write_buf.get(), _pimpl->_write_buf.get() + _pimpl->_bufsize);
83 /// Destroys a systembuf object.
85 /// \post The file descriptor attached to this systembuf is closed.
86 systembuf::~systembuf(void)
92 /// Reads new data when the systembuf read buffer underflows.
94 /// \return The new character to be read, or EOF if no more.
96 systembuf::underflow(void)
98 PRE(gptr() >= egptr());
101 ssize_t cnt = ::read(_pimpl->_fd, _pimpl->_read_buf.get(),
103 ok = (cnt != -1 && cnt != 0);
106 return traits_type::eof();
108 setg(_pimpl->_read_buf.get(), _pimpl->_read_buf.get(),
109 _pimpl->_read_buf.get() + cnt);
110 return traits_type::to_int_type(*gptr());
115 /// Writes data to the file descriptor when the write buffer overflows.
117 /// \param c The character that causes the overflow.
119 /// \return EOF if error, some other value for success.
121 /// \throw something TODO(jmmv): According to the documentation, it is OK for
122 /// this method to throw in case of errors. Revisit this code to see if we
125 systembuf::overflow(int c)
127 PRE(pptr() >= epptr());
129 return traits_type::eof();
130 if (!traits_type::eq_int_type(c, traits_type::eof())) {
131 traits_type::assign(*pptr(), c);
134 return traits_type::not_eof(c);
138 /// Synchronizes the stream with the file descriptor.
140 /// \return 0 on success, -1 on error.
142 systembuf::sync(void)
144 ssize_t cnt = pptr() - pbase();
147 ok = ::write(_pimpl->_fd, pbase(), cnt) == cnt;