From: Timo Sirainen Date: Sun, 26 Oct 2003 20:13:15 +0000 (+0200) Subject: Removed fetch_init/fetch_next from mail-storage. search_* makes it X-Git-Tag: 1.1.alpha1~4261 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b1f57b7183fe44d7d7ba30b944d3de1f015b9253;p=thirdparty%2Fdovecot%2Fcore.git Removed fetch_init/fetch_next from mail-storage. search_* makes it unnecessary --HG-- branch : HEAD --- diff --git a/src/imap/cmd-copy.c b/src/imap/cmd-copy.c index 0a1dccd33c..3956cf8513 100644 --- a/src/imap/cmd-copy.c +++ b/src/imap/cmd-copy.c @@ -2,29 +2,32 @@ #include "common.h" #include "commands.h" +#include "imap-search.h" static int fetch_and_copy(struct mail_copy_context *copy_ctx, struct mailbox *srcbox, struct mailbox *destbox, const char *messageset, int uidset, int *all_found) { - struct mail_fetch_context *fetch_ctx; + struct mail_search_arg *search_arg; + struct mail_search_context *search_ctx; struct mail *mail; int failed = FALSE; - fetch_ctx = srcbox->fetch_init(srcbox, MAIL_FETCH_STREAM_HEADER | - MAIL_FETCH_STREAM_BODY, NULL, - messageset, uidset); - if (fetch_ctx == NULL) + search_arg = imap_search_get_msgset_arg(messageset, uidset); + search_ctx = srcbox->search_init(srcbox, NULL, search_arg, NULL, + MAIL_FETCH_STREAM_HEADER | + MAIL_FETCH_STREAM_BODY, NULL); + if (search_ctx == NULL) return FALSE; - while ((mail = srcbox->fetch_next(fetch_ctx)) != NULL) { + while ((mail = srcbox->search_next(search_ctx)) != NULL) { if (!destbox->copy(mail, copy_ctx)) { failed = TRUE; break; } } - if (!srcbox->fetch_deinit(fetch_ctx, all_found)) + if (!srcbox->search_deinit(search_ctx, all_found)) return FALSE; return !failed; diff --git a/src/imap/cmd-search.c b/src/imap/cmd-search.c index a6eeb5545f..b6d7471a8b 100644 --- a/src/imap/cmd-search.c +++ b/src/imap/cmd-search.c @@ -37,7 +37,7 @@ static int imap_search(struct client *client, const char *charset, str_printfa(str, " %u", uid ? mail->uid : mail->seq); } - ret = client->mailbox->search_deinit(ctx); + ret = client->mailbox->search_deinit(ctx, NULL); if (!first || ret) { str_append(str, "\r\n"); diff --git a/src/imap/cmd-store.c b/src/imap/cmd-store.c index 61b2dcbfef..4022de7326 100644 --- a/src/imap/cmd-store.c +++ b/src/imap/cmd-store.c @@ -2,6 +2,7 @@ #include "common.h" #include "commands.h" +#include "imap-search.h" static int get_modify_type(struct client *client, const char *item, enum modify_type *modify_type, int *silent) @@ -59,7 +60,8 @@ int cmd_store(struct client *client) struct mail_full_flags flags; enum modify_type modify_type; struct mailbox *box; - struct mail_fetch_context *fetch_ctx; + struct mail_search_arg *search_arg; + struct mail_search_context *search_ctx; struct mail *mail; const char *messageset, *item; int silent, all_found, failed; @@ -104,14 +106,16 @@ int cmd_store(struct client *client) MAILBOX_LOCK_READ); } - fetch_ctx = failed ? NULL : - box->fetch_init(box, MAIL_FETCH_FLAGS, NULL, - messageset, client->cmd_uid); - if (fetch_ctx == NULL) + search_arg = imap_search_get_msgset_arg(messageset, client->cmd_uid); + search_ctx = failed ? NULL : + box->search_init(box, NULL, search_arg, NULL, + MAIL_FETCH_FLAGS, NULL); + + if (search_ctx == NULL) failed = TRUE; else { failed = FALSE; - while ((mail = box->fetch_next(fetch_ctx)) != NULL) { + while ((mail = box->search_next(search_ctx)) != NULL) { if (!mail->update_flags(mail, &flags, modify_type)) { failed = TRUE; break; @@ -126,7 +130,7 @@ int cmd_store(struct client *client) } } - if (!box->fetch_deinit(fetch_ctx, &all_found)) + if (!box->search_deinit(search_ctx, &all_found)) failed = TRUE; (void)box->lock(box, MAILBOX_LOCK_UNLOCK); diff --git a/src/imap/imap-fetch.c b/src/imap/imap-fetch.c index 2a92bedc73..d8f150c63b 100644 --- a/src/imap/imap-fetch.c +++ b/src/imap/imap-fetch.c @@ -10,6 +10,7 @@ #include "imap-date.h" #include "commands.h" #include "imap-fetch.h" +#include "imap-search.h" #include @@ -330,6 +331,7 @@ int imap_fetch(struct client *client, const char *messageset, int uidset) { struct mailbox *box = client->mailbox; + struct mail_search_arg *search_arg; struct imap_fetch_context ctx; struct mail *mail; struct imap_fetch_body_data *body; @@ -386,13 +388,14 @@ int imap_fetch(struct client *client, return -1; } - ctx.fetch_ctx = box->fetch_init(box, fetch_data, wanted_headers, - messageset, uidset); - if (ctx.fetch_ctx == NULL) + search_arg = imap_search_get_msgset_arg(messageset, uidset); + ctx.search_ctx = box->search_init(box, NULL, search_arg, NULL, + fetch_data, wanted_headers); + if (ctx.search_ctx == NULL) ctx.failed = TRUE; else { ctx.str = str_new(default_pool, 8192); - while ((mail = box->fetch_next(ctx.fetch_ctx)) != NULL) { + while ((mail = box->search_next(ctx.search_ctx)) != NULL) { if (!fetch_mail(&ctx, mail)) { ctx.failed = TRUE; break; @@ -400,7 +403,7 @@ int imap_fetch(struct client *client, } str_free(ctx.str); - if (!box->fetch_deinit(ctx.fetch_ctx, &all_found)) + if (!box->search_deinit(ctx.search_ctx, &all_found)) ctx.failed = TRUE; } diff --git a/src/imap/imap-fetch.h b/src/imap/imap-fetch.h index bd55060ff8..e53623b2a1 100644 --- a/src/imap/imap-fetch.h +++ b/src/imap/imap-fetch.h @@ -19,7 +19,7 @@ struct imap_fetch_body_data { }; struct imap_fetch_context { - struct mail_fetch_context *fetch_ctx; + struct mail_search_context *search_ctx; enum mail_fetch_field fetch_data; enum imap_fetch_field imap_data; diff --git a/src/imap/imap-search.c b/src/imap/imap-search.c index 9b2c6168d6..50836dbf31 100644 --- a/src/imap/imap-search.c +++ b/src/imap/imap-search.c @@ -349,3 +349,13 @@ imap_search_args_build(pool_t pool, struct imap_arg *args, const char **error) return first_sarg; } +struct mail_search_arg * +imap_search_get_msgset_arg(const char *messageset, int uidset) +{ + struct mail_search_arg *arg; + + arg = t_new(struct mail_search_arg, 1); + arg->type = uidset ? SEARCH_UID : SEARCH_SET; + arg->value.str = t_strdup(messageset); + return arg; +} diff --git a/src/imap/imap-search.h b/src/imap/imap-search.h index bc710ea047..c2a9a8d8d5 100644 --- a/src/imap/imap-search.h +++ b/src/imap/imap-search.h @@ -5,4 +5,7 @@ struct mail_search_arg * imap_search_args_build(pool_t pool, struct imap_arg *args, const char **error); +struct mail_search_arg * +imap_search_get_msgset_arg(const char *messageset, int uidset); + #endif diff --git a/src/imap/imap-sort.c b/src/imap/imap-sort.c index 6589283b86..4e8818bd1b 100644 --- a/src/imap/imap-sort.c +++ b/src/imap/imap-sort.c @@ -244,7 +244,7 @@ int imap_sort(struct client *client, const char *charset, mail_sort_input(ctx, mail); mail_sort_flush(ctx); - ret = client->mailbox->search_deinit(ctx->search_ctx); + ret = client->mailbox->search_deinit(ctx->search_ctx, NULL); if (ctx->written || ret) { str_append(ctx->str, "\r\n"); diff --git a/src/imap/imap-thread.c b/src/imap/imap-thread.c index 57b8a7f51f..87a468d2bd 100644 --- a/src/imap/imap-thread.c +++ b/src/imap/imap-thread.c @@ -142,7 +142,7 @@ int imap_thread(struct client *client, const char *charset, mail_thread_finish(ctx); o_stream_send_str(client->output, "\r\n"); - ret = client->mailbox->search_deinit(ctx->search_ctx); + ret = client->mailbox->search_deinit(ctx->search_ctx, NULL); mail_thread_deinit(ctx); return ret; } diff --git a/src/lib-storage/index/index-fetch.c b/src/lib-storage/index/index-fetch.c index 89c1a3cbcd..fdaaf8020c 100644 --- a/src/lib-storage/index/index-fetch.c +++ b/src/lib-storage/index/index-fetch.c @@ -10,92 +10,6 @@ #include "index-messageset.h" #include "index-mail.h" -struct mail_fetch_context { - struct index_mailbox *ibox; - struct mail_index *index; - - struct messageset_context *msgset_ctx; - struct index_mail mail; - - enum mail_lock_type old_lock; -}; - -struct mail_fetch_context * -index_storage_fetch_init(struct mailbox *box, - enum mail_fetch_field wanted_fields, - const char *const *wanted_headers, - const char *messageset, int uidset) -{ - struct index_mailbox *ibox = (struct index_mailbox *) box; - struct mail_fetch_context *ctx; - int check_mail; - - ctx = i_new(struct mail_fetch_context, 1); - ctx->old_lock = ibox->index->lock_type; - - check_mail = (client_workarounds & - WORKAROUND_OE6_FETCH_NO_NEWMAIL) == 0; - if (!index_storage_sync_and_lock(ibox, check_mail, TRUE, - MAIL_LOCK_SHARED)) - return NULL; - - ctx->ibox = ibox; - ctx->index = ibox->index; - - index_mail_init(ibox, &ctx->mail, wanted_fields, wanted_headers); - ctx->msgset_ctx = index_messageset_init(ibox, messageset, uidset, TRUE); - return ctx; -} - -int index_storage_fetch_deinit(struct mail_fetch_context *ctx, int *all_found) -{ - int ret; - - ret = index_messageset_deinit(ctx->msgset_ctx); - - if (all_found != NULL) - *all_found = ret > 0; - - if (ctx->ibox->fetch_mail.pool != NULL) - index_mail_deinit(&ctx->ibox->fetch_mail); - if (ctx->mail.pool != NULL) - index_mail_deinit(&ctx->mail); - - if (!index_storage_lock(ctx->ibox, ctx->old_lock)) - ret = -1; - - i_free(ctx); - return ret >= 0; -} - -struct mail *index_storage_fetch_next(struct mail_fetch_context *ctx) -{ - const struct messageset_mail *msgset_mail; - int ret; - - do { - msgset_mail = index_messageset_next(ctx->msgset_ctx); - if (msgset_mail == NULL) { - ret = -1; - break; - } - - ctx->mail.mail.seq = msgset_mail->client_seq; - ctx->mail.mail.uid = msgset_mail->rec->uid; - - ret = index_mail_next(&ctx->mail, msgset_mail->rec, - msgset_mail->idx_seq, FALSE); - } while (ret == 0); - - if (ret < 0) { - /* error or last record */ - index_mail_deinit(&ctx->mail); - return NULL; - } - - return &ctx->mail.mail; -} - static struct mail * fetch_record(struct index_mailbox *ibox, struct mail_index_record *rec, unsigned int idx_seq, enum mail_fetch_field wanted_fields) diff --git a/src/lib-storage/index/index-search.c b/src/lib-storage/index/index-search.c index d93f732753..68bc9ccf21 100644 --- a/src/lib-storage/index/index-search.c +++ b/src/lib-storage/index/index-search.c @@ -870,15 +870,21 @@ index_storage_search_init(struct mailbox *box, const char *charset, return ctx; } -int index_storage_search_deinit(struct mail_search_context *ctx) +int index_storage_search_deinit(struct mail_search_context *ctx, int *all_found) { - int ret; + int ret, msgset_ret; ret = !ctx->failed && ctx->error == NULL; if (ctx->msgset_ctx != NULL) { - if (index_messageset_deinit(ctx->msgset_ctx) < 0) + msgset_ret = index_messageset_deinit(ctx->msgset_ctx); + if (msgset_ret < 0) ret = FALSE; + if (all_found != NULL) + *all_found = msgset_ret > 0; + } else { + if (all_found != NULL) + *all_found = !ctx->failed; } if (ctx->ibox->fetch_mail.pool != NULL) diff --git a/src/lib-storage/index/index-storage.h b/src/lib-storage/index/index-storage.h index eba3509bb4..18204f2550 100644 --- a/src/lib-storage/index/index-storage.h +++ b/src/lib-storage/index/index-storage.h @@ -98,14 +98,6 @@ int index_storage_get_status(struct mailbox *box, struct mailbox_status *status); int index_storage_sync(struct mailbox *box, enum mailbox_sync_flags flags); -struct mail_fetch_context * -index_storage_fetch_init(struct mailbox *box, - enum mail_fetch_field wanted_fields, - const char *const *wanted_headers, - const char *messageset, int uidset); -int index_storage_fetch_deinit(struct mail_fetch_context *ctx, int *all_found); -struct mail *index_storage_fetch_next(struct mail_fetch_context *ctx); - struct mail *index_storage_fetch_uid(struct mailbox *box, unsigned int uid, enum mail_fetch_field wanted_fields); struct mail *index_storage_fetch_seq(struct mailbox *box, unsigned int seq, @@ -119,7 +111,8 @@ index_storage_search_init(struct mailbox *box, const char *charset, const enum mail_sort_type *sort_program, enum mail_fetch_field wanted_fields, const char *const wanted_headers[]); -int index_storage_search_deinit(struct mail_search_context *ctx); +int index_storage_search_deinit(struct mail_search_context *ctx, + int *all_found); struct mail *index_storage_search_next(struct mail_search_context *ctx); struct mail_copy_context *index_storage_copy_init(struct mailbox *box); diff --git a/src/lib-storage/index/maildir/maildir-storage.c b/src/lib-storage/index/maildir/maildir-storage.c index afac326951..986d8beaf7 100644 --- a/src/lib-storage/index/maildir/maildir-storage.c +++ b/src/lib-storage/index/maildir/maildir-storage.c @@ -857,9 +857,6 @@ struct mailbox maildir_mailbox = { index_storage_get_status, index_storage_sync, maildir_storage_auto_sync, - index_storage_fetch_init, - index_storage_fetch_deinit, - index_storage_fetch_next, index_storage_fetch_uid, index_storage_fetch_seq, index_storage_search_get_sorting, diff --git a/src/lib-storage/index/mbox/mbox-storage.c b/src/lib-storage/index/mbox/mbox-storage.c index 1f2794e055..d10c0a365a 100644 --- a/src/lib-storage/index/mbox/mbox-storage.c +++ b/src/lib-storage/index/mbox/mbox-storage.c @@ -857,9 +857,6 @@ struct mailbox mbox_mailbox = { index_storage_get_status, index_storage_sync, mbox_storage_auto_sync, - index_storage_fetch_init, - index_storage_fetch_deinit, - index_storage_fetch_next, index_storage_fetch_uid, index_storage_fetch_seq, index_storage_search_get_sorting, diff --git a/src/lib-storage/mail-storage.h b/src/lib-storage/mail-storage.h index 7a99ffcd0c..5ced9790bb 100644 --- a/src/lib-storage/mail-storage.h +++ b/src/lib-storage/mail-storage.h @@ -253,21 +253,6 @@ struct mailbox { void (*auto_sync)(struct mailbox *box, enum mailbox_sync_flags flags, unsigned int min_newmail_notify_interval); - /* Initialize new fetch request. wanted_fields isn't required, but it - can be used for optimizations. update_flags must be set to TRUE, if - you want to call mail->update_flags() */ - struct mail_fetch_context * - (*fetch_init)(struct mailbox *box, - enum mail_fetch_field wanted_fields, - const char *const *wanted_headers, - const char *messageset, int uidset); - /* Deinitialize fetch request. all_found is set to TRUE if all of the - fetched messages were found (ie. not just deleted). */ - int (*fetch_deinit)(struct mail_fetch_context *ctx, int *all_found); - /* Fetch the next message. Returned mail object can be used until - the next call to fetch_next() or fetch_deinit(). */ - struct mail *(*fetch_next)(struct mail_fetch_context *ctx); - /* Simplified fetching for a single UID or sequence. Must be called between fetch_init() .. fetch_deinit() or search_init() .. search_deinit() */ @@ -296,8 +281,9 @@ struct mailbox { const enum mail_sort_type *sort_program, enum mail_fetch_field wanted_fields, const char *const wanted_headers[]); - /* Deinitialize search request. */ - int (*search_deinit)(struct mail_search_context *ctx); + /* Deinitialize search request. all_found is set to TRUE if all of the + messages in search range were found. */ + int (*search_deinit)(struct mail_search_context *ctx, int *all_found); /* Search the next message. Returned mail object can be used until the next call to search_next() or search_deinit(). */ struct mail *(*search_next)(struct mail_search_context *ctx); diff --git a/src/lib-storage/proxy-mailbox.c b/src/lib-storage/proxy-mailbox.c index f241598ad8..a3e8ab9ffc 100644 --- a/src/lib-storage/proxy-mailbox.c +++ b/src/lib-storage/proxy-mailbox.c @@ -54,17 +54,6 @@ static void _auto_sync(struct mailbox *box, enum mailbox_sync_flags flags, p->box->auto_sync(p->box, flags, min_newmail_notify_interval); } -static struct mail_fetch_context * -_fetch_init(struct mailbox *box, enum mail_fetch_field wanted_fields, - const char *const *wanted_headers, - const char *messageset, int uidset) -{ - struct proxy_mailbox *p = (struct proxy_mailbox *) box; - - return p->box->fetch_init(p->box, wanted_fields, wanted_headers, - messageset, uidset); -} - static struct mail *_fetch_uid(struct mailbox *box, unsigned int uid, enum mail_fetch_field wanted_fields) { @@ -142,8 +131,6 @@ void proxy_mailbox_init(struct proxy_mailbox *proxy, struct mailbox *box) pb->name = box->name; pb->storage = box->storage; - pb->fetch_deinit = box->fetch_deinit; - pb->fetch_next = box->fetch_next; pb->search_deinit = box->search_deinit; pb->search_next = box->search_next; pb->save_deinit = box->save_deinit; @@ -159,7 +146,6 @@ void proxy_mailbox_init(struct proxy_mailbox *proxy, struct mailbox *box) pb->get_status = _get_status; pb->sync = _sync; pb->auto_sync = _auto_sync; - pb->fetch_init = _fetch_init; pb->fetch_uid = _fetch_uid; pb->fetch_seq = _fetch_seq; pb->search_get_sorting = _search_get_sorting; diff --git a/src/pop3/client.c b/src/pop3/client.c index a269f79463..4762bc5808 100644 --- a/src/pop3/client.c +++ b/src/pop3/client.c @@ -7,6 +7,7 @@ #include "ostream.h" #include "mail-storage.h" #include "commands.h" +#include "mail-search.h" #include @@ -39,10 +40,10 @@ static void client_output_timeout(void *context) static int init_mailbox(struct client *client) { - struct mail_fetch_context *ctx; + struct mail_search_arg search_arg; + struct mail_search_context *ctx; struct mail *mail; struct mailbox_status status; - const char *messageset; int i, all_found, failed; if (!client->mailbox->get_status(client->mailbox, @@ -58,12 +59,14 @@ static int init_mailbox(struct client *client) if (client->messages_count == 0) return TRUE; + memset(&search_arg, 0, sizeof(search_arg)); + search_arg.type = SEARCH_ALL; + client->message_sizes = i_new(uoff_t, client->messages_count); - messageset = t_strdup_printf("1:%u", client->messages_count); for (i = 0; i < 2; i++) { - ctx = client->mailbox->fetch_init(client->mailbox, - MAIL_FETCH_SIZE, NULL, - messageset, FALSE); + ctx = client->mailbox->search_init(client->mailbox, NULL, + &search_arg, NULL, + MAIL_FETCH_SIZE, NULL); if (ctx == NULL) { client_send_storage_error(client); return FALSE; @@ -71,7 +74,7 @@ static int init_mailbox(struct client *client) client->total_size = 0; failed = FALSE; - while ((mail = client->mailbox->fetch_next(ctx)) != NULL) { + while ((mail = client->mailbox->search_next(ctx)) != NULL) { uoff_t size = mail->get_size(mail); if (size == (uoff_t)-1) { @@ -84,7 +87,7 @@ static int init_mailbox(struct client *client) client->message_sizes[mail->seq-1] = size; } - if (!client->mailbox->fetch_deinit(ctx, &all_found)) { + if (!client->mailbox->search_deinit(ctx, &all_found)) { client_send_storage_error(client); return FALSE; } diff --git a/src/pop3/commands.c b/src/pop3/commands.c index 45b4ea226f..218a2f1f15 100644 --- a/src/pop3/commands.c +++ b/src/pop3/commands.c @@ -6,6 +6,7 @@ #include "str.h" #include "message-size.h" #include "mail-storage.h" +#include "mail-search.h" #include "capability.h" #include "commands.h" @@ -255,20 +256,25 @@ static void stream_send_escaped(struct ostream *output, struct istream *input, static void fetch(struct client *client, unsigned int msgnum, uoff_t body_lines) { - struct mail_fetch_context *ctx; + struct mail_search_arg search_arg; + struct mail_search_context *ctx; struct mail *mail; struct istream *stream; - ctx = client->mailbox->fetch_init(client->mailbox, - MAIL_FETCH_STREAM_HEADER | - MAIL_FETCH_STREAM_BODY, NULL, - dec2str(msgnum+1), FALSE); + memset(&search_arg, 0, sizeof(search_arg)); + search_arg.type = SEARCH_SET; + search_arg.value.str = dec2str(msgnum+1); + + ctx = client->mailbox->search_init(client->mailbox, NULL, + &search_arg, NULL, + MAIL_FETCH_STREAM_HEADER | + MAIL_FETCH_STREAM_BODY, NULL); if (ctx == NULL) { client_send_storage_error(client); return; } - mail = client->mailbox->fetch_next(ctx); + mail = client->mailbox->search_next(ctx); stream = mail == NULL ? NULL : mail->get_stream(mail, NULL, NULL); if (stream == NULL) client_send_line(client, "-ERR Message not found."); @@ -284,7 +290,7 @@ static void fetch(struct client *client, unsigned int msgnum, client_send_line(client, "."); } - (void)client->mailbox->fetch_deinit(ctx, NULL); + (void)client->mailbox->search_deinit(ctx, NULL); } static int cmd_retr(struct client *client, const char *args) @@ -333,33 +339,37 @@ static int cmd_top(struct client *client, const char *args) static void list_uids(struct client *client, unsigned int message) { - struct mail_fetch_context *ctx; + struct mail_search_arg search_arg; + struct mail_search_context *ctx; struct mail *mail; - const char *messageset; int found = FALSE; if (client->messages_count == 0 && message == 0) return; - messageset = message == 0 ? - t_strdup_printf("1:%u", client->messages_count) : - t_strdup_printf("%u", message); + memset(&search_arg, 0, sizeof(search_arg)); + if (message == 0) + search_arg.type = SEARCH_ALL; + else { + search_arg.type = SEARCH_SET; + search_arg.value.str = dec2str(message); + } - ctx = client->mailbox->fetch_init(client->mailbox, 0, NULL, - messageset, FALSE); + ctx = client->mailbox->search_init(client->mailbox, NULL, + &search_arg, NULL, 0, NULL); if (ctx == NULL) { client_send_storage_error(client); return; } - while ((mail = client->mailbox->fetch_next(ctx)) != NULL) { + while ((mail = client->mailbox->search_next(ctx)) != NULL) { client_send_line(client, message == 0 ? "%u %u.%u" : "+OK %u %u.%u", mail->seq, client->uidvalidity, mail->uid); found = TRUE; } - (void)client->mailbox->fetch_deinit(ctx, NULL); + (void)client->mailbox->search_deinit(ctx, NULL); if (!found && message != 0) client_send_line(client, "-ERR Message not found.");