Path: blob/main/contrib/kyua/utils/process/systembuf.cpp
48199 views
// Copyright 2010 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728#include "utils/process/systembuf.hpp"2930extern "C" {31#include <unistd.h>32}3334#include "utils/auto_array.ipp"35#include "utils/noncopyable.hpp"36#include "utils/sanity.hpp"3738using utils::process::systembuf;394041/// Private implementation fields for systembuf.42struct systembuf::impl : utils::noncopyable {43/// File descriptor attached to the systembuf.44int _fd;4546/// Size of the _read_buf and _write_buf buffers.47std::size_t _bufsize;4849/// In-memory buffer for read operations.50utils::auto_array< char > _read_buf;5152/// In-memory buffer for write operations.53utils::auto_array< char > _write_buf;5455/// Initializes private implementation data.56///57/// \param fd The file descriptor.58/// \param bufsize The size of the created read and write buffers.59impl(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};676869/// 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.75systembuf::systembuf(const int fd, std::size_t bufsize) :76_pimpl(new impl(fd, bufsize))77{78setp(_pimpl->_write_buf.get(), _pimpl->_write_buf.get() + _pimpl->_bufsize);79}808182/// Destroys a systembuf object.83///84/// \post The file descriptor attached to this systembuf is closed.85systembuf::~systembuf(void)86{87::close(_pimpl->_fd);88}899091/// Reads new data when the systembuf read buffer underflows.92///93/// \return The new character to be read, or EOF if no more.94systembuf::int_type95systembuf::underflow(void)96{97PRE(gptr() >= egptr());9899bool ok;100ssize_t cnt = ::read(_pimpl->_fd, _pimpl->_read_buf.get(),101_pimpl->_bufsize);102ok = (cnt != -1 && cnt != 0);103104if (!ok)105return traits_type::eof();106else {107setg(_pimpl->_read_buf.get(), _pimpl->_read_buf.get(),108_pimpl->_read_buf.get() + cnt);109return traits_type::to_int_type(*gptr());110}111}112113114/// 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 for121/// this method to throw in case of errors. Revisit this code to see if we122/// can do better.123systembuf::int_type124systembuf::overflow(int c)125{126PRE(pptr() >= epptr());127if (sync() == -1)128return traits_type::eof();129if (!traits_type::eq_int_type(c, traits_type::eof())) {130traits_type::assign(*pptr(), c);131pbump(1);132}133return traits_type::not_eof(c);134}135136137/// Synchronizes the stream with the file descriptor.138///139/// \return 0 on success, -1 on error.140int141systembuf::sync(void)142{143ssize_t cnt = pptr() - pbase();144145bool ok;146ok = ::write(_pimpl->_fd, pbase(), cnt) == cnt;147148if (ok)149pbump(-cnt);150return ok ? 0 : -1;151}152153154