changeset 363:567e932cdc66 HEAD

Added autoclose_fd-flag for io_buffer_create_file() and io_buffer_create_mmap().
author Timo Sirainen <tss@iki.fi>
date Sun, 06 Oct 2002 06:09:36 +0300
parents aa449d013563
children ea958a5b9de1
files src/auth/userinfo-passwd-file.c src/lib-imap/imap-message-cache.c src/lib-index/mail-index-update-cache.c src/lib-index/mail-index-util.c src/lib-index/maildir/maildir-open.c src/lib-index/maildir/maildir-update.c src/lib-index/mbox/mbox-fsck.c src/lib-index/mbox/mbox-open.c src/lib-index/mbox/mbox-rebuild.c src/lib-index/mbox/mbox-rewrite.c src/lib-storage/index/index-copy.c src/lib-storage/index/index-msgcache.c src/lib-storage/index/index-search.c src/lib-storage/index/mbox/mbox-expunge.c src/lib/iobuffer.c src/lib/iobuffer.h src/master/settings.c
diffstat 17 files changed, 44 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/src/auth/userinfo-passwd-file.c	Sun Oct 06 05:54:35 2002 +0300
+++ b/src/auth/userinfo-passwd-file.c	Sun Oct 06 06:09:36 2002 +0300
@@ -298,7 +298,7 @@
 	char *const *args;
 	char *line;
 
-	inbuf = io_buffer_create_file(pw->fd, default_pool, 2048);
+	inbuf = io_buffer_create_file(pw->fd, default_pool, 2048, FALSE);
 	for (;;) {
 		line = io_buffer_next_line(inbuf);
 		if (line == NULL) {
--- a/src/lib-imap/imap-message-cache.c	Sun Oct 06 05:54:35 2002 +0300
+++ b/src/lib-imap/imap-message-cache.c	Sun Oct 06 06:09:36 2002 +0300
@@ -348,7 +348,6 @@
 void imap_msgcache_close(ImapMessageCache *cache)
 {
 	if (cache->open_inbuf != NULL) {
-		(void)close(cache->open_inbuf->fd);
 		io_buffer_destroy(cache->open_inbuf);
 		cache->open_inbuf = NULL;
 	}
--- a/src/lib-index/mail-index-update-cache.c	Sun Oct 06 05:54:35 2002 +0300
+++ b/src/lib-index/mail-index-update-cache.c	Sun Oct 06 06:09:36 2002 +0300
@@ -23,7 +23,6 @@
 	mail_index_update_headers(update, inbuf, cache_fields, NULL, NULL);
 	failed = !index->update_end(update);
 
-	(void)close(inbuf->fd);
 	io_buffer_destroy(inbuf);
 
 	return !failed;
--- a/src/lib-index/mail-index-util.c	Sun Oct 06 05:54:35 2002 +0300
+++ b/src/lib-index/mail-index-util.c	Sun Oct 06 06:09:36 2002 +0300
@@ -155,7 +155,6 @@
 	message_get_body_size(inbuf, &body_size, (uoff_t)-1);
 	*virtual_size = body_size.virtual_size;
 
-	(void)close(inbuf->fd);
 	io_buffer_destroy(inbuf);
 	return TRUE;
 }
--- a/src/lib-index/maildir/maildir-open.c	Sun Oct 06 05:54:35 2002 +0300
+++ b/src/lib-index/maildir/maildir-open.c	Sun Oct 06 06:09:36 2002 +0300
@@ -35,5 +35,6 @@
 		return NULL;
 	}
 
-	return io_buffer_create_mmap(fd, default_pool, MAIL_MMAP_BLOCK_SIZE, 0);
+	return io_buffer_create_mmap(fd, default_pool, MAIL_MMAP_BLOCK_SIZE,
+				     0, TRUE);
 }
--- a/src/lib-index/maildir/maildir-update.c	Sun Oct 06 05:54:35 2002 +0300
+++ b/src/lib-index/maildir/maildir-update.c	Sun Oct 06 06:09:36 2002 +0300
@@ -11,7 +11,7 @@
 	i_assert(path != NULL);
 
 	inbuf = io_buffer_create_mmap(fd, default_pool,
-				      MAIL_MMAP_BLOCK_SIZE, 0);
+				      MAIL_MMAP_BLOCK_SIZE, 0, FALSE);
 	mail_index_update_headers(update, inbuf, 0, NULL, NULL);
 	io_buffer_destroy(inbuf);
 	return TRUE;
