dovecot-2.2: iostreams: Added close_parent flag to close() handl...

dovecot at dovecot.org dovecot at dovecot.org
Wed Mar 13 22:14:01 EET 2013


details:   http://hg.dovecot.org/dovecot-2.2/rev/6cabb95d32ec
changeset: 16020:6cabb95d32ec
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Mar 13 22:11:39 2013 +0200
description:
iostreams: Added close_parent flag to close() handler and clarified close/destroy APIs.
This makes it unambiguous how things work: Unless you explicitly call
[io]_stream_close(), the parent streams won't be closed. This is what most
(hopefully all!) of the existing code expects.

I was wondering a bit if [io]_stream_destroy() should simply have been
removed and replaced with [io]_stream_unref() calls, since they would have
worked basically everywhere, but there might be some places where it's
better to have explicitly closed the stream (and where closing the parent
stream doesn't matter).

diffstat:

 src/lib-compression/istream-bzlib.c         |   5 ++++-
 src/lib-compression/istream-zlib.c          |   5 ++++-
 src/lib-compression/ostream-bzlib.c         |   5 ++++-
 src/lib-compression/ostream-zlib.c          |   5 ++++-
 src/lib-fs/ostream-cmp.c                    |   5 ++++-
 src/lib-http/http-transfer-chunked.c        |   5 ++++-
 src/lib-mail/istream-attachment-extractor.c |   5 ++++-
 src/lib-mail/istream-binary-converter.c     |   5 ++++-
 src/lib-ssl-iostream/istream-openssl.c      |   6 ++++--
 src/lib-ssl-iostream/ostream-openssl.c      |   6 ++++--
 src/lib/iostream-private.h                  |   4 ++--
 src/lib/iostream-temp.c                     |   4 +++-
 src/lib/iostream.c                          |  18 ++++++++++++------
 src/lib/istream-concat.c                    |   9 ++++++---
 src/lib/istream-file.c                      |   3 ++-
 src/lib/istream-mmap.c                      |   3 ++-
 src/lib/istream-rawlog.c                    |   6 ++++--
 src/lib/istream-seekable.c                  |   3 ++-
 src/lib/istream-tee.c                       |   3 ++-
 src/lib/istream.c                           |  28 ++++++++++++++++++++++------
 src/lib/istream.h                           |   6 +++---
 src/lib/ostream-file.c                      |   3 ++-
 src/lib/ostream-rawlog.c                    |   6 ++++--
 src/lib/ostream.c                           |  22 +++++++++++++++-------
 src/lib/ostream.h                           |   5 +++--
 25 files changed, 124 insertions(+), 51 deletions(-)

diffs (truncated from 582 to 300 lines):

diff -r 34d61f447433 -r 6cabb95d32ec src/lib-compression/istream-bzlib.c
--- a/src/lib-compression/istream-bzlib.c	Wed Mar 13 15:05:05 2013 +0200
+++ b/src/lib-compression/istream-bzlib.c	Wed Mar 13 22:11:39 2013 +0200
@@ -23,7 +23,8 @@
 	unsigned int zs_closed:1;
 };
 
-static void i_stream_bzlib_close(struct iostream_private *stream)
+static void i_stream_bzlib_close(struct iostream_private *stream,
+				 bool close_parent)
 {
 	struct bzlib_istream *zstream = (struct bzlib_istream *)stream;
 
@@ -31,6 +32,8 @@
 		(void)BZ2_bzDecompressEnd(&zstream->zs);
 		zstream->zs_closed = TRUE;
 	}
+	if (close_parent)
+		i_stream_close(zstream->istream.parent);
 }
 
 static void bzlib_read_error(struct bzlib_istream *zstream, const char *error)
diff -r 34d61f447433 -r 6cabb95d32ec src/lib-compression/istream-zlib.c
--- a/src/lib-compression/istream-zlib.c	Wed Mar 13 15:05:05 2013 +0200
+++ b/src/lib-compression/istream-zlib.c	Wed Mar 13 22:11:39 2013 +0200
@@ -40,7 +40,8 @@
 
 static void i_stream_zlib_init(struct zlib_istream *zstream);
 
