From: Timo Sirainen Date: Tue, 15 Nov 2016 23:43:34 +0000 (+0200) Subject: LAYOUT=index: Update mailbox name to mailbox index's header when opening. X-Git-Tag: 2.3.0.rc1~2606 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=97a5e150cacf7da9dd200e317b75460352038d65;p=thirdparty%2Fdovecot%2Fcore.git LAYOUT=index: Update mailbox name to mailbox index's header when opening. This way if the mailbox list index gets lost, a recent mailbox name is still available. --- diff --git a/src/lib-storage/index/index-storage.c b/src/lib-storage/index/index-storage.c index e2f4f93713..d2ec513383 100644 --- a/src/lib-storage/index/index-storage.c +++ b/src/lib-storage/index/index-storage.c @@ -297,6 +297,8 @@ int index_storage_mailbox_open(struct mailbox *box, bool move_to_memory) box->pop3_uidl_hdr_ext_id = mail_index_ext_register(box->index, "hdr-pop3-uidl", sizeof(struct mailbox_index_pop3_uidl), 0, 0); + box->box_name_hdr_ext_id = + mail_index_ext_register(box->index, "box-name", 0, 0, 0); box->opened = TRUE; diff --git a/src/lib-storage/list/mailbox-list-index-backend.c b/src/lib-storage/list/mailbox-list-index-backend.c index 2b46275d43..0d37103ed6 100644 --- a/src/lib-storage/list/mailbox-list-index-backend.c +++ b/src/lib-storage/list/mailbox-list-index-backend.c @@ -456,6 +456,54 @@ index_list_mailbox_exists(struct mailbox *box, bool auto_boxes ATTR_UNUSED, return 0; } +static int index_list_mailbox_open(struct mailbox *box) +{ + struct index_list_mailbox *ibox = INDEX_LIST_STORAGE_CONTEXT(box); + const void *data; + const unsigned char *name_hdr; + size_t name_hdr_size; + + if (ibox->module_ctx.super.open(box) < 0) + return -1; + + /* if mailbox name has changed, update it to the header. Use \0 + as the hierarchy separator in the header. This is to make sure + we don't keep rewriting the name just in case some backend switches + between separators when accessed different ways. */ + + /* Get the current mailbox name with \0 separators. */ + char sep = mailbox_list_get_hierarchy_sep(box->list); + char *box_zerosep_name = t_strdup_noconst(box->name); + unsigned int box_name_len = strlen(box_zerosep_name); + for (unsigned int i = 0; i < box_name_len; i++) { + if (box_zerosep_name[i] == sep) + box_zerosep_name[i] = '\0'; + } + + /* Does it match what's in the header now? */ + mail_index_get_header_ext(box->view, box->box_name_hdr_ext_id, + &data, &name_hdr_size); + name_hdr = data; + while (name_hdr_size > 0 && name_hdr[name_hdr_size-1] == '\0') { + /* Remove trailing \0 - header doesn't shrink always */ + name_hdr_size--; + } + if (name_hdr_size == box_name_len && + memcmp(box_zerosep_name, name_hdr, box_name_len) == 0) { + /* Same mailbox name */ + } else { + /* Mailbox name changed - update */ + struct mail_index_transaction *trans = + mail_index_transaction_begin(box->view, 0); + mail_index_ext_resize_hdr(trans, box->box_name_hdr_ext_id, + box_name_len); + mail_index_update_header_ext(trans, box->box_name_hdr_ext_id, 0, + box_zerosep_name, box_name_len); + (void)mail_index_transaction_commit(&trans); + } + return 0; +} + static void index_list_try_delete(struct mailbox_list *_list, const char *name, enum mailbox_list_path_type type) @@ -734,4 +782,5 @@ void mailbox_list_index_backend_init_mailbox(struct mailbox *box) box->v.create_box = index_list_mailbox_create; box->v.update_box = index_list_mailbox_update; box->v.exists = index_list_mailbox_exists; + box->v.open = index_list_mailbox_open; } diff --git a/src/lib-storage/mail-storage-private.h b/src/lib-storage/mail-storage-private.h index c9a3261c14..8893709c7a 100644 --- a/src/lib-storage/mail-storage-private.h +++ b/src/lib-storage/mail-storage-private.h @@ -354,6 +354,7 @@ struct mailbox { struct mail_msgpart_partial_cache partial_cache; uint32_t vsize_hdr_ext_id; uint32_t pop3_uidl_hdr_ext_id; + uint32_t box_name_hdr_ext_id; /* MAIL_RECENT flags handling */ ARRAY_TYPE(seq_range) recent_flags;