dovecot-2.2: lib-storage: Added support to get/set/iterate mailb...

dovecot at dovecot.org dovecot at dovecot.org
Sat Oct 13 04:28:25 EEST 2012


details:   http://hg.dovecot.org/dovecot-2.2/rev/5659c178bdeb
changeset: 15224:5659c178bdeb
user:      Timo Sirainen <tss at iki.fi>
date:      Sat Oct 13 04:27:14 2012 +0300
description:
lib-storage: Added support to get/set/iterate mailbox attributes.
The attributes are stored in a dict specified by mail_attribute_dict
setting. The idea is to use this as storage for extensions that require
per-mailbox key=value pairs, such as METADATA.

diffstat:

 src/lib-storage/fail-mailbox.c                    |   5 +++
 src/lib-storage/index/Makefile.am                 |   2 +
 src/lib-storage/index/cydir/cydir-storage.c       |   5 +++
 src/lib-storage/index/dbox-multi/mdbox-storage.c  |   5 +++
 src/lib-storage/index/dbox-single/sdbox-storage.c |   5 +++
 src/lib-storage/index/imapc/imapc-storage.c       |   5 +++
 src/lib-storage/index/index-storage.c             |  26 +++++++++++++++
 src/lib-storage/index/index-storage.h             |  14 ++++++++
 src/lib-storage/index/maildir/maildir-storage.c   |   5 +++
 src/lib-storage/index/mbox/mbox-storage.c         |   5 +++
 src/lib-storage/index/pop3c/pop3c-storage.c       |   5 +++
 src/lib-storage/index/raw/raw-storage.c           |   5 +++
 src/lib-storage/mail-storage-private.h            |  19 +++++++++++
 src/lib-storage/mail-storage-settings.c           |   2 +
 src/lib-storage/mail-storage-settings.h           |   1 +
 src/lib-storage/mail-storage.c                    |  38 +++++++++++++++++++++++
 src/lib-storage/mail-storage.h                    |  25 +++++++++++++++
 src/plugins/virtual/virtual-storage.c             |   5 +++
 18 files changed, 177 insertions(+), 0 deletions(-)

diffs (truncated from 420 to 300 lines):

diff -r dc3a95fe330b -r 5659c178bdeb src/lib-storage/fail-mailbox.c
--- a/src/lib-storage/fail-mailbox.c	Sat Oct 13 04:18:26 2012 +0300
+++ b/src/lib-storage/fail-mailbox.c	Sat Oct 13 04:27:14 2012 +0300
@@ -264,6 +264,11 @@
 		fail_mailbox_set_subscribed,
 		NULL,
 		NULL,
+		NULL,
+		NULL,
+		NULL,
+		NULL,
+		NULL,
 		fail_mailbox_sync_init,
 		fail_mailbox_sync_next,
 		fail_mailbox_sync_deinit,
diff -r dc3a95fe330b -r 5659c178bdeb src/lib-storage/index/Makefile.am
--- a/src/lib-storage/index/Makefile.am	Sat Oct 13 04:18:26 2012 +0300
+++ b/src/lib-storage/index/Makefile.am	Sat Oct 13 04:27:14 2012 +0300
@@ -5,6 +5,7 @@
 AM_CPPFLAGS = \
 	-I$(top_srcdir)/src/lib \
 	-I$(top_srcdir)/src/lib-test \
+	-I$(top_srcdir)/src/lib-dict \
 	-I$(top_srcdir)/src/lib-fs \
 	-I$(top_srcdir)/src/lib-mail \
 	-I$(top_srcdir)/src/lib-imap \
@@ -14,6 +15,7 @@
 libstorage_index_la_SOURCES = \
 	istream-mail.c \
 	index-attachment.c \
+	index-attribute.c \
 	index-mail.c \
 	index-mail-binary.c \
 	index-mail-headers.c \
diff -r dc3a95fe330b -r 5659c178bdeb src/lib-storage/index/cydir/cydir-storage.c
--- a/src/lib-storage/index/cydir/cydir-storage.c	Sat Oct 13 04:18:26 2012 +0300
+++ b/src/lib-storage/index/cydir/cydir-storage.c	Sat Oct 13 04:27:14 2012 +0300
@@ -141,6 +141,11 @@
 		index_storage_get_status,
 		NULL,
 		index_storage_set_subscribed,
