mail-index-sync-keywords.c revision 76b43e4417bab52e913da39b5f5bc2a130d3f149
429N/A/* Copyright (c) 2004-2008 Dovecot authors, see the included COPYING file */
429N/A
429N/A#include "lib.h"
429N/A#include "array.h"
429N/A#include "buffer.h"
429N/A#include "mail-index-view-private.h"
429N/A#include "mail-index-sync-private.h"
429N/A#include "mail-transaction-log.h"
429N/A
429N/Astatic bool
429N/Akeyword_lookup(struct mail_index_sync_map_ctx *ctx,
429N/A const char *keyword_name, unsigned int *idx_r)
429N/A{
429N/A struct mail_index_map *map = ctx->view->map;
429N/A const unsigned int *idx_map;
429N/A unsigned int i, count, keyword_idx;
429N/A
429N/A if (array_is_created(&map->keyword_idx_map) &&
429N/A mail_index_keyword_lookup(ctx->view->index, keyword_name,
429N/A &keyword_idx)) {
429N/A /* FIXME: slow. maybe create index -> file mapping as well */
941N/A idx_map = array_get(&map->keyword_idx_map, &count);
941N/A for (i = 0; i < count; i++) {
3158N/A if (idx_map[i] == keyword_idx) {
941N/A *idx_r = i;
429N/A return TRUE;
429N/A }
429N/A }
429N/A }
1516N/A return FALSE;
546N/A}
429N/A
429N/Astatic buffer_t *
429N/Akeywords_get_header_buf(struct mail_index_map *map,
429N/A const struct mail_index_ext *ext,
1026N/A unsigned int new_count, unsigned int *keywords_count_r,
429N/A size_t *rec_offset_r, size_t *name_offset_root_r,
941N/A size_t *name_offset_r)
941N/A{
941N/A buffer_t *buf;
546N/A const struct mail_index_keyword_header *kw_hdr;
941N/A const struct mail_index_keyword_header_rec *kw_rec;
546N/A const char *name;
546N/A struct mail_index_keyword_header new_kw_hdr;
1054N/A uint32_t offset;
546N/A
516N/A kw_hdr = CONST_PTR_OFFSET(map->hdr_base, ext->hdr_offset);
429N/A kw_rec = (const void *)(kw_hdr + 1);
429N/A name = (const char *)(kw_rec + kw_hdr->keywords_count);
429N/A
429N/A if (kw_hdr->keywords_count == 0)
429N/A return NULL;
429N/A
1100N/A i_assert((size_t)(name - (const char *)kw_hdr) < ext->hdr_size);
429N/A
429N/A new_kw_hdr = *kw_hdr;
429N/A new_kw_hdr.keywords_count += new_count;
429N/A *keywords_count_r = new_kw_hdr.keywords_count;
429N/A
429N/A offset = kw_rec[kw_hdr->keywords_count-1].name_offset;
429N/A offset += strlen(name + offset) + 1;
441N/A
441N/A buf = buffer_create_dynamic(pool_datastack_create(), 512);
441N/A buffer_append(buf, &new_kw_hdr, sizeof(new_kw_hdr));
441N/A buffer_append(buf, kw_rec, sizeof(*kw_rec) * kw_hdr->keywords_count);
441N/A *rec_offset_r = buf->used;
429N/A buffer_write(buf, buf->used + sizeof(*kw_rec) * new_count,
429N/A name, offset);
429N/A *name_offset_root_r = buf->used;
429N/A *name_offset_r = offset;
429N/A return buf;
429N/A}
429N/A
429N/Astatic void keywords_ext_register(struct mail_index_sync_map_ctx *ctx,
429N/A uint32_t ext_map_idx, uint32_t reset_id,
429N/A uint32_t hdr_size, uint32_t keywords_count)
429N/A{
429N/A buffer_t *ext_intro_buf;
429N/A struct mail_transaction_ext_intro *u;
429N/A
429N/A i_assert(keywords_count > 0);
429N/A
429N/A ext_intro_buf =
429N/A buffer_create_static_hard(pool_datastack_create(),
429N/A sizeof(*u) + sizeof("keywords")-1);
429N/A
429N/A u = buffer_append_space_unsafe(ext_intro_buf, sizeof(*u));
441N/A u->ext_id = ext_map_idx;
429N/A u->reset_id = reset_id;
546N/A u->hdr_size = hdr_size;
429N/A u->record_size = (keywords_count + CHAR_BIT - 1) / CHAR_BIT;
429N/A if ((u->record_size % 4) != 0) {
429N/A /* since we aren't properly aligned anyway,
429N/A reserve one extra byte for future */
429N/A u->record_size++;
429N/A }
429N/A u->record_align = 1;
429N/A
429N/A if (ext_map_idx == (uint32_t)-1) {
429N/A u->name_size = strlen("keywords");
429N/A buffer_append(ext_intro_buf, "keywords", u->name_size);
429N/A }
429N/A
429N/A ctx->internal_update = TRUE;
441N/A if (mail_index_sync_ext_intro(ctx, u) < 0)
3171N/A i_panic("Keyword extension growing failed");
429N/A ctx->internal_update = FALSE;
429N/A}
429N/A
429N/Astatic void
429N/Akeywords_header_add(struct mail_index_sync_map_ctx *ctx,
429N/A const char *keyword_name, unsigned int *keyword_idx_r)
429N/A{
429N/A struct mail_index_map *map;
429N/A const struct mail_index_ext *ext = NULL;
441N/A struct mail_index_keyword_header *kw_hdr;
429N/A struct mail_index_keyword_header_rec kw_rec;
429N/A uint32_t ext_map_idx;
429N/A buffer_t *buf = NULL;
429N/A size_t keyword_len, rec_offset, name_offset, name_offset_root;
429N/A unsigned int keywords_count;
429N/A
429N/A /* if we crash in the middle of writing the header, the
429N/A keywords are more or less corrupted. avoid that by
429N/A making sure the header is updated atomically. */
429N/A map = mail_index_sync_get_atomic_map(ctx);
429N/A
429N/A if (!mail_index_map_lookup_ext(map, "keywords", &ext_map_idx))
429N/A ext_map_idx = (uint32_t)-1;
429N/A else {
429N/A /* update existing header */
429N/A ext = array_idx(&map->extensions, ext_map_idx);
429N/A buf = keywords_get_header_buf(map, ext, 1, &keywords_count,
429N/A &rec_offset, &name_offset_root,
429N/A &name_offset);
429N/A }
429N/A
429N/A if (buf == NULL) {
429N/A /* create new / replace broken header */
429N/A buf = buffer_create_dynamic(pool_datastack_create(), 512);
429N/A kw_hdr = buffer_append_space_unsafe(buf, sizeof(*kw_hdr));
429N/A kw_hdr->keywords_count = 1;
429N/A
1230N/A keywords_count = kw_hdr->keywords_count;
1230N/A rec_offset = buf->used;
429N/A name_offset_root = rec_offset +
429N/A kw_hdr->keywords_count * sizeof(kw_rec);
429N/A name_offset = 0;
429N/A }
429N/A
429N/A /* add the keyword */
429N/A memset(&kw_rec, 0, sizeof(kw_rec));
429N/A kw_rec.name_offset = name_offset;
429N/A
429N/A keyword_len = strlen(keyword_name) + 1;
429N/A buffer_write(buf, rec_offset, &kw_rec, sizeof(kw_rec));
1230N/A buffer_write(buf, name_offset_root, keyword_name, keyword_len);
1230N/A
1230N/A rec_offset += sizeof(kw_rec);
1230N/A kw_rec.name_offset += keyword_len;
1230N/A name_offset_root += keyword_len;
429N/A
429N/A if ((buf->used % 4) != 0)
429N/A buffer_append_zero(buf, 4 - (buf->used % 4));
429N/A
941N/A if (ext == NULL || buf->used > ext->hdr_size ||
941N/A (uint32_t)ext->record_size * CHAR_BIT < keywords_count) {
941N/A /* if we need to grow the buffer, add some padding */
429N/A buffer_append_zero(buf, 128);
429N/A keywords_ext_register(ctx, ext_map_idx,
429N/A ext == NULL ? 0 : ext->reset_id,
429N/A buf->used, keywords_count);
1230N/A
429N/A /* map may have changed */
429N/A map = ctx->view->map;
429N/A
429N/A if (!mail_index_map_lookup_ext(map, "keywords", &ext_map_idx))
429N/A i_unreached();
429N/A ext = array_idx(&map->extensions, ext_map_idx);
429N/A
429N/A i_assert(ext->hdr_size == buf->used);
429N/A }
429N/A
429N/A buffer_copy(map->hdr_copy_buf, ext->hdr_offset, buf, 0, buf->used);
429N/A map->hdr_base = map->hdr_copy_buf->data;
429N/A
429N/A if (mail_index_map_parse_keywords(map) < 0)
429N/A i_panic("Keyword update corrupted keywords header");
429N/A
429N/A *keyword_idx_r = keywords_count - 1;
429N/A i_assert(*keyword_idx_r / CHAR_BIT < ext->record_size);
429N/A}
429N/A
429N/Astatic int
1230N/Akeywords_update_records(struct mail_index_sync_map_ctx *ctx,
1230N/A const struct mail_index_ext *ext,
429N/A unsigned int keyword_idx, enum modify_type type,
429N/A uint32_t uid1, uint32_t uid2)
1230N/A{
1230N/A struct mail_index_view *view = ctx->view;
1230N/A struct mail_index_record *rec;
1230N/A unsigned char *data, data_mask;
429N/A unsigned int data_offset;
429N/A uint32_t seq1, seq2;
429N/A
429N/A i_assert(keyword_idx != (unsigned int)-1);
429N/A
429N/A if (!mail_index_lookup_seq_range(view, uid1, uid2, &seq1, &seq2))
429N/A return 1;
429N/A
429N/A mail_index_sync_write_seq_update(ctx, seq1, seq2);
429N/A
429N/A data_offset = keyword_idx / CHAR_BIT;
429N/A data_mask = 1 << (keyword_idx % CHAR_BIT);
429N/A
429N/A i_assert(data_offset < ext->record_size);
1100N/A data_offset += ext->record_offset;
429N/A
1100N/A i_assert(data_offset >= sizeof(struct mail_index_record));
1100N/A
1100N/A switch (type) {
1100N/A case MODIFY_ADD:
1100N/A for (seq1--; seq1 < seq2; seq1++) {
1100N/A rec = MAIL_INDEX_MAP_IDX(view->map, seq1);
1100N/A data = PTR_OFFSET(rec, data_offset);
1100N/A *data |= data_mask;
1100N/A }
429N/A break;
1751N/A case MODIFY_REMOVE:
1751N/A data_mask = ~data_mask;
429N/A for (seq1--; seq1 < seq2; seq1++) {
429N/A rec = MAIL_INDEX_MAP_IDX(view->map, seq1);
429N/A data = PTR_OFFSET(rec, data_offset);
429N/A *data &= data_mask;
429N/A }
429N/A break;
429N/A default:
429N/A i_unreached();
429N/A }
429N/A return 1;
429N/A}
429N/A
429N/Aint mail_index_sync_keywords(struct mail_index_sync_map_ctx *ctx,
429N/A const struct mail_transaction_header *hdr,
429N/A const struct mail_transaction_keyword_update *rec)
429N/A{
429N/A struct mail_index_view *view = ctx->view;
429N/A const char *keyword_name;
429N/A const struct mail_index_ext *ext;
429N/A const uint32_t *uid, *end;
429N/A uint32_t seqset_offset, ext_map_idx;
429N/A unsigned int keyword_idx;
429N/A int ret;
429N/A
429N/A seqset_offset = sizeof(*rec) + rec->name_size;
429N/A if ((seqset_offset % 4) != 0)
1751N/A seqset_offset += 4 - (seqset_offset % 4);
1751N/A i_assert(seqset_offset < hdr->size);
1751N/A
1751N/A uid = CONST_PTR_OFFSET(rec, seqset_offset);
429N/A end = CONST_PTR_OFFSET(rec, hdr->size);
941N/A
1751N/A keyword_name = t_strndup(rec + 1, rec->name_size);
429N/A if (*keyword_name == '\0') {
1751N/A mail_index_sync_set_corrupted(ctx,
1751N/A "Trying to use empty keyword");
1751N/A return -1;
1751N/A }
1751N/A if (!keyword_lookup(ctx, keyword_name, &keyword_idx))
1751N/A keywords_header_add(ctx, keyword_name, &keyword_idx);
1751N/A
1751N/A /* if the keyword wasn't found, the "keywords" extension was created.
1751N/A if it was found, the record size should already be correct, but
1751N/A in case it isn't just fix it ourself. */
1751N/A if (!mail_index_map_lookup_ext(view->map, "keywords", &ext_map_idx))
1751N/A i_unreached();
1751N/A
1751N/A ext = array_idx(&view->map->extensions, ext_map_idx);
1751N/A if (keyword_idx / CHAR_BIT >= ext->record_size) {
1751N/A if (rec->modify_type == MODIFY_REMOVE) {
1751N/A /* nothing to do */
1751N/A return 1;
1751N/A }
1751N/A
1751N/A /* grow the record size */
1751N/A keywords_ext_register(ctx, ext_map_idx, ext->reset_id,
1751N/A ext->hdr_size,
1751N/A array_count(&view->map->keyword_idx_map));
1751N/A if (!mail_index_map_lookup_ext(view->map, "keywords",
1751N/A &ext_map_idx))
1751N/A i_unreached();
1751N/A ext = array_idx(&view->map->extensions, ext_map_idx);
1751N/A }
1751N/A
1751N/A while (uid+2 <= end) {
429N/A ret = keywords_update_records(ctx, ext, keyword_idx,
429N/A rec->modify_type,
1026N/A uid[0], uid[1]);
1026N/A if (ret <= 0)
1026N/A return ret;
1026N/A
1026N/A uid += 2;
1026N/A }
1026N/A
1026N/A return 1;
1026N/A}
1026N/A
1026N/Aint
1751N/Amail_index_sync_keywords_reset(struct mail_index_sync_map_ctx *ctx,
1751N/A const struct mail_transaction_header *hdr,
1751N/A const struct mail_transaction_keyword_reset *r)
1751N/A{
1100N/A struct mail_index_map *map = ctx->view->map;
1751N/A struct mail_index_record *rec;
1751N/A const struct mail_index_ext *ext;
1100N/A const struct mail_transaction_keyword_reset *end;
1100N/A uint32_t ext_map_idx, seq1, seq2;
1751N/A
1751N/A if (!mail_index_map_lookup_ext(map, "keywords", &ext_map_idx)) {
1751N/A /* nothing to do */
1751N/A return 1;
1751N/A }
1100N/A
1751N/A ext = array_idx(&map->extensions, ext_map_idx);
3158N/A end = CONST_PTR_OFFSET(r, hdr->size);
1751N/A for (; r != end; r++) {
1751N/A if (!mail_index_lookup_seq_range(ctx->view, r->uid1, r->uid2,
3158N/A &seq1, &seq2))
1751N/A continue;
1751N/A
3158N/A mail_index_sync_write_seq_update(ctx, seq1, seq2);
1751N/A for (seq1--; seq1 < seq2; seq1++) {
1751N/A rec = MAIL_INDEX_MAP_IDX(map, seq1);
3158N/A memset(PTR_OFFSET(rec, ext->record_offset),
1751N/A 0, ext->record_size);
1751N/A }
1751N/A }
3158N/A return 1;
1751N/A}
1751N/A