From: Timo Sirainen Date: Mon, 29 Nov 2021 17:32:09 +0000 (+0100) Subject: lib-storage: Add and use mail_stream_access_start() X-Git-Tag: 2.3.18~56 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=988a575c35888d3985580e44924b8dee1c2e8e78;p=thirdparty%2Fdovecot%2Fcore.git lib-storage: Add and use mail_stream_access_start() This replaces setting mail_stream_accessed directly. --- diff --git a/src/lib-storage/index/dbox-multi/mdbox-mail.c b/src/lib-storage/index/dbox-multi/mdbox-mail.c index 22f46427bb..ed3fe064fe 100644 --- a/src/lib-storage/index/dbox-multi/mdbox-mail.c +++ b/src/lib-storage/index/dbox-multi/mdbox-mail.c @@ -99,11 +99,8 @@ int mdbox_mail_open(struct dbox_mail *mail, uoff_t *offset_r, uint32_t prev_file_id = 0, map_uid = 0; bool deleted; - if (_mail->lookup_abort != MAIL_LOOKUP_ABORT_NEVER) { - mail_set_aborted(_mail); + if (!mail_stream_access_start(_mail)) return -1; - } - _mail->mail_stream_accessed = TRUE; do { if (mail->open_file != NULL) { diff --git a/src/lib-storage/index/dbox-single/sdbox-mail.c b/src/lib-storage/index/dbox-single/sdbox-mail.c index 380fddf286..3b0352ca99 100644 --- a/src/lib-storage/index/dbox-single/sdbox-mail.c +++ b/src/lib-storage/index/dbox-single/sdbox-mail.c @@ -121,11 +121,8 @@ int sdbox_mail_open(struct dbox_mail *mail, uoff_t *offset_r, bool deleted; int ret; - if (_mail->lookup_abort != MAIL_LOOKUP_ABORT_NEVER) { - mail_set_aborted(_mail); + if (!mail_stream_access_start(_mail)) return -1; - } - _mail->mail_stream_accessed = TRUE; ret = sdbox_mail_file_set(mail); if (ret < 0) diff --git a/src/lib-storage/index/imapc/imapc-mail-fetch.c b/src/lib-storage/index/imapc/imapc-mail-fetch.c index 1aaa4f58e3..222237829a 100644 --- a/src/lib-storage/index/imapc/imapc-mail-fetch.c +++ b/src/lib-storage/index/imapc/imapc-mail-fetch.c @@ -213,11 +213,8 @@ imapc_mail_send_fetch(struct mail *_mail, enum mail_fetch_field fields, i_assert(headers == NULL || IMAPC_BOX_HAS_FEATURE(mbox, IMAPC_FEATURE_FETCH_HEADERS)); - if (_mail->lookup_abort != MAIL_LOOKUP_ABORT_NEVER) { - mail_set_aborted(_mail); + if (!mail_stream_access_start(_mail)) return -1; - } - _mail->mail_stream_accessed = TRUE; /* drop any fields that we may already be fetching currently */ fields &= ENUM_NEGATE(mail->fetching_fields); @@ -328,6 +325,9 @@ static void imapc_mail_cache_get(struct imapc_mail *mail, } mail->header_fetched = TRUE; mail->body_fetched = TRUE; + /* The stream was already accessed and now it's cached. + It still needs to be set accessed to avoid assert-crash. */ + mail->imail.mail.mail.mail_stream_accessed = TRUE; imapc_mail_init_stream(mail); } diff --git a/src/lib-storage/index/imapc/imapc-save.c b/src/lib-storage/index/imapc/imapc-save.c index 4d15b0cdcd..4971dd53cc 100644 --- a/src/lib-storage/index/imapc/imapc-save.c +++ b/src/lib-storage/index/imapc/imapc-save.c @@ -152,6 +152,9 @@ imapc_save_add_to_index(struct imapc_save_context *ctx, uint32_t uid) imail->data.stream = i_stream_create_fd_autoclose(&ctx->fd, 0); imapc_mail->header_fetched = TRUE; imapc_mail->body_fetched = TRUE; + /* The saved stream wasn't actually read, but it needs to be + set accessed to avoid assert-crash. */ + _mail->mail_stream_accessed = TRUE; imapc_mail_init_stream(imapc_mail); } diff --git a/src/lib-storage/index/index-mail.c b/src/lib-storage/index/index-mail.c index d62ff4a7cf..9902cd148c 100644 --- a/src/lib-storage/index/index-mail.c +++ b/src/lib-storage/index/index-mail.c @@ -1350,7 +1350,7 @@ int index_mail_init_stream(struct index_mail *mail, bool has_nuls, body_size_from_stream = FALSE; int ret; - _mail->mail_stream_accessed = TRUE; + i_assert(_mail->mail_stream_accessed); if (!data->initialized_wrapper_stream && _mail->transaction->stats_track) { diff --git a/src/lib-storage/index/maildir/maildir-mail.c b/src/lib-storage/index/maildir/maildir-mail.c index ed20f8656f..bdc8adf4fe 100644 --- a/src/lib-storage/index/maildir/maildir-mail.c +++ b/src/lib-storage/index/maildir/maildir-mail.c @@ -68,11 +68,8 @@ maildir_open_mail(struct maildir_mailbox *mbox, struct mail *mail, *deleted_r = FALSE; - if (mail->lookup_abort != MAIL_LOOKUP_ABORT_NEVER) { - mail_set_aborted(mail); + if (!mail_stream_access_start(mail)) return NULL; - } - mail->mail_stream_accessed = TRUE; ctx.fd = -1; ctx.path = NULL; diff --git a/src/lib-storage/index/mbox/mbox-mail.c b/src/lib-storage/index/mbox/mbox-mail.c index 53e1499309..09223b0181 100644 --- a/src/lib-storage/index/mbox/mbox-mail.c +++ b/src/lib-storage/index/mbox/mbox-mail.c @@ -41,11 +41,8 @@ static int mbox_mail_seek(struct index_mail *mail) if (_mail->expunged || mbox->syncing) return -1; - if (_mail->lookup_abort != MAIL_LOOKUP_ABORT_NEVER) { - mail_set_aborted(_mail); + if (!mail_stream_access_start(_mail)) return -1; - } - _mail->mail_stream_accessed = TRUE; if (mbox->mbox_stream != NULL && istream_raw_mbox_is_corrupted(mbox->mbox_stream)) { diff --git a/src/lib-storage/index/pop3c/pop3c-mail.c b/src/lib-storage/index/pop3c/pop3c-mail.c index 3b660745dc..27a842758d 100644 --- a/src/lib-storage/index/pop3c/pop3c-mail.c +++ b/src/lib-storage/index/pop3c/pop3c-mail.c @@ -146,8 +146,8 @@ static bool pop3c_mail_prefetch(struct mail *_mail) const char *cmd; if (pmail->imail.data.access_part != 0 && - pmail->imail.data.stream == NULL) { - _mail->mail_stream_accessed = TRUE; + pmail->imail.data.stream == NULL && + mail_stream_access_start(_mail)) { capa = pop3c_client_get_capabilities(mbox->client); pmail->prefetching_body = (capa & POP3C_CAPABILITY_TOP) == 0 || (pmail->imail.data.access_part & (READ_BODY | PARSE_BODY)) != 0; @@ -207,11 +207,8 @@ pop3c_mail_get_stream(struct mail *_mail, bool get_body, } if (mail->data.stream == NULL) { - if (_mail->lookup_abort != MAIL_LOOKUP_ABORT_NEVER) { - mail_set_aborted(_mail); + if (!mail_stream_access_start(_mail)) return -1; - } - _mail->mail_stream_accessed = TRUE; capa = pop3c_client_get_capabilities(mbox->client); if (get_body || (capa & POP3C_CAPABILITY_TOP) == 0) { cmd = t_strdup_printf("RETR %u\r\n", _mail->seq); diff --git a/src/lib-storage/index/raw/raw-mail.c b/src/lib-storage/index/raw/raw-mail.c index 2e9033ae31..b388023a23 100644 --- a/src/lib-storage/index/raw/raw-mail.c +++ b/src/lib-storage/index/raw/raw-mail.c @@ -85,11 +85,8 @@ raw_mail_get_stream(struct mail *_mail, bool get_body ATTR_UNUSED, struct index_mail *mail = INDEX_MAIL(_mail); if (mail->data.stream == NULL) { - if (_mail->lookup_abort != MAIL_LOOKUP_ABORT_NEVER) { - mail_set_aborted(_mail); + if (!mail_stream_access_start(_mail)) return -1; - } - _mail->mail_stream_accessed = TRUE; /* we can't just reference mbox->input, because index_mail_close() expects to be able to free the stream */ mail->data.stream = diff --git a/src/lib-storage/mail-storage-private.h b/src/lib-storage/mail-storage-private.h index 846e2b155b..32369b0c3d 100644 --- a/src/lib-storage/mail-storage-private.h +++ b/src/lib-storage/mail-storage-private.h @@ -827,6 +827,9 @@ bool mail_has_attachment_keywords(struct mail *mail); and 1 if attachment was found. */ int mail_set_attachment_keywords(struct mail *mail); +/* Attempt to start accessing the mail stream. Returns TRUE is ok, FALSE if + prevented by mail->lookup_abort. */ +bool mail_stream_access_start(struct mail *mail); /* Emit mail opened events */ void mail_opened_event(struct mail *mail); diff --git a/src/lib-storage/mail.c b/src/lib-storage/mail.c index dcc404d30a..4ae559cdb5 100644 --- a/src/lib-storage/mail.c +++ b/src/lib-storage/mail.c @@ -639,6 +639,16 @@ int mail_set_attachment_keywords(struct mail *mail) return ret; } +bool mail_stream_access_start(struct mail *mail) +{ + if (mail->lookup_abort != MAIL_LOOKUP_ABORT_NEVER) { + mail_set_aborted(mail); + return FALSE; + } + mail->mail_stream_accessed = TRUE; + return TRUE; +} + void mail_opened_event(struct mail *mail) { struct mail_private *pmail =