writer.c 18.9 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,2003,2004,2005,2006
Richard P. Curnow's avatar
Richard P. Curnow committed
6
 *
Richard Curnow's avatar
Richard Curnow committed
7 8 9
 * 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.
Richard P. Curnow's avatar
Richard P. Curnow committed
10
 *
Richard Curnow's avatar
Richard Curnow committed
11 12 13 14
 * 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.
Richard P. Curnow's avatar
Richard P. Curnow committed
15
 *
Richard Curnow's avatar
Richard Curnow committed
16 17
 * 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.,
18
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Richard P. Curnow's avatar
Richard P. Curnow committed
19
 *
Richard Curnow's avatar
Richard Curnow committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 **********************************************************************
 */

/* Write the database to disc. */

#include "mairix.h"
#include "reader.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <sys/mman.h>

struct write_map_toktable {/*{{{*/
Richard P. Curnow's avatar
Richard P. Curnow committed
36

Richard Curnow's avatar
Richard Curnow committed
37 38 39 40 41 42 43
  /* Table of character offsets to null-terminated token texts */
  int tok_offset;

  /* Table of character offsets to byte strings containing compressed
   * delta-encoding of file indices matching the token */
  int enc_offset;
};/*}}}*/
44
struct write_map_toktable2 {/*{{{*/
Richard P. Curnow's avatar
Richard P. Curnow committed
45

46 47 48 49 50 51 52 53
  /* Table of character offsets to null-terminated token texts */
  int tok_offset;

  /* Table of character offsets to byte strings containing compressed
   * delta-encoding of file indices matching the token */
  int enc0_offset;
  int enc1_offset;
};/*}}}*/
Richard Curnow's avatar
Richard Curnow committed
54 55 56 57 58 59 60 61

struct write_map {/*{{{*/
/* Contain offset information for the various tables.
   UI stuff in 4 byte units rel to base addr.
   Char stuff in byte units rel to base addr. */

  /* Path information */
  int path_offset;
62 63 64 65 66 67 68 69 70 71 72 73 74
  int mtime_offset; /* Message file mtimes (maildir/mh), mbox number (mbox) */
  int size_offset; /* Message sizes (maildir/mh), entry in respective mbox (mbox) */
  int date_offset; /* Message dates (all folder types) */
  int tid_offset;  /* Thread group index table (all folder types) */

  int mbox_paths_offset;
  int mbox_entries_offset;
  int mbox_mtime_offset;
  int mbox_size_offset;
  /* Character offset to checksum of first msg in the mbox.  Positions of
   * subsequent messages computed by indexing - no explicit table entries
   * anywhere. */
  int mbox_checksum_offset;
Richard Curnow's avatar
Richard Curnow committed
75 76 77 78 79 80

  struct write_map_toktable to;
  struct write_map_toktable cc;
  struct write_map_toktable from;
  struct write_map_toktable subject;
  struct write_map_toktable body;
81
  struct write_map_toktable attachment_name;
82
  struct write_map_toktable2 msg_ids;
Richard Curnow's avatar
Richard Curnow committed
83 84 85 86 87 88

  /* To get base address for character data */
  int beyond_last_ui_offset;
};
/*}}}*/