+		index_storage_attribute_set,
+		index_storage_attribute_get,
+		index_storage_attribute_iter_init,
+		index_storage_attribute_iter_next,
+		index_storage_attribute_iter_deinit,
 		index_storage_list_index_has_changed,
 		index_storage_list_index_update_sync,
 		cydir_storage_sync_init,
diff -r dc3a95fe330b -r 5659c178bdeb src/lib-storage/index/dbox-multi/mdbox-storage.c
--- a/src/lib-storage/index/dbox-multi/mdbox-storage.c	Sat Oct 13 04:18:26 2012 +0300
+++ b/src/lib-storage/index/dbox-multi/mdbox-storage.c	Sat Oct 13 04:27:14 2012 +0300
@@ -440,6 +440,11 @@
 		index_storage_get_status,
 		mdbox_mailbox_get_metadata,
 		index_storage_set_subscribed,
+		index_storage_attribute_set,
+		index_storage_attribute_get,
+		index_storage_attribute_iter_init,
+		index_storage_attribute_iter_next,
+		index_storage_attribute_iter_deinit,
 		index_storage_list_index_has_changed,
 		index_storage_list_index_update_sync,
 		mdbox_storage_sync_init,
diff -r dc3a95fe330b -r 5659c178bdeb src/lib-storage/index/dbox-single/sdbox-storage.c
--- a/src/lib-storage/index/dbox-single/sdbox-storage.c	Sat Oct 13 04:18:26 2012 +0300
+++ b/src/lib-storage/index/dbox-single/sdbox-storage.c	Sat Oct 13 04:27:14 2012 +0300
@@ -415,6 +415,11 @@
 		index_storage_get_status,
 		sdbox_mailbox_get_metadata,
 		index_storage_set_subscribed,
+		index_storage_attribute_set,
+		index_storage_attribute_get,
+		index_storage_attribute_iter_init,
+		index_storage_attribute_iter_next,
+		index_storage_attribute_iter_deinit,
 		index_storage_list_index_has_changed,
 		index_storage_list_index_update_sync,
 		sdbox_storage_sync_init,
diff -r dc3a95fe330b -r 5659c178bdeb src/lib-storage/index/imapc/imapc-storage.c
--- a/src/lib-storage/index/imapc/imapc-storage.c	Sat Oct 13 04:18:26 2012 +0300
+++ b/src/lib-storage/index/imapc/imapc-storage.c	Sat Oct 13 04:27:14 2012 +0300
@@ -884,6 +884,11 @@
 		imapc_mailbox_get_status,
 		imapc_mailbox_get_metadata,
 		index_storage_set_subscribed,
+		index_storage_attribute_set,
+		index_storage_attribute_get,
+		index_storage_attribute_iter_init,
+		index_storage_attribute_iter_next,
+		index_storage_attribute_iter_deinit,
 		NULL,
 		NULL,
 		imapc_mailbox_sync_init,
diff -r dc3a95fe330b -r 5659c178bdeb src/lib-storage/index/index-storage.c
--- a/src/lib-storage/index/index-storage.c	Sat Oct 13 04:18:26 2012 +0300
+++ b/src/lib-storage/index/index-storage.c	Sat Oct 13 04:27:14 2012 +0300
@@ -6,6 +6,7 @@
 #include "ioloop.h"
 #include "str.h"
 #include "mkdir-parents.h"
+#include "dict.h"
 #include "mail-index-alloc-cache.h"
 #include "mail-index-private.h"
 #include "mail-index-modseq.h"
@@ -359,6 +360,10 @@
 
 void index_storage_mailbox_free(struct mailbox *box)
 {
+	if (box->_attr_dict != NULL) {
+		(void)dict_wait(box->_attr_dict);
+		dict_deinit(&box->_attr_dict);
+	}
 	if (box->index_pvt != NULL)
 		mail_index_alloc_cache_unref(&box->index_pvt);
 	if (box->index != NULL)
@@ -579,6 +584,23 @@
 	return mailbox_transaction_commit(&t);
 }
 
