dovecot-2.1: Simplified creating filter ostreams.

dovecot at dovecot.org dovecot at dovecot.org
Wed Sep 21 14:40:48 EEST 2011


details:   http://hg.dovecot.org/dovecot-2.1/rev/8c3c0e01e00d
changeset: 13530:8c3c0e01e00d
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Sep 21 14:40:35 2011 +0300
description:
Simplified creating filter ostreams.

diffstat:

 src/lib-fs/ostream-cmp.c               |   34 +---
 src/lib-ssl-iostream/ostream-openssl.c |    2 +-
 src/lib/ostream-buffer.c               |    2 +-
 src/lib/ostream-file.c                 |    4 +-
 src/lib/ostream-private.h              |   10 +-
 src/lib/ostream.c                      |  248 ++++++++++++++++++++++++--------
 src/plugins/zlib/ostream-bzlib.c       |   53 +-----
 src/plugins/zlib/ostream-zlib.c        |   66 +------
 8 files changed, 224 insertions(+), 195 deletions(-)

diffs (truncated from 765 to 300 lines):

diff -r cf77e448295c -r 8c3c0e01e00d src/lib-fs/ostream-cmp.c
--- a/src/lib-fs/ostream-cmp.c	Wed Sep 21 13:56:13 2011 +0300
+++ b/src/lib-fs/ostream-cmp.c	Wed Sep 21 14:40:35 2011 +0300
@@ -9,41 +9,18 @@
 	struct ostream_private ostream;
 
 	struct istream *input;
-	struct ostream *output;
 	bool equals;
 };
 
-static void cstream_copy_error(struct cmp_ostream *cstream)
-{
-	struct ostream *src = cstream->output;
-	struct ostream *dest = &cstream->ostream.ostream;
-
-	dest->stream_errno = src->stream_errno;
-	dest->last_failed_errno = src->last_failed_errno;
-	dest->overflow = src->overflow;
-}
-
 static void o_stream_cmp_close(struct iostream_private *stream)
 {
 	struct cmp_ostream *cstream = (struct cmp_ostream *)stream;
 
-	if (cstream->output == NULL)
+	if (cstream->input == NULL)
 		return;
 
 	i_stream_unref(&cstream->input);
 	o_stream_flush(&cstream->ostream.ostream);
-	o_stream_unref(&cstream->output);
-}
-
-static int o_stream_cmp_flush(struct ostream_private *stream)
-{
-	struct cmp_ostream *cstream = (struct cmp_ostream *)stream;
-	int ret;
-
-	ret = o_stream_flush(cstream->output);
-	if (ret < 0)
-		cstream_copy_error(cstream);
-	return ret;
 }
 
 bool stream_cmp_block(struct istream *input,
@@ -82,8 +59,8 @@
 		}
 	}
 
-	if ((ret = o_stream_sendv(cstream->output, iov, iov_count)) < 0) {
-		cstream_copy_error(cstream);
+	if ((ret = o_stream_sendv(stream->parent, iov, iov_count)) < 0) {
+		o_stream_copy_error_from_parent(stream);
 		return -1;
 	}
 
@@ -98,15 +75,12 @@
 
 	cstream = i_new(struct cmp_ostream, 1);
 	cstream->ostream.sendv = o_stream_cmp_sendv;
-	cstream->ostream.flush = o_stream_cmp_flush;
 	cstream->ostream.iostream.close = o_stream_cmp_close;
 	cstream->input = input;
-	cstream->output = output;
 	cstream->equals = TRUE;
 	i_stream_ref(input);
-	o_stream_ref(output);
 
-	return o_stream_create(&cstream->ostream);
+	return o_stream_create(&cstream->ostream, output);
 }
 
 bool o_stream_cmp_equals(struct ostream *_output)
diff -r cf77e448295c -r 8c3c0e01e00d src/lib-ssl-iostream/ostream-openssl.c
--- a/src/lib-ssl-iostream/ostream-openssl.c	Wed Sep 21 13:56:13 2011 +0300
+++ b/src/lib-ssl-iostream/ostream-openssl.c	Wed Sep 21 14:40:35 2011 +0300
@@ -253,5 +253,5 @@
 	o_stream_set_flush_callback(ssl_io->plain_output,
 				    plain_flush_callback, sstream);
 
