From 43834f87bf431198f986e86052a4f6e558fdb07d Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Fri, 13 Jun 2014 02:18:53 +0300 Subject: [PATCH] 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(). --- 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(+) diff --git a/src/lib/istream-file.c b/src/lib/istream-file.c index 98966701c3..c86fa907e3 100644 --- a/src/lib/istream-file.c +++ b/src/lib/istream-file.c @@ -229,6 +229,15 @@ struct istream *i_stream_create_fd(int fd, size_t max_buffer_size, 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 --git a/src/lib/istream.h b/src/lib/istream.h index 3e6e76e5ed..1f4f4f44aa 100644 --- a/src/lib/istream.h +++ b/src/lib/istream.h @@ -24,6 +24,8 @@ typedef void istream_callback_t(void *context); 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 --git a/src/lib/ostream-file.c b/src/lib/ostream-file.c index 73d9e425fb..ca01f80f27 100644 --- a/src/lib/ostream-file.c +++ b/src/lib/ostream-file.c @@ -971,6 +971,16 @@ o_stream_create_fd(int fd, size_t max_buffer_size, bool autoclose_fd) return ostream; } +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) { @@ -990,3 +1000,12 @@ o_stream_create_fd_file(int fd, uoff_t offset, bool autoclose_fd) 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 --git a/src/lib/ostream.h b/src/lib/ostream.h index 86d5ba1b84..dbf1ff28f4 100644 --- a/src/lib/ostream.h +++ b/src/lib/ostream.h @@ -30,10 +30,13 @@ typedef int stream_flush_callback_t(void *context); 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. */ -- 2.47.3