mail-index-sync-ext.c revision 9e3c9d98927a1244c786266c5f2b8467f566b2b7
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (c) 2004-2009 Dovecot authors, see the included COPYING file */
fc71e94957d0c2959a609450a2f303640d681858Sascha Wildevoid mail_index_sync_init_expunge_handlers(struct mail_index_sync_map_ctx *ctx)
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen if (!array_is_created(&ctx->view->map->extensions))
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen rext = array_get(&ctx->view->index->extensions, &rext_count);
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde ext = array_get(&ctx->view->map->extensions, &ext_count);
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde id_map = array_get(&ctx->view->map->ext_id_map, &id_map_count);
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde contexts = array_get_modifiable(&ctx->extra_contexts, &context_count);
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde for (idx_ext_id = 0; idx_ext_id < rext_count; idx_ext_id++) {
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen map_ext_id = idx_ext_id >= id_map_count ? (uint32_t)-1 :
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen if (rext[idx_ext_id].expunge_handler == NULL ||
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen !rext[idx_ext_id].expunge_handler_call_always))
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen eh.context = rext[idx_ext_id].expunge_context;
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde eh.record_offset = map_ext_id == (uint32_t)-1 ? 0 :
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainenmail_index_sync_deinit_expunge_handlers(struct mail_index_sync_map_ctx *ctx)
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen unsigned int i, count;
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen if (!array_is_created(&ctx->expunge_handlers))
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen eh = array_get(&ctx->expunge_handlers, &count);
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen for (i = 0; i < count; i++) {
64bfe7b4a42512971db154937905dfa2bdb9cf2cTimo Sirainen eh[i].handler(ctx, 0, NULL, eh[i].sync_context,
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainenvoid mail_index_sync_init_handlers(struct mail_index_sync_map_ctx *ctx)
c5a6a6565be93224fc26522eda855b0990f256e8Timo Sirainen unsigned int count;
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde if (!array_is_created(&ctx->view->map->extensions))
64bfe7b4a42512971db154937905dfa2bdb9cf2cTimo Sirainen /* set space for extra contexts */
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde count = array_count(&ctx->view->index->extensions);
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen /* make sure the extra_contexts contains everything */
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen (void)array_idx_modifiable(&ctx->extra_contexts, count - 1);
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde /* we need to update the expunge handler list in case they had
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen already been called */
64bfe7b4a42512971db154937905dfa2bdb9cf2cTimo Sirainenvoid mail_index_sync_deinit_handlers(struct mail_index_sync_map_ctx *ctx)
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde rext = array_get(&ctx->view->index->extensions, &rext_count);
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen array_get_modifiable(&ctx->extra_contexts, &context_count);
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen for (i = 0; i < context_count; i++) {
64bfe7b4a42512971db154937905dfa2bdb9cf2cTimo Sirainen rext[i].sync_handler.callback(ctx, 0, NULL, NULL,
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainenget_ext_header(struct mail_index_map *map, const struct mail_index_ext *ext)
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen /* do some kludgy jumping to get to it. */
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen hdr_base = buffer_get_modifiable_data(map->hdr_copy_buf, NULL);
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen ext_hdr = PTR_OFFSET(hdr_base, ext->ext_offset);
fc71e94957d0c2959a609450a2f303640d681858Sascha Wildestatic int mail_index_ext_align_cmp(const void *p1, const void *p2)
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen const struct mail_index_ext *const *e1 = p1, *const *e2 = p2;
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen return (int)(*e2)->record_align - (int)(*e1)->record_align;
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainenstatic void sync_ext_reorder(struct mail_index_map *map, uint32_t ext_map_idx,
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen uint16_t *old_offsets, *copy_sizes, min_align, max_align;
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen unsigned int i, count;
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen const void *src;
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen i_assert(MAIL_INDEX_MAP_IS_IN_MEMORY(map) && map->refcount == 1);
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen ext = array_get_modifiable(&map->extensions, &count);
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen /* @UNSAFE */
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen sorted = t_new(struct mail_index_ext *, count);
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen for (i = 0; i < count; i++) {
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen qsort(sorted, count, sizeof(struct mail_index_ext *),
2ba63f475f74b2aa87f9fd9e28a6c5738deb0878Timo Sirainen /* we are growing the extension record. remember this
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen so we don't write extra data while copying the record */
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen /* we simply try to use the extensions with largest alignment
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen requirement first. FIXME: if the extension sizes don't match
3b22894b8805b186c73d8b754001e8d7e944be85Timo Sirainen alignment, this may not give the minimal layout. */
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen for (i = 0; i < count; i++) {
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen /* all done */
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen /* we have to leave space here */
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen i_assert(min_align > 1 && min_align < (uint16_t)-1);
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen /* keep record size divisible with maximum alignment */
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen /* copy the records to new buffer */
64bfe7b4a42512971db154937905dfa2bdb9cf2cTimo Sirainen new_buffer_size = map->rec_map->records_count * new_record_size;
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde new_buffer = buffer_create_dynamic(default_pool, new_buffer_size);
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde for (rec_idx = 0; rec_idx < map->rec_map->records_count; rec_idx++) {
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde /* write the base record */
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde sizeof(struct mail_index_record));
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde /* write extensions */
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen for (i = 0; i < count; i++) {
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen buffer_write(new_buffer, offset + ext[i].record_offset,
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde src = CONST_PTR_OFFSET(src, map->hdr.record_size);
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde /* we didn't fully write the last record */
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde size_t space = new_buffer_size - new_buffer->used;
5c0ab4cc4dff573940df683eb4b23bd6077153faTimo Sirainen buffer_get_modifiable_data(map->rec_map->buffer, NULL);
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen /* update record offsets in headers */
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen for (i = 0; i < count; i++) {
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainensync_ext_resize(const struct mail_transaction_ext_intro *u,
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen uint32_t ext_map_idx, struct mail_index_sync_map_ctx *ctx,
9bb91f1dbf7cf8cfbd2df7784101df98d59fb46dTimo Sirainen ext = array_idx_modifiable(&map->extensions, ext_map_idx);
9bb91f1dbf7cf8cfbd2df7784101df98d59fb46dTimo Sirainen old_size = MAIL_INDEX_HEADER_SIZE_ALIGN(ext->hdr_size);
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen new_size = MAIL_INDEX_HEADER_SIZE_ALIGN(u->hdr_size);
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen /* header shrank */
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen /* header grown */
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen (ext->record_align > u->record_align && !no_shrink)) {
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen (ext->record_size > u->record_size && !no_shrink)) {
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen i_assert((map->hdr_copy_buf->used % sizeof(uint64_t)) == 0);
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen map->hdr.header_size = map->hdr_copy_buf->used;
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen map->write_base_header = map->write_ext_header = TRUE;
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen /* move all hdr_offset of all extensions after this one */
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen unsigned int i, count = array_count(&map->extensions);
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen ssize_t diff = (ssize_t)new_size - (ssize_t)old_size;
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen ext = array_idx_modifiable(&map->extensions, 0);
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen sync_ext_reorder(map, ext_map_idx, old_record_size);
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde /* header size changed. recreate index file. */
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainenmail_index_sync_ext_unknown_complain(struct mail_index_sync_map_ctx *ctx,
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen unsigned char *p;
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen /* don't try to track too high values */
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen buffer_create_dynamic(default_pool, ext_map_idx + 8);
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen p = buffer_get_space_unsafe(ctx->unknown_extensions, ext_map_idx, 1);
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainen if (*p != 0) {
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde /* we've already complained once */
aba994a4e79a020b4748e0ceffc194e5a18e1d1aTimo Sirainenmail_index_sync_ext_init_new(struct mail_index_sync_map_ctx *ctx,
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen /* be sure to get a unique mapping before we modify the extensions,
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen otherwise other map users will see the new extension but not the
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen data records that sync_ext_reorder() adds. */
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen i_assert(hdr_buf->used == map->hdr.header_size);
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen if (MAIL_INDEX_HEADER_SIZE_ALIGN(hdr_buf->used) != hdr_buf->used) {
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen /* we need to add padding between base header and extensions */
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen /* register record offset initially using zero,
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen sync_ext_reorder() will fix it. */
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen ext_map_idx = mail_index_map_register_ext(map, name, hdr_buf->used,
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen ext = array_idx(&map->extensions, ext_map_idx);
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen /* <ext_hdr> <name> [padding] [header data] */
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen buffer_append(hdr_buf, ext_hdr, sizeof(*ext_hdr));
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen buffer_append(hdr_buf, name, ext_hdr->name_size);
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen /* header must begin and end in correct alignment */
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen MAIL_INDEX_HEADER_SIZE_ALIGN(hdr_buf->used) - hdr_buf->used +
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen ext->hdr_offset + MAIL_INDEX_HEADER_SIZE_ALIGN(ext->hdr_size));
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen i_assert((hdr_buf->used % sizeof(uint64_t)) == 0);
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen i_assert(ext->record_offset != 0 || ext->record_size == 0);
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainenvoid mail_index_sync_ext_init(struct mail_index_sync_map_ctx *ctx,
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen if (!mail_index_ext_lookup(ctx->view->index, name, &ext_id))
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen rext = array_idx(&ctx->view->index->extensions, ext_id);
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen if (mail_index_map_lookup_ext(map, name, ext_map_idx_r)) {
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen /* make sure it's the expected size */
a3f5bd60d81b456b7e4b79b26a825b71b836a537Timo Sirainen memset(&u, 0, sizeof(u));
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen sync_ext_resize(&u, *ext_map_idx_r, ctx, FALSE);
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen mail_index_sync_ext_init_new(ctx, name, &ext_hdr,
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainenint mail_index_sync_ext_intro(struct mail_index_sync_map_ctx *ctx,
9261dbf0675204898c6557591c7aa376e23a52b2Timo Sirainen /* default to ignoring the following extension updates in case this
9261dbf0675204898c6557591c7aa376e23a52b2Timo Sirainen intro is corrupted */
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen u->ext_id >= array_count(&map->extensions))) {
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen if (!mail_index_sync_ext_unknown_complain(ctx, u->ext_id))
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen "Extension introduction for unknown id %u", u->ext_id);
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen if (u->ext_id == (uint32_t)-1 && u->name_size == 0) {
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen "Extension introduction without id or name");
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen if (!mail_index_map_lookup_ext(map, name, &ext_map_idx))
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen /* Keyword extension is handled internally by the keyword
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen code. Any attempt to modify them directly could cause
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen assert-crashes later, so prevent them immediately. */
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen "Extension introduction for keywords");
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen no_shrink = (u->flags & MAIL_TRANSACTION_EXT_INTRO_FLAG_NO_SHRINK) != 0;
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen /* make sure the header looks valid before doing anything with it */
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen if (mail_index_map_ext_hdr_check(&map->hdr, &ext_hdr,
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen /* exists already */
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen /* check if we need to resize anything */
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen sync_ext_resize(u, ext_map_idx, ctx, no_shrink);
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen /* extension was reset and this transaction hadn't
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen yet seen it. ignore this update (except for
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen mail_index_sync_ext_init_new(ctx, name, &ext_hdr, &ext_map_idx);
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainenstatic void mail_index_sync_ext_clear(struct mail_index_view *view,
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen memset(buffer_get_space_unsafe(map->hdr_copy_buf, ext->hdr_offset,
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen for (i = 0; i < view->map->rec_map->records_count; i++) {
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen memset(PTR_OFFSET(rec, ext->record_offset), 0,
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen map->rec_map->write_seq_last = view->map->rec_map->records_count;
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainenint mail_index_sync_ext_reset(struct mail_index_sync_map_ctx *ctx,
69d60dcff2614c4bfc8ad59e8fdc09e39c9dd0dcTimo Sirainen "Extension reset without intro prefix");
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen if (ctx->cur_ext_map_idx == (uint32_t)-2 && ctx->cur_ext_ignore) {
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen /* previous extension intro was broken */
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen /* since we're resetting the extension, don't check cur_ext_ignore */
4b1359bde7d32667197548652a4b4f540062e2acTimo Sirainen /* a new index file will be created, so the old data won't be
fc71e94957d0c2959a609450a2f303640d681858Sascha Wilde accidentally used by other processes. */
if (!u->preserve_data)
const struct mail_transaction_ext_hdr_update *u)
const struct mail_transaction_ext_rec_update *u)
void *old_data;
int ret;
void **extra_context =
if (ret <= 0)
return ret;
const struct mail_transaction_ext_atomic_inc *u)
void *data;