89
static void create_rw_mapping(char *filename, size_t len, int *out_fd, char **out_data)/*{{{*/
Richard Curnow's avatar
Richard Curnow committed
90 91 92 93
{
  int fd;
  char *data;
  struct stat sb;
Richard P. Curnow's avatar
Richard P. Curnow committed
94

95
  fd = open(filename, O_RDWR | O_CREAT, 0600);
Richard Curnow's avatar
Richard Curnow committed
96
  if (fd < 0) {
97
    report_error("open", filename);
98
    unlock_and_exit(2);
Richard Curnow's avatar
Richard Curnow committed
99 100 101
  }

  if (fstat(fd, &sb) < 0) {
102
    report_error("stat", filename);
103
    unlock_and_exit(2);
Richard Curnow's avatar
Richard Curnow committed
104 105 106 107 108
  }

  if (sb.st_size < len) {
    /* Extend */
    if (lseek(fd, len - 1, SEEK_SET) < 0) {
109
      report_error("lseek", filename);
110
      unlock_and_exit(2);
Richard Curnow's avatar
Richard Curnow committed
111 112
    }
    if (write(fd, "\000", 1) < 0) {
113
      report_error("write", filename);
114
      unlock_and_exit(2);
Richard Curnow's avatar
Richard Curnow committed
115 116 117 118
    }
  } else if (sb.st_size > len) {
    /* Truncate */
    if (ftruncate(fd, len) < 0) {
119
      report_error("ftruncate", filename);
120
      unlock_and_exit(2);
Richard Curnow's avatar
Richard Curnow committed
121 122 123 124 125 126
    }
  } else {
    /* Exactly the right length already - nothing to do! */
  }

  data = mmap(0, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
127
  if (data == MAP_FAILED) {
128
    report_error("writer:mmap", filename);
129
    unlock_and_exit(2);
Richard Curnow's avatar
Richard Curnow committed
130 131
  }

132 133
  *out_data = data;
  *out_fd = fd;
Richard Curnow's avatar
Richard Curnow committed
134 135
}
/*}}}*/
Richard P. Curnow's avatar
Richard P. Curnow committed
136

Richard Curnow's avatar
Richard Curnow committed
137 138 139 140 141 142 143
static int toktable_char_length(struct toktable *tab)/*{{{*/
{
  int result = 0;
  int i;
  for (i=0; i<tab->size; i++) {
    if (tab->tokens[i]) {
      result += (1 + strlen(tab->tokens[i]->text));
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
      result += (1 + tab->tokens[i]->match0.n);
    }
  }
  return result;
}
/*}}}*/
static int toktable2_char_length(struct toktable2 *tab)/*{{{*/
{
  int result = 0;
  int i;
  for (i=0; i<tab->size; i++) {
    if (tab->tokens[i]) {
      result += (1 + strlen(tab->tokens[i]->text));
      result += (1 + tab->tokens[i]->match0.n);
      result += (1 + tab->tokens[i]->match1.n);
Richard Curnow's avatar
Richard Curnow committed
159 160 161 162 163 164 165 166 167 168 169 170 171
    }
  }
  return result;
}
/*}}}*/
static int char_length(struct database *db)/*{{{*/
{
  /* Return total length of character data to be written. */
  int result;
  int i;

  result = 0;

172 173
  /* For type table. */
  result += db->n_msgs;
Richard P. Curnow's avatar
Richard P. Curnow committed
174

175 176 177 178 179 180 181
  for (i=0; i<db->n_msgs; i++) {
    switch (db->type[i]) {
      case MTY_DEAD:
        break;
      case MTY_MBOX:
        break;
      case MTY_FILE:
182
      case MTY_IMAP:
183 184 185 186 187 188 189 190 191 192 193
        assert(db->msgs[i].src.mpf.path);
        result += (1 + strlen(db->msgs[i].src.mpf.path));
        break;
    }
  }

  for (i=0; i<db->n_mboxen; i++) {
    struct mbox *mb = &db->mboxen[i];
    result += mb->n_msgs * sizeof(checksum_t);
    if (mb->path) {
      result += (1 + strlen(mb->path));
Richard Curnow's avatar
Richard Curnow committed
194 195 196 197 198 199 200 201
    }
  }

  result += toktable_char_length(db->to);
  result += toktable_char_length(db->cc);
  result += toktable_char_length(db->from);
  result += toktable_char_length(db->subject);
  result += toktable_char_length(db->body);
202
  result += toktable_char_length(db->attachment_name);
203
  result += toktable2_char_length(db->msg_ids);
Richard Curnow's avatar
Richard Curnow committed
204 205 206 207 208 209 210

  return result;
}
/*}}}*/

static void compute_mapping(struct database *db, struct write_map *map)/*{{{*/
{
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  int total = UI_HEADER_LEN;

  map->path_offset  = total, total += db->n_msgs;
  map->mtime_offset = total, total += db->n_msgs;
  map->date_offset  = total, total += db->n_msgs;
  map->size_offset  = total, total += db->n_msgs;
  map->tid_offset   = total, total += db->n_msgs;

  map->mbox_paths_offset = total, total += db->n_mboxen;
  map->mbox_entries_offset = total, total += db->n_mboxen;
  map->mbox_mtime_offset = total, total += db->n_mboxen;
  map->mbox_size_offset  = total, total += db->n_mboxen;
  map->mbox_checksum_offset = total, total += db->n_mboxen;

  map->to.tok_offset = total, total += db->to->n;
  map->to.enc_offset = total, total += db->to->n;

  map->cc.tok_offset = total, total += db->cc->n;
  map->cc.enc_offset = total, total += db->cc->n;

  map->from.tok_offset = total, total += db->from->n;
  map->from.enc_offset = total, total += db->from->n;

  map->subject.tok_offset = total, total += db->subject->n;
  map->subject.enc_offset = total, total += db->subject->n;

  map->body.tok_offset = total, total += db->body->n;
  map->body.enc_offset = total, total += db->body->n;
Richard Curnow's avatar
Richard Curnow committed
239

240 241 242
  map->attachment_name.tok_offset = total, total += db->attachment_name->n;
  map->attachment_name.enc_offset = total, total += db->attachment_name->n;

243 244 245
  map->msg_ids.tok_offset = total, total += db->msg_ids->n;
  map->msg_ids.enc0_offset = total, total += db->msg_ids->n;
  map->msg_ids.enc1_offset = total, total += db->msg_ids->n;
Richard Curnow's avatar
Richard Curnow committed
246

247
  map->beyond_last_ui_offset = total;
Richard Curnow's avatar
Richard Curnow committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261
}
/*}}}*/
static void write_header(char *data, unsigned int *uidata, struct database *db, struct write_map *map)/*{{{*/
{
  /* Endianness-independent writes - at least the magic number will be
   * recognized if the database is read by this program on a machine of
   * opposite endianness. */
  unsigned char *ucdata = (unsigned char *) data;

  ucdata[0] = HEADER_MAGIC0;
  ucdata[1] = HEADER_MAGIC1;
  ucdata[2] = HEADER_MAGIC2;
  ucdata[3] = HEADER_MAGIC3;

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
  uidata[UI_ENDIAN] = 0x44332211; /* For checking reversed endianness on read */
  uidata[UI_N_MSGS] = db->n_msgs;
  uidata[UI_MSG_CDATA] = map->path_offset; /* offset table of ptrs to filenames */
  uidata[UI_MSG_MTIME] = map->mtime_offset; /* offset of mtime table */
  uidata[UI_MSG_DATE] = map->date_offset; /* offset of table of message Date: header lines as time_t */
  uidata[UI_MSG_SIZE] = map->size_offset; /* offset of table of message sizes in bytes */
  uidata[UI_MSG_TID] = map->tid_offset; /* offset of table of thread group numbers */

  uidata[UI_MBOX_N] = db->n_mboxen;
  uidata[UI_MBOX_PATHS] = map->mbox_paths_offset;
  uidata[UI_MBOX_ENTRIES] = map->mbox_entries_offset;
  uidata[UI_MBOX_MTIME] = map->mbox_mtime_offset;
  uidata[UI_MBOX_SIZE]  = map->mbox_size_offset;
  uidata[UI_MBOX_CKSUM] = map->mbox_checksum_offset;

  uidata[UI_HASH_KEY] = db->hash_key;

  uidata[UI_TO_N] = db->to->n;
  uidata[UI_TO_TOK] = map->to.tok_offset;
  uidata[UI_TO_ENC] = map->to.enc_offset;
Richard P. Curnow's avatar
Richard P. Curnow committed
282

283 284 285
  uidata[UI_CC_N] = db->cc->n;
  uidata[UI_CC_TOK] = map->cc.tok_offset;
  uidata[UI_CC_ENC] = map->cc.enc_offset;
Richard Curnow's avatar
Richard Curnow committed
286

287 288 289
  uidata[UI_FROM_N] = db->from->n;
  uidata[UI_FROM_TOK] = map->from.tok_offset;
  uidata[UI_FROM_ENC] = map->from.enc_offset;
Richard Curnow's avatar
Richard Curnow committed
290

291 292 293
  uidata[UI_SUBJECT_N] = db->subject->n;
  uidata[UI_SUBJECT_TOK] = map->subject.tok_offset;
  uidata[UI_SUBJECT_ENC] = map->subject.enc_offset;
Richard P. Curnow's avatar
Richard P. Curnow committed
294

295 296 297
  uidata[UI_BODY_N] = db->body->n;
  uidata[UI_BODY_TOK] = map->body.tok_offset;
  uidata[UI_BODY_ENC] = map->body.enc_offset;
Richard P. Curnow's avatar
Richard P. Curnow committed
298

299 300 301 302
  uidata[UI_ATTACHMENT_NAME_N] = db->attachment_name->n;
  uidata[UI_ATTACHMENT_NAME_TOK] = map->attachment_name.tok_offset;
  uidata[UI_ATTACHMENT_NAME_ENC] = map->attachment_name.enc_offset;

303 304 305 306
  uidata[UI_MSGID_N]    = db->msg_ids->n;
  uidata[UI_MSGID_TOK]  = map->msg_ids.tok_offset;
  uidata[UI_MSGID_ENC0] = map->msg_ids.enc0_offset;
  uidata[UI_MSGID_ENC1] = map->msg_ids.enc1_offset;
Richard P. Curnow's avatar
Richard P. Curnow committed
307

Richard Curnow's avatar
Richard Curnow committed
308 309 310
  return;
}
/*}}}*/
311
static char *write_type_and_flag_table(struct database *db, unsigned int *uidata, char *data, char *cdata)/*{{{*/
312 313 314
{
  int i;
  for (i=0; i<db->n_msgs; i++) {
315
    struct msgpath *msgdata = db->msgs + i;
316 317 318 319 320 321 322 323 324 325
    switch (db->type[i]) {
      case MTY_FILE:
        cdata[i] = DB_MSG_FILE;
        break;
      case MTY_MBOX:
        cdata[i] = DB_MSG_MBOX;
        break;
      case MTY_DEAD:
        cdata[i] = DB_MSG_DEAD;
        break;
326 327 328
      case MTY_IMAP:
        cdata[i] = DB_MSG_IMAP;
        break;
329
    }
330 331 332 333

    if (msgdata->seen)    cdata[i] |= FLAG_SEEN;
    if (msgdata->replied) cdata[i] |= FLAG_REPLIED;
    if (msgdata->flagged) cdata[i] |= FLAG_FLAGGED;
334
  }
335
  uidata[UI_MSG_TYPE_AND_FLAGS] = cdata - data;
336 337 338 339
  return cdata + db->n_msgs;
}
/*}}}*/
static char *write_messages(struct database *db, struct write_map *map, unsigned int *uidata, char *data, char *cdata)/*{{{*/
Richard Curnow's avatar
Richard Curnow committed
340 341 342 343
{
  int i;
  char *start_cdata = cdata;

344
  for (i=0; i<db->n_msgs; i++) {
Richard Curnow's avatar
Richard Curnow committed
345
    int slen;
346 347 348 349 350 351 352
    switch (db->type[i]) {
      case MTY_FILE:
        slen = strlen(db->msgs[i].src.mpf.path);
        uidata[map->path_offset + i] = cdata - data;
        uidata[map->mtime_offset + i] = db->msgs[i].src.mpf.mtime;
        uidata[map->size_offset + i] = db->msgs[i].src.mpf.size;
        uidata[map->date_offset + i] = db->msgs[i].date;
353 354 355 356 357 358 359 360
        uidata[map->tid_offset + i]  = db->msgs[i].tid;
        memcpy(cdata, db->msgs[i].src.mpf.path, 1 + slen); /* include trailing null */
        cdata += (1 + slen);
        break;
      case MTY_IMAP:
        slen = strlen(db->msgs[i].src.mpf.path);
        uidata[map->path_offset + i] = cdata - data;
        uidata[map->date_offset + i] = db->msgs[i].date;
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
        uidata[map->tid_offset + i]  = db->msgs[i].tid;
        memcpy(cdata, db->msgs[i].src.mpf.path, 1 + slen); /* include trailing null */
        cdata += (1 + slen);
        break;
      case MTY_MBOX:
        {
          int mbno = db->msgs[i].src.mbox.file_index;
          int msgno = db->msgs[i].src.mbox.msg_index;
          struct mbox *mb = &db->mboxen[mbno];
          uidata[map->path_offset + i] = encode_mbox_indices(mbno, msgno);
          uidata[map->mtime_offset + i] = mb->start[msgno];
          uidata[map->size_offset + i] = mb->len[msgno];
          uidata[map->date_offset + i] = db->msgs[i].date;
          uidata[map->tid_offset + i]  = db->msgs[i].tid;
        }
        break;
      case MTY_DEAD:
        uidata[map->path_offset + i] = 0; /* Can't ever happen for real */
        uidata[map->mtime_offset + i] = 0; /* For cleanliness */
        uidata[map->size_offset + i] = 0;  /* For cleanliness */
        /* The following line is necessary, otherwise 'random' tid
         * information is written to the database, which can crash the search
         * functions. */
        uidata[map->tid_offset + i]  = db->msgs[i].tid;
        break;
Richard Curnow's avatar
Richard Curnow committed
386 387 388
    }
  }
  if (verbose) {
389
    printf("Wrote %d messages (%d bytes of tables, %d bytes of text)\n",
390
           db->n_msgs, 4*5*db->n_msgs, (int)(cdata - start_cdata));
Richard Curnow's avatar
Richard Curnow committed
391 392 393 394 395 396 397 398 399 400 401 402 403
  }
  return cdata; /* new value */
}
/*}}}*/
#if 0
static int compare_tokens(const void *a, const void *b)/*{{{*/
{
  const struct token **aa = (const struct token **) a;
  const struct token **bb = (const struct token **) b;
  return strcmp((*aa)->text, (*bb)->text);
}
/*}}}*/
#endif
404

405
static  char *write_mbox_headers(struct database *db, struct write_map *map, unsigned int *uidata, char *data, char *cdata)/*{{{*/
406 407
{
  int i, len;
408
  char *start_cdata = cdata;
Richard P. Curnow's avatar
Richard P. Curnow committed
409

410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
  for (i=0; i<db->n_mboxen; i++) {
    struct mbox *mb = &db->mboxen[i];
    uidata[map->mbox_entries_offset + i] = mb->n_msgs;
    uidata[map->mbox_mtime_offset + i] = mb->current_mtime;
    uidata[map->mbox_size_offset  + i] = mb->current_size;
    if (mb->path) {
      uidata[map->mbox_paths_offset + i] = cdata - data;
      len = strlen(mb->path);
      memcpy(cdata, mb->path, 1+len);
      cdata += 1+len;
    } else {
      uidata[map->mbox_paths_offset + i] = 0;
    }
  }
  if (verbose) {
425
    printf("Wrote %d mbox headers (%d bytes of tables, %d bytes of paths)\n",
426
        db->n_mboxen, 4*4*db->n_mboxen, (int)(cdata - start_cdata));
427 428 429 430
  }
  return cdata;
}
/*}}}*/
431
static char * write_mbox_checksums(struct database *db, struct write_map *map, unsigned int *uidata, char *data, char *cdata)/*{{{*/
432 433
{
  int i, j;
434
  char *start_cdata = cdata;
435 436 437 438 439 440 441 442 443 444 445

  for (i=0; i<db->n_mboxen; i++) {
    struct mbox *mb = &db->mboxen[i];
    uidata[map->mbox_checksum_offset + i] = cdata - data;
    for (j=0; j<mb->n_msgs; j++) {
      memcpy(cdata, mb->check_all[j], sizeof(checksum_t));
      cdata += sizeof(checksum_t);
    }
  }
  if (verbose) {
    printf("Wrote %d bytes of mbox message checksums\n",
446
           (int)(cdata - start_cdata));
447 448 449 450 451
  }
  return cdata;
}
/*}}}*/

Richard Curnow's avatar
Richard Curnow committed
452 453 454 455 456 457 458 459
static char *write_toktable(struct toktable *tab, struct write_map_toktable *map, unsigned int *uidata, char *data, char *cdata, char *header_name)/*{{{*/
{
  int i, j, n, max;
  char *start_cdata, *mid_cdata;
  struct token **stok;
  stok = new_array(struct token *, tab->n);
  max = tab->size;
  n = tab->n;
Richard P. Curnow's avatar
Richard P. Curnow committed
460

Richard Curnow's avatar
Richard Curnow committed
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
  for (i=0, j=0; i<max; i++) {
    struct token *tok = tab->tokens[i];
    if (tok) {
      stok[j++] = tok;
    }
  }

  assert(j == n);

#if 0
  /* The search functions don't rely on the tokens being sorted.  So not
   * sorting here will save time. */
  qsort(stok, n, sizeof(struct token *), compare_tokens);
#endif

  start_cdata = cdata;

  /* FIXME : Eventually, the tokens have to be sorted - need to feed them from
   * a different data structure (array with no holes) */
  for (i=0; i<n; i++) {
    int slen;
    uidata[map->tok_offset + i] = cdata - data;
    slen = strlen(stok[i]->text);
    memcpy(cdata, stok[i]->text, 1 + slen);
    cdata += (1 + slen);
  }

  mid_cdata = cdata;

  for (i=0; i<n; i++) {
    int dlen;
492
    dlen = stok[i]->match0.n;
Richard Curnow's avatar
Richard Curnow committed
493
    uidata[map->enc_offset + i] = cdata - data;
494 495 496 497 498 499
    memcpy(cdata, stok[i]->match0.msginfo, dlen);
    cdata += dlen;
    *cdata++ = 0xff; /* termination character */
  }

  if (verbose) {
500
    printf("%s: Wrote %d tokens (%d bytes of tables, %d bytes of text, %d bytes of hit encoding)\n",
501
            header_name, n, 2*4*n, (int)(mid_cdata - start_cdata), (int)(cdata - mid_cdata));
502 503 504 505 506 507 508 509 510 511 512 513 514 515
  }

  free(stok);
  return cdata;
}
/*}}}*/
static char *write_toktable2(struct toktable2 *tab, struct write_map_toktable2 *map, unsigned int *uidata, char *data, char *cdata, char *header_name)/*{{{*/
{
  int i, j, n, max;
  char *start_cdata, *mid_cdata;
  struct token2 **stok;
  stok = new_array(struct token2 *, tab->n);
  max = tab->size;
  n = tab->n;
Richard P. Curnow's avatar
Richard P. Curnow committed
516

517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
  for (i=0, j=0; i<max; i++) {
    struct token2 *tok = tab->tokens[i];
    if (tok) {
      stok[j++] = tok;
    }
  }

  assert(j == n);

#if 0
  /* The search functions don't rely on the tokens being sorted.  So not
   * sorting here will save time. */
  qsort(stok, n, sizeof(struct token *), compare_tokens);
#endif

  start_cdata = cdata;

  /* FIXME : Eventually, the tokens have to be sorted - need to feed them from
   * a different data structure (array with no holes) */
  for (i=0; i<n; i++) {
    int slen;
    uidata[map->tok_offset + i] = cdata - data;
    slen = strlen(stok[i]->text);
    memcpy(cdata, stok[i]->text, 1 + slen);
    cdata += (1 + slen);
  }

  mid_cdata = cdata;

  for (i=0; i<n; i++) {
    int dlen;
    dlen = stok[i]->match0.n;
    uidata[map->enc0_offset + i] = cdata - data;
    memcpy(cdata, stok[i]->match0.msginfo, dlen);
    cdata += dlen;
    *cdata++ = 0xff; /* termination character */
  }

  for (i=0; i<n; i++) {
    int dlen;
    dlen = stok[i]->match1.n;
    uidata[map->enc1_offset + i] = cdata - data;
    memcpy(cdata, stok[i]->match1.msginfo, dlen);
Richard Curnow's avatar
Richard Curnow committed
560 561 562 563 564
    cdata += dlen;
    *cdata++ = 0xff; /* termination character */
  }

  if (verbose) {
565
    printf("%s: Wrote %d tokens (%d bytes of tables, %d bytes of text, %d bytes of hit encoding)\n",
566
            header_name, n, 2*4*n, (int)(mid_cdata - start_cdata), (int)(cdata - mid_cdata));
Richard Curnow's avatar
Richard Curnow committed
567 568 569 570 571 572
  }

  free(stok);
  return cdata;
}
/*}}}*/
573
void write_database(struct database *db, char *filename, int do_integrity_checks)/*{{{*/
Richard Curnow's avatar
Richard Curnow committed
574 575
{
  int file_len;
576
  int fd;
Richard Curnow's avatar
Richard Curnow committed
577 578 579 580
  char *data, *cdata;
  unsigned int *uidata;
  struct write_map map;

581 582 583
  if (do_integrity_checks) {
    check_database_integrity(db);
  }
584 585

  if (!verify_mbox_size_constraints(db)) {
586
    unlock_and_exit(1);
587
  }
Richard P. Curnow's avatar
Richard P. Curnow committed
588

Richard Curnow's avatar
Richard Curnow committed
589 590
  /* Work out mappings */
  compute_mapping(db, &map);
Richard P. Curnow's avatar
Richard P. Curnow committed
591

Richard Curnow's avatar
Richard Curnow committed
592
  file_len = char_length(db) + (4 * map.beyond_last_ui_offset);
Richard P. Curnow's avatar
Richard P. Curnow committed
593

594
  create_rw_mapping(filename, file_len, &fd, &data);
Richard Curnow's avatar
Richard Curnow committed
595 596 597 598
  uidata = (unsigned int *) data; /* align(int) < align(page)! */
  cdata = data + (4 * map.beyond_last_ui_offset);

  write_header(data, uidata, db, &map);
599
  cdata = write_type_and_flag_table(db, uidata, data, cdata);
600 601 602
  cdata = write_messages(db, &map, uidata, data, cdata);
  cdata = write_mbox_headers(db, &map, uidata, data, cdata);
  cdata = write_mbox_checksums(db, &map, uidata, data, cdata);
Richard Curnow's avatar
Richard Curnow committed
603 604 605 606 607
  cdata = write_toktable(db->to, &map.to, uidata, data, cdata, "To");
  cdata = write_toktable(db->cc, &map.cc, uidata, data, cdata, "Cc");
  cdata = write_toktable(db->from, &map.from, uidata, data, cdata, "From");
  cdata = write_toktable(db->subject, &map.subject, uidata, data, cdata, "Subject");
  cdata = write_toktable(db->body, &map.body, uidata, data, cdata, "Body");
608
  cdata = write_toktable(db->attachment_name, &map.attachment_name, uidata, data, cdata, "Attachment Name");
609
  cdata = write_toktable2(db->msg_ids, &map.msg_ids, uidata, data, cdata, "(Threading)");
Richard P. Curnow's avatar
Richard P. Curnow committed
610

Richard Curnow's avatar
Richard Curnow committed
611 612 613
  /* Write data */
  /* Unmap / close file */
  if (munmap(data, file_len) < 0) {
614
    report_error("munmap", filename);
615
    unlock_and_exit(2);
Richard Curnow's avatar
Richard Curnow committed
616
  }
617
  if (fsync(fd) < 0) {
618
    report_error("fsync", filename);
619
    unlock_and_exit(2);
620 621
  }
  if (close(fd) < 0) {
622
    report_error("close", filename);
623
    unlock_and_exit(2);
624
  }
Richard Curnow's avatar
Richard Curnow committed
625 626
}
  /*}}}*/