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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 /* This file contains code to parse ``mbox'' and ``mmdf'' style mailboxes */
30 #include "mutt_curses.h"
41 /* struct used by mutt_sync_mailbox() to store new offsets */
52 * ctx - context to lock
53 * excl - exclusive lock?
54 * retry - should retry if unable to lock?
56 int mbox_lock_mailbox (CONTEXT *ctx, int excl, int retry)
60 if ((r = mx_lock_file (ctx->path, fileno (ctx->fp), excl, 1, retry)) == 0)
62 else if (retry && !excl)
71 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;
92 #ifdef NFS_ATTRIBUTE_HACK
93 struct utimbuf newtime;
98 if (stat (ctx->path, &sb) == -1)
100 mutt_perror (ctx->path);
103 ctx->mtime = sb.st_mtime;
104 ctx->size = sb.st_size;
106 #ifdef NFS_ATTRIBUTE_HACK
107 if (sb.st_mtime > sb.st_atime)
109 newtime.modtime = sb.st_mtime;
110 newtime.actime = time (NULL);
111 utime (ctx->path, &newtime);
115 buf[sizeof (buf) - 1] = 0;
119 snprintf (msgbuf, sizeof (msgbuf), _("Reading %s..."), ctx->path);
120 mutt_progress_init (&progress, msgbuf, M_PROGRESS_MSG, ReadInc, 0);
125 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
128 if (mutt_strcmp (buf, MMDF_SEP) == 0)
130 loc = ftello (ctx->fp);
134 mutt_progress_update (&progress, count,
135 (int) (loc / (ctx->size / 100 + 1)));
137 if (ctx->msgcount == ctx->hdrmax)
138 mx_alloc_memory (ctx);
139 ctx->hdrs[ctx->msgcount] = hdr = mutt_new_header ();
141 hdr->index = ctx->msgcount;
143 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
145 /* TODO: memory leak??? */
146 dprint (1, (debugfile, "mmdf_parse_mailbox: unexpected EOF\n"));
152 if (!is_from (buf, return_path, sizeof (return_path), &t))
154 if (fseeko (ctx->fp, loc, SEEK_SET) != 0)
156 dprint (1, (debugfile, "mmdf_parse_mailbox: fseek() failed\n"));
157 mutt_error _("Mailbox is corrupt!");
162 hdr->received = t - mutt_local_tz (t);
164 hdr->env = mutt_read_rfc822_header (ctx->fp, hdr, 0, 0);
166 loc = ftello (ctx->fp);
168 if (hdr->content->length > 0 && hdr->lines > 0)
170 tmploc = loc + hdr->content->length;
172 if (0 < tmploc && tmploc < ctx->size)
174 if (fseeko (ctx->fp, tmploc, SEEK_SET) != 0 ||
175 fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL ||
176 mutt_strcmp (MMDF_SEP, buf) != 0)
178 if (fseeko (ctx->fp, loc, SEEK_SET) != 0)
179 dprint (1, (debugfile, "mmdf_parse_mailbox: fseek() failed\n"));
180 hdr->content->length = -1;
184 hdr->content->length = -1;
187 hdr->content->length = -1;
189 if (hdr->content->length < 0)
193 loc = ftello (ctx->fp);
194 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
197 } while (mutt_strcmp (buf, MMDF_SEP) != 0);
200 hdr->content->length = loc - hdr->content->offset;
203 if (!hdr->env->return_path && return_path[0])
204 hdr->env->return_path = rfc822_parse_adrlist (hdr->env->return_path, return_path);
207 hdr->env->from = rfc822_cpy_adr (hdr->env->return_path, 0);
213 dprint (1, (debugfile, "mmdf_parse_mailbox: corrupt mailbox!\n"));
214 mutt_error _("Mailbox is corrupt!");
219 if (ctx->msgcount > oldmsgcount)
220 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
225 /* Note that this function is also called when new mail is appended to the
226 * currently open folder, and NOT just when the mailbox is initially read.
228 * NOTE: it is assumed that the mailbox being read has been locked before
229 * this routine gets called. Strange things could happen if it's not!
231 int mbox_parse_mailbox (CONTEXT *ctx)
234 char buf[HUGE_STRING], return_path[STRING];
237 int count = 0, lines = 0;
239 #ifdef NFS_ATTRIBUTE_HACK
240 struct utimbuf newtime;
245 /* Save information about the folder at the time we opened it. */
246 if (stat (ctx->path, &sb) == -1)
248 mutt_perror (ctx->path);
252 ctx->size = sb.st_size;
253 ctx->mtime = sb.st_mtime;
255 #ifdef NFS_ATTRIBUTE_HACK
256 if (sb.st_mtime > sb.st_atime)
258 newtime.modtime = sb.st_mtime;
259 newtime.actime = time (NULL);
260 utime (ctx->path, &newtime);
265 ctx->readonly = access (ctx->path, W_OK) ? 1 : 0;
269 snprintf (msgbuf, sizeof (msgbuf), _("Reading %s..."), ctx->path);
270 mutt_progress_init (&progress, msgbuf, M_PROGRESS_MSG, ReadInc, 0);
273 loc = ftello (ctx->fp);
274 while (fgets (buf, sizeof (buf), ctx->fp) != NULL)
276 if (is_from (buf, return_path, sizeof (return_path), &t))
278 /* Save the Content-Length of the previous message */
281 #define PREV ctx->hdrs[ctx->msgcount-1]
283 if (PREV->content->length < 0)
285 PREV->content->length = loc - PREV->content->offset - 1;
286 if (PREV->content->length < 0)
287 PREV->content->length = 0;
290 PREV->lines = lines ? lines - 1 : 0;
296 mutt_progress_update (&progress, count,
297 (int)(ftello (ctx->fp) / (ctx->size / 100 + 1)));
299 if (ctx->msgcount == ctx->hdrmax)
300 mx_alloc_memory (ctx);
302 curhdr = ctx->hdrs[ctx->msgcount] = mutt_new_header ();
303 curhdr->received = t - mutt_local_tz (t);
304 curhdr->offset = loc;
305 curhdr->index = ctx->msgcount;
307 curhdr->env = mutt_read_rfc822_header (ctx->fp, curhdr, 0, 0);
309 /* if we know how long this message is, either just skip over the body,
310 * or if we don't know how many lines there are, count them now (this will
311 * save time by not having to search for the next message marker).
313 if (curhdr->content->length > 0)
317 loc = ftello (ctx->fp);
318 tmploc = loc + curhdr->content->length + 1;
320 if (0 < tmploc && tmploc < ctx->size)
323 * check to see if the content-length looks valid. we expect to
324 * to see a valid message separator at this point in the stream
326 if (fseeko (ctx->fp, tmploc, SEEK_SET) != 0 ||
327 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
328 mutt_strncmp ("From ", buf, 5) != 0)
330 dprint (1, (debugfile, "mbox_parse_mailbox: bad content-length in message %d (cl=" OFF_T_FMT ")\n", curhdr->index, curhdr->content->length));
331 dprint (1, (debugfile, "\tLINE: %s", buf));
332 if (fseeko (ctx->fp, loc, SEEK_SET) != 0) /* nope, return the previous position */
334 dprint (1, (debugfile, "mbox_parse_mailbox: fseek() failed\n"));
336 curhdr->content->length = -1;
339 else if (tmploc != ctx->size)
341 /* content-length would put us past the end of the file, so it
344 curhdr->content->length = -1;
347 if (curhdr->content->length != -1)
349 /* good content-length. check to see if we know how many lines
350 * are in this message.
352 if (curhdr->lines == 0)
354 int cl = curhdr->content->length;
356 /* count the number of lines in this message */
357 if (fseeko (ctx->fp, loc, SEEK_SET) != 0)
358 dprint (1, (debugfile, "mbox_parse_mailbox: fseek() failed\n"));
361 if (fgetc (ctx->fp) == '\n')
366 /* return to the offset of the next message separator */
367 if (fseeko (ctx->fp, tmploc, SEEK_SET) != 0)
368 dprint (1, (debugfile, "mbox_parse_mailbox: fseek() failed\n"));
374 if (!curhdr->env->return_path && return_path[0])
375 curhdr->env->return_path = rfc822_parse_adrlist (curhdr->env->return_path, return_path);
377 if (!curhdr->env->from)
378 curhdr->env->from = rfc822_cpy_adr (curhdr->env->return_path, 0);
385 loc = ftello (ctx->fp);
389 * Only set the content-length of the previous message if we have read more
390 * than one message during _this_ invocation. If this routine is called
391 * when new mail is received, we need to make sure not to clobber what
392 * previously was the last message since the headers may be sorted.
396 if (PREV->content->length < 0)
398 PREV->content->length = ftello (ctx->fp) - PREV->content->offset - 1;
399 if (PREV->content->length < 0)
400 PREV->content->length = 0;
404 PREV->lines = lines ? lines - 1 : 0;
406 mx_update_context (ctx, count);
414 /* open a mbox or mmdf style mailbox */
415 int mbox_open_mailbox (CONTEXT *ctx)
419 if ((ctx->fp = fopen (ctx->path, "r")) == NULL)
421 mutt_perror (ctx->path);
424 mutt_block_signals ();
425 if (mbox_lock_mailbox (ctx, 0, 1) == -1)
427 mutt_unblock_signals ();
431 if (ctx->magic == M_MBOX)
432 rc = mbox_parse_mailbox (ctx);
433 else if (ctx->magic == M_MMDF)
434 rc = mmdf_parse_mailbox (ctx);
438 mbox_unlock_mailbox (ctx);
439 mutt_unblock_signals ();
443 /* return 1 if address lists are strictly identical */
444 static int strict_addrcmp (const ADDRESS *a, const ADDRESS *b)
448 if (mutt_strcmp (a->mailbox, b->mailbox) ||
449 mutt_strcmp (a->personal, b->personal))
461 static int strict_cmp_lists (const LIST *a, const LIST *b)
465 if (mutt_strcmp (a->data, b->data))
477 static int strict_cmp_envelopes (const ENVELOPE *e1, const ENVELOPE *e2)
481 if (mutt_strcmp (e1->message_id, e2->message_id) ||
482 mutt_strcmp (e1->subject, e2->subject) ||
483 !strict_cmp_lists (e1->references, e2->references) ||
484 !strict_addrcmp (e1->from, e2->from) ||
485 !strict_addrcmp (e1->sender, e2->sender) ||
486 !strict_addrcmp (e1->reply_to, e2->reply_to) ||
487 !strict_addrcmp (e1->to, e2->to) ||
488 !strict_addrcmp (e1->cc, e2->cc) ||
489 !strict_addrcmp (e1->return_path, e2->return_path))
496 if (e1 == NULL && e2 == NULL)
503 static int strict_cmp_parameters (const PARAMETER *p1, const PARAMETER *p2)
507 if (mutt_strcmp (p1->attribute, p2->attribute) ||
508 mutt_strcmp (p1->value, p2->value))
520 static int strict_cmp_bodies (const BODY *b1, const BODY *b2)
522 if (b1->type != b2->type ||
523 b1->encoding != b2->encoding ||
524 mutt_strcmp (b1->subtype, b2->subtype) ||
525 mutt_strcmp (b1->description, b2->description) ||
526 !strict_cmp_parameters (b1->parameter, b2->parameter) ||
527 b1->length != b2->length)
532 /* return 1 if headers are strictly identical */
533 int mbox_strict_cmp_headers (const HEADER *h1, const HEADER *h2)
537 if (h1->received != h2->received ||
538 h1->date_sent != h2->date_sent ||
539 h1->content->length != h2->content->length ||
540 h1->lines != h2->lines ||
541 h1->zhours != h2->zhours ||
542 h1->zminutes != h2->zminutes ||
543 h1->zoccident != h2->zoccident ||
544 h1->mime != h2->mime ||
545 !strict_cmp_envelopes (h1->env, h2->env) ||
546 !strict_cmp_bodies (h1->content, h2->content))
553 if (h1 == NULL && h2 == NULL)
560 /* check to see if the mailbox has changed on disk.
563 * M_REOPENED mailbox has been reopened
564 * M_NEW_MAIL new mail has arrived!
565 * M_LOCKED couldn't lock the file
569 int mbox_check_mailbox (CONTEXT *ctx, int *index_hint)
572 char buffer[LONG_STRING];
576 if (stat (ctx->path, &st) == 0)
578 if (st.st_mtime == ctx->mtime && st.st_size == ctx->size)
581 if (st.st_size == ctx->size)
583 /* the file was touched, but it is still the same length, so just exit */
584 ctx->mtime = st.st_mtime;
588 if (st.st_size > ctx->size)
590 /* lock the file if it isn't already */
593 mutt_block_signals ();
594 if (mbox_lock_mailbox (ctx, 0, 0) == -1)
596 mutt_unblock_signals ();
597 /* we couldn't lock the mailbox, but nothing serious happened:
598 * probably the new mail arrived: no reason to wait till we can
599 * parse it: we'll get it on the next pass
607 * Check to make sure that the only change to the mailbox is that
608 * message(s) were appended to this file. My heuristic is that we should
609 * see the message separator at *exactly* what used to be the end of the
612 if (fseeko (ctx->fp, ctx->size, SEEK_SET) != 0)
613 dprint (1, (debugfile, "mbox_check_mailbox: fseek() failed\n"));
614 if (fgets (buffer, sizeof (buffer), ctx->fp) != NULL)
616 if ((ctx->magic == M_MBOX && mutt_strncmp ("From ", buffer, 5) == 0) ||
617 (ctx->magic == M_MMDF && mutt_strcmp (MMDF_SEP, buffer) == 0))
619 if (fseeko (ctx->fp, ctx->size, SEEK_SET) != 0)
620 dprint (1, (debugfile, "mbox_check_mailbox: fseek() failed\n"));
621 if (ctx->magic == M_MBOX)
622 mbox_parse_mailbox (ctx);
624 mmdf_parse_mailbox (ctx);
626 /* Only unlock the folder if it was locked inside of this routine.
627 * It may have been locked elsewhere, like in
628 * mutt_checkpoint_mailbox().
633 mbox_unlock_mailbox (ctx);
634 mutt_unblock_signals ();
637 return (M_NEW_MAIL); /* signal that new mail arrived */
644 dprint (1, (debugfile, "mbox_check_mailbox: fgets returned NULL.\n"));
654 if (mutt_reopen_mailbox (ctx, index_hint) != -1)
658 mbox_unlock_mailbox (ctx);
659 mutt_unblock_signals ();
667 mbox_unlock_mailbox (ctx);
668 mx_fastclose_mailbox (ctx);
669 mutt_unblock_signals ();
670 mutt_error _("Mailbox was corrupted!");
675 * Returns 1 if the mailbox has at least 1 new messages (not old)
676 * otherwise returns 0.
678 static int mbox_has_new(CONTEXT *ctx)
682 for (i = 0; i < ctx->msgcount; i++)
683 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->read && !ctx->hdrs[i]->old)
688 /* if mailbox has at least 1 new message, sets mtime > atime of mailbox
689 * so buffy check reports new mail */
690 void mbox_reset_atime (CONTEXT *ctx, struct stat *st)
692 struct utimbuf utimebuf;
698 if (stat (ctx->path, &_st) < 0)
703 utimebuf.actime = st->st_atime;
704 utimebuf.modtime = st->st_mtime;
707 * When $mbox_check_recent is set, existing new mail is ignored, so do not
708 * recent the atime to mtime-1 to signal new mail.
710 if (!option(OPTMAILCHECKRECENT) && utimebuf.actime >= utimebuf.modtime && mbox_has_new(ctx))
711 utimebuf.actime = utimebuf.modtime - 1;
713 utime (ctx->path, &utimebuf);
720 int mbox_sync_mailbox (CONTEXT *ctx, int *index_hint)
722 char tempfile[_POSIX_PATH_MAX];
724 int i, j, save_sort = SORT_ORDER;
726 int need_sort = 0; /* flag to resort mailbox if new mail arrives */
727 int first = -1; /* first message to be written */
728 LOFF_T offset; /* location in mailbox to write changed messages */
730 struct m_update_t *newOffset = NULL;
731 struct m_update_t *oldOffset = NULL;
736 /* sort message by their position in the mailbox on disk */
737 if (Sort != SORT_ORDER)
741 mutt_sort_headers (ctx, 0);
746 /* need to open the file for writing in such a way that it does not truncate
747 * the file, so use read-write mode.
749 if ((ctx->fp = freopen (ctx->path, "r+", ctx->fp)) == NULL)
751 mx_fastclose_mailbox (ctx);
752 mutt_error _("Fatal error! Could not reopen mailbox!");
756 mutt_block_signals ();
758 if (mbox_lock_mailbox (ctx, 1, 1) == -1)
760 mutt_unblock_signals ();
761 mutt_error _("Unable to lock mailbox!");
765 /* Check to make sure that the file hasn't changed on disk */
766 if ((i = mbox_check_mailbox (ctx, index_hint)) == M_NEW_MAIL || i == M_REOPENED)
768 /* new mail arrived, or mailbox reopened */
777 /* Create a temporary file to write the new version of the mailbox in. */
778 mutt_mktemp (tempfile, sizeof (tempfile));
779 if ((i = open (tempfile, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1 ||
780 (fp = fdopen (i, "w")) == NULL)
787 mutt_error _("Could not create temporary file!");
792 /* find the first deleted/changed message. we save a lot of time by only
793 * rewriting the mailbox from the point where it has actually changed.
795 for (i = 0 ; i < ctx->msgcount && !ctx->hdrs[i]->deleted &&
796 !ctx->hdrs[i]->changed && !ctx->hdrs[i]->attach_del; i++)
798 if (i == ctx->msgcount)
800 /* this means ctx->changed or ctx->deleted was set, but no
801 * messages were found to be changed or deleted. This should
802 * never happen, is we presume it is a bug in mutt.
804 mutt_error _("sync: mbox modified, but no modified messages! (report this bug)");
805 mutt_sleep(5); /* the mutt_error /will/ get cleared! */
806 dprint(1, (debugfile, "mbox_sync_mailbox(): no modified messages.\n"));
811 /* save the index of the first changed/deleted message */
813 /* where to start overwriting */
814 offset = ctx->hdrs[i]->offset;
816 /* the offset stored in the header does not include the MMDF_SEP, so make
817 * sure we seek to the correct location
819 if (ctx->magic == M_MMDF)
820 offset -= (sizeof MMDF_SEP - 1);
822 /* allocate space for the new offsets */
823 newOffset = safe_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
824 oldOffset = safe_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
828 snprintf (msgbuf, sizeof (msgbuf), _("Writing %s..."), ctx->path);
829 mutt_progress_init (&progress, msgbuf, M_PROGRESS_MSG, WriteInc, ctx->msgcount);
832 for (i = first, j = 0; i < ctx->msgcount; i++)
835 mutt_progress_update (&progress, i, (int)(ftello (ctx->fp) / (ctx->size / 100 + 1)));
837 * back up some information which is needed to restore offsets when
841 oldOffset[i-first].valid = 1;
842 oldOffset[i-first].hdr = ctx->hdrs[i]->offset;
843 oldOffset[i-first].body = ctx->hdrs[i]->content->offset;
844 oldOffset[i-first].lines = ctx->hdrs[i]->lines;
845 oldOffset[i-first].length = ctx->hdrs[i]->content->length;
847 if (! ctx->hdrs[i]->deleted)
851 if (ctx->magic == M_MMDF)
853 if (fputs (MMDF_SEP, fp) == EOF)
855 mutt_perror (tempfile);
863 /* save the new offset for this message. we add `offset' because the
864 * temporary file only contains saved message which are located after
865 * `offset' in the real mailbox
867 newOffset[i - first].hdr = ftello (fp) + offset;
869 if (mutt_copy_message (fp, ctx, ctx->hdrs[i], M_CM_UPDATE,
870 CH_FROM | CH_UPDATE | CH_UPDATE_LEN) != 0)
872 mutt_perror (tempfile);
878 /* Since messages could have been deleted, the offsets stored in memory
879 * will be wrong, so update what we can, which is the offset of this
880 * message, and the offset of the body. If this is a multipart message,
881 * we just flush the in memory cache so that the message will be reparsed
882 * if the user accesses it later.
884 newOffset[i - first].body = ftello (fp) - ctx->hdrs[i]->content->length + offset;
885 mutt_free_body (&ctx->hdrs[i]->content->parts);
890 if(fputs(MMDF_SEP, fp) == EOF)
892 mutt_perror (tempfile);
899 if(fputs("\n", fp) == EOF)
901 mutt_perror (tempfile);
910 if (fclose (fp) != 0)
913 dprint(1, (debugfile, "mbox_sync_mailbox: safe_fclose (&) returned non-zero.\n"));
915 mutt_perror (tempfile);
921 /* Save the state of this folder. */
922 if (stat (ctx->path, &statbuf) == -1)
924 mutt_perror (ctx->path);
930 if ((fp = fopen (tempfile, "r")) == NULL)
932 mutt_unblock_signals ();
933 mx_fastclose_mailbox (ctx);
934 dprint (1, (debugfile, "mbox_sync_mailbox: unable to reopen temp copy of mailbox!\n"));
935 mutt_perror (tempfile);
940 if (fseeko (ctx->fp, offset, SEEK_SET) != 0 || /* seek the append location */
941 /* do a sanity check to make sure the mailbox looks ok */
942 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
943 (ctx->magic == M_MBOX && mutt_strncmp ("From ", buf, 5) != 0) ||
944 (ctx->magic == M_MMDF && mutt_strcmp (MMDF_SEP, buf) != 0))
946 dprint (1, (debugfile, "mbox_sync_mailbox: message not in expected position."));
947 dprint (1, (debugfile, "\tLINE: %s\n", buf));
952 if (fseeko (ctx->fp, offset, SEEK_SET) != 0) /* return to proper offset */
955 dprint (1, (debugfile, "mbox_sync_mailbox: fseek() failed\n"));
959 /* copy the temp mailbox back into place starting at the first
960 * change/deleted message
963 mutt_message _("Committing changes...");
964 i = mutt_copy_stream (fp, ctx->fp);
966 if (ferror (ctx->fp))
971 ctx->size = ftello (ctx->fp); /* update the size of the mailbox */
972 ftruncate (fileno (ctx->fp), ctx->size);
978 mbox_unlock_mailbox (ctx);
980 if (fclose (ctx->fp) != 0 || i == -1)
982 /* error occured while writing the mailbox back, so keep the temp copy
986 char savefile[_POSIX_PATH_MAX];
988 snprintf (savefile, sizeof (savefile), "%s/mutt.%s-%s-%u",
989 NONULL (Tempdir), NONULL(Username), NONULL(Hostname), (unsigned int)getpid ());
990 rename (tempfile, savefile);
991 mutt_unblock_signals ();
992 mx_fastclose_mailbox (ctx);
993 mutt_pretty_mailbox (savefile, sizeof (savefile));
994 mutt_error (_("Write failed! Saved partial mailbox to %s"), savefile);
999 /* Restore the previous access/modification times */
1000 mbox_reset_atime (ctx, &statbuf);
1002 /* reopen the mailbox in read-only mode */
1003 if ((ctx->fp = fopen (ctx->path, "r")) == NULL)
1006 mutt_unblock_signals ();
1007 mx_fastclose_mailbox (ctx);
1008 mutt_error _("Fatal error! Could not reopen mailbox!");
1012 /* update the offsets of the rewritten messages */
1013 for (i = first, j = first; i < ctx->msgcount; i++)
1015 if (!ctx->hdrs[i]->deleted)
1017 ctx->hdrs[i]->offset = newOffset[i - first].hdr;
1018 ctx->hdrs[i]->content->hdr_offset = newOffset[i - first].hdr;
1019 ctx->hdrs[i]->content->offset = newOffset[i - first].body;
1020 ctx->hdrs[i]->index = j++;
1025 unlink (tempfile); /* remove partial copy of the mailbox */
1026 mutt_unblock_signals ();
1028 return (0); /* signal success */
1030 bail: /* Come here in case of disaster */
1034 /* restore offsets, as far as they are valid */
1035 if (first >= 0 && oldOffset)
1037 for (i = first; i < ctx->msgcount && oldOffset[i-first].valid; i++)
1039 ctx->hdrs[i]->offset = oldOffset[i-first].hdr;
1040 ctx->hdrs[i]->content->hdr_offset = oldOffset[i-first].hdr;
1041 ctx->hdrs[i]->content->offset = oldOffset[i-first].body;
1042 ctx->hdrs[i]->lines = oldOffset[i-first].lines;
1043 ctx->hdrs[i]->content->length = oldOffset[i-first].length;
1047 /* this is ok to call even if we haven't locked anything */
1048 mbox_unlock_mailbox (ctx);
1050 mutt_unblock_signals ();
1054 if ((ctx->fp = freopen (ctx->path, "r", ctx->fp)) == NULL)
1056 mutt_error _("Could not reopen mailbox!");
1057 mx_fastclose_mailbox (ctx);
1062 /* if the mailbox was reopened, the thread tree will be invalid so make
1063 * sure to start threading from scratch. */
1064 mutt_sort_headers (ctx, (need_sort == M_REOPENED));
1069 /* close a mailbox opened in write-mode */
1070 int mbox_close_mailbox (CONTEXT *ctx)
1072 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
1073 mutt_unblock_signals ();
1074 mx_fastclose_mailbox (ctx);
1078 int mutt_reopen_mailbox (CONTEXT *ctx, int *index_hint)
1080 int (*cmp_headers) (const HEADER *, const HEADER *) = NULL;
1088 /* silent operations */
1092 mutt_message _("Reopening mailbox...");
1094 /* our heuristics require the old mailbox to be unsorted */
1095 if (Sort != SORT_ORDER)
1101 mutt_sort_headers (ctx, 1);
1108 /* simulate a close */
1110 hash_destroy (&ctx->id_hash, NULL);
1112 hash_destroy (&ctx->subj_hash, NULL);
1113 mutt_clear_threads (ctx);
1117 for (i = 0; i < ctx->msgcount; i++)
1118 mutt_free_header (&(ctx->hdrs[i])); /* nothing to do! */
1123 /* save the old headers */
1124 old_msgcount = ctx->msgcount;
1125 old_hdrs = ctx->hdrs;
1129 ctx->hdrmax = 0; /* force allocation of new headers */
1138 ctx->id_hash = NULL;
1139 ctx->subj_hash = NULL;
1145 cmp_headers = mbox_strict_cmp_headers;
1146 safe_fclose (&ctx->fp);
1147 if (!(ctx->fp = safe_fopen (ctx->path, "r")))
1150 rc = ((ctx->magic == M_MBOX) ? mbox_parse_mailbox
1151 : mmdf_parse_mailbox) (ctx);
1161 /* free the old headers */
1162 for (j = 0; j < old_msgcount; j++)
1163 mutt_free_header (&(old_hdrs[j]));
1170 /* now try to recover the old flags */
1172 index_hint_set = (index_hint == NULL);
1176 for (i = 0; i < ctx->msgcount; i++)
1180 /* some messages have been deleted, and new messages have been
1181 * appended at the end; the heuristic is that old messages have then
1182 * "advanced" towards the beginning of the folder, so we begin the
1183 * search at index "i"
1185 for (j = i; j < old_msgcount; j++)
1187 if (old_hdrs[j] == NULL)
1189 if (cmp_headers (ctx->hdrs[i], old_hdrs[j]))
1197 for (j = 0; j < i && j < old_msgcount; j++)
1199 if (old_hdrs[j] == NULL)
1201 if (cmp_headers (ctx->hdrs[i], old_hdrs[j]))
1211 /* this is best done here */
1212 if (!index_hint_set && *index_hint == j)
1215 if (old_hdrs[j]->changed)
1217 /* Only update the flags if the old header was changed;
1218 * otherwise, the header may have been modified externally,
1219 * and we don't want to lose _those_ changes
1221 mutt_set_flag (ctx, ctx->hdrs[i], M_FLAG, old_hdrs[j]->flagged);
1222 mutt_set_flag (ctx, ctx->hdrs[i], M_REPLIED, old_hdrs[j]->replied);
1223 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, old_hdrs[j]->old);
1224 mutt_set_flag (ctx, ctx->hdrs[i], M_READ, old_hdrs[j]->read);
1226 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, old_hdrs[j]->deleted);
1227 mutt_set_flag (ctx, ctx->hdrs[i], M_TAG, old_hdrs[j]->tagged);
1229 /* we don't need this header any more */
1230 mutt_free_header (&(old_hdrs[j]));
1234 /* free the remaining old headers */
1235 for (j = 0; j < old_msgcount; j++)
1239 mutt_free_header (&(old_hdrs[j]));
1248 return ((ctx->changed || msg_mod) ? M_REOPENED : M_NEW_MAIL);
1253 * 1 if the mailbox is not empty
1254 * 0 if the mailbox is empty
1257 int mbox_check_empty (const char *path)
1261 if (stat (path, &st) == -1)
1264 return ((st.st_size == 0));