/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "str.h"
#include "strescape.h"
#include "net.h"
#include "write-full.h"
#include "mail-search-build.h"
#include "index-storage.h"
#include "index-mailbox-size.h"
/*
Saving new mails: After transaction is committed and synced, trigger
vsize updating. Lock vsize updates. Check if the message count +
last-indexed-uid are still valid. If they are, add all the missing new
mails. Unlock.
Fetching vsize: Lock vsize updates. Check if the message count +
last-indexed-uid are still valid. If not, set them to zero. Add all
the missing mails. Unlock.
Expunging mails: Check if syncing would expunge any mails. If so, lock the
vsize updates before locking syncing (to avoid deadlocks). Check if the
message count + last-indexed-uid are still valid. If not, unlock vsize and
do nothing else. Otherwise, for each expunged mail whose UID <=
last-indexed-uid, decrease the message count and the vsize in memory. After
syncing is successfully committed, write the changes to header. Unlock.
Note that the final expunge handling with some mailbox formats is done while
syncing is no longer locked. Because of this we need to have the vsize
locking. The final vsize header update requires committing a transaction,
which internally is the same as a sync lock. So to avoid deadlocks we always
need to lock vsize updates before sync.
*/
struct mailbox_vsize_update {
bool lock_failed;
bool rebuild;
bool written;
bool finish_in_background;
};
{
const void *data;
if (size > 0) {
}
else {
if (size != 0) {
"vsize-hdr has invalid size: %"PRIuSIZE_T,
size);
}
}
}
static void
{
return;
seq2 = 0;
"vsize-hdr has invalid message-count (%u < %u)",
} else {
/* some messages have been expunged, rescan */
}
}
}
struct mailbox_vsize_update *
{
return update;
}
unsigned int lock_secs)
{
const char *error;
int ret;
return TRUE;
if (update->lock_failed)
return FALSE;
return FALSE;
if (ret <= 0) {
/* don't log lock timeouts, because we're somewhat expecting
them. Especially when lock_secs is 0. */
if (ret < 0)
return FALSE;
}
return TRUE;
}
{
return vsize_update_lock_full(update, 0);
}
{
}
{
}
static void
{
return;
/* no changes */
return;
}
(void)mail_index_transaction_commit(&trans);
}
{
const char *path;
int fd;
if (fd == -1) {
"Can't start vsize building on background: "
"net_connect_unix(%s) failed: %m", path);
return;
}
"Can't start vsize building on background: "
"write(%s) failed: %m", path);
}
i_close_fd(&fd);
}
{
if (update->finish_in_background)
}
{
return;
"vsize-hdr's message_count shrank below 0");
return;
}
"vsize-hdr's vsize shrank below 0");
return;
}
}
static int
bool require_result)
{
unsigned int mails_left;
int ret = 0;
/* nothing to do - we should have usually caught this already
before locking */
return 0;
}
/* note that update->view may be more up-to-date than box->view.
we'll just add whatever new mails are in box->view. if we'll notice
that some of the new mails are missing, we'll need to stop there
since that expunge will be applied later on to the vsize header. */
/* nothing existed, but update uidnext */
return 0;
}
if (!require_result)
mails_left = 0;
else
if (mails_left == 0) {
/* if there are any more mails whose vsize can't be
looked up from cache, abort and finish on
background. */
}
if (ret < 0 &&
/* abort and finish on background */
i_assert(mails_left == 0);
"Finishing vsize calculation on background");
if (require_result)
break;
}
/* slow vsize lookup */
i_assert(mails_left > 0);
mails_left--;
}
if (ret < 0) {
continue;
ret = -1;
break;
}
}
if (mailbox_search_deinit(&search_ctx) < 0)
ret = -1;
if (ret == 0) {
/* success, cache all */
} else {
/* search failed, cache only up to highest seen uid */
}
(void)mailbox_transaction_commit(&trans);
return ret;
}
struct mailbox_metadata *metadata_r)
{
int ret;
/* up to date */
return 0;
}
/* we need to update it - lock it if possible. if not, update it
anyway internally even though we won't be saving the result. */
return ret;
}
struct mailbox_metadata *metadata_r)
{
int ret = 0;
/* if physical size = virtual size always for the storage, we can
use the optimized vsize code for this */
return -1;
return 0;
}
/* do it the slow way (we could implement similar logic as for vsize,
but for now it's not really needed) */
return -1;
metadata_r->physical_size = 0;
else {
const char *errstr;
if (error != MAIL_ERROR_EXPUNGED) {
i_error("Couldn't get size of mail UID %u in %s: %s",
ret = -1;
break;
}
}
}
if (mailbox_search_deinit(&ctx) < 0) {
i_error("Listing mails in %s failed: %s",
ret = -1;
}
(void)mailbox_transaction_commit(&trans);
return ret;
}
{
/* update here only if we don't need to rebuild the whole vsize. */
/* Get the UIDNEXT only after checking that vsize updating is
even potentially wanted for this mailbox. We especially
don't want to do this with imapc, because it could trigger
a remote STATUS (UIDNEXT) call. */
}
}