Skip to content

Commit

Permalink
lib-storage: Added mail_get_stream_because() and mail_get_hdr_stream_…
Browse files Browse the repository at this point in the history
…because()

With mail_debug=yes each mail access is now logged with a reason. This can
be helpful when figuring out why something isn't in dovecot.index.cache.
  • Loading branch information
sirainen committed Aug 4, 2016
1 parent b5c1836 commit 8deeb07
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/lib-storage/index/index-mail.c
Expand Up @@ -1077,9 +1077,11 @@ void index_mail_stream_log_failure_for(struct index_mail *mail,
return;
}
mail_storage_set_critical(_mail->box->storage,
"read(%s) failed: %s (uid=%u, box=%s)",
"read(%s) failed: %s (uid=%u, box=%s, read reason=%s)",
i_stream_get_name(input), i_stream_get_error(input),
_mail->uid, mailbox_get_vname(_mail->box));
_mail->uid, mailbox_get_vname(_mail->box),
mail->mail.get_stream_reason == NULL ? "" :
mail->mail.get_stream_reason);
}

static int index_mail_parse_body(struct index_mail *mail,
Expand Down Expand Up @@ -1142,6 +1144,14 @@ int index_mail_init_stream(struct index_mail *mail,
bool has_nuls;
int ret;

if (_mail->box->storage->user->mail_debug &&
mail->mail.get_stream_reason != NULL &&
mail->mail.get_stream_reason[0] != '\0') {
i_debug("Mailbox %s: Opened mail UID=%u because: %s",
_mail->box->vname, _mail->uid,
mail->mail.get_stream_reason);
}

if (!data->initialized_wrapper_stream &&
_mail->transaction->stats_track) {
input = i_stream_create_mail(_mail, data->stream,
Expand Down
2 changes: 2 additions & 0 deletions src/lib-storage/mail-storage-private.h
Expand Up @@ -504,6 +504,8 @@ struct mail_private {

pool_t pool, data_pool;
ARRAY(union mail_module_context *) module_contexts;

const char *get_stream_reason;
};

struct mailbox_list_context {
Expand Down
11 changes: 11 additions & 0 deletions src/lib-storage/mail-storage.h
Expand Up @@ -857,10 +857,21 @@ int mail_get_header_stream(struct mail *mail,
int mail_get_stream(struct mail *mail, struct message_size *hdr_size,
struct message_size *body_size, struct istream **stream_r)
ATTR_NULL(2, 3);
/* Same as mail_get_stream(), but specify a reason why the mail is being read.
This can be useful for debugging purposes. */
int mail_get_stream_because(struct mail *mail, struct message_size *hdr_size,
struct message_size *body_size,
const char *reason, struct istream **stream_r)
ATTR_NULL(2, 3);
/* Similar to mail_get_stream(), but the stream may or may not contain the
message body. */
int mail_get_hdr_stream(struct mail *mail, struct message_size *hdr_size,
struct istream **stream_r) ATTR_NULL(2);
/* Same as mail_get_hdr_stream(), but specify a reason why the header is being
read. This can be useful for debugging purposes. */
int mail_get_hdr_stream_because(struct mail *mail,
struct message_size *hdr_size,
const char *reason, struct istream **stream_r);
/* Returns the message part's body decoded to 8bit binary. If the
Content-Transfer-Encoding isn't supported, returns -1 and sets error to
MAIL_ERROR_CONVERSION. If the part refers to a multipart, all of its
Expand Down
19 changes: 19 additions & 0 deletions src/lib-storage/mail.c
Expand Up @@ -246,6 +246,14 @@ void mail_set_aborted(struct mail *mail)

int mail_get_stream(struct mail *mail, struct message_size *hdr_size,
struct message_size *body_size, struct istream **stream_r)
{
return mail_get_stream_because(mail, hdr_size, body_size,
"mail stream", stream_r);
}

int mail_get_stream_because(struct mail *mail, struct message_size *hdr_size,
struct message_size *body_size,
const char *reason, struct istream **stream_r)
{
struct mail_private *p = (struct mail_private *)mail;
int ret;
Expand All @@ -255,13 +263,22 @@ int mail_get_stream(struct mail *mail, struct message_size *hdr_size,
return -1;
}
T_BEGIN {
p->get_stream_reason = reason;
ret = p->v.get_stream(mail, TRUE, hdr_size, body_size, stream_r);
p->get_stream_reason = "";
} T_END;
return ret;
}

int mail_get_hdr_stream(struct mail *mail, struct message_size *hdr_size,
struct istream **stream_r)
{
return mail_get_hdr_stream_because(mail, hdr_size, "header stream", stream_r);
}

int mail_get_hdr_stream_because(struct mail *mail,
struct message_size *hdr_size,
const char *reason, struct istream **stream_r)
{
struct mail_private *p = (struct mail_private *)mail;
int ret;
Expand All @@ -271,7 +288,9 @@ int mail_get_hdr_stream(struct mail *mail, struct message_size *hdr_size,
return -1;
}
T_BEGIN {
p->get_stream_reason = reason;
ret = p->v.get_stream(mail, FALSE, hdr_size, NULL, stream_r);
p->get_stream_reason = "";
} T_END;
return ret;
}
Expand Down

0 comments on commit 8deeb07

Please sign in to comment.