--- a/src/lib-index/mbox/mbox-fsck.c	Sun Oct 06 05:54:35 2002 +0300
+++ b/src/lib-index/mbox/mbox-fsck.c	Sun Oct 06 06:09:36 2002 +0300
@@ -296,7 +296,7 @@
 		return mbox_set_syscall_error(index, "open()");
 
 	inbuf = io_buffer_create_mmap(fd, default_pool,
-				      MAIL_MMAP_BLOCK_SIZE, 0);
+				      MAIL_MMAP_BLOCK_SIZE, 0, TRUE);
 
 	if (!mbox_lock(index, index->mbox_path, fd, FALSE))
 		failed = TRUE;
@@ -305,9 +305,6 @@
 		(void)mbox_unlock(index, index->mbox_path, fd);
 	}
 
-	if (close(fd) < 0)
-		mbox_set_syscall_error(index, "close()");
-
 	io_buffer_destroy(inbuf);
 
 	if (failed)
--- a/src/lib-index/mbox/mbox-open.c	Sun Oct 06 05:54:35 2002 +0300
+++ b/src/lib-index/mbox/mbox-open.c	Sun Oct 06 06:09:36 2002 +0300
@@ -92,6 +92,6 @@
 	} else {
 		return io_buffer_create_mmap(fd, default_pool,
 					     MAIL_MMAP_BLOCK_SIZE,
-					     stop_offset - offset);
+					     stop_offset - offset, TRUE);
 	}
 }
--- a/src/lib-index/mbox/mbox-rebuild.c	Sun Oct 06 05:54:35 2002 +0300
+++ b/src/lib-index/mbox/mbox-rebuild.c	Sun Oct 06 06:09:36 2002 +0300
@@ -56,12 +56,11 @@
 	}
 
 	inbuf = io_buffer_create_mmap(fd, default_pool,
-				      MAIL_MMAP_BLOCK_SIZE, 0);
+				      MAIL_MMAP_BLOCK_SIZE, 0, TRUE);
 	mbox_skip_empty_lines(inbuf);
 	failed = !mbox_index_append(index, inbuf);
 
 	(void)mbox_unlock(index, index->mbox_path, fd);
-	if (close(fd) < 0) mbox_set_syscall_error(index, "close()");
 	io_buffer_destroy(inbuf);
 
 	if (failed)
--- a/src/lib-index/mbox/mbox-rewrite.c	Sun Oct 06 05:54:35 2002 +0300
+++ b/src/lib-index/mbox/mbox-rewrite.c	Sun Oct 06 06:09:36 2002 +0300
@@ -343,8 +343,8 @@
 	if (lseek(out_fd, (off_t)out_offset, SEEK_SET) < 0)
 		return -1;
 
-	inbuf = io_buffer_create_mmap(in_fd, default_pool, 65536, 0);
-	outbuf = io_buffer_create_file(out_fd, default_pool, 1024);
+	inbuf = io_buffer_create_mmap(in_fd, default_pool, 65536, 0, FALSE);
+	outbuf = io_buffer_create_file(out_fd, default_pool, 1024, FALSE);
 
 	ret = io_buffer_send_iobuffer(outbuf, inbuf, inbuf->size);
 	if (ret < 0)
@@ -431,8 +431,8 @@
 	dirty_offset = 0;
 
 	inbuf = io_buffer_create_mmap(mbox_fd, default_pool,
-				      MAIL_MMAP_BLOCK_SIZE, 0);
-	outbuf = io_buffer_create_file(tmp_fd, default_pool, 8192);
+				      MAIL_MMAP_BLOCK_SIZE, 0, FALSE);
+	outbuf = io_buffer_create_file(tmp_fd, default_pool, 8192, FALSE);
 
 	failed = FALSE; seq = 1;
 	rec = index->lookup(index, 1);
--- a/src/lib-storage/index/index-copy.c	Sun Oct 06 05:54:35 2002 +0300
+++ b/src/lib-storage/index/index-copy.c	Sun Oct 06 06:09:36 2002 +0300
@@ -30,7 +30,6 @@
 				  ctx->custom_flags, rec->internal_date,
 				  inbuf, inbuf->size);
 
