db.c 33.8 KB
Newer Older
Richard Curnow's avatar
Richard Curnow committed
1 2 3 4
/*
  mairix - message index builder and finder for maildir folders.

 **********************************************************************
5
 * Copyright (C) Richard P. Curnow  2002-2004
Richard Curnow's avatar
Richard Curnow committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 * 
 **********************************************************************
 */

/* Handle complete database */

#include "mairix.h"
#include "reader.h"
#include <ctype.h>
#include <assert.h>
29 30
#include <sys/time.h>
#include <unistd.h>
Richard Curnow's avatar
Richard Curnow committed
31

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
struct sortable_token {/*{{{*/
  char *text;
  int index;
};
/*}}}*/
static int compare_sortable_tokens(const void *a, const void *b)/*{{{*/
{
  const struct sortable_token *aa = (const struct sortable_token *) a;
  const struct sortable_token *bb = (const struct sortable_token *) b;
  int foo;
  foo = strcmp(aa->text, bb->text);
  if (foo) {
    return foo;
  } else {
    if (aa->index < bb->index) return -1;
    else if (aa->index > bb->index) return +1;
    else return 0;
  }
}
/*}}}*/
52
static void check_toktable_enc_integrity(int n_msgs, struct toktable *table)/*{{{*/
Richard Curnow's avatar
Richard Curnow committed
53 54 55 56 57
{
  /* FIXME : Check reachability of tokens that are displaced from their natural
   * hash bucket (if deletions have occurred during purge). */

  int idx, incr;
58
  int i, k;
Richard Curnow's avatar
Richard Curnow committed
59 60
  unsigned char *j, *last_char;
  int broken_chains = 0;
61 62
  struct sortable_token *sort_list;
  int any_duplicates;
Richard Curnow's avatar
Richard Curnow committed
63 64 65 66 67 68
  
  for (i=0; i<table->size; i++) {
    struct token *tok = table->tokens[i];
    if (tok) {
      idx = 0;
      incr = 0;
69 70
      last_char = tok->match0.msginfo + tok->match0.n;
      for (j = tok->match0.msginfo; j < last_char; ) {
Richard Curnow's avatar
Richard Curnow committed
71 72 73
        incr = read_increment(&j);
        idx += incr;
      }
74 75
      if (idx != tok->match0.highest) {
        fprintf(stderr, "broken encoding chain for token <%s>, highest=%ld\n", tok->text, tok->match0.highest);
Richard Curnow's avatar
Richard Curnow committed
76 77 78
        fflush(stderr);
        broken_chains = 1;
      }
79 80
      if (idx >= n_msgs) {
        fprintf(stderr, "end of chain higher than number of message paths (%d) for token <%s>\n", n_msgs, tok->text);
Richard Curnow's avatar
Richard Curnow committed
81 82 83 84 85 86 87 88
        fflush(stderr);
        broken_chains = 1;
      }
    }
  }

  assert(!broken_chains);

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  /* Check there are no duplicated tokens in the table. */
  sort_list = new_array(struct sortable_token, table->n);
  k = 0;
  for (i=0; i<table->size; i++) {
    struct token *tok = table->tokens[i];
    if (tok) {
      sort_list[k].text = new_string(tok->text);
      sort_list[k].index = i;
      k++;
    }
  }
  assert(k == table->n);

  qsort(sort_list, table->n, sizeof(struct sortable_token), compare_sortable_tokens);
  /* Check for uniqueness of neighbouring token texts */
  any_duplicates = 0;
  for (i=0; i<(table->n - 1); i++) {
    if (!strcmp(sort_list[i].text, sort_list[i+1].text)) {
      fprintf(stderr, "Token table contains duplicated token %s at indices %d and %d\n", 
               sort_list[i].text, sort_list[i].index, sort_list[i+1].index);
      any_duplicates = 1;
    }
  }

  /* release */
  for (i=0; i<table->n; i++) {
    free(sort_list[i].text);
  }
  free(sort_list);

  if (any_duplicates) {
    fprintf(stderr, "Token table contained duplicate entries, aborting\n");
    assert(0);
  }
Richard Curnow's avatar
Richard Curnow committed
123 124 125 126 127 128 129 130 131 132 133
}
/*}}}*/
static int compare_strings(const void *a, const void *b)/*{{{*/
{
  const char **aa = (const char **) a;
  const char **bb = (const char **) b;
  return strcmp(*aa, *bb);
}
/*}}}*/
static void check_message_path_integrity(struct database *db)/*{{{*/
{
134
  /* TODO : for now only checks integrity of non-mbox paths. */
Richard Curnow's avatar
Richard Curnow committed
135 136 137 138 139 140
  /* Check there are no duplicates */
  int i;
  int n;
  int has_duplicate = 0;
  
  char **paths;
141 142 143 144 145 146 147 148 149
  paths = new_array(char *, db->n_msgs);
  for (i=0, n=0; i<db->n_msgs; i++) {
    switch (db->type[i]) {
      case MTY_DEAD:
      case MTY_MBOX:
        break;
      case MTY_FILE:
        paths[n++] = db->msgs[i].src.mpf.path;
        break;
Richard Curnow's avatar
Richard Curnow committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    }
  }

  qsort(paths, n, sizeof(char *), compare_strings);

  for (i=1; i<n; i++) {
    if (!strcmp(paths[i-1], paths[i])) {
      fprintf(stderr, "Path <%s> repeated\n", paths[i]);
      has_duplicate = 1;
    }
  }

  fflush(stderr);
  assert(!has_duplicate);

  free(paths);
  return;
}
/*}}}*/
void check_database_integrity(struct database *db)/*{{{*/
{
  if (verbose) fprintf(stderr, "Checking message path integrity\n");
  check_message_path_integrity(db);
  
  /* Just check encoding chains for now */
  if (verbose) fprintf(stderr, "Checking to\n");
176
  check_toktable_enc_integrity(db->n_msgs, db->to);
Richard Curnow's avatar
Richard Curnow committed
177
  if (verbose) fprintf(stderr, "Checking cc\n");
178
  check_toktable_enc_integrity(db->n_msgs, db->cc);
Richard Curnow's avatar
Richard Curnow committed
179
  if (verbose) fprintf(stderr, "Checking from\n");
180
  check_toktable_enc_integrity(db->n_msgs, db->from);
Richard Curnow's avatar
Richard Curnow committed
181
  if (verbose) fprintf(stderr, "Checking subject\n");
182
  check_toktable_enc_integrity(db->n_msgs, db->subject);
Richard Curnow's avatar
Richard Curnow committed
183
  if (verbose) fprintf(stderr, "Checking body\n");
184
  check_toktable_enc_integrity(db->n_msgs, db->body);
Richard Curnow's avatar
Richard Curnow committed
185 186 187 188 189
}
/*}}}*/
struct database *new_database(void)/*{{{*/
{
  struct database *result = new(struct database);
190 191
  struct timeval tv;
  pid_t  pid;
Richard Curnow's avatar
Richard Curnow committed
192 193 194 195 196 197 198

