changeset 21810:c6e5b4a3890a

quota: Use quota_alloc_result in quota_settings.test_alloc
author Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
date Thu, 23 Mar 2017 10:16:30 +0200
parents 370b09372136
children aadb3940e4df
files src/plugins/quota/quota-private.h src/plugins/quota/quota.c src/plugins/trash/trash-plugin.c
diffstat 3 files changed, 27 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/quota/quota-private.h	Thu Mar 23 13:48:04 2017 +0200
+++ b/src/plugins/quota/quota-private.h	Thu Mar 23 10:16:30 2017 +0200
@@ -22,8 +22,8 @@
 	pool_t pool;
 
 	ARRAY(struct quota_root_settings *) root_sets;
-	int (*test_alloc)(struct quota_transaction_context *ctx,
-			  uoff_t size, bool *too_large_r);
+	enum quota_alloc_result (*test_alloc)(
+		struct quota_transaction_context *ctx, uoff_t size);
 
 	const char *quota_exceeded_msg;
 	unsigned int debug:1;
--- a/src/plugins/quota/quota.c	Thu Mar 23 13:48:04 2017 +0200
+++ b/src/plugins/quota/quota.c	Thu Mar 23 10:16:30 2017 +0200
@@ -50,8 +50,8 @@
 	&quota_backend_maildir
 };
 
-static int quota_default_test_alloc(struct quota_transaction_context *ctx,
-				    uoff_t size, bool *too_large_r);
+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)
@@ -1215,33 +1215,21 @@
 		return QUOTA_ALLOC_RESULT_OK;
 	/* this is a virtual function mainly for trash plugin and similar,
 	   which may automatically delete mails to stay under quota. */
-	bool too_large = FALSE;
-	int ret = ctx->quota->set->test_alloc(ctx, size, &too_large);
-	if (ret < 0) {
-		return QUOTA_ALLOC_RESULT_TEMPFAIL;
-	} else if (ret == 0 && too_large) {
-		return QUOTA_ALLOC_RESULT_OVER_QUOTA_LIMIT;
-	} else if (ret == 0 && !too_large) {
-		return QUOTA_ALLOC_RESULT_OVER_QUOTA;
-	} else { /* (ret > 0) */
-		return QUOTA_ALLOC_RESULT_OK;
-	}
+	return ctx->quota->set->test_alloc(ctx, size);
 }
 
-static int quota_default_test_alloc(struct quota_transaction_context *ctx,
-				    uoff_t size, bool *too_large_r)
+static enum quota_alloc_result quota_default_test_alloc(
+			struct quota_transaction_context *ctx, uoff_t size)
 {
 	struct quota_root *const *roots;
 	unsigned int i, count;
 	bool ignore;
 	int ret;
 
-	*too_large_r = FALSE;
+	if (!quota_transaction_is_over(ctx, size))
+		return QUOTA_ALLOC_RESULT_OK;
 
-	if (!quota_transaction_is_over(ctx, size))
-		return 1;
-
-	/* limit reached. only thing left to do now is to set too_large_r. */
+	/* limit reached. */
 	roots = array_get(&ctx->quota->roots, &count);
 	for (i = 0; i < count; i++) {
 		uint64_t bytes_limit, count_limit;
@@ -1254,16 +1242,14 @@
 						 &bytes_limit, &count_limit,
 						 &ignore);
 		if (ret < 0)
-			return -1;
+			return QUOTA_ALLOC_RESULT_TEMPFAIL;
 
 		/* if size is bigger than any limit, then
 		   it is bigger than the lowest limit */
-		if (bytes_limit > 0 && size > bytes_limit) {
-			*too_large_r = TRUE;
-			break;
-		}
+		if (bytes_limit > 0 && size > bytes_limit)
+			return QUOTA_ALLOC_RESULT_OVER_QUOTA_LIMIT;
 	}
-	return 0;
+	return QUOTA_ALLOC_RESULT_OVER_QUOTA;
 }
 
 void quota_alloc(struct quota_transaction_context *ctx, struct mail *mail)
--- a/src/plugins/trash/trash-plugin.c	Thu Mar 23 13:48:04 2017 +0200
+++ b/src/plugins/trash/trash-plugin.c	Thu Mar 23 10:16:30 2017 +0200
@@ -44,8 +44,8 @@
 
 static MODULE_CONTEXT_DEFINE_INIT(trash_user_module,
 				  &mail_user_module_register);
-static int (*trash_next_quota_test_alloc)(struct quota_transaction_context *,
-					  uoff_t, bool *);
+static enum quota_alloc_result (*trash_next_quota_test_alloc)(
+		struct quota_transaction_context *, uoff_t);
 
 static int trash_clean_mailbox_open(struct trash_mailbox *trash)
 {
@@ -217,21 +217,22 @@
 	return 1;
 }
 
-static int
+static enum quota_alloc_result
 trash_quota_test_alloc(struct quota_transaction_context *ctx,
-		       uoff_t size, bool *too_large_r)
+		       uoff_t size)
 {
-	int ret, i;
+	int i;
 	uint64_t size_needed = 0;
 	unsigned int count_needed = 0;
 
 	for (i = 0; ; i++) {
-		ret = trash_next_quota_test_alloc(ctx, size, too_large_r);
-		if (ret != 0 || *too_large_r) {
-			if (ctx->quota->user->mail_debug && *too_large_r) {
+		enum quota_alloc_result ret;
+		ret = trash_next_quota_test_alloc(ctx, size);
+		if (ret != QUOTA_ALLOC_RESULT_OVER_QUOTA) {
+			if (ret == QUOTA_ALLOC_RESULT_OVER_QUOTA_LIMIT &&
+			    ctx->quota->user->mail_debug)
 				i_debug("trash plugin: Mail is larger than "
 					"quota, won't even try to handle");
-			}
 			return ret;
 		}
 
@@ -251,11 +252,10 @@
 			count_needed = 1 + ctx->count_over - ctx->count_ceil;
 
 		/* not enough space. try deleting some from mailbox. */
-		ret = trash_try_clean_mails(ctx, size_needed, count_needed);
-		if (ret <= 0)
-			return 0;
+		if (trash_try_clean_mails(ctx, size_needed, count_needed) <= 0)
+			return QUOTA_ALLOC_RESULT_OVER_QUOTA;
 	}
-	return 0;
+	return QUOTA_ALLOC_RESULT_OVER_QUOTA;
 }
 
 static bool trash_find_storage(struct mail_user *user,