xref: /freebsd/contrib/kyua/utils/process/systembuf.cpp (revision 6ba2210ee039f2f12878c217bcf058e9c8b26b29)
1 // Copyright 2010 The Kyua Authors.
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/noncopyable.hpp"
37 #include "utils/sanity.hpp"
38 
39 using utils::process::systembuf;
40 
41 
42 /// Private implementation fields for systembuf.
43 struct systembuf::impl : utils::noncopyable {
44     /// File descriptor attached to the systembuf.
45     int _fd;
46 
47     /// Size of the _read_buf and _write_buf buffers.
48     std::size_t _bufsize;
49 
50     /// In-memory buffer for read operations.
51     utils::auto_array< char > _read_buf;
52 
53     /// In-memory buffer for write operations.
54     utils::auto_array< char > _write_buf;
55 
56     /// Initializes private implementation data.
57     ///
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) :
61         _fd(fd),
62         _bufsize(bufsize),
63         _read_buf(new char[bufsize]),
64         _write_buf(new char[bufsize])
65     {
66     }
67 };
68 
69 
70 /// Constructs a new systembuf based on an open file descriptor.
71 ///
72 /// This grabs ownership of the file descriptor.
73 ///
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))
78 {
79     setp(_pimpl->_write_buf.get(), _pimpl->_write_buf.get() + _pimpl->_bufsize);
80 }
81 
82 
83 /// Destroys a systembuf object.
84 ///
85 /// \post The file descriptor attached to this systembuf is closed.
86 systembuf::~systembuf(void)
87 {
88     ::close(_pimpl->_fd);
89 }
90 
91 
92 /// Reads new data when the systembuf read buffer underflows.
93 ///
94 /// \return The new character to be read, or EOF if no more.
95 systembuf::int_type
96 systembuf::underflow(void)
97 {
98     PRE(gptr() >= egptr());
99 
100     bool ok;
101     ssize_t cnt = ::read(_pimpl->_fd, _pimpl->_read_buf.get(),
102                          _pimpl->_bufsize);
103     ok = (cnt != -1 && cnt != 0);
104 
105     if (!ok)
106         return traits_type::eof();
107     else {
108         setg(_pimpl->_read_buf.get(), _pimpl->_read_buf.get(),
109              _pimpl->_read_buf.get() + cnt);
110         return traits_type::to_int_type(*gptr());
111     }
112 }
113 
114 
115 /// Writes data to the file descriptor when the write buffer overflows.
116 ///
117 /// \param c The character that causes the overflow.
118 ///
119 /// \return EOF if error, some other value for success.
120 ///
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
123 ///     can do better.
124 systembuf::int_type
125 systembuf::overflow(int c)
126 {
127     PRE(pptr() >= epptr());
128     if (sync() == -1)
129         return traits_type::eof();
130     if (!traits_type::eq_int_type(c, traits_type::eof())) {
131         traits_type::assign(*pptr(), c);
132         pbump(1);
133     }
134     return traits_type::not_eof(c);
135 }
136 
137 
138 /// Synchronizes the stream with the file descriptor.
139 ///
140 /// \return 0 on success, -1 on error.
141 int
142 systembuf::sync(void)
143 {
144     ssize_t cnt = pptr() - pbase();
145 
146     bool ok;
147     ok = ::write(_pimpl->_fd, pbase(), cnt) == cnt;
148 
149     if (ok)
150         pbump(-cnt);
151     return ok ? 0 : -1;
152 }
153