ostream.c revision 904ca86f5d3f27505f50f5342214aabb78629cc8
76b43e4417bab52e913da39b5f5bc2a130d3f149Timo Sirainen/* Copyright (c) 2002-2013 Dovecot authors, see the included COPYING file */
74ab5ea66c0c4b388f1c774ae6a47ab94f1b4f18Timo Sirainenvoid o_stream_set_name(struct ostream *stream, const char *name)
74ab5ea66c0c4b388f1c774ae6a47ab94f1b4f18Timo Sirainen stream->real_stream->iostream.name = i_strdup(name);
74ab5ea66c0c4b388f1c774ae6a47ab94f1b4f18Timo Sirainenconst char *o_stream_get_name(struct ostream *stream)
e62f6437a4ff01d692a5a61369fe4168d69191edTimo Sirainen while (stream->real_stream->iostream.name == NULL) {
ebfbf5d78dcf95e8b176429f4b5b0694eb4e17d5Timo Sirainenconst char *o_stream_get_error(struct ostream *stream)
74ab5ea66c0c4b388f1c774ae6a47ab94f1b4f18Timo Sirainen /* we'll only return errors for streams that have stream_errno set.
74ab5ea66c0c4b388f1c774ae6a47ab94f1b4f18Timo Sirainen we might be returning unintended error otherwise. */
74ab5ea66c0c4b388f1c774ae6a47ab94f1b4f18Timo Sirainen return "<no error>";
74ab5ea66c0c4b388f1c774ae6a47ab94f1b4f18Timo Sirainen for (s = stream; s != NULL; s = s->real_stream->parent) {
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainenstatic void o_stream_close_full(struct ostream *stream, bool close_parents)
ebfbf5d78dcf95e8b176429f4b5b0694eb4e17d5Timo Sirainen io_stream_close(&stream->real_stream->iostream, close_parents);
74ab5ea66c0c4b388f1c774ae6a47ab94f1b4f18Timo Sirainen io_stream_ref(&stream->real_stream->iostream);
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainen if (stream->real_stream->last_errors_not_checked &&
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainen !stream->real_stream->error_handling_disabled &&
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainen stream->real_stream->iostream.refcount == 1) {
74ab5ea66c0c4b388f1c774ae6a47ab94f1b4f18Timo Sirainen i_panic("output stream %s is missing error handling",
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainen io_stream_unref(&stream->real_stream->iostream);
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainenvoid o_stream_set_flush_callback(struct ostream *stream,
e62f6437a4ff01d692a5a61369fe4168d69191edTimo Sirainen struct ostream_private *_stream = stream->real_stream;
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainen _stream->set_flush_callback(_stream, callback, context);
e62f6437a4ff01d692a5a61369fe4168d69191edTimo Sirainenvoid o_stream_unset_flush_callback(struct ostream *stream)
74ab5ea66c0c4b388f1c774ae6a47ab94f1b4f18Timo Sirainen struct ostream_private *_stream = stream->real_stream;
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainen _stream->set_flush_callback(_stream, NULL, NULL);
74ab5ea66c0c4b388f1c774ae6a47ab94f1b4f18Timo Sirainenvoid o_stream_set_max_buffer_size(struct ostream *stream, size_t max_size)
1caf757864e7734345660e7d190f84e42668a6f8Timo Sirainen io_stream_set_max_buffer_size(&stream->real_stream->iostream, max_size);
1caf757864e7734345660e7d190f84e42668a6f8Timo Sirainen struct ostream_private *_stream = stream->real_stream;
fcde781c3ceb470c8dff34a68df19c69f93bcec9Timo Sirainen struct ostream_private *_stream = stream->real_stream;
1caf757864e7734345660e7d190f84e42668a6f8Timo Sirainenstatic void o_stream_clear_error(struct ostream *stream)
74ab5ea66c0c4b388f1c774ae6a47ab94f1b4f18Timo Sirainen i_free_and_null(stream->real_stream->iostream.error);
1caf757864e7734345660e7d190f84e42668a6f8Timo Sirainen struct ostream_private *_stream = stream->real_stream;
1caf757864e7734345660e7d190f84e42668a6f8Timo Sirainen if (unlikely((ret = _stream->flush(_stream)) < 0)) {
74ab5ea66c0c4b388f1c774ae6a47ab94f1b4f18Timo Sirainen stream->last_failed_errno = stream->stream_errno;
43d32cbe60fdaef2699d99f1ca259053e9350411Timo Sirainenvoid o_stream_set_flush_pending(struct ostream *stream, bool set)
74ab5ea66c0c4b388f1c774ae6a47ab94f1b4f18Timo Sirainen struct ostream_private *_stream = stream->real_stream;
74ab5ea66c0c4b388f1c774ae6a47ab94f1b4f18Timo Sirainensize_t o_stream_get_buffer_used_size(const struct ostream *stream)
e62f6437a4ff01d692a5a61369fe4168d69191edTimo Sirainen const struct ostream_private *_stream = stream->real_stream;
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainensize_t o_stream_get_buffer_avail_size(const struct ostream *stream)
e62f6437a4ff01d692a5a61369fe4168d69191edTimo Sirainen size_t used = o_stream_get_buffer_used_size(stream);
e62f6437a4ff01d692a5a61369fe4168d69191edTimo Sirainen return stream->real_stream->max_buffer_size <= used ? 0 :
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainenint o_stream_seek(struct ostream *stream, uoff_t offset)
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainen struct ostream_private *_stream = stream->real_stream;
74ab5ea66c0c4b388f1c774ae6a47ab94f1b4f18Timo Sirainen if (unlikely(_stream->seek(_stream, offset) < 0)) {
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainen stream->last_failed_errno = stream->stream_errno;
74ab5ea66c0c4b388f1c774ae6a47ab94f1b4f18Timo Sirainenssize_t o_stream_send(struct ostream *stream, const void *data, size_t size)
74ab5ea66c0c4b388f1c774ae6a47ab94f1b4f18Timo Sirainenssize_t o_stream_sendv(struct ostream *stream, const struct const_iovec *iov,
74ab5ea66c0c4b388f1c774ae6a47ab94f1b4f18Timo Sirainen struct ostream_private *_stream = stream->real_stream;
e62f6437a4ff01d692a5a61369fe4168d69191edTimo Sirainen unsigned int i;
if (total_size == 0)
if (ret < 0) {
return ret;
unsigned int iov_count)
return ret;
int ret;
return ret;
const unsigned char *data;
if (ret <= 0) {
if (ret == 0)
bool close_parent)
if (set) {
int ret;
return ret;
void *context)
static size_t
struct ostream *
return output;