-	return o_stream_create(&sstream->ostream);
+	return o_stream_create(&sstream->ostream, NULL);
 }
diff -r cf77e448295c -r 8c3c0e01e00d src/lib/ostream-buffer.c
--- a/src/lib/ostream-buffer.c	Wed Sep 21 13:56:13 2011 +0300
+++ b/src/lib/ostream-buffer.c	Wed Sep 21 14:40:35 2011 +0300
@@ -59,5 +59,5 @@
 	bstream->ostream.write_at = o_stream_buffer_write_at;
 
 	bstream->buf = buf;
-	return o_stream_create(&bstream->ostream);
+	return o_stream_create(&bstream->ostream, NULL);
 }
diff -r cf77e448295c -r 8c3c0e01e00d src/lib/ostream-file.c
--- a/src/lib/ostream-file.c	Wed Sep 21 13:56:13 2011 +0300
+++ b/src/lib/ostream-file.c	Wed Sep 21 14:40:35 2011 +0300
@@ -933,7 +933,7 @@
 
 	fstream = o_stream_create_fd_common(fd, autoclose_fd);
 	fstream->ostream.max_buffer_size = max_buffer_size;
-	ostream = o_stream_create(&fstream->ostream);
+	ostream = o_stream_create(&fstream->ostream, NULL);
 
 	offset = lseek(fd, 0, SEEK_CUR);
 	if (offset >= 0) {
@@ -969,7 +969,7 @@
 	fstream->real_offset = offset;
 	fstream->buffer_offset = offset;
 
-	ostream = o_stream_create(&fstream->ostream);
+	ostream = o_stream_create(&fstream->ostream, NULL);
 	ostream->offset = offset;
 	return ostream;
 }
diff -r cf77e448295c -r 8c3c0e01e00d src/lib/ostream-private.h
--- a/src/lib/ostream-private.h	Wed Sep 21 13:56:13 2011 +0300
+++ b/src/lib/ostream-private.h	Wed Sep 21 14:40:35 2011 +0300
@@ -11,6 +11,9 @@
 /* methods: */
 	void (*cork)(struct ostream_private *stream, bool set);
 	int (*flush)(struct ostream_private *stream);
+	void (*set_flush_callback)(struct ostream_private *stream,
+				   stream_flush_callback_t *callback,
+				   void *context);
 	void (*flush_pending)(struct ostream_private *stream, bool set);
 	size_t (*get_used_size)(const struct ostream_private *stream);
 	int (*seek)(struct ostream_private *stream, uoff_t offset);
@@ -27,15 +30,20 @@
 	struct ostream ostream;
 	size_t max_buffer_size;
 
+	struct ostream *parent; /* for filter streams */
+
 	stream_flush_callback_t *callback;
 	void *context;
 
 	unsigned int corked:1;
 };
 
-struct ostream *o_stream_create(struct ostream_private *_stream);
+struct ostream *
+o_stream_create(struct ostream_private *_stream, struct ostream *parent);
 
 off_t io_stream_copy(struct ostream *outstream, struct istream *instream,
 		     size_t block_size);
 
+void o_stream_copy_error_from_parent(struct ostream_private *_stream);
+
 #endif
diff -r cf77e448295c -r 8c3c0e01e00d src/lib/ostream.c
--- a/src/lib/ostream.c	Wed Sep 21 13:56:13 2011 +0300
+++ b/src/lib/ostream.c	Wed Sep 21 14:40:35 2011 +0300
@@ -12,8 +12,12 @@
 
 const char *o_stream_get_name(struct ostream *stream)
 {
-	return stream->real_stream->iostream.name == NULL ? "" :
-		stream->real_stream->iostream.name;
+	while (stream->real_stream->iostream.name == NULL) {
+		stream = stream->real_stream->parent;
+		if (stream == NULL)
+			return "";
+	}
+	return stream->real_stream->iostream.name;
 }
 
 void o_stream_destroy(struct ostream **stream)