  result->to = new_toktable();
  result->cc = new_toktable();
  result->from = new_toktable();
  result->subject = new_toktable();
  result->body = new_toktable();

199 200 201 202 203 204 205 206 207 208
  result->msg_ids = new_toktable2();

  gettimeofday(&tv, NULL);
  pid = getpid();
  result->hash_key = tv.tv_sec ^ (pid ^ (tv.tv_usec << 15));

  result->msgs = NULL;
  result->type = NULL;
  result->n_msgs = 0;
  result->max_msgs = 0;
Richard Curnow's avatar
Richard Curnow committed
209

210 211 212
  result->mboxen = NULL;
  result->n_mboxen = 0;
  result->max_mboxen = 0;
Richard Curnow's avatar
Richard Curnow committed
213 214 215 216 217 218 219 220 221 222 223 224 225

  return result;
}
/*}}}*/
void free_database(struct database *db)/*{{{*/
{
  int i;

  free_toktable(db->to);
  free_toktable(db->cc);
  free_toktable(db->from);
  free_toktable(db->subject);
  free_toktable(db->body);
226 227 228 229 230 231 232 233 234 235 236 237 238 239
  free_toktable2(db->msg_ids);

  if (db->msgs) {
    for (i=0; i<db->n_msgs; i++) {
      switch (db->type[i]) {
        case MTY_DEAD:
          break;
        case MTY_MBOX:
          break;
        case MTY_FILE:
          assert(db->msgs[i].src.mpf.path);
          free(db->msgs[i].src.mpf.path);
          break;
      }
Richard Curnow's avatar
Richard Curnow committed
240
    }
241 242
    free(db->msgs);
    free(db->type);
Richard Curnow's avatar
Richard Curnow committed
243 244 245 246 247 248 249 250 251 252
  }

  free(db);
}
/*}}}*/

