From 213a25b3a96d49c09df50248fd8d71d6faa6c9b8 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Tue, 29 Jun 2010 19:34:16 +0100 Subject: [PATCH] single-dbox: Simplified syncing code. --HG-- branch : HEAD --- src/lib-storage/index/dbox-single/Makefile.am | 1 - .../index/dbox-single/sdbox-sync-file.c | 66 -------- .../index/dbox-single/sdbox-sync.c | 145 ++++++++---------- .../index/dbox-single/sdbox-sync.h | 14 -- 4 files changed, 63 insertions(+), 163 deletions(-) delete mode 100644 src/lib-storage/index/dbox-single/sdbox-sync-file.c diff --git a/src/lib-storage/index/dbox-single/Makefile.am b/src/lib-storage/index/dbox-single/Makefile.am index d810f5b183..04c29bcdfa 100644 --- a/src/lib-storage/index/dbox-single/Makefile.am +++ b/src/lib-storage/index/dbox-single/Makefile.am @@ -17,7 +17,6 @@ libstorage_dbox_single_la_SOURCES = \ sdbox-mail.c \ sdbox-save.c \ sdbox-sync.c \ - sdbox-sync-file.c \ sdbox-sync-rebuild.c \ sdbox-storage.c diff --git a/src/lib-storage/index/dbox-single/sdbox-sync-file.c b/src/lib-storage/index/dbox-single/sdbox-sync-file.c deleted file mode 100644 index d5fcd18fbb..0000000000 --- a/src/lib-storage/index/dbox-single/sdbox-sync-file.c +++ /dev/null @@ -1,66 +0,0 @@ -/* Copyright (c) 2007-2010 Dovecot authors, see the included COPYING file */ - -#include "lib.h" -#include "array.h" -#include "istream.h" -#include "ostream.h" -#include "str.h" -#include "hex-binary.h" -#include "sdbox-storage.h" -#include "sdbox-file.h" -#include "sdbox-sync.h" - -#include - -static void -dbox_sync_file_move_if_needed(struct dbox_file *file, - enum sdbox_sync_entry_type type) -{ - bool move_to_alt = type == SDBOX_SYNC_ENTRY_TYPE_MOVE_TO_ALT; - bool deleted; - - if (move_to_alt != dbox_file_is_in_alt(file)) { - /* move the file. if it fails, nothing broke so - don't worry about it. */ - if (dbox_file_open(file, &deleted) > 0 && !deleted) - (void)dbox_file_move(file, move_to_alt); - } -} - -static void -dbox_sync_mark_single_file_expunged(struct sdbox_sync_context *ctx, - const struct sdbox_sync_file_entry *entry) -{ - struct mailbox *box = &ctx->mbox->box; - uint32_t seq; - - mail_index_lookup_seq(ctx->sync_view, entry->uid, &seq); - mail_index_expunge(ctx->trans, seq); - - if (box->v.sync_notify != NULL) - box->v.sync_notify(box, entry->uid, MAILBOX_SYNC_TYPE_EXPUNGE); -} - -int sdbox_sync_file(struct sdbox_sync_context *ctx, - const struct sdbox_sync_file_entry *entry) -{ - struct sdbox_mailbox *mbox = ctx->mbox; - struct dbox_file *file; - int ret = 1; - - file = sdbox_file_init(mbox, entry->uid); - switch (entry->type) { - case SDBOX_SYNC_ENTRY_TYPE_EXPUNGE: - if (dbox_file_unlink(file) >= 0) { - dbox_sync_mark_single_file_expunged(ctx, entry); - ret = 1; - } - break; - case SDBOX_SYNC_ENTRY_TYPE_MOVE_FROM_ALT: - case SDBOX_SYNC_ENTRY_TYPE_MOVE_TO_ALT: - dbox_sync_file_move_if_needed(file, entry->type); - break; - } - dbox_file_unref(&file); - return ret; -} diff --git a/src/lib-storage/index/dbox-single/sdbox-sync.c b/src/lib-storage/index/dbox-single/sdbox-sync.c index 62b93c68a2..2b599d4308 100644 --- a/src/lib-storage/index/dbox-single/sdbox-sync.c +++ b/src/lib-storage/index/dbox-single/sdbox-sync.c @@ -1,91 +1,101 @@ /* Copyright (c) 2007-2010 Dovecot authors, see the included COPYING file */ #include "lib.h" -#include "array.h" -#include "ioloop.h" -#include "str.h" -#include "hash.h" #include "sdbox-storage.h" #include "sdbox-file.h" #include "sdbox-sync.h" #define SDBOX_REBUILD_COUNT 3 -static unsigned int sdbox_sync_file_entry_hash(const void *p) +static void +dbox_sync_file_move_if_needed(struct dbox_file *file, + enum sdbox_sync_entry_type type) { - const struct sdbox_sync_file_entry *entry = p; - - return entry->uid; + bool move_to_alt = type == SDBOX_SYNC_ENTRY_TYPE_MOVE_TO_ALT; + bool deleted; + + if (move_to_alt != dbox_file_is_in_alt(file)) { + /* move the file. if it fails, nothing broke so + don't worry about it. */ + if (dbox_file_open(file, &deleted) > 0 && !deleted) + (void)dbox_file_move(file, move_to_alt); + } } -static int sdbox_sync_file_entry_cmp(const void *p1, const void *p2) +static void dbox_sync_file_expunge(struct sdbox_sync_context *ctx, + struct dbox_file *file, uint32_t seq) { - const struct sdbox_sync_file_entry *entry1 = p1, *entry2 = p2; + struct sdbox_file *sfile = (struct sdbox_file *)file; + struct mailbox *box = &ctx->mbox->box; - /* this is only for hashing, don't bother ever returning 1. */ - if (entry1->uid != entry2->uid) - return -1; - return 0; + if (mail_index_transaction_is_expunged(ctx->trans, seq)) { + /* already expunged within this transaction */ + return; + } + if (dbox_file_unlink(file) < 0) { + /* some non-ENOENT error trying to unlink the file */ + return; + } + + /* file was either unlinked by us or someone else */ + mail_index_expunge(ctx->trans, seq); + if (box->v.sync_notify != NULL) + box->v.sync_notify(box, sfile->uid, MAILBOX_SYNC_TYPE_EXPUNGE); } -static int sdbox_sync_add_seq(struct sdbox_sync_context *ctx, - const struct mail_index_sync_rec *sync_rec, - uint32_t seq) +static void sdbox_sync_file(struct sdbox_sync_context *ctx, + uint32_t seq, uint32_t uid, + enum sdbox_sync_entry_type type) { - struct sdbox_sync_file_entry *entry, lookup_entry; - - i_assert(sync_rec->type == MAIL_INDEX_SYNC_TYPE_EXPUNGE || - sync_rec->type == MAIL_INDEX_SYNC_TYPE_FLAGS); - - memset(&lookup_entry, 0, sizeof(lookup_entry)); - mail_index_lookup_uid(ctx->sync_view, seq, &lookup_entry.uid); - - entry = hash_table_lookup(ctx->syncs, &lookup_entry); - if (entry == NULL) { - entry = p_new(ctx->pool, struct sdbox_sync_file_entry, 1); - *entry = lookup_entry; - hash_table_insert(ctx->syncs, entry, entry); + struct dbox_file *file; + + file = sdbox_file_init(ctx->mbox, uid); + switch (type) { + case SDBOX_SYNC_ENTRY_TYPE_EXPUNGE: + dbox_sync_file_expunge(ctx, file, seq); + break; + case SDBOX_SYNC_ENTRY_TYPE_MOVE_FROM_ALT: + case SDBOX_SYNC_ENTRY_TYPE_MOVE_TO_ALT: + dbox_sync_file_move_if_needed(file, type); + break; } - - if (sync_rec->type == MAIL_INDEX_SYNC_TYPE_EXPUNGE) - entry->type = SDBOX_SYNC_ENTRY_TYPE_EXPUNGE; - else if ((sync_rec->add_flags & DBOX_INDEX_FLAG_ALT) != 0) - entry->type = SDBOX_SYNC_ENTRY_TYPE_MOVE_TO_ALT; - else - entry->type = SDBOX_SYNC_ENTRY_TYPE_MOVE_FROM_ALT; - return 1; + dbox_file_unref(&file); } -static int sdbox_sync_add(struct sdbox_sync_context *ctx, - const struct mail_index_sync_rec *sync_rec) +static void sdbox_sync_add(struct sdbox_sync_context *ctx, + const struct mail_index_sync_rec *sync_rec) { + uint32_t uid; + enum sdbox_sync_entry_type type; uint32_t seq, seq1, seq2; - int ret; if (sync_rec->type == MAIL_INDEX_SYNC_TYPE_EXPUNGE) { /* we're interested */ + type = SDBOX_SYNC_ENTRY_TYPE_EXPUNGE; } else if (sync_rec->type == MAIL_INDEX_SYNC_TYPE_FLAGS) { /* we care only about alt flag changes */ - if ((sync_rec->add_flags & DBOX_INDEX_FLAG_ALT) == 0 && - (sync_rec->remove_flags & DBOX_INDEX_FLAG_ALT) == 0) - return 1; + if ((sync_rec->add_flags & DBOX_INDEX_FLAG_ALT) != 0) + type = SDBOX_SYNC_ENTRY_TYPE_MOVE_TO_ALT; + else if ((sync_rec->remove_flags & DBOX_INDEX_FLAG_ALT) != 0) + type = SDBOX_SYNC_ENTRY_TYPE_MOVE_FROM_ALT; + else + return; } else { /* not interested */ - return 1; + return; } if (!mail_index_lookup_seq_range(ctx->sync_view, sync_rec->uid1, sync_rec->uid2, &seq1, &seq2)) { /* already expunged everything. nothing to do. */ - return 1; + return; } for (seq = seq1; seq <= seq2; seq++) { - if ((ret = sdbox_sync_add_seq(ctx, sync_rec, seq)) <= 0) - return ret; + mail_index_lookup_uid(ctx->sync_view, seq, &uid); + sdbox_sync_file(ctx, seq, uid, type); } - return 1; } static int sdbox_sync_index(struct sdbox_sync_context *ctx) @@ -93,8 +103,6 @@ static int sdbox_sync_index(struct sdbox_sync_context *ctx) struct mailbox *box = &ctx->mbox->box; const struct mail_index_header *hdr; struct mail_index_sync_rec sync_rec; - struct hash_iterate_context *iter; - void *key, *value; uint32_t seq1, seq2; int ret = 1; @@ -106,39 +114,14 @@ static int sdbox_sync_index(struct sdbox_sync_context *ctx) /* mark the newly seen messages as recent */ if (mail_index_lookup_seq_range(ctx->sync_view, hdr->first_recent_uid, - hdr->next_uid, &seq1, &seq2)) { - index_mailbox_set_recent_seq(&ctx->mbox->box, ctx->sync_view, - seq1, seq2); - } - - /* read all changes and group changes to same file_id together */ - ctx->pool = pool_alloconly_create("dbox sync pool", 1024*32); - ctx->syncs = hash_table_create(default_pool, ctx->pool, 0, - sdbox_sync_file_entry_hash, - sdbox_sync_file_entry_cmp); - - while (mail_index_sync_next(ctx->index_sync_ctx, &sync_rec)) { - if ((ret = sdbox_sync_add(ctx, &sync_rec)) <= 0) - break; - } - - if (ret > 0) { - /* now sync each file separately */ - iter = hash_table_iterate_init(ctx->syncs); - while (hash_table_iterate(iter, &key, &value)) { - const struct sdbox_sync_file_entry *entry = value; + hdr->next_uid, &seq1, &seq2)) + index_mailbox_set_recent_seq(box, ctx->sync_view, seq1, seq2); - if ((ret = sdbox_sync_file(ctx, entry)) <= 0) - break; - } - hash_table_iterate_deinit(&iter); - } + while (mail_index_sync_next(ctx->index_sync_ctx, &sync_rec)) + sdbox_sync_add(ctx, &sync_rec); if (box->v.sync_notify != NULL) box->v.sync_notify(box, 0, 0); - - hash_table_destroy(&ctx->syncs); - pool_unref(&ctx->pool); return ret; } @@ -247,8 +230,6 @@ int sdbox_sync_finish(struct sdbox_sync_context **_ctx, bool success) } else { mail_index_sync_rollback(&ctx->index_sync_ctx); } - if (ctx->path != NULL) - str_free(&ctx->path); i_free(ctx); return ret; diff --git a/src/lib-storage/index/dbox-single/sdbox-sync.h b/src/lib-storage/index/dbox-single/sdbox-sync.h index a9ec916182..10116674ae 100644 --- a/src/lib-storage/index/dbox-single/sdbox-sync.h +++ b/src/lib-storage/index/dbox-single/sdbox-sync.h @@ -16,23 +16,12 @@ enum sdbox_sync_entry_type { SDBOX_SYNC_ENTRY_TYPE_MOVE_TO_ALT }; -struct sdbox_sync_file_entry { - uint32_t uid; - enum sdbox_sync_entry_type type; -}; - struct sdbox_sync_context { struct sdbox_mailbox *mbox; struct mail_index_sync_ctx *index_sync_ctx; struct mail_index_view *sync_view; struct mail_index_transaction *trans; enum sdbox_sync_flags flags; - - string_t *path; - unsigned int path_dir_prefix_len; - - pool_t pool; - struct hash_table *syncs; /* struct sdbox_sync_file_entry */ }; int sdbox_sync_begin(struct sdbox_mailbox *mbox, enum sdbox_sync_flags flags, @@ -40,9 +29,6 @@ int sdbox_sync_begin(struct sdbox_mailbox *mbox, enum sdbox_sync_flags flags, int sdbox_sync_finish(struct sdbox_sync_context **ctx, bool success); int sdbox_sync(struct sdbox_mailbox *mbox, enum sdbox_sync_flags flags); -int sdbox_sync_file(struct sdbox_sync_context *ctx, - const struct sdbox_sync_file_entry *entry); - int sdbox_sync_index_rebuild(struct sdbox_mailbox *mbox); struct mailbox_sync_context * -- 2.47.3