changeset 416:cca1c7004a6f HEAD

Bugfixes to mbox and OBuffer handling.
author Timo Sirainen <tss@iki.fi>
date Mon, 14 Oct 2002 18:13:07 +0300
parents bd30c10f2864
children 829e85f92aa6
files src/lib-index/mbox/mbox-open.c src/lib-index/mbox/mbox-rewrite.c src/lib-storage/index/mbox/mbox-expunge.c src/lib/obuffer-file.c src/lib/obuffer.h
diffstat 5 files changed, 25 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-index/mbox/mbox-open.c	Mon Oct 14 04:16:05 2002 +0300
+++ b/src/lib-index/mbox/mbox-open.c	Mon Oct 14 18:13:07 2002 +0300
@@ -12,7 +12,7 @@
 IBuffer *mbox_open_mail(MailIndex *index, MailIndexRecord *rec)
 {
 	IBuffer *inbuf;
-	uoff_t offset, stop_offset;
+	uoff_t offset, v_stop_offset;
 	const unsigned char *data;
 	size_t size;
 	int failed;
@@ -26,7 +26,7 @@
 	if (!mbox_mail_get_start_offset(index, rec, &offset))
 		return NULL;
 
-	stop_offset = offset + rec->header_size + rec->body_size;
+	v_stop_offset = rec->header_size + rec->body_size;
 
 	inbuf = mbox_file_open(index, offset, FALSE);
 	if (inbuf == NULL)
@@ -34,7 +34,7 @@
 
 	/* make sure message size is valid - it must end with
 	   either EOF or "\nFrom "*/
-	if (!i_buffer_seek(inbuf, stop_offset - offset)) {
+	if (!i_buffer_seek(inbuf, v_stop_offset)) {
 		errno = inbuf->buf_errno;
 		mbox_set_syscall_error(index, "i_buffer_seek()");
 		i_buffer_unref(inbuf);
@@ -73,12 +73,13 @@
 		/* file has been updated, rescan it */
 		index->set_flags |= MAIL_INDEX_FLAG_FSCK;
 
-		index_set_error(index, "mbox file %s was modified "
-				"unexpectedly, fscking", index->mbox_path);
+		index_set_error(index,
+			"mbox file %s was modified unexpectedly, fscking",
+			index->mbox_path);
 		i_buffer_unref(inbuf);
 		return NULL;
 	}
 
-	i_buffer_set_read_limit(inbuf, stop_offset - offset);
+	i_buffer_set_read_limit(inbuf, v_stop_offset);
 	return inbuf;
 }
--- a/src/lib-index/mbox/mbox-rewrite.c	Mon Oct 14 04:16:05 2002 +0300
+++ b/src/lib-index/mbox/mbox-rewrite.c	Mon Oct 14 18:13:07 2002 +0300
@@ -57,7 +57,7 @@
 	i_assert(inbuf->v_offset <= end_offset);
 
 	old_limit = inbuf->v_limit;
-	i_buffer_set_read_limit(inbuf, end_offset - inbuf->v_offset);
+	i_buffer_set_read_limit(inbuf, end_offset);
 	if (o_buffer_send_ibuffer(outbuf, inbuf) < 0) {
 		index_set_error(index, "Error rewriting mbox file %s: %s",
 				index->mbox_path, strerror(outbuf->buf_errno));
--- a/src/lib-storage/index/mbox/mbox-expunge.c	Mon Oct 14 04:16:05 2002 +0300
+++ b/src/lib-storage/index/mbox/mbox-expunge.c	Mon Oct 14 18:13:07 2002 +0300
@@ -70,7 +70,7 @@
 			i_assert(inbuf->v_offset <= from_offset);
 			i_buffer_skip(inbuf, from_offset - inbuf->v_offset);
 
-			if (outbuf->v_offset == 0) {
+			if (outbuf->offset == 0) {
 				/* we're writing to beginning of mbox, so we
 				   don't want the [\r]\n there */
 				(void)i_buffer_read_data(inbuf, &data,
@@ -82,8 +82,7 @@
 					i_buffer_skip(inbuf, 2);
 			}
 
-			i_buffer_set_read_limit(inbuf,
-						end_offset - inbuf->v_offset);
+			i_buffer_set_read_limit(inbuf, end_offset);
 			failed = o_buffer_send_ibuffer(outbuf, inbuf) < 0;
 			i_buffer_set_read_limit(inbuf, old_limit);
 
@@ -101,14 +100,10 @@
 	   as well just appended more data.. but if we've deleted all mail,
 	   don't write the only \n there. */
 	copy_size = inbuf->v_size - inbuf->v_offset;
-	if (outbuf->v_offset == 0 && copy_size == 1)
+	if (outbuf->offset == 0 && copy_size == 1)
 		return TRUE;
 
-	i_buffer_set_read_limit(inbuf, copy_size);
-	failed = o_buffer_send_ibuffer(outbuf, inbuf) < 0;
-	i_buffer_set_read_limit(inbuf, old_limit);
-
-	return !failed && inbuf->v_offset == end_offset;
+	return o_buffer_send_ibuffer(outbuf, inbuf) >= 0;
 }
 
 int mbox_expunge_locked(IndexMailbox *ibox,
@@ -144,13 +139,13 @@
 	failed = !expunge_real(ibox, rec, seq, inbuf, outbuf,
 			       expunge_func, context);
 
-	if (failed && outbuf->v_offset > 0) {
+	if (failed && outbuf->offset > 0) {
 		/* we moved some of the data. move the rest as well so there
 		   won't be invalid holes in mbox file */
 		(void)o_buffer_send_ibuffer(outbuf, inbuf);
 	}
 
-	if (ftruncate(ibox->index->mbox_fd, outbuf->v_offset) < 0) {
+	if (ftruncate(ibox->index->mbox_fd, outbuf->offset) < 0) {
 		mail_storage_set_error(ibox->box.storage, "ftruncate() failed "
 				       "for mbox file %s: %m",
 				       ibox->index->mbox_path);
--- a/src/lib/obuffer-file.c	Mon Oct 14 04:16:05 2002 +0300
+++ b/src/lib/obuffer-file.c	Mon Oct 14 18:13:07 2002 +0300
@@ -202,7 +202,7 @@
 
 	update_iovec(iov, iov_size, ret);
 	update_buffer(fbuf, ret);
-	fbuf->obuf.obuffer.v_offset += ret;
+	fbuf->obuf.obuffer.offset += ret;
 
 	return ret;
 }
@@ -392,7 +392,7 @@
 	}
 
 	buf->obuffer.buf_errno = 0;
-	buf->obuffer.v_offset = 0;
+	buf->obuffer.offset = offset;
 	return 1;
 }
 
@@ -528,8 +528,8 @@
 	in_fd = i_buffer_get_fd(ctx->inbuf);
 	i_assert(in_fd != -1);
 
-	offset = ctx->inbuf->v_offset;
-	send_size = ctx->inbuf->v_limit - offset;
+	offset = ctx->inbuf->start_offset + ctx->inbuf->v_offset;
+	send_size = ctx->inbuf->v_limit - ctx->inbuf->v_offset;
 
 	ret = safe_sendfile(ctx->fbuf->fd, in_fd, &offset,
 			    MAX_SSIZE_T(send_size));
@@ -542,7 +542,7 @@
 	}
 
 	i_buffer_skip(ctx->inbuf, (size_t)ret);
-	outbuf->v_offset += ret;
+	outbuf->offset += ret;
 
 	if (outbuf->closed || (size_t)ret == send_size)
 		io_loop_stop(ctx->ioloop);
@@ -594,8 +594,8 @@
 		return -1;
 
 	/* first try if we can do it with a single sendfile() call */
-	offset = inbuf->v_offset;
-	send_size = inbuf->v_limit - offset;
+	offset = inbuf->start_offset + inbuf->v_offset;
+	send_size = inbuf->v_limit - inbuf->v_offset;
 
 	s_ret = safe_sendfile(foutbuf->fd, in_fd, &offset,
 			      MAX_SSIZE_T(send_size));
@@ -608,7 +608,7 @@
 	}
 
 	i_buffer_skip(inbuf, (size_t)s_ret);
-	outbuf->obuffer.v_offset += s_ret;
+	outbuf->obuffer.offset += s_ret;
 
 	if ((uoff_t)s_ret == send_size) {
 		/* yes, all sent */
--- a/src/lib/obuffer.h	Mon Oct 14 04:16:05 2002 +0300
+++ b/src/lib/obuffer.h	Mon Oct 14 18:13:07 2002 +0300
@@ -4,7 +4,7 @@
 #include "ioloop.h" /* TimeoutFunc */
 
 struct _OBuffer {
-	uoff_t v_offset; /* relative to start offset */
+	uoff_t offset;
 
 	int buf_errno;
 	unsigned int closed:1;
@@ -43,8 +43,8 @@
    max_size, 0 if not. */
 int o_buffer_have_space(OBuffer *buf, size_t size);
 
-/* Seek to specified position from beginning of file and v_offset is set to 0.
-   This works only for files. Returns 1 if successful, -1 if error. */
+/* Seek to specified position from beginning of file. This works only for
+   files. Returns 1 if successful, -1 if error. */
 int o_buffer_seek(OBuffer *buf, uoff_t offset);
 /* Returns number of bytes sent or buffered, or -1 if disconnected */
 ssize_t o_buffer_send(OBuffer *buf, const void *data, size_t size);