static int get_max (int a, int b) {/*{{{*/
  return (a > b) ? a : b;
}
/*}}}*/
253
static void import_toktable(char *data, unsigned int hash_key, int n_msgs, struct toktable_db *in, struct toktable *out)/*{{{*/
Richard Curnow's avatar
Richard Curnow committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
{
  int n, size, i;

  n = in->n;
  size = 1;
  while (size < n) size <<= 1;
  size <<= 1; /* safe hash table size */

  out->size = size;
  out->mask = size - 1;
  out->n = n;
  out->tokens = new_array(struct token *, size);
  memset(out->tokens, 0, size * sizeof(struct token *));
  out->hwm = (n + size) >> 1;

  for (i=0; i<n; i++) {
    unsigned int hash, index;
    char *text;
    unsigned char *enc;
    int enc_len;
    struct token *nt;
    int enc_hi;
    int idx, incr;
    unsigned char *j;

    /* Recover enc_len and enc_hi from the data */
    enc = (unsigned char *) data + in->enc_offsets[i];
    idx = 0;
    for (j = enc; *j != 0xff; ) {
      incr = read_increment(&j);
      idx += incr;
    }
    enc_len = j - enc;
    enc_hi = idx;

    text = data + in->tok_offsets[i];
290
    hash = hashfn(text, strlen(text), hash_key);
Richard Curnow's avatar
Richard Curnow committed
291 292 293 294 295

    nt = new(struct token);
    nt->hashval = hash;
    nt->text = new_string(text);
    /* Allow a bit of headroom for adding more entries later */
296 297 298 299 300 301 302 303 304
    nt->match0.max = get_max(16, enc_len + (enc_len >> 1));
    nt->match0.n = enc_len;
    nt->match0.highest = enc_hi;
    assert(nt->match0.highest < n_msgs);
    nt->match0.msginfo = new_array(char, nt->match0.max);
    memcpy(nt->match0.msginfo, enc, nt->match0.n);

    index = hash & out->mask;
    while (out->tokens[index]) {
305 306 307 308 309 310 311 312 313 314 315
      /* Audit to look for corrupt database with multiple entries for the same
       * string. */
      if (!strcmp(nt->text, out->tokens[index]->text)) {
        fprintf(stderr, "\n!!! Corrupt token table found in database, token <%s> duplicated, aborting\n",
            nt->text);
        fprintf(stderr, "  Delete the database file and rebuild from scratch as a workaround\n");
        /* No point going on - need to find out why the database got corrupted
         * in the 1st place.  Workaround for user - rebuild database from
         * scratch by deleting it then rerunning. */
        exit(1);
      }
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
      ++index;
      index &= out->mask;
    }

    out->tokens[index] = nt;
  }
}
/*}}}*/
static void import_toktable2(char *data, unsigned int hash_key, int n_msgs, struct toktable2_db *in, struct toktable2 *out)/*{{{*/
{
  int n, size, i;

  n = in->n;
  size = 1;
  while (size < n) size <<= 1;
  size <<= 1; /* safe hash table size */

  out->size = size;
  out->mask = size - 1;
  out->n = n;
  out->tokens = new_array(struct token2 *, size);
  memset(out->tokens, 0, size * sizeof(struct token *));
  out->hwm = (n + size) >> 1;

  for (i=0; i<n; i++) {
    unsigned int hash, index;
    char *text;
    struct token2 *nt;
    unsigned char *enc0, *enc1;
    int enc0_len, enc1_len;
    int enc0_hi, enc1_hi;
    int idx, incr;
    unsigned char *j;

/*{{{ do enc0*/
    enc0 = (unsigned char *) data + in->enc0_offsets[i];
    idx = 0;
    for (j = enc0; *j != 0xff; ) {
      incr = read_increment(&j);
      idx += incr;
    }
    enc0_len = j - enc0;
    enc0_hi = idx;
/*}}}*/
/*{{{ do enc1*/
    enc1 = (unsigned char *) data + in->enc1_offsets[i];
    idx = 0;
    for (j = enc1; *j != 0xff; ) {
      incr = read_increment(&j);
      idx += incr;
    }
    enc1_len = j - enc1;
    enc1_hi = idx;
/*}}}*/

    text = data + in->tok_offsets[i];
    hash = hashfn(text, strlen(text), hash_key);

    nt = new(struct token2);
    nt->hashval = hash;
    nt->text = new_string(text);
    /* Allow a bit of headroom for adding more entries later */
    /*{{{ set up match0 chain */
    nt->match0.max = get_max(16, enc0_len + (enc0_len >> 1));
    nt->match0.n = enc0_len;
    nt->match0.highest = enc0_hi;
    assert(nt->match0.highest < n_msgs);
    nt->match0.msginfo = new_array(char, nt->match0.max);
    memcpy(nt->match0.msginfo, enc0, nt->match0.n);
    /*}}}*/
    /*{{{ set up match1 chain */
    nt->match1.max = get_max(16, enc1_len + (enc1_len >> 1));
    nt->match1.n = enc1_len;
    nt->match1.highest = enc1_hi;
    assert(nt->match1.highest < n_msgs);
    nt->match1.msginfo = new_array(char, nt->match1.max);
    memcpy(nt->match1.msginfo, enc1, nt->match1.n);
    /*}}}*/
Richard Curnow's avatar
Richard Curnow committed
394 395 396 397 398 399 400 401 402 403 404

    index = hash & out->mask;
    while (out->tokens[index]) {
      ++index;
      index &= out->mask;
    }

    out->tokens[index] = nt;
  }
}
/*}}}*/
405
struct database *new_database_from_file(char *db_filename, int do_integrity_checks)/*{{{*/
Richard Curnow's avatar
Richard Curnow committed
406 407 408 409
{ 
  /* Read existing database from file for doing incremental update */
  struct database *result;
  struct read_db *input;
410
  int i, n, N;
Richard Curnow's avatar
Richard Curnow committed
411 412 413
  
  result = new_database();
  input = open_db(db_filename);
Richard Curnow's avatar
Richard Curnow committed
414 415 416 417 418
  if (!input) {
    /* Nothing to initialise */
    if (verbose) printf("Database file was empty, creating a new database\n");
    return result;
  }
Richard Curnow's avatar
Richard Curnow committed
419 420

  /* Build pathname information */
421 422 423 424 425
  n = result->n_msgs = input->n_msgs;
  result->max_msgs = input->n_msgs; /* let it be extended as-and-when */
  result->msgs = new_array(struct msgpath, n);
  result->type = new_array(enum message_type, n);

426 427
  result->hash_key = input->hash_key;

428 429 430 431 432 433 434
  /* Set up mbox structures */
  N = result->n_mboxen = result->max_mboxen = input->n_mboxen;
  result->mboxen = N ? (new_array(struct mbox, N)) : NULL;
  for (i=0; i<N; i++) {
    int nn;
    if (input->mbox_paths_table[i]) {
      result->mboxen[i].path = new_string(input->data + input->mbox_paths_table[i]);
Richard Curnow's avatar
Richard Curnow committed
435
    } else {
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
      /* mbox is dead. */
      result->mboxen[i].path = NULL;
    }
    result->mboxen[i].file_mtime = input->mbox_mtime_table[i];
    result->mboxen[i].file_size  = input->mbox_size_table[i];
    nn = result->mboxen[i].n_msgs = input->mbox_entries_table[i];
    result->mboxen[i].max_msgs = nn;
    result->mboxen[i].start = new_array(off_t, nn);
    result->mboxen[i].len   = new_array(size_t, nn);
    result->mboxen[i].check_all = new_array(checksum_t, nn);
    /* Copy the entire checksum table in one go. */
    memcpy(result->mboxen[i].check_all,
           input->data + input->mbox_checksum_table[i],
           nn * sizeof(checksum_t));
    result->mboxen[i].n_so_far = 0;
  }

  for (i=0; i<n; i++) {
    switch (input->msg_type[i]) {
      case DB_MSG_DEAD:
        result->type[i] = MTY_DEAD;
        break;
      case DB_MSG_FILE:
        result->type[i] = MTY_FILE;
        result->msgs[i].src.mpf.path = new_string(input->data + input->path_offsets[i]);
        result->msgs[i].src.mpf.mtime = input->mtime_table[i];
        result->msgs[i].src.mpf.size  = input->size_table[i];
        break;
      case DB_MSG_MBOX:
        {
          int mbi, msgi, n;
          struct mbox *mb;
          result->type[i] = MTY_MBOX;
          decode_mbox_indices(input->path_offsets[i], &mbi, &msgi);
          result->msgs[i].src.mbox.file_index = mbi;
          mb = &result->mboxen[mbi];
          assert(mb->n_so_far == msgi);
          n = mb->n_so_far;
          result->msgs[i].src.mbox.msg_index = n;
          mb->start[n] = input->mtime_table[i];
          mb->len[n] = input->size_table[i];
          ++mb->n_so_far;
        }

        break;
Richard Curnow's avatar
Richard Curnow committed
481
    }
482 483
    result->msgs[i].date  = input->date_table[i];
    result->msgs[i].tid   = input->tid_table[i];
Richard Curnow's avatar
Richard Curnow committed
484 485
  }

486 487 488 489 490 491
  import_toktable(input->data, input->hash_key, result->n_msgs, &input->to, result->to);
  import_toktable(input->data, input->hash_key, result->n_msgs, &input->cc, result->cc);
  import_toktable(input->data, input->hash_key, result->n_msgs, &input->from, result->from);
  import_toktable(input->data, input->hash_key, result->n_msgs, &input->subject, result->subject);
  import_toktable(input->data, input->hash_key, result->n_msgs, &input->body, result->body);
  import_toktable2(input->data, input->hash_key, result->n_msgs, &input->msg_ids, result->msg_ids);
Richard Curnow's avatar
Richard Curnow committed
492 493 494

  close_db(input);

495 496 497
  if (do_integrity_checks) {
    check_database_integrity(result);
  }
Richard Curnow's avatar
Richard Curnow committed
498 499 500 501 502
  
  return result;
}
/*}}}*/

