changeset 4514:511f1f5c76fa HEAD

Changes to make trash plugin working again.
author Timo Sirainen <tss@iki.fi>
date Sun, 30 Jul 2006 21:32:07 +0300
parents f91f3bcb7939
children 2ee97af8c99f
files src/plugins/quota/quota-fs.c src/plugins/quota/quota-maildir.c src/plugins/quota/quota-private.h src/plugins/quota/quota.c src/plugins/quota/quota.h src/plugins/trash/trash-plugin.c
diffstat 6 files changed, 36 insertions(+), 70 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/quota/quota-fs.c	Sun Jul 30 21:31:23 2006 +0300
+++ b/src/plugins/quota/quota-fs.c	Sun Jul 30 21:32:07 2006 +0300
@@ -110,7 +110,7 @@
 
 	roots = array_get(&quota->roots, &count);
 	for (i = 0; i < count; i++) {
-		if (roots[i]->backend == &quota_backend_fs) {
+		if (roots[i]->backend.name == quota_backend_fs.name) {
 			struct fs_quota_root *root =
 				(struct fs_quota_root *)roots[i];
 
--- a/src/plugins/quota/quota-maildir.c	Sun Jul 30 21:31:23 2006 +0300
+++ b/src/plugins/quota/quota-maildir.c	Sun Jul 30 21:32:07 2006 +0300
@@ -577,7 +577,7 @@
 
 	roots = array_get_modifiable(&quota->roots, &count);
 	for (i = 0; i < count; i++) {
-		if (roots[i]->backend == &quota_backend_maildir)
+		if (roots[i]->backend.name == quota_backend_maildir.name)
 			maildir_quota_root_storage_added(roots[i], _storage);
 	}
 
--- a/src/plugins/quota/quota-private.h	Sun Jul 30 21:31:23 2006 +0300
+++ b/src/plugins/quota/quota-private.h	Sun Jul 30 21:32:07 2006 +0300
@@ -11,6 +11,9 @@
 struct quota {
 	ARRAY_DEFINE(roots, struct quota_root *);
 	ARRAY_DEFINE(storages, struct mail_storage *);
+
+	int (*test_alloc)(struct quota_transaction_context *ctx,
+			  uoff_t size, bool *too_large_r);
 };
 
 struct quota_backend_vfuncs {
@@ -33,6 +36,7 @@
 };
 
 struct quota_backend {
+	/* quota backends equal if backend1.name == backend2.name */
 	const char *name;
 	struct quota_backend_vfuncs v;
 };
@@ -52,7 +56,7 @@
 	/* pointer to the quota that owns this root */
 	struct quota *quota;
 
-	struct quota_backend *backend;
+	struct quota_backend backend;
 	struct quota_rule default_rule;
 	ARRAY_DEFINE(rules, struct quota_rule);
 
--- a/src/plugins/quota/quota.c	Sun Jul 30 21:31:23 2006 +0300
+++ b/src/plugins/quota/quota.c	Sun Jul 30 21:32:07 2006 +0300
@@ -23,7 +23,7 @@
 extern struct quota_backend quota_backend_fs;
 extern struct quota_backend quota_backend_maildir;
 
-static struct quota_backend *quota_backends[] = {
+static const struct quota_backend *quota_backends[] = {
 #ifdef HAVE_FS_QUOTA
 	&quota_backend_fs,
 #endif
@@ -33,13 +33,15 @@
 };
 #define QUOTA_CLASS_COUNT (sizeof(quota_backends)/sizeof(quota_backends[0]))
 
-void (*hook_quota_root_created)(struct quota_root *root);
+static int quota_default_test_alloc(struct quota_transaction_context *ctx,
+				    uoff_t size, bool *too_large_r);
 
 struct quota *quota_init(void)
 {
 	struct quota *quota;
 
 	quota = i_new(struct quota, 1);
+	quota->test_alloc = quota_default_test_alloc;
 	ARRAY_CREATE(&quota->roots, default_pool, struct quota_root *, 4);
 	ARRAY_CREATE(&quota->storages, default_pool, struct mail_storage *, 8);
 
@@ -60,7 +62,7 @@
 	i_free(quota);
 }
 
-static struct quota_backend *quota_backend_find(const char *name)
+static const struct quota_backend *quota_backend_find(const char *name)
 {
 	unsigned int i;
 
@@ -75,7 +77,7 @@
 struct quota_root *quota_root_init(struct quota *quota, const char *root_def)
 {
 	struct quota_root *root;
-	struct quota_backend *backend;
+	const struct quota_backend *backend;
 	const char *p, *args, *backend_name;
 
 	t_push();
@@ -98,7 +100,7 @@
 
 	root = backend->v.alloc();
 	root->quota = quota;
-	root->backend = backend;
+	root->backend = *backend;
 	root->pool = pool_alloconly_create("quota root", 512);
 
 	if (args != NULL) {
@@ -126,9 +128,6 @@
 			return NULL;
 		}
 	}
-
-	if (hook_quota_root_created != NULL)
-		hook_quota_root_created(root);
 	return root;
 }
 
@@ -147,7 +146,7 @@
 	array_free(&root->rules);
 	array_free(&root->quota_module_contexts);
 
-	root->backend->v.deinit(root);
+	root->backend.v.deinit(root);
 	pool_unref(pool);
 }
 
@@ -248,11 +247,11 @@
 	backends = t_new(struct quota_backend *, count + 1);
 	for (i = 0; i < count; i++) {
 		for (j = 0; backends[j] != NULL; j++) {
-			if (backends[j] == roots[i]->backend)
+			if (backends[j]->name == roots[i]->backend.name)
 				break;
 		}
 		if (backends[j] == NULL)
-			backends[j] = roots[i]->backend;
+			backends[j] = &roots[i]->backend;
 	}
 
 	for (i = 0; backends[i] != NULL; i++) {
@@ -341,7 +340,7 @@
 
 const char *const *quota_root_get_resources(struct quota_root *root)
 {
-	return root->backend->v.get_resources(root);
+	return root->backend.v.get_resources(root);
 }
 
 int quota_get_resource(struct quota_root *root, const char *mailbox_name,
@@ -359,7 +358,7 @@
 	else
 		*limit_r = 0;
 
-	ret = root->backend->v.get_resource(root, name, value_r, limit_r);
+	ret = root->backend.v.get_resource(root, name, value_r, limit_r);
 	return ret <= 0 ? ret :
 		(*limit_r == 0 ? 0 : 1);
 }
@@ -432,7 +431,7 @@
 	else {
 		roots = array_get(&ctx->quota->roots, &count);
 		for (i = 0; i < count; i++) {
-			if (roots[i]->backend->v.update(roots[i], ctx) < 0)
+			if (roots[i]->backend.v.update(roots[i], ctx) < 0)
 				ret = -1;
 		}
 	}
@@ -462,6 +461,12 @@
 int quota_test_alloc(struct quota_transaction_context *ctx,
 		     uoff_t size, bool *too_large_r)
 {
+	return ctx->quota->test_alloc(ctx, size, too_large_r);
+}
+
+static int quota_default_test_alloc(struct quota_transaction_context *ctx,
+				    uoff_t size, bool *too_large_r)
+{
 	struct quota_root *const *roots;
 	unsigned int i, count;
 
--- a/src/plugins/quota/quota.h	Sun Jul 30 21:31:23 2006 +0300
+++ b/src/plugins/quota/quota.h	Sun Jul 30 21:32:07 2006 +0300
@@ -14,8 +14,6 @@
 struct quota_root_iter;
 struct quota_transaction_context;
 
-extern void (*hook_quota_root_created)(struct quota_root *root);
-
 struct quota *quota_init(void);
 void quota_deinit(struct quota *quota);
 
--- a/src/plugins/trash/trash-plugin.c	Sun Jul 30 21:31:23 2006 +0300
+++ b/src/plugins/trash/trash-plugin.c	Sun Jul 30 21:32:07 2006 +0300
@@ -15,14 +15,6 @@
 
 #define MAX_RETRY_COUNT 3
 
-#define TRASH_CONTEXT(obj) \
-	*((void **)array_idx_modifiable(&(obj)->quota_module_contexts, \
-					trash_quota_module_id))
-
-struct trash_quota_root {
-	struct quota_backend_vfuncs super;
-};
-
 struct trash_mailbox {
 	const char *name;
 	int priority; /* lower number = higher priority */
@@ -38,11 +30,8 @@
 	unsigned int mail_set:1;
 };
 
-/* defined by imap, pop3, lda */
-extern void (*hook_quota_root_created)(struct quota_root *root);
-
-static void (*trash_next_hook_quota_root_created)(struct quota_root *root);
-static unsigned int trash_quota_module_id;
+static int (*trash_next_quota_test_alloc)(struct quota_transaction_context *,
+					  uoff_t, bool *);
 
 static pool_t config_pool;
 /* trash_boxes ordered by priority, highest first */
@@ -158,14 +147,13 @@
 }
 
 static int
-trash_quota_root_try_alloc(struct quota_root_transaction_context *ctx,
-			   struct mail *mail, bool *too_large_r)
+trash_quota_test_alloc(struct quota_transaction_context *ctx,
+		       uoff_t size, bool *too_large_r)
 {
-	struct trash_quota_root *troot = TRASH_CONTEXT(ctx->root);
 	int ret, i;
 
 	for (i = 0; ; i++) {
-		ret = troot->super.try_alloc(ctx, mail, too_large_r);
+		ret = trash_next_quota_test_alloc(ctx, size, too_large_r);
 		if (ret != 0 || *too_large_r)
 			return ret;
 
@@ -178,7 +166,7 @@
 		}
 
 		/* not enough space. try deleting some from mailbox. */
-		ret = trash_try_clean_mails(mail_get_physical_size(mail));
+		ret = trash_try_clean_mails(size);
 		if (ret <= 0)
 			return 0;
 	}
@@ -186,34 +174,6 @@
 	return 0;
 }
 
-static void trash_quota_root_deinit(struct quota_root *root)
-{
-	struct trash_quota_root *troot = TRASH_CONTEXT(root);
-	void *null = NULL;
-
-	array_idx_set(&root->quota_module_contexts,
-		      trash_quota_module_id, &null);
-	troot->super.deinit(root);
-	i_free(troot);
-}
-
-static void trash_quota_root_created(struct quota_root *root)
-{
-	struct trash_quota_root *troot;
-
-	if (trash_next_hook_quota_root_created != NULL)
-		trash_next_hook_quota_root_created(root);
-
-	troot = i_new(struct trash_quota_root, 1);
-	troot->super = root->v;
-	root->v.deinit = trash_quota_root_deinit;
-	root->v.try_alloc = trash_quota_root_try_alloc;
-
-	trash_quota_module_id = quota_module_id++;
-	array_idx_set(&root->quota_module_contexts,
-		      trash_quota_module_id, &troot);
-}
-
 static int trash_mailbox_priority_cmp(const void *p1, const void *p2)
 {
 	const struct trash_mailbox *t1 = p1, *t2 = p2;
@@ -261,8 +221,6 @@
 {
 	const char *env;
 
-	trash_next_hook_quota_root_created = hook_quota_root_created;
-
 	env = getenv("TRASH");
 	if (env == NULL)
 		return;
@@ -276,13 +234,14 @@
 	if (read_configuration(env) < 0)
 		return;
 
-	hook_quota_root_created = trash_quota_root_created;
+	trash_next_quota_test_alloc = quota->test_alloc;
+	quota->test_alloc = trash_quota_test_alloc;
 }
 
 void trash_plugin_deinit(void)
 {
+	quota->test_alloc = trash_next_quota_test_alloc;
+
 	if (config_pool != NULL)
 		pool_unref(config_pool);
-
-	hook_quota_root_created = trash_next_hook_quota_root_created;
 }