+static int
+mailbox_delete_all_attributes(struct mailbox *box, enum mail_attribute_type type)
+{
+	struct mailbox_attribute_iter *iter;
+	const char *key;
+	int ret = 0;
+
+	iter = mailbox_attribute_iter_init(box, type, "");
+	while ((key = mailbox_attribute_iter_next(iter)) != NULL) {
+		if (mailbox_attribute_unset(box, type, key) < 0)
+			ret = -1;
+	}
+	if (mailbox_attribute_iter_deinit(&iter) < 0)
+		ret = -1;
+	return ret;
+}
+
 int index_storage_mailbox_delete(struct mailbox *box)
 {
 	struct mailbox_metadata metadata;
@@ -609,6 +631,10 @@
 	if (!box->deleting_must_be_empty) {
 		if (mailbox_expunge_all_mails(box) < 0)
 			return -1;
+		if (mailbox_delete_all_attributes(box, MAIL_ATTRIBUTE_TYPE_PRIVATE) < 0)
+			return -1;
+		if (mailbox_delete_all_attributes(box, MAIL_ATTRIBUTE_TYPE_SHARED) < 0)
+			return -1;
 	}
 	if (mailbox_mark_index_deleted(box, TRUE) < 0)
 		return -1;
diff -r dc3a95fe330b -r 5659c178bdeb src/lib-storage/index/index-storage.h
--- a/src/lib-storage/index/index-storage.h	Sat Oct 13 04:18:26 2012 +0300
+++ b/src/lib-storage/index/index-storage.h	Sat Oct 13 04:27:14 2012 +0300
@@ -116,6 +116,20 @@
 			       enum mailbox_metadata_items items,
 			       struct mailbox_metadata *metadata_r);
 
+int index_storage_attribute_set(struct mailbox *box,
+				enum mail_attribute_type type,
+				const char *key, const char *value);
+int index_storage_attribute_get(struct mailbox *box,
+				enum mail_attribute_type type,
+				const char *key, const char **value_r);
+struct mailbox_attribute_iter *
+index_storage_attribute_iter_init(struct mailbox *box,
+				  enum mail_attribute_type type,
+				  const char *prefix);
+const char *
+index_storage_attribute_iter_next(struct mailbox_attribute_iter *iter);
+int index_storage_attribute_iter_deinit(struct mailbox_attribute_iter *iter);
+
 struct mail_search_context *
 index_storage_search_init(struct mailbox_transaction_context *t,
 			  struct mail_search_args *args,
diff -r dc3a95fe330b -r 5659c178bdeb src/lib-storage/index/maildir/maildir-storage.c
--- a/src/lib-storage/index/maildir/maildir-storage.c	Sat Oct 13 04:18:26 2012 +0300
+++ b/src/lib-storage/index/maildir/maildir-storage.c	Sat Oct 13 04:27:14 2012 +0300
@@ -678,6 +678,11 @@
 		index_storage_get_status,
 		maildir_mailbox_get_metadata,
 		index_storage_set_subscribed,
+		index_storage_attribute_set,
+		index_storage_attribute_get,
+		index_storage_attribute_iter_init,
+		index_storage_attribute_iter_next,
+		index_storage_attribute_iter_deinit,
 		maildir_list_index_has_changed,
 		maildir_list_index_update_sync,
 		maildir_storage_sync_init,
diff -r dc3a95fe330b -r 5659c178bdeb src/lib-storage/index/mbox/mbox-storage.c
--- a/src/lib-storage/index/mbox/mbox-storage.c	Sat Oct 13 04:18:26 2012 +0300
+++ b/src/lib-storage/index/mbox/mbox-storage.c	Sat Oct 13 04:27:14 2012 +0300
@@ -817,6 +817,11 @@
 		index_storage_get_status,
 		mbox_mailbox_get_metadata,
 		index_storage_set_subscribed,
+		index_storage_attribute_set,
+		index_storage_attribute_get,
+		index_storage_attribute_iter_init,
+		index_storage_attribute_iter_next,
+		index_storage_attribute_iter_deinit,
 		index_storage_list_index_has_changed,
 		index_storage_list_index_update_sync,
 		mbox_storage_sync_init,
diff -r dc3a95fe330b -r 5659c178bdeb src/lib-storage/index/pop3c/pop3c-storage.c
--- a/src/lib-storage/index/pop3c/pop3c-storage.c	Sat Oct 13 04:18:26 2012 +0300
+++ b/src/lib-storage/index/pop3c/pop3c-storage.c	Sat Oct 13 04:27:14 2012 +0300
@@ -250,6 +250,11 @@
 		index_storage_get_status,
 		index_mailbox_get_metadata,
 		index_storage_set_subscribed,
+		index_storage_attribute_set,
+		index_storage_attribute_get,
+		index_storage_attribute_iter_init,
+		index_storage_attribute_iter_next,
+		index_storage_attribute_iter_deinit,
 		index_storage_list_index_has_changed,
 		index_storage_list_index_update_sync,
 		pop3c_storage_sync_init,
diff -r dc3a95fe330b -r 5659c178bdeb src/lib-storage/index/raw/raw-storage.c
--- a/src/lib-storage/index/raw/raw-storage.c	Sat Oct 13 04:18:26 2012 +0300
+++ b/src/lib-storage/index/raw/raw-storage.c	Sat Oct 13 04:27:14 2012 +0300
@@ -220,6 +220,11 @@
 		index_storage_get_status,
 		index_mailbox_get_metadata,
 		index_storage_set_subscribed,
+		index_storage_attribute_set,
+		index_storage_attribute_get,
+		index_storage_attribute_iter_init,
+		index_storage_attribute_iter_next,
+		index_storage_attribute_iter_deinit,
 		index_storage_list_index_has_changed,
 		index_storage_list_index_update_sync,
 		raw_storage_sync_init,
diff -r dc3a95fe330b -r 5659c178bdeb src/lib-storage/mail-storage-private.h
--- a/src/lib-storage/mail-storage-private.h	Sat Oct 13 04:18:26 2012 +0300
+++ b/src/lib-storage/mail-storage-private.h	Sat Oct 13 04:27:14 2012 +0300
@@ -142,6 +142,17 @@
 			    struct mailbox_metadata *metadata_r);
 	int (*set_subscribed)(struct mailbox *box, bool set);
 
