[dovecot-cvs] dovecot/src/lib-storage/index index-messageset.c,1.14,1.15 index-save.c,1.25,1.26 index-storage.h,1.32,1.33

cras at procontrol.fi cras at procontrol.fi
Thu Feb 20 01:37:25 EET 2003


Update of /home/cvs/dovecot/src/lib-storage/index
In directory danu:/tmp/cvs-serv2044/lib-storage/index

Modified Files:
	index-messageset.c index-save.c index-storage.h 
Log Message:
mbox: strip some headers when saving message. also always set Content-Length
header so message may safely contain lines beginning with "From ".



Index: index-messageset.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-storage/index/index-messageset.c,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -d -r1.14 -r1.15
--- index-messageset.c	22 Jan 2003 19:23:28 -0000	1.14
+++ index-messageset.c	19 Feb 2003 23:37:23 -0000	1.15
@@ -71,6 +71,11 @@
 {
 	int ret = ctx->ret;
 
+	if (ret == 0) {
+		/* we just didn't go through all of them */
+		ret = 1;
+	}
+
 	if (ret == 1 && ctx->expunges_found) {
 		/* some of the messages weren't found */
 		ret = 0;

Index: index-save.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-storage/index/index-save.c,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -d -r1.25 -r1.26
--- index-save.c	19 Feb 2003 21:31:35 -0000	1.25
+++ index-save.c	19 Feb 2003 23:37:23 -0000	1.26
@@ -9,6 +9,19 @@
 #include <stdlib.h>
 #include <unistd.h>
 
+struct save_header_context {
+	struct mail_storage *storage;
+	const char *path;
+
+	struct ostream *output;
+	write_func_t *write_func;
+
+	header_callback_t *header_callback;
+	void *context;
+
+	int failed;
+};
+
 static int write_with_crlf(struct ostream *output, const unsigned char *data,
 			   size_t size)
 {
@@ -70,8 +83,55 @@
 	return size;
 }
 
+static void set_write_error(struct mail_storage *storage,
+			    struct ostream *output, const char *path)
+{
+	errno = output->stream_errno;
+	if (errno == ENOSPC)
+		mail_storage_set_error(storage, "Not enough disk space");
+	else {
+		mail_storage_set_critical(storage,
+					  "Can't write to file %s: %m", path);
+	}
+}
+
+static void save_header_callback(struct message_part *part __attr_unused__,
+				 const unsigned char *name, size_t name_len,
+				 const unsigned char *value, size_t value_len,
+				 void *context)
+{
+	struct save_header_context *ctx = context;
+	int ret;
+
+	if (ctx->failed)
+		return;
+
+	ret = ctx->header_callback(name, name_len, ctx->write_func,
+				   ctx->context);
+	if (ret <= 0) {
+		if (ret < 0)
+			ctx->failed = TRUE;
+		return;
+	}
+
+	if (name_len == 0) {
+		name = "\n"; value_len = 1;
+	} else {
+		if (value[value_len] == '\r')
+			value_len++;
+		i_assert(value[value_len] == '\n');
+		value_len += (size_t) (value-name) + 1;
+	}
+
+	if (ctx->write_func(ctx->output, name, value_len) < 0) {
+		set_write_error(ctx->storage, ctx->output, ctx->path);
+		ctx->failed = TRUE;
+	}
+}
+
 int index_storage_save(struct mail_storage *storage, const char *path,
-		       struct istream *input, struct ostream *output)
+		       struct istream *input, struct ostream *output,
+		       header_callback_t *header_callback, void *context)
 {
 	int (*write_func)(struct ostream *, const unsigned char *, size_t);
 	const unsigned char *data;
@@ -81,8 +141,38 @@
 
 	write_func = getenv("MAIL_SAVE_CRLF") ? write_with_crlf : write_with_lf;
 
+	if (header_callback != NULL) {
+		struct save_header_context ctx;
+
+		memset(&ctx, 0, sizeof(ctx));
+		ctx.storage = storage;
+		ctx.output = output;
+		ctx.path = path;
+		ctx.write_func = write_func;
+		ctx.header_callback = header_callback;
+		ctx.context = context;
+
+		message_parse_header(NULL, input, NULL,
+				     save_header_callback, &ctx);
+
+		if (ctx.failed)
+			return FALSE;
+	}
+
 	failed = FALSE;
 	for (;;) {
+		data = i_stream_get_data(input, &size);
+		if (!failed) {
+			ret = write_func(output, data, size);
+			if (ret < 0) {
+				set_write_error(storage, output, path);
+				failed = TRUE;
+			} else {
+				size = ret;
+			}
+		}
+		i_stream_skip(input, size);
+
 		ret = i_stream_read(input);
 		if (ret < 0) {
 			errno = input->stream_errno;
@@ -105,27 +195,6 @@
 			failed = TRUE;
 			break;
 		}
-
-		data = i_stream_get_data(input, &size);
-		if (!failed) {
-			ret = write_func(output, data, size);
-			if (ret < 0) {
-				errno = output->stream_errno;
-				if (errno == ENOSPC) {
-					mail_storage_set_error(storage,
-						"Not enough disk space");
-				} else {
-					mail_storage_set_critical(storage,
-						"write_full() failed for file "
-						"%s: %m", path);
-				}
-				failed = TRUE;
-			} else {
-				size = ret;
-			}
-		}
-
-		i_stream_skip(input, size);
 	}
 
 	return !failed;

Index: index-storage.h
===================================================================
RCS file: /home/cvs/dovecot/src/lib-storage/index/index-storage.h,v
retrieving revision 1.32
retrieving revision 1.33
diff -u -d -r1.32 -r1.33
--- index-storage.h	14 Feb 2003 10:46:44 -0000	1.32
+++ index-storage.h	19 Feb 2003 23:37:23 -0000	1.33
@@ -5,6 +5,12 @@
 #include "mail-index.h"
 #include "index-mail.h"
 
+typedef int write_func_t(struct ostream *, const unsigned char *, size_t);
+
+/* Return -1 = failure, 0 = don't write the header, 1 = write it */
+typedef int header_callback_t(const unsigned char *name, size_t len,
+			      write_func_t *write_func, void *context);
+
 struct index_autosync_file {
 	struct index_autosync_file *next;
 
@@ -69,7 +75,8 @@
 		       unsigned int seq, int notify);
 
 int index_storage_save(struct mail_storage *storage, const char *path,
-		       struct istream *input, struct ostream *output);
+		       struct istream *input, struct ostream *output,
+		       header_callback_t *header_callback, void *context);
 
 void index_mailbox_check_add(struct index_mailbox *ibox, const char *path);
 void index_mailbox_check_remove_all(struct index_mailbox *ibox);




More information about the dovecot-cvs mailing list