mbox-mail.c revision 8f017a40470ef2f4b530000d947a8bce44350a5e
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2003-2007 Dovecot authors, see the included COPYING file */
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainenstatic void mbox_prepare_resync(struct index_mail *mail)
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen (struct mbox_transaction_context *)mail->trans;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen struct mbox_mailbox *mbox = (struct mbox_mailbox *)mail->ibox;
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainen (struct mbox_transaction_context *)mail->trans;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen struct mbox_mailbox *mbox = (struct mbox_mailbox *)mail->ibox;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen if (mail->mail.mail.expunged || mbox->syncing)
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen /* refresh index file after mbox has been locked to
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen make sure we get only up-to-date mbox offsets. */
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen if (mail_index_refresh(mbox->ibox.index) < 0) {
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen } else if ((sync_flags & MBOX_SYNC_FORCE_SYNC) != 0) {
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen /* dirty offsets are broken and mbox is write-locked.
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen sync it to update offsets. */
4e8e7a93628b4ed60aaaa47c6f72c1433f21e81dTimo Sirainen ret = mbox_file_seek(mbox, mail->trans->trans_view,
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen /* success */
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen /* we'll need to re-sync it completely */
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen sync_flags |= MBOX_SYNC_UNDIRTY | MBOX_SYNC_FORCE_SYNC;
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen mail_storage_set_critical(&mbox->storage->storage,
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen "Losing sync for mail uid=%u in mbox file %s",
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainenstatic int mbox_mail_get_received_date(struct mail *_mail, time_t *date_r)
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen struct index_mail *mail = (struct index_mail *)_mail;
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen struct mbox_mailbox *mbox = (struct mbox_mailbox *)mail->ibox;
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen (void)index_mail_get_received_date(_mail, date_r);
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen istream_raw_mbox_get_received_time(mbox->mbox_stream);
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen /* it's broken and conflicts with our "not found"
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen return value. change it. */
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen index_mail_cache_add(mail, MAIL_CACHE_RECEIVED_DATE, &t, sizeof(t));
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainenstatic int mbox_mail_get_save_date(struct mail *_mail, time_t *date_r)
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen struct index_mail *mail = (struct index_mail *)_mail;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen (void)index_mail_get_save_date(_mail, date_r);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen /* no way to know this. save the current time into cache and use
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen that from now on. this works only as long as the index files
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen are permanent */
791fb70b3255a11a91ce0c2dc3ae1460d4cf8459Timo Sirainen index_mail_cache_add(mail, MAIL_CACHE_SAVE_DATE,
a18e2525cb076066784967d6c8118a01dd38ac6bTimo Sirainenmbox_mail_get_special(struct mail *_mail, enum mail_fetch_field field,
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen const char **value_r)
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen#define EMPTY_MD5_SUM "00000000000000000000000000000000"
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen struct index_mail *mail = (struct index_mail *)_mail;
202b4674243a4a4826c35ed4d089831985c47256Timo Sirainen struct mbox_mailbox *mbox = (struct mbox_mailbox *)mail->ibox;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen *value_r = istream_raw_mbox_get_sender(mbox->mbox_stream);
a01faca549a403b2eda938cea0b1fb76c3ff44b6Aki Tuomi if (index_mail_get_special(_mail, field, value_r) < 0)
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen if (**value_r != '\0' && strcmp(*value_r, EMPTY_MD5_SUM) != 0)
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen /* i guess in theory the EMPTY_MD5_SUM is valid and can happen,
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen but it's almost guaranteed that it means the MD5 sum is
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen missing. recalculate it. */
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen if (mbox_sync(mbox, MBOX_SYNC_FORCE_SYNC) < 0)
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen return index_mail_get_special(_mail, field, value_r);
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainenstatic int mbox_mail_get_physical_size(struct mail *_mail, uoff_t *size_r)
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen struct index_mail *mail = (struct index_mail *)_mail;
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen struct mbox_mailbox *mbox = (struct mbox_mailbox *)mail->ibox;
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen /* our header size varies, so don't do any caching */
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen hdr_offset = istream_raw_mbox_get_header_offset(stream);
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen body_offset = istream_raw_mbox_get_body_offset(stream);
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen mail_storage_set_critical(_mail->box->storage,
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen "Couldn't get mbox size");
f6497ac81e6de57870936d538acccb75ce408fc1Timo Sirainen body_size = istream_raw_mbox_get_body_size(stream, (uoff_t)-1);
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainen data->physical_size = (body_offset - hdr_offset) + body_size;
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainenstatic int mbox_mail_get_stream(struct mail *_mail,
f6ae9ae80a1fcf6c8f45ab759f0074caaa66c9c8Timo Sirainen struct index_mail *mail = (struct index_mail *)_mail;
736b1800b0409ba7443d33ecb8d0fb9f8b091660Timo Sirainen struct mbox_mailbox *mbox = (struct mbox_mailbox *)mail->ibox;
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainen offset = istream_raw_mbox_get_header_offset(raw_stream);
a18503d5dc0751a1f9785e48438a219d95c0b9c2Timo Sirainen raw_stream = i_stream_create_limit(raw_stream,