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 */
44 /* struct used by mutt_sync_mailbox() to store new offsets */
54 * ctx - context to lock
55 * excl - exclusive lock?
56 * retry - should retry if unable to lock?
58 int mbox_lock_mailbox (CONTEXT * ctx, int excl, int retry)
62 if ((r = mx_lock_file (ctx->path, fileno (ctx->fp), excl, 1, retry)) == 0)
64 else if (retry && !excl) {
72 void mbox_unlock_mailbox (CONTEXT * ctx)
77 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
82 int mmdf_parse_mailbox (CONTEXT * ctx)
84 char buf[HUGE_STRING];
85 char return_path[LONG_STRING];
86 int count = 0, oldmsgcount = ctx->msgcount;
93 #ifdef NFS_ATTRIBUTE_HACK
94 struct utimbuf newtime;
97 if (stat (ctx->path, &sb) == -1) {
98 mutt_perror (ctx->path);
101 ctx->mtime = sb.st_mtime;
102 ctx->size = sb.st_size;
104 #ifdef NFS_ATTRIBUTE_HACK
105 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;
119 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
122 if (mutt_strcmp (buf, MMDF_SEP) == 0) {
123 loc = ftell (ctx->fp);
126 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
127 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
128 (int) (loc / (ctx->size / 100 + 1)));
131 if (ctx->msgcount == ctx->hdrmax)
132 mx_alloc_memory (ctx);
133 ctx->hdrs[ctx->msgcount] = hdr = mutt_new_header ();
135 hdr->index = ctx->msgcount;
137 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL) {
138 /* TODO: memory leak??? */
139 dprint (1, (debugfile, "mmdf_parse_mailbox: unexpected EOF\n"));
145 if (!is_from (buf, return_path, sizeof (return_path), &t)) {
146 if (fseek (ctx->fp, loc, SEEK_SET) != 0) {
147 dprint (1, (debugfile, "mmdf_parse_mailbox: fseek() failed\n"));
148 mutt_error _("Mailbox is corrupt!");
154 hdr->received = t - tz;
156 hdr->env = mutt_read_rfc822_header (ctx->fp, hdr, 0, 0);
158 loc = ftell (ctx->fp);
160 if (hdr->content->length > 0 && hdr->lines > 0) {
161 tmploc = loc + hdr->content->length;
163 if (0 < tmploc && tmploc < ctx->size) {
164 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0 ||
165 fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL ||
166 mutt_strcmp (MMDF_SEP, buf) != 0) {
167 if (fseek (ctx->fp, loc, SEEK_SET) != 0)
168 dprint (1, (debugfile, "mmdf_parse_mailbox: fseek() failed\n"));
169 hdr->content->length = -1;
173 hdr->content->length = -1;
176 hdr->content->length = -1;
178 if (hdr->content->length < 0) {
181 loc = ftell (ctx->fp);
182 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
185 } while (mutt_strcmp (buf, MMDF_SEP) != 0);
188 hdr->content->length = loc - hdr->content->offset;
191 if (!hdr->env->return_path && return_path[0])
192 hdr->env->return_path =
193 rfc822_parse_adrlist (hdr->env->return_path, return_path);
196 hdr->env->from = rfc822_cpy_adr (hdr->env->return_path);
201 dprint (1, (debugfile, "mmdf_parse_mailbox: corrupt mailbox!\n"));
202 mutt_error _("Mailbox is corrupt!");
208 if (ctx->msgcount > oldmsgcount)
209 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
214 /* Note that this function is also called when new mail is appended to the
215 * currently open folder, and NOT just when the mailbox is initially read.
217 * NOTE: it is assumed that the mailbox being read has been locked before
218 * this routine gets called. Strange things could happen if it's not!
220 int mbox_parse_mailbox (CONTEXT * ctx)
223 char buf[HUGE_STRING], return_path[STRING];
226 int count = 0, lines = 0;
229 #ifdef NFS_ATTRIBUTE_HACK
230 struct utimbuf newtime;
233 /* Save information about the folder at the time we opened it. */
234 if (stat (ctx->path, &sb) == -1) {
235 mutt_perror (ctx->path);
239 ctx->size = sb.st_size;
240 ctx->mtime = sb.st_mtime;
242 #ifdef NFS_ATTRIBUTE_HACK
243 if (sb.st_mtime > sb.st_atime) {
244 newtime.modtime = sb.st_mtime;
245 newtime.actime = time (NULL);
246 utime (ctx->path, &newtime);
251 ctx->readonly = access (ctx->path, W_OK) ? 1 : 0;
253 /* precompute the local timezone to speed up calculation of the
255 tz = mutt_local_tz (0);
257 loc = ftell (ctx->fp);
258 while (fgets (buf, sizeof (buf), ctx->fp) != NULL) {
259 if (is_from (buf, return_path, sizeof (return_path), &t)) {
260 /* Save the Content-Length of the previous message */
262 #define PREV ctx->hdrs[ctx->msgcount-1]
264 if (PREV->content->length < 0) {
265 PREV->content->length = loc - PREV->content->offset - 1;
266 if (PREV->content->length < 0)
267 PREV->content->length = 0;
270 PREV->lines = lines ? lines - 1 : 0;
275 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
276 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
277 (int) (ftell (ctx->fp) / (ctx->size / 100 + 1)));
279 if (ctx->msgcount == ctx->hdrmax)
280 mx_alloc_memory (ctx);
282 curhdr = ctx->hdrs[ctx->msgcount] = mutt_new_header ();
283 curhdr->received = t - tz;
284 curhdr->offset = loc;
285 curhdr->index = ctx->msgcount;
287 curhdr->env = mutt_read_rfc822_header (ctx->fp, curhdr, 0, 0);
289 /* if we know how long this message is, either just skip over the body,
290 * or if we don't know how many lines there are, count them now (this will
291 * save time by not having to search for the next message marker).
293 if (curhdr->content->length > 0) {
296 loc = ftell (ctx->fp);
297 tmploc = loc + curhdr->content->length + 1;
299 if (0 < tmploc && tmploc < ctx->size) {
301 * check to see if the content-length looks valid. we expect to
302 * to see a valid message separator at this point in the stream
304 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0 ||
305 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
306 mutt_strncmp ("From ", buf, 5) != 0) {
309 "mbox_parse_mailbox: bad content-length in message %d (cl=%ld)\n",
310 curhdr->index, curhdr->content->length));
311 dprint (1, (debugfile, "\tLINE: %s", buf));
312 if (fseek (ctx->fp, loc, SEEK_SET) != 0) { /* nope, return the previous position */
313 dprint (1, (debugfile, "mbox_parse_mailbox: fseek() failed\n"));
315 curhdr->content->length = -1;
318 else if (tmploc != ctx->size) {
319 /* content-length would put us past the end of the file, so it
322 curhdr->content->length = -1;
325 if (curhdr->content->length != -1) {
326 /* good content-length. check to see if we know how many lines
327 * are in this message.
329 if (curhdr->lines == 0) {
330 int cl = curhdr->content->length;
332 /* count the number of lines in this message */
333 if (fseek (ctx->fp, loc, SEEK_SET) != 0)
334 dprint (1, (debugfile, "mbox_parse_mailbox: fseek() failed\n"));
336 if (fgetc (ctx->fp) == '\n')
341 /* return to the offset of the next message separator */
342 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0)
343 dprint (1, (debugfile, "mbox_parse_mailbox: fseek() failed\n"));
349 if (!curhdr->env->return_path && return_path[0])
350 curhdr->env->return_path =
351 rfc822_parse_adrlist (curhdr->env->return_path, return_path);
353 if (!curhdr->env->from)
354 curhdr->env->from = rfc822_cpy_adr (curhdr->env->return_path);
361 loc = ftell (ctx->fp);
365 * Only set the content-length of the previous message if we have read more
366 * than one message during _this_ invocation. If this routine is called
367 * when new mail is received, we need to make sure not to clobber what
368 * previously was the last message since the headers may be sorted.
371 if (PREV->content->length < 0) {
372 PREV->content->length = ftell (ctx->fp) - PREV->content->offset - 1;
373 if (PREV->content->length < 0)
374 PREV->content->length = 0;
378 PREV->lines = lines ? lines - 1 : 0;
380 mx_update_context (ctx, count);
388 /* open a mbox or mmdf style mailbox */
389 int mbox_open_mailbox (CONTEXT * ctx)
393 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
394 mutt_perror (ctx->path);
397 mutt_block_signals ();
398 if (mbox_lock_mailbox (ctx, 0, 1) == -1) {
399 mutt_unblock_signals ();
403 if (ctx->magic == M_MBOX)
404 rc = mbox_parse_mailbox (ctx);
405 else if (ctx->magic == M_MMDF)
406 rc = mmdf_parse_mailbox (ctx);
410 mbox_unlock_mailbox (ctx);
411 mutt_unblock_signals ();
415 /* return 1 if address lists are strictly identical */
416 static int strict_addrcmp (const ADDRESS * a, const ADDRESS * b)
419 if (mutt_strcmp (a->mailbox, b->mailbox) ||
420 mutt_strcmp (a->personal, b->personal))
432 static int strict_cmp_lists (const LIST * a, const LIST * b)
435 if (mutt_strcmp (a->data, b->data))
447 static int strict_cmp_envelopes (const ENVELOPE * e1, const ENVELOPE * e2)
450 if (mutt_strcmp (e1->message_id, e2->message_id) ||
451 mutt_strcmp (e1->subject, e2->subject) ||
452 !strict_cmp_lists (e1->references, e2->references) ||
453 !strict_addrcmp (e1->from, e2->from) ||
454 !strict_addrcmp (e1->sender, e2->sender) ||
455 !strict_addrcmp (e1->reply_to, e2->reply_to) ||
456 !strict_addrcmp (e1->to, e2->to) ||
457 !strict_addrcmp (e1->cc, e2->cc) ||
458 !strict_addrcmp (e1->return_path, e2->return_path))
464 if (e1 == NULL && e2 == NULL)
471 static int strict_cmp_parameters (const PARAMETER * p1, const PARAMETER * p2)
474 if (mutt_strcmp (p1->attribute, p2->attribute) ||
475 mutt_strcmp (p1->value, p2->value))
487 static int strict_cmp_bodies (const BODY * b1, const BODY * b2)
489 if (b1->type != b2->type ||
490 b1->encoding != b2->encoding ||
491 mutt_strcmp (b1->subtype, b2->subtype) ||
492 mutt_strcmp (b1->description, b2->description) ||
493 !strict_cmp_parameters (b1->parameter, b2->parameter) ||
494 b1->length != b2->length)
499 /* return 1 if headers are strictly identical */
500 int mbox_strict_cmp_headers (const HEADER * h1, const HEADER * h2)
503 if (h1->received != h2->received ||
504 h1->date_sent != h2->date_sent ||
505 h1->content->length != h2->content->length ||
506 h1->lines != h2->lines ||
507 h1->zhours != h2->zhours ||
508 h1->zminutes != h2->zminutes ||
509 h1->zoccident != h2->zoccident ||
510 h1->mime != h2->mime ||
511 !strict_cmp_envelopes (h1->env, h2->env) ||
512 !strict_cmp_bodies (h1->content, h2->content))
518 if (h1 == NULL && h2 == NULL)
525 /* check to see if the mailbox has changed on disk.
528 * M_REOPENED mailbox has been reopened
529 * M_NEW_MAIL new mail has arrived!
530 * M_LOCKED couldn't lock the file
534 int mbox_check_mailbox (CONTEXT * ctx, int *index_hint)
537 char buffer[LONG_STRING];
541 if (stat (ctx->path, &st) == 0) {
542 if (st.st_mtime == ctx->mtime && st.st_size == ctx->size)
545 if (st.st_size == ctx->size) {
546 /* the file was touched, but it is still the same length, so just exit */
547 ctx->mtime = st.st_mtime;
551 if (st.st_size > ctx->size) {
552 /* lock the file if it isn't already */
554 mutt_block_signals ();
555 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
556 mutt_unblock_signals ();
557 /* we couldn't lock the mailbox, but nothing serious happened:
558 * probably the new mail arrived: no reason to wait till we can
559 * parse it: we'll get it on the next pass
567 * Check to make sure that the only change to the mailbox is that
568 * message(s) were appended to this file. My heuristic is that we should
569 * see the message separator at *exactly* what used to be the end of the
572 if (fseek (ctx->fp, ctx->size, SEEK_SET) != 0)
573 dprint (1, (debugfile, "mbox_check_mailbox: fseek() failed\n"));
574 if (fgets (buffer, sizeof (buffer), ctx->fp) != NULL) {
575 if ((ctx->magic == M_MBOX && mutt_strncmp ("From ", buffer, 5) == 0)
576 || (ctx->magic == M_MMDF && mutt_strcmp (MMDF_SEP, buffer) == 0)) {
577 if (fseek (ctx->fp, ctx->size, SEEK_SET) != 0)
578 dprint (1, (debugfile, "mbox_check_mailbox: fseek() failed\n"));
579 if (ctx->magic == M_MBOX)
580 mbox_parse_mailbox (ctx);
582 mmdf_parse_mailbox (ctx);
584 /* Only unlock the folder if it was locked inside of this routine.
585 * It may have been locked elsewhere, like in
586 * mutt_checkpoint_mailbox().
590 mbox_unlock_mailbox (ctx);
591 mutt_unblock_signals ();
594 return (M_NEW_MAIL); /* signal that new mail arrived */
600 dprint (1, (debugfile, "mbox_check_mailbox: fgets returned NULL.\n"));
609 if (mutt_reopen_mailbox (ctx, index_hint) != -1) {
611 mbox_unlock_mailbox (ctx);
612 mutt_unblock_signals ();
620 mbox_unlock_mailbox (ctx);
621 mx_fastclose_mailbox (ctx);
622 mutt_unblock_signals ();
623 mutt_error _("Mailbox was corrupted!");
632 int mbox_sync_mailbox (CONTEXT * ctx, int *index_hint)
634 char tempfile[_POSIX_PATH_MAX];
636 int i, j, save_sort = SORT_ORDER;
638 int need_sort = 0; /* flag to resort mailbox if new mail arrives */
639 int first = -1; /* first message to be written */
640 long offset; /* location in mailbox to write changed messages */
642 struct utimbuf utimebuf;
643 struct m_update_t *newOffset = NULL;
644 struct m_update_t *oldOffset = NULL;
647 /* sort message by their position in the mailbox on disk */
648 if (Sort != SORT_ORDER) {
651 mutt_sort_headers (ctx, 0);
654 /* need to open the file for writing in such a way that it does not truncate
655 * the file, so use read-write mode.
657 if ((ctx->fp = freopen (ctx->path, "r+", ctx->fp)) == NULL) {
658 mx_fastclose_mailbox (ctx);
659 mutt_error _("Fatal error! Could not reopen mailbox!");
664 mutt_block_signals ();
666 if (mbox_lock_mailbox (ctx, 1, 1) == -1) {
667 mutt_unblock_signals ();
668 mutt_error _("Unable to lock mailbox!");
673 /* Check to make sure that the file hasn't changed on disk */
674 if ((i = mbox_check_mailbox (ctx, index_hint)) == M_NEW_MAIL
675 || i == M_REOPENED) {
676 /* new mail arrived, or mailbox reopened */
687 /* Create a temporary file to write the new version of the mailbox in. */
688 mutt_mktemp (tempfile);
689 if ((i = open (tempfile, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1 ||
690 (fp = fdopen (i, "w")) == NULL) {
695 mutt_error _("Could not create temporary file!");
701 /* find the first deleted/changed message. we save a lot of time by only
702 * rewriting the mailbox from the point where it has actually changed.
704 for (i = 0; i < ctx->msgcount && !ctx->hdrs[i]->deleted &&
705 !ctx->hdrs[i]->changed && !ctx->hdrs[i]->attach_del; i++);
706 if (i == ctx->msgcount) {
707 /* this means ctx->changed or ctx->deleted was set, but no
708 * messages were found to be changed or deleted. This should
709 * never happen, is we presume it is a bug in mutt.
712 _("sync: mbox modified, but no modified messages! (report this bug)");
713 mutt_sleep (5); /* the mutt_error /will/ get cleared! */
714 dprint (1, (debugfile, "mbox_sync_mailbox(): no modified messages.\n"));
719 /* save the index of the first changed/deleted message */
721 /* where to start overwriting */
722 offset = ctx->hdrs[i]->offset;
724 /* the offset stored in the header does not include the MMDF_SEP, so make
725 * sure we seek to the correct location
727 if (ctx->magic == M_MMDF)
728 offset -= (sizeof MMDF_SEP - 1);
730 /* allocate space for the new offsets */
731 newOffset = safe_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
732 oldOffset = safe_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
734 for (i = first, j = 0; i < ctx->msgcount; i++) {
736 * back up some information which is needed to restore offsets when
740 oldOffset[i - first].valid = 1;
741 oldOffset[i - first].hdr = ctx->hdrs[i]->offset;
742 oldOffset[i - first].body = ctx->hdrs[i]->content->offset;
743 oldOffset[i - first].lines = ctx->hdrs[i]->lines;
744 oldOffset[i - first].length = ctx->hdrs[i]->content->length;
746 if (!ctx->hdrs[i]->deleted) {
748 if (!ctx->quiet && WriteInc && ((i % WriteInc) == 0 || j == 1))
749 mutt_message (_("Writing messages... %d (%d%%)"), i,
750 (int) (ftell (ctx->fp) / (ctx->size / 100 + 1)));
752 if (ctx->magic == M_MMDF) {
753 if (fputs (MMDF_SEP, fp) == EOF) {
754 mutt_perror (tempfile);
762 /* save the new offset for this message. we add `offset' because the
763 * temporary file only contains saved message which are located after
764 * `offset' in the real mailbox
766 newOffset[i - first].hdr = ftell (fp) + offset;
768 if (mutt_copy_message
769 (fp, ctx, ctx->hdrs[i], M_CM_UPDATE,
770 CH_FROM | CH_UPDATE | CH_UPDATE_LEN) == -1) {
771 mutt_perror (tempfile);
777 /* Since messages could have been deleted, the offsets stored in memory
778 * will be wrong, so update what we can, which is the offset of this
779 * message, and the offset of the body. If this is a multipart message,
780 * we just flush the in memory cache so that the message will be reparsed
781 * if the user accesses it later.
783 newOffset[i - first].body =
784 ftell (fp) - ctx->hdrs[i]->content->length + offset;
785 mutt_free_body (&ctx->hdrs[i]->content->parts);
787 switch (ctx->magic) {
789 if (fputs (MMDF_SEP, fp) == EOF) {
790 mutt_perror (tempfile);
797 if (fputs ("\n", fp) == EOF) {
798 mutt_perror (tempfile);
807 if (fclose (fp) != 0) {
810 (debugfile, "mbox_sync_mailbox: fclose() returned non-zero.\n"));
812 mutt_perror (tempfile);
818 /* Save the state of this folder. */
819 if (stat (ctx->path, &statbuf) == -1) {
820 mutt_perror (ctx->path);
826 if ((fp = fopen (tempfile, "r")) == NULL) {
827 mutt_unblock_signals ();
828 mx_fastclose_mailbox (ctx);
831 "mbox_sync_mailbox: unable to reopen temp copy of mailbox!\n"));
832 mutt_perror (tempfile);
837 if (fseek (ctx->fp, offset, SEEK_SET) != 0 || /* seek the append location */
838 /* do a sanity check to make sure the mailbox looks ok */
839 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
840 (ctx->magic == M_MBOX && mutt_strncmp ("From ", buf, 5) != 0) ||
841 (ctx->magic == M_MMDF && mutt_strcmp (MMDF_SEP, buf) != 0)) {
844 "mbox_sync_mailbox: message not in expected position."));
845 dprint (1, (debugfile, "\tLINE: %s\n", buf));
849 if (fseek (ctx->fp, offset, SEEK_SET) != 0) { /* return to proper offset */
851 dprint (1, (debugfile, "mbox_sync_mailbox: fseek() failed\n"));
854 /* copy the temp mailbox back into place starting at the first
855 * change/deleted message
857 mutt_message _("Committing changes...");
859 i = mutt_copy_stream (fp, ctx->fp);
861 if (ferror (ctx->fp))
865 ctx->size = ftell (ctx->fp); /* update the size of the mailbox */
866 ftruncate (fileno (ctx->fp), ctx->size);
872 mbox_unlock_mailbox (ctx);
874 if (fclose (ctx->fp) != 0 || i == -1) {
875 /* error occured while writing the mailbox back, so keep the temp copy
879 char savefile[_POSIX_PATH_MAX];
881 snprintf (savefile, sizeof (savefile), "%s/mutt.%s-%s-%u",
882 NONULL (Tempdir), NONULL (Username), NONULL (Hostname),
883 (unsigned int) getpid ());
884 rename (tempfile, savefile);
885 mutt_unblock_signals ();
886 mx_fastclose_mailbox (ctx);
887 mutt_pretty_mailbox (savefile);
888 mutt_error (_("Write failed! Saved partial mailbox to %s"), savefile);
893 /* Restore the previous access/modification times */
894 utimebuf.actime = statbuf.st_atime;
895 utimebuf.modtime = statbuf.st_mtime;
896 utime (ctx->path, &utimebuf);
898 /* reopen the mailbox in read-only mode */
899 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
901 mutt_unblock_signals ();
902 mx_fastclose_mailbox (ctx);
903 mutt_error _("Fatal error! Could not reopen mailbox!");
909 /* update the offsets of the rewritten messages */
910 for (i = first, j = first; i < ctx->msgcount; i++) {
911 if (!ctx->hdrs[i]->deleted) {
912 ctx->hdrs[i]->offset = newOffset[i - first].hdr;
913 ctx->hdrs[i]->content->hdr_offset = newOffset[i - first].hdr;
914 ctx->hdrs[i]->content->offset = newOffset[i - first].body;
915 ctx->hdrs[i]->index = j++;
920 unlink (tempfile); /* remove partial copy of the mailbox */
921 mutt_unblock_signals ();
922 Sort = save_sort; /* Restore the default value. */
924 return (0); /* signal success */
926 bail: /* Come here in case of disaster */
930 /* restore offsets, as far as they are valid */
931 if (first >= 0 && oldOffset) {
932 for (i = first; i < ctx->msgcount && oldOffset[i - first].valid; i++) {
933 ctx->hdrs[i]->offset = oldOffset[i - first].hdr;
934 ctx->hdrs[i]->content->hdr_offset = oldOffset[i - first].hdr;
935 ctx->hdrs[i]->content->offset = oldOffset[i - first].body;
936 ctx->hdrs[i]->lines = oldOffset[i - first].lines;
937 ctx->hdrs[i]->content->length = oldOffset[i - first].length;
941 /* this is ok to call even if we haven't locked anything */
942 mbox_unlock_mailbox (ctx);
944 mutt_unblock_signals ();
948 if ((ctx->fp = freopen (ctx->path, "r", ctx->fp)) == NULL) {
949 mutt_error _("Could not reopen mailbox!");
951 mx_fastclose_mailbox (ctx);
955 if (need_sort || save_sort != Sort) {
957 /* if the mailbox was reopened, the thread tree will be invalid so make
958 * sure to start threading from scratch. */
959 mutt_sort_headers (ctx, (need_sort == M_REOPENED));
965 /* close a mailbox opened in write-mode */
966 int mbox_close_mailbox (CONTEXT * ctx)
968 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
970 #ifdef USE_COMPRESSED
971 if (ctx->compressinfo)
972 mutt_slow_close_compressed (ctx);
975 mutt_unblock_signals ();
976 mx_fastclose_mailbox (ctx);
980 int mutt_reopen_mailbox (CONTEXT * ctx, int *index_hint)
982 int (*cmp_headers) (const HEADER *, const HEADER *) = NULL;
990 /* silent operations */
993 mutt_message _("Reopening mailbox...");
995 /* our heuristics require the old mailbox to be unsorted */
996 if (Sort != SORT_ORDER) {
1001 mutt_sort_headers (ctx, 1);
1008 /* simulate a close */
1010 hash_destroy (&ctx->id_hash, NULL);
1012 hash_destroy (&ctx->subj_hash, NULL);
1013 mutt_clear_threads (ctx);
1015 if (ctx->readonly) {
1016 for (i = 0; i < ctx->msgcount; i++)
1017 mutt_free_header (&(ctx->hdrs[i])); /* nothing to do! */
1021 /* save the old headers */
1022 old_msgcount = ctx->msgcount;
1023 old_hdrs = ctx->hdrs;
1027 ctx->hdrmax = 0; /* force allocation of new headers */
1036 ctx->id_hash = NULL;
1037 ctx->subj_hash = NULL;
1039 switch (ctx->magic) {
1042 if (fseek (ctx->fp, 0, SEEK_SET) != 0) {
1043 dprint (1, (debugfile, "mutt_reopen_mailbox: fseek() failed\n"));
1047 cmp_headers = mbox_strict_cmp_headers;
1048 if (ctx->magic == M_MBOX)
1049 rc = mbox_parse_mailbox (ctx);
1051 rc = mmdf_parse_mailbox (ctx);
1061 /* free the old headers */
1062 for (j = 0; j < old_msgcount; j++)
1063 mutt_free_header (&(old_hdrs[j]));
1070 /* now try to recover the old flags */
1072 index_hint_set = (index_hint == NULL);
1074 if (!ctx->readonly) {
1075 for (i = 0; i < ctx->msgcount; i++) {
1078 /* some messages have been deleted, and new messages have been
1079 * appended at the end; the heuristic is that old messages have then
1080 * "advanced" towards the beginning of the folder, so we begin the
1081 * search at index "i"
1083 for (j = i; j < old_msgcount; j++) {
1084 if (old_hdrs[j] == NULL)
1086 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
1092 for (j = 0; j < i && j < old_msgcount; j++) {
1093 if (old_hdrs[j] == NULL)
1095 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
1103 /* this is best done here */
1104 if (!index_hint_set && *index_hint == j)
1107 if (old_hdrs[j]->changed) {
1108 /* Only update the flags if the old header was changed;
1109 * otherwise, the header may have been modified externally,
1110 * and we don't want to lose _those_ changes
1112 mutt_set_flag (ctx, ctx->hdrs[i], M_FLAG, old_hdrs[j]->flagged);
1113 mutt_set_flag (ctx, ctx->hdrs[i], M_REPLIED, old_hdrs[j]->replied);
1114 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, old_hdrs[j]->old);
1115 mutt_set_flag (ctx, ctx->hdrs[i], M_READ, old_hdrs[j]->read);
1117 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, old_hdrs[j]->deleted);
1118 mutt_set_flag (ctx, ctx->hdrs[i], M_TAG, old_hdrs[j]->tagged);
1120 /* we don't need this header any more */
1121 mutt_free_header (&(old_hdrs[j]));
1125 /* free the remaining old headers */
1126 for (j = 0; j < old_msgcount; j++) {
1128 mutt_free_header (&(old_hdrs[j]));
1137 return ((ctx->changed || msg_mod) ? M_REOPENED : M_NEW_MAIL);
1142 * 1 if the mailbox is not empty
1143 * 0 if the mailbox is empty
1146 int mbox_check_empty (const char *path)
1150 if (stat (path, &st) == -1)
1153 return ((st.st_size == 0));