changeset 196:95d21ab87eeb HEAD

X-IMAPbase is now parsed correctly.
author Timo Sirainen <tss@iki.fi>
date Mon, 09 Sep 2002 05:11:01 +0300
parents db6e288be0e9
children 38d77994baa5
files src/lib-index/mail-custom-flags.c src/lib-index/mail-custom-flags.h src/lib-index/mail-index.c src/lib-index/mbox/mbox-append.c src/lib-index/mbox/mbox-fsck.c src/lib-index/mbox/mbox-index.c src/lib-index/mbox/mbox-index.h src/lib-storage/index/index-storage.c src/lib-storage/index/mbox/mbox-storage.c
diffstat 9 files changed, 89 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-index/mail-custom-flags.c	Sun Sep 08 18:34:27 2002 +0300
+++ b/src/lib-index/mail-custom-flags.c	Mon Sep 09 05:11:01 2002 +0300
@@ -220,7 +220,7 @@
 	const char *path;
 	int fd;
 
-	path = t_strconcat(index->dir, CUSTOM_FLAGS_FILE_NAME, NULL);
+	path = t_strconcat(index->dir, "/", CUSTOM_FLAGS_FILE_NAME, NULL);
 	fd = open(path, O_RDWR | O_CREAT, 0660);
 	if (fd == -1) {
 		index_set_error(index, "Can't open custom flags file %s: %m",
@@ -444,10 +444,17 @@
 	return used_flags;
 }
 
-static int get_flag_index(MailCustomFlags *mcf, const char *flag)
+static int get_flag_index(MailCustomFlags *mcf, const char *flag,
+			  int index_hint)
 {
 	int i, first_empty;
 
+	if (index_hint >= 0 && index_hint < MAIL_CUSTOM_FLAGS_COUNT) {
+		if (mcf->custom_flags[index_hint] != NULL &&
+		    strcasecmp(mcf->custom_flags[index_hint], flag) == 0)
+			return index_hint;
+	}
+
 	/* check existing flags */
 	for (i = 0; i < MAIL_CUSTOM_FLAGS_COUNT; i++) {
 		if (mcf->custom_flags[i] == NULL)
@@ -487,10 +494,11 @@
 }
 
 int mail_custom_flags_fix_list(MailCustomFlags *mcf, MailFlags *flags,
-			       const char *custom_flags[])
+			       const char *custom_flags[], unsigned int count)
 {
 	MailFlags oldflags, flag;
-	int i, idx;
+	unsigned int i;
+	int idx;
 
 	i_assert(mcf->custom_flags_refcount == 0);
 
@@ -504,12 +512,12 @@
 	*flags &= MAIL_SYSTEM_FLAGS_MASK;
 
 	flag = MAIL_CUSTOM_FLAG_1;
-	for (i = 0; i < MAIL_CUSTOM_FLAGS_COUNT; i++, flag <<= 1) {
+	for (i = 0; i < count; i++, flag <<= 1) {
 		if (oldflags & flag) {
 			i_assert(custom_flags[i] != NULL &&
 				 *custom_flags[i] != '\0');
 
-			idx = get_flag_index(mcf, custom_flags[i]);
+			idx = get_flag_index(mcf, custom_flags[i], i);
 			if (idx == -1) {
 				(void)lock_file(mcf, F_UNLCK);
 				return 0;
--- a/src/lib-index/mail-custom-flags.h	Sun Sep 08 18:34:27 2002 +0300
+++ b/src/lib-index/mail-custom-flags.h	Mon Sep 09 05:11:01 2002 +0300
@@ -9,10 +9,11 @@
 void mail_custom_flags_free(MailCustomFlags *mcf);
 
 /* Change custom flags so that they reflect the real flag numbers in
-   the file. Returns 1 if ok, 0 if number of custom flags exceeded or
-   -1 if error */
+   the file. Initially flags contains the custom flags in the order of the
+   specified list, it's modified to reflect the actual list. Returns 1 if ok,
+   0 if number of custom flags exceeded or -1 if error */
 int mail_custom_flags_fix_list(MailCustomFlags *mcf, MailFlags *flags,
-			       const char *custom_flags[]);
+			       const char *custom_flags[], unsigned int count);
 
 /* Returns a pointer to list of flags. */
 const char **mail_custom_flags_list_get(MailCustomFlags *mcf);
--- a/src/lib-index/mail-index.c	Sun Sep 08 18:34:27 2002 +0300
+++ b/src/lib-index/mail-index.c	Mon Sep 09 05:11:01 2002 +0300
@@ -549,6 +549,11 @@
 				break;
 		}
 
+		/* custom flags file needs to be open before
+		   rebuilding index */
+		if (!mail_custom_flags_open_or_create(index))
+			break;
+
 		if (hdr.flags & MAIL_INDEX_FLAG_REBUILD) {
 			/* index is corrupted, rebuild */
 			if (!index->rebuild(index))
@@ -559,8 +564,6 @@
 			break;
 		if (!mail_modifylog_open_or_create(index))
 			break;
-		if (!mail_custom_flags_open_or_create(index))
-			break;
 
 		if (hdr.flags & MAIL_INDEX_FLAG_FSCK) {
 			/* index needs fscking */
@@ -721,7 +724,8 @@
 		*dir_unlocked = TRUE;
 
 	/* create the data file, build the index and hash */
-	if (!mail_index_data_create(index) || !index->rebuild(index) ||
+	if (!mail_custom_flags_open_or_create(index) ||
+	    !mail_index_data_create(index) || !index->rebuild(index) ||
 	    !mail_hash_create(index) || !mail_modifylog_create(index)) {
 		index->updating = FALSE;
 		mail_index_close(index);
--- a/src/lib-index/mbox/mbox-append.c	Sun Sep 08 18:34:27 2002 +0300
+++ b/src/lib-index/mbox/mbox-append.c	Mon Sep 09 05:11:01 2002 +0300
@@ -84,6 +84,7 @@
 	uoff_t abs_start_offset, stop_offset, old_size;
 	unsigned char *data, md5_digest[16];
 	size_t size, pos;
+	int failed;
 
 	/* get the From-line */
 	pos = 0;
@@ -153,14 +154,17 @@
 	if (!index->update_end(update)) {
 		/* failed - delete the record */
 		(void)index->expunge(index, rec, 0, FALSE);
-		return FALSE;
+		failed = TRUE;
+	} else {
+		/* save message flags */
+		rec->msg_flags = ctx.flags;
+		mail_index_mark_flag_changes(index, rec, 0, rec->msg_flags);
+		failed = FALSE;
 	}
 
-	/* save message flags */
-	rec->msg_flags = ctx.flags;
-	mail_index_mark_flag_changes(index, rec, 0, rec->msg_flags);
+	mbox_header_free_context(&ctx);
 
-	return TRUE;
+	return !failed;
 }
 
 int mbox_index_append(MailIndex *index, IOBuffer *inbuf)
--- a/src/lib-index/mbox/mbox-fsck.c	Sun Sep 08 18:34:27 2002 +0300
+++ b/src/lib-index/mbox/mbox-fsck.c	Mon Sep 09 05:11:01 2002 +0300
@@ -136,6 +136,8 @@
 	message_parse_header(NULL, inbuf, &hdr_size, mbox_header_func, &ctx);
 	md5_final(&ctx.md5, current_digest);
 
+	mbox_header_free_context(&ctx);
+
 	body_offset = inbuf->offset;
 	do {
 		if (verify_header_md5sum(index, rec, current_digest) &&
--- a/src/lib-index/mbox/mbox-index.c	Sun Sep 08 18:34:27 2002 +0300
+++ b/src/lib-index/mbox/mbox-index.c	Mon Sep 09 05:11:01 2002 +0300
@@ -18,6 +18,11 @@
 	ctx->custom_flags = mail_custom_flags_list_get(index->custom_flags);
 }
 
+void mbox_header_free_context(MboxHeaderContext *ctx)
+{
+	mail_custom_flags_list_unref(ctx->index->custom_flags);
+}
+
 static MailFlags mbox_get_status_flags(const char *value, size_t len)
 {
 	MailFlags flags;
@@ -69,33 +74,67 @@
 	return flags;
 }
 
-static void mbox_parse_imapbase(const char *value, size_t len,
-				MboxHeaderContext *ctx)
+static int mbox_parse_imapbase(const char *value, size_t len,
+			       MboxHeaderContext *ctx)
 {
-	size_t i, spaces;
+	const char **custom_flags, **old_flags;
+	size_t pos, start;
+	MailFlags flags;
+	int idx, ret, spaces, max;
 
 	/* skip <uid validity> and <last uid> fields */
 	spaces = 0;
-	for (i = 0; i < len; i++) {
-		if (value[i] == ' ' && (i == 0 || value[i-1] != ' ')) {
+	for (pos = 0; pos < len; pos++) {
+		if (value[pos] == ' ' && (pos == 0 || value[pos-1] != ' ')) {
 			if (++spaces == 2)
 				break;
 		}
 	}
 
-	while (i < len && value[i] == ' ') i++;
+	while (pos < len && value[pos] == ' ') pos++;
 
-	if (i == len)
-		return;
+	if (pos == len)
+		return TRUE;
+
+	t_push();
 
 	/* we're at the 3rd field now, which begins the list of custom flags */
-
+	max = MAIL_CUSTOM_FLAGS_COUNT;
+	custom_flags = t_new(const char *, max);
+	for (idx = 0, start = pos; ; pos++) {
+		if (pos == len || value[pos] == ' ' || value[pos] == '\t') {
+			if (start != pos) {
+				if (idx == max) {
+					/* need more memory */
+					old_flags = custom_flags;
+					max *= 2;
+					custom_flags = t_new(const char *, max);
+					memcpy(custom_flags, old_flags,
+					       sizeof(const char *) * idx);
+				}
 
-	/* FIXME */
+				custom_flags[idx++] =
+					t_strdup_until(value+start, value+pos);
+			}
+			start = pos+1;
+
+			if (pos == len)
+				break;
+		}
+	}
+
 	mail_custom_flags_list_unref(ctx->index->custom_flags);
 
+	flags = MAIL_CUSTOM_FLAGS_MASK;
+	ret = mail_custom_flags_fix_list(ctx->index->custom_flags, &flags,
+					 custom_flags, idx);
+
 	ctx->custom_flags =
 		mail_custom_flags_list_get(ctx->index->custom_flags);
+
+	t_pop();
+
+	return ret > 0;
 }
 
 void mbox_header_func(MessagePart *part __attr_unused__,
@@ -166,7 +205,7 @@
 		} else if (name_len == 10 &&
 			   strncasecmp(name, "X-IMAPbase", 10) == 0) {
 			/* update list of custom message flags */
-			mbox_parse_imapbase(value, value_len, ctx);
+			(void)mbox_parse_imapbase(value, value_len, ctx);
 		}
 		break;
 	}
--- a/src/lib-index/mbox/mbox-index.h	Sun Sep 08 18:34:27 2002 +0300
+++ b/src/lib-index/mbox/mbox-index.h	Mon Sep 09 05:11:01 2002 +0300
@@ -15,6 +15,7 @@
 MailIndex *mbox_index_alloc(const char *dir, const char *mbox_path);
 
 void mbox_header_init_context(MboxHeaderContext *ctx, MailIndex *index);
+void mbox_header_free_context(MboxHeaderContext *ctx);
 void mbox_header_func(MessagePart *part __attr_unused__,
 		      const char *name, size_t name_len,
 		      const char *value, size_t value_len,
--- a/src/lib-storage/index/index-storage.c	Sun Sep 08 18:34:27 2002 +0300
+++ b/src/lib-storage/index/index-storage.c	Mon Sep 09 05:11:01 2002 +0300
@@ -59,7 +59,8 @@
 	int ret;
 
 	ret = mail_custom_flags_fix_list(ibox->index->custom_flags,
-					 flags, custom_flags);
+					 flags, custom_flags,
+					 MAIL_CUSTOM_FLAGS_COUNT);
 	switch (ret) {
 	case 1:
 		return TRUE;
--- a/src/lib-storage/index/mbox/mbox-storage.c	Sun Sep 08 18:34:27 2002 +0300
+++ b/src/lib-storage/index/mbox/mbox-storage.c	Mon Sep 09 05:11:01 2002 +0300
@@ -366,15 +366,12 @@
 static void mbox_storage_close(Mailbox *box)
 {
 	IndexMailbox *ibox = (IndexMailbox *) box;
-	const char **list;
 
 	if (!ibox->index->set_lock(ibox->index, MAIL_LOCK_EXCLUSIVE))
 		mail_storage_set_index_error(ibox);
 	else {
 		/* update flags by rewrite mbox file */
-                list = mail_custom_flags_list_get(ibox->index->custom_flags);
 		mbox_index_rewrite(ibox->index);
-		mail_custom_flags_list_unref(ibox->index->custom_flags);
 
 		(void)ibox->index->set_lock(ibox->index, MAIL_LOCK_UNLOCK);
 	}