Tomas Janousek 7515ec7
diff -Naur cyrus-imapd-2.3.8/imap/ctl_cyrusdb.c cyrus-imapd-2.3.8-rmquota+deletemailbox.uncompiled/imap/ctl_cyrusdb.c
Tomas Janousek 7515ec7
--- cyrus-imapd-2.3.8/imap/ctl_cyrusdb.c	2006-11-30 19:11:17.000000000 +0200
Tomas Janousek 7515ec7
+++ cyrus-imapd-2.3.8-rmquota+deletemailbox.uncompiled/imap/ctl_cyrusdb.c	2007-02-21 16:34:33.000000000 +0200
9e566a4
@@ -133,7 +133,7 @@
38f2261
     /* if it is MBTYPE_RESERVED, unset it & call mboxlist_delete */
38f2261
     if(!r && (mbtype & MBTYPE_RESERVE)) {
38f2261
 	if(!r) {
38f2261
-	    r = mboxlist_deletemailbox(name, 1, NULL, NULL, 0, 0, 1);
38f2261
+	    r = mboxlist_deletemailbox(name, 1, NULL, NULL, 0, 0, 1, 1);
38f2261
 	    if(r) {
38f2261
 		/* log the error */
38f2261
 		syslog(LOG_ERR,
Tomas Janousek 7515ec7
diff -Naur cyrus-imapd-2.3.8/imap/ctl_mboxlist.c cyrus-imapd-2.3.8-rmquota+deletemailbox.uncompiled/imap/ctl_mboxlist.c
Tomas Janousek 7515ec7
--- cyrus-imapd-2.3.8/imap/ctl_mboxlist.c	2007-02-05 20:41:46.000000000 +0200
Tomas Janousek 7515ec7
+++ cyrus-imapd-2.3.8-rmquota+deletemailbox.uncompiled/imap/ctl_mboxlist.c	2007-02-21 16:34:48.000000000 +0200
Tomas Janousek 7515ec7
@@ -461,7 +461,7 @@
38f2261
 	    
38f2261
 	    wipe_head = wipe_head->next;
38f2261
 	    
38f2261
-	    ret = mboxlist_deletemailbox(me->mailbox, 1, "", NULL, 0, 1, 1);
38f2261
+	    ret = mboxlist_deletemailbox(me->mailbox, 1, "", NULL, 0, 1, 1, 1);
Tomas Janousek 7515ec7
 	    if(!ret) sync_log_mailbox(me->mailbox);
38f2261
 	    if(ret) {
38f2261
 		fprintf(stderr, "couldn't delete defunct mailbox %s\n",
Tomas Janousek 7515ec7
diff -Naur cyrus-imapd-2.3.8/imap/imapd.c cyrus-imapd-2.3.8-rmquota+deletemailbox.uncompiled/imap/imapd.c
Tomas Janousek 7515ec7
--- cyrus-imapd-2.3.8/imap/imapd.c	2007-02-05 20:49:55.000000000 +0200
Tomas Janousek 7515ec7
+++ cyrus-imapd-2.3.8-rmquota+deletemailbox.uncompiled/imap/imapd.c	2007-02-21 16:34:58.000000000 +0200
Tomas Janousek 7515ec7
@@ -4983,7 +4983,7 @@
38f2261
 
38f2261
     r = mboxlist_deletemailbox(name, imapd_userisadmin,
38f2261
 			       imapd_userid, imapd_authstate,
38f2261
-			       0, 0, 0);
38f2261
+			       0, 0, 0, 1);
38f2261
     
9e566a4
     if (!r) sync_log_mailbox(name);
9e566a4
 
Tomas Janousek 7515ec7
@@ -5007,6 +5007,12 @@
38f2261
     char *p;
38f2261
     int domainlen = 0;
9e566a4
     int sync_lockfd = (-1);
38f2261
+    int keepQuota = 1;
38f2261
+
38f2261
+    if(name && *name == '+') {
Tomas Janousek 7515ec7
+        keepQuota = 0;
Tomas Janousek 7515ec7
+        name++;
38f2261
+    }
38f2261
 
38f2261
     r = (*imapd_namespace.mboxname_tointernal)(&imapd_namespace, name,
38f2261
 					       imapd_userid, mailboxname);
Tomas Janousek 7515ec7
@@ -5065,7 +5071,7 @@
38f2261
 
38f2261
 	r = mboxlist_deletemailbox(mailboxname, imapd_userisadmin,
9e566a4
 				   imapd_userid, imapd_authstate, 1-force,
38f2261
-				   localonly, 0);
38f2261
+				   localonly, 0, keepQuota);
38f2261
     }
38f2261
 
38f2261
     /* was it a top-level user mailbox? */
Tomas Janousek 7515ec7
@@ -6423,6 +6429,7 @@
38f2261
 {
38f2261
     int newquota = -1;
38f2261
     int badresource = 0;
38f2261
+    int rmquota = 0;
38f2261
     int c;
38f2261
     int force = 0;
38f2261
     static struct buf arg;
Tomas Janousek 7515ec7
@@ -6439,7 +6446,8 @@
38f2261
     if (c != ')' || arg.s[0] != '\0') {
38f2261
 	for (;;) {
38f2261
 	    if (c != ' ') goto badlist;
38f2261
-	    if (strcasecmp(arg.s, "storage") != 0) badresource = 1;
38f2261
+	    if (strcasecmp(arg.s, "remove") == 0) rmquota = 1;
38f2261
+	    else if (strcasecmp(arg.s, "storage") != 0) badresource = 1;
38f2261
 	    c = getword(imapd_in, &arg;;
38f2261
 	    if (c != ' ' && c != ')') goto badlist;
38f2261
 	    if (arg.s[0] == '\0') goto badlist;
Tomas Janousek 7515ec7
@@ -6508,7 +6516,10 @@
Tomas Janousek 7515ec7
 
Tomas Janousek 7515ec7
     /* local mailbox */
Tomas Janousek 7515ec7
     if (!r || (r == IMAP_MAILBOX_NONEXISTENT)) {
9e566a4
-	r = mboxlist_setquota(mailboxname, newquota, force);
Tomas Janousek 7515ec7
+        if(!rmquota) 
9e566a4
+	    r = mboxlist_setquota(mailboxname, newquota, force);
Tomas Janousek 7515ec7
+	else 
9e566a4
+	    r = mboxlist_unsetquota(mailboxname);
38f2261
     }
38f2261
 
9e566a4
     imapd_check(NULL, 0, 0);
Tomas Janousek 7515ec7
@@ -8221,7 +8232,7 @@
38f2261
 	/* note also that we need to remember to let proxyadmins do this */
38f2261
 	r = mboxlist_deletemailbox(mailboxname,
38f2261
 				   imapd_userisadmin || imapd_userisproxyadmin,
38f2261
-				   imapd_userid, imapd_authstate, 0, 1, 0);
38f2261
+				   imapd_userid, imapd_authstate, 0, 1, 0, 1);
38f2261
 	if(r) syslog(LOG_ERR,
38f2261
 		     "Could not delete local mailbox during move of %s",
38f2261
 		     mailboxname);
Tomas Janousek 7515ec7
diff -Naur cyrus-imapd-2.3.8/imap/mailbox.c cyrus-imapd-2.3.8-rmquota+deletemailbox.uncompiled/imap/mailbox.c
Tomas Janousek 7515ec7
--- cyrus-imapd-2.3.8/imap/mailbox.c	2007-02-05 20:41:47.000000000 +0200
Tomas Janousek 7515ec7
+++ cyrus-imapd-2.3.8-rmquota+deletemailbox.uncompiled/imap/mailbox.c	2007-02-21 16:35:10.000000000 +0200
Tomas Janousek 7515ec7
@@ -2745,27 +2745,7 @@
38f2261
 
38f2261
     seen_delete_mailbox(mailbox);
38f2261
 
38f2261
-    if (delete_quota_root && !rquota) {
38f2261
-	quota_delete(&mailbox->quota, &tid;;
38f2261
-	free(mailbox->quota.root);
38f2261
-	mailbox->quota.root = NULL;
38f2261
-    } else if (!rquota) {
38f2261
-	/* Free any quota being used by this mailbox */
38f2261
-	if (mailbox->quota.used >= mailbox->quota_mailbox_used) {
38f2261
-	    mailbox->quota.used -= mailbox->quota_mailbox_used;
38f2261
-	}
38f2261
-	else {
38f2261
-	    mailbox->quota.used = 0;
38f2261
-	}
38f2261
-	r = quota_write(&mailbox->quota, &tid;;
38f2261
-	if (r) {
38f2261
-	    syslog(LOG_ERR,
9e566a4
-		   "LOSTQUOTA: unable to record free of " UQUOTA_T_FMT " bytes in quota %s",
38f2261
-		   mailbox->quota_mailbox_used, mailbox->quota.root);
38f2261
-	}
38f2261
-	else
38f2261
-	    quota_commit(&tid;;
38f2261
-    }
38f2261
+    mailbox_updatequota(mailbox,NULL);
38f2261
 
Tomas Janousek 7515ec7
     /* Flush data (message file) directory */
Tomas Janousek 7515ec7
     mailbox_delete_files(mailbox->path);
Tomas Janousek 7515ec7
@@ -3392,3 +3372,48 @@
38f2261
 	if (*p == '.') *p = '/';
38f2261
     }
38f2261
 }
38f2261
+
38f2261
+/* This function is used to update the quota. Can be used to replace
Tomas Janousek 7515ec7
+ * identical parts of the code, and can be quite handy some times
38f2261
+ * The tid is used in order to make possible to make the quota update
38f2261
+ * being a part of a bigger transaction to the quota db */
38f2261
+int mailbox_updatequota(struct mailbox *mailbox, struct txn **tid)
38f2261
+{
38f2261
+    int r = 0, havetid = 0;
38f2261
+    struct txn **ltid = NULL;
38f2261
+
38f2261
+    if(tid) {
38f2261
+        ltid = tid;
38f2261
+        havetid = 1;
38f2261
+    }
38f2261
+    /* Ensure that we are locked */
38f2261
+    if(!mailbox->header_lock_count) return IMAP_INTERNAL;
38f2261
+
38f2261
+
38f2261
+    if(mailbox->quota.root) {
38f2261
+        r = quota_read(&mailbox->quota, ltid, 1);
38f2261
+        if( r == 0 ) {
38f2261
+            if (mailbox->quota.used >= mailbox->quota_mailbox_used) {
38f2261
+                mailbox->quota.used -= mailbox->quota_mailbox_used;
38f2261
+            }
38f2261
+            else {
38f2261
+                mailbox->quota.used = 0;
38f2261
+            }
38f2261
+            r = quota_write(&mailbox->quota, ltid);
38f2261
+            if (r) {
38f2261
+                syslog(LOG_ERR,
38f2261
+                   "LOSTQUOTA: unable to record free of %lu bytes in quota %s",
38f2261
+                   mailbox->quota_mailbox_used, mailbox->quota.root);
38f2261
+            }
38f2261
+            else if(!havetid)
9e566a4
+                quota_commit(tid);
38f2261
+        }
38f2261
+        /* It is not a big mistake not to have quota .. just remove from the mailbox */
38f2261
+        else if ( r == IMAP_QUOTAROOT_NONEXISTENT) {
38f2261
+                free(mailbox->quota.root);
38f2261
+                r = 0;
38f2261
+        }
38f2261
+    }
38f2261
+    return r;
38f2261
+}
38f2261
+
Tomas Janousek 7515ec7
diff -Naur cyrus-imapd-2.3.8/imap/mailbox.h cyrus-imapd-2.3.8-rmquota+deletemailbox.uncompiled/imap/mailbox.h
Tomas Janousek 7515ec7
--- cyrus-imapd-2.3.8/imap/mailbox.h	2006-11-30 19:11:19.000000000 +0200
Tomas Janousek 7515ec7
+++ cyrus-imapd-2.3.8-rmquota+deletemailbox.uncompiled/imap/mailbox.h	2007-02-21 16:35:20.000000000 +0200
Tomas Janousek 7515ec7
@@ -364,6 +364,8 @@
38f2261
 			  struct mailbox *mailboxp);
38f2261
 extern int mailbox_delete(struct mailbox *mailbox, int delete_quota_root);
38f2261
 
38f2261
+extern int mailbox_updatequota(struct mailbox *mailbox, struct txn **tid);
38f2261
+
38f2261
 extern int mailbox_rename_copy(struct mailbox *oldmailbox, 
9e566a4
 			       const char *newname, char *newpartition,
38f2261
 			       bit32 *olduidvalidityp, bit32 *newuidvalidityp,
Tomas Janousek 7515ec7
diff -Naur cyrus-imapd-2.3.8/imap/mboxlist.c cyrus-imapd-2.3.8-rmquota+deletemailbox.uncompiled/imap/mboxlist.c
Tomas Janousek 7515ec7
--- cyrus-imapd-2.3.8/imap/mboxlist.c	2007-02-05 20:41:47.000000000 +0200
Tomas Janousek 7515ec7
+++ cyrus-imapd-2.3.8-rmquota+deletemailbox.uncompiled/imap/mboxlist.c	2007-02-21 16:35:29.000000000 +0200
Tomas Janousek 7515ec7
@@ -95,6 +95,12 @@
38f2261
 static int mboxlist_opensubs();
38f2261
 static void mboxlist_closesubs();
38f2261
 
38f2261
+static int child_cb(char *name,
38f2261
+                    int matchlen __attribute__((unused)),
38f2261
+                    int maycreate __attribute__((unused)),
38f2261
+                    void *rock);
38f2261
+
Tomas Janousek 7515ec7
+
38f2261
 static int mboxlist_rmquota(const char *name, int matchlen, int maycreate,
38f2261
 			    void *rock);
38f2261
 static int mboxlist_changequota(const char *name, int matchlen, int maycreate,
Tomas Janousek 7515ec7
@@ -102,6 +108,7 @@
38f2261
 
38f2261
 struct change_rock {
38f2261
     struct quota *quota;
38f2261
+    struct quota *oldquota;
38f2261
     struct txn **tid;
38f2261
 };
38f2261
 
Tomas Janousek 7515ec7
@@ -889,9 +896,9 @@
38f2261
  */
38f2261
 int mboxlist_deletemailbox(const char *name, int isadmin, char *userid, 
38f2261
 			   struct auth_state *auth_state, int checkacl,
38f2261
-			   int local_only, int force)
38f2261
+			   int local_only, int force, int keepQuota)
38f2261
 {
38f2261
-    int r;
38f2261
+    int r, has_children = 0;
38f2261
     char *acl;
38f2261
     long access;
38f2261
     struct mailbox mailbox;
Tomas Janousek 7515ec7
@@ -902,6 +909,7 @@
9e566a4
     int mbtype;
38f2261
     const char *p;
38f2261
     mupdate_handle *mupdate_h = NULL;
38f2261
+    char *quotaroot = NULL;
38f2261
 
38f2261
     if(!isadmin && force) return IMAP_PERMISSION_DENIED;
38f2261
 
Tomas Janousek 7515ec7
@@ -1014,15 +1022,47 @@
38f2261
 
38f2261
     if ((r && !force) || isremote) goto done;
38f2261
 
38f2261
-    if (!r || force) r = mailbox_delete(&mailbox, deletequotaroot);
38f2261
+    if (!r || force) {
38f2261
+        /* first we have to keep the previous quota root in order to delete it */
38f2261
+        if(mailbox.quota.root)
38f2261
+                quotaroot = xstrdup(mailbox.quota.root);
Tomas Janousek 7515ec7
+        r = mailbox_delete(&mailbox, deletequotaroot);
38f2261
+    }
Tomas Janousek 7515ec7
+
38f2261
 
38f2261
     /*
38f2261
      * See if we have to remove mailbox's quota root
38f2261
      */
38f2261
-    if (!r && mailbox.quota.root != NULL) {
Tomas Janousek 7515ec7
-	/* xxx look for any other mailboxes in this quotaroot */
38f2261
+    if (!r && quotaroot != NULL) {
Tomas Janousek 7515ec7
+        /* xxx look for any other mailboxes in this quotaroot */
Tomas Janousek 7515ec7
+        /* If we have not asked to remove the quota (default behaviour), we check
38f2261
+         * whether there are any subfolders beneeth the quota root. If there aren't
38f2261
+         * any subfolders the reasonable thing is to delete the quota */
38f2261
+        if(keepQuota) {
38f2261
+            char pattern[MAX_MAILBOX_PATH+1];
38f2261
+            strlcpy(pattern, quotaroot, sizeof(pattern));
38f2261
+            if (config_virtdomains && name[strlen(name)-1] == '!') {
38f2261
+                strlcat(pattern, "*", sizeof(pattern));
38f2261
+            }
38f2261
+            else {
38f2261
+                strlcat(pattern, ".*", sizeof(pattern));
Tomas Janousek 7515ec7
+            }   
Tomas Janousek 7515ec7
+                /* find if there are subfolders. Then we want to
Tomas Janousek 7515ec7
+                 * keep the existing quota */
38f2261
+            mboxlist_findall(NULL, pattern, isadmin, userid,
38f2261
+                    auth_state, child_cb, (void *) &has_children);
38f2261
+
Tomas Janousek 7515ec7
+            if(!has_children)
Tomas Janousek 7515ec7
+                if(!mboxlist_mylookup(quotaroot, NULL, NULL, NULL, NULL, NULL, 0, 1))
Tomas Janousek 7515ec7
+                    has_children = 1;
38f2261
+        }
38f2261
+        /* If we want to remove the quota explicitely or the quota root folder has no subfolders
38f2261
+         * we execute the rmquota patch */
38f2261
+        if(!keepQuota || !has_children )
38f2261
+            mboxlist_unsetquota(quotaroot);
38f2261
+        free(quotaroot);
38f2261
     }
Tomas Janousek 7515ec7
-
Tomas Janousek 7515ec7
+    
38f2261
  done:
Tomas Janousek 7515ec7
     if(r && tid && !force) {
Tomas Janousek 7515ec7
 	/* Abort the transaction if it is still in progress */
Tomas Janousek 7515ec7
@@ -2483,6 +2523,7 @@
38f2261
     if (r) return r;
38f2261
 
38f2261
     crock.quota = "a;
38f2261
+    crock.oldquota = NULL;
38f2261
     crock.tid = &tid;
38f2261
     /* top level mailbox */
38f2261
     if(have_mailbox)
Tomas Janousek 7515ec7
@@ -2501,15 +2542,19 @@
38f2261
  */
38f2261
 int mboxlist_unsetquota(const char *root)
38f2261
 {
38f2261
+    char newquota[MAX_MAILBOX_PATH+1];
38f2261
     char pattern[MAX_MAILBOX_PATH+1];
38f2261
     struct quota quota;
38f2261
-    int r=0;
38f2261
+    struct change_rock crock;
38f2261
+    int r=0, k=0;
38f2261
 
38f2261
     if (!root[0] || root[0] == '.' || strchr(root, '/')
38f2261
 	|| strchr(root, '*') || strchr(root, '%') || strchr(root, '?')) {
38f2261
 	return IMAP_MAILBOX_BADNAME;
38f2261
     }
38f2261
     
Tomas Janousek 7515ec7
+    crock.tid=NULL;
Tomas Janousek 7515ec7
+    
38f2261
     quota.root = (char *) root;
Tomas Janousek 7515ec7
     r = quota_read(&quota, NULL, 0);
38f2261
     if (r == IMAP_QUOTAROOT_NONEXISTENT) {
Tomas Janousek 7515ec7
@@ -2534,9 +2579,47 @@
Tomas Janousek 7515ec7
     /* submailboxes - we're using internal names here */
Tomas Janousek 7515ec7
     mboxlist_findall(NULL, pattern, 1, 0, 0, mboxlist_rmquota, (void *)root);
38f2261
 
38f2261
-    r = quota_delete(&quota, NULL);
38f2261
+    r = quota_delete(&quota, crock.tid);
38f2261
+
38f2261
+    /* If we cannot delete the quota then abort the operation */
38f2261
+    if(!r) {
38f2261
+       /* quota_findroot performs several checks that we can
38f2261
+        * assume that are already done, and don't have to perform
38f2261
+        * them again. One of them is that it returns 1 only if
38f2261
+        * quotaroot exists.
38f2261
+        */
38f2261
+        if(quota_findroot(newquota, sizeof(newquota), root)) {
38f2261
+            struct quota rootquota;
38f2261
+            rootquota.root = newquota;
38f2261
+            k = quota_read(&rootquota, crock.tid, 0);
38f2261
+            if (!k) {
38f2261
+                crock.quota = &rootquota;
38f2261
+                crock.oldquota = "a;
38f2261
+                /* top level mailbox */
38f2261
+                k = mboxlist_changequota(root, 0, 0, &crock);
38f2261
+            }
38f2261
+            /* submailboxes - we're using internal names here */
38f2261
+            if (!k)
38f2261
+                k = mboxlist_findall(NULL, pattern, 1, 0, 0, mboxlist_changequota, &crock);
38f2261
+            if(!k)
38f2261
+                k = quota_write(&rootquota, crock.tid);
38f2261
+
38f2261
+         }
38f2261
+         else {
38f2261
+           /* top level mailbox */
38f2261
+           mboxlist_rmquota(root, 0, 0, (void *)root);
38f2261
+           /* submailboxes - we're using internal names here */
38f2261
+           mboxlist_findall(NULL, pattern, 1, 0, 0, mboxlist_rmquota, (void *)root);
38f2261
+       }
38f2261
+    }
38f2261
+
38f2261
+    if(!r && !k)
38f2261
+        quota_commit(crock.tid);
38f2261
+    else
38f2261
+        quota_abort(crock.tid);
38f2261
 
38f2261
     return r;
Tomas Janousek 7515ec7
+
38f2261
 }
Tomas Janousek 7515ec7
 
Tomas Janousek 7515ec7
 /*
Tomas Janousek 7515ec7
@@ -2632,6 +2715,7 @@
38f2261
     struct mailbox mailbox;
38f2261
     struct change_rock *crock = (struct change_rock *) rock;
38f2261
     struct quota *mboxlist_newquota = crock->quota;
38f2261
+    struct quota *mboxlist_oldquota = crock->oldquota;
38f2261
     struct txn **tid = crock->tid;
38f2261
 
38f2261
     assert(rock != NULL);
Tomas Janousek 7515ec7
@@ -2649,27 +2733,24 @@
38f2261
     if (r) goto error;
38f2261
 
38f2261
     if (mailbox.quota.root) {
38f2261
-	if (strlen(mailbox.quota.root) >= strlen(mboxlist_newquota->root)) {
38f2261
-	    /* Part of a child quota root */
38f2261
-	    mailbox_close(&mailbox);
38f2261
-	    return 0;
38f2261
-	}
38f2261
-
38f2261
-	r = quota_read(&mailbox.quota, tid, 1);
38f2261
-	if (r) goto error;
38f2261
-	if (mailbox.quota.used >= mailbox.quota_mailbox_used) {
38f2261
-	    mailbox.quota.used -= mailbox.quota_mailbox_used;
Tomas Janousek 7515ec7
-	}
Tomas Janousek 7515ec7
-	else {
38f2261
-	    mailbox.quota.used = 0;
38f2261
-	}
38f2261
-	r = quota_write(&mailbox.quota, tid);
38f2261
-	if (r) {
38f2261
-	    syslog(LOG_ERR,
9e566a4
-		   "LOSTQUOTA: unable to record free of " UQUOTA_T_FMT " bytes in quota %s",
38f2261
-		   mailbox.quota_mailbox_used, mailbox.quota.root);
Tomas Janousek 7515ec7
-	}
38f2261
-	free(mailbox.quota.root);
Tomas Janousek 7515ec7
+        if(mboxlist_oldquota) {
Tomas Janousek 7515ec7
+            if (strlen(mailbox.quota.root) > strlen(mboxlist_oldquota->root)) {
Tomas Janousek 7515ec7
+                /* Part of a child quota root */
Tomas Janousek 7515ec7
+                mailbox_close(&mailbox);
Tomas Janousek 7515ec7
+                return 0;
Tomas Janousek 7515ec7
+            }
Tomas Janousek 7515ec7
+        }
Tomas Janousek 7515ec7
+        else {
Tomas Janousek 7515ec7
+            if (strlen(mailbox.quota.root) >= strlen(mboxlist_newquota->root)) {
Tomas Janousek 7515ec7
+                /* Part of a child quota root */
Tomas Janousek 7515ec7
+                mailbox_close(&mailbox);
Tomas Janousek 7515ec7
+                return 0;
Tomas Janousek 7515ec7
+            }
Tomas Janousek 7515ec7
+        }
38f2261
+
Tomas Janousek 7515ec7
+        r = mailbox_updatequota(&mailbox,tid);
Tomas Janousek 7515ec7
+        if (r)
Tomas Janousek 7515ec7
+            goto error;
38f2261
     }
38f2261
 
38f2261
     mailbox.quota.root = xstrdup(mboxlist_newquota->root);
Tomas Janousek 7515ec7
@@ -2682,6 +2763,11 @@
Tomas Janousek 7515ec7
 
38f2261
  error:
38f2261
     mailbox_close(&mailbox);
38f2261
+    syslog(LOG_ERR, "LOSTQUOTA: unable to change quota root for %s to %s: %s. \
38f2261
+                        Command aborted. Run reconstruct to make sure mailboxes \
38f2261
+                         are in consistent state",
38f2261
+           name, mboxlist_newquota->root, error_message(r));
38f2261
+    return 1;
38f2261
  error_noclose:
38f2261
     syslog(LOG_ERR, "LOSTQUOTA: unable to change quota root for %s to %s: %s",
Tomas Janousek 7515ec7
 	   name, mboxlist_newquota->root, error_message(r));
Tomas Janousek 7515ec7
diff -Naur cyrus-imapd-2.3.8/imap/mboxlist.h cyrus-imapd-2.3.8-rmquota+deletemailbox.uncompiled/imap/mboxlist.h
Tomas Janousek 7515ec7
--- cyrus-imapd-2.3.8/imap/mboxlist.h	2006-11-30 19:11:19.000000000 +0200
Tomas Janousek 7515ec7
+++ cyrus-imapd-2.3.8-rmquota+deletemailbox.uncompiled/imap/mboxlist.h	2007-02-21 16:35:34.000000000 +0200
Tomas Janousek 7515ec7
@@ -126,7 +126,7 @@
38f2261
  * the planet */
38f2261
 int mboxlist_deletemailbox(const char *name, int isadmin, char *userid, 
38f2261
 			   struct auth_state *auth_state, int checkacl,
38f2261
-			   int local_only, int force);
38f2261
+			   int local_only, int force, int keepQuota);
38f2261
 
38f2261
 /* Rename/move a mailbox (hierarchical) */
38f2261
 int mboxlist_renamemailbox(char *oldname, char *newname, char *partition, 
Tomas Janousek 7515ec7
diff -Naur cyrus-imapd-2.3.8/imap/mupdate.c cyrus-imapd-2.3.8-rmquota+deletemailbox.uncompiled/imap/mupdate.c
Tomas Janousek 7515ec7
--- cyrus-imapd-2.3.8/imap/mupdate.c	2007-01-31 16:10:05.000000000 +0200
Tomas Janousek 7515ec7
+++ cyrus-imapd-2.3.8-rmquota+deletemailbox.uncompiled/imap/mupdate.c	2007-02-21 16:35:44.000000000 +0200
Tomas Janousek 7515ec7
@@ -2313,7 +2313,7 @@
38f2261
 	    remote_boxes.head = r->next;
38f2261
 	} else if (ret < 0) {
38f2261
 	    /* Local without corresponding remote, delete it */
38f2261
-	    mboxlist_deletemailbox(l->mailbox, 1, "", NULL, 0, 0, 0);
38f2261
+	    mboxlist_deletemailbox(l->mailbox, 1, "", NULL, 0, 0, 0, 1);
38f2261
 	    local_boxes.head = l->next;
38f2261
 	} else /* (ret > 0) */ {
38f2261
 	    /* Remote without corresponding local, insert it */
Tomas Janousek 7515ec7
@@ -2328,7 +2328,7 @@
38f2261
     if(l && !r) {
38f2261
 	/* we have more deletes to do */
38f2261
 	while(l) {
38f2261
-	    mboxlist_deletemailbox(l->mailbox, 1, "", NULL, 0, 0, 0);
38f2261
+	    mboxlist_deletemailbox(l->mailbox, 1, "", NULL, 0, 0, 0, 1);
38f2261
 	    local_boxes.head = l->next;
38f2261
 	    l = local_boxes.head;
38f2261
 	}
Tomas Janousek 7515ec7
diff -Naur cyrus-imapd-2.3.8/imap/nntpd.c cyrus-imapd-2.3.8-rmquota+deletemailbox.uncompiled/imap/nntpd.c
Tomas Janousek 7515ec7
--- cyrus-imapd-2.3.8/imap/nntpd.c	2007-02-05 20:49:56.000000000 +0200
Tomas Janousek 7515ec7
+++ cyrus-imapd-2.3.8-rmquota+deletemailbox.uncompiled/imap/nntpd.c	2007-02-21 16:35:53.000000000 +0200
Tomas Janousek 7515ec7
@@ -3358,7 +3358,7 @@
38f2261
     /* XXX should we delete right away, or wait until empty? */
38f2261
 
38f2261
     r = mboxlist_deletemailbox(mailboxname, 0,
38f2261
-			       newsmaster, newsmaster_authstate, 1, 0, 0);
38f2261
+			       newsmaster, newsmaster_authstate, 1, 0, 0, 1);
38f2261
 
9e566a4
     if (!r) sync_log_mailbox(mailboxname);
9e566a4
 
Tomas Janousek 7515ec7
diff -Naur cyrus-imapd-2.3.8/imap/sync_reset.c cyrus-imapd-2.3.8-rmquota+deletemailbox.uncompiled/imap/sync_reset.c
Tomas Janousek 7515ec7
--- cyrus-imapd-2.3.8/imap/sync_reset.c	2006-11-30 19:11:20.000000000 +0200
Tomas Janousek 7515ec7
+++ cyrus-imapd-2.3.8-rmquota+deletemailbox.uncompiled/imap/sync_reset.c	2007-02-22 13:23:41.000000000 +0200
Tomas Janousek 7515ec7
@@ -258,7 +258,7 @@
9e566a4
     if (r) goto fail;
9e566a4
 
9e566a4
     for (item = list->head ; item ; item = item->next) {
9e566a4
-        r=mboxlist_deletemailbox(item->name, 1, NULL, sync_authstate, 1, 0, 0);
9e566a4
+        r=mboxlist_deletemailbox(item->name, 1, NULL, sync_authstate, 1, 0, 0, 1);
9e566a4
 
9e566a4
         if (r) goto fail;
9e566a4
     }
Tomas Janousek 7515ec7
@@ -274,7 +274,7 @@
9e566a4
     if (r) goto fail;
9e566a4
 
9e566a4
     for (item = list->head ; item ; item = item->next) {
9e566a4
-        r=mboxlist_deletemailbox(item->name, 1, NULL, sync_authstate, 1, 0, 0);
9e566a4
+        r=mboxlist_deletemailbox(item->name, 1, NULL, sync_authstate, 1, 0, 0, 1);
9e566a4
 
9e566a4
         if (r) goto fail;
9e566a4
     }
Tomas Janousek 7515ec7
@@ -282,7 +282,7 @@
9e566a4
 
9e566a4
     /* Nuke inbox (recursive nuke possible?) */
9e566a4
     snprintf(buf, sizeof(buf)-1, "user.%s", user);
9e566a4
-    r = mboxlist_deletemailbox(buf, 1, "cyrus", sync_authstate, 1, 0, 0);
9e566a4
+    r = mboxlist_deletemailbox(buf, 1, "cyrus", sync_authstate, 1, 0, 0, 1);
9e566a4
     if (r && (r != IMAP_MAILBOX_NONEXISTENT)) goto fail;
9e566a4
 
9e566a4
     if ((r=user_deletedata(user, sync_userid, sync_authstate, 1)))
Tomas Janousek 7515ec7
diff -Naur cyrus-imapd-2.3.8/imap/sync_server.c cyrus-imapd-2.3.8-rmquota+deletemailbox.uncompiled/imap/sync_server.c
Tomas Janousek 7515ec7
--- cyrus-imapd-2.3.8/imap/sync_server.c	2006-11-30 19:11:20.000000000 +0200
Tomas Janousek 7515ec7
+++ cyrus-imapd-2.3.8-rmquota+deletemailbox.uncompiled/imap/sync_server.c	2007-02-22 13:24:17.000000000 +0200
Tomas Janousek 7515ec7
@@ -1581,7 +1581,7 @@
9e566a4
 
9e566a4
     for (item = list->head ; item ; item = item->next) {
9e566a4
         r=mboxlist_deletemailbox(item->name, sync_userisadmin, sync_userid,
9e566a4
-				 sync_authstate, 0, 0, 1);
9e566a4
+				 sync_authstate, 0, 0, 1, 1);
9e566a4
 
9e566a4
         if (r) goto fail;
9e566a4
     }
Tomas Janousek 7515ec7
@@ -1591,7 +1591,7 @@
9e566a4
     (sync_namespacep->mboxname_tointernal)(sync_namespacep, "INBOX",
9e566a4
 					   user, buf);
9e566a4
     r = mboxlist_deletemailbox(buf, sync_userisadmin, sync_userid,
9e566a4
-			       sync_authstate, 0, 0, 1);
9e566a4
+			       sync_authstate, 0, 0, 1, 1);
9e566a4
     if (r && (r != IMAP_MAILBOX_NONEXISTENT)) goto fail;
9e566a4
 
9e566a4
     if ((r=user_deletedata(user, sync_userid, sync_authstate, 1)))
Tomas Janousek 7515ec7
@@ -2526,7 +2526,7 @@
9e566a4
 
9e566a4
     /* Delete with admin priveleges */
9e566a4
     r = mboxlist_deletemailbox(name, sync_userisadmin, sync_userid,
9e566a4
-			       sync_authstate, 0, 0, 1);
9e566a4
+			       sync_authstate, 0, 0, 1, 1);
9e566a4
 
9e566a4
     if (r)
9e566a4
         prot_printf(sync_out, "NO Failed to delete %s: %s\r\n",