503
static void add_angled_terms(int file_index, unsigned int hash_key, struct toktable2 *table, int add_to_chain1, char *s)/*{{{*/
Richard Curnow's avatar
Richard Curnow committed
504 505 506 507 508 509 510 511 512
{
  char *left, *right;

  if (s) {
    left = strchr(s, '<');
    while (left) {
      right = strchr(left, '>');
      if (right) {
        *right = '\0';
513
        add_token2_in_file(file_index, hash_key, left+1, table, add_to_chain1);
Richard Curnow's avatar
Richard Curnow committed
514 515 516 517 518 519 520 521 522 523
        *right = '>'; /* restore */
      } else {
        break;
      }
      left = strchr(right, '<');
    }
  }
}
/*}}}*/

524 525 526 527 528 529 530 531 532 533 534 535 536 537
/* Macro for what characters can make up token strings.

   The following characters have special meanings:
   0x2b +
   0x2d -
   0x2e .
   0x40 @
   0x5f _

   since they can occur within email addresses and message IDs when considered
   as a whole rather than as individual words.  Underscore (0x5f) is considered
   a word-character always too.

   */
538 539 540
static unsigned char special_table[256] = {
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 00-0f */
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10-1f */
541
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 2, 0, /* 20-2f */
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 30-3f */
  2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40-4f */
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, /* 50-5f */
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60-6f */
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 70-7f */
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80-8f */
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90-9f */
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* a0-af */
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* b0-bf */
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* c0-cf */
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* d0-df */
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* e0-ef */
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  /* f0-ff */
};

#define CHAR_VALID(x,mask) (isalnum(x) || (special_table[(unsigned int)(unsigned char) x] & mask))

