]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-index: Allow multiple hooks for transaction creation
authorAki Tuomi <aki.tuomi@dovecot.fi>
Mon, 20 Feb 2017 12:34:25 +0000 (14:34 +0200)
committerGitLab <gitlab@git.dovecot.net>
Mon, 20 Feb 2017 12:52:57 +0000 (14:52 +0200)
src/lib-index/mail-index-transaction-private.h
src/lib-index/mail-index-transaction.c

index 47c9d8eb07c729e75dfe8f407c7a5deb593f983c..eb3025668eebe0227a272dfe616b839070d2eb07 100644 (file)
@@ -107,8 +107,10 @@ struct mail_index_transaction {
         (array_is_created(&(t)->updates) && array_count(&(t)->updates) > 0) || \
         (t)->index_deleted || (t)->index_undeleted)
 
-extern void (*hook_mail_index_transaction_created)
-               (struct mail_index_transaction *t);
+typedef void hook_mail_index_transaction_created_t(struct mail_index_transaction *t);
+
+void mail_index_transaction_hook_register(const hook_mail_index_transaction_created_t *hook);
+void mail_index_transaction_hook_unregister(const hook_mail_index_transaction_created_t *hook);
 
 struct mail_index_record *
 mail_index_transaction_lookup(struct mail_index_transaction *t, uint32_t seq);
index 264a0be6659ff00deb15a8041649f2c027dd81fa..c7441dca5298a58fd814ce792ff5f2393be6f879 100644 (file)
@@ -9,9 +9,36 @@
 #include "mail-transaction-log-private.h"
 #include "mail-index-transaction-private.h"
 
+static ARRAY(const hook_mail_index_transaction_created_t *)
+       hook_mail_index_transaction_created;
+
+void mail_index_transaction_hook_register(const hook_mail_index_transaction_created_t *hook)
+{
+       if (!array_is_created(&hook_mail_index_transaction_created))
+               i_array_init(&hook_mail_index_transaction_created, 8);
+       array_append(&hook_mail_index_transaction_created, &hook, 1);
+}
+
+void mail_index_transaction_hook_unregister(const hook_mail_index_transaction_created_t *hook)
+{
+       unsigned int idx;
+       bool found = FALSE;
+
+       i_assert(array_is_created(&hook_mail_index_transaction_created));
+       for(idx = 0; idx < array_count(&hook_mail_index_transaction_created); idx++) {
+               const hook_mail_index_transaction_created_t *const *hook_ptr =
+                       array_idx(&hook_mail_index_transaction_created, idx);
+               if (*hook_ptr == hook) {
+                       array_delete(&hook_mail_index_transaction_created, idx, 1);
+                       found = TRUE;
+                       break;
+               }
+       }
+       i_assert(found == TRUE);
+       if (array_count(&hook_mail_index_transaction_created) == 0)
+               array_free(&hook_mail_index_transaction_created);
+}
 
-void (*hook_mail_index_transaction_created)
-               (struct mail_index_transaction *t) = NULL;
 
 struct mail_index_view *
 mail_index_transaction_get_view(struct mail_index_transaction *t)
@@ -321,7 +348,11 @@ mail_index_transaction_begin(struct mail_index_view *view,
                     I_MIN(5, mail_index_module_register.id));
        DLLIST_PREPEND(&view->transactions_list, t);
 
-       if (hook_mail_index_transaction_created != NULL)
-               hook_mail_index_transaction_created(t);
+       if (array_is_created(&hook_mail_index_transaction_created)) {
+               const hook_mail_index_transaction_created_t *const *ptr;
+               array_foreach(&hook_mail_index_transaction_created, ptr) {
+                       (*ptr)(t);
+               }
+       }
        return t;
 }