-static void i_stream_zlib_close(struct iostream_private *stream)
+static void i_stream_zlib_close(struct iostream_private *stream,
+				bool close_parent)
 {
 	struct zlib_istream *zstream = (struct zlib_istream *)stream;
 
@@ -48,6 +49,8 @@
 		(void)inflateEnd(&zstream->zs);
 		zstream->zs_closed = TRUE;
 	}
+	if (close_parent)
+		i_stream_close(zstream->istream.parent);
 }
 
 static void zlib_read_error(struct zlib_istream *zstream, const char *error)
diff -r 34d61f447433 -r 6cabb95d32ec src/lib-compression/ostream-bzlib.c
--- a/src/lib-compression/ostream-bzlib.c	Wed Mar 13 15:05:05 2013 +0200
+++ b/src/lib-compression/ostream-bzlib.c	Wed Mar 13 22:11:39 2013 +0200
@@ -20,12 +20,15 @@
 	unsigned int flushed:1;
 };
 
-static void o_stream_bzlib_close(struct iostream_private *stream)
+static void o_stream_bzlib_close(struct iostream_private *stream,
+				 bool close_parent)
 {
 	struct bzlib_ostream *zstream = (struct bzlib_ostream *)stream;
 
 	(void)o_stream_flush(&zstream->ostream.ostream);
 	(void)BZ2_bzCompressEnd(&zstream->zs);
+	if (close_parent)
+		o_stream_close(zstream->ostream.parent);
 }
 
 static int o_stream_zlib_send_outbuf(struct bzlib_ostream *zstream)
diff -r 34d61f447433 -r 6cabb95d32ec src/lib-compression/ostream-zlib.c
--- a/src/lib-compression/ostream-zlib.c	Wed Mar 13 15:05:05 2013 +0200
+++ b/src/lib-compression/ostream-zlib.c	Wed Mar 13 22:11:39 2013 +0200
@@ -27,12 +27,15 @@
 	unsigned int flushed:1;
 };
 
-static void o_stream_zlib_close(struct iostream_private *stream)
+static void o_stream_zlib_close(struct iostream_private *stream,
+				bool close_parent)
 {
 	struct zlib_ostream *zstream = (struct zlib_ostream *)stream;
 
 	(void)o_stream_flush(&zstream->ostream.ostream);
 	(void)deflateEnd(&zstream->zs);
+	if (close_parent)
+		o_stream_close(zstream->ostream.parent);
 }
 
 static int o_stream_zlib_send_gz_header(struct zlib_ostream *zstream)
diff -r 34d61f447433 -r 6cabb95d32ec src/lib-fs/ostream-cmp.c
--- a/src/lib-fs/ostream-cmp.c	Wed Mar 13 15:05:05 2013 +0200
+++ b/src/lib-fs/ostream-cmp.c	Wed Mar 13 22:11:39 2013 +0200
@@ -12,7 +12,8 @@
 	bool equals;
 };
 
-static void o_stream_cmp_close(struct iostream_private *stream)
+static void o_stream_cmp_close(struct iostream_private *stream,
+			       bool close_parent)
 {
 	struct cmp_ostream *cstream = (struct cmp_ostream *)stream;
 
@@ -21,6 +22,8 @@
 
 	i_stream_unref(&cstream->input);
 	(void)o_stream_flush(&cstream->ostream.ostream);
+	if (close_parent)
+		o_stream_close(cstream->ostream.parent);
 }
 
 bool stream_cmp_block(struct istream *input,
diff -r 34d61f447433 -r 6cabb95d32ec src/lib-http/http-transfer-chunked.c
--- a/src/lib-http/http-transfer-chunked.c	Wed Mar 13 15:05:05 2013 +0200
+++ b/src/lib-http/http-transfer-chunked.c	Wed Mar 13 22:11:39 2013 +0200
@@ -556,13 +556,16 @@
 }
 
 static void
