]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
quota: Add backend register/unregister
authorAki Tuomi <aki.tuomi@dovecot.fi>
Sat, 25 Mar 2017 13:46:58 +0000 (15:46 +0200)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Mon, 27 Mar 2017 10:06:28 +0000 (13:06 +0300)
This way, other mail plugins can register their own
quota backends.

src/plugins/quota/quota-plugin.c
src/plugins/quota/quota-private.h
src/plugins/quota/quota.c

index 68ccf2dfa7b29fcf507b7224f8d7a5700cbad2f9..63dbd389c9e4043cc961f101dda57bdcc9ddbbc7 100644 (file)
@@ -5,6 +5,8 @@
 #include "mail-storage-hooks.h"
 #include "quota-plugin.h"
 
+void quota_backends_register(void);
+void quota_backends_unregister(void);
 
 const char *quota_plugin_version = DOVECOT_ABI_VERSION;
 
@@ -19,9 +21,11 @@ static struct mail_storage_hooks quota_mail_storage_hooks = {
 void quota_plugin_init(struct module *module)
 {
        mail_storage_hooks_add(module, &quota_mail_storage_hooks);
+       quota_backends_register();
 }
 
 void quota_plugin_deinit(void)
 {
        mail_storage_hooks_remove(&quota_mail_storage_hooks);
+       quota_backends_unregister();
 }
index ba7ba3d943e34f25d8f6cde392f1ced50284716e..a869f7101efb6373816efab662c9f0736cd9b8ed 100644 (file)
@@ -208,4 +208,7 @@ bool quota_warning_match(const struct quota_warning_rule *w,
 bool quota_transaction_is_over(struct quota_transaction_context *ctx, uoff_t size);
 int quota_transaction_set_limits(struct quota_transaction_context *ctx);
 
+void quota_backend_register(const struct quota_backend *backend);
+void quota_backend_unregister(const struct quota_backend *backend);
+
 #endif
index 19aeaf26d688c3658561ad27824ec36295c72f91..3810c132f981a209133c7928492ecc9a7ceb8c06 100644 (file)
@@ -41,7 +41,7 @@ extern struct quota_backend quota_backend_dirsize;
 extern struct quota_backend quota_backend_fs;
 extern struct quota_backend quota_backend_maildir;
 
-static const struct quota_backend *quota_backends[] = {
+static const struct quota_backend *quota_internal_backends[] = {
 #ifdef HAVE_FS_QUOTA
        &quota_backend_fs,
 #endif
@@ -51,22 +51,65 @@ static const struct quota_backend *quota_backends[] = {
        &quota_backend_maildir
 };
 
+static ARRAY(const struct quota_backend*) quota_backends;
+
 static enum quota_alloc_result quota_default_test_alloc(
                struct quota_transaction_context *ctx, uoff_t size);
 static void quota_over_flag_check_root(struct quota_root *root);
 
 static const struct quota_backend *quota_backend_find(const char *name)
 {
-       unsigned int i;
+       const struct quota_backend *const *backend;
 
-       for (i = 0; i < N_ELEMENTS(quota_backends); i++) {
-               if (strcmp(quota_backends[i]->name, name) == 0)
-                       return quota_backends[i];
+       array_foreach(&quota_backends, backend) {
+               if (strcmp((*backend)->name, name) == 0)
+                       return *backend;
        }
 
        return NULL;
 }
 
+void quota_backend_register(const struct quota_backend *backend)
+{
+       i_assert(quota_backend_find(backend->name) == NULL);
+       array_append(&quota_backends, &backend, 1);
+}
+
+void quota_backend_unregister(const struct quota_backend *backend)
+{
+       for(unsigned int i = 0; i < array_count(&quota_backends); i++) {
+               const struct quota_backend *const *be =
+                       array_idx(&quota_backends, i);
+               if (strcmp((*be)->name, backend->name) == 0) {
+                       array_delete(&quota_backends, i, 1);
+                       return;
+               }
+       }
+
+       i_unreached();
+}
+
+void quota_backends_register(void);
+void quota_backends_unregister(void);
+
+void quota_backends_register(void)
+{
+       i_array_init(&quota_backends, 8);
+       array_append(&quota_backends, quota_internal_backends,
+                    N_ELEMENTS(quota_internal_backends));
+}
+
+void quota_backends_unregister(void)
+{
+       for(size_t i = 0; i < N_ELEMENTS(quota_internal_backends); i++) {
+               quota_backend_unregister(quota_internal_backends[i]);
+       }
+
+       i_assert(array_count(&quota_backends) == 0);
+       array_free(&quota_backends);
+
+}
+
 static int quota_root_add_rules(struct mail_user *user, const char *root_name,
                                struct quota_root_settings *root_set,
                                const char **error_r)