]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
LAYOUT=index: Update mailbox name to mailbox index's header when opening.
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Tue, 15 Nov 2016 23:43:34 +0000 (01:43 +0200)
committerGitLab <gitlab@git.dovecot.net>
Wed, 16 Nov 2016 15:08:03 +0000 (17:08 +0200)
This way if the mailbox list index gets lost, a recent mailbox name is still
available.

src/lib-storage/index/index-storage.c
src/lib-storage/list/mailbox-list-index-backend.c
src/lib-storage/mail-storage-private.h

index e2f4f937138cec59181fc5149eceb53ad4eda6c1..d2ec5133836440dc76c18a2988603cdbd837cefc 100644 (file)
@@ -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;
 
index 2b46275d43845f5fa8b185b1cb01d8a3a4c1d3df..0d37103ed622292d501843a2bff4c6371507581f 100644 (file)
@@ -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;
 }
index c9a3261c14f7ef5f58c4c7d115fdaffcead4d30d..8893709c7abda7185363e765816a38d546715386 100644 (file)
@@ -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;