2 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /* This file contains code to parse ``mbox'' and ``mmdf'' style mailboxes */
40 /* struct used by mutt_sync_mailbox() to store new offsets */
51 * ctx - context to lock
52 * excl - exclusive lock?
53 * retry - should retry if unable to lock?
55 int mbox_lock_mailbox (CONTEXT *ctx, int excl, int retry)
59 if ((r = mx_lock_file (ctx->path, fileno (ctx->fp), excl, 1, retry)) == 0)
61 else if (retry && !excl)
70 void mbox_unlock_mailbox (CONTEXT *ctx)
76 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
81 int mmdf_parse_mailbox (CONTEXT *ctx)
83 char buf[HUGE_STRING];
84 char return_path[LONG_STRING];
85 int count = 0, oldmsgcount = ctx->msgcount;
91 #ifdef NFS_ATTRIBUTE_HACK
92 struct utimbuf newtime;
95 if (stat (ctx->path, &sb) == -1)
97 mutt_perror (ctx->path);
100 ctx->mtime = sb.st_mtime;
101 ctx->size = sb.st_size;
103 #ifdef NFS_ATTRIBUTE_HACK
104 if (sb.st_mtime > sb.st_atime)
106 newtime.modtime = sb.st_mtime;
107 newtime.actime = time (NULL);
108 utime (ctx->path, &newtime);
112 /* precompute the local timezone to speed up calculation of the
114 tz = mutt_local_tz (0);
116 buf[sizeof (buf) - 1] = 0;
120 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
123 if (mutt_strcmp (buf, MMDF_SEP) == 0)
125 loc = ftell (ctx->fp);
128 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
129 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
130 loc / (ctx->size / 100 + 1));
133 if (ctx->msgcount == ctx->hdrmax)
134 mx_alloc_memory (ctx);
135 ctx->hdrs[ctx->msgcount] = hdr = mutt_new_header ();
137 hdr->index = ctx->msgcount;
139 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
141 /* TODO: memory leak??? */
142 dprint (1, (debugfile, "mmdf_parse_mailbox: unexpected EOF\n"));
148 if (!is_from (buf, return_path, sizeof (return_path), &t))
150 if (fseek (ctx->fp, loc, SEEK_SET) != 0)
152 dprint (1, (debugfile, "mmdf_parse_mailbox: fseek() failed\n"));
153 mutt_error _("Mailbox is corrupt!");
158 hdr->received = t - tz;
160 hdr->env = mutt_read_rfc822_header (ctx->fp, hdr, 0, 0);
162 loc = ftell (ctx->fp);
164 if (hdr->content->length > 0 && hdr->lines > 0)
166 tmploc = loc + hdr->content->length;
168 if (0 < tmploc && tmploc < ctx->size)
170 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0 ||
171 fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL ||
172 mutt_strcmp (MMDF_SEP, buf) != 0)
174 if (fseek (ctx->fp, loc, SEEK_SET) != 0)
175 dprint (1, (debugfile, "mmdf_parse_mailbox: fseek() failed\n"));
176 hdr->content->length = -1;
180 hdr->content->length = -1;
183 hdr->content->length = -1;
185 if (hdr->content->length < 0)
189 loc = ftell (ctx->fp);
190 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
193 } while (mutt_strcmp (buf, MMDF_SEP) != 0);
196 hdr->content->length = loc - hdr->content->offset;
199 if (!hdr->env->return_path && return_path[0])
200 hdr->env->return_path = rfc822_parse_adrlist (hdr->env->return_path, return_path);
203 hdr->env->from = rfc822_cpy_adr (hdr->env->return_path);
209 dprint (1, (debugfile, "mmdf_parse_mailbox: corrupt mailbox!\n"));
210 mutt_error _("Mailbox is corrupt!");
215 if (ctx->msgcount > oldmsgcount)
216 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
221 /* Note that this function is also called when new mail is appended to the
222 * currently open folder, and NOT just when the mailbox is initially read.
224 * NOTE: it is assumed that the mailbox being read has been locked before
225 * this routine gets called. Strange things could happen if it's not!
227 int mbox_parse_mailbox (CONTEXT *ctx)
230 char buf[HUGE_STRING], return_path[STRING];
233 int count = 0, lines = 0;
235 #ifdef NFS_ATTRIBUTE_HACK
236 struct utimbuf newtime;
239 /* Save information about the folder at the time we opened it. */
240 if (stat (ctx->path, &sb) == -1)
242 mutt_perror (ctx->path);
246 ctx->size = sb.st_size;
247 ctx->mtime = sb.st_mtime;
249 #ifdef NFS_ATTRIBUTE_HACK
250 if (sb.st_mtime > sb.st_atime)
252 newtime.modtime = sb.st_mtime;
253 newtime.actime = time (NULL);
254 utime (ctx->path, &newtime);
259 ctx->readonly = access (ctx->path, W_OK) ? 1 : 0;
261 /* precompute the local timezone to speed up calculation of the
263 tz = mutt_local_tz (0);
265 loc = ftell (ctx->fp);
266 while (fgets (buf, sizeof (buf), ctx->fp) != NULL)
268 if (is_from (buf, return_path, sizeof (return_path), &t))
270 /* Save the Content-Length of the previous message */
273 #define PREV ctx->hdrs[ctx->msgcount-1]
275 if (PREV->content->length < 0)
277 PREV->content->length = loc - PREV->content->offset - 1;
278 if (PREV->content->length < 0)
279 PREV->content->length = 0;
282 PREV->lines = lines ? lines - 1 : 0;
287 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
288 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
289 ftell (ctx->fp) / (ctx->size / 100 + 1));
291 if (ctx->msgcount == ctx->hdrmax)
292 mx_alloc_memory (ctx);
294 curhdr = ctx->hdrs[ctx->msgcount] = mutt_new_header ();
295 curhdr->received = t - tz;
296 curhdr->offset = loc;
297 curhdr->index = ctx->msgcount;
299 curhdr->env = mutt_read_rfc822_header (ctx->fp, curhdr, 0, 0);
301 /* if we know how long this message is, either just skip over the body,
302 * or if we don't know how many lines there are, count them now (this will
303 * save time by not having to search for the next message marker).
305 if (curhdr->content->length > 0)
309 loc = ftell (ctx->fp);
310 tmploc = loc + curhdr->content->length + 1;
312 if (0 < tmploc && tmploc < ctx->size)
315 * check to see if the content-length looks valid. we expect to
316 * to see a valid message separator at this point in the stream
318 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0 ||
319 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
320 mutt_strncmp ("From ", buf, 5) != 0)
322 dprint (1, (debugfile, "mbox_parse_mailbox: bad content-length in message %d (cl=%ld)\n", curhdr->index, curhdr->content->length));
323 dprint (1, (debugfile, "\tLINE: %s", buf));
324 if (fseek (ctx->fp, loc, SEEK_SET) != 0) /* nope, return the previous position */
326 dprint (1, (debugfile, "mbox_parse_mailbox: fseek() failed\n"));
328 curhdr->content->length = -1;
331 else if (tmploc != ctx->size)
333 /* content-length would put us past the end of the file, so it
336 curhdr->content->length = -1;
339 if (curhdr->content->length != -1)
341 /* good content-length. check to see if we know how many lines
342 * are in this message.
344 if (curhdr->lines == 0)
346 int cl = curhdr->content->length;
348 /* count the number of lines in this message */
349 if (fseek (ctx->fp, loc, SEEK_SET) != 0)
350 dprint (1, (debugfile, "mbox_parse_mailbox: fseek() failed\n"));
353 if (fgetc (ctx->fp) == '\n')
358 /* return to the offset of the next message separator */
359 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0)
360 dprint (1, (debugfile, "mbox_parse_mailbox: fseek() failed\n"));
366 if (!curhdr->env->return_path && return_path[0])
367 curhdr->env->return_path = rfc822_parse_adrlist (curhdr->env->return_path, return_path);
369 if (!curhdr->env->from)
370 curhdr->env->from = rfc822_cpy_adr (curhdr->env->return_path);
377 loc = ftell (ctx->fp);
381 * Only set the content-length of the previous message if we have read more
382 * than one message during _this_ invocation. If this routine is called
383 * when new mail is received, we need to make sure not to clobber what
384 * previously was the last message since the headers may be sorted.
388 if (PREV->content->length < 0)
390 PREV->content->length = ftell (ctx->fp) - PREV->content->offset - 1;
391 if (PREV->content->length < 0)
392 PREV->content->length = 0;
396 PREV->lines = lines ? lines - 1 : 0;
398 mx_update_context (ctx, count);
406 /* open a mbox or mmdf style mailbox */
407 int mbox_open_mailbox (CONTEXT *ctx)
411 if ((ctx->fp = fopen (ctx->path, "r")) == NULL)
413 mutt_perror (ctx->path);
416 mutt_block_signals ();
417 if (mbox_lock_mailbox (ctx, 0, 1) == -1)
419 mutt_unblock_signals ();
423 if (ctx->magic == M_MBOX)
424 rc = mbox_parse_mailbox (ctx);
425 else if (ctx->magic == M_MMDF)
426 rc = mmdf_parse_mailbox (ctx);
430 mbox_unlock_mailbox (ctx);
431 mutt_unblock_signals ();
435 /* return 1 if address lists are strictly identical */
436 static int strict_addrcmp (const ADDRESS *a, const ADDRESS *b)
440 if (mutt_strcmp (a->mailbox, b->mailbox) ||
441 mutt_strcmp (a->personal, b->personal))
453 static int strict_cmp_lists (const LIST *a, const LIST *b)
457 if (mutt_strcmp (a->data, b->data))
469 static int strict_cmp_envelopes (const ENVELOPE *e1, const ENVELOPE *e2)
473 if (mutt_strcmp (e1->message_id, e2->message_id) ||
474 mutt_strcmp (e1->subject, e2->subject) ||
475 !strict_cmp_lists (e1->references, e2->references) ||
476 !strict_addrcmp (e1->from, e2->from) ||
477 !strict_addrcmp (e1->sender, e2->sender) ||
478 !strict_addrcmp (e1->reply_to, e2->reply_to) ||
479 !strict_addrcmp (e1->to, e2->to) ||
480 !strict_addrcmp (e1->cc, e2->cc) ||
481 !strict_addrcmp (e1->return_path, e2->return_path))
488 if (e1 == NULL && e2 == NULL)
495 static int strict_cmp_parameters (const PARAMETER *p1, const PARAMETER *p2)
499 if (mutt_strcmp (p1->attribute, p2->attribute) ||
500 mutt_strcmp (p1->value, p2->value))
512 static int strict_cmp_bodies (const BODY *b1, const BODY *b2)
514 if (b1->type != b2->type ||
515 b1->encoding != b2->encoding ||
516 mutt_strcmp (b1->subtype, b2->subtype) ||
517 mutt_strcmp (b1->description, b2->description) ||
518 !strict_cmp_parameters (b1->parameter, b2->parameter) ||
519 b1->length != b2->length)
524 /* return 1 if headers are strictly identical */
525 int mbox_strict_cmp_headers (const HEADER *h1, const HEADER *h2)
529 if (h1->received != h2->received ||
530 h1->date_sent != h2->date_sent ||
531 h1->content->length != h2->content->length ||
532 h1->lines != h2->lines ||
533 h1->zhours != h2->zhours ||
534 h1->zminutes != h2->zminutes ||
535 h1->zoccident != h2->zoccident ||
536 h1->mime != h2->mime ||
537 !strict_cmp_envelopes (h1->env, h2->env) ||
538 !strict_cmp_bodies (h1->content, h2->content))
545 if (h1 == NULL && h2 == NULL)
552 /* check to see if the mailbox has changed on disk.
555 * M_REOPENED mailbox has been reopened
556 * M_NEW_MAIL new mail has arrived!
557 * M_LOCKED couldn't lock the file
561 int mbox_check_mailbox (CONTEXT *ctx, int *index_hint)
564 char buffer[LONG_STRING];
568 if (stat (ctx->path, &st) == 0)
570 if (st.st_mtime == ctx->mtime && st.st_size == ctx->size)
573 if (st.st_size == ctx->size)
575 /* the file was touched, but it is still the same length, so just exit */
576 ctx->mtime = st.st_mtime;
580 if (st.st_size > ctx->size)
582 /* lock the file if it isn't already */
585 mutt_block_signals ();
586 if (mbox_lock_mailbox (ctx, 0, 0) == -1)
588 mutt_unblock_signals ();
589 /* we couldn't lock the mailbox, but nothing serious happened:
590 * probably the new mail arrived: no reason to wait till we can
591 * parse it: we'll get it on the next pass
599 * Check to make sure that the only change to the mailbox is that
600 * message(s) were appended to this file. My heuristic is that we should
601 * see the message separator at *exactly* what used to be the end of the
604 if (fseek (ctx->fp, ctx->size, SEEK_SET) != 0)
605 dprint (1, (debugfile, "mbox_check_mailbox: fseek() failed\n"));
606 if (fgets (buffer, sizeof (buffer), ctx->fp) != NULL)
608 if ((ctx->magic == M_MBOX && mutt_strncmp ("From ", buffer, 5) == 0) ||
609 (ctx->magic == M_MMDF && mutt_strcmp (MMDF_SEP, buffer) == 0))
611 if (fseek (ctx->fp, ctx->size, SEEK_SET) != 0)
612 dprint (1, (debugfile, "mbox_check_mailbox: fseek() failed\n"));
613 if (ctx->magic == M_MBOX)
614 mbox_parse_mailbox (ctx);
616 mmdf_parse_mailbox (ctx);
618 /* Only unlock the folder if it was locked inside of this routine.
619 * It may have been locked elsewhere, like in
620 * mutt_checkpoint_mailbox().
625 mbox_unlock_mailbox (ctx);
626 mutt_unblock_signals ();
629 return (M_NEW_MAIL); /* signal that new mail arrived */
636 dprint (1, (debugfile, "mbox_check_mailbox: fgets returned NULL.\n"));
646 if (mutt_reopen_mailbox (ctx, index_hint) != -1)
650 mbox_unlock_mailbox (ctx);
651 mutt_unblock_signals ();
659 mbox_unlock_mailbox (ctx);
660 mx_fastclose_mailbox (ctx);
661 mutt_unblock_signals ();
662 mutt_error _("Mailbox was corrupted!");
670 int mbox_sync_mailbox (CONTEXT *ctx, int *index_hint)
672 char tempfile[_POSIX_PATH_MAX];
674 int i, j, save_sort = SORT_ORDER;
676 int need_sort = 0; /* flag to resort mailbox if new mail arrives */
677 int first = -1; /* first message to be written */
678 long offset; /* location in mailbox to write changed messages */
680 struct utimbuf utimebuf;
681 struct m_update_t *newOffset = NULL;
682 struct m_update_t *oldOffset = NULL;
685 /* sort message by their position in the mailbox on disk */
686 if (Sort != SORT_ORDER)
690 mutt_sort_headers (ctx, 0);
693 /* need to open the file for writing in such a way that it does not truncate
694 * the file, so use read-write mode.
696 if ((ctx->fp = freopen (ctx->path, "r+", ctx->fp)) == NULL)
698 mx_fastclose_mailbox (ctx);
699 mutt_error _("Fatal error! Could not reopen mailbox!");
703 mutt_block_signals ();
705 if (mbox_lock_mailbox (ctx, 1, 1) == -1)
707 mutt_unblock_signals ();
708 mutt_error _("Unable to lock mailbox!");
712 /* Check to make sure that the file hasn't changed on disk */
713 if ((i = mbox_check_mailbox (ctx, index_hint)) == M_NEW_MAIL || i == M_REOPENED)
715 /* new mail arrived, or mailbox reopened */
727 /* Create a temporary file to write the new version of the mailbox in. */
728 mutt_mktemp (tempfile);
729 if ((i = open (tempfile, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1 ||
730 (fp = fdopen (i, "w")) == NULL)
737 mutt_error _("Could not create temporary file!");
742 /* find the first deleted/changed message. we save a lot of time by only
743 * rewriting the mailbox from the point where it has actually changed.
745 for (i = 0 ; i < ctx->msgcount && !ctx->hdrs[i]->deleted &&
746 !ctx->hdrs[i]->changed && !ctx->hdrs[i]->attach_del; i++)
748 if (i == ctx->msgcount)
750 /* this means ctx->changed or ctx->deleted was set, but no
751 * messages were found to be changed or deleted. This should
752 * never happen, is we presume it is a bug in mutt.
754 mutt_error _("sync: mbox modified, but no modified messages! (report this bug)");
755 mutt_sleep(5); /* the mutt_error /will/ get cleared! */
756 dprint(1, (debugfile, "mbox_sync_mailbox(): no modified messages.\n"));
761 /* save the index of the first changed/deleted message */
763 /* where to start overwriting */
764 offset = ctx->hdrs[i]->offset;
766 /* the offset stored in the header does not include the MMDF_SEP, so make
767 * sure we seek to the correct location
769 if (ctx->magic == M_MMDF)
770 offset -= (sizeof MMDF_SEP - 1);
772 /* allocate space for the new offsets */
773 newOffset = safe_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
774 oldOffset = safe_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
776 for (i = first, j = 0; i < ctx->msgcount; i++)
779 * back up some information which is needed to restore offsets when
783 oldOffset[i-first].valid = 1;
784 oldOffset[i-first].hdr = ctx->hdrs[i]->offset;
785 oldOffset[i-first].body = ctx->hdrs[i]->content->offset;
786 oldOffset[i-first].lines = ctx->hdrs[i]->lines;
787 oldOffset[i-first].length = ctx->hdrs[i]->content->length;
789 if (! ctx->hdrs[i]->deleted)
792 if (!ctx->quiet && WriteInc && ((i % WriteInc) == 0 || j == 1))
793 mutt_message (_("Writing messages... %d (%d%%)"), i,
794 ftell (ctx->fp) / (ctx->size / 100 + 1));
796 if (ctx->magic == M_MMDF)
798 if (fputs (MMDF_SEP, fp) == EOF)
800 mutt_perror (tempfile);
808 /* save the new offset for this message. we add `offset' because the
809 * temporary file only contains saved message which are located after
810 * `offset' in the real mailbox
812 newOffset[i - first].hdr = ftell (fp) + offset;
814 if (mutt_copy_message (fp, ctx, ctx->hdrs[i], M_CM_UPDATE, CH_FROM | CH_UPDATE | CH_UPDATE_LEN) == -1)
816 mutt_perror (tempfile);
822 /* Since messages could have been deleted, the offsets stored in memory
823 * will be wrong, so update what we can, which is the offset of this
824 * message, and the offset of the body. If this is a multipart message,
825 * we just flush the in memory cache so that the message will be reparsed
826 * if the user accesses it later.
828 newOffset[i - first].body = ftell (fp) - ctx->hdrs[i]->content->length + offset;
829 mutt_free_body (&ctx->hdrs[i]->content->parts);
834 if(fputs(MMDF_SEP, fp) == EOF)
836 mutt_perror (tempfile);
843 if(fputs("\n", fp) == EOF)
845 mutt_perror (tempfile);
854 if (fclose (fp) != 0)
857 dprint(1, (debugfile, "mbox_sync_mailbox: fclose() returned non-zero.\n"));
859 mutt_perror (tempfile);
865 /* Save the state of this folder. */
866 if (stat (ctx->path, &statbuf) == -1)
868 mutt_perror (ctx->path);
874 if ((fp = fopen (tempfile, "r")) == NULL)
876 mutt_unblock_signals ();
877 mx_fastclose_mailbox (ctx);
878 dprint (1, (debugfile, "mbox_sync_mailbox: unable to reopen temp copy of mailbox!\n"));
879 mutt_perror (tempfile);
884 if (fseek (ctx->fp, offset, SEEK_SET) != 0 || /* seek the append location */
885 /* do a sanity check to make sure the mailbox looks ok */
886 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
887 (ctx->magic == M_MBOX && mutt_strncmp ("From ", buf, 5) != 0) ||
888 (ctx->magic == M_MMDF && mutt_strcmp (MMDF_SEP, buf) != 0))
890 dprint (1, (debugfile, "mbox_sync_mailbox: message not in expected position."));
891 dprint (1, (debugfile, "\tLINE: %s\n", buf));
896 if (fseek (ctx->fp, offset, SEEK_SET) != 0) /* return to proper offset */
899 dprint (1, (debugfile, "mbox_sync_mailbox: fseek() failed\n"));
903 /* copy the temp mailbox back into place starting at the first
904 * change/deleted message
906 mutt_message _("Committing changes...");
907 i = mutt_copy_stream (fp, ctx->fp);
909 if (ferror (ctx->fp))
914 ctx->size = ftell (ctx->fp); /* update the size of the mailbox */
915 ftruncate (fileno (ctx->fp), ctx->size);
921 mbox_unlock_mailbox (ctx);
923 if (fclose (ctx->fp) != 0 || i == -1)
925 /* error occured while writing the mailbox back, so keep the temp copy
929 char savefile[_POSIX_PATH_MAX];
931 snprintf (savefile, sizeof (savefile), "%s/mutt.%s-%s-%d",
932 NONULL (Tempdir), NONULL(Username), NONULL(Hostname), getpid ());
933 rename (tempfile, savefile);
934 mutt_unblock_signals ();
935 mx_fastclose_mailbox (ctx);
936 mutt_pretty_mailbox (savefile);
937 mutt_error (_("Write failed! Saved partial mailbox to %s"), savefile);
942 /* Restore the previous access/modification times */
943 utimebuf.actime = statbuf.st_atime;
944 utimebuf.modtime = statbuf.st_mtime;
945 utime (ctx->path, &utimebuf);
947 /* reopen the mailbox in read-only mode */
948 if ((ctx->fp = fopen (ctx->path, "r")) == NULL)
951 mutt_unblock_signals ();
952 mx_fastclose_mailbox (ctx);
953 mutt_error _("Fatal error! Could not reopen mailbox!");
958 /* update the offsets of the rewritten messages */
959 for (i = first, j = first; i < ctx->msgcount; i++)
961 if (!ctx->hdrs[i]->deleted)
963 ctx->hdrs[i]->offset = newOffset[i - first].hdr;
964 ctx->hdrs[i]->content->hdr_offset = newOffset[i - first].hdr;
965 ctx->hdrs[i]->content->offset = newOffset[i - first].body;
966 ctx->hdrs[i]->index = j++;
971 unlink (tempfile); /* remove partial copy of the mailbox */
972 mutt_unblock_signals ();
973 Sort = save_sort; /* Restore the default value. */
975 return (0); /* signal success */
977 bail: /* Come here in case of disaster */
981 /* restore offsets, as far as they are valid */
982 if (first >= 0 && oldOffset)
984 for (i = first; i < ctx->msgcount && oldOffset[i-first].valid; i++)
986 ctx->hdrs[i]->offset = oldOffset[i-first].hdr;
987 ctx->hdrs[i]->content->hdr_offset = oldOffset[i-first].hdr;
988 ctx->hdrs[i]->content->offset = oldOffset[i-first].body;
989 ctx->hdrs[i]->lines = oldOffset[i-first].lines;
990 ctx->hdrs[i]->content->length = oldOffset[i-first].length;
994 /* this is ok to call even if we haven't locked anything */
995 mbox_unlock_mailbox (ctx);
997 mutt_unblock_signals ();
1001 if ((ctx->fp = freopen (ctx->path, "r", ctx->fp)) == NULL)
1003 mutt_error _("Could not reopen mailbox!");
1004 mx_fastclose_mailbox (ctx);
1008 if (need_sort || save_sort != Sort)
1011 /* if the mailbox was reopened, the thread tree will be invalid so make
1012 * sure to start threading from scratch. */
1013 mutt_sort_headers (ctx, (need_sort == M_REOPENED));
1019 /* close a mailbox opened in write-mode */
1020 int mbox_close_mailbox (CONTEXT *ctx)
1022 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
1024 #ifdef USE_COMPRESSED
1025 if (ctx->compressinfo)
1026 mutt_slow_close_compressed (ctx);
1029 mutt_unblock_signals ();
1030 mx_fastclose_mailbox (ctx);
1034 int mutt_reopen_mailbox (CONTEXT *ctx, int *index_hint)
1036 int (*cmp_headers) (const HEADER *, const HEADER *) = NULL;
1044 /* silent operations */
1047 mutt_message _("Reopening mailbox...");
1049 /* our heuristics require the old mailbox to be unsorted */
1050 if (Sort != SORT_ORDER)
1056 mutt_sort_headers (ctx, 1);
1063 /* simulate a close */
1065 hash_destroy (&ctx->id_hash, NULL);
1067 hash_destroy (&ctx->subj_hash, NULL);
1068 mutt_clear_threads (ctx);
1072 for (i = 0; i < ctx->msgcount; i++)
1073 mutt_free_header (&(ctx->hdrs[i])); /* nothing to do! */
1078 /* save the old headers */
1079 old_msgcount = ctx->msgcount;
1080 old_hdrs = ctx->hdrs;
1084 ctx->hdrmax = 0; /* force allocation of new headers */
1093 ctx->id_hash = NULL;
1094 ctx->subj_hash = NULL;
1100 if (fseek (ctx->fp, 0, SEEK_SET) != 0)
1102 dprint (1, (debugfile, "mutt_reopen_mailbox: fseek() failed\n"));
1107 cmp_headers = mbox_strict_cmp_headers;
1108 rc = ((ctx->magic == M_MBOX) ? mbox_parse_mailbox
1109 : mmdf_parse_mailbox) (ctx);
1120 /* free the old headers */
1121 for (j = 0; j < old_msgcount; j++)
1122 mutt_free_header (&(old_hdrs[j]));
1129 /* now try to recover the old flags */
1131 index_hint_set = (index_hint == NULL);
1135 for (i = 0; i < ctx->msgcount; i++)
1139 /* some messages have been deleted, and new messages have been
1140 * appended at the end; the heuristic is that old messages have then
1141 * "advanced" towards the beginning of the folder, so we begin the
1142 * search at index "i"
1144 for (j = i; j < old_msgcount; j++)
1146 if (old_hdrs[j] == NULL)
1148 if (cmp_headers (ctx->hdrs[i], old_hdrs[j]))
1156 for (j = 0; j < i && j < old_msgcount; j++)
1158 if (old_hdrs[j] == NULL)
1160 if (cmp_headers (ctx->hdrs[i], old_hdrs[j]))
1170 /* this is best done here */
1171 if (!index_hint_set && *index_hint == j)
1174 if (old_hdrs[j]->changed)
1176 /* Only update the flags if the old header was changed;
1177 * otherwise, the header may have been modified externally,
1178 * and we don't want to lose _those_ changes
1180 mutt_set_flag (ctx, ctx->hdrs[i], M_FLAG, old_hdrs[j]->flagged);
1181 mutt_set_flag (ctx, ctx->hdrs[i], M_REPLIED, old_hdrs[j]->replied);
1182 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, old_hdrs[j]->old);
1183 mutt_set_flag (ctx, ctx->hdrs[i], M_READ, old_hdrs[j]->read);
1185 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, old_hdrs[j]->deleted);
1186 mutt_set_flag (ctx, ctx->hdrs[i], M_TAG, old_hdrs[j]->tagged);
1188 /* we don't need this header any more */
1189 mutt_free_header (&(old_hdrs[j]));
1193 /* free the remaining old headers */
1194 for (j = 0; j < old_msgcount; j++)
1198 mutt_free_header (&(old_hdrs[j]));
1207 return ((ctx->changed || msg_mod) ? M_REOPENED : M_NEW_MAIL);
1212 * 1 if the mailbox is not empty
1213 * 0 if the mailbox is empty
1216 int mbox_check_empty (const char *path)
1220 if (stat (path, &st) == -1)
1223 return ((st.st_size == 0));