-http_transfer_chunked_ostream_close(struct iostream_private *stream)
+http_transfer_chunked_ostream_close(struct iostream_private *stream,
+				    bool close_parent)
 {
 	struct http_transfer_chunked_ostream *tcstream =
 		(struct http_transfer_chunked_ostream *)stream;
 
 	(void)o_stream_send(tcstream->ostream.parent, "0\r\n\r\n", 5);
 	(void)o_stream_flush(&tcstream->ostream.ostream);
+	if (close_parent)
+		o_stream_close(tcstream->ostream.parent);
 }
 
 static ssize_t
diff -r 34d61f447433 -r 6cabb95d32ec src/lib-mail/istream-attachment-extractor.c
--- a/src/lib-mail/istream-attachment-extractor.c	Wed Mar 13 15:05:05 2013 +0200
+++ b/src/lib-mail/istream-attachment-extractor.c	Wed Mar 13 22:11:39 2013 +0200
@@ -627,7 +627,8 @@
 	return ret;
 }
 
-static void i_stream_attachment_extractor_close(struct iostream_private *stream)
+static void i_stream_attachment_extractor_close(struct iostream_private *stream,
+						bool close_parent)
 {
 	struct attachment_istream *astream =
 		(struct attachment_istream *)stream;
@@ -641,6 +642,8 @@
 	hash_format_deinit_free(&astream->set.hash_format);
 	if (astream->pool != NULL)
 		pool_unref(&astream->pool);
+	if (close_parent)
+		i_stream_close(astream->istream.parent);
 }
 
 struct istream *
diff -r 34d61f447433 -r 6cabb95d32ec src/lib-mail/istream-binary-converter.c
--- a/src/lib-mail/istream-binary-converter.c	Wed Mar 13 15:05:05 2013 +0200
+++ b/src/lib-mail/istream-binary-converter.c	Wed Mar 13 22:11:39 2013 +0200
@@ -260,7 +260,8 @@
 	return new_size - old_size;
 }
 
-static void i_stream_binary_converter_close(struct iostream_private *stream)
+static void i_stream_binary_converter_close(struct iostream_private *stream,
+					    bool close_parent)
 {
 	struct binary_converter_istream *bstream =
 		(struct binary_converter_istream *)stream;
@@ -270,6 +271,8 @@
 		(void)message_parser_deinit(&bstream->parser, &parts);
 	if (bstream->pool != NULL)
 		pool_unref(&bstream->pool);
+	if (close_parent)
+		i_stream_close(bstream->istream.parent);
 }
 
 struct istream *i_stream_create_binary_converter(struct istream *input)
diff -r 34d61f447433 -r 6cabb95d32ec src/lib-ssl-iostream/istream-openssl.c
--- a/src/lib-ssl-iostream/istream-openssl.c	Wed Mar 13 15:05:05 2013 +0200
+++ b/src/lib-ssl-iostream/istream-openssl.c	Wed Mar 13 22:11:39 2013 +0200
@@ -10,11 +10,13 @@
 	bool seen_eof;
 };
 
-static void i_stream_ssl_close(struct iostream_private *stream)
+static void i_stream_ssl_close(struct iostream_private *stream,
+			       bool close_parent)
 {
 	struct ssl_istream *sstream = (struct ssl_istream *)stream;
 
-	i_stream_close(sstream->ssl_io->plain_input);
+	if (close_parent)
+		i_stream_close(sstream->ssl_io->plain_input);
 }
 
 static void i_stream_ssl_destroy(struct iostream_private *stream)
diff -r 34d61f447433 -r 6cabb95d32ec src/lib-ssl-iostream/ostream-openssl.c
--- a/src/lib-ssl-iostream/ostream-openssl.c	Wed Mar 13 15:05:05 2013 +0200
+++ b/src/lib-ssl-iostream/ostream-openssl.c	Wed Mar 13 22:11:39 2013 +0200
@@ -11,11 +11,13 @@
 	buffer_t *buffer;
 };
 
-static void o_stream_ssl_close(struct iostream_private *stream)
+static void
+o_stream_ssl_close(struct iostream_private *stream, bool close_parent)
 {
 	struct ssl_ostream *sstream = (struct ssl_ostream *)stream;
 
-	o_stream_close(sstream->ssl_io->plain_output);
+	if (close_parent)
+		o_stream_close(sstream->ssl_io->plain_output);
 }
 
 static void o_stream_ssl_destroy(struct iostream_private *stream)
