index-expunge.c revision 74a02d35d21807b9f9069964514d8c09e3631ea4
45312f52ff3a3d4c137447be4c7556500c2f8bf2Timo Sirainen/* Copyright (C) 2002 Timo Sirainen */
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen#include "lib.h"
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen#include "index-storage.h"
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen
ea5f188fc29dfaa0c4071e6413e16e1d04263722Timo Sirainenint index_expunge_seek_first(IndexMailbox *ibox, unsigned int *seq,
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen MailIndexRecord **rec)
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen{
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen MailIndexHeader *hdr;
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen
63a61b7a739ae0f3f520215137d9c50f94d0f34fTimo Sirainen i_assert(ibox->index->lock_type == MAIL_LOCK_EXCLUSIVE);
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen
18565c69efcd7db003dbf27cf625ed822e889fb1Timo Sirainen hdr = ibox->index->get_header(ibox->index);
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen if (hdr->deleted_messages_count == 0) {
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen /* no deleted messages */
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen *seq = 0;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen *rec = NULL;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen return TRUE;
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen }
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen /* find mails with DELETED flag and expunge them */
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen if (hdr->first_deleted_uid_lowwater > 1) {
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen *rec = hdr->first_deleted_uid_lowwater >= hdr->next_uid ? NULL :
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen ibox->index->lookup_uid_range(ibox->index,
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen hdr->first_deleted_uid_lowwater,
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen hdr->next_uid-1);
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen if (*rec == NULL) {
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen mail_storage_set_critical(ibox->box.storage,
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen "index header's deleted_messages_count (%u) "
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen "or first_deleted_uid_lowwater (%u) "
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen "is invalid.", hdr->deleted_messages_count,
a0b0d629931773c17a236f6214adbe0e13b9b3fdTimo Sirainen hdr->first_deleted_uid_lowwater);
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen /* fsck should be enough to fix it */
af1f4b17a92ca7b2661737e65c7849df289d3070Timo Sirainen ibox->index->set_flags |= MAIL_INDEX_FLAG_FSCK;
af1f4b17a92ca7b2661737e65c7849df289d3070Timo Sirainen return FALSE;
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainen } else {
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen *seq = ibox->index->get_sequence(ibox->index, *rec);
fe6c1556d3529a6376d4cbb3766c34aebde0de99Timo Sirainen }
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen } else {
01cbf4ac5d44137ab434791be7f838d98d0fcf3bTimo Sirainen *rec = ibox->index->lookup(ibox->index, 1);
4b41116563110d00330896a568eff1078c382827Timo Sirainen *seq = 1;
4b41116563110d00330896a568eff1078c382827Timo Sirainen }
992a13add4eea0810e4db0f042a595dddf85536aTimo Sirainen
ebe6df72f1309135f02b6a4d2aef1e81a073f91cTimo Sirainen return TRUE;
ebe6df72f1309135f02b6a4d2aef1e81a073f91cTimo Sirainen}
ebe6df72f1309135f02b6a4d2aef1e81a073f91cTimo Sirainen
b12a6d0e54318273acf0d0fb8b3f4c29f67b62b0Timo Sirainenint index_storage_expunge(Mailbox *box)
b12a6d0e54318273acf0d0fb8b3f4c29f67b62b0Timo Sirainen{
b12a6d0e54318273acf0d0fb8b3f4c29f67b62b0Timo Sirainen IndexMailbox *ibox = (IndexMailbox *) box;
b12a6d0e54318273acf0d0fb8b3f4c29f67b62b0Timo Sirainen int failed;
b12a6d0e54318273acf0d0fb8b3f4c29f67b62b0Timo Sirainen
b12a6d0e54318273acf0d0fb8b3f4c29f67b62b0Timo Sirainen if (box->readonly) {
fe6c1556d3529a6376d4cbb3766c34aebde0de99Timo Sirainen mail_storage_set_error(box->storage, "Mailbox is read-only");
73247459cf41eb1e5ae5bc61354db46d3b05ee75Timo Sirainen return FALSE;
73247459cf41eb1e5ae5bc61354db46d3b05ee75Timo Sirainen }
73247459cf41eb1e5ae5bc61354db46d3b05ee75Timo Sirainen
73247459cf41eb1e5ae5bc61354db46d3b05ee75Timo Sirainen if (!ibox->index->set_lock(ibox->index, MAIL_LOCK_EXCLUSIVE))
fe6c1556d3529a6376d4cbb3766c34aebde0de99Timo Sirainen return mail_storage_set_index_error(ibox);
fe6c1556d3529a6376d4cbb3766c34aebde0de99Timo Sirainen
fe6c1556d3529a6376d4cbb3766c34aebde0de99Timo Sirainen failed = !ibox->expunge_locked(ibox, NULL, NULL);
fe6c1556d3529a6376d4cbb3766c34aebde0de99Timo Sirainen
73247459cf41eb1e5ae5bc61354db46d3b05ee75Timo Sirainen if (!ibox->index->set_lock(ibox->index, MAIL_LOCK_UNLOCK) || failed)
73247459cf41eb1e5ae5bc61354db46d3b05ee75Timo Sirainen return mail_storage_set_index_error(ibox);
fe6c1556d3529a6376d4cbb3766c34aebde0de99Timo Sirainen return TRUE;
fe6c1556d3529a6376d4cbb3766c34aebde0de99Timo Sirainen}
fe6c1556d3529a6376d4cbb3766c34aebde0de99Timo Sirainen