2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
10 /* This file contains code to parse ``mbox'' and ``mmdf'' style mailboxes */
39 /* struct used by mutt_sync_mailbox() to store new offsets */
49 * ctx - context to lock
50 * excl - exclusive lock?
51 * retry - should retry if unable to lock?
53 int mbox_lock_mailbox (CONTEXT * ctx, int excl, int retry)
57 if ((r = mx_lock_file (ctx->path, fileno (ctx->fp), excl, 1, retry)) == 0)
59 else if (retry && !excl) {
67 void mbox_unlock_mailbox (CONTEXT * ctx)
72 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
77 int mmdf_parse_mailbox (CONTEXT * ctx)
79 char buf[HUGE_STRING];
80 char return_path[LONG_STRING];
81 int count = 0, oldmsgcount = ctx->msgcount;
88 #ifdef NFS_ATTRIBUTE_HACK
89 struct utimbuf newtime;
92 if (stat (ctx->path, &sb) == -1) {
93 mutt_perror (ctx->path);
96 ctx->mtime = sb.st_mtime;
97 ctx->size = sb.st_size;
99 #ifdef NFS_ATTRIBUTE_HACK
100 if (sb.st_mtime > sb.st_atime) {
101 newtime.modtime = sb.st_mtime;
102 newtime.actime = time (NULL);
103 utime (ctx->path, &newtime);
107 /* precompute the local timezone to speed up calculation of the
109 tz = mutt_local_tz (0);
111 buf[sizeof (buf) - 1] = 0;
114 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
117 if (safe_strcmp (buf, MMDF_SEP) == 0) {
118 loc = ftell (ctx->fp);
121 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
122 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
123 (int) (loc / (ctx->size / 100 + 1)));
126 if (ctx->msgcount == ctx->hdrmax)
127 mx_alloc_memory (ctx);
128 ctx->hdrs[ctx->msgcount] = hdr = mutt_new_header ();
130 hdr->index = ctx->msgcount;
132 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL) {
133 /* TODO: memory leak??? */
134 dprint (1, (debugfile, "mmdf_parse_mailbox: unexpected EOF\n"));
140 if (!is_from (buf, return_path, sizeof (return_path), &t)) {
141 if (fseek (ctx->fp, loc, SEEK_SET) != 0) {
142 dprint (1, (debugfile, "mmdf_parse_mailbox: fseek() failed\n"));
143 mutt_error _("Mailbox is corrupt!");
149 hdr->received = t - tz;
151 hdr->env = mutt_read_rfc822_header (ctx->fp, hdr, 0, 0);
153 loc = ftell (ctx->fp);
155 if (hdr->content->length > 0 && hdr->lines > 0) {
156 tmploc = loc + hdr->content->length;
158 if (0 < tmploc && tmploc < ctx->size) {
159 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0 ||
160 fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL ||
161 safe_strcmp (MMDF_SEP, buf) != 0) {
162 if (fseek (ctx->fp, loc, SEEK_SET) != 0)
163 dprint (1, (debugfile, "mmdf_parse_mailbox: fseek() failed\n"));
164 hdr->content->length = -1;
168 hdr->content->length = -1;
171 hdr->content->length = -1;
173 if (hdr->content->length < 0) {
176 loc = ftell (ctx->fp);
177 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
180 } while (safe_strcmp (buf, MMDF_SEP) != 0);
183 hdr->content->length = loc - hdr->content->offset;
186 if (!hdr->env->return_path && return_path[0])
187 hdr->env->return_path =
188 rfc822_parse_adrlist (hdr->env->return_path, return_path);
191 hdr->env->from = rfc822_cpy_adr (hdr->env->return_path);
196 dprint (1, (debugfile, "mmdf_parse_mailbox: corrupt mailbox!\n"));
197 mutt_error _("Mailbox is corrupt!");
203 if (ctx->msgcount > oldmsgcount)
204 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
209 /* Note that this function is also called when new mail is appended to the
210 * currently open folder, and NOT just when the mailbox is initially read.
212 * NOTE: it is assumed that the mailbox being read has been locked before
213 * this routine gets called. Strange things could happen if it's not!
215 int mbox_parse_mailbox (CONTEXT * ctx)
218 char buf[HUGE_STRING], return_path[STRING];
221 int count = 0, lines = 0;
224 #ifdef NFS_ATTRIBUTE_HACK
225 struct utimbuf newtime;
228 /* Save information about the folder at the time we opened it. */
229 if (stat (ctx->path, &sb) == -1) {
230 mutt_perror (ctx->path);
234 ctx->size = sb.st_size;
235 ctx->mtime = sb.st_mtime;
237 #ifdef NFS_ATTRIBUTE_HACK
238 if (sb.st_mtime > sb.st_atime) {
239 newtime.modtime = sb.st_mtime;
240 newtime.actime = time (NULL);
241 utime (ctx->path, &newtime);
246 ctx->readonly = access (ctx->path, W_OK) ? 1 : 0;
248 /* precompute the local timezone to speed up calculation of the
250 tz = mutt_local_tz (0);
252 loc = ftell (ctx->fp);
253 while (fgets (buf, sizeof (buf), ctx->fp) != NULL) {
254 if (is_from (buf, return_path, sizeof (return_path), &t)) {
255 /* Save the Content-Length of the previous message */
257 #define PREV ctx->hdrs[ctx->msgcount-1]
259 if (PREV->content->length < 0) {
260 PREV->content->length = loc - PREV->content->offset - 1;
261 if (PREV->content->length < 0)
262 PREV->content->length = 0;
265 PREV->lines = lines ? lines - 1 : 0;
270 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
271 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
272 (int) (ftell (ctx->fp) / (ctx->size / 100 + 1)));
274 if (ctx->msgcount == ctx->hdrmax)
275 mx_alloc_memory (ctx);
277 curhdr = ctx->hdrs[ctx->msgcount] = mutt_new_header ();
278 curhdr->received = t - tz;
279 curhdr->offset = loc;
280 curhdr->index = ctx->msgcount;
282 curhdr->env = mutt_read_rfc822_header (ctx->fp, curhdr, 0, 0);
284 /* if we know how long this message is, either just skip over the body,
285 * or if we don't know how many lines there are, count them now (this will
286 * save time by not having to search for the next message marker).
288 if (curhdr->content->length > 0) {
291 loc = ftell (ctx->fp);
292 tmploc = loc + curhdr->content->length + 1;
294 if (0 < tmploc && tmploc < ctx->size) {
296 * check to see if the content-length looks valid. we expect to
297 * to see a valid message separator at this point in the stream
299 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0 ||
300 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
301 safe_strncmp ("From ", buf, 5) != 0) {
304 "mbox_parse_mailbox: bad content-length in message %d (cl=%ld)\n",
305 curhdr->index, curhdr->content->length));
306 dprint (1, (debugfile, "\tLINE: %s", buf));
307 if (fseek (ctx->fp, loc, SEEK_SET) != 0) { /* nope, return the previous position */
308 dprint (1, (debugfile, "mbox_parse_mailbox: fseek() failed\n"));
310 curhdr->content->length = -1;
313 else if (tmploc != ctx->size) {
314 /* content-length would put us past the end of the file, so it
317 curhdr->content->length = -1;
320 if (curhdr->content->length != -1) {
321 /* good content-length. check to see if we know how many lines
322 * are in this message.
324 if (curhdr->lines == 0) {
325 int cl = curhdr->content->length;
327 /* count the number of lines in this message */
328 if (fseek (ctx->fp, loc, SEEK_SET) != 0)
329 dprint (1, (debugfile, "mbox_parse_mailbox: fseek() failed\n"));
331 if (fgetc (ctx->fp) == '\n')
336 /* return to the offset of the next message separator */
337 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0)
338 dprint (1, (debugfile, "mbox_parse_mailbox: fseek() failed\n"));
344 if (!curhdr->env->return_path && return_path[0])
345 curhdr->env->return_path =
346 rfc822_parse_adrlist (curhdr->env->return_path, return_path);
348 if (!curhdr->env->from)
349 curhdr->env->from = rfc822_cpy_adr (curhdr->env->return_path);
356 loc = ftell (ctx->fp);
360 * Only set the content-length of the previous message if we have read more
361 * than one message during _this_ invocation. If this routine is called
362 * when new mail is received, we need to make sure not to clobber what
363 * previously was the last message since the headers may be sorted.
366 if (PREV->content->length < 0) {
367 PREV->content->length = ftell (ctx->fp) - PREV->content->offset - 1;
368 if (PREV->content->length < 0)
369 PREV->content->length = 0;
373 PREV->lines = lines ? lines - 1 : 0;
375 mx_update_context (ctx, count);
383 /* open a mbox or mmdf style mailbox */
384 int mbox_open_mailbox (CONTEXT * ctx)
388 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
389 mutt_perror (ctx->path);
392 mutt_block_signals ();
393 if (mbox_lock_mailbox (ctx, 0, 1) == -1) {
394 mutt_unblock_signals ();
398 if (ctx->magic == M_MBOX)
399 rc = mbox_parse_mailbox (ctx);
400 else if (ctx->magic == M_MMDF)
401 rc = mmdf_parse_mailbox (ctx);
405 mbox_unlock_mailbox (ctx);
406 mutt_unblock_signals ();
410 /* return 1 if address lists are strictly identical */
411 static int strict_addrcmp (const ADDRESS * a, const ADDRESS * b)
414 if (safe_strcmp (a->mailbox, b->mailbox) ||
415 safe_strcmp (a->personal, b->personal))
427 static int strict_cmp_lists (const LIST * a, const LIST * b)
430 if (safe_strcmp (a->data, b->data))
442 static int strict_cmp_envelopes (const ENVELOPE * e1, const ENVELOPE * e2)
445 if (safe_strcmp (e1->message_id, e2->message_id) ||
446 safe_strcmp (e1->subject, e2->subject) ||
447 !strict_cmp_lists (e1->references, e2->references) ||
448 !strict_addrcmp (e1->from, e2->from) ||
449 !strict_addrcmp (e1->sender, e2->sender) ||
450 !strict_addrcmp (e1->reply_to, e2->reply_to) ||
451 !strict_addrcmp (e1->to, e2->to) ||
452 !strict_addrcmp (e1->cc, e2->cc) ||
453 !strict_addrcmp (e1->return_path, e2->return_path))
459 if (e1 == NULL && e2 == NULL)
466 static int strict_cmp_parameters (const PARAMETER * p1, const PARAMETER * p2)
469 if (safe_strcmp (p1->attribute, p2->attribute) ||
470 safe_strcmp (p1->value, p2->value))
482 static int strict_cmp_bodies (const BODY * b1, const BODY * b2)
484 if (b1->type != b2->type ||
485 b1->encoding != b2->encoding ||
486 safe_strcmp (b1->subtype, b2->subtype) ||
487 safe_strcmp (b1->description, b2->description) ||
488 !strict_cmp_parameters (b1->parameter, b2->parameter) ||
489 b1->length != b2->length)
494 /* return 1 if headers are strictly identical */
495 int mbox_strict_cmp_headers (const HEADER * h1, const HEADER * h2)
498 if (h1->received != h2->received ||
499 h1->date_sent != h2->date_sent ||
500 h1->content->length != h2->content->length ||
501 h1->lines != h2->lines ||
502 h1->zhours != h2->zhours ||
503 h1->zminutes != h2->zminutes ||
504 h1->zoccident != h2->zoccident ||
505 h1->mime != h2->mime ||
506 !strict_cmp_envelopes (h1->env, h2->env) ||
507 !strict_cmp_bodies (h1->content, h2->content))
513 if (h1 == NULL && h2 == NULL)
520 /* check to see if the mailbox has changed on disk.
523 * M_REOPENED mailbox has been reopened
524 * M_NEW_MAIL new mail has arrived!
525 * M_LOCKED couldn't lock the file
529 int mbox_check_mailbox (CONTEXT * ctx, int *index_hint)
532 char buffer[LONG_STRING];
536 if (stat (ctx->path, &st) == 0) {
537 if (st.st_mtime == ctx->mtime && st.st_size == ctx->size)
540 if (st.st_size == ctx->size) {
541 /* the file was touched, but it is still the same length, so just exit */
542 ctx->mtime = st.st_mtime;
546 if (st.st_size > ctx->size) {
547 /* lock the file if it isn't already */
549 mutt_block_signals ();
550 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
551 mutt_unblock_signals ();
552 /* we couldn't lock the mailbox, but nothing serious happened:
553 * probably the new mail arrived: no reason to wait till we can
554 * parse it: we'll get it on the next pass
562 * Check to make sure that the only change to the mailbox is that
563 * message(s) were appended to this file. My heuristic is that we should
564 * see the message separator at *exactly* what used to be the end of the
567 if (fseek (ctx->fp, ctx->size, SEEK_SET) != 0)
568 dprint (1, (debugfile, "mbox_check_mailbox: fseek() failed\n"));
569 if (fgets (buffer, sizeof (buffer), ctx->fp) != NULL) {
570 if ((ctx->magic == M_MBOX && safe_strncmp ("From ", buffer, 5) == 0)
571 || (ctx->magic == M_MMDF && safe_strcmp (MMDF_SEP, buffer) == 0)) {
572 if (fseek (ctx->fp, ctx->size, SEEK_SET) != 0)
573 dprint (1, (debugfile, "mbox_check_mailbox: fseek() failed\n"));
574 if (ctx->magic == M_MBOX)
575 mbox_parse_mailbox (ctx);
577 mmdf_parse_mailbox (ctx);
579 /* Only unlock the folder if it was locked inside of this routine.
580 * It may have been locked elsewhere, like in
581 * mutt_checkpoint_mailbox().
585 mbox_unlock_mailbox (ctx);
586 mutt_unblock_signals ();
589 return (M_NEW_MAIL); /* signal that new mail arrived */
595 dprint (1, (debugfile, "mbox_check_mailbox: fgets returned NULL.\n"));
604 if (mutt_reopen_mailbox (ctx, index_hint) != -1) {
606 mbox_unlock_mailbox (ctx);
607 mutt_unblock_signals ();
615 mbox_unlock_mailbox (ctx);
616 mx_fastclose_mailbox (ctx);
617 mutt_unblock_signals ();
618 mutt_error _("Mailbox was corrupted!");
627 int mbox_sync_mailbox (CONTEXT * ctx, int *index_hint)
629 char tempfile[_POSIX_PATH_MAX];
631 int i, j, save_sort = SORT_ORDER;
633 int need_sort = 0; /* flag to resort mailbox if new mail arrives */
634 int first = -1; /* first message to be written */
635 long offset; /* location in mailbox to write changed messages */
637 struct utimbuf utimebuf;
638 struct m_update_t *newOffset = NULL;
639 struct m_update_t *oldOffset = NULL;
642 /* sort message by their position in the mailbox on disk */
643 if (Sort != SORT_ORDER) {
646 mutt_sort_headers (ctx, 0);
649 /* need to open the file for writing in such a way that it does not truncate
650 * the file, so use read-write mode.
652 if ((ctx->fp = freopen (ctx->path, "r+", ctx->fp)) == NULL) {
653 mx_fastclose_mailbox (ctx);
654 mutt_error _("Fatal error! Could not reopen mailbox!");
659 mutt_block_signals ();
661 if (mbox_lock_mailbox (ctx, 1, 1) == -1) {
662 mutt_unblock_signals ();
663 mutt_error _("Unable to lock mailbox!");
668 /* Check to make sure that the file hasn't changed on disk */
669 if ((i = mbox_check_mailbox (ctx, index_hint)) == M_NEW_MAIL
670 || i == M_REOPENED) {
671 /* new mail arrived, or mailbox reopened */
682 /* Create a temporary file to write the new version of the mailbox in. */
683 mutt_mktemp (tempfile);
684 if ((i = open (tempfile, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1 ||
685 (fp = fdopen (i, "w")) == NULL) {
690 mutt_error _("Could not create temporary file!");
696 /* find the first deleted/changed message. we save a lot of time by only
697 * rewriting the mailbox from the point where it has actually changed.
699 for (i = 0; i < ctx->msgcount && !ctx->hdrs[i]->deleted &&
700 !ctx->hdrs[i]->changed && !ctx->hdrs[i]->attach_del; i++);
701 if (i == ctx->msgcount) {
702 /* this means ctx->changed or ctx->deleted was set, but no
703 * messages were found to be changed or deleted. This should
704 * never happen, is we presume it is a bug in mutt.
707 _("sync: mbox modified, but no modified messages! (report this bug)");
708 mutt_sleep (5); /* the mutt_error /will/ get cleared! */
709 dprint (1, (debugfile, "mbox_sync_mailbox(): no modified messages.\n"));
714 /* save the index of the first changed/deleted message */
716 /* where to start overwriting */
717 offset = ctx->hdrs[i]->offset;
719 /* the offset stored in the header does not include the MMDF_SEP, so make
720 * sure we seek to the correct location
722 if (ctx->magic == M_MMDF)
723 offset -= (sizeof MMDF_SEP - 1);
725 /* allocate space for the new offsets */
726 newOffset = safe_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
727 oldOffset = safe_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
729 for (i = first, j = 0; i < ctx->msgcount; i++) {
731 * back up some information which is needed to restore offsets when
735 oldOffset[i - first].valid = 1;
736 oldOffset[i - first].hdr = ctx->hdrs[i]->offset;
737 oldOffset[i - first].body = ctx->hdrs[i]->content->offset;
738 oldOffset[i - first].lines = ctx->hdrs[i]->lines;
739 oldOffset[i - first].length = ctx->hdrs[i]->content->length;
741 if (!ctx->hdrs[i]->deleted) {
743 if (!ctx->quiet && WriteInc && ((i % WriteInc) == 0 || j == 1))
744 mutt_message (_("Writing messages... %d (%d%%)"), i,
745 (int) (ftell (ctx->fp) / (ctx->size / 100 + 1)));
747 if (ctx->magic == M_MMDF) {
748 if (fputs (MMDF_SEP, fp) == EOF) {
749 mutt_perror (tempfile);
757 /* save the new offset for this message. we add `offset' because the
758 * temporary file only contains saved message which are located after
759 * `offset' in the real mailbox
761 newOffset[i - first].hdr = ftell (fp) + offset;
763 if (mutt_copy_message
764 (fp, ctx, ctx->hdrs[i], M_CM_UPDATE,
765 CH_FROM | CH_UPDATE | CH_UPDATE_LEN) == -1) {
766 mutt_perror (tempfile);
772 /* Since messages could have been deleted, the offsets stored in memory
773 * will be wrong, so update what we can, which is the offset of this
774 * message, and the offset of the body. If this is a multipart message,
775 * we just flush the in memory cache so that the message will be reparsed
776 * if the user accesses it later.
778 newOffset[i - first].body =
779 ftell (fp) - ctx->hdrs[i]->content->length + offset;
780 mutt_free_body (&ctx->hdrs[i]->content->parts);
782 switch (ctx->magic) {
784 if (fputs (MMDF_SEP, fp) == EOF) {
785 mutt_perror (tempfile);
792 if (fputs ("\n", fp) == EOF) {
793 mutt_perror (tempfile);
802 if (fclose (fp) != 0) {
805 (debugfile, "mbox_sync_mailbox: fclose() returned non-zero.\n"));
807 mutt_perror (tempfile);
813 /* Save the state of this folder. */
814 if (stat (ctx->path, &statbuf) == -1) {
815 mutt_perror (ctx->path);
821 if ((fp = fopen (tempfile, "r")) == NULL) {
822 mutt_unblock_signals ();
823 mx_fastclose_mailbox (ctx);
826 "mbox_sync_mailbox: unable to reopen temp copy of mailbox!\n"));
827 mutt_perror (tempfile);
832 if (fseek (ctx->fp, offset, SEEK_SET) != 0 || /* seek the append location */
833 /* do a sanity check to make sure the mailbox looks ok */
834 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
835 (ctx->magic == M_MBOX && safe_strncmp ("From ", buf, 5) != 0) ||
836 (ctx->magic == M_MMDF && safe_strcmp (MMDF_SEP, buf) != 0)) {
839 "mbox_sync_mailbox: message not in expected position."));
840 dprint (1, (debugfile, "\tLINE: %s\n", buf));
844 if (fseek (ctx->fp, offset, SEEK_SET) != 0) { /* return to proper offset */
846 dprint (1, (debugfile, "mbox_sync_mailbox: fseek() failed\n"));
849 /* copy the temp mailbox back into place starting at the first
850 * change/deleted message
852 mutt_message _("Committing changes...");
854 i = mutt_copy_stream (fp, ctx->fp);
856 if (ferror (ctx->fp))
860 ctx->size = ftell (ctx->fp); /* update the size of the mailbox */
861 ftruncate (fileno (ctx->fp), ctx->size);
867 mbox_unlock_mailbox (ctx);
869 if (fclose (ctx->fp) != 0 || i == -1) {
870 /* error occured while writing the mailbox back, so keep the temp copy
874 char savefile[_POSIX_PATH_MAX];
876 snprintf (savefile, sizeof (savefile), "%s/mutt.%s-%s-%u",
877 NONULL (Tempdir), NONULL (Username), NONULL (Hostname),
878 (unsigned int) getpid ());
879 rename (tempfile, savefile);
880 mutt_unblock_signals ();
881 mx_fastclose_mailbox (ctx);
882 mutt_pretty_mailbox (savefile);
883 mutt_error (_("Write failed! Saved partial mailbox to %s"), savefile);
888 /* Restore the previous access/modification times */
889 utimebuf.actime = statbuf.st_atime;
890 utimebuf.modtime = statbuf.st_mtime;
891 utime (ctx->path, &utimebuf);
893 /* reopen the mailbox in read-only mode */
894 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
896 mutt_unblock_signals ();
897 mx_fastclose_mailbox (ctx);
898 mutt_error _("Fatal error! Could not reopen mailbox!");
904 /* update the offsets of the rewritten messages */
905 for (i = first, j = first; i < ctx->msgcount; i++) {
906 if (!ctx->hdrs[i]->deleted) {
907 ctx->hdrs[i]->offset = newOffset[i - first].hdr;
908 ctx->hdrs[i]->content->hdr_offset = newOffset[i - first].hdr;
909 ctx->hdrs[i]->content->offset = newOffset[i - first].body;
910 ctx->hdrs[i]->index = j++;
915 unlink (tempfile); /* remove partial copy of the mailbox */
916 mutt_unblock_signals ();
917 Sort = save_sort; /* Restore the default value. */
919 return (0); /* signal success */
921 bail: /* Come here in case of disaster */
925 /* restore offsets, as far as they are valid */
926 if (first >= 0 && oldOffset) {
927 for (i = first; i < ctx->msgcount && oldOffset[i - first].valid; i++) {
928 ctx->hdrs[i]->offset = oldOffset[i - first].hdr;
929 ctx->hdrs[i]->content->hdr_offset = oldOffset[i - first].hdr;
930 ctx->hdrs[i]->content->offset = oldOffset[i - first].body;
931 ctx->hdrs[i]->lines = oldOffset[i - first].lines;
932 ctx->hdrs[i]->content->length = oldOffset[i - first].length;
936 /* this is ok to call even if we haven't locked anything */
937 mbox_unlock_mailbox (ctx);
939 mutt_unblock_signals ();
943 if ((ctx->fp = freopen (ctx->path, "r", ctx->fp)) == NULL) {
944 mutt_error _("Could not reopen mailbox!");
946 mx_fastclose_mailbox (ctx);
950 if (need_sort || save_sort != Sort) {
952 /* if the mailbox was reopened, the thread tree will be invalid so make
953 * sure to start threading from scratch. */
954 mutt_sort_headers (ctx, (need_sort == M_REOPENED));
960 /* close a mailbox opened in write-mode */
961 int mbox_close_mailbox (CONTEXT * ctx)
963 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
965 #ifdef USE_COMPRESSED
966 if (ctx->compressinfo)
967 mutt_slow_close_compressed (ctx);
970 mutt_unblock_signals ();
971 mx_fastclose_mailbox (ctx);
975 int mutt_reopen_mailbox (CONTEXT * ctx, int *index_hint)
977 int (*cmp_headers) (const HEADER *, const HEADER *) = NULL;
985 /* silent operations */
988 mutt_message _("Reopening mailbox...");
990 /* our heuristics require the old mailbox to be unsorted */
991 if (Sort != SORT_ORDER) {
996 mutt_sort_headers (ctx, 1);
1003 /* simulate a close */
1005 hash_destroy (&ctx->id_hash, NULL);
1007 hash_destroy (&ctx->subj_hash, NULL);
1008 mutt_clear_threads (ctx);
1010 if (ctx->readonly) {
1011 for (i = 0; i < ctx->msgcount; i++)
1012 mutt_free_header (&(ctx->hdrs[i])); /* nothing to do! */
1016 /* save the old headers */
1017 old_msgcount = ctx->msgcount;
1018 old_hdrs = ctx->hdrs;
1022 ctx->hdrmax = 0; /* force allocation of new headers */
1031 ctx->id_hash = NULL;
1032 ctx->subj_hash = NULL;
1034 switch (ctx->magic) {
1037 if (fseek (ctx->fp, 0, SEEK_SET) != 0) {
1038 dprint (1, (debugfile, "mutt_reopen_mailbox: fseek() failed\n"));
1042 cmp_headers = mbox_strict_cmp_headers;
1043 if (ctx->magic == M_MBOX)
1044 rc = mbox_parse_mailbox (ctx);
1046 rc = mmdf_parse_mailbox (ctx);
1056 /* free the old headers */
1057 for (j = 0; j < old_msgcount; j++)
1058 mutt_free_header (&(old_hdrs[j]));
1065 /* now try to recover the old flags */
1067 index_hint_set = (index_hint == NULL);
1069 if (!ctx->readonly) {
1070 for (i = 0; i < ctx->msgcount; i++) {
1073 /* some messages have been deleted, and new messages have been
1074 * appended at the end; the heuristic is that old messages have then
1075 * "advanced" towards the beginning of the folder, so we begin the
1076 * search at index "i"
1078 for (j = i; j < old_msgcount; j++) {
1079 if (old_hdrs[j] == NULL)
1081 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
1087 for (j = 0; j < i && j < old_msgcount; j++) {
1088 if (old_hdrs[j] == NULL)
1090 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
1098 /* this is best done here */
1099 if (!index_hint_set && *index_hint == j)
1102 if (old_hdrs[j]->changed) {
1103 /* Only update the flags if the old header was changed;
1104 * otherwise, the header may have been modified externally,
1105 * and we don't want to lose _those_ changes
1107 mutt_set_flag (ctx, ctx->hdrs[i], M_FLAG, old_hdrs[j]->flagged);
1108 mutt_set_flag (ctx, ctx->hdrs[i], M_REPLIED, old_hdrs[j]->replied);
1109 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, old_hdrs[j]->old);
1110 mutt_set_flag (ctx, ctx->hdrs[i], M_READ, old_hdrs[j]->read);
1112 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, old_hdrs[j]->deleted);
1113 mutt_set_flag (ctx, ctx->hdrs[i], M_TAG, old_hdrs[j]->tagged);
1115 /* we don't need this header any more */
1116 mutt_free_header (&(old_hdrs[j]));
1120 /* free the remaining old headers */
1121 for (j = 0; j < old_msgcount; j++) {
1123 mutt_free_header (&(old_hdrs[j]));
1132 return ((ctx->changed || msg_mod) ? M_REOPENED : M_NEW_MAIL);
1137 * 1 if the mailbox is not empty
1138 * 0 if the mailbox is empty
1141 int mbox_check_empty (const char *path)
1145 if (stat (path, &st) == -1)
1148 return ((st.st_size == 0));