changeset 21823:c7975678d4dc

quota: Add backend register/unregister This way, other mail plugins can register their own quota backends.
author Aki Tuomi <aki.tuomi@dovecot.fi>
date Sat, 25 Mar 2017 15:46:58 +0200
parents c09853ffcc46
children d228cc8cf685
files src/plugins/quota/quota-plugin.c src/plugins/quota/quota-private.h src/plugins/quota/quota.c
diffstat 3 files changed, 55 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/quota/quota-plugin.c	Fri Mar 24 14:46:05 2017 +0200
+++ b/src/plugins/quota/quota-plugin.c	Sat Mar 25 15:46:58 2017 +0200
@@ -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 @@
 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();
 }
--- a/src/plugins/quota/quota-private.h	Fri Mar 24 14:46:05 2017 +0200
+++ b/src/plugins/quota/quota-private.h	Sat Mar 25 15:46:58 2017 +0200
@@ -208,4 +208,7 @@
 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
--- a/src/plugins/quota/quota.c	Fri Mar 24 14:46:05 2017 +0200
+++ b/src/plugins/quota/quota.c	Sat Mar 25 15:46:58 2017 +0200
@@ -41,7 +41,7 @@
 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 @@
 	&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)