-	(void)close(inbuf->fd);
 	io_buffer_destroy(inbuf);
 	return !failed;
 }
--- a/src/lib-storage/index/index-msgcache.c	Sun Oct 06 05:54:35 2002 +0300
+++ b/src/lib-storage/index/index-msgcache.c	Sun Oct 06 06:09:36 2002 +0300
@@ -37,7 +37,6 @@
 	if (!io_buffer_seek(inbuf, 0)) {
 		i_error("index_msgcache_inbuf_rewind: lseek() failed: %m");
 
-		(void)close(inbuf->fd);
 		io_buffer_destroy(inbuf);
 		return NULL;
 	}
--- a/src/lib-storage/index/index-search.c	Sun Oct 06 05:54:35 2002 +0300
+++ b/src/lib-storage/index/index-search.c	Sun Oct 06 06:09:36 2002 +0300
@@ -551,7 +551,6 @@
 				      search_text_body);
 	}
 
-	(void)close(inbuf->fd);
 	io_buffer_destroy(inbuf);
 	return TRUE;
 }
--- a/src/lib-storage/index/mbox/mbox-expunge.c	Sun Oct 06 05:54:35 2002 +0300
+++ b/src/lib-storage/index/mbox/mbox-expunge.c	Sun Oct 06 06:09:36 2002 +0300
@@ -131,8 +131,8 @@
 	}
 
 	inbuf = io_buffer_create_mmap(fd, default_pool,
-				      MAIL_MMAP_BLOCK_SIZE, 0);
-	outbuf = io_buffer_create_file(fd, default_pool, 4096);
+				      MAIL_MMAP_BLOCK_SIZE, 0, FALSE);
+	outbuf = io_buffer_create_file(fd, default_pool, 4096, FALSE);
 
 	failed = !expunge_real(ibox, rec, seq, inbuf, outbuf,
 			       expunge_func, context);
--- a/src/lib/iobuffer.c	Sun Oct 06 05:54:35 2002 +0300
+++ b/src/lib/iobuffer.c	Sun Oct 06 06:09:36 2002 +0300
@@ -65,17 +65,19 @@
 	return buf;
 }
 
-IOBuffer *io_buffer_create_file(int fd, Pool pool, size_t max_buffer_size)
+IOBuffer *io_buffer_create_file(int fd, Pool pool, size_t max_buffer_size,
+				int autoclose_fd)
 {
 	IOBuffer *buf;
 
 	buf = io_buffer_create(fd, pool, IO_PRIORITY_DEFAULT, max_buffer_size);
 	buf->file = TRUE;
+	buf->close_file = autoclose_fd;
         return buf;
 }
 
 IOBuffer *io_buffer_create_mmap(int fd, Pool pool, size_t block_size,
-				uoff_t size)
+				uoff_t size, int autoclose_fd)
 {
 	IOBuffer *buf;
 	off_t start_offset, stop_offset;
@@ -93,7 +95,7 @@
 		block_size += mmap_pagesize;
 	}
 
-	buf = io_buffer_create_file(fd, pool, block_size);
+	buf = io_buffer_create_file(fd, pool, block_size, autoclose_fd);
 	buf->mmaped = TRUE;
 	buf->receive = TRUE;
 
@@ -141,6 +143,7 @@
 			}
 		}
 	}
+	io_buffer_close(buf);
         p_free(buf->pool, buf);
 }
 
@@ -149,7 +152,12 @@
 	if (buf == NULL)
 		return;
 
-        buf->closed = TRUE;
+	buf->closed = TRUE;
+	if (buf->close_file && buf->fd != -1) {
+		if (close(buf->fd) < 0)
+			i_error("io_buffer_close(): close() failed: %m");
+		buf->fd = -1;
+	}
 }
 
 void io_buffer_reset(IOBuffer *buf)
