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 /* precompute the local timezone to speed up calculation of the
117 tz = mutt_local_tz (0);
119 buf[sizeof (buf) - 1] = 0;
123 snprintf (msgbuf, sizeof (msgbuf), _("Reading %s..."), ctx->path);
124 mutt_progress_init (&progress, msgbuf, M_PROGRESS_MSG, ReadInc, 0);
129 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
132 if (mutt_strcmp (buf, MMDF_SEP) == 0)
134 loc = ftello (ctx->fp);
138 mutt_progress_update (&progress, count,
139 (int) (loc / (ctx->size / 100 + 1)));
141 if (ctx->msgcount == ctx->hdrmax)
142 mx_alloc_memory (ctx);
143 ctx->hdrs[ctx->msgcount] = hdr = mutt_new_header ();
145 hdr->index = ctx->msgcount;
147 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
149 /* TODO: memory leak??? */
150 dprint (1, (debugfile, "mmdf_parse_mailbox: unexpected EOF\n"));
156 if (!is_from (buf, return_path, sizeof (return_path), &t))
158 if (fseeko (ctx->fp, loc, SEEK_SET) != 0)
160 dprint (1, (debugfile, "mmdf_parse_mailbox: fseek() failed\n"));
161 mutt_error _("Mailbox is corrupt!");
166 hdr->received = t - tz;
168 hdr->env = mutt_read_rfc822_header (ctx->fp, hdr, 0, 0);
170 loc = ftello (ctx->fp);
172 if (hdr->content->length > 0 && hdr->lines > 0)
174 tmploc = loc + hdr->content->length;
176 if (0 < tmploc && tmploc < ctx->size)
178 if (fseeko (ctx->fp, tmploc, SEEK_SET) != 0 ||
179 fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL ||
180 mutt_strcmp (MMDF_SEP, buf) != 0)
182 if (fseeko (ctx->fp, loc, SEEK_SET) != 0)
183 dprint (1, (debugfile, "mmdf_parse_mailbox: fseek() failed\n"));
184 hdr->content->length = -1;
188 hdr->content->length = -1;
191 hdr->content->length = -1;
193 if (hdr->content->length < 0)
197 loc = ftello (ctx->fp);
198 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
201 } while (mutt_strcmp (buf, MMDF_SEP) != 0);
204 hdr->content->length = loc - hdr->content->offset;
207 if (!hdr->env->return_path && return_path[0])
208 hdr->env->return_path = rfc822_parse_adrlist (hdr->env->return_path, return_path);
211 hdr->env->from = rfc822_cpy_adr (hdr->env->return_path, 0);
217 dprint (1, (debugfile, "mmdf_parse_mailbox: corrupt mailbox!\n"));
218 mutt_error _("Mailbox is corrupt!");
223 if (ctx->msgcount > oldmsgcount)
224 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
229 /* Note that this function is also called when new mail is appended to the
230 * currently open folder, and NOT just when the mailbox is initially read.
232 * NOTE: it is assumed that the mailbox being read has been locked before
233 * this routine gets called. Strange things could happen if it's not!
235 int mbox_parse_mailbox (CONTEXT *ctx)
238 char buf[HUGE_STRING], return_path[STRING];
241 int count = 0, lines = 0;
243 #ifdef NFS_ATTRIBUTE_HACK
244 struct utimbuf newtime;
249 /* Save information about the folder at the time we opened it. */
250 if (stat (ctx->path, &sb) == -1)
252 mutt_perror (ctx->path);
256 ctx->size = sb.st_size;
257 ctx->mtime = sb.st_mtime;
259 #ifdef NFS_ATTRIBUTE_HACK
260 if (sb.st_mtime > sb.st_atime)
262 newtime.modtime = sb.st_mtime;
263 newtime.actime = time (NULL);
264 utime (ctx->path, &newtime);
269 ctx->readonly = access (ctx->path, W_OK) ? 1 : 0;
271 /* precompute the local timezone to speed up calculation of the
273 tz = mutt_local_tz (0);
277 snprintf (msgbuf, sizeof (msgbuf), _("Reading %s..."), ctx->path);
278 mutt_progress_init (&progress, msgbuf, M_PROGRESS_MSG, ReadInc, 0);
281 loc = ftello (ctx->fp);
282 while (fgets (buf, sizeof (buf), ctx->fp) != NULL)
284 if (is_from (buf, return_path, sizeof (return_path), &t))
286 /* Save the Content-Length of the previous message */
289 #define PREV ctx->hdrs[ctx->msgcount-1]
291 if (PREV->content->length < 0)
293 PREV->content->length = loc - PREV->content->offset - 1;
294 if (PREV->content->length < 0)
295 PREV->content->length = 0;
298 PREV->lines = lines ? lines - 1 : 0;
304 mutt_progress_update (&progress, count,
305 (int)(ftello (ctx->fp) / (ctx->size / 100 + 1)));
307 if (ctx->msgcount == ctx->hdrmax)
308 mx_alloc_memory (ctx);
310 curhdr = ctx->hdrs[ctx->msgcount] = mutt_new_header ();
311 curhdr->received = t - tz;
312 curhdr->offset = loc;
313 curhdr->index = ctx->msgcount;
315 curhdr->env = mutt_read_rfc822_header (ctx->fp, curhdr, 0, 0);
317 /* if we know how long this message is, either just skip over the body,
318 * or if we don't know how many lines there are, count them now (this will
319 * save time by not having to search for the next message marker).
321 if (curhdr->content->length > 0)
325 loc = ftello (ctx->fp);
326 tmploc = loc + curhdr->content->length + 1;
328 if (0 < tmploc && tmploc < ctx->size)
331 * check to see if the content-length looks valid. we expect to
332 * to see a valid message separator at this point in the stream
334 if (fseeko (ctx->fp, tmploc, SEEK_SET) != 0 ||
335 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
336 mutt_strncmp ("From ", buf, 5) != 0)
338 dprint (1, (debugfile, "mbox_parse_mailbox: bad content-length in message %d (cl=" OFF_T_FMT ")\n", curhdr->index, curhdr->content->length));
339 dprint (1, (debugfile, "\tLINE: %s", buf));
340 if (fseeko (ctx->fp, loc, SEEK_SET) != 0) /* nope, return the previous position */
342 dprint (1, (debugfile, "mbox_parse_mailbox: fseek() failed\n"));
344 curhdr->content->length = -1;
347 else if (tmploc != ctx->size)
349 /* content-length would put us past the end of the file, so it
352 curhdr->content->length = -1;
355 if (curhdr->content->length != -1)
357 /* good content-length. check to see if we know how many lines
358 * are in this message.
360 if (curhdr->lines == 0)
362 int cl = curhdr->content->length;
364 /* count the number of lines in this message */
365 if (fseeko (ctx->fp, loc, SEEK_SET) != 0)
366 dprint (1, (debugfile, "mbox_parse_mailbox: fseek() failed\n"));
369 if (fgetc (ctx->fp) == '\n')
374 /* return to the offset of the next message separator */
375 if (fseeko (ctx->fp, tmploc, SEEK_SET) != 0)
376 dprint (1, (debugfile, "mbox_parse_mailbox: fseek() failed\n"));
382 if (!curhdr->env->return_path && return_path[0])
383 curhdr->env->return_path = rfc822_parse_adrlist (curhdr->env->return_path, return_path);
385 if (!curhdr->env->from)
386 curhdr->env->from = rfc822_cpy_adr (curhdr->env->return_path, 0);
393 loc = ftello (ctx->fp);
397 * Only set the content-length of the previous message if we have read more
398 * than one message during _this_ invocation. If this routine is called
399 * when new mail is received, we need to make sure not to clobber what
400 * previously was the last message since the headers may be sorted.
404 if (PREV->content->length < 0)
406 PREV->content->length = ftello (ctx->fp) - PREV->content->offset - 1;
407 if (PREV->content->length < 0)
408 PREV->content->length = 0;
412 PREV->lines = lines ? lines - 1 : 0;
414 mx_update_context (ctx, count);
422 /* open a mbox or mmdf style mailbox */
423 int mbox_open_mailbox (CONTEXT *ctx)
427 if ((ctx->fp = fopen (ctx->path, "r")) == NULL)
429 mutt_perror (ctx->path);
432 mutt_block_signals ();
433 if (mbox_lock_mailbox (ctx, 0, 1) == -1)
435 mutt_unblock_signals ();
439 if (ctx->magic == M_MBOX)
440 rc = mbox_parse_mailbox (ctx);
441 else if (ctx->magic == M_MMDF)
442 rc = mmdf_parse_mailbox (ctx);
446 mbox_unlock_mailbox (ctx);
447 mutt_unblock_signals ();
451 /* return 1 if address lists are strictly identical */
452 static int strict_addrcmp (const ADDRESS *a, const ADDRESS *b)
456 if (mutt_strcmp (a->mailbox, b->mailbox) ||
457 mutt_strcmp (a->personal, b->personal))
469 static int strict_cmp_lists (const LIST *a, const LIST *b)
473 if (mutt_strcmp (a->data, b->data))
485 static int strict_cmp_envelopes (const ENVELOPE *e1, const ENVELOPE *e2)
489 if (mutt_strcmp (e1->message_id, e2->message_id) ||
490 mutt_strcmp (e1->subject, e2->subject) ||
491 !strict_cmp_lists (e1->references, e2->references) ||
492 !strict_addrcmp (e1->from, e2->from) ||
493 !strict_addrcmp (e1->sender, e2->sender) ||
494 !strict_addrcmp (e1->reply_to, e2->reply_to) ||
495 !strict_addrcmp (e1->to, e2->to) ||
496 !strict_addrcmp (e1->cc, e2->cc) ||
497 !strict_addrcmp (e1->return_path, e2->return_path))
504 if (e1 == NULL && e2 == NULL)
511 static int strict_cmp_parameters (const PARAMETER *p1, const PARAMETER *p2)
515 if (mutt_strcmp (p1->attribute, p2->attribute) ||
516 mutt_strcmp (p1->value, p2->value))
528 static int strict_cmp_bodies (const BODY *b1, const BODY *b2)
530 if (b1->type != b2->type ||
531 b1->encoding != b2->encoding ||
532 mutt_strcmp (b1->subtype, b2->subtype) ||
533 mutt_strcmp (b1->description, b2->description) ||
534 !strict_cmp_parameters (b1->parameter, b2->parameter) ||
535 b1->length != b2->length)
540 /* return 1 if headers are strictly identical */
541 int mbox_strict_cmp_headers (const HEADER *h1, const HEADER *h2)
545 if (h1->received != h2->received ||
546 h1->date_sent != h2->date_sent ||
547 h1->content->length != h2->content->length ||
548 h1->lines != h2->lines ||
549 h1->zhours != h2->zhours ||
550 h1->zminutes != h2->zminutes ||
551 h1->zoccident != h2->zoccident ||
552 h1->mime != h2->mime ||
553 !strict_cmp_envelopes (h1->env, h2->env) ||
554 !strict_cmp_bodies (h1->content, h2->content))
561 if (h1 == NULL && h2 == NULL)
568 /* check to see if the mailbox has changed on disk.
571 * M_REOPENED mailbox has been reopened
572 * M_NEW_MAIL new mail has arrived!
573 * M_LOCKED couldn't lock the file
577 int mbox_check_mailbox (CONTEXT *ctx, int *index_hint)
580 char buffer[LONG_STRING];
584 if (stat (ctx->path, &st) == 0)
586 if (st.st_mtime == ctx->mtime && st.st_size == ctx->size)
589 if (st.st_size == ctx->size)
591 /* the file was touched, but it is still the same length, so just exit */
592 ctx->mtime = st.st_mtime;
596 if (st.st_size > ctx->size)
598 /* lock the file if it isn't already */
601 mutt_block_signals ();
602 if (mbox_lock_mailbox (ctx, 0, 0) == -1)
604 mutt_unblock_signals ();
605 /* we couldn't lock the mailbox, but nothing serious happened:
606 * probably the new mail arrived: no reason to wait till we can
607 * parse it: we'll get it on the next pass
615 * Check to make sure that the only change to the mailbox is that
616 * message(s) were appended to this file. My heuristic is that we should
617 * see the message separator at *exactly* what used to be the end of the
620 if (fseeko (ctx->fp, ctx->size, SEEK_SET) != 0)
621 dprint (1, (debugfile, "mbox_check_mailbox: fseek() failed\n"));
622 if (fgets (buffer, sizeof (buffer), ctx->fp) != NULL)
624 if ((ctx->magic == M_MBOX && mutt_strncmp ("From ", buffer, 5) == 0) ||
625 (ctx->magic == M_MMDF && mutt_strcmp (MMDF_SEP, buffer) == 0))
627 if (fseeko (ctx->fp, ctx->size, SEEK_SET) != 0)
628 dprint (1, (debugfile, "mbox_check_mailbox: fseek() failed\n"));
629 if (ctx->magic == M_MBOX)
630 mbox_parse_mailbox (ctx);
632 mmdf_parse_mailbox (ctx);
634 /* Only unlock the folder if it was locked inside of this routine.
635 * It may have been locked elsewhere, like in
636 * mutt_checkpoint_mailbox().
641 mbox_unlock_mailbox (ctx);
642 mutt_unblock_signals ();
645 return (M_NEW_MAIL); /* signal that new mail arrived */
652 dprint (1, (debugfile, "mbox_check_mailbox: fgets returned NULL.\n"));
662 if (mutt_reopen_mailbox (ctx, index_hint) != -1)
666 mbox_unlock_mailbox (ctx);
667 mutt_unblock_signals ();
675 mbox_unlock_mailbox (ctx);
676 mx_fastclose_mailbox (ctx);
677 mutt_unblock_signals ();
678 mutt_error _("Mailbox was corrupted!");
682 /* if mailbox has at least 1 new message, sets mtime > atime of mailbox
683 * so buffy check reports new mail */
684 static void reset_atime (CONTEXT *ctx)
686 struct utimbuf utimebuf;
688 time_t now = time (NULL);
690 for (i = 0; i < ctx->msgcount; i++)
692 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->read && !ctx->hdrs[i]->old)
694 utimebuf.actime = now - 1;
695 utimebuf.modtime = now;
696 utime (ctx->path, &utimebuf);
706 int mbox_sync_mailbox (CONTEXT *ctx, int *index_hint)
708 char tempfile[_POSIX_PATH_MAX];
710 int i, j, save_sort = SORT_ORDER;
712 int need_sort = 0; /* flag to resort mailbox if new mail arrives */
713 int first = -1; /* first message to be written */
714 LOFF_T offset; /* location in mailbox to write changed messages */
715 struct m_update_t *newOffset = NULL;
716 struct m_update_t *oldOffset = NULL;
721 /* sort message by their position in the mailbox on disk */
722 if (Sort != SORT_ORDER)
726 mutt_sort_headers (ctx, 0);
731 /* need to open the file for writing in such a way that it does not truncate
732 * the file, so use read-write mode.
734 if ((ctx->fp = freopen (ctx->path, "r+", ctx->fp)) == NULL)
736 mx_fastclose_mailbox (ctx);
737 mutt_error _("Fatal error! Could not reopen mailbox!");
741 mutt_block_signals ();
743 if (mbox_lock_mailbox (ctx, 1, 1) == -1)
745 mutt_unblock_signals ();
746 mutt_error _("Unable to lock mailbox!");
750 /* Check to make sure that the file hasn't changed on disk */
751 if ((i = mbox_check_mailbox (ctx, index_hint)) == M_NEW_MAIL || i == M_REOPENED)
753 /* new mail arrived, or mailbox reopened */
762 /* Create a temporary file to write the new version of the mailbox in. */
763 mutt_mktemp (tempfile);
764 if ((i = open (tempfile, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1 ||
765 (fp = fdopen (i, "w")) == NULL)
772 mutt_error _("Could not create temporary file!");
777 /* find the first deleted/changed message. we save a lot of time by only
778 * rewriting the mailbox from the point where it has actually changed.
780 for (i = 0 ; i < ctx->msgcount && !ctx->hdrs[i]->deleted &&
781 !ctx->hdrs[i]->changed && !ctx->hdrs[i]->attach_del; i++)
783 if (i == ctx->msgcount)
785 /* this means ctx->changed or ctx->deleted was set, but no
786 * messages were found to be changed or deleted. This should
787 * never happen, is we presume it is a bug in mutt.
789 mutt_error _("sync: mbox modified, but no modified messages! (report this bug)");
790 mutt_sleep(5); /* the mutt_error /will/ get cleared! */
791 dprint(1, (debugfile, "mbox_sync_mailbox(): no modified messages.\n"));
796 /* save the index of the first changed/deleted message */
798 /* where to start overwriting */
799 offset = ctx->hdrs[i]->offset;
801 /* the offset stored in the header does not include the MMDF_SEP, so make
802 * sure we seek to the correct location
804 if (ctx->magic == M_MMDF)
805 offset -= (sizeof MMDF_SEP - 1);
807 /* allocate space for the new offsets */
808 newOffset = safe_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
809 oldOffset = safe_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
813 snprintf (msgbuf, sizeof (msgbuf), _("Writing %s..."), ctx->path);
814 mutt_progress_init (&progress, msgbuf, M_PROGRESS_MSG, WriteInc, ctx->msgcount);
817 for (i = first, j = 0; i < ctx->msgcount; i++)
820 mutt_progress_update (&progress, i, (int)(ftello (ctx->fp) / (ctx->size / 100 + 1)));
822 * back up some information which is needed to restore offsets when
826 oldOffset[i-first].valid = 1;
827 oldOffset[i-first].hdr = ctx->hdrs[i]->offset;
828 oldOffset[i-first].body = ctx->hdrs[i]->content->offset;
829 oldOffset[i-first].lines = ctx->hdrs[i]->lines;
830 oldOffset[i-first].length = ctx->hdrs[i]->content->length;
832 if (! ctx->hdrs[i]->deleted)
836 if (ctx->magic == M_MMDF)
838 if (fputs (MMDF_SEP, fp) == EOF)
840 mutt_perror (tempfile);
848 /* save the new offset for this message. we add `offset' because the
849 * temporary file only contains saved message which are located after
850 * `offset' in the real mailbox
852 newOffset[i - first].hdr = ftello (fp) + offset;
854 if (mutt_copy_message (fp, ctx, ctx->hdrs[i], M_CM_UPDATE,
855 CH_FROM | CH_UPDATE | CH_UPDATE_LEN) != 0)
857 mutt_perror (tempfile);
863 /* Since messages could have been deleted, the offsets stored in memory
864 * will be wrong, so update what we can, which is the offset of this
865 * message, and the offset of the body. If this is a multipart message,
866 * we just flush the in memory cache so that the message will be reparsed
867 * if the user accesses it later.
869 newOffset[i - first].body = ftello (fp) - ctx->hdrs[i]->content->length + offset;
870 mutt_free_body (&ctx->hdrs[i]->content->parts);
875 if(fputs(MMDF_SEP, fp) == EOF)
877 mutt_perror (tempfile);
884 if(fputs("\n", fp) == EOF)
886 mutt_perror (tempfile);
895 if (fclose (fp) != 0)
898 dprint(1, (debugfile, "mbox_sync_mailbox: safe_fclose (&) returned non-zero.\n"));
900 mutt_perror (tempfile);
906 if ((fp = fopen (tempfile, "r")) == NULL)
908 mutt_unblock_signals ();
909 mx_fastclose_mailbox (ctx);
910 dprint (1, (debugfile, "mbox_sync_mailbox: unable to reopen temp copy of mailbox!\n"));
911 mutt_perror (tempfile);
916 if (fseeko (ctx->fp, offset, SEEK_SET) != 0 || /* seek the append location */
917 /* do a sanity check to make sure the mailbox looks ok */
918 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
919 (ctx->magic == M_MBOX && mutt_strncmp ("From ", buf, 5) != 0) ||
920 (ctx->magic == M_MMDF && mutt_strcmp (MMDF_SEP, buf) != 0))
922 dprint (1, (debugfile, "mbox_sync_mailbox: message not in expected position."));
923 dprint (1, (debugfile, "\tLINE: %s\n", buf));
928 if (fseeko (ctx->fp, offset, SEEK_SET) != 0) /* return to proper offset */
931 dprint (1, (debugfile, "mbox_sync_mailbox: fseek() failed\n"));
935 /* copy the temp mailbox back into place starting at the first
936 * change/deleted message
939 mutt_message _("Committing changes...");
940 i = mutt_copy_stream (fp, ctx->fp);
942 if (ferror (ctx->fp))
947 ctx->size = ftello (ctx->fp); /* update the size of the mailbox */
948 ftruncate (fileno (ctx->fp), ctx->size);
954 mbox_unlock_mailbox (ctx);
956 if (fclose (ctx->fp) != 0 || i == -1)
958 /* error occured while writing the mailbox back, so keep the temp copy
962 char savefile[_POSIX_PATH_MAX];
964 snprintf (savefile, sizeof (savefile), "%s/mutt.%s-%s-%u",
965 NONULL (Tempdir), NONULL(Username), NONULL(Hostname), (unsigned int)getpid ());
966 rename (tempfile, savefile);
967 mutt_unblock_signals ();
968 mx_fastclose_mailbox (ctx);
969 mutt_pretty_mailbox (savefile, sizeof (savefile));
970 mutt_error (_("Write failed! Saved partial mailbox to %s"), savefile);
975 /* reopen the mailbox in read-only mode */
976 if ((ctx->fp = fopen (ctx->path, "r")) == NULL)
979 mutt_unblock_signals ();
980 mx_fastclose_mailbox (ctx);
981 mutt_error _("Fatal error! Could not reopen mailbox!");
985 /* update the offsets of the rewritten messages */
986 for (i = first, j = first; i < ctx->msgcount; i++)
988 if (!ctx->hdrs[i]->deleted)
990 ctx->hdrs[i]->offset = newOffset[i - first].hdr;
991 ctx->hdrs[i]->content->hdr_offset = newOffset[i - first].hdr;
992 ctx->hdrs[i]->content->offset = newOffset[i - first].body;
993 ctx->hdrs[i]->index = j++;
998 unlink (tempfile); /* remove partial copy of the mailbox */
999 mutt_unblock_signals ();
1001 /* if mailbox has new mail, mangle atime+mtime to make buffy check
1002 * report new mail for it */
1003 if (!option (OPTCHECKMBOXSIZE))
1006 return (0); /* signal success */
1008 bail: /* Come here in case of disaster */
1012 /* restore offsets, as far as they are valid */
1013 if (first >= 0 && oldOffset)
1015 for (i = first; i < ctx->msgcount && oldOffset[i-first].valid; i++)
1017 ctx->hdrs[i]->offset = oldOffset[i-first].hdr;
1018 ctx->hdrs[i]->content->hdr_offset = oldOffset[i-first].hdr;
1019 ctx->hdrs[i]->content->offset = oldOffset[i-first].body;
1020 ctx->hdrs[i]->lines = oldOffset[i-first].lines;
1021 ctx->hdrs[i]->content->length = oldOffset[i-first].length;
1025 /* this is ok to call even if we haven't locked anything */
1026 mbox_unlock_mailbox (ctx);
1028 mutt_unblock_signals ();
1032 if ((ctx->fp = freopen (ctx->path, "r", ctx->fp)) == NULL)
1034 mutt_error _("Could not reopen mailbox!");
1035 mx_fastclose_mailbox (ctx);
1040 /* if the mailbox was reopened, the thread tree will be invalid so make
1041 * sure to start threading from scratch. */
1042 mutt_sort_headers (ctx, (need_sort == M_REOPENED));
1047 /* close a mailbox opened in write-mode */
1048 int mbox_close_mailbox (CONTEXT *ctx)
1050 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
1051 mutt_unblock_signals ();
1052 mx_fastclose_mailbox (ctx);
1056 int mutt_reopen_mailbox (CONTEXT *ctx, int *index_hint)
1058 int (*cmp_headers) (const HEADER *, const HEADER *) = NULL;
1066 /* silent operations */
1070 mutt_message _("Reopening mailbox...");
1072 /* our heuristics require the old mailbox to be unsorted */
1073 if (Sort != SORT_ORDER)
1079 mutt_sort_headers (ctx, 1);
1086 /* simulate a close */
1088 hash_destroy (&ctx->id_hash, NULL);
1090 hash_destroy (&ctx->subj_hash, NULL);
1091 mutt_clear_threads (ctx);
1095 for (i = 0; i < ctx->msgcount; i++)
1096 mutt_free_header (&(ctx->hdrs[i])); /* nothing to do! */
1101 /* save the old headers */
1102 old_msgcount = ctx->msgcount;
1103 old_hdrs = ctx->hdrs;
1107 ctx->hdrmax = 0; /* force allocation of new headers */
1116 ctx->id_hash = NULL;
1117 ctx->subj_hash = NULL;
1123 cmp_headers = mbox_strict_cmp_headers;
1124 safe_fclose (&ctx->fp);
1125 if (!(ctx->fp = safe_fopen (ctx->path, "r")))
1128 rc = ((ctx->magic == M_MBOX) ? mbox_parse_mailbox
1129 : mmdf_parse_mailbox) (ctx);
1139 /* free the old headers */
1140 for (j = 0; j < old_msgcount; j++)
1141 mutt_free_header (&(old_hdrs[j]));
1148 /* now try to recover the old flags */
1150 index_hint_set = (index_hint == NULL);
1154 for (i = 0; i < ctx->msgcount; i++)
1158 /* some messages have been deleted, and new messages have been
1159 * appended at the end; the heuristic is that old messages have then
1160 * "advanced" towards the beginning of the folder, so we begin the
1161 * search at index "i"
1163 for (j = i; j < old_msgcount; j++)
1165 if (old_hdrs[j] == NULL)
1167 if (cmp_headers (ctx->hdrs[i], old_hdrs[j]))
1175 for (j = 0; j < i && j < old_msgcount; j++)
1177 if (old_hdrs[j] == NULL)
1179 if (cmp_headers (ctx->hdrs[i], old_hdrs[j]))
1189 /* this is best done here */
1190 if (!index_hint_set && *index_hint == j)
1193 if (old_hdrs[j]->changed)
1195 /* Only update the flags if the old header was changed;
1196 * otherwise, the header may have been modified externally,
1197 * and we don't want to lose _those_ changes
1199 mutt_set_flag (ctx, ctx->hdrs[i], M_FLAG, old_hdrs[j]->flagged);
1200 mutt_set_flag (ctx, ctx->hdrs[i], M_REPLIED, old_hdrs[j]->replied);
1201 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, old_hdrs[j]->old);
1202 mutt_set_flag (ctx, ctx->hdrs[i], M_READ, old_hdrs[j]->read);
1204 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, old_hdrs[j]->deleted);
1205 mutt_set_flag (ctx, ctx->hdrs[i], M_TAG, old_hdrs[j]->tagged);
1207 /* we don't need this header any more */
1208 mutt_free_header (&(old_hdrs[j]));
1212 /* free the remaining old headers */
1213 for (j = 0; j < old_msgcount; j++)
1217 mutt_free_header (&(old_hdrs[j]));
1226 return ((ctx->changed || msg_mod) ? M_REOPENED : M_NEW_MAIL);
1231 * 1 if the mailbox is not empty
1232 * 0 if the mailbox is empty
1235 int mbox_check_empty (const char *path)
1239 if (stat (path, &st) == -1)
1242 return ((st.st_size == 0));