mbox-append.c revision 1969b0cbe66efb9a81ec2404f4903d7844e63729
2e37d45867d081db150ab78dad303b9077aea24fTimo Sirainen/* Copyright (C) 2002 Timo Sirainen */
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainenmail_index_record_append(MailIndex *index, time_t internal_date,
a0c453a8edaec90fb0d945c874de0b1845bc7d7eTimo Sirainenstatic void mbox_read_message(IOBuffer *inbuf, unsigned int *virtual_size)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen unsigned char *msg;
85a4ae7e8df7ea45a7665828e5edf48a5fc85730Timo Sirainen /* read until "[\r]\nFrom " is found */
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainen while (io_buffer_read_data(inbuf, &msg, &size, startpos) >= 0) {
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen /* missing CR */
85a4ae7e8df7ea45a7665828e5edf48a5fc85730Timo Sirainen /* See if it's space after "From" */
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen /* yes, see if we had \r too */
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen /* end of file, remove the last [\r]\n */
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainenstatic int mbox_index_append_next(MailIndex *index, IOBuffer *inbuf)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen uoff_t abs_start_offset, stop_offset, old_size;
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen /* get the From-line */
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen while (io_buffer_read_data(inbuf, &data, &size, pos) >= 0) {
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen if (pos == size || size <= 5 || strncmp(data, "From ", 5) != 0) {
88dc563319efecd6e68bad16b0d92672da05584aTimo Sirainen /* a) no \n found, or line too long
a0c453a8edaec90fb0d945c874de0b1845bc7d7eTimo Sirainen b) not a From-line */
88dc563319efecd6e68bad16b0d92672da05584aTimo Sirainen index_set_error(index, "Error indexing mbox file %s: "
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen "From-line not found where expected",
88dc563319efecd6e68bad16b0d92672da05584aTimo Sirainen /* parse the From-line */
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen internal_date = mbox_from_parse_date(data, size);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen abs_start_offset = inbuf->start_offset + inbuf->offset;
88dc563319efecd6e68bad16b0d92672da05584aTimo Sirainen /* now, find the ending "[\r]\nFrom " */
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen /* add message to index */
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen rec = mail_index_record_append(index, internal_date, virtual_size);
88dc563319efecd6e68bad16b0d92672da05584aTimo Sirainen /* location = offset to beginning of headers in message */
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen index->update_field_raw(update, FIELD_TYPE_LOCATION,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen /* parse the header and cache wanted fields. get the message flags
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen from Status and X-Status fields. temporarily limit the buffer size
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen so the message body is parsed properly (FIXME: does this have
a0c453a8edaec90fb0d945c874de0b1845bc7d7eTimo Sirainen side effects?) */
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen io_buffer_seek(inbuf, abs_start_offset - inbuf->start_offset);
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen mail_index_update_headers(update, inbuf, 0, mbox_header_func, &ctx);
88dc563319efecd6e68bad16b0d92672da05584aTimo Sirainen /* save MD5 */
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen binary_to_hex(md5_digest, sizeof(md5_digest)), 0);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen /* failed - delete the record */
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen /* save message flags, after location field is saved */
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen if (!index->update_flags(index, rec, 0, ctx.flags, FALSE))
d54ab8987e482a8df250615b44f41fa040c38741Timo Sirainenint mbox_index_append(MailIndex *index, IOBuffer *inbuf)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen /* no new data */
64b61cd24d630223478ccbe1934b9f60f0881f59Timo Sirainen if (!index->set_lock(index, MAIL_LOCK_EXCLUSIVE))
437a8b0fe254057b0c1f1723d689bafa91cae2abTimo Sirainen if (inbuf->start_offset + inbuf->offset != 0) {
64b61cd24d630223478ccbe1934b9f60f0881f59Timo Sirainen /* we're at the [\r]\n before the From-line,
64b61cd24d630223478ccbe1934b9f60f0881f59Timo Sirainen "Error indexing mbox file %s: "
64b61cd24d630223478ccbe1934b9f60f0881f59Timo Sirainen "LF not found where expected",