@@ -244,8 +252,8 @@
 	}
 
 	if (ret < 0) {
-		buf->closed = TRUE;
 		buf->buf_errno = errno;
+		io_buffer_close(buf);
 	} else {
 		buf->offset += ret;
 		buf->skip += ret;
@@ -297,8 +305,8 @@
 			my_write(ctx->outbuf->fd, ctx->data, size);
 
 		if (ret < 0) {
-			ctx->outbuf->closed = TRUE;
 			ctx->outbuf->buf_errno = errno;
+                        io_buffer_close(ctx->outbuf);
 		} else {
 			ctx->outbuf->offset += ret;
 			ctx->data += ret;
@@ -444,8 +452,8 @@
 				net_transmit(buf->fd, data, size);
 			if (ret < 0) {
 				/* disconnected */
-				buf->closed = TRUE;
 				buf->buf_errno = errno;
+				io_buffer_close(buf);
 				return -1;
 			}
 
@@ -499,7 +507,7 @@
 	if (ret < 0) {
 		if (errno != EINTR && errno != EAGAIN) {
 			ctx->outbuf->buf_errno = errno;
-			ctx->outbuf->closed = TRUE;
+                        io_buffer_close(ctx->outbuf);
 		}
 		ret = 0;
 	}
@@ -591,6 +599,9 @@
 	i_assert(size < OFF_T_MAX);
 	i_assert(inbuf->limit > 0 || size <= inbuf->limit - inbuf->offset);
 
+	if (inbuf->closed || outbuf->closed)
+		return -1;
+
 	ret = io_buffer_sendfile(outbuf, inbuf, size);
 	if (ret > 0 || outbuf->buf_errno != EINVAL)
 		return ret < 0 ? -1 : 1;
@@ -857,6 +868,9 @@
 {
 	uoff_t real_offset;
 
+	if (buf->closed)
+		return FALSE;
+
 	real_offset = buf->start_offset + offset;
 	if (real_offset > OFF_T_MAX) {
 		errno = EINVAL;
@@ -992,14 +1006,17 @@
 	i_assert(size <= SSIZE_T_MAX);
 	i_assert(!buf->receive);
 
+	if (buf->closed)
+		return -1;
+
 	if (buf->pos == 0 && !buf->corked) {
 		/* buffer is empty, try to send the data immediately */
 		ret = buf->file ? my_write(buf->fd, buf->buffer, size) :
 			net_transmit(buf->fd, buf->buffer, size);
 		if (ret < 0) {
 			/* disconnected */
-			buf->closed = TRUE;
 			buf->buf_errno = errno;
+			io_buffer_close(buf);
 			return -1;
 		}
 
--- a/src/lib/iobuffer.h	Sun Oct 06 05:54:35 2002 +0300
+++ b/src/lib/iobuffer.h	Sun Oct 06 06:09:36 2002 +0300
@@ -34,6 +34,7 @@
 	size_t buffer_size, max_buffer_size;
 
 	unsigned int file:1; /* reading/writing a file */
+	unsigned int close_file:1; /* io_buffer_close() actually close()s fd */
 	unsigned int mmaped:1; /* reading a file with mmap() */
 	unsigned int closed:1; /* all further read/writes will return 0 */
 	unsigned int transmit:1; /* this is a transmit buffer */
@@ -49,11 +50,12 @@
 IOBuffer *io_buffer_create(int fd, Pool pool, int priority,
 			   size_t max_buffer_size);
 /* Same as io_buffer_create(), but specify that we're reading/writing file. */
-IOBuffer *io_buffer_create_file(int fd, Pool pool, size_t max_buffer_size);
+IOBuffer *io_buffer_create_file(int fd, Pool pool, size_t max_buffer_size,
+				int autoclose_fd);
 /* Read the file by mmap()ing it in blocks. stop_offset specifies where to
    stop reading, or 0 to end of file. */
 IOBuffer *io_buffer_create_mmap(int fd, Pool pool, size_t block_size,
-				uoff_t size);
+				uoff_t size, int autoclose_fd);
 /* Destroy a buffer. */
 void io_buffer_destroy(IOBuffer *buf);
 /* Mark the buffer closed. Any sends/reads after this will return -1.
--- a/src/master/settings.c	Sun Oct 06 05:54:35 2002 +0300
+++ b/src/master/settings.c	Sun Oct 06 06:09:36 2002 +0300
@@ -289,7 +289,7 @@
 		i_fatal("Can't open configuration file %s: %m", path);
 
 	linenum = 0;
-	inbuf = io_buffer_create_file(fd, default_pool, 2048);
+	inbuf = io_buffer_create_file(fd, default_pool, 2048, TRUE);
 	for (;;) {
 		line = io_buffer_next_line(inbuf);
 		if (line == NULL) {
@@ -339,7 +339,6 @@
 	};
 
 	io_buffer_destroy(inbuf);
-	(void)close(fd);
 
         settings_verify();
 }