changeset 232:9277e893304e HEAD

s/io_buffer_read_data/io_buffer_read_data_blocking/ and fixed the various kludges elsewhere to use it more easily
author Timo Sirainen <tss@iki.fi>
date Sun, 15 Sep 2002 14:09:08 +0300
parents bbd248788571
children 3f92df43cfa7
files src/lib-imap/imap-message-cache.c src/lib-index/mbox/mbox-append.c src/lib-index/mbox/mbox-fsck.c src/lib-index/mbox/mbox-index.c src/lib-mail/message-parser.c src/lib-mail/message-send.c src/lib-mail/message-size.c src/lib-storage/index/mbox/mbox-expunge.c src/lib/iobuffer.c src/lib/iobuffer.h
diffstat 10 files changed, 92 insertions(+), 130 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-imap/imap-message-cache.c	Sun Sep 15 11:36:14 2002 +0300
+++ b/src/lib-imap/imap-message-cache.c	Sun Sep 15 14:09:08 2002 +0300
@@ -446,12 +446,9 @@
 
 	if (!cr_skipped) {
 		/* see if we need to add virtual CR */
-		while (io_buffer_read_data(inbuf, &msg, &size, 0) >= 0) {
-			if (size > 0) {
-				if (msg[0] == '\n')
-					dest->virtual_size++;
-				break;
-			}
+		if (io_buffer_read_data_blocking(inbuf, &msg, &size, 0) > 0) {
+			if (msg[0] == '\n')
+				dest->virtual_size++;
 		}
 	}
 
--- a/src/lib-index/mbox/mbox-append.c	Sun Sep 15 11:36:14 2002 +0300
+++ b/src/lib-index/mbox/mbox-append.c	Sun Sep 15 14:09:08 2002 +0300
@@ -31,7 +31,7 @@
 
 	/* read until "[\r]\nFrom " is found */
 	startpos = i = 0; lastmsg = TRUE;
-	while (io_buffer_read_data(inbuf, &msg, &size, startpos) >= 0) {
+	while (io_buffer_read_data_blocking(inbuf, &msg, &size, startpos) > 0) {
 		for (i = startpos; i < size; i++) {
 			if (msg[i] == ' ' && i >= 5) {
 				/* See if it's space after "From" */
@@ -53,12 +53,10 @@
 			break;
 		}
 
-		if (i > 0) {
-			startpos = i < 7 ? i : 7;
-			i -= startpos;
+		startpos = i < 7 ? i : 7;
+		i -= startpos;
 
-			io_buffer_skip(inbuf, i);
-		}
+		io_buffer_skip(inbuf, i);
 	}
 
 	if (lastmsg && startpos > 0) {
@@ -88,7 +86,7 @@
 
 	/* get the From-line */
 	pos = 0;
-	while (io_buffer_read_data(inbuf, &data, &size, pos) >= 0) {
+	while (io_buffer_read_data_blocking(inbuf, &data, &size, pos) > 0) {
 		for (; pos < size; pos++) {
 			if (data[pos] == '\n')
 				break;
--- a/src/lib-index/mbox/mbox-fsck.c	Sun Sep 15 11:36:14 2002 +0300
+++ b/src/lib-index/mbox/mbox-fsck.c	Sun Sep 15 14:09:08 2002 +0300
@@ -17,15 +17,12 @@
 	unsigned char *msg;
 	size_t i, size;
 
-	while (io_buffer_read_data(inbuf, &msg, &size, 0) >= 0) {
+	while (io_buffer_read_data_blocking(inbuf, &msg, &size, 0) > 0) {
 		for (i = 0; i < size; i++) {
-			if (msg[i] == '\n')
-				break;
-		}
-
-		if (i < size) {
-			io_buffer_skip(inbuf, i+1);
-			break;
+			if (msg[i] == '\n') {
+				io_buffer_skip(inbuf, i+1);
+				return;
+			}
 		}
 
 		io_buffer_skip(inbuf, i);
@@ -60,7 +57,7 @@
 	}
 
 	/* read forward a bit */
-	if (io_buffer_read_data(inbuf, &data, &size, 6) <= 0)
+	if (io_buffer_read_data_blocking(inbuf, &data, &size, 6) < 0)
 		return FALSE;
 
 	/* either there should be the next From-line,
@@ -199,12 +196,8 @@
 		return FALSE;
 
 	/* first make sure we start with a "From " line. */
-	while (io_buffer_read_data(inbuf, &data, &size, 5) >= 0) {
-		if (size > 5)
-			break;
-	}
-
-	if (size <= 5 || strncmp(data, "From ", 5) != 0) {
+	if (io_buffer_read_data_blocking(inbuf, &data, &size, 5) < 0 ||
+	    strncmp(data, "From ", 5) != 0) {
 		index_set_error(index, "File isn't in mbox format: %s",
 				index->mbox_path);
 		return FALSE;
--- a/src/lib-index/mbox/mbox-index.c	Sun Sep 15 11:36:14 2002 +0300
+++ b/src/lib-index/mbox/mbox-index.c	Sun Sep 15 14:09:08 2002 +0300
@@ -277,8 +277,8 @@
 	size_t size, pos;
 
 	pos = 0;
-	while (io_buffer_read_data(inbuf, &data, &size, pos) >= 0) {
-		if (size > 0 && pos == 0) {
+	while (io_buffer_read_data_blocking(inbuf, &data, &size, pos) > 0) {
+		if (pos == 0) {
 			if (data[0] == '\n') {
 				io_buffer_skip(inbuf, 1);
 				return TRUE;
@@ -288,6 +288,7 @@
 
 			pos++;
 		}
+
 		if (size > 1 && pos == 1) {
 			if (data[1] != '\n')
 				return FALSE;
--- a/src/lib-mail/message-parser.c	Sun Sep 15 11:36:14 2002 +0300
+++ b/src/lib-mail/message-parser.c	Sun Sep 15 14:09:08 2002 +0300
@@ -267,7 +267,7 @@
 
 	startpos = 0;
 
-	while (io_buffer_read_data(inbuf, &msg, &size, startpos) >= 0) {
+	while (io_buffer_read_data_blocking(inbuf, &msg, &size, startpos) > 0) {
 		for (i = startpos; i < size; i++) {
 			if (msg[i] == '\n') {
 				if (msg_size != NULL) {
@@ -284,15 +284,13 @@
 			break;
 		}
 
-		if (i > 0) {
-			/* leave the last character, it may be \r */
-			io_buffer_skip(inbuf, i - 1);
-			startpos = 1;
+		/* leave the last character, it may be \r */
+		io_buffer_skip(inbuf, i - 1);
+		startpos = 1;
 
-			if (msg_size != NULL) {
-				msg_size->physical_size += i - 1;
-				msg_size->virtual_size += i - 1;
-			}
+		if (msg_size != NULL) {
+			msg_size->physical_size += i - 1;
+			msg_size->virtual_size += i - 1;
 		}
 	}
 
@@ -318,8 +316,8 @@
 
 	missing_cr_count = startpos = line_start = 0;
 	colon_pos = UINT_MAX;
-	while ((ret = io_buffer_read_data(inbuf, &msg,
-					  &size, startpos+1)) != -1) {
+	while ((ret = io_buffer_read_data_blocking(inbuf, &msg, &size,
+						   startpos+1)) != -1) {
 		if (ret == -2) {
 			/* overflow, line is too long. just skip it. */
 			i_assert(size > 2);
@@ -330,11 +328,6 @@
 			continue;
 		}
 
-		if (size == 0) {
-			/* no, we never want empty buffer */
-			continue;
-		}
-
 		/* don't parse the last character, so we can always have
 		   one character read-ahead. we never care about the last
 		   character anyway, it's either the first character in
@@ -461,7 +454,7 @@
 	boundary = NULL;
 	missing_cr_count = startpos = line_start = 0;
 
-	while (io_buffer_read_data(inbuf, &msg, &size, startpos) >= 0) {
+	while (io_buffer_read_data_blocking(inbuf, &msg, &size, startpos) > 0) {
 		for (i = startpos; i < size; i++) {
 			if (msg[i] != '\n')
 				continue;
@@ -485,41 +478,37 @@
 			line_start = i+1;
 		}
 
-		if (boundary != NULL) {
-			/* boundary found */
+		if (boundary != NULL)
 			break;
+
+		if (i - line_start > 128 &&
+		    msg[line_start] == '-' && msg[line_start+1] == '-') {
+			/* long partial line, see if it's a boundary.
+			   RFC-2046 says that the boundaries must be
+			   70 chars without "--" or less. We allow
+			   a bit larger.. */
+			boundary = boundary_find(boundaries,
+						 msg + line_start + 2,
+						 i - line_start - 2);
+			if (boundary != NULL)
+				break;
+
+			/* nope, we can skip over the line, just
+			   leave the last char since it may be \r */
+			i--;
+		} else {
+			/* leave the last line to buffer, it may be
+			   boundary */
+			i = line_start;
+			if (i > 2) i -= 2; /* leave the \r\n too */
+			line_start -= i;
 		}
 
-		if (i > 0) {
-			if (i - line_start > 128 &&
-			    msg[line_start] == '-' && msg[line_start+1] == '-') {
-				/* long partial line, see if it's a boundary.
-				   RFC-2046 says that the boundaries must be
-				   70 chars without "--" or less. We allow
-				   a bit larger.. */
-				boundary = boundary_find(boundaries,
-							 msg + line_start + 2,
-							 i - line_start - 2);
-				if (boundary != NULL)
-					break;
+		io_buffer_skip(inbuf, i);
+		msg_size->physical_size += i;
+		msg_size->virtual_size += i;
 
-				/* nope, we can skip over the line, just
-				   leave the last char since it may be \r */
-				i--;
-			} else {
-				/* leave the last line to buffer, it may be
-				   boundary */
-				i = line_start;
-				if (i > 2) i -= 2; /* leave the \r\n too */
-				line_start -= i;
-			}
-
-			io_buffer_skip(inbuf, i);
-			msg_size->physical_size += i;
-			msg_size->virtual_size += i;
-
-			startpos = size - i;
-		}
+		startpos = size - i;
 	}
 
 	if (boundary != NULL) {
@@ -582,11 +571,9 @@
 
 	/* now, see if it's end boundary */
 	end_boundary = FALSE;
-	while (io_buffer_read_data(inbuf, &msg, &size, 1) >= 0) {
-		if (size >= 2) {
-			end_boundary = msg[0] == '-' && msg[1] == '-';
-			break;
-		}
+	if (io_buffer_read_data_blocking(inbuf, &msg, &size, 1) > 0) {
+		i_assert(size >= 2);
+		end_boundary = msg[0] == '-' && msg[1] == '-';
 	}
 
 	/* skip the rest of the line */
--- a/src/lib-mail/message-send.c	Sun Sep 15 11:36:14 2002 +0300
+++ b/src/lib-mail/message-send.c	Sun Sep 15 14:09:08 2002 +0300
@@ -29,7 +29,7 @@
 	message_skip_virtual(inbuf, virtual_skip, NULL, &cr_skipped);
 
 	/* go through the message data and insert CRs where needed.  */
-	while (io_buffer_read_data(inbuf, &msg, &size, 0) >= 0) {
+	while (io_buffer_read_data_blocking(inbuf, &msg, &size, 0) > 0) {
 		add_cr = FALSE;
 		for (i = 0; i < size; i++) {
 			if (msg[i] == '\n') {
--- a/src/lib-mail/message-size.c	Sun Sep 15 11:36:14 2002 +0300
+++ b/src/lib-mail/message-size.c	Sun Sep 15 14:09:08 2002 +0300
@@ -13,7 +13,7 @@
 	memset(hdr, 0, sizeof(MessageSize));
 
 	missing_cr_count = 0; startpos = 0;
-	while (io_buffer_read_data(inbuf, &msg, &size, startpos) >= 0) {
+	while (io_buffer_read_data_blocking(inbuf, &msg, &size, startpos) > 0) {
 		for (i = startpos; i < size; i++) {
 			if (msg[i] != '\n')
 				continue;
@@ -42,13 +42,11 @@
 			break;
 		}
 
-		if (i > 0) {
-			/* leave the last two characters, they may be \r\n */
-			startpos = size == 1 ? 1 : 2;
-			io_buffer_skip(inbuf, i - startpos);
+		/* leave the last two characters, they may be \r\n */
+		startpos = size == 1 ? 1 : 2;
+		io_buffer_skip(inbuf, i - startpos);
 
-			hdr->physical_size += i - startpos;
-		}
+		hdr->physical_size += i - startpos;
 	}
 	io_buffer_skip(inbuf, startpos);
 	hdr->physical_size += startpos;
@@ -67,7 +65,7 @@
 
 	missing_cr_count = 0; startpos = 0;
 	while (max_virtual_size != 0 &&
-	       io_buffer_read_data(inbuf, &msg, &size, startpos) >= 0) {
+	       io_buffer_read_data_blocking(inbuf, &msg, &size, startpos) > 0) {
 		for (i = startpos; i < size && max_virtual_size != 0; i++) {
 			if (max_virtual_size > 0)
 				max_virtual_size--;
@@ -92,13 +90,11 @@
 			body->lines++;
 		}
 
-		if (i > 0) {
-			/* leave the last character, it may be \r */
-			io_buffer_skip(inbuf, i - 1);
-			startpos = 1;
+		/* leave the last character, it may be \r */
+		io_buffer_skip(inbuf, i - 1);
+		startpos = 1;
 
-			body->physical_size += i - 1;
-		}
+		body->physical_size += i - 1;
 	}
 	io_buffer_skip(inbuf, startpos);
 	body->physical_size += startpos;
@@ -118,7 +114,7 @@
 		return;
 
 	startpos = 0;
-	while (io_buffer_read_data(inbuf, &msg, &size, startpos) >= 0) {
+	while (io_buffer_read_data_blocking(inbuf, &msg, &size, startpos) > 0) {
 		for (i = startpos; i < size && virtual_skip > 0; i++) {
 			virtual_skip--;
 
@@ -159,11 +155,9 @@
 			break;
 		}
 
-		if (i > 0) {
-			/* leave the last character, it may be \r */
-			io_buffer_skip(inbuf, i - 1);
-			startpos = 1;
-		}
+		/* leave the last character, it may be \r */
+		io_buffer_skip(inbuf, i - 1);
+		startpos = 1;
 	}
 }
 
--- a/src/lib-storage/index/mbox/mbox-expunge.c	Sun Sep 15 11:36:14 2002 +0300
+++ b/src/lib-storage/index/mbox/mbox-expunge.c	Sun Sep 15 14:09:08 2002 +0300
@@ -70,8 +70,8 @@
 			if (outbuf->offset == 0) {
 				/* we're writing to beginning of mbox, so we
 				   don't want the [\r]\n there */
-				while (io_buffer_read_data(inbuf, &data,
-							   &size, 1) == 0) ;
+				(void)io_buffer_read_data_blocking(inbuf, &data,
+								   &size, 1);
 				if (size > 0 && data[0] == '\n')
 					io_buffer_skip(inbuf, 1);
 				else if (size > 1 && data[0] == '\r' &&
--- a/src/lib/iobuffer.c	Sun Sep 15 11:36:14 2002 +0300
+++ b/src/lib/iobuffer.c	Sun Sep 15 14:09:08 2002 +0300
@@ -551,15 +551,10 @@
 {
 	unsigned char *in_data;
 	size_t size, full_size, sent_size, data_size;
-	ssize_t ret;
 
-	while ((ret = io_buffer_read_data(ctx->inbuf, &in_data,
-					  &size, 0)) <= 0) {
-		if (ret == -1) {
-			/* disconnected */
-			ctx->outbuf->closed = TRUE;
-			break;
-		}
+	if (io_buffer_read_data_blocking(ctx->inbuf, &in_data, &size, 0) < 0) {
+		io_loop_stop(ctx->ioloop);
+		return;
 	}
 
 	full_size = ctx->size;
@@ -900,28 +895,24 @@
         return buf->buffer + buf->skip;
 }
 
-int io_buffer_read_data(IOBuffer *buf, unsigned char **data,
-			size_t *size, size_t threshold)
+int io_buffer_read_data_blocking(IOBuffer *buf, unsigned char **data,
+				 size_t *size, size_t threshold)
 {
 	ssize_t ret;
 
-	if (buf->pos - buf->skip > threshold)
-		ret = 1;
-	else {
+	if (buf->pos - buf->skip <= threshold) {
 		/* we need more data */
-		ret = io_buffer_read(buf);
-		if (ret <= 0 && ret != -2) {
+		ret = io_buffer_read_blocking(buf, SSIZE_T_MAX);
+		if (ret < 0) {
 			*size = 0;
 			*data = NULL;
 			return ret;
 		}
-
-		if (ret > 0)
-			ret = 1;
 	}
 
 	*data = io_buffer_get_data(buf, size);
-	return ret;
+	i_assert(*size > 0);
+	return 1;
 }
 
 unsigned char *io_buffer_get_space(IOBuffer *buf, size_t size)
--- a/src/lib/iobuffer.h	Sun Sep 15 11:36:14 2002 +0300
+++ b/src/lib/iobuffer.h	Sun Sep 15 14:09:08 2002 +0300
@@ -114,7 +114,8 @@
 /* Blocking read, doesn't return until at least one byte is read, or until
    socket is disconnected or timeout has occured. Note that the fd must be
    nonblocking, or the timeout doesn't work. If you don't want limit size,
-   set it to SSIZE_T_MAX. Returns number of bytes read, or -1 if error. */
+   set it to SSIZE_T_MAX. Returns number of bytes read (never 0), -1 if
+   error or -2 if buffer is full. */
 ssize_t io_buffer_read_blocking(IOBuffer *buf, size_t size);
 /* Skip forward a number of bytes */
 void io_buffer_skip(IOBuffer *buf, uoff_t size);
@@ -129,10 +130,10 @@
    or NULL if there's no data. */
 unsigned char *io_buffer_get_data(IOBuffer *buf, size_t *size);
 /* Like io_buffer_get_data(), but read it when needed. There always must be
-   more than `threshold' bytes in buffer. Returns 1 if data was read, 0 if
-   read was interrupted or nonblocking, -1 if EOF / error */
-int io_buffer_read_data(IOBuffer *buf, unsigned char **data,
-			size_t *size, size_t threshold);
+   more than `threshold' bytes in buffer. Returns 1 if data was read,
+   -1 if EOF / error */
+int io_buffer_read_data_blocking(IOBuffer *buf, unsigned char **data,
+				 size_t *size, size_t threshold);
 
 /* Returns a pointer to buffer wanted amount of space,
    or NULL if size is too big. */