From: Aki Tuomi Date: Sat, 25 Mar 2017 13:46:58 +0000 (+0200) Subject: quota: Add backend register/unregister X-Git-Tag: 2.3.0.rc1~1865 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bd2a176b573f9679e7e45339c20ef71704f071c0;p=thirdparty%2Fdovecot%2Fcore.git quota: Add backend register/unregister This way, other mail plugins can register their own quota backends. --- diff --git a/src/plugins/quota/quota-plugin.c b/src/plugins/quota/quota-plugin.c index 68ccf2dfa7..63dbd389c9 100644 --- a/src/plugins/quota/quota-plugin.c +++ b/src/plugins/quota/quota-plugin.c @@ -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, "a_mail_storage_hooks); + quota_backends_register(); } void quota_plugin_deinit(void) { mail_storage_hooks_remove("a_mail_storage_hooks); + quota_backends_unregister(); } diff --git a/src/plugins/quota/quota-private.h b/src/plugins/quota/quota-private.h index 9de8d008ff..6053a31716 100644 --- a/src/plugins/quota/quota-private.h +++ b/src/plugins/quota/quota-private.h @@ -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 diff --git a/src/plugins/quota/quota.c b/src/plugins/quota/quota.c index 33876647cc..f3f688ef15 100644 --- a/src/plugins/quota/quota.c +++ b/src/plugins/quota/quota.c @@ -43,7 +43,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 "a_backend_fs, #endif @@ -53,22 +53,65 @@ static const struct quota_backend *quota_backends[] = { "a_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("a_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("a_backends, &backend, 1); +} + +void quota_backend_unregister(const struct quota_backend *backend) +{ + for(unsigned int i = 0; i < array_count("a_backends); i++) { + const struct quota_backend *const *be = + array_idx("a_backends, i); + if (strcmp((*be)->name, backend->name) == 0) { + array_delete("a_backends, i, 1); + return; + } + } + + i_unreached(); +} + +void quota_backends_register(void); +void quota_backends_unregister(void); + +void quota_backends_register(void) +{ + i_array_init("a_backends, 8); + array_append("a_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("a_backends) == 0); + array_free("a_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)