+	int (*attribute_set)(struct mailbox *box, enum mail_attribute_type type,
+			     const char *key, const char *value);
+	int (*attribute_get)(struct mailbox *box, enum mail_attribute_type type,
+			     const char *key, const char **value_r);
+	struct mailbox_attribute_iter *
+		(*attribute_iter_init)(struct mailbox *box,
+				       enum mail_attribute_type type,
+				       const char *prefix);
+	const char *(*attribute_iter_next)(struct mailbox_attribute_iter *iter);
+	int (*attribute_iter_deinit)(struct mailbox_attribute_iter *iter);
+
 	/* Lookup sync extension record and figure out if it mailbox has
 	   changed since. Returns 1 = yes, 0 = no, -1 = error. */
 	int (*list_index_has_changed)(struct mailbox *box,
@@ -250,6 +261,8 @@
 	/* Filled lazily when mailbox is opened, use mailbox_get_path()
 	   to access it */
 	const char *_path;
+	/* Filled lazily by mailbox_attribute_*() */
+	struct dict *_attr_dict;
 
 	/* default vfuncs for new struct mails. */
 	const struct mail_vfuncs *mail_vfuncs;
@@ -307,6 +320,8 @@
 	unsigned int disallow_new_keywords:1;
 	/* Mailbox has been synced at least once */
 	unsigned int synced:1;
+	/* Failed to create attribute dict, don't try again */
+	unsigned int attr_dict_failed:1;
 };
 
 struct mail_vfuncs {
@@ -520,6 +535,10 @@
 	unsigned int *idx;
 };
 
+struct mailbox_attribute_iter {
+	struct mailbox *box;
+};
+
 /* Modules should use do "my_id = mail_storage_module_id++" and
    use objects' module_contexts[id] for their own purposes. */
 extern struct mail_storage_module_register mail_storage_module_register;
diff -r dc3a95fe330b -r 5659c178bdeb src/lib-storage/mail-storage-settings.c
--- a/src/lib-storage/mail-storage-settings.c	Sat Oct 13 04:18:26 2012 +0300
+++ b/src/lib-storage/mail-storage-settings.c	Sat Oct 13 04:27:14 2012 +0300
@@ -30,6 +30,7 @@
 	DEF(SET_STR_VARS, mail_attachment_dir),
 	DEF(SET_STR, mail_attachment_hash),
 	DEF(SET_SIZE, mail_attachment_min_size),
+	DEF(SET_STR_VARS, mail_attribute_dict),
 	DEF(SET_UINT, mail_prefetch_count),
 	DEF(SET_STR, mail_cache_fields),
 	DEF(SET_STR, mail_never_cache_fields),
@@ -61,6 +62,7 @@
 	.mail_attachment_dir = "",
 	.mail_attachment_hash = "%{sha1}",
 	.mail_attachment_min_size = 1024*128,


More information about the dovecot-cvs mailing list