maildir-mail.c revision 7ede6554e451ec039a67beec7d6ee4aff61d386e
281N/A/* Copyright (c) 2003-2009 Dovecot authors, see the included COPYING file */
281N/A
281N/A#include "lib.h"
281N/A#include "istream.h"
281N/A#include "index-mail.h"
281N/A#include "maildir-storage.h"
281N/A#include "maildir-filename.h"
281N/A#include "maildir-uidlist.h"
281N/A#include "maildir-sync.h"
281N/A
281N/A#include <stdlib.h>
281N/A#include <fcntl.h>
281N/A#include <unistd.h>
281N/A#include <sys/stat.h>
281N/A
281N/Astatic int
281N/Ado_open(struct maildir_mailbox *mbox, const char *path, int *fd)
281N/A{
281N/A *fd = open(path, O_RDONLY);
281N/A if (*fd != -1)
281N/A return 1;
281N/A if (errno == ENOENT)
281N/A return 0;
281N/A
281N/A mail_storage_set_critical(&mbox->storage->storage,
281N/A "open(%s) failed: %m", path);
281N/A return -1;
289N/A}
289N/A
289N/Astatic int
281N/Ado_stat(struct maildir_mailbox *mbox, const char *path, struct stat *st)
281N/A{
281N/A if (stat(path, st) == 0)
281N/A return 1;
281N/A if (errno == ENOENT)
281N/A return 0;
281N/A
281N/A mail_storage_set_critical(&mbox->storage->storage,
281N/A "stat(%s) failed: %m", path);
281N/A return -1;
281N/A}
281N/A
281N/Astatic struct istream *
281N/Amaildir_open_mail(struct maildir_mailbox *mbox, struct mail *mail,
281N/A bool *deleted_r)
281N/A{
281N/A struct mail_private *p = (struct mail_private *)mail;
281N/A const char *path;
281N/A int fd = -1;
281N/A
281N/A *deleted_r = FALSE;
281N/A
281N/A p->stats_open_lookup_count++;
281N/A if (mail->uid != 0) {
281N/A if (maildir_file_do(mbox, mail->uid, do_open, &fd) < 0)
281N/A return NULL;
281N/A } else {
281N/A path = maildir_save_file_get_path(mail->transaction, mail->seq);
281N/A if (do_open(mbox, path, &fd) <= 0)
281N/A return NULL;
281N/A }
281N/A
281N/A if (fd == -1) {
281N/A *deleted_r = TRUE;
281N/A return NULL;
281N/A }
281N/A
281N/A return i_stream_create_fd(fd, MAIL_READ_BLOCK_SIZE, TRUE);
281N/A}
281N/A
281N/Astatic int maildir_mail_stat(struct mail *mail, struct stat *st)
281N/A{
281N/A struct maildir_mailbox *mbox = (struct maildir_mailbox *)mail->box;
281N/A struct index_mail *imail = (struct index_mail *)mail;
281N/A const char *path;
281N/A int fd, ret;
281N/A
281N/A if (mail->lookup_abort == MAIL_LOOKUP_ABORT_NOT_IN_CACHE)
281N/A return mail_set_aborted(mail);
281N/A
281N/A if (imail->data.access_part != 0 && imail->data.stream == NULL) {
281N/A /* we're going to open the mail anyway */
281N/A struct istream *input;
281N/A
281N/A (void)mail_get_stream(mail, NULL, NULL, &input);
281N/A }
281N/A
281N/A if (imail->data.stream != NULL) {
281N/A fd = i_stream_get_fd(imail->data.stream);
281N/A i_assert(fd != -1);
281N/A
281N/A imail->mail.stats_fstat_lookup_count++;
281N/A if (fstat(fd, st) < 0) {
281N/A mail_storage_set_critical(&mbox->storage->storage,
281N/A "fstat(maildir) failed: %m");
281N/A return -1;
281N/A }
281N/A } else if (mail->uid != 0) {
281N/A imail->mail.stats_stat_lookup_count++;
281N/A ret = maildir_file_do(mbox, mail->uid, do_stat, st);
281N/A if (ret <= 0) {
281N/A if (ret == 0)
281N/A mail_set_expunged(mail);
281N/A return -1;
281N/A }
281N/A } else {
281N/A imail->mail.stats_stat_lookup_count++;
281N/A path = maildir_save_file_get_path(mail->transaction, mail->seq);
281N/A if (stat(path, st) < 0) {
281N/A mail_storage_set_critical(mail->box->storage,
281N/A "stat(%s) failed: %m", path);
281N/A return -1;
281N/A }
281N/A }
281N/A return 0;
281N/A}
281N/A
281N/Astatic int maildir_mail_get_received_date(struct mail *_mail, time_t *date_r)
281N/A{
281N/A struct index_mail *mail = (struct index_mail *)_mail;
281N/A struct index_mail_data *data = &mail->data;
281N/A struct stat st;
281N/A
281N/A if (index_mail_get_received_date(_mail, date_r) == 0)
281N/A return 0;
281N/A
281N/A if (maildir_mail_stat(_mail, &st) < 0)
281N/A return -1;
281N/A
281N/A *date_r = data->received_date = st.st_mtime;
281N/A return 0;
281N/A}
281N/A
281N/Astatic int maildir_mail_get_save_date(struct mail *_mail, time_t *date_r)
281N/A{
281N/A struct index_mail *mail = (struct index_mail *)_mail;
281N/A struct index_mail_data *data = &mail->data;
281N/A struct stat st;
281N/A
281N/A if (index_mail_get_save_date(_mail, date_r) == 0)
281N/A return 0;
281N/A
281N/A if (maildir_mail_stat(_mail, &st) < 0)
281N/A return -1;
281N/A
281N/A *date_r = data->save_date = st.st_ctime;
281N/A return data->save_date;
281N/A}
281N/A
281N/Astatic int
281N/Amaildir_mail_get_fname(struct maildir_mailbox *mbox, struct mail *mail,
281N/A const char **fname_r)
281N/A{
281N/A enum maildir_uidlist_rec_flag flags;
281N/A struct mail_index_view *view;
281N/A uint32_t seq;
281N/A bool exists;
281N/A int ret;
281N/A
281N/A ret = maildir_uidlist_lookup(mbox->uidlist, mail->uid, &flags, fname_r);
281N/A if (ret != 0)
281N/A return ret;
281N/A
281N/A /* file exists in index file, but not in dovecot-uidlist anymore. */
281N/A mail_set_expunged(mail);
281N/A
281N/A /* one reason this could happen is if we delayed opening
281N/A dovecot-uidlist and we're trying to open a mail that got recently
281N/A expunged. Let's test this theory first: */
281N/A (void)mail_index_refresh(mbox->ibox.index);
281N/A view = mail_index_view_open(mbox->ibox.index);
281N/A exists = mail_index_lookup_seq(view, mail->uid, &seq);
281N/A mail_index_view_close(&view);
281N/A
281N/A if (exists) {
281N/A /* the message still exists in index. this means there's some
281N/A kind of a desync, which doesn't get fixed if cur/ mtime is
281N/A the same as in index. fix this by forcing a resync. */
281N/A (void)maildir_storage_sync_force(mbox, mail->uid);
281N/A }
281N/A return 0;
281N/A}
281N/A
281N/Astatic int maildir_get_pop3_state(struct index_mail *mail)
281N/A{
281N/A const struct mail_cache_field *fields;
281N/A unsigned int i, count, psize_idx, vsize_idx;
281N/A enum mail_cache_decision_type dec, vsize_dec;
281N/A enum mail_fetch_field allowed_pop3_fields;
281N/A bool not_pop3_only = FALSE;
281N/A
281N/A if (mail->pop3_state_set)
281N/A return mail->pop3_state;
281N/A
281N/A /* if this mail itself has non-pop3 fields we know we're not
281N/A pop3-only */
281N/A allowed_pop3_fields = MAIL_FETCH_FLAGS | MAIL_FETCH_STREAM_HEADER |
281N/A MAIL_FETCH_STREAM_BODY | MAIL_FETCH_UIDL_FILE_NAME |
281N/A MAIL_FETCH_VIRTUAL_SIZE;
281N/A
281N/A if (mail->wanted_headers != NULL ||
281N/A (mail->wanted_fields & ~allowed_pop3_fields) != 0)
281N/A not_pop3_only = TRUE;
281N/A
281N/A /* get vsize decisions */
281N/A psize_idx = mail->ibox->cache_fields[MAIL_CACHE_PHYSICAL_FULL_SIZE].idx;
281N/A vsize_idx = mail->ibox->cache_fields[MAIL_CACHE_VIRTUAL_FULL_SIZE].idx;
281N/A if (not_pop3_only) {
281N/A vsize_dec = mail_cache_field_get_decision(mail->ibox->cache,
281N/A vsize_idx);
281N/A vsize_dec &= ~MAIL_CACHE_DECISION_FORCED;
281N/A } else {
281N/A /* also check if there are any non-[pv]size cached fields */
281N/A vsize_dec = MAIL_CACHE_DECISION_NO;
281N/A fields = mail_cache_register_get_list(mail->ibox->cache,
281N/A pool_datastack_create(),
281N/A &count);
281N/A for (i = 0; i < count; i++) {
281N/A dec = fields[i].decision & ~MAIL_CACHE_DECISION_FORCED;
281N/A if (fields[i].idx == vsize_idx)
281N/A vsize_dec = dec;
281N/A else if (dec != MAIL_CACHE_DECISION_NO &&
281N/A fields[i].idx != psize_idx)
281N/A not_pop3_only = TRUE;
281N/A }
281N/A }
281N/A
281N/A if (!not_pop3_only) {
281N/A /* either nothing is cached, or only vsize is cached. */
281N/A mail->pop3_state = 1;
281N/A } else if (vsize_dec != MAIL_CACHE_DECISION_YES &&
281N/A (mail->ibox->box.open_flags &
281N/A MAILBOX_OPEN_POP3_SESSION) == 0) {
281N/A /* if virtual size isn't cached permanently,
281N/A POP3 isn't being used */
281N/A mail->pop3_state = -1;
281N/A } else {
281N/A /* possibly a mixed pop3/imap */
281N/A mail->pop3_state = 0;
281N/A }
281N/A mail->pop3_state_set = TRUE;
281N/A return mail->pop3_state;
281N/A}
281N/A
281N/Astatic int maildir_quick_size_lookup(struct index_mail *mail, bool vsize,
281N/A uoff_t *size_r)
281N/A{
281N/A struct mail *_mail = &mail->mail.mail;
281N/A struct maildir_mailbox *mbox = (struct maildir_mailbox *)mail->ibox;
281N/A enum maildir_uidlist_rec_ext_key key;
281N/A const char *path, *fname, *value;
281N/A uoff_t size;
281N/A char *p;
281N/A
281N/A if (_mail->uid != 0) {
281N/A if (maildir_mail_get_fname(mbox, _mail, &fname) <= 0)
281N/A return -1;
281N/A } else {
281N/A path = maildir_save_file_get_path(_mail->transaction,
281N/A _mail->seq);
281N/A fname = strrchr(path, '/');
281N/A fname = fname != NULL ? fname + 1 : path;
281N/A }
281N/A
281N/A /* size can be included in filename */
281N/A if (maildir_filename_get_size(fname,
281N/A vsize ? MAILDIR_EXTRA_VIRTUAL_SIZE :
281N/A MAILDIR_EXTRA_FILE_SIZE,
281N/A size_r))
281N/A return 1;
281N/A
281N/A /* size can be included in uidlist entry */
281N/A if (_mail->uid != 0) {
281N/A key = vsize ? MAILDIR_UIDLIST_REC_EXT_VSIZE :
281N/A MAILDIR_UIDLIST_REC_EXT_PSIZE;
281N/A value = maildir_uidlist_lookup_ext(mbox->uidlist, _mail->uid,
281N/A key);
281N/A if (value != NULL) {
281N/A size = strtoull(value, &p, 10);
281N/A if (*p == '\0') {
281N/A *size_r = size;
281N/A return 1;
281N/A }
281N/A }
281N/A }
281N/A return 0;
281N/A}
281N/A
281N/Astatic void
281N/Amaildir_handle_size_caching(struct index_mail *mail, bool quick_check,
281N/A bool vsize)
281N/A{
281N/A struct maildir_mailbox *mbox = (struct maildir_mailbox *)mail->ibox;
281N/A enum mail_fetch_field field;
281N/A uoff_t size;
281N/A int pop3_state;
281N/A
281N/A field = vsize ? MAIL_FETCH_VIRTUAL_SIZE : MAIL_FETCH_PHYSICAL_SIZE;
281N/A if ((mail->data.dont_cache_fetch_fields & field) != 0)
281N/A return;
281N/A
281N/A if (quick_check && maildir_quick_size_lookup(mail, vsize, &size) > 0) {
281N/A /* already in filename / uidlist. don't add it anywhere,
281N/A including to the uidlist if it's already in filename.
281N/A do some extra checks here to catch potential cache bugs. */
281N/A if (vsize && mail->data.virtual_size != size) {
281N/A mail_cache_set_corrupted(mail->ibox->cache,
281N/A "Corrupted virtual size for uid=%u: "
281N/A "%"PRIuUOFF_T" != %"PRIuUOFF_T,
281N/A mail->mail.mail.uid,
281N/A mail->data.virtual_size, size);
281N/A mail->data.virtual_size = size;
281N/A } else if (!vsize && mail->data.physical_size != size) {
281N/A mail_cache_set_corrupted(mail->ibox->cache,
281N/A "Corrupted physical size for uid=%u: "
281N/A "%"PRIuUOFF_T" != %"PRIuUOFF_T,
281N/A mail->mail.mail.uid,
281N/A mail->data.physical_size, size);
281N/A mail->data.physical_size = size;
281N/A }
281N/A mail->data.dont_cache_fetch_fields |= field;
281N/A return;
281N/A }
281N/A
281N/A /* 1 = pop3-only, 0 = mixed, -1 = no pop3 */
281N/A pop3_state = maildir_get_pop3_state(mail);
281N/A if (pop3_state >= 0 && mail->mail.mail.uid != 0) {
281N/A /* if size is wanted permanently, store it to uidlist
281N/A so that in case cache file gets lost we can get it quickly */
281N/A mail->data.dont_cache_fetch_fields |= field;
281N/A size = vsize ? mail->data.virtual_size :
281N/A mail->data.physical_size;
281N/A maildir_uidlist_set_ext(mbox->uidlist, mail->mail.mail.uid,
281N/A vsize ? MAILDIR_UIDLIST_REC_EXT_VSIZE :
281N/A MAILDIR_UIDLIST_REC_EXT_PSIZE,
281N/A dec2str(size));
281N/A }
281N/A}
281N/A
281N/Astatic int maildir_mail_get_virtual_size(struct mail *_mail, uoff_t *size_r)
281N/A{
281N/A struct index_mail *mail = (struct index_mail *)_mail;
281N/A struct index_mail_data *data = &mail->data;
281N/A struct message_size hdr_size, body_size;
281N/A struct istream *input;
281N/A uoff_t old_offset;
281N/A
281N/A if (index_mail_get_cached_virtual_size(mail, size_r)) {
281N/A i_assert(mail->data.virtual_size != (uoff_t)-1);
281N/A maildir_handle_size_caching(mail, TRUE, TRUE);
281N/A return 0;
281N/A }
281N/A
281N/A if (maildir_quick_size_lookup(mail, TRUE, &data->virtual_size) < 0)
281N/A return -1;
281N/A if (data->virtual_size != (uoff_t)-1) {
281N/A data->dont_cache_fetch_fields |= MAIL_FETCH_VIRTUAL_SIZE;
281N/A *size_r = data->virtual_size;
281N/A return 0;
281N/A }
281N/A
281N/A /* fallback to reading the file */
281N/A old_offset = data->stream == NULL ? 0 : data->stream->v_offset;
281N/A if (mail_get_stream(_mail, &hdr_size, &body_size, &input) < 0)
281N/A return -1;
281N/A i_stream_seek(data->stream, old_offset);
281N/A
281N/A maildir_handle_size_caching(mail, FALSE, TRUE);
281N/A *size_r = data->virtual_size;
281N/A return 0;
281N/A}
281N/A
281N/Astatic int maildir_mail_get_physical_size(struct mail *_mail, uoff_t *size_r)
281N/A{
281N/A struct index_mail *mail = (struct index_mail *)_mail;
281N/A struct maildir_mailbox *mbox = (struct maildir_mailbox *)mail->ibox;
281N/A struct index_mail_data *data = &mail->data;
281N/A struct stat st;
281N/A const char *path;
281N/A int ret;
281N/A
281N/A if (index_mail_get_physical_size(_mail, size_r) == 0) {
281N/A i_assert(mail->data.physical_size != (uoff_t)-1);
281N/A maildir_handle_size_caching(mail, TRUE, FALSE);
281N/A return 0;
281N/A }
281N/A
281N/A if (maildir_quick_size_lookup(mail, FALSE, &data->physical_size) < 0)
281N/A return -1;
281N/A if (data->physical_size != (uoff_t)-1) {
281N/A data->dont_cache_fetch_fields |= MAIL_FETCH_PHYSICAL_SIZE;
281N/A *size_r = data->physical_size;
281N/A return 0;
281N/A }
281N/A
281N/A if (_mail->uid != 0) {
281N/A ret = maildir_file_do(mbox, _mail->uid, do_stat, &st);
281N/A if (ret <= 0) {
281N/A if (ret == 0)
281N/A mail_set_expunged(_mail);
281N/A return -1;
281N/A }
281N/A } else {
281N/A /* saved mail which hasn't been committed yet */
281N/A path = maildir_save_file_get_path(_mail->transaction,
281N/A _mail->seq);
281N/A if (stat(path, &st) < 0) {
281N/A mail_storage_set_critical(_mail->box->storage,
281N/A "stat(%s) failed: %m", path);
281N/A return -1;
281N/A }
281N/A }
281N/A
281N/A data->physical_size = st.st_size;
281N/A maildir_handle_size_caching(mail, FALSE, FALSE);
281N/A *size_r = st.st_size;
281N/A return 0;
281N/A}
281N/A
281N/Astatic int
281N/Amaildir_mail_get_special(struct mail *_mail, enum mail_fetch_field field,
281N/A const char **value_r)
281N/A{
281N/A struct index_mail *mail = (struct index_mail *)_mail;
281N/A struct maildir_mailbox *mbox = (struct maildir_mailbox *)mail->ibox;
281N/A const char *path, *fname, *end, *uidl;
281N/A
281N/A switch (field) {
281N/A case MAIL_FETCH_UIDL_FILE_NAME:
281N/A case MAIL_FETCH_GUID:
281N/A if (_mail->uid != 0) {
281N/A if (maildir_mail_get_fname(mbox, _mail, &fname) <= 0)
281N/A return -1;
281N/A } else {
281N/A path = maildir_save_file_get_path(_mail->transaction,
281N/A _mail->seq);
281N/A fname = strrchr(path, '/');
281N/A fname = fname != NULL ? fname + 1 : path;
281N/A }
281N/A end = strchr(fname, MAILDIR_INFO_SEP);
281N/A *value_r = end == NULL ? fname : t_strdup_until(fname, end);
281N/A return 0;
281N/A case MAIL_FETCH_UIDL_BACKEND:
281N/A uidl = maildir_uidlist_lookup_ext(mbox->uidlist, _mail->uid,
281N/A MAILDIR_UIDLIST_REC_EXT_POP3_UIDL);
281N/A if (uidl == NULL) {
281N/A /* use the default */
281N/A *value_r = "";
281N/A } else if (*uidl == '\0') {
281N/A /* special optimization case: use the base file name */
281N/A return maildir_mail_get_special(_mail,
281N/A MAIL_FETCH_UIDL_FILE_NAME, value_r);
281N/A } else {
281N/A *value_r = uidl;
281N/A }
281N/A return 0;
281N/A default:
281N/A return index_mail_get_special(_mail, field, value_r);
281N/A }
281N/A}
281N/A
281N/Astatic int maildir_mail_get_stream(struct mail *_mail,
281N/A struct message_size *hdr_size,
281N/A struct message_size *body_size,
281N/A struct istream **stream_r)
281N/A{
281N/A struct index_mail *mail = (struct index_mail *)_mail;
281N/A struct maildir_mailbox *mbox = (struct maildir_mailbox *)mail->ibox;
281N/A struct index_mail_data *data = &mail->data;
281N/A bool deleted;
281N/A
281N/A if (data->stream == NULL) {
281N/A data->stream = maildir_open_mail(mbox, _mail, &deleted);
281N/A if (data->stream == NULL) {
281N/A if (deleted)
281N/A mail_set_expunged(_mail);
281N/A return -1;
281N/A }
281N/A }
281N/A
281N/A return index_mail_init_stream(mail, hdr_size, body_size, stream_r);
281N/A}
281N/A
281N/Astatic void maildir_mail_set_cache_corrupted(struct mail *_mail,
281N/A enum mail_fetch_field field)
281N/A{
281N/A struct index_mail *mail = (struct index_mail *)_mail;
281N/A struct maildir_mailbox *mbox = (struct maildir_mailbox *)mail->ibox;
281N/A enum maildir_uidlist_rec_flag flags;
281N/A const char *fname;
281N/A uoff_t size;
281N/A int ret;
281N/A
281N/A if (field == MAIL_FETCH_VIRTUAL_SIZE) {
281N/A /* make sure it gets removed from uidlist.
281N/A if it's in file name, we can't really do more than log it. */
281N/A ret = maildir_uidlist_lookup(mbox->uidlist, _mail->uid,
281N/A &flags, &fname);
281N/A if (ret <= 0)
281N/A return;
281N/A if (maildir_filename_get_size(fname, MAILDIR_EXTRA_VIRTUAL_SIZE,
281N/A &size)) {
281N/A const char *subdir =
281N/A (flags & MAILDIR_UIDLIST_REC_FLAG_NEW_DIR) != 0 ?
281N/A "new" : "cur";
281N/A mail_storage_set_critical(_mail->box->storage,
281N/A "Maildir filename has wrong W value: %s/%s/%s",
mbox->path, subdir, fname);
} else if (maildir_uidlist_lookup_ext(mbox->uidlist, _mail->uid,
MAILDIR_UIDLIST_REC_EXT_VSIZE) != NULL) {
maildir_uidlist_set_ext(mbox->uidlist, _mail->uid,
MAILDIR_UIDLIST_REC_EXT_VSIZE,
NULL);
}
}
index_mail_set_cache_corrupted(_mail, field);
}
struct mail_vfuncs maildir_mail_vfuncs = {
index_mail_close,
index_mail_free,
index_mail_set_seq,
index_mail_set_uid,
index_mail_set_uid_cache_updates,
index_mail_get_flags,
index_mail_get_keywords,
index_mail_get_keyword_indexes,
index_mail_get_modseq,
index_mail_get_parts,
index_mail_get_date,
maildir_mail_get_received_date,
maildir_mail_get_save_date,
maildir_mail_get_virtual_size,
maildir_mail_get_physical_size,
index_mail_get_first_header,
index_mail_get_headers,
index_mail_get_header_stream,
maildir_mail_get_stream,
maildir_mail_get_special,
index_mail_update_flags,
index_mail_update_keywords,
index_mail_expunge,
maildir_mail_set_cache_corrupted,
index_mail_get_index_mail
};