1 // Copyright 2010 Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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.
16 //
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.
28 
29 #include "utils/process/systembuf.hpp"
30 
31 extern "C" {
32 #include <unistd.h>
33 }
34 
35 #include "utils/auto_array.ipp"
36 #include "utils/sanity.hpp"
37 
38 using utils::process::systembuf;
39 
40 
41 /// Private implementation fields for systembuf.
42 struct systembuf::impl {
43     /// File descriptor attached to the systembuf.
44     int _fd;
45 
46     /// Size of the _read_buf and _write_buf buffers.
47     std::size_t _bufsize;
48 
49     /// In-memory buffer for read operations.
50     utils::auto_array< char > _read_buf;
51 
52     /// In-memory buffer for write operations.
53     utils::auto_array< char > _write_buf;
54 
55     /// Initializes private implementation data.
56     ///
57     /// \param fd The file descriptor.
58     /// \param bufsize The size of the created read and write buffers.
implsystembuf::impl59     impl(const int fd, const std::size_t bufsize) :
60         _fd(fd),
61         _bufsize(bufsize),
62         _read_buf(new char[bufsize]),
63         _write_buf(new char[bufsize])
64     {
65     }
66 };
67 
68 
69 /// Constructs a new systembuf based on an open file descriptor.
70 ///
71 /// This grabs ownership of the file descriptor.
72 ///
73 /// \param fd The file descriptor to wrap.  Must be open and valid.
74 /// \param bufsize The size to use for the internal read/write buffers.
systembuf(const int fd,std::size_t bufsize)75 systembuf::systembuf(const int fd, std::size_t bufsize) :
76     _pimpl(new impl(fd, bufsize))
77 {
78     setp(_pimpl->_write_buf.get(), _pimpl->_write_buf.get() + _pimpl->_bufsize);
79 }
80 
81 
82 /// Destroys a systembuf object.
83 ///
84 /// \post The file descriptor attached to this systembuf is closed.
~systembuf(void)85 systembuf::~systembuf(void)
86 {
87     ::close(_pimpl->_fd);
88 }
89 
90 
91 /// Reads new data when the systembuf read buffer underflows.
92 ///
93 /// \return The new character to be read, or EOF if no more.
94 systembuf::int_type
underflow(void)95 systembuf::underflow(void)
96 {
97     PRE(gptr() >= egptr());
98 
99     bool ok;
100     ssize_t cnt = ::read(_pimpl->_fd, _pimpl->_read_buf.get(),
101                          _pimpl->_bufsize);
102     ok = (cnt != -1 && cnt != 0);
103 
104     if (!ok)
105         return traits_type::eof();
106     else {
107         setg(_pimpl->_read_buf.get(), _pimpl->_read_buf.get(),
108              _pimpl->_read_buf.get() + cnt);
109         return traits_type::to_int_type(*gptr());
110     }
111 }
112 
113 
114 /// Writes data to the file descriptor when the write buffer overflows.
115 ///
116 /// \param c The character that causes the overflow.
117 ///
118 /// \return EOF if error, some other value for success.
119 ///
120 /// \throw something TODO(jmmv): According to the documentation, it is OK for
121 ///     this method to throw in case of errors.  Revisit this code to see if we
122 ///     can do better.
123 systembuf::int_type
overflow(int c)124 systembuf::overflow(int c)
125 {
126     PRE(pptr() >= epptr());
127     if (sync() == -1)
128         return traits_type::eof();
129     if (!traits_type::eq_int_type(c, traits_type::eof())) {
130         traits_type::assign(*pptr(), c);
131         pbump(1);
132     }
133     return traits_type::not_eof(c);
134 }
135 
136 
137 /// Synchronizes the stream with the file descriptor.
138 ///
139 /// \return 0 on success, -1 on error.
140 int
sync(void)141 systembuf::sync(void)
142 {
143     ssize_t cnt = pptr() - pbase();
144 
145     bool ok;
146     ok = ::write(_pimpl->_fd, pbase(), cnt) == cnt;
147 
148     if (ok)
149         pbump(-cnt);
150     return ok ? 0 : -1;
151 }
152