]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Added [io]_stream_create_fd_*autoclose()
authorTimo Sirainen <tss@iki.fi>
Thu, 12 Jun 2014 23:18:53 +0000 (02:18 +0300)
committerTimo Sirainen <tss@iki.fi>
Thu, 12 Jun 2014 23:18:53 +0000 (02:18 +0300)
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
src/lib/istream.h
src/lib/ostream-file.c
src/lib/ostream.h

index 98966701c39a353eec6eefad84e09023073efe10..c86fa907e308487dbe7bd24dcecc6122cc2b5b13 100644 (file)
@@ -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;
index 3e6e76e5edb95848c5583a6088fe82ed4655fe61..1f4f4f44aae88ca56020e5093e9c3f22b4678d83 100644 (file)
@@ -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);
index 73d9e425fbe797c97a791fa4d0dca52b52dc05f4..ca01f80f27b420b25751320003dc34d401c0b17f 100644 (file)
@@ -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;
+}
index 86d5ba1b843aad6781e146cf92d36be772065395..dbf1ff28f4b403c64ddf98f75df79d50d2da8e97 100644 (file)
@@ -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. */