559
static void tokenise_string(int file_index, unsigned int hash_key, struct toktable *table, char *data, int match_mask)/*{{{*/
Richard Curnow's avatar
Richard Curnow committed
560
{
561 562
  unsigned char *ss, *es, old_es;
  ss = (unsigned char *) data;
Richard Curnow's avatar
Richard Curnow committed
563
  for (;;) {
564
    while (*ss && !CHAR_VALID(*ss,match_mask)) ss++;
Richard Curnow's avatar
Richard Curnow committed
565 566
    if (!*ss) break;
    es = ss + 1;
567
    while (*es && CHAR_VALID(*es,match_mask)) es++;
Richard Curnow's avatar
Richard Curnow committed
568 569 570 571 572
    
    /* deal with token [ss,es) */
    old_es = *es;
    *es = '\0';
    /* FIXME: Ought to do this by passing start and length - clean up later */
573
    add_token_in_file(file_index, hash_key, ss, table);
Richard Curnow's avatar
Richard Curnow committed
574 575 576 577 578 579 580
    *es = old_es;

    if (!*es) break;
    ss = es;
  }
}
/*}}}*/
581
static void tokenise_html_string(int file_index, unsigned int hash_key, struct toktable *table, char *data)/*{{{*/
Richard Curnow's avatar
Richard Curnow committed
582
{
583
  unsigned char *ss, *es, old_es;
Richard Curnow's avatar
Richard Curnow committed
584 585 586

  /* FIXME : Probably want to rewrite this as an explicit FSM */
  
587
  ss = (unsigned char *) data;
Richard Curnow's avatar
Richard Curnow committed
588 589
  for (;;) {
    /* Assume < and > are never valid token characters ! */
590
    while (*ss && !CHAR_VALID(*ss, 1)) {
Richard Curnow's avatar
Richard Curnow committed
591 592 593 594 595 596 597 598
      if (*ss++ == '<') {
        /* Skip over HTML tag */
        while (*ss && (*ss != '>')) ss++;
      }
    }
    if (!*ss) break;
    
    es = ss + 1;
599
    while (*es && CHAR_VALID(*es, 1)) es++;
Richard Curnow's avatar
Richard Curnow committed
600 601 602 603 604
    
    /* deal with token [ss,es) */
    old_es = *es;
    *es = '\0';
    /* FIXME: Ought to do this by passing start and length - clean up later */
605
    add_token_in_file(file_index, hash_key, ss, table);
Richard Curnow's avatar
Richard Curnow committed
606 607 608 609 610 611 612
    *es = old_es;

    if (!*es) break;
    ss = es;
  }
}
/*}}}*/
613
void tokenise_message(int file_index, struct database *db, struct rfc822 *msg)/*{{{*/
Richard Curnow's avatar
Richard Curnow committed
614 615 616
{
  struct attachment *a;

617 618
  /* Match on whole addresses in these headers as well as the individual words */
  if (msg->hdrs.to) {
619 620
    tokenise_string(file_index, db->hash_key, db->to, msg->hdrs.to, 1);
    tokenise_string(file_index, db->hash_key, db->to, msg->hdrs.to, 2);
621 622
  }
  if (msg->hdrs.cc) {
623 624
    tokenise_string(file_index, db->hash_key, db->cc, msg->hdrs.cc, 1);
    tokenise_string(file_index, db->hash_key, db->cc, msg->hdrs.cc, 2);
625 626
  }
  if (msg->hdrs.from) {
627 628
    tokenise_string(file_index, db->hash_key, db->from, msg->hdrs.from, 1);
    tokenise_string(file_index, db->hash_key, db->from, msg->hdrs.from, 2);
629
  }
630
  if (msg->hdrs.subject) tokenise_string(file_index, db->hash_key, db->subject, msg->hdrs.subject, 1);
Richard Curnow's avatar
Richard Curnow committed
631 632 633 634

  for (a=msg->atts.next; a!=&msg->atts; a=a->next) {
    switch (a->ct) {
      case CT_TEXT_PLAIN:
635
        tokenise_string(file_index, db->hash_key, db->body, a->data.normal.bytes, 1);
Richard Curnow's avatar
Richard Curnow committed
636 637
        break;
      case CT_TEXT_HTML:
638
        tokenise_html_string(file_index, db->hash_key, db->body, a->data.normal.bytes);
Richard Curnow's avatar
Richard Curnow committed
639 640 641 642 643
        break;
      case CT_MESSAGE_RFC822:
        /* Just recurse for now - maybe we should have separate token tables
         * for tokens occurring in embedded messages? */

Richard Curnow's avatar
Richard Curnow committed
644 645 646
        if (a->data.rfc822) {
          tokenise_message(file_index, db, a->data.rfc822);
        }
Richard Curnow's avatar
Richard Curnow committed
647 648 649 650 651 652 653 654 655 656 657 658
        break;
      default:
        /* Don't do anything - unknown text format or some nasty binary stuff.
         * In future, we could have all kinds of 'plug-ins' here, e.g.
         * something that can parse PDF to get the basic text strings out of
         * the pages? */
        break;
    }

  }

  /* Deal with threading information */
659 660 661
  add_angled_terms(file_index, db->hash_key, db->msg_ids, 1, msg->hdrs.message_id);
  add_angled_terms(file_index, db->hash_key, db->msg_ids, 0, msg->hdrs.in_reply_to);
  add_angled_terms(file_index, db->hash_key, db->msg_ids, 0, msg->hdrs.references);
Richard Curnow's avatar
Richard Curnow committed
662 663 664 665 666
}
/*}}}*/
static void scan_new_messages(struct database *db, int start_at)/*{{{*/
{
  int i;
667 668 669 670 671 672 673 674 675 676 677 678 679 680
  for (i=start_at; i<db->n_msgs; i++) {
    struct rfc822 *msg = NULL;
    switch (db->type[i]) {
      case MTY_DEAD:
        assert(0);
        break;
      case MTY_MBOX:
        assert(0); /* Should never get here - mbox messages are scanned elsewhere. */
        break;
      case MTY_FILE:
        if (verbose) fprintf(stderr, "Scanning <%s>\n", db->msgs[i].src.mpf.path);
        msg = make_rfc822(db->msgs[i].src.mpf.path);
        break;
    }
Richard Curnow's avatar
Richard Curnow committed
681 682
    if(msg) 
    {
683
      db->msgs[i].date = msg->hdrs.date;
Richard Curnow's avatar
Richard Curnow committed
684 685 686 687
      tokenise_message(i, db, msg);
      free_rfc822(msg);
    }
    else
688
      fprintf(stderr, "Skipping %s (could not parse message)\n", db->msgs[i].src.mpf.path);
Richard Curnow's avatar
Richard Curnow committed
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
  }
}
/*}}}*/

