[dovecot-cvs] dovecot/src/plugins/quota Makefile.am, 1.8, 1.9 quota-count.c, 1.1, 1.2 quota-dict.c, 1.17, 1.18 quota-private.h, 1.14, 1.15 quota.c, 1.16, 1.17

tss at dovecot.org tss at dovecot.org
Sun Dec 3 18:55:42 UTC 2006


Update of /var/lib/cvs/dovecot/src/plugins/quota
In directory talvi:/tmp/cvs-serv13941

Modified Files:
	Makefile.am quota-dict.c quota-private.h quota.c 
Added Files:
	quota-count.c 
Log Message:
dict quota: If dictionary doesn't yet contain the quota, calculate it by
going through all mails in all mailboxes.



Index: Makefile.am
===================================================================
RCS file: /var/lib/cvs/dovecot/src/plugins/quota/Makefile.am,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- Makefile.am	17 Jun 2006 18:03:51 -0000	1.8
+++ Makefile.am	3 Dec 2006 18:55:38 -0000	1.9
@@ -14,6 +14,7 @@
 
 lib01_quota_plugin_la_SOURCES = \
 	quota.c \
+	quota-count.c \
 	quota-fs.c \
 	quota-dict.c \
 	quota-dirsize.c \


Index: quota-dict.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/plugins/quota/quota-dict.c,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -d -r1.17 -r1.18
--- quota-dict.c	22 Nov 2006 17:23:09 -0000	1.17
+++ quota-dict.c	3 Dec 2006 18:55:38 -0000	1.18
@@ -72,29 +72,56 @@
 }
 
 static int
+dict_quota_count(struct dict_quota_root *root,
+		 bool want_bytes, uint64_t *value_r)
+{
+	struct dict_transaction_context *dt;
+	uint64_t bytes, count;
+
+	if (quota_count(root->root.quota, &bytes, &count) < 0)
+		return -1;
+
+	t_push();
+	dt = dict_transaction_begin(root->dict);
+	dict_set(dt, DICT_QUOTA_CURRENT_BYTES_PATH, dec2str(bytes));
+	dict_set(dt, DICT_QUOTA_CURRENT_COUNT_PATH, dec2str(count));
+	t_pop();
+
+	if (dict_transaction_commit(dt) < 0)
+		i_error("dict_quota: Couldn't update quota");
+
+	*value_r = want_bytes ? bytes : count;
+	return 1;
+}
+
+static int
 dict_quota_get_resource(struct quota_root *_root, const char *name,
 			uint64_t *value_r, uint64_t *limit __attr_unused__)
 {
 	struct dict_quota_root *root = (struct dict_quota_root *)_root;
 	const char *value;
+	bool want_bytes;
 	int ret;
 
-	if (strcmp(name, QUOTA_NAME_STORAGE_BYTES) == 0) {
-		t_push();
-		ret = dict_lookup(root->dict, unsafe_data_stack_pool,
-				  DICT_QUOTA_CURRENT_BYTES_PATH, &value);
-		*value_r = ret <= 0 ? 0 : strtoull(value, NULL, 10);
-		t_pop();
-	} else if (strcmp(name, QUOTA_NAME_MESSAGES) == 0) {
-		t_push();
-		ret = dict_lookup(root->dict, unsafe_data_stack_pool,
-				  DICT_QUOTA_CURRENT_COUNT_PATH, &value);
-		*value_r = ret <= 0 ? 0 : strtoull(value, NULL, 10);
-		t_pop();
-	} else {
+	if (strcmp(name, QUOTA_NAME_STORAGE_BYTES) == 0)
+		want_bytes = TRUE;
+	else if (strcmp(name, QUOTA_NAME_MESSAGES) == 0)
+		want_bytes = FALSE;
+	else
 		return 0;
-	}
 
+	t_push();
+	ret = dict_lookup(root->dict, unsafe_data_stack_pool,
+			  want_bytes ? DICT_QUOTA_CURRENT_BYTES_PATH :
+			  DICT_QUOTA_CURRENT_COUNT_PATH, &value);
+	if (ret < 0)
+		*value_r = 0;
+	else if (ret == 0)
+		ret = dict_quota_count(root, want_bytes, value_r);
+	else
+		*value_r = strtoull(value, NULL, 10);
+
+	t_pop();
 	return ret;
 }
 
@@ -106,10 +133,14 @@
 	struct dict_transaction_context *dt;
 
 	dt = dict_transaction_begin(root->dict);
-	dict_atomic_inc(dt, DICT_QUOTA_CURRENT_BYTES_PATH,
-			ctx->bytes_used);
-	dict_atomic_inc(dt, DICT_QUOTA_CURRENT_COUNT_PATH,
-			ctx->count_used);
+	if (ctx->bytes_used != 0) {
+		dict_atomic_inc(dt, DICT_QUOTA_CURRENT_BYTES_PATH,
+				ctx->bytes_used);
+	}
+	if (ctx->count_used != 0) {
+		dict_atomic_inc(dt, DICT_QUOTA_CURRENT_COUNT_PATH,
+				ctx->count_used);
+	}
 	
 	if (dict_transaction_commit(dt) < 0)
 		return -1;

Index: quota-private.h
===================================================================
RCS file: /var/lib/cvs/dovecot/src/plugins/quota/quota-private.h,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -d -r1.14 -r1.15
--- quota-private.h	18 Nov 2006 13:41:41 -0000	1.14
+++ quota-private.h	3 Dec 2006 18:55:38 -0000	1.15
@@ -14,6 +14,8 @@
 
 	int (*test_alloc)(struct quota_transaction_context *ctx,
 			  uoff_t size, bool *too_large_r);
+
+	unsigned int counting:1;
 };
 
 struct quota_backend_vfuncs {
@@ -81,4 +83,6 @@
 void quota_remove_user_storage(struct quota *quota, 
 			       struct mail_storage *storage);
 
+int quota_count(struct quota *quota, uint64_t *bytes_r, uint64_t *count_r);
+
 #endif

Index: quota.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/plugins/quota/quota.c,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -d -r1.16 -r1.17
--- quota.c	22 Nov 2006 17:23:09 -0000	1.16
+++ quota.c	3 Dec 2006 18:55:38 -0000	1.17
@@ -402,7 +402,12 @@
 	ctx->quota = quota;
 	ctx->box = box;
 	ctx->bytes_left = (uint64_t)-1;
-	ctx->count_left = (uint64_t) -1;
+	ctx->count_left = (uint64_t)-1;
+
+	if (quota->counting) {
+		/* we got here through quota_count_storage() */
+		return ctx;
+	}
 
 	/* find the lowest quota limits from all roots and use them */
 	roots = array_get(&quota->roots, &count);



More information about the dovecot-cvs mailing list