changeset 21818:944cd7603aff

lib-storage: Change mail_user_autoexpunge() to return number of autoexpunged mails
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Thu, 23 Mar 2017 14:27:28 +0200
parents df29d7876ea5
children c7c9d84d9be3
files src/lib-storage/mail-autoexpunge.c src/lib-storage/mail-autoexpunge.h
diffstat 2 files changed, 26 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-storage/mail-autoexpunge.c	Thu Mar 23 14:19:42 2017 +0200
+++ b/src/lib-storage/mail-autoexpunge.c	Thu Mar 23 14:27:28 2017 +0200
@@ -59,7 +59,7 @@
 
 static int
 mailbox_autoexpunge(struct mailbox *box, unsigned int interval_time,
-		    unsigned int max_mails)
+		    unsigned int max_mails, unsigned int *expunged_count)
 {
 	struct mailbox_transaction_context *t;
 	struct mail *mail;
@@ -70,6 +70,7 @@
 	time_t timestamp, expire_time, last_rename_stamp = 0;
 	const void *data;
 	size_t size;
+	unsigned int count = 0;
 	int ret = 0;
 
 	if ((unsigned int)ioloop_time < interval_time)
@@ -118,6 +119,7 @@
 			   don't even check saved-dates before we're
 			   below max_mails. */
 			mail_expunge(mail);
+			count++;
 		} else if (interval_time == 0) {
 			/* only max_mails is used. nothing further to do. */
 			break;
@@ -125,6 +127,7 @@
 			if (I_MAX(last_rename_stamp, timestamp) > expire_time)
 				break;
 			mail_expunge(mail);
+			count++;
 		} else if (mailbox_get_last_mail_error(box) == MAIL_ERROR_EXPUNGED) {
 			/* already expunged */
 		} else {
@@ -136,13 +139,16 @@
 	mail_free(&mail);
 	if (mailbox_transaction_commit(&t) < 0)
 		ret = -1;
+	else
+		*expunged_count += count;
 	return ret;
 }
 
 static void
 mailbox_autoexpunge_set(struct mail_namespace *ns, const char *vname,
 			unsigned int autoexpunge,
-			unsigned int autoexpunge_max_mails)
+			unsigned int autoexpunge_max_mails,
+			unsigned int *expunged_count)
 {
 	struct mailbox *box;
 
@@ -150,7 +156,8 @@
 	   any ACLs the user might normally have against expunging in
 	   the mailbox. */
 	box = mailbox_alloc(ns->list, vname, MAILBOX_FLAG_IGNORE_ACLS);
-	if (mailbox_autoexpunge(box, autoexpunge, autoexpunge_max_mails) < 0) {
+	if (mailbox_autoexpunge(box, autoexpunge, autoexpunge_max_mails,
+				expunged_count) < 0) {
 		i_error("Failed to autoexpunge mailbox '%s': %s",
 			mailbox_get_vname(box),
 			mailbox_get_last_internal_error(box, NULL));
@@ -160,7 +167,8 @@
 
 static void
 mailbox_autoexpunge_wildcards(struct mail_namespace *ns,
-			      const struct mailbox_settings *set)
+			      const struct mailbox_settings *set,
+			      unsigned int *expunged_count)
 {
 	struct mailbox_list_iterate_context *iter;
 	const struct mailbox_info *info;
@@ -173,7 +181,8 @@
 				      MAILBOX_LIST_ITER_RETURN_NO_FLAGS);
 	while ((info = mailbox_list_iter_next(iter)) != NULL) T_BEGIN {
 		mailbox_autoexpunge_set(ns, info->vname, set->autoexpunge,
-					set->autoexpunge_max_mails);
+					set->autoexpunge_max_mails,
+					expunged_count);
 	} T_END;
 	if (mailbox_list_iter_deinit(&iter) < 0) {
 		i_error("Failed to iterate autoexpunge mailboxes '%s': %s",
@@ -183,7 +192,8 @@
 
 static bool
 mail_namespace_autoexpunge(struct mail_namespace *ns,
-			   struct mailbox_autoexpunge_lock *lock)
+			   struct mailbox_autoexpunge_lock *lock,
+			   unsigned int *expunged_count)
 {
 	struct mailbox_settings *const *box_set;
 	const char *vname;
@@ -200,7 +210,7 @@
 			return FALSE;
 
 		if (strpbrk((*box_set)->name, "*?") != NULL)
-			mailbox_autoexpunge_wildcards(ns, *box_set);
+			mailbox_autoexpunge_wildcards(ns, *box_set, expunged_count);
 		else {
 			if ((*box_set)->name[0] == '\0' && ns->prefix_len > 0 &&
 			    ns->prefix[ns->prefix_len-1] == mail_namespace_get_sep(ns))
@@ -208,20 +218,22 @@
 			else
 				vname = t_strconcat(ns->prefix, (*box_set)->name, NULL);
 			mailbox_autoexpunge_set(ns, vname, (*box_set)->autoexpunge,
-						(*box_set)->autoexpunge_max_mails);
+						(*box_set)->autoexpunge_max_mails,
+						expunged_count);
 		}
 	}
 	return TRUE;
 }
 
-void mail_user_autoexpunge(struct mail_user *user)
+unsigned int mail_user_autoexpunge(struct mail_user *user)
 {
 	struct mailbox_autoexpunge_lock lock = { .fd = -1 };
 	struct mail_namespace *ns;
+	unsigned int expunged_count;
 
 	for (ns = user->namespaces; ns != NULL; ns = ns->next) {
 		if (ns->alias_for == NULL) {
-			if (!mail_namespace_autoexpunge(ns, &lock))
+			if (!mail_namespace_autoexpunge(ns, &lock, &expunged_count))
 				break;
 		}
 	}
@@ -232,4 +244,5 @@
 		i_close_fd(&lock.fd);
 		file_lock_free(&lock.lock);
 	}
+	return expunged_count;
 }
--- a/src/lib-storage/mail-autoexpunge.h	Thu Mar 23 14:19:42 2017 +0200
+++ b/src/lib-storage/mail-autoexpunge.h	Thu Mar 23 14:27:28 2017 +0200
@@ -1,6 +1,8 @@
 #ifndef MAIL_AUTOEXPUNGE_H
 #define MAIL_AUTOEXPUNGE_H
 
-void mail_user_autoexpunge(struct mail_user *user);
+/* Perform autoexpunging for all the user's mailboxes that have autoexpunging
+   configured. Returns number of mails that were autoexpunged. */
+unsigned int mail_user_autoexpunge(struct mail_user *user);
 
 #endif