static inline void set_bit(unsigned long *x, int n)/*{{{*/
{
  int set;
  unsigned long mask;
  set = (n >> 5);
  mask = (1UL << (n & 31));
  x[set] |= mask;
}
/*}}}*/
static inline int isset_bit(unsigned long *x, int n)/*{{{*/
{
  int set;
  unsigned long mask;
  set = (n >> 5);
  mask = (1UL << (n & 31));
  return (x[set] & mask) ? 1 : 0;
}
/*}}}*/
static int find_base(int *table, int index) {/*{{{*/
  int a = index;

  /* TODO : make this compress the path lengths down to the base entry */
  while (table[a] != a) {
    a = table[a];
  }
  return a;
}
/*}}}*/
static void find_threading(struct database *db)/*{{{*/
{

  /* ix is a table mapping path array index to the lowest path array index that
   * is known to share at least one message ID in its hdrs somewhere (i.e. they
   * must be in the same thread) */
  int *ix;

  int i, m, np, nm, sm;
  int next_tid;
  
732
  np = db->n_msgs;
Richard Curnow's avatar
Richard Curnow committed
733 734 735 736 737 738 739 740 741
  nm = db->msg_ids->n;
  sm = db->msg_ids->size;
  
  ix = new_array(int, np);
  for (i=0; i<np; i++) {
    ix[i] = i; /* default - every message in a thread of its own */
  }

  for (m=0; m<sm; m++) {
742
    struct token2 *tok = db->msg_ids->tokens[m];
Richard Curnow's avatar
Richard Curnow committed
743
    if (tok) {
744 745
      unsigned char *j = tok->match0.msginfo;
      unsigned char *last_char = j + tok->match0.n;
Richard Curnow's avatar
Richard Curnow committed
746
      int cur = 0, incr, first=1;
747
      int new_base=-1, old_base;
Richard Curnow's avatar
Richard Curnow committed
748 749 750 751 752 753 754 755 756 757 758 759
      while (j < last_char) {
        incr = read_increment(&j);
        cur += incr;
        if (first) {
          new_base = find_base(ix, cur);
          first = 0;
        } else {
          old_base = find_base(ix, cur);
          if (old_base < new_base) {
            ix[new_base] = old_base;
            new_base = old_base;
          } else if (old_base > new_base) {
760
            assert(new_base != -1);
Richard Curnow's avatar
Richard Curnow committed
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
            ix[old_base] = new_base;
          }
        }
      }
    }
  }

  /* Now make each entry point directly to its base */
  for (i=0; i<np; i++) {
    if (ix[i] != i) {
      /* Sure to work as we're going up from the bottom */
      ix[i] = ix[ix[i]];
    }
  }

  /* Now allocate contiguous thread group numbers */
  next_tid = 0;
  for (i=0; i<np; i++) {
    if (ix[i] == i) {
780
      db->msgs[i].tid = next_tid++;
Richard Curnow's avatar
Richard Curnow committed
781
    } else {
782
      db->msgs[i].tid = db->msgs[ix[i]].tid;
Richard Curnow's avatar
Richard Curnow committed
783 784 785 786 787 788 789
    }
  }

  free(ix);
  return;
}
/*}}}*/
790
static int lookup_msgpath(struct msgpath *sorted_paths, int n_msgs, char *key)/*{{{*/
Richard Curnow's avatar
Richard Curnow committed
791 792 793
{
  /* Implement bisection search */
 int l, h, m, r;
794
 l = 0, h = n_msgs;
Richard Curnow's avatar
Richard Curnow committed
795 796 797
 m = -1;
 while (h > l) {
   m = (h + l) >> 1;
798 799
   /* Should only get called on 'file' type messages - TBC */
   r = strcmp(sorted_paths[m].src.mpf.path, key);
Richard Curnow's avatar
Richard Curnow committed
800 801 802 803 804 805 806 807
   if (r == 0) break;
   if (l == m) return -1;
   if (r > 0) h = m;
   else       l = m;
 }
 return m;
}
/*}}}*/
808
void maybe_grow_message_arrays(struct database *db)/*{{{*/
Richard Curnow's avatar
Richard Curnow committed
809
{
810 811 812
  if (db->n_msgs == db->max_msgs) {
    if (db->max_msgs == 0) {
      db->max_msgs = 256;
Richard Curnow's avatar
Richard Curnow committed
813
    } else {
814
      db->max_msgs += (db->max_msgs >> 1);
Richard Curnow's avatar
Richard Curnow committed
815
    }
816 817
    db->msgs  = grow_array(struct msgpath,    db->max_msgs, db->msgs);
    db->type  = grow_array(enum message_type, db->max_msgs, db->type);
Richard Curnow's avatar
Richard Curnow committed
818 819 820
  }
}
/*}}}*/
821 822 823 824 825 826 827 828 829 830 831
static void add_msg_path(struct database *db, char *path, time_t mtime, size_t message_size)/*{{{*/
{
  maybe_grow_message_arrays(db);
  db->type[db->n_msgs] = MTY_FILE;
  db->msgs[db->n_msgs].src.mpf.path = new_string(path);
  db->msgs[db->n_msgs].src.mpf.mtime = mtime;
  db->msgs[db->n_msgs].src.mpf.size = message_size;
  ++db->n_msgs;
}
/*}}}*/
int update_database(struct database *db, struct msgpath *sorted_paths, int n_msgs)/*{{{*/
Richard Curnow's avatar
Richard Curnow committed
832 833 834 835 836 837 838 839 840 841 842 843 844
{
  /* The incoming list must be sorted into order, to make binary searching
   * possible.  We search for each existing path in the incoming sorted array.
   * If the date differs, or the file no longer exist, the existing database
   * entry for that file is nulled.  (These are only recovered if the database
   * is actively compressed.)  If the date differed, a new entry for the file
   * is put at the end of the list.  Similarly, any new file goes at the end.
   * These new entries are all rescanned to find tokens and add them to the
   * database. */

  char *file_in_db, *file_in_new_list;
  int matched_index;
  int i, new_entries_start_at;
845
  int any_new, n_newly_pruned, n_already_dead;
Richard Curnow's avatar
Richard Curnow committed
846
  
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
  file_in_db = new_array(char, n_msgs);
  file_in_new_list = new_array(char, db->n_msgs);
  bzero(file_in_db, n_msgs);
  bzero(file_in_new_list, db->n_msgs);

  n_already_dead = 0;
  n_newly_pruned = 0;

  for (i=0; i<db->n_msgs; i++) {
    switch (db->type[i]) {
      case MTY_FILE:
        matched_index = lookup_msgpath(sorted_paths, n_msgs, db->msgs[i].src.mpf.path);
        if ((matched_index >= 0) &&
            (sorted_paths[matched_index].src.mpf.mtime == db->msgs[i].src.mpf.mtime)) {
        /* Treat stale files as though the path has changed. */
          file_in_db[matched_index] = 1;
          file_in_new_list[i] = 1;
        }
        break;
      case MTY_MBOX:
        /* Nothing to do on this pass. */
        break;
      case MTY_DEAD:
        break;
Richard Curnow's avatar
Richard Curnow committed
871 872 873 874
    }
  }

  /* Add new entries to database */
875
  new_entries_start_at = db->n_msgs;
Richard Curnow's avatar
Richard Curnow committed
876
  
877
  for (i=0; i<db->n_msgs; i++) {
Richard Curnow's avatar
Richard Curnow committed
878
    /* Weed dead entries */
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
    switch (db->type[i]) {
      case MTY_FILE:
        if (!file_in_new_list[i]) {
          free(db->msgs[i].src.mpf.path);
          db->msgs[i].src.mpf.path = NULL;
          db->type[i] = MTY_DEAD;
          ++n_newly_pruned;
        }
        break;
      case MTY_MBOX:
        {
          int msg_index, file_index, number_valid;
          int mbox_valid;
          msg_index = db->msgs[i].src.mbox.msg_index;
          file_index = db->msgs[i].src.mbox.file_index;
          assert (file_index < db->n_mboxen);
          mbox_valid = (db->mboxen[file_index].path) ? 1 : 0;
          number_valid = db->mboxen[file_index].n_old_msgs_valid;
          if (!mbox_valid || (msg_index >= number_valid)) {
            db->type[i] = MTY_DEAD;
            ++n_newly_pruned;
          }
        }
        break;
      case MTY_DEAD:
        /* already dead */
        ++n_already_dead;
        break;
Richard Curnow's avatar
Richard Curnow committed
907 908 909 910
    }
  }

  if (verbose) {
911
    fprintf(stderr, "%d newly dead messages, %d messages now dead in total\n", n_newly_pruned, n_newly_pruned+n_already_dead);
Richard Curnow's avatar
Richard Curnow committed
912 913 914
  }

  any_new = 0;
915
  for (i=0; i<n_msgs; i++) {
Richard Curnow's avatar
Richard Curnow committed
916 917
    if (!file_in_db[i]) {
      any_new = 1;
918 919
      /* The 'sorted_paths' array is only used for file-per-message folders. */
      add_msg_path(db, sorted_paths[i].src.mpf.path, sorted_paths[i].src.mpf.mtime, sorted_paths[i].src.mpf.size);
Richard Curnow's avatar
Richard Curnow committed
920 921
    }
  }
922

Richard Curnow's avatar
Richard Curnow committed
923 924
  if (any_new) {
    scan_new_messages(db, new_entries_start_at);
925 926 927 928 929 930
  }

  /* Add newly found mbox messages. */
  any_new |= add_mbox_messages(db);
  
  if (any_new) {
Richard Curnow's avatar
Richard Curnow committed
931 932 933 934 935 936 937 938
    find_threading(db);
  } else {
    if (verbose) fprintf(stderr, "No new messages found\n");
  }
  
  free(file_in_db);
  free(file_in_new_list);

939
  return any_new || (n_newly_pruned > 0);
Richard Curnow's avatar
Richard Curnow committed
940 941
}
/*}}}*/
942
static void recode_encoding(struct matches *m, int *new_idx)/*{{{*/
Richard Curnow's avatar
Richard Curnow committed
943 944 945 946
{
  unsigned char *new_enc, *old_enc;
  unsigned char *j, *last_char;
  int incr, idx, n_idx;
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972

  old_enc = m->msginfo;
  j = old_enc;
  last_char = old_enc + m->n;
  
  new_enc = new_array(unsigned char, m->max); /* Probably not bigger than this. */
  m->n = 0;
  m->highest = 0;
  m->msginfo = new_enc;
  idx = 0;

  while (j < last_char) {
    incr = read_increment(&j);
    idx += incr;
    n_idx = new_idx[idx];
    if (n_idx >= 0) {
      check_and_enlarge_encoding(m);
      insert_index_on_encoding(m, n_idx);
    }
  }
  free(old_enc);
}
/*}}}*/
static void recode_toktable(struct toktable *tbl, int *new_idx)/*{{{*/
{
  /* Re-encode the vectors according to the new path indices */
Richard Curnow's avatar
Richard Curnow committed
973 974 975 976 977 978 979
  int i;
  int any_dead = 0;
  int any_moved, pass;
  
  for (i=0; i<tbl->size; i++) {
    struct token *tok = tbl->tokens[i];
    if (tok) {
980 981 982 983 984 985 986
      recode_encoding(&tok->match0, new_idx);
      if (tok->match0.n == 0) {
        /* Delete this token.  Gotcha - there may be tokens further on in the
         * array that didn't get their natural hash bucket due to collisions.
         * Need to shuffle such tokens up to guarantee that the buckets between
         * the natural one and the one where they are now are all occupied, to
         * prevent their lookups failing. */
Richard Curnow's avatar
Richard Curnow committed
987

988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
#if 0
        fprintf(stderr, "Token <%s> (bucket %d) no longer has files containing it, deleting\n", tok->text, i);
#endif
        free_token(tok);
        tbl->tokens[i] = NULL;
        --tbl->n; /* Maintain number in use counter */
        any_dead = 1;
      }

    }
  }


  if (any_dead) {
    /* Now close gaps.  This has to be done in a second pass, otherwise we get a
     * problem with moving entries that need deleting back before the current
       scan point. */

    pass = 1;
    for (;;) {
      int i;

      if (verbose) {
        fprintf(stderr, "Pass %d\n", pass);
      }

      any_moved = 0;

      for (i=0; i<tbl->size; i++) {
        if (tbl->tokens[i]) {
          int nat_bucket_i;
          nat_bucket_i = tbl->tokens[i]->hashval & tbl->mask;
          if (nat_bucket_i != i) {
            /* Find earliest bucket that we could move i to */
            int j = nat_bucket_i;
            while (j != i) {
              if (!tbl->tokens[j]) {
                /* put it here */
#if 0
                fprintf(stderr, "Moved <%s> from bucket %d to %d (natural bucket %d)\n", tbl->tokens[i]->text, i, j, nat_bucket_i);
#endif
                tbl->tokens[j] = tbl->tokens[i];
                tbl->tokens[i] = NULL;
                any_moved = 1;
                break;
              } else {
                j++;
                j &= tbl->mask;
              }
            }
            if (tbl->tokens[i]) {
#if 0
              fprintf(stderr, "NOT moved <%s> from bucket %d (natural bucket %d)\n", tbl->tokens[i]->text, i, nat_bucket_i);
#endif
            }
          }
Richard Curnow's avatar
Richard Curnow committed
1044 1045
        }
      }
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065

      if (!any_moved) break;
      pass++;
    }
  }
}
/*}}}*/
static void recode_toktable2(struct toktable2 *tbl, int *new_idx)/*{{{*/
{
  /* Re-encode the vectors according to the new path indices */
  int i;
  int any_dead = 0;
  int any_moved, pass;
  
  for (i=0; i<tbl->size; i++) {
    struct token2 *tok = tbl->tokens[i];
    if (tok) {
      recode_encoding(&tok->match0, new_idx);
      recode_encoding(&tok->match1, new_idx);
      if ((tok->match0.n == 0) && (tok->match1.n == 0)) {
Richard Curnow's avatar
Richard Curnow committed
1066 1067 1068 1069 1070 1071
        /* Delete this token.  Gotcha - there may be tokens further on in the
         * array that didn't get their natural hash bucket due to collisions.
         * Need to shuffle such tokens up to guarantee that the buckets between
         * the natural one and the one where they are now are all occupied, to
         * prevent their lookups failing. */

Richard Curnow's avatar
Richard Curnow committed
1072
#if 0
Richard Curnow's avatar
Richard Curnow committed
1073
        fprintf(stderr, "Token <%s> (bucket %d) no longer has files containing it, deleting\n", tok->text, i);
Richard Curnow's avatar
Richard Curnow committed
1074
#endif
1075
        free_token2(tok);
Richard Curnow's avatar
Richard Curnow committed
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
        tbl->tokens[i] = NULL;
        --tbl->n; /* Maintain number in use counter */
        any_dead = 1;
      }
    }
  }

  if (any_dead) {
    /* Now close gaps.  This has to be done in a second pass, otherwise we get a
     * problem with moving entries that need deleting back before the current
       scan point. */

    pass = 1;
    for (;;) {
      int i;

1092 1093 1094 1095
      if (verbose) {
        fprintf(stderr, "Pass %d\n", pass);
      }

Richard Curnow's avatar
Richard Curnow committed
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
      any_moved = 0;

      for (i=0; i<tbl->size; i++) {
        if (tbl->tokens[i]) {
          int nat_bucket_i;
          nat_bucket_i = tbl->tokens[i]->hashval & tbl->mask;
          if (nat_bucket_i != i) {
            /* Find earliest bucket that we could move i to */
            int j = nat_bucket_i;
            while (j != i) {
              if (!tbl->tokens[j]) {
                /* put it here */
Richard Curnow's avatar
Richard Curnow committed
1108
#if 0
Richard Curnow's avatar
Richard Curnow committed
1109
                fprintf(stderr, "Moved <%s> from bucket %d to %d (natural bucket %d)\n", tbl->tokens[i]->text, i, j, nat_bucket_i);
Richard Curnow's avatar
Richard Curnow committed
1110
#endif
Richard Curnow's avatar
Richard Curnow committed
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
                tbl->tokens[j] = tbl->tokens[i];
                tbl->tokens[i] = NULL;
                any_moved = 1;
                break;
              } else {
                j++;
                j &= tbl->mask;
              }
            }
            if (tbl->tokens[i]) {
#if 0
              fprintf(stderr, "NOT moved <%s> from bucket %d (natural bucket %d)\n", tbl->tokens[i]->text, i, nat_bucket_i);
#endif
            }
          }
        }
      }

      if (!any_moved) break;
      pass++;
    }
  }
}
/*}}}*/
1135
int cull_dead_messages(struct database *db, int do_integrity_checks)/*{{{*/
Richard Curnow's avatar
Richard Curnow committed
1136 1137 1138 1139 1140 1141 1142 1143
{
  /* Return true if any culled */

  int *new_idx, i, j, n_old;
  int any_culled = 0;

  /* Check db is OK before we start on this. (Check afterwards is done in the
   * writer.c code.) */
1144 1145 1146
  if (do_integrity_checks) {
    check_database_integrity(db);
  }
Richard Curnow's avatar
Richard Curnow committed
1147 1148 1149 1150 1151

  if (verbose) {
    fprintf(stderr, "Culling dead messages\n");
  }

1152
  n_old = db->n_msgs;
Richard Curnow's avatar
Richard Curnow committed
1153 1154 1155

  new_idx = new_array(int, n_old);
  for (i=0, j=0; i<n_old; i++) {
1156 1157 1158 1159 1160 1161 1162 1163 1164
    switch (db->type[i]) {
      case MTY_FILE:
      case MTY_MBOX:
        new_idx[i] = j++;
        break;
      case MTY_DEAD:
        new_idx[i] = -1;
        any_culled = 1;
        break;
Richard Curnow's avatar
Richard Curnow committed
1165 1166 1167 1168 1169 1170 1171 1172
    }
  }

  recode_toktable(db->to, new_idx);
  recode_toktable(db->cc, new_idx);
  recode_toktable(db->from, new_idx);
  recode_toktable(db->subject, new_idx);
  recode_toktable(db->body, new_idx);
1173
  recode_toktable2(db->msg_ids, new_idx);
Richard Curnow's avatar
Richard Curnow committed
1174 1175 1176
  
  /* And crunch down the filename table */
  for (i=0, j=0; i<n_old; i++) {
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
    switch (db->type[i]) {
      case MTY_DEAD:
        break;
      case MTY_FILE:
      case MTY_MBOX:
        if (i > j) {
          db->msgs[j] = db->msgs[i];
          db->type[j]  = db->type[i];
        }
        j++;
        break;
Richard Curnow's avatar
Richard Curnow committed
1188 1189
    }
  }
1190
  db->n_msgs = j;
Richard Curnow's avatar
Richard Curnow committed
1191 1192 1193

  free(new_idx);

1194 1195 1196
  /* .. and cull dead mboxen */
  cull_dead_mboxen(db);

Richard Curnow's avatar
Richard Curnow committed
1197 1198 1199
  return any_culled;
}
/*}}}*/