@@ -46,26 +50,17 @@
 {
 	struct ostream_private *_stream = stream->real_stream;
 
-	_stream->callback = callback;
-	_stream->context = context;
+	_stream->set_flush_callback(_stream, callback, context);
 }
 
 void o_stream_unset_flush_callback(struct ostream *stream)
 {
-	struct ostream_private *_stream = stream->real_stream;
-
-	_stream->callback = NULL;
-	_stream->context = NULL;
+	o_stream_set_flush_callback(stream, NULL, NULL);
 }
 
 void o_stream_set_max_buffer_size(struct ostream *stream, size_t max_size)
 {
-	if (stream->real_stream->iostream.set_max_buffer_size != NULL) {
-		io_stream_set_max_buffer_size(&stream->real_stream->iostream,
-					      max_size);
-	} else {
-		stream->real_stream->max_buffer_size = max_size;
-	}
+	io_stream_set_max_buffer_size(&stream->real_stream->iostream, max_size);
 }
 
 void o_stream_cork(struct ostream *stream)
@@ -75,10 +70,7 @@
 	if (unlikely(stream->closed))
 		return;
 
-	if (_stream->cork != NULL)
-		_stream->cork(_stream, TRUE);
-	else
-		_stream->corked = TRUE;
+	_stream->cork(_stream, TRUE);
 }
 
 void o_stream_uncork(struct ostream *stream)
@@ -88,12 +80,7 @@
 	if (unlikely(stream->closed))
 		return;
 
-	if (_stream->cork != NULL)
-		_stream->cork(_stream, FALSE);
-	else {
-		_stream->corked = FALSE;
-		(void)o_stream_flush(stream);
-	}
+	_stream->cork(_stream, FALSE);
 }
 
 int o_stream_flush(struct ostream *stream)
@@ -105,11 +92,9 @@
 		return -1;
 
 	stream->stream_errno = 0;
-	if (_stream->flush != NULL) {
-		if (unlikely((ret = _stream->flush(_stream)) < 0)) {
-			i_assert(stream->stream_errno != 0);
-			stream->last_failed_errno = stream->stream_errno;
-		}
+	if (unlikely((ret = _stream->flush(_stream)) < 0)) {
+		i_assert(stream->stream_errno != 0);
+		stream->last_failed_errno = stream->stream_errno;
 	}
 	return ret;
 }
@@ -121,16 +106,14 @@
 	if (unlikely(stream->closed))
 		return;
 
-	if (_stream->flush_pending != NULL)
-		_stream->flush_pending(_stream, set);
+	_stream->flush_pending(_stream, set);
 }
 
 size_t o_stream_get_buffer_used_size(const struct ostream *stream)
 {
 	const struct ostream_private *_stream = stream->real_stream;
 
-	return _stream->get_used_size == NULL ? 0 :
-		_stream->get_used_size(_stream);
+	return _stream->get_used_size(_stream);
 }
 
 size_t o_stream_get_buffer_avail_size(const struct ostream *stream)
@@ -149,14 +132,9 @@
 		return -1;
 
 	stream->stream_errno = 0;
-	if (_stream->seek != NULL) {
-		if (unlikely(_stream->seek(_stream, offset) < 0)) {
-			i_assert(stream->stream_errno != 0);
-			stream->last_failed_errno = stream->stream_errno;
-		}
-	} else {
-		stream->stream_errno = EPIPE;
-		stream->last_failed_errno = EPIPE;
+	if (unlikely(_stream->seek(_stream, offset) < 0)) {
+		i_assert(stream->stream_errno != 0);
+		stream->last_failed_errno = stream->stream_errno;
 		return -1;
 	}
 	return 1;
@@ -230,11 +208,6 @@
 	if (unlikely(stream->closed))
 		return -1;
 
-	if (stream->real_stream->write_at == NULL) {
-		/* stream doesn't support seeking */
-		stream->stream_errno = EPIPE;
-		return -1;
-	}
 	ret = stream->real_stream->write_at(stream->real_stream,
 					    data, size, offset);
 	if (unlikely(ret < 0)) {
@@ -244,22 +217,6 @@
 	return ret;
 }
 
-static off_t o_stream_default_send_istream(struct ostream_private *outstream,
-					   struct istream *instream)
-{
-	return io_stream_copy(&outstream->ostream, instream, IO_BLOCK_SIZE);
-}
-
-struct ostream *o_stream_create(struct ostream_private *_stream)
-{


More information about the dovecot-cvs mailing list