dovecot-2.2: lib: Added [io]_stream_create_fd_*autoclose()

dovecot at dovecot.org dovecot at dovecot.org
Thu Jun 12 23:20:48 UTC 2014


details:   http://hg.dovecot.org/dovecot-2.2/rev/0bcb43692d91
changeset: 17479:0bcb43692d91
user:      Timo Sirainen <tss at iki.fi>
date:      Fri Jun 13 02:18:53 2014 +0300
description:
lib: Added [io]_stream_create_fd_*autoclose()
These make it clearer that the fd parameter will be closed automatically
when the stream is closed.

Eventually (in v2.3) we'll want to get rid of the autoclose boolean
parameter in [io]_stream_create_fd().

diffstat:

 src/lib/istream-file.c |   9 +++++++++
 src/lib/istream.h      |   2 ++
 src/lib/ostream-file.c |  19 +++++++++++++++++++
 src/lib/ostream.h      |   3 +++
 4 files changed, 33 insertions(+), 0 deletions(-)

diffs (81 lines):

diff -r f42dfaebdfc4 -r 0bcb43692d91 src/lib/istream-file.c
--- a/src/lib/istream-file.c	Fri Jun 13 02:04:12 2014 +0300
+++ b/src/lib/istream-file.c	Fri Jun 13 02:18:53 2014 +0300
@@ -229,6 +229,15 @@
 	return i_stream_create_file_common(fd, NULL, max_buffer_size, autoclose_fd);
 }
 
+struct istream *i_stream_create_fd_autoclose(int *fd, size_t max_buffer_size)
+{
+	struct istream *input;
+
+	input = i_stream_create_fd(*fd, max_buffer_size, TRUE);
+	*fd = -1;
+	return input;
+}
+
 struct istream *i_stream_create_file(const char *path, size_t max_buffer_size)
 {
 	struct istream *input;
diff -r f42dfaebdfc4 -r 0bcb43692d91 src/lib/istream.h
--- a/src/lib/istream.h	Fri Jun 13 02:04:12 2014 +0300
+++ b/src/lib/istream.h	Fri Jun 13 02:18:53 2014 +0300
@@ -24,6 +24,8 @@
 
 struct istream *i_stream_create_fd(int fd, size_t max_buffer_size,
 				   bool autoclose_fd);
+/* The fd is set to -1 immediately to avoid accidentally closing it twice. */
+struct istream *i_stream_create_fd_autoclose(int *fd, size_t max_buffer_size);
 /* Open the given path only when something is actually tried to be read from
    the stream. */
 struct istream *i_stream_create_file(const char *path, size_t max_buffer_size);
diff -r f42dfaebdfc4 -r 0bcb43692d91 src/lib/ostream-file.c
--- a/src/lib/ostream-file.c	Fri Jun 13 02:04:12 2014 +0300
+++ b/src/lib/ostream-file.c	Fri Jun 13 02:18:53 2014 +0300
@@ -972,6 +972,16 @@
 }
 
 struct ostream *
+o_stream_create_fd_autoclose(int *fd, size_t max_buffer_size)
+{
+	struct ostream *output;
+
+	output = o_stream_create_fd(*fd, max_buffer_size, TRUE);
+	*fd = -1;
+	return output;
+}
+
+struct ostream *
 o_stream_create_fd_file(int fd, uoff_t offset, bool autoclose_fd)
 {
 	struct file_ostream *fstream;
@@ -990,3 +1000,12 @@
 	ostream->offset = offset;
 	return ostream;
 }
+
+struct ostream *o_stream_create_fd_file_autoclose(int *fd, uoff_t offset)
+{
+	struct ostream *output;
+
+	output = o_stream_create_fd_file(*fd, offset, TRUE);
+	*fd = -1;
+	return output;
+}
diff -r f42dfaebdfc4 -r 0bcb43692d91 src/lib/ostream.h
--- a/src/lib/ostream.h	Fri Jun 13 02:04:12 2014 +0300
+++ b/src/lib/ostream.h	Fri Jun 13 02:18:53 2014 +0300
@@ -30,10 +30,13 @@
    If max_buffer_size is 0, an "optimal" buffer size is used (max 128kB). */
 struct ostream *
 o_stream_create_fd(int fd, size_t max_buffer_size, bool autoclose_fd);
+/* The fd is set to -1 immediately to avoid accidentally closing it twice. */
+struct ostream *o_stream_create_fd_autoclose(int *fd, size_t max_buffer_size);
 /* Create an output stream from a regular file which begins at given offset.
    If offset==(uoff_t)-1, the current offset isn't known. */
 struct ostream *
 o_stream_create_fd_file(int fd, uoff_t offset, bool autoclose_fd);
+struct ostream *o_stream_create_fd_file_autoclose(int *fd, uoff_t offset);
 /* Create an output stream to a buffer. */
 struct ostream *o_stream_create_buffer(buffer_t *buf);
 /* Create an output streams that always fails the writes. */


More information about the dovecot-cvs mailing list