bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2003-2018 Dovecot authors, see the included COPYING file */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "lib.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "istream.h"
8363f50d7b5d605912e55c34f7f28e9f4ce01341Timo Sirainen#include "nfs-workarounds.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "index-mail.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "maildir-storage.h"
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen#include "maildir-filename.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "maildir-uidlist.h"
9334fbad0aabb2fed88f40b2205d0d6f80bdffa2Timo Sirainen#include "maildir-sync.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen#include <stdio.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <fcntl.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <unistd.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <sys/stat.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
f0a2d04321ba456e5c5ba821c0d1ed9e8e0e2e08Timo Sirainenstruct maildir_open_context {
f0a2d04321ba456e5c5ba821c0d1ed9e8e0e2e08Timo Sirainen int fd;
f0a2d04321ba456e5c5ba821c0d1ed9e8e0e2e08Timo Sirainen char *path;
f0a2d04321ba456e5c5ba821c0d1ed9e8e0e2e08Timo Sirainen};
f0a2d04321ba456e5c5ba821c0d1ed9e8e0e2e08Timo Sirainen
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainenstatic int
f0a2d04321ba456e5c5ba821c0d1ed9e8e0e2e08Timo Sirainendo_open(struct maildir_mailbox *mbox, const char *path,
f0a2d04321ba456e5c5ba821c0d1ed9e8e0e2e08Timo Sirainen struct maildir_open_context *ctx)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
8363f50d7b5d605912e55c34f7f28e9f4ce01341Timo Sirainen ctx->fd = nfs_safe_open(path, O_RDONLY);
f0a2d04321ba456e5c5ba821c0d1ed9e8e0e2e08Timo Sirainen if (ctx->fd != -1) {
f0a2d04321ba456e5c5ba821c0d1ed9e8e0e2e08Timo Sirainen ctx->path = i_strdup(path);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return 1;
f0a2d04321ba456e5c5ba821c0d1ed9e8e0e2e08Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (errno == ENOENT)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
98922c5675bbbfadc84d58768bef867fe82256c2Timo Sirainen if (errno == EACCES) {
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(&mbox->box, "%s",
98922c5675bbbfadc84d58768bef867fe82256c2Timo Sirainen mail_error_eacces_msg("open", path));
98922c5675bbbfadc84d58768bef867fe82256c2Timo Sirainen } else {
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(&mbox->box,
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi "open(%s) failed: %m", path);
98922c5675bbbfadc84d58768bef867fe82256c2Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
b5e6f6f27c1461f0f9f202615eeb738a645188c3Timo Sirainenstatic int
2cfe9983ce7a6280636ee12beccc2e865111967bTimo Sirainendo_stat(struct maildir_mailbox *mbox, const char *path, struct stat *st)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (stat(path, st) == 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return 1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (errno == ENOENT)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
98922c5675bbbfadc84d58768bef867fe82256c2Timo Sirainen if (errno == EACCES) {
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(&mbox->box, "%s",
98922c5675bbbfadc84d58768bef867fe82256c2Timo Sirainen mail_error_eacces_msg("stat", path));
98922c5675bbbfadc84d58768bef867fe82256c2Timo Sirainen } else {
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(&mbox->box, "stat(%s) failed: %m", path);
98922c5675bbbfadc84d58768bef867fe82256c2Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic struct istream *
b6a7e0a7899e7f5d60c23cdaa50e025e4c67d05fTimo Sirainenmaildir_open_mail(struct maildir_mailbox *mbox, struct mail *mail,
b6a7e0a7899e7f5d60c23cdaa50e025e4c67d05fTimo Sirainen bool *deleted_r)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
d42eb03b3a4e79a2da22a1be2de59b95660af2beTimo Sirainen struct istream *input;
b6a7e0a7899e7f5d60c23cdaa50e025e4c67d05fTimo Sirainen const char *path;
f0a2d04321ba456e5c5ba821c0d1ed9e8e0e2e08Timo Sirainen struct maildir_open_context ctx;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
b6a7e0a7899e7f5d60c23cdaa50e025e4c67d05fTimo Sirainen *deleted_r = FALSE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
f0a2d04321ba456e5c5ba821c0d1ed9e8e0e2e08Timo Sirainen ctx.fd = -1;
f0a2d04321ba456e5c5ba821c0d1ed9e8e0e2e08Timo Sirainen ctx.path = NULL;
f0a2d04321ba456e5c5ba821c0d1ed9e8e0e2e08Timo Sirainen
2d01cc1880cf2afd4fb1c8ad7fa6ce78e562e71eTimo Sirainen mail->transaction->stats.open_lookup_count++;
fc40a9a002458e372ff4b9f6f4e15239520c0bcdTimo Sirainen if (!mail->saving) {
f0a2d04321ba456e5c5ba821c0d1ed9e8e0e2e08Timo Sirainen if (maildir_file_do(mbox, mail->uid, do_open, &ctx) < 0)
b6a7e0a7899e7f5d60c23cdaa50e025e4c67d05fTimo Sirainen return NULL;
b6a7e0a7899e7f5d60c23cdaa50e025e4c67d05fTimo Sirainen } else {
b6a7e0a7899e7f5d60c23cdaa50e025e4c67d05fTimo Sirainen path = maildir_save_file_get_path(mail->transaction, mail->seq);
f0a2d04321ba456e5c5ba821c0d1ed9e8e0e2e08Timo Sirainen if (do_open(mbox, path, &ctx) <= 0)
b6a7e0a7899e7f5d60c23cdaa50e025e4c67d05fTimo Sirainen return NULL;
b6a7e0a7899e7f5d60c23cdaa50e025e4c67d05fTimo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
f0a2d04321ba456e5c5ba821c0d1ed9e8e0e2e08Timo Sirainen if (ctx.fd == -1) {
b6a7e0a7899e7f5d60c23cdaa50e025e4c67d05fTimo Sirainen *deleted_r = TRUE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return NULL;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
bace943c67e6cd14ce6c994f533d82a3caad5bf1Timo Sirainen input = i_stream_create_fd_autoclose(&ctx.fd, 0);
c60d1eda4df179d83d531647732d5e3e45064219Timo Sirainen if (input->stream_errno == EISDIR) {
c60d1eda4df179d83d531647732d5e3e45064219Timo Sirainen i_stream_destroy(&input);
f6f021c133f680cf3d559187524fd9abcbaae9b9Timo Sirainen if (maildir_lose_unexpected_dir(&mbox->storage->storage,
f6f021c133f680cf3d559187524fd9abcbaae9b9Timo Sirainen ctx.path) >= 0)
c60d1eda4df179d83d531647732d5e3e45064219Timo Sirainen *deleted_r = TRUE;
c60d1eda4df179d83d531647732d5e3e45064219Timo Sirainen } else {
c60d1eda4df179d83d531647732d5e3e45064219Timo Sirainen i_stream_set_name(input, ctx.path);
c60d1eda4df179d83d531647732d5e3e45064219Timo Sirainen index_mail_set_read_buffer_size(mail, input);
c60d1eda4df179d83d531647732d5e3e45064219Timo Sirainen }
f0a2d04321ba456e5c5ba821c0d1ed9e8e0e2e08Timo Sirainen i_free(ctx.path);
d42eb03b3a4e79a2da22a1be2de59b95660af2beTimo Sirainen return input;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
35283613d4c04ce18836e9fc431582c87b3710a0Timo Sirainenstatic int maildir_mail_stat(struct mail *mail, struct stat *st_r)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
77f8c49beac69a8f5d5d41239528474ee1b877eaJosef 'Jeff' Sipek struct maildir_mailbox *mbox = MAILDIR_MAILBOX(mail->box);
87490012895b4f371635ded00add04c9107dcfefJosef 'Jeff' Sipek struct index_mail *imail = INDEX_MAIL(mail);
b6a7e0a7899e7f5d60c23cdaa50e025e4c67d05fTimo Sirainen const char *path;
fe0d19245382de9d4710d6ed0f7977ff5be4b3b2Timo Sirainen int fd, ret;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
35283613d4c04ce18836e9fc431582c87b3710a0Timo Sirainen if (mail->lookup_abort == MAIL_LOOKUP_ABORT_NOT_IN_CACHE) {
35283613d4c04ce18836e9fc431582c87b3710a0Timo Sirainen mail_set_aborted(mail);
35283613d4c04ce18836e9fc431582c87b3710a0Timo Sirainen return -1;
35283613d4c04ce18836e9fc431582c87b3710a0Timo Sirainen }
7204b8112e005ff81dcf628f7880ef1feed1effeTimo Sirainen mail->mail_metadata_accessed = TRUE;
ebe6df72f1309135f02b6a4d2aef1e81a073f91cTimo Sirainen
bc6ef0b01b99c43ee46aa796420516e89a744c30Timo Sirainen if (imail->data.access_part != 0 &&
a0aedab7cd06125e4d73638b1bd0c01c7caa2626Timo Sirainen imail->data.stream == NULL) {
992a13add4eea0810e4db0f042a595dddf85536aTimo Sirainen /* we're going to open the mail anyway */
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen struct istream *input;
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen (void)mail_get_stream(mail, NULL, NULL, &input);
992a13add4eea0810e4db0f042a595dddf85536aTimo Sirainen }
992a13add4eea0810e4db0f042a595dddf85536aTimo Sirainen
fe0d19245382de9d4710d6ed0f7977ff5be4b3b2Timo Sirainen if (imail->data.stream != NULL &&
fe0d19245382de9d4710d6ed0f7977ff5be4b3b2Timo Sirainen (fd = i_stream_get_fd(imail->data.stream)) != -1) {
2d01cc1880cf2afd4fb1c8ad7fa6ce78e562e71eTimo Sirainen mail->transaction->stats.fstat_lookup_count++;
fe0d19245382de9d4710d6ed0f7977ff5be4b3b2Timo Sirainen if (fstat(fd, st_r) < 0) {
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mail_set_critical(mail, "fstat(%s) failed: %m",
fe0d19245382de9d4710d6ed0f7977ff5be4b3b2Timo Sirainen i_stream_get_name(imail->data.stream));
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen return -1;
fe0d19245382de9d4710d6ed0f7977ff5be4b3b2Timo Sirainen }
3eb63515855f386449c22233d1f1baf1ddfe8a2dTimo Sirainen } else if (!mail->saving) {
2d01cc1880cf2afd4fb1c8ad7fa6ce78e562e71eTimo Sirainen mail->transaction->stats.stat_lookup_count++;
35283613d4c04ce18836e9fc431582c87b3710a0Timo Sirainen ret = maildir_file_do(mbox, mail->uid, do_stat, st_r);
e9d29ae46d435aee85514decfe6ee27399ebf794Timo Sirainen if (ret <= 0) {
e9d29ae46d435aee85514decfe6ee27399ebf794Timo Sirainen if (ret == 0)
e9d29ae46d435aee85514decfe6ee27399ebf794Timo Sirainen mail_set_expunged(mail);
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen return -1;
e9d29ae46d435aee85514decfe6ee27399ebf794Timo Sirainen }
b6a7e0a7899e7f5d60c23cdaa50e025e4c67d05fTimo Sirainen } else {
2d01cc1880cf2afd4fb1c8ad7fa6ce78e562e71eTimo Sirainen mail->transaction->stats.stat_lookup_count++;
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen path = maildir_save_file_get_path(mail->transaction, mail->seq);
35283613d4c04ce18836e9fc431582c87b3710a0Timo Sirainen if (stat(path, st_r) < 0) {
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mail_set_critical(mail, "stat(%s) failed: %m", path);
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen return -1;
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen return 0;
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen}
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainenstatic int maildir_mail_get_received_date(struct mail *_mail, time_t *date_r)
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen{
87490012895b4f371635ded00add04c9107dcfefJosef 'Jeff' Sipek struct index_mail *mail = INDEX_MAIL(_mail);
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen struct index_mail_data *data = &mail->data;
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen struct stat st;
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen if (index_mail_get_received_date(_mail, date_r) == 0)
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen return 0;
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen if (maildir_mail_stat(_mail, &st) < 0)
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen *date_r = data->received_date = st.st_mtime;
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen return 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainenstatic int maildir_mail_get_save_date(struct mail *_mail, time_t *date_r)
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen{
87490012895b4f371635ded00add04c9107dcfefJosef 'Jeff' Sipek struct index_mail *mail = INDEX_MAIL(_mail);
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen struct index_mail_data *data = &mail->data;
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen struct stat st;
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen if (index_mail_get_save_date(_mail, date_r) == 0)
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen return 0;
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen if (maildir_mail_stat(_mail, &st) < 0)
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen return -1;
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen *date_r = data->save_date = st.st_ctime;
51078c3413b7ed4811bc725acbb1289723361ba9Timo Sirainen return 0;
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen}
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen
7ede6554e451ec039a67beec7d6ee4aff61d386eTimo Sirainenstatic int
3ec2c1f31631bb5ff86f5fc93a563c33e5cae90dTimo Sirainenmaildir_mail_get_fname(struct maildir_mailbox *mbox, struct mail *mail,
3ec2c1f31631bb5ff86f5fc93a563c33e5cae90dTimo Sirainen const char **fname_r)
3ec2c1f31631bb5ff86f5fc93a563c33e5cae90dTimo Sirainen{
3ec2c1f31631bb5ff86f5fc93a563c33e5cae90dTimo Sirainen enum maildir_uidlist_rec_flag flags;
9334fbad0aabb2fed88f40b2205d0d6f80bdffa2Timo Sirainen struct mail_index_view *view;
9334fbad0aabb2fed88f40b2205d0d6f80bdffa2Timo Sirainen uint32_t seq;
9334fbad0aabb2fed88f40b2205d0d6f80bdffa2Timo Sirainen bool exists;
7ede6554e451ec039a67beec7d6ee4aff61d386eTimo Sirainen int ret;
3ec2c1f31631bb5ff86f5fc93a563c33e5cae90dTimo Sirainen
6013fbad6638795a00e6c2a2dd2cdbee19612494Timo Sirainen ret = maildir_sync_lookup(mbox, mail->uid, &flags, fname_r);
7ede6554e451ec039a67beec7d6ee4aff61d386eTimo Sirainen if (ret != 0)
7ede6554e451ec039a67beec7d6ee4aff61d386eTimo Sirainen return ret;
9334fbad0aabb2fed88f40b2205d0d6f80bdffa2Timo Sirainen
9334fbad0aabb2fed88f40b2205d0d6f80bdffa2Timo Sirainen /* file exists in index file, but not in dovecot-uidlist anymore. */
9334fbad0aabb2fed88f40b2205d0d6f80bdffa2Timo Sirainen mail_set_expunged(mail);
9334fbad0aabb2fed88f40b2205d0d6f80bdffa2Timo Sirainen
9334fbad0aabb2fed88f40b2205d0d6f80bdffa2Timo Sirainen /* one reason this could happen is if we delayed opening
9334fbad0aabb2fed88f40b2205d0d6f80bdffa2Timo Sirainen dovecot-uidlist and we're trying to open a mail that got recently
9334fbad0aabb2fed88f40b2205d0d6f80bdffa2Timo Sirainen expunged. Let's test this theory first: */
b66d803de86bfb411165b3465b0d9ef64ecfe2a1Timo Sirainen mail_index_refresh(mbox->box.index);
d22301419109ed4a38351715e6760011421dadecTimo Sirainen view = mail_index_view_open(mbox->box.index);
9334fbad0aabb2fed88f40b2205d0d6f80bdffa2Timo Sirainen exists = mail_index_lookup_seq(view, mail->uid, &seq);
9334fbad0aabb2fed88f40b2205d0d6f80bdffa2Timo Sirainen mail_index_view_close(&view);
9334fbad0aabb2fed88f40b2205d0d6f80bdffa2Timo Sirainen
9334fbad0aabb2fed88f40b2205d0d6f80bdffa2Timo Sirainen if (exists) {
9334fbad0aabb2fed88f40b2205d0d6f80bdffa2Timo Sirainen /* the message still exists in index. this means there's some
9334fbad0aabb2fed88f40b2205d0d6f80bdffa2Timo Sirainen kind of a desync, which doesn't get fixed if cur/ mtime is
9334fbad0aabb2fed88f40b2205d0d6f80bdffa2Timo Sirainen the same as in index. fix this by forcing a resync. */
9334fbad0aabb2fed88f40b2205d0d6f80bdffa2Timo Sirainen (void)maildir_storage_sync_force(mbox, mail->uid);
3ec2c1f31631bb5ff86f5fc93a563c33e5cae90dTimo Sirainen }
7ede6554e451ec039a67beec7d6ee4aff61d386eTimo Sirainen return 0;
3ec2c1f31631bb5ff86f5fc93a563c33e5cae90dTimo Sirainen}
3ec2c1f31631bb5ff86f5fc93a563c33e5cae90dTimo Sirainen
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainenstatic int maildir_get_pop3_state(struct index_mail *mail)
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen{
d22301419109ed4a38351715e6760011421dadecTimo Sirainen struct mailbox *box = mail->mail.mail.box;
d22301419109ed4a38351715e6760011421dadecTimo Sirainen struct index_mailbox_context *ibox = INDEX_STORAGE_CONTEXT(box);
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen const struct mail_cache_field *fields;
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen unsigned int i, count, psize_idx, vsize_idx;
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen enum mail_cache_decision_type dec, vsize_dec;
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen enum mail_fetch_field allowed_pop3_fields;
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen bool not_pop3_only = FALSE;
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen if (mail->pop3_state_set)
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen return mail->pop3_state;
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen /* if this mail itself has non-pop3 fields we know we're not
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen pop3-only */
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen allowed_pop3_fields = MAIL_FETCH_FLAGS | MAIL_FETCH_STREAM_HEADER |
5eb85ec9546bac4eea7d78de8997920ba3debd30Timo Sirainen MAIL_FETCH_STREAM_BODY | MAIL_FETCH_STORAGE_ID |
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen MAIL_FETCH_VIRTUAL_SIZE;
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen
ecd69c4e8371853667e01b0c16d436ef7f7393e2Timo Sirainen if (mail->data.wanted_headers != NULL ||
ecd69c4e8371853667e01b0c16d436ef7f7393e2Timo Sirainen (mail->data.wanted_fields & ~allowed_pop3_fields) != 0)
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen not_pop3_only = TRUE;
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen /* get vsize decisions */
d22301419109ed4a38351715e6760011421dadecTimo Sirainen psize_idx = ibox->cache_fields[MAIL_CACHE_PHYSICAL_FULL_SIZE].idx;
d22301419109ed4a38351715e6760011421dadecTimo Sirainen vsize_idx = ibox->cache_fields[MAIL_CACHE_VIRTUAL_FULL_SIZE].idx;
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen if (not_pop3_only) {
d22301419109ed4a38351715e6760011421dadecTimo Sirainen vsize_dec = mail_cache_field_get_decision(box->cache,
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen vsize_idx);
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen vsize_dec &= ~MAIL_CACHE_DECISION_FORCED;
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen } else {
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen /* also check if there are any non-[pv]size cached fields */
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen vsize_dec = MAIL_CACHE_DECISION_NO;
d22301419109ed4a38351715e6760011421dadecTimo Sirainen fields = mail_cache_register_get_list(box->cache,
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen pool_datastack_create(),
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen &count);
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen for (i = 0; i < count; i++) {
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen dec = fields[i].decision & ~MAIL_CACHE_DECISION_FORCED;
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen if (fields[i].idx == vsize_idx)
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen vsize_dec = dec;
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen else if (dec != MAIL_CACHE_DECISION_NO &&
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen fields[i].idx != psize_idx)
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen not_pop3_only = TRUE;
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen }
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen }
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen
fdb838ec8774e41004461b7f76ab409774af06c4Timo Sirainen if (index_mail_get_vsize_extension(&mail->mail.mail) != NULL) {
fdb838ec8774e41004461b7f76ab409774af06c4Timo Sirainen /* having a vsize extension in index is the same as having
fdb838ec8774e41004461b7f76ab409774af06c4Timo Sirainen vsize's caching decision YES */
fdb838ec8774e41004461b7f76ab409774af06c4Timo Sirainen vsize_dec = MAIL_CACHE_DECISION_YES;
fdb838ec8774e41004461b7f76ab409774af06c4Timo Sirainen }
fdb838ec8774e41004461b7f76ab409774af06c4Timo Sirainen
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen if (!not_pop3_only) {
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen /* either nothing is cached, or only vsize is cached. */
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen mail->pop3_state = 1;
236bedf76e31651ea9fca63fbdc25be673819526Timo Sirainen } else if (vsize_dec != MAIL_CACHE_DECISION_YES &&
d22301419109ed4a38351715e6760011421dadecTimo Sirainen (box->flags & MAILBOX_FLAG_POP3_SESSION) == 0) {
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen /* if virtual size isn't cached permanently,
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen POP3 isn't being used */
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen mail->pop3_state = -1;
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen } else {
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen /* possibly a mixed pop3/imap */
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen mail->pop3_state = 0;
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen }
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen mail->pop3_state_set = TRUE;
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen return mail->pop3_state;
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen}
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainenstatic int maildir_quick_size_lookup(struct index_mail *mail, bool vsize,
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen uoff_t *size_r)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen struct mail *_mail = &mail->mail.mail;
77f8c49beac69a8f5d5d41239528474ee1b877eaJosef 'Jeff' Sipek struct maildir_mailbox *mbox = MAILDIR_MAILBOX(_mail->box);
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen enum maildir_uidlist_rec_ext_key key;
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen const char *path, *fname, *value;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
3eb63515855f386449c22233d1f1baf1ddfe8a2dTimo Sirainen if (!_mail->saving) {
7ede6554e451ec039a67beec7d6ee4aff61d386eTimo Sirainen if (maildir_mail_get_fname(mbox, _mail, &fname) <= 0)
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen return -1;
b6a7e0a7899e7f5d60c23cdaa50e025e4c67d05fTimo Sirainen } else {
f567e9ad51a3b4f61580299f12b67e070fb16c5cTimo Sirainen if (maildir_save_file_get_size(_mail->transaction, _mail->seq,
f567e9ad51a3b4f61580299f12b67e070fb16c5cTimo Sirainen vsize, size_r) == 0)
f567e9ad51a3b4f61580299f12b67e070fb16c5cTimo Sirainen return 1;
f567e9ad51a3b4f61580299f12b67e070fb16c5cTimo Sirainen
b142deb9a831c89b1bb9129ada655f3e56b9d4ccTimo Sirainen path = maildir_save_file_get_path(_mail->transaction,
b142deb9a831c89b1bb9129ada655f3e56b9d4ccTimo Sirainen _mail->seq);
b142deb9a831c89b1bb9129ada655f3e56b9d4ccTimo Sirainen fname = strrchr(path, '/');
b142deb9a831c89b1bb9129ada655f3e56b9d4ccTimo Sirainen fname = fname != NULL ? fname + 1 : path;
b6a7e0a7899e7f5d60c23cdaa50e025e4c67d05fTimo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* size can be included in filename */
80bcc6caa317a52bddcafe74fede886247dbba5bTimo Sirainen if (vsize || !mbox->storage->set->maildir_broken_filename_sizes) {
80bcc6caa317a52bddcafe74fede886247dbba5bTimo Sirainen if (maildir_filename_get_size(fname,
80bcc6caa317a52bddcafe74fede886247dbba5bTimo Sirainen vsize ? MAILDIR_EXTRA_VIRTUAL_SIZE :
80bcc6caa317a52bddcafe74fede886247dbba5bTimo Sirainen MAILDIR_EXTRA_FILE_SIZE, size_r))
80bcc6caa317a52bddcafe74fede886247dbba5bTimo Sirainen return 1;
80bcc6caa317a52bddcafe74fede886247dbba5bTimo Sirainen }
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen /* size can be included in uidlist entry */
3eb63515855f386449c22233d1f1baf1ddfe8a2dTimo Sirainen if (!_mail->saving) {
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen key = vsize ? MAILDIR_UIDLIST_REC_EXT_VSIZE :
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen MAILDIR_UIDLIST_REC_EXT_PSIZE;
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen value = maildir_uidlist_lookup_ext(mbox->uidlist, _mail->uid,
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen key);
9261dbf0675204898c6557591c7aa376e23a52b2Timo Sirainen if (value != NULL && str_to_uoff(value, size_r) == 0)
9261dbf0675204898c6557591c7aa376e23a52b2Timo Sirainen return 1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen return 0;
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainenstatic void
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainenmaildir_handle_size_caching(struct index_mail *mail, bool quick_check,
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen bool vsize)
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen{
d22301419109ed4a38351715e6760011421dadecTimo Sirainen struct mailbox *box = mail->mail.mail.box;
77f8c49beac69a8f5d5d41239528474ee1b877eaJosef 'Jeff' Sipek struct maildir_mailbox *mbox = MAILDIR_MAILBOX(box);
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen enum mail_fetch_field field;
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen uoff_t size;
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen int pop3_state;
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen field = vsize ? MAIL_FETCH_VIRTUAL_SIZE : MAIL_FETCH_PHYSICAL_SIZE;
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen if ((mail->data.dont_cache_fetch_fields & field) != 0)
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen return;
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen
55b6e3105184ad6a2f987346380966f556300055Timo Sirainen if (quick_check && maildir_quick_size_lookup(mail, vsize, &size) > 0) {
7aeaf23f760d86aad525d831efcac9f860a55a39Timo Sirainen /* already in filename / uidlist. don't add it anywhere,
cf49fc07f541c0f74578ac6c3b334ddade143aa1Timo Sirainen including to the uidlist if it's already in filename.
cf49fc07f541c0f74578ac6c3b334ddade143aa1Timo Sirainen do some extra checks here to catch potential cache bugs. */
cf49fc07f541c0f74578ac6c3b334ddade143aa1Timo Sirainen if (vsize && mail->data.virtual_size != size) {
7522446d6514e5593c9d4d7e4beacd328301cb23Aki Tuomi mail_set_mail_cache_corrupted(&mail->mail.mail,
7522446d6514e5593c9d4d7e4beacd328301cb23Aki Tuomi "Corrupted virtual size: "
cf49fc07f541c0f74578ac6c3b334ddade143aa1Timo Sirainen "%"PRIuUOFF_T" != %"PRIuUOFF_T,
cf49fc07f541c0f74578ac6c3b334ddade143aa1Timo Sirainen mail->data.virtual_size, size);
2ff23d6fb7e2ff85aa23b7f4769aeac1d0316a1bTimo Sirainen mail->data.virtual_size = size;
cf49fc07f541c0f74578ac6c3b334ddade143aa1Timo Sirainen } else if (!vsize && mail->data.physical_size != size) {
7522446d6514e5593c9d4d7e4beacd328301cb23Aki Tuomi mail_set_mail_cache_corrupted(&mail->mail.mail,
7522446d6514e5593c9d4d7e4beacd328301cb23Aki Tuomi "Corrupted physical size: "
cf49fc07f541c0f74578ac6c3b334ddade143aa1Timo Sirainen "%"PRIuUOFF_T" != %"PRIuUOFF_T,
cf49fc07f541c0f74578ac6c3b334ddade143aa1Timo Sirainen mail->data.physical_size, size);
2ff23d6fb7e2ff85aa23b7f4769aeac1d0316a1bTimo Sirainen mail->data.physical_size = size;
cf49fc07f541c0f74578ac6c3b334ddade143aa1Timo Sirainen }
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen mail->data.dont_cache_fetch_fields |= field;
7aeaf23f760d86aad525d831efcac9f860a55a39Timo Sirainen return;
7aeaf23f760d86aad525d831efcac9f860a55a39Timo Sirainen }
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen /* 1 = pop3-only, 0 = mixed, -1 = no pop3 */
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen pop3_state = maildir_get_pop3_state(mail);
18065635d4e79dd96eb3b3215718abd12f6a6808Timo Sirainen if (pop3_state >= 0 && mail->mail.mail.uid != 0) {
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen /* if size is wanted permanently, store it to uidlist
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen so that in case cache file gets lost we can get it quickly */
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen mail->data.dont_cache_fetch_fields |= field;
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen size = vsize ? mail->data.virtual_size :
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen mail->data.physical_size;
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen maildir_uidlist_set_ext(mbox->uidlist, mail->mail.mail.uid,
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen vsize ? MAILDIR_UIDLIST_REC_EXT_VSIZE :
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen MAILDIR_UIDLIST_REC_EXT_PSIZE,
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen dec2str(size));
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen }
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen}
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainenstatic int maildir_mail_get_virtual_size(struct mail *_mail, uoff_t *size_r)
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen{
77f8c49beac69a8f5d5d41239528474ee1b877eaJosef 'Jeff' Sipek struct maildir_mailbox *mbox = MAILDIR_MAILBOX(_mail->box);
87490012895b4f371635ded00add04c9107dcfefJosef 'Jeff' Sipek struct index_mail *mail = INDEX_MAIL(_mail);
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen struct index_mail_data *data = &mail->data;
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen struct message_size hdr_size, body_size;
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen struct istream *input;
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen uoff_t old_offset;
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen
a0aedab7cd06125e4d73638b1bd0c01c7caa2626Timo Sirainen if (maildir_uidlist_is_read(mbox->uidlist) ||
a0aedab7cd06125e4d73638b1bd0c01c7caa2626Timo Sirainen (_mail->box->flags & MAILBOX_FLAG_POP3_SESSION) != 0) {
a0aedab7cd06125e4d73638b1bd0c01c7caa2626Timo Sirainen /* try to get the size from uidlist. this is especially useful
a0aedab7cd06125e4d73638b1bd0c01c7caa2626Timo Sirainen with pop3 to avoid unnecessarily opening the cache file. */
a0aedab7cd06125e4d73638b1bd0c01c7caa2626Timo Sirainen if (maildir_quick_size_lookup(mail, TRUE,
a0aedab7cd06125e4d73638b1bd0c01c7caa2626Timo Sirainen &data->virtual_size) < 0)
a0aedab7cd06125e4d73638b1bd0c01c7caa2626Timo Sirainen return -1;
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen }
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen
a0aedab7cd06125e4d73638b1bd0c01c7caa2626Timo Sirainen if (data->virtual_size == (uoff_t)-1) {
a0aedab7cd06125e4d73638b1bd0c01c7caa2626Timo Sirainen if (index_mail_get_cached_virtual_size(mail, size_r)) {
a0aedab7cd06125e4d73638b1bd0c01c7caa2626Timo Sirainen i_assert(mail->data.virtual_size != (uoff_t)-1);
a0aedab7cd06125e4d73638b1bd0c01c7caa2626Timo Sirainen maildir_handle_size_caching(mail, TRUE, TRUE);
a0aedab7cd06125e4d73638b1bd0c01c7caa2626Timo Sirainen return 0;
a0aedab7cd06125e4d73638b1bd0c01c7caa2626Timo Sirainen }
6d50c4d875bb05f9076e9e0ecbacb8beb2e9ae42Timo Sirainen if (maildir_quick_size_lookup(mail, TRUE,
6d50c4d875bb05f9076e9e0ecbacb8beb2e9ae42Timo Sirainen &data->virtual_size) < 0)
a0aedab7cd06125e4d73638b1bd0c01c7caa2626Timo Sirainen return -1;
a0aedab7cd06125e4d73638b1bd0c01c7caa2626Timo Sirainen }
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen if (data->virtual_size != (uoff_t)-1) {
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen data->dont_cache_fetch_fields |= MAIL_FETCH_VIRTUAL_SIZE;
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen *size_r = data->virtual_size;
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen return 0;
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen }
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen /* fallback to reading the file */
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen old_offset = data->stream == NULL ? 0 : data->stream->v_offset;
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen if (mail_get_stream(_mail, &hdr_size, &body_size, &input) < 0)
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen return -1;
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen i_stream_seek(data->stream, old_offset);
ae1b268ffff743ad9927c304a1344c5cbd7f909dTimo Sirainen
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen maildir_handle_size_caching(mail, FALSE, TRUE);
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen *size_r = data->virtual_size;
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen return 0;
d67fde1a8ebc1d85704c5986d8f93aae97eccef3Timo Sirainen}
d67fde1a8ebc1d85704c5986d8f93aae97eccef3Timo Sirainen
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainenstatic int maildir_mail_get_physical_size(struct mail *_mail, uoff_t *size_r)
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen{
87490012895b4f371635ded00add04c9107dcfefJosef 'Jeff' Sipek struct index_mail *mail = INDEX_MAIL(_mail);
77f8c49beac69a8f5d5d41239528474ee1b877eaJosef 'Jeff' Sipek struct maildir_mailbox *mbox = MAILDIR_MAILBOX(_mail->box);
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen struct index_mail_data *data = &mail->data;
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen struct stat st;
7e6522921c81999ec389dab54b338a6fb0f193aaTimo Sirainen struct message_size hdr_size, body_size;
7e6522921c81999ec389dab54b338a6fb0f193aaTimo Sirainen struct istream *input;
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen const char *path;
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen int ret;
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen
6d50c4d875bb05f9076e9e0ecbacb8beb2e9ae42Timo Sirainen if (maildir_uidlist_is_read(mbox->uidlist) ||
6d50c4d875bb05f9076e9e0ecbacb8beb2e9ae42Timo Sirainen (_mail->box->flags & MAILBOX_FLAG_POP3_SESSION) != 0) {
6d50c4d875bb05f9076e9e0ecbacb8beb2e9ae42Timo Sirainen /* try to get the size from uidlist (see virtual size above) */
6d50c4d875bb05f9076e9e0ecbacb8beb2e9ae42Timo Sirainen if (maildir_quick_size_lookup(mail, FALSE,
6d50c4d875bb05f9076e9e0ecbacb8beb2e9ae42Timo Sirainen &data->physical_size) < 0)
6d50c4d875bb05f9076e9e0ecbacb8beb2e9ae42Timo Sirainen return -1;
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen }
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen
6d50c4d875bb05f9076e9e0ecbacb8beb2e9ae42Timo Sirainen if (data->physical_size == (uoff_t)-1) {
6d50c4d875bb05f9076e9e0ecbacb8beb2e9ae42Timo Sirainen if (index_mail_get_physical_size(_mail, size_r) == 0) {
6d50c4d875bb05f9076e9e0ecbacb8beb2e9ae42Timo Sirainen i_assert(mail->data.physical_size != (uoff_t)-1);
6d50c4d875bb05f9076e9e0ecbacb8beb2e9ae42Timo Sirainen maildir_handle_size_caching(mail, TRUE, FALSE);
6d50c4d875bb05f9076e9e0ecbacb8beb2e9ae42Timo Sirainen return 0;
6d50c4d875bb05f9076e9e0ecbacb8beb2e9ae42Timo Sirainen }
6d50c4d875bb05f9076e9e0ecbacb8beb2e9ae42Timo Sirainen if (maildir_quick_size_lookup(mail, FALSE,
6d50c4d875bb05f9076e9e0ecbacb8beb2e9ae42Timo Sirainen &data->physical_size) < 0)
6d50c4d875bb05f9076e9e0ecbacb8beb2e9ae42Timo Sirainen return -1;
6d50c4d875bb05f9076e9e0ecbacb8beb2e9ae42Timo Sirainen }
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen if (data->physical_size != (uoff_t)-1) {
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen data->dont_cache_fetch_fields |= MAIL_FETCH_PHYSICAL_SIZE;
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen *size_r = data->physical_size;
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen return 0;
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen }
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen
7e6522921c81999ec389dab54b338a6fb0f193aaTimo Sirainen if (mail->mail.v.istream_opened != NULL) {
7e6522921c81999ec389dab54b338a6fb0f193aaTimo Sirainen /* we can't use stat(), because this may be a mail that some
7e6522921c81999ec389dab54b338a6fb0f193aaTimo Sirainen plugin has changed (e.g. zlib). need to do it the slow
7e6522921c81999ec389dab54b338a6fb0f193aaTimo Sirainen way. */
7e6522921c81999ec389dab54b338a6fb0f193aaTimo Sirainen if (mail_get_stream(_mail, &hdr_size, &body_size, &input) < 0)
7e6522921c81999ec389dab54b338a6fb0f193aaTimo Sirainen return -1;
7e6522921c81999ec389dab54b338a6fb0f193aaTimo Sirainen st.st_size = hdr_size.physical_size + body_size.physical_size;
7e6522921c81999ec389dab54b338a6fb0f193aaTimo Sirainen } else if (!_mail->saving) {
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen ret = maildir_file_do(mbox, _mail->uid, do_stat, &st);
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen if (ret <= 0) {
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen if (ret == 0)
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen mail_set_expunged(_mail);
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen return -1;
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen }
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen } else {
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen /* saved mail which hasn't been committed yet */
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen path = maildir_save_file_get_path(_mail->transaction,
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen _mail->seq);
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen if (stat(path, &st) < 0) {
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mail_set_critical(_mail, "stat(%s) failed: %m", path);
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen return -1;
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen }
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen }
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen data->physical_size = st.st_size;
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen maildir_handle_size_caching(mail, FALSE, FALSE);
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen *size_r = st.st_size;
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen return 0;
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen}
97eb53ade9057e6966dbb77289ad0204c7e1657bTimo Sirainen
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainenstatic int
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainenmaildir_mail_get_special(struct mail *_mail, enum mail_fetch_field field,
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen const char **value_r)
366d6311c9d5bac6613e3cd64619eb878adce9ecTimo Sirainen{
87490012895b4f371635ded00add04c9107dcfefJosef 'Jeff' Sipek struct index_mail *mail = INDEX_MAIL(_mail);
77f8c49beac69a8f5d5d41239528474ee1b877eaJosef 'Jeff' Sipek struct maildir_mailbox *mbox = MAILDIR_MAILBOX(_mail->box);
e1eeb3be29afde2830c2b7ed594c6f1fef2f69dcTimo Sirainen const char *path, *fname = NULL, *end, *guid, *uidl, *order;
03de962febfc2ac572f9e4029463c16d29c1ed55Timo Sirainen struct stat st;
366d6311c9d5bac6613e3cd64619eb878adce9ecTimo Sirainen
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen switch (field) {
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen case MAIL_FETCH_GUID:
e4e7475f646d66a257d682738fbff1f206ce4924Timo Sirainen /* use GUID from uidlist if it exists */
24cd47a2c8f7507e555459b7e841de771ba3c318Timo Sirainen i_assert(!_mail->saving);
24cd47a2c8f7507e555459b7e841de771ba3c318Timo Sirainen
46c903eac3abcc7d32780da76ee6a79c26d185b6Timo Sirainen if (mail->data.guid != NULL) {
46c903eac3abcc7d32780da76ee6a79c26d185b6Timo Sirainen *value_r = mail->data.guid;
46c903eac3abcc7d32780da76ee6a79c26d185b6Timo Sirainen return 0;
46c903eac3abcc7d32780da76ee6a79c26d185b6Timo Sirainen }
46c903eac3abcc7d32780da76ee6a79c26d185b6Timo Sirainen
24cd47a2c8f7507e555459b7e841de771ba3c318Timo Sirainen /* first make sure that we have a refreshed uidlist */
24cd47a2c8f7507e555459b7e841de771ba3c318Timo Sirainen if (maildir_mail_get_fname(mbox, _mail, &fname) <= 0)
24cd47a2c8f7507e555459b7e841de771ba3c318Timo Sirainen return -1;
24cd47a2c8f7507e555459b7e841de771ba3c318Timo Sirainen
e4e7475f646d66a257d682738fbff1f206ce4924Timo Sirainen guid = maildir_uidlist_lookup_ext(mbox->uidlist, _mail->uid,
e4e7475f646d66a257d682738fbff1f206ce4924Timo Sirainen MAILDIR_UIDLIST_REC_EXT_GUID);
e4e7475f646d66a257d682738fbff1f206ce4924Timo Sirainen if (guid != NULL) {
ed34a210eff7707787ee154e5581528b8f4b2daaTimo Sirainen if (*guid != '\0') {
46c903eac3abcc7d32780da76ee6a79c26d185b6Timo Sirainen *value_r = mail->data.guid =
5f44975ec6c5755dd74bcd4c47a123a7242ecab3Timo Sirainen p_strdup(mail->mail.data_pool, guid);
ed34a210eff7707787ee154e5581528b8f4b2daaTimo Sirainen return 0;
ed34a210eff7707787ee154e5581528b8f4b2daaTimo Sirainen }
ed34a210eff7707787ee154e5581528b8f4b2daaTimo Sirainen
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mail_set_critical(_mail,
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi "Maildir: Corrupted dovecot-uidlist: "
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi "UID had empty GUID, clearing it");
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen maildir_uidlist_unset_ext(mbox->uidlist, _mail->uid,
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen MAILDIR_UIDLIST_REC_EXT_GUID);
e4e7475f646d66a257d682738fbff1f206ce4924Timo Sirainen }
e4e7475f646d66a257d682738fbff1f206ce4924Timo Sirainen
e4e7475f646d66a257d682738fbff1f206ce4924Timo Sirainen /* default to base filename: */
5eb85ec9546bac4eea7d78de8997920ba3debd30Timo Sirainen if (maildir_mail_get_special(_mail, MAIL_FETCH_STORAGE_ID,
46c903eac3abcc7d32780da76ee6a79c26d185b6Timo Sirainen value_r) < 0)
46c903eac3abcc7d32780da76ee6a79c26d185b6Timo Sirainen return -1;
46c903eac3abcc7d32780da76ee6a79c26d185b6Timo Sirainen mail->data.guid = mail->data.filename;
46c903eac3abcc7d32780da76ee6a79c26d185b6Timo Sirainen return 0;
5eb85ec9546bac4eea7d78de8997920ba3debd30Timo Sirainen case MAIL_FETCH_STORAGE_ID:
46c903eac3abcc7d32780da76ee6a79c26d185b6Timo Sirainen if (mail->data.filename != NULL) {
46c903eac3abcc7d32780da76ee6a79c26d185b6Timo Sirainen *value_r = mail->data.filename;
129cc29ae6d8ad64d6d2b72f18da18fa134d0f3eTimo Sirainen return 0;
129cc29ae6d8ad64d6d2b72f18da18fa134d0f3eTimo Sirainen }
24cd47a2c8f7507e555459b7e841de771ba3c318Timo Sirainen if (fname != NULL) {
24cd47a2c8f7507e555459b7e841de771ba3c318Timo Sirainen /* we came here from MAIL_FETCH_GUID,
24cd47a2c8f7507e555459b7e841de771ba3c318Timo Sirainen avoid a second lookup */
24cd47a2c8f7507e555459b7e841de771ba3c318Timo Sirainen } else if (!_mail->saving) {
7ede6554e451ec039a67beec7d6ee4aff61d386eTimo Sirainen if (maildir_mail_get_fname(mbox, _mail, &fname) <= 0)
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen return -1;
b6a7e0a7899e7f5d60c23cdaa50e025e4c67d05fTimo Sirainen } else {
b142deb9a831c89b1bb9129ada655f3e56b9d4ccTimo Sirainen path = maildir_save_file_get_path(_mail->transaction,
b142deb9a831c89b1bb9129ada655f3e56b9d4ccTimo Sirainen _mail->seq);
b142deb9a831c89b1bb9129ada655f3e56b9d4ccTimo Sirainen fname = strrchr(path, '/');
b142deb9a831c89b1bb9129ada655f3e56b9d4ccTimo Sirainen fname = fname != NULL ? fname + 1 : path;
b6a7e0a7899e7f5d60c23cdaa50e025e4c67d05fTimo Sirainen }
8754bb7a1f24705ffa5434f9e10d57e0b3b88d6eTimo Sirainen end = strchr(fname, MAILDIR_INFO_SEP);
46c903eac3abcc7d32780da76ee6a79c26d185b6Timo Sirainen mail->data.filename = end == NULL ?
5f44975ec6c5755dd74bcd4c47a123a7242ecab3Timo Sirainen p_strdup(mail->mail.data_pool, fname) :
5f44975ec6c5755dd74bcd4c47a123a7242ecab3Timo Sirainen p_strdup_until(mail->mail.data_pool, fname, end);
46c903eac3abcc7d32780da76ee6a79c26d185b6Timo Sirainen *value_r = mail->data.filename;
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen return 0;
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen case MAIL_FETCH_UIDL_BACKEND:
e76c494ad6535d3de314cc0d3ac7a44b06e53c4bTimo Sirainen uidl = maildir_uidlist_lookup_ext(mbox->uidlist, _mail->uid,
e76c494ad6535d3de314cc0d3ac7a44b06e53c4bTimo Sirainen MAILDIR_UIDLIST_REC_EXT_POP3_UIDL);
89fb98e9eb7e95255a579c8e9d865383c2334a74Timo Sirainen if (uidl == NULL) {
89fb98e9eb7e95255a579c8e9d865383c2334a74Timo Sirainen /* use the default */
89fb98e9eb7e95255a579c8e9d865383c2334a74Timo Sirainen *value_r = "";
89fb98e9eb7e95255a579c8e9d865383c2334a74Timo Sirainen } else if (*uidl == '\0') {
89fb98e9eb7e95255a579c8e9d865383c2334a74Timo Sirainen /* special optimization case: use the base file name */
89fb98e9eb7e95255a579c8e9d865383c2334a74Timo Sirainen return maildir_mail_get_special(_mail,
5eb85ec9546bac4eea7d78de8997920ba3debd30Timo Sirainen MAIL_FETCH_STORAGE_ID, value_r);
89fb98e9eb7e95255a579c8e9d865383c2334a74Timo Sirainen } else {
5f44975ec6c5755dd74bcd4c47a123a7242ecab3Timo Sirainen *value_r = p_strdup(mail->mail.data_pool, uidl);
89fb98e9eb7e95255a579c8e9d865383c2334a74Timo Sirainen }
0d73d91d1a1809f173d433023ccf97e6ec5ba629Timo Sirainen return 0;
e1eeb3be29afde2830c2b7ed594c6f1fef2f69dcTimo Sirainen case MAIL_FETCH_POP3_ORDER:
e1eeb3be29afde2830c2b7ed594c6f1fef2f69dcTimo Sirainen order = maildir_uidlist_lookup_ext(mbox->uidlist, _mail->uid,
e1eeb3be29afde2830c2b7ed594c6f1fef2f69dcTimo Sirainen MAILDIR_UIDLIST_REC_EXT_POP3_ORDER);
e1eeb3be29afde2830c2b7ed594c6f1fef2f69dcTimo Sirainen if (order == NULL) {
e1eeb3be29afde2830c2b7ed594c6f1fef2f69dcTimo Sirainen *value_r = "";
e1eeb3be29afde2830c2b7ed594c6f1fef2f69dcTimo Sirainen } else {
5f44975ec6c5755dd74bcd4c47a123a7242ecab3Timo Sirainen *value_r = p_strdup(mail->mail.data_pool, order);
e1eeb3be29afde2830c2b7ed594c6f1fef2f69dcTimo Sirainen }
e1eeb3be29afde2830c2b7ed594c6f1fef2f69dcTimo Sirainen return 0;
03de962febfc2ac572f9e4029463c16d29c1ed55Timo Sirainen case MAIL_FETCH_REFCOUNT:
03de962febfc2ac572f9e4029463c16d29c1ed55Timo Sirainen if (maildir_mail_stat(_mail, &st) < 0)
03de962febfc2ac572f9e4029463c16d29c1ed55Timo Sirainen return -1;
03de962febfc2ac572f9e4029463c16d29c1ed55Timo Sirainen *value_r = p_strdup_printf(mail->mail.data_pool, "%lu",
03de962febfc2ac572f9e4029463c16d29c1ed55Timo Sirainen (unsigned long)st.st_nlink);
03de962febfc2ac572f9e4029463c16d29c1ed55Timo Sirainen return 0;
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen default:
b19a1420da0618a10edf67c2cfd13c8c8633057aTimo Sirainen return index_mail_get_special(_mail, field, value_r);
366d6311c9d5bac6613e3cd64619eb878adce9ecTimo Sirainen }
366d6311c9d5bac6613e3cd64619eb878adce9ecTimo Sirainen}
366d6311c9d5bac6613e3cd64619eb878adce9ecTimo Sirainen
6df0ab0c1ab91f06b6418cb30eff44405a1b8f02Timo Sirainenstatic int
6df0ab0c1ab91f06b6418cb30eff44405a1b8f02Timo Sirainenmaildir_mail_get_stream(struct mail *_mail, bool get_body ATTR_UNUSED,
6df0ab0c1ab91f06b6418cb30eff44405a1b8f02Timo Sirainen struct message_size *hdr_size,
6df0ab0c1ab91f06b6418cb30eff44405a1b8f02Timo Sirainen struct message_size *body_size,
6df0ab0c1ab91f06b6418cb30eff44405a1b8f02Timo Sirainen struct istream **stream_r)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
87490012895b4f371635ded00add04c9107dcfefJosef 'Jeff' Sipek struct index_mail *mail = INDEX_MAIL(_mail);
77f8c49beac69a8f5d5d41239528474ee1b877eaJosef 'Jeff' Sipek struct maildir_mailbox *mbox = MAILDIR_MAILBOX(_mail->box);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct index_mail_data *data = &mail->data;
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen bool deleted;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (data->stream == NULL) {
b6a7e0a7899e7f5d60c23cdaa50e025e4c67d05fTimo Sirainen data->stream = maildir_open_mail(mbox, _mail, &deleted);
992a13add4eea0810e4db0f042a595dddf85536aTimo Sirainen if (data->stream == NULL) {
910fa4e4204a73d3d24c03f3059dd24e727ca057Timo Sirainen if (deleted)
910fa4e4204a73d3d24c03f3059dd24e727ca057Timo Sirainen mail_set_expunged(_mail);
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen return -1;
992a13add4eea0810e4db0f042a595dddf85536aTimo Sirainen }
6564208826b0f46a00f010d1b5711d85944c3c88Timo Sirainen if (mail->mail.v.istream_opened != NULL) {
6564208826b0f46a00f010d1b5711d85944c3c88Timo Sirainen if (mail->mail.v.istream_opened(_mail,
a4d796994ba01c207f1d3e373f58f06c6779af53Timo Sirainen &data->stream) < 0) {
a4d796994ba01c207f1d3e373f58f06c6779af53Timo Sirainen i_stream_unref(&data->stream);
6564208826b0f46a00f010d1b5711d85944c3c88Timo Sirainen return -1;
a4d796994ba01c207f1d3e373f58f06c6779af53Timo Sirainen }
6564208826b0f46a00f010d1b5711d85944c3c88Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen return index_mail_init_stream(mail, hdr_size, body_size, stream_r);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
225e82df5dd1e765f4e52b80c954558f00e5a7dfTimo Sirainenstatic void maildir_update_pop3_uidl(struct mail *_mail, const char *uidl)
225e82df5dd1e765f4e52b80c954558f00e5a7dfTimo Sirainen{
77f8c49beac69a8f5d5d41239528474ee1b877eaJosef 'Jeff' Sipek struct maildir_mailbox *mbox = MAILDIR_MAILBOX(_mail->box);
225e82df5dd1e765f4e52b80c954558f00e5a7dfTimo Sirainen const char *fname;
225e82df5dd1e765f4e52b80c954558f00e5a7dfTimo Sirainen
5eb85ec9546bac4eea7d78de8997920ba3debd30Timo Sirainen if (maildir_mail_get_special(_mail, MAIL_FETCH_STORAGE_ID,
225e82df5dd1e765f4e52b80c954558f00e5a7dfTimo Sirainen &fname) == 0 &&
225e82df5dd1e765f4e52b80c954558f00e5a7dfTimo Sirainen strcmp(uidl, fname) == 0) {
225e82df5dd1e765f4e52b80c954558f00e5a7dfTimo Sirainen /* special case optimization: empty UIDL means the same
225e82df5dd1e765f4e52b80c954558f00e5a7dfTimo Sirainen as base filename */
225e82df5dd1e765f4e52b80c954558f00e5a7dfTimo Sirainen uidl = "";
225e82df5dd1e765f4e52b80c954558f00e5a7dfTimo Sirainen }
225e82df5dd1e765f4e52b80c954558f00e5a7dfTimo Sirainen
225e82df5dd1e765f4e52b80c954558f00e5a7dfTimo Sirainen maildir_uidlist_set_ext(mbox->uidlist, _mail->uid,
225e82df5dd1e765f4e52b80c954558f00e5a7dfTimo Sirainen MAILDIR_UIDLIST_REC_EXT_POP3_UIDL, uidl);
225e82df5dd1e765f4e52b80c954558f00e5a7dfTimo Sirainen}
225e82df5dd1e765f4e52b80c954558f00e5a7dfTimo Sirainen
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainenstatic void maildir_mail_remove_sizes_from_uidlist(struct mail *mail)
8c9e48cd6de80da0fa32b9c0dee003472c9a7c0dTimo Sirainen{
77f8c49beac69a8f5d5d41239528474ee1b877eaJosef 'Jeff' Sipek struct maildir_mailbox *mbox = MAILDIR_MAILBOX(mail->box);
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen if (maildir_uidlist_lookup_ext(mbox->uidlist, mail->uid,
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen MAILDIR_UIDLIST_REC_EXT_VSIZE) != NULL) {
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen maildir_uidlist_unset_ext(mbox->uidlist, mail->uid,
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen MAILDIR_UIDLIST_REC_EXT_VSIZE);
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen }
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen if (maildir_uidlist_lookup_ext(mbox->uidlist, mail->uid,
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen MAILDIR_UIDLIST_REC_EXT_PSIZE) != NULL) {
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen maildir_uidlist_unset_ext(mbox->uidlist, mail->uid,
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen MAILDIR_UIDLIST_REC_EXT_PSIZE);
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen }
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen}
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainenstruct maildir_size_fix_ctx {
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen uoff_t physical_size;
aac02871f096a42d4146dd7d104a7302a570b2b5Phil Carmody char wrong_key;
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen};
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainenstatic int
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainendo_fix_size(struct maildir_mailbox *mbox, const char *path,
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen struct maildir_size_fix_ctx *ctx)
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen{
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen const char *fname, *newpath, *extra, *info, *dir;
3ec3632e12638396944e854a8f7a2bc6c67b981dTimo Sirainen struct stat st;
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen fname = strrchr(path, '/');
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen i_assert(fname != NULL);
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen dir = t_strdup_until(path, fname++);
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen extra = strchr(fname, MAILDIR_EXTRA_SEP);
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen i_assert(extra != NULL);
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen info = strchr(fname, MAILDIR_INFO_SEP);
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen if (info == NULL) info = "";
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen if (ctx->physical_size == (uoff_t)-1) {
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen if (stat(path, &st) < 0) {
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen if (errno == ENOENT)
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen return 0;
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(&mbox->box,
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi "stat(%s) failed: %m", path);
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen return -1;
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen }
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen ctx->physical_size = st.st_size;
3ec3632e12638396944e854a8f7a2bc6c67b981dTimo Sirainen }
3ec3632e12638396944e854a8f7a2bc6c67b981dTimo Sirainen
3ec3632e12638396944e854a8f7a2bc6c67b981dTimo Sirainen newpath = t_strdup_printf("%s/%s,S=%"PRIuUOFF_T"%s", dir,
3ec3632e12638396944e854a8f7a2bc6c67b981dTimo Sirainen t_strdup_until(fname, extra),
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen ctx->physical_size, info);
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen if (rename(path, newpath) == 0) {
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(&mbox->box,
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen "Maildir filename has wrong %c value, "
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen "renamed the file from %s to %s",
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen ctx->wrong_key, path, newpath);
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen return 1;
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen }
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen if (errno == ENOENT)
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen return 0;
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(&mbox->box, "rename(%s, %s) failed: %m",
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi path, newpath);
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen return -1;
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen}
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainenstatic void
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainenmaildir_mail_remove_sizes_from_filename(struct mail *mail,
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen enum mail_fetch_field field)
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen{
77f8c49beac69a8f5d5d41239528474ee1b877eaJosef 'Jeff' Sipek struct maildir_mailbox *mbox = MAILDIR_MAILBOX(mail->box);
6d9ce72cef0963ac414353dc4dfb3a51f62fa542Timo Sirainen struct mail_private *pmail = (struct mail_private *)mail;
8c9e48cd6de80da0fa32b9c0dee003472c9a7c0dTimo Sirainen enum maildir_uidlist_rec_flag flags;
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen const char *fname;
8c9e48cd6de80da0fa32b9c0dee003472c9a7c0dTimo Sirainen uoff_t size;
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen struct maildir_size_fix_ctx ctx;
8c9e48cd6de80da0fa32b9c0dee003472c9a7c0dTimo Sirainen
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen if (mbox->storage->set->maildir_broken_filename_sizes) {
0fa25bd866479a9aac973e716ba04dd8be8c2814Timo Sirainen /* never try to fix sizes in maildir filenames */
0fa25bd866479a9aac973e716ba04dd8be8c2814Timo Sirainen return;
0fa25bd866479a9aac973e716ba04dd8be8c2814Timo Sirainen }
0fa25bd866479a9aac973e716ba04dd8be8c2814Timo Sirainen
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen if (maildir_sync_lookup(mbox, mail->uid, &flags, &fname) <= 0)
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen return;
b12b6da6f084212d42421a28ae329eae79751c42Timo Sirainen if (strchr(fname, MAILDIR_EXTRA_SEP) == NULL)
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen return;
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch i_zero(&ctx);
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen ctx.physical_size = (uoff_t)-1;
45799f8340fdcddfb077c1d44ad1d2014d7db31cTimo Sirainen if (field == MAIL_FETCH_VIRTUAL_SIZE &&
45799f8340fdcddfb077c1d44ad1d2014d7db31cTimo Sirainen maildir_filename_get_size(fname, MAILDIR_EXTRA_VIRTUAL_SIZE,
45799f8340fdcddfb077c1d44ad1d2014d7db31cTimo Sirainen &size)) {
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen ctx.wrong_key = 'W';
45799f8340fdcddfb077c1d44ad1d2014d7db31cTimo Sirainen } else if (field == MAIL_FETCH_PHYSICAL_SIZE &&
45799f8340fdcddfb077c1d44ad1d2014d7db31cTimo Sirainen maildir_filename_get_size(fname, MAILDIR_EXTRA_FILE_SIZE,
45799f8340fdcddfb077c1d44ad1d2014d7db31cTimo Sirainen &size)) {
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen ctx.wrong_key = 'S';
45799f8340fdcddfb077c1d44ad1d2014d7db31cTimo Sirainen } else {
45799f8340fdcddfb077c1d44ad1d2014d7db31cTimo Sirainen /* the broken size isn't in filename */
45799f8340fdcddfb077c1d44ad1d2014d7db31cTimo Sirainen return;
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen }
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen if (pmail->v.istream_opened != NULL) {
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen /* the mail could be e.g. compressed. get the physical size
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen the slow way by actually reading the mail. */
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen struct istream *input;
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen const struct stat *stp;
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen if (mail_get_stream(mail, NULL, NULL, &input) < 0)
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen return;
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen if (i_stream_stat(input, TRUE, &stp) < 0)
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen return;
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen ctx.physical_size = stp->st_size;
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen }
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen
9559586a78b7b21e0b74748c427e0a252242be40Timo Sirainen (void)maildir_file_do(mbox, mail->uid, do_fix_size, &ctx);
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen}
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen
0206dc57f2c04da69599dea5816235cfeb2b897aMartti Rannanjärvistatic void maildir_mail_set_cache_corrupted(struct mail *_mail,
0206dc57f2c04da69599dea5816235cfeb2b897aMartti Rannanjärvi enum mail_fetch_field field,
0206dc57f2c04da69599dea5816235cfeb2b897aMartti Rannanjärvi const char *reason)
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen{
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen if (field == MAIL_FETCH_PHYSICAL_SIZE ||
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen field == MAIL_FETCH_VIRTUAL_SIZE) {
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen maildir_mail_remove_sizes_from_uidlist(_mail);
de4e3a2e1e8f82b2d3226c090b71b518b43bf9cfTimo Sirainen maildir_mail_remove_sizes_from_filename(_mail, field);
8c9e48cd6de80da0fa32b9c0dee003472c9a7c0dTimo Sirainen }
0206dc57f2c04da69599dea5816235cfeb2b897aMartti Rannanjärvi index_mail_set_cache_corrupted(_mail, field, reason);
8c9e48cd6de80da0fa32b9c0dee003472c9a7c0dTimo Sirainen}
8c9e48cd6de80da0fa32b9c0dee003472c9a7c0dTimo Sirainen
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainenstruct mail_vfuncs maildir_mail_vfuncs = {
0d86aa0d47f7393c669c084b34c0537b193688adTimo Sirainen index_mail_close,
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen index_mail_free,
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen index_mail_set_seq,
a12399903f415a7e14c2816cffa2f7a09dcbb097Timo Sirainen index_mail_set_uid,
aa247243412a49f9bdebf7255e131dc6ece4ed46Timo Sirainen index_mail_set_uid_cache_updates,
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen index_mail_prefetch,
421d30619384e72a27e2a5d13ff6525aff4d17feTimo Sirainen index_mail_precache,
ecd69c4e8371853667e01b0c16d436ef7f7393e2Timo Sirainen index_mail_add_temp_wanted_fields,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
439980f88f421039dea8335e92d3fa82b3f470a1Timo Sirainen index_mail_get_flags,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen index_mail_get_keywords,
a757f31393b9d6fc7760a9dec8363404ab3ae576Timo Sirainen index_mail_get_keyword_indexes,
a2f250a332dfc1e6cd4ffd196c621eb9dbf7b8a1Timo Sirainen index_mail_get_modseq,
f46885a5b78b15a8d2419f6e5d13b643bd85e41fTimo Sirainen index_mail_get_pvt_modseq,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index_mail_get_parts,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index_mail_get_date,
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen maildir_mail_get_received_date,
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen maildir_mail_get_save_date,
d67fde1a8ebc1d85704c5986d8f93aae97eccef3Timo Sirainen maildir_mail_get_virtual_size,
d67fde1a8ebc1d85704c5986d8f93aae97eccef3Timo Sirainen maildir_mail_get_physical_size,
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen index_mail_get_first_header,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index_mail_get_headers,
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen index_mail_get_header_stream,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen maildir_mail_get_stream,
306b3f41b05da642d87e7ca7a1496efce9f5902fTimo Sirainen index_mail_get_binary_stream,
366d6311c9d5bac6613e3cd64619eb878adce9ecTimo Sirainen maildir_mail_get_special,
21aaa6affb9f134112b75b5db737309fc35ef1cfMartti Rannanjärvi index_mail_get_backend_mail,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index_mail_update_flags,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen index_mail_update_keywords,
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen index_mail_update_modseq,
f46885a5b78b15a8d2419f6e5d13b643bd85e41fTimo Sirainen index_mail_update_pvt_modseq,
225e82df5dd1e765f4e52b80c954558f00e5a7dfTimo Sirainen maildir_update_pop3_uidl,
838e367716bbd5e44b4a1691db9cbf72af53e9f0Timo Sirainen index_mail_expunge,
6564208826b0f46a00f010d1b5711d85944c3c88Timo Sirainen maildir_mail_set_cache_corrupted,
6de6ec228a41275ddda972d4a554699ea75cd06dTimo Sirainen index_mail_opened,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen};