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);
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)(ftell (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);
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!");
686 int mbox_sync_mailbox (CONTEXT *ctx, int *index_hint)
688 char tempfile[_POSIX_PATH_MAX];
690 int i, j, save_sort = SORT_ORDER;
692 int need_sort = 0; /* flag to resort mailbox if new mail arrives */
693 int first = -1; /* first message to be written */
694 LOFF_T offset; /* location in mailbox to write changed messages */
696 struct utimbuf utimebuf;
697 struct m_update_t *newOffset = NULL;
698 struct m_update_t *oldOffset = NULL;
703 /* sort message by their position in the mailbox on disk */
704 if (Sort != SORT_ORDER)
708 mutt_sort_headers (ctx, 0);
713 /* need to open the file for writing in such a way that it does not truncate
714 * the file, so use read-write mode.
716 if ((ctx->fp = freopen (ctx->path, "r+", ctx->fp)) == NULL)
718 mx_fastclose_mailbox (ctx);
719 mutt_error _("Fatal error! Could not reopen mailbox!");
723 mutt_block_signals ();
725 if (mbox_lock_mailbox (ctx, 1, 1) == -1)
727 mutt_unblock_signals ();
728 mutt_error _("Unable to lock mailbox!");
732 /* Check to make sure that the file hasn't changed on disk */
733 if ((i = mbox_check_mailbox (ctx, index_hint)) == M_NEW_MAIL || i == M_REOPENED)
735 /* new mail arrived, or mailbox reopened */
744 /* Create a temporary file to write the new version of the mailbox in. */
745 mutt_mktemp (tempfile);
746 if ((i = open (tempfile, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1 ||
747 (fp = fdopen (i, "w")) == NULL)
754 mutt_error _("Could not create temporary file!");
759 /* find the first deleted/changed message. we save a lot of time by only
760 * rewriting the mailbox from the point where it has actually changed.
762 for (i = 0 ; i < ctx->msgcount && !ctx->hdrs[i]->deleted &&
763 !ctx->hdrs[i]->changed && !ctx->hdrs[i]->attach_del; i++)
765 if (i == ctx->msgcount)
767 /* this means ctx->changed or ctx->deleted was set, but no
768 * messages were found to be changed or deleted. This should
769 * never happen, is we presume it is a bug in mutt.
771 mutt_error _("sync: mbox modified, but no modified messages! (report this bug)");
772 mutt_sleep(5); /* the mutt_error /will/ get cleared! */
773 dprint(1, (debugfile, "mbox_sync_mailbox(): no modified messages.\n"));
778 /* save the index of the first changed/deleted message */
780 /* where to start overwriting */
781 offset = ctx->hdrs[i]->offset;
783 /* the offset stored in the header does not include the MMDF_SEP, so make
784 * sure we seek to the correct location
786 if (ctx->magic == M_MMDF)
787 offset -= (sizeof MMDF_SEP - 1);
789 /* allocate space for the new offsets */
790 newOffset = safe_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
791 oldOffset = safe_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
795 snprintf (msgbuf, sizeof (msgbuf), _("Writing %s..."), ctx->path);
796 mutt_progress_init (&progress, msgbuf, M_PROGRESS_MSG, WriteInc, ctx->msgcount);
799 for (i = first, j = 0; i < ctx->msgcount; i++)
802 mutt_progress_update (&progress, i, (int)(ftell (ctx->fp) / (ctx->size / 100 + 1)));
804 * back up some information which is needed to restore offsets when
808 oldOffset[i-first].valid = 1;
809 oldOffset[i-first].hdr = ctx->hdrs[i]->offset;
810 oldOffset[i-first].body = ctx->hdrs[i]->content->offset;
811 oldOffset[i-first].lines = ctx->hdrs[i]->lines;
812 oldOffset[i-first].length = ctx->hdrs[i]->content->length;
814 if (! ctx->hdrs[i]->deleted)
818 if (ctx->magic == M_MMDF)
820 if (fputs (MMDF_SEP, fp) == EOF)
822 mutt_perror (tempfile);
830 /* save the new offset for this message. we add `offset' because the
831 * temporary file only contains saved message which are located after
832 * `offset' in the real mailbox
834 newOffset[i - first].hdr = ftello (fp) + offset;
836 if (mutt_copy_message (fp, ctx, ctx->hdrs[i], M_CM_UPDATE, CH_FROM | CH_UPDATE | CH_UPDATE_LEN) == -1)
838 mutt_perror (tempfile);
844 /* Since messages could have been deleted, the offsets stored in memory
845 * will be wrong, so update what we can, which is the offset of this
846 * message, and the offset of the body. If this is a multipart message,
847 * we just flush the in memory cache so that the message will be reparsed
848 * if the user accesses it later.
850 newOffset[i - first].body = ftello (fp) - ctx->hdrs[i]->content->length + offset;
851 mutt_free_body (&ctx->hdrs[i]->content->parts);
856 if(fputs(MMDF_SEP, fp) == EOF)
858 mutt_perror (tempfile);
865 if(fputs("\n", fp) == EOF)
867 mutt_perror (tempfile);
876 if (fclose (fp) != 0)
879 dprint(1, (debugfile, "mbox_sync_mailbox: fclose() returned non-zero.\n"));
881 mutt_perror (tempfile);
887 /* Save the state of this folder. */
888 if (stat (ctx->path, &statbuf) == -1)
890 mutt_perror (ctx->path);
896 if ((fp = fopen (tempfile, "r")) == NULL)
898 mutt_unblock_signals ();
899 mx_fastclose_mailbox (ctx);
900 dprint (1, (debugfile, "mbox_sync_mailbox: unable to reopen temp copy of mailbox!\n"));
901 mutt_perror (tempfile);
906 if (fseeko (ctx->fp, offset, SEEK_SET) != 0 || /* seek the append location */
907 /* do a sanity check to make sure the mailbox looks ok */
908 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
909 (ctx->magic == M_MBOX && mutt_strncmp ("From ", buf, 5) != 0) ||
910 (ctx->magic == M_MMDF && mutt_strcmp (MMDF_SEP, buf) != 0))
912 dprint (1, (debugfile, "mbox_sync_mailbox: message not in expected position."));
913 dprint (1, (debugfile, "\tLINE: %s\n", buf));
918 if (fseeko (ctx->fp, offset, SEEK_SET) != 0) /* return to proper offset */
921 dprint (1, (debugfile, "mbox_sync_mailbox: fseek() failed\n"));
925 /* copy the temp mailbox back into place starting at the first
926 * change/deleted message
928 mutt_message _("Committing changes...");
929 i = mutt_copy_stream (fp, ctx->fp);
931 if (ferror (ctx->fp))
936 ctx->size = ftello (ctx->fp); /* update the size of the mailbox */
937 ftruncate (fileno (ctx->fp), ctx->size);
943 mbox_unlock_mailbox (ctx);
945 if (fclose (ctx->fp) != 0 || i == -1)
947 /* error occured while writing the mailbox back, so keep the temp copy
951 char savefile[_POSIX_PATH_MAX];
953 snprintf (savefile, sizeof (savefile), "%s/mutt.%s-%s-%u",
954 NONULL (Tempdir), NONULL(Username), NONULL(Hostname), (unsigned int)getpid ());
955 rename (tempfile, savefile);
956 mutt_unblock_signals ();
957 mx_fastclose_mailbox (ctx);
958 mutt_pretty_mailbox (savefile);
959 mutt_error (_("Write failed! Saved partial mailbox to %s"), savefile);
964 /* Restore the previous access/modification times */
965 utimebuf.actime = statbuf.st_atime;
966 utimebuf.modtime = statbuf.st_mtime;
967 utime (ctx->path, &utimebuf);
969 /* reopen the mailbox in read-only mode */
970 if ((ctx->fp = fopen (ctx->path, "r")) == NULL)
973 mutt_unblock_signals ();
974 mx_fastclose_mailbox (ctx);
975 mutt_error _("Fatal error! Could not reopen mailbox!");
979 /* update the offsets of the rewritten messages */
980 for (i = first, j = first; i < ctx->msgcount; i++)
982 if (!ctx->hdrs[i]->deleted)
984 ctx->hdrs[i]->offset = newOffset[i - first].hdr;
985 ctx->hdrs[i]->content->hdr_offset = newOffset[i - first].hdr;
986 ctx->hdrs[i]->content->offset = newOffset[i - first].body;
987 ctx->hdrs[i]->index = j++;
992 unlink (tempfile); /* remove partial copy of the mailbox */
993 mutt_unblock_signals ();
995 return (0); /* signal success */
997 bail: /* Come here in case of disaster */
1001 /* restore offsets, as far as they are valid */
1002 if (first >= 0 && oldOffset)
1004 for (i = first; i < ctx->msgcount && oldOffset[i-first].valid; i++)
1006 ctx->hdrs[i]->offset = oldOffset[i-first].hdr;
1007 ctx->hdrs[i]->content->hdr_offset = oldOffset[i-first].hdr;
1008 ctx->hdrs[i]->content->offset = oldOffset[i-first].body;
1009 ctx->hdrs[i]->lines = oldOffset[i-first].lines;
1010 ctx->hdrs[i]->content->length = oldOffset[i-first].length;
1014 /* this is ok to call even if we haven't locked anything */
1015 mbox_unlock_mailbox (ctx);
1017 mutt_unblock_signals ();
1021 if ((ctx->fp = freopen (ctx->path, "r", ctx->fp)) == NULL)
1023 mutt_error _("Could not reopen mailbox!");
1024 mx_fastclose_mailbox (ctx);
1029 /* if the mailbox was reopened, the thread tree will be invalid so make
1030 * sure to start threading from scratch. */
1031 mutt_sort_headers (ctx, (need_sort == M_REOPENED));
1036 /* close a mailbox opened in write-mode */
1037 int mbox_close_mailbox (CONTEXT *ctx)
1039 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
1040 mutt_unblock_signals ();
1041 mx_fastclose_mailbox (ctx);
1045 int mutt_reopen_mailbox (CONTEXT *ctx, int *index_hint)
1047 int (*cmp_headers) (const HEADER *, const HEADER *) = NULL;
1055 /* silent operations */
1058 mutt_message _("Reopening mailbox...");
1060 /* our heuristics require the old mailbox to be unsorted */
1061 if (Sort != SORT_ORDER)
1067 mutt_sort_headers (ctx, 1);
1074 /* simulate a close */
1076 hash_destroy (&ctx->id_hash, NULL);
1078 hash_destroy (&ctx->subj_hash, NULL);
1079 mutt_clear_threads (ctx);
1083 for (i = 0; i < ctx->msgcount; i++)
1084 mutt_free_header (&(ctx->hdrs[i])); /* nothing to do! */
1089 /* save the old headers */
1090 old_msgcount = ctx->msgcount;
1091 old_hdrs = ctx->hdrs;
1095 ctx->hdrmax = 0; /* force allocation of new headers */
1104 ctx->id_hash = NULL;
1105 ctx->subj_hash = NULL;
1111 if (fseek (ctx->fp, 0, SEEK_SET) != 0)
1113 dprint (1, (debugfile, "mutt_reopen_mailbox: fseek() failed\n"));
1118 cmp_headers = mbox_strict_cmp_headers;
1119 rc = ((ctx->magic == M_MBOX) ? mbox_parse_mailbox
1120 : mmdf_parse_mailbox) (ctx);
1131 /* free the old headers */
1132 for (j = 0; j < old_msgcount; j++)
1133 mutt_free_header (&(old_hdrs[j]));
1140 /* now try to recover the old flags */
1142 index_hint_set = (index_hint == NULL);
1146 for (i = 0; i < ctx->msgcount; i++)
1150 /* some messages have been deleted, and new messages have been
1151 * appended at the end; the heuristic is that old messages have then
1152 * "advanced" towards the beginning of the folder, so we begin the
1153 * search at index "i"
1155 for (j = i; j < old_msgcount; j++)
1157 if (old_hdrs[j] == NULL)
1159 if (cmp_headers (ctx->hdrs[i], old_hdrs[j]))
1167 for (j = 0; j < i && j < old_msgcount; j++)
1169 if (old_hdrs[j] == NULL)
1171 if (cmp_headers (ctx->hdrs[i], old_hdrs[j]))
1181 /* this is best done here */
1182 if (!index_hint_set && *index_hint == j)
1185 if (old_hdrs[j]->changed)
1187 /* Only update the flags if the old header was changed;
1188 * otherwise, the header may have been modified externally,
1189 * and we don't want to lose _those_ changes
1191 mutt_set_flag (ctx, ctx->hdrs[i], M_FLAG, old_hdrs[j]->flagged);
1192 mutt_set_flag (ctx, ctx->hdrs[i], M_REPLIED, old_hdrs[j]->replied);
1193 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, old_hdrs[j]->old);
1194 mutt_set_flag (ctx, ctx->hdrs[i], M_READ, old_hdrs[j]->read);
1196 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, old_hdrs[j]->deleted);
1197 mutt_set_flag (ctx, ctx->hdrs[i], M_TAG, old_hdrs[j]->tagged);
1199 /* we don't need this header any more */
1200 mutt_free_header (&(old_hdrs[j]));
1204 /* free the remaining old headers */
1205 for (j = 0; j < old_msgcount; j++)
1209 mutt_free_header (&(old_hdrs[j]));
1218 return ((ctx->changed || msg_mod) ? M_REOPENED : M_NEW_MAIL);
1223 * 1 if the mailbox is not empty
1224 * 0 if the mailbox is empty
1227 int mbox_check_empty (const char *path)
1231 if (stat (path, &st) == -1)
1234 return ((st.st_size == 0));