diff -r 34d61f447433 -r 6cabb95d32ec src/lib/iostream-private.h
--- a/src/lib/iostream-private.h	Wed Mar 13 15:05:05 2013 +0200
+++ b/src/lib/iostream-private.h	Wed Mar 13 22:11:39 2013 +0200
@@ -7,7 +7,7 @@
 	int refcount;
 	char *name;
 
-	void (*close)(struct iostream_private *stream);
+	void (*close)(struct iostream_private *streami, bool close_parent);
 	void (*destroy)(struct iostream_private *stream);
 	void (*set_max_buffer_size)(struct iostream_private *stream,
 				    size_t max_size);
@@ -19,7 +19,7 @@
 void io_stream_init(struct iostream_private *stream);
 void io_stream_ref(struct iostream_private *stream);
 void io_stream_unref(struct iostream_private *stream);
-void io_stream_close(struct iostream_private *stream);
+void io_stream_close(struct iostream_private *stream, bool close_parent);
 void io_stream_set_max_buffer_size(struct iostream_private *stream,
 				   size_t max_size);
 
diff -r 34d61f447433 -r 6cabb95d32ec src/lib/iostream-temp.c
--- a/src/lib/iostream-temp.c	Wed Mar 13 15:05:05 2013 +0200
+++ b/src/lib/iostream-temp.c	Wed Mar 13 22:11:39 2013 +0200
@@ -27,7 +27,9 @@
 	bool fd_tried;
 };
 
-static void o_stream_temp_close(struct iostream_private *stream)
+static void
+o_stream_temp_close(struct iostream_private *stream,
+		    bool close_parent ATTR_UNUSED)
 {
 	struct temp_ostream *tstream = (struct temp_ostream *)stream;
 
diff -r 34d61f447433 -r 6cabb95d32ec src/lib/iostream.c
--- a/src/lib/iostream.c	Wed Mar 13 15:05:05 2013 +0200
+++ b/src/lib/iostream.c	Wed Mar 13 22:11:39 2013 +0200
@@ -4,16 +4,22 @@
 #include "iostream-private.h"
 
 static void
-io_stream_default_close_destroy(struct iostream_private *stream ATTR_UNUSED)
+io_stream_default_close(struct iostream_private *stream ATTR_UNUSED,
+			bool close_parent ATTR_UNUSED)
+{
+}
+
+static void
+io_stream_default_destroy(struct iostream_private *stream ATTR_UNUSED)
 {
 }
 
 void io_stream_init(struct iostream_private *stream)
 {
 	if (stream->close == NULL)
-		stream->close = io_stream_default_close_destroy;
+		stream->close = io_stream_default_close;
 	if (stream->destroy == NULL)
-		stream->destroy = io_stream_default_close_destroy;
+		stream->destroy = io_stream_default_destroy;
 
 	stream->refcount = 1;
 }
@@ -29,7 +35,7 @@
 	if (--stream->refcount != 0)
 		return;
 
-	stream->close(stream);
+	stream->close(stream, FALSE);
 	stream->destroy(stream);
 	if (stream->destroy_callback != NULL)
 		stream->destroy_callback(stream->destroy_context);
@@ -38,9 +44,9 @@
         i_free(stream);
 }
 
-void io_stream_close(struct iostream_private *stream)
+void io_stream_close(struct iostream_private *stream, bool close_parent)
 {
-	stream->close(stream);
+	stream->close(stream, close_parent);
 }
 
 void io_stream_set_max_buffer_size(struct iostream_private *stream,
diff -r 34d61f447433 -r 6cabb95d32ec src/lib/istream-concat.c
--- a/src/lib/istream-concat.c	Wed Mar 13 15:05:05 2013 +0200
+++ b/src/lib/istream-concat.c	Wed Mar 13 22:11:39 2013 +0200
@@ -15,13 +15,16 @@
 	size_t prev_stream_left, prev_skip;
 };


More information about the dovecot-cvs mailing list