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 */
16 #include <lib-lib/mem.h>
17 #include <lib-lib/str.h>
18 #include <lib-lib/macros.h>
32 #include "lib/debug.h"
43 /* struct used by mutt_sync_mailbox() to store new offsets */
53 static int mbox_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
60 static int mbox_reopen_mailbox (CONTEXT*, int*);
63 * ctx - context to lock
64 * excl - exclusive lock?
65 * retry - should retry if unable to lock?
67 int mbox_lock_mailbox (CONTEXT * ctx, int excl, int retry)
71 if ((r = mx_lock_file (ctx->path, fileno (ctx->fp), excl, 1, retry)) == 0)
73 else if (retry && !excl) {
81 void mbox_unlock_mailbox (CONTEXT * ctx)
86 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
91 static int mmdf_parse_mailbox (CONTEXT * ctx)
93 char buf[HUGE_STRING];
94 char return_path[LONG_STRING];
95 int count = 0, oldmsgcount = ctx->msgcount;
102 #ifdef NFS_ATTRIBUTE_HACK
103 struct utimbuf newtime;
106 if (stat (ctx->path, &sb) == -1) {
107 mutt_perror (ctx->path);
110 ctx->mtime = sb.st_mtime;
111 ctx->size = sb.st_size;
113 #ifdef NFS_ATTRIBUTE_HACK
114 if (sb.st_mtime > sb.st_atime) {
115 newtime.modtime = sb.st_mtime;
116 newtime.actime = time (NULL);
117 utime (ctx->path, &newtime);
121 /* precompute the local timezone to speed up calculation of the
123 tz = mutt_local_tz (0);
125 buf[sizeof (buf) - 1] = 0;
128 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
131 if (str_cmp (buf, MMDF_SEP) == 0) {
132 loc = ftello (ctx->fp);
135 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
136 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
137 (int) (loc / (ctx->size / 100 + 1)));
140 if (ctx->msgcount == ctx->hdrmax)
141 mx_alloc_memory (ctx);
142 ctx->hdrs[ctx->msgcount] = hdr = mutt_new_header ();
144 hdr->index = ctx->msgcount;
146 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL) {
147 /* TODO: memory leak??? */
148 debug_print (1, ("unexpected EOF\n"));
154 if (!is_from (buf, return_path, sizeof (return_path), &t)) {
155 if (fseeko (ctx->fp, loc, SEEK_SET) != 0) {
156 debug_print (1, ("fseeko() failed\n"));
157 mutt_error _("Mailbox is corrupt!");
163 hdr->received = t - tz;
165 hdr->env = mutt_read_rfc822_header (ctx->fp, hdr, 0, 0);
167 loc = ftello (ctx->fp);
169 if (hdr->content->length > 0 && hdr->lines > 0) {
170 tmploc = loc + hdr->content->length;
172 if (0 < tmploc && tmploc < ctx->size) {
173 if (fseeko (ctx->fp, tmploc, SEEK_SET) != 0 ||
174 fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL ||
175 str_cmp (MMDF_SEP, buf) != 0) {
176 if (fseeko (ctx->fp, loc, SEEK_SET) != 0)
177 debug_print (1, ("fseeko() failed\n"));
178 hdr->content->length = -1;
182 hdr->content->length = -1;
185 hdr->content->length = -1;
187 if (hdr->content->length < 0) {
190 loc = ftello (ctx->fp);
191 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
194 } while (str_cmp (buf, MMDF_SEP) != 0);
197 hdr->content->length = loc - hdr->content->offset;
200 if (!hdr->env->return_path && return_path[0])
201 hdr->env->return_path =
202 rfc822_parse_adrlist (hdr->env->return_path, return_path);
205 hdr->env->from = rfc822_cpy_adr (hdr->env->return_path);
210 debug_print (1, ("corrupt mailbox!\n"));
211 mutt_error _("Mailbox is corrupt!");
217 if (ctx->msgcount > oldmsgcount)
218 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
223 /* Note that this function is also called when new mail is appended to the
224 * currently open folder, and NOT just when the mailbox is initially read.
226 * NOTE: it is assumed that the mailbox being read has been locked before
227 * this routine gets called. Strange things could happen if it's not!
229 static int mbox_parse_mailbox (CONTEXT * ctx)
232 char buf[HUGE_STRING], return_path[STRING];
235 int count = 0, lines = 0;
238 #ifdef NFS_ATTRIBUTE_HACK
239 struct utimbuf newtime;
242 /* Save information about the folder at the time we opened it. */
243 if (stat (ctx->path, &sb) == -1) {
244 mutt_perror (ctx->path);
248 ctx->size = sb.st_size;
249 ctx->mtime = sb.st_mtime;
251 #ifdef NFS_ATTRIBUTE_HACK
252 if (sb.st_mtime > sb.st_atime) {
253 newtime.modtime = sb.st_mtime;
254 newtime.actime = time (NULL);
255 utime (ctx->path, &newtime);
260 ctx->readonly = access (ctx->path, W_OK) ? 1 : 0;
262 /* precompute the local timezone to speed up calculation of the
264 tz = mutt_local_tz (0);
266 loc = ftello (ctx->fp);
267 while (fgets (buf, sizeof (buf), ctx->fp) != NULL) {
268 if (is_from (buf, return_path, sizeof (return_path), &t)) {
269 /* Save the Content-Length of the previous message */
271 #define PREV ctx->hdrs[ctx->msgcount-1]
273 if (PREV->content->length < 0) {
274 PREV->content->length = loc - PREV->content->offset - 1;
275 if (PREV->content->length < 0)
276 PREV->content->length = 0;
279 PREV->lines = lines ? lines - 1 : 0;
284 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
285 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
286 (int) (ftello (ctx->fp) / (ctx->size / 100 + 1)));
288 if (ctx->msgcount == ctx->hdrmax)
289 mx_alloc_memory (ctx);
291 curhdr = ctx->hdrs[ctx->msgcount] = mutt_new_header ();
292 curhdr->received = t - tz;
293 curhdr->offset = loc;
294 curhdr->index = ctx->msgcount;
296 curhdr->env = mutt_read_rfc822_header (ctx->fp, curhdr, 0, 0);
298 /* if we know how long this message is, either just skip over the body,
299 * or if we don't know how many lines there are, count them now (this will
300 * save time by not having to search for the next message marker).
302 if (curhdr->content->length > 0) {
305 loc = ftello (ctx->fp);
306 tmploc = loc + curhdr->content->length + 1;
308 if (0 < tmploc && tmploc < ctx->size) {
310 * check to see if the content-length looks valid. we expect to
311 * to see a valid message separator at this point in the stream
313 if (fseeko (ctx->fp, tmploc, SEEK_SET) != 0 ||
314 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
315 str_ncmp ("From ", buf, 5) != 0) {
316 debug_print (1, ("bad content-length in message %d (cl=%zd)\n",
317 curhdr->index, curhdr->content->length));
318 debug_print (1, ("LINE: %s\n", buf));
319 if (fseeko (ctx->fp, loc, SEEK_SET) != 0) { /* nope, return the previous position */
320 debug_print (1, ("fseeko() failed\n"));
322 curhdr->content->length = -1;
325 else if (tmploc != ctx->size) {
326 /* content-length would put us past the end of the file, so it
329 curhdr->content->length = -1;
332 if (curhdr->content->length != -1) {
333 /* good content-length. check to see if we know how many lines
334 * are in this message.
336 if (curhdr->lines == 0) {
337 int cl = curhdr->content->length;
339 /* count the number of lines in this message */
340 if (fseeko (ctx->fp, loc, SEEK_SET) != 0)
341 debug_print (1, ("fseeko() failed\n"));
343 if (fgetc (ctx->fp) == '\n')
348 /* return to the offset of the next message separator */
349 if (fseeko (ctx->fp, tmploc, SEEK_SET) != 0)
350 debug_print (1, ("fseeko() failed\n"));
356 if (!curhdr->env->return_path && return_path[0])
357 curhdr->env->return_path =
358 rfc822_parse_adrlist (curhdr->env->return_path, return_path);
360 if (!curhdr->env->from)
361 curhdr->env->from = rfc822_cpy_adr (curhdr->env->return_path);
368 loc = ftello (ctx->fp);
372 * Only set the content-length of the previous message if we have read more
373 * than one message during _this_ invocation. If this routine is called
374 * when new mail is received, we need to make sure not to clobber what
375 * previously was the last message since the headers may be sorted.
378 if (PREV->content->length < 0) {
379 PREV->content->length = ftello (ctx->fp) - PREV->content->offset - 1;
380 if (PREV->content->length < 0)
381 PREV->content->length = 0;
385 PREV->lines = lines ? lines - 1 : 0;
387 mx_update_context (ctx, count);
395 /* open a mbox or mmdf style mailbox */
396 static int mbox_open_mailbox (CONTEXT * ctx)
400 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
401 mutt_perror (ctx->path);
404 mutt_block_signals ();
405 if (mbox_lock_mailbox (ctx, 0, 1) == -1) {
406 mutt_unblock_signals ();
410 if (ctx->magic == M_MBOX)
411 rc = mbox_parse_mailbox (ctx);
412 else if (ctx->magic == M_MMDF)
413 rc = mmdf_parse_mailbox (ctx);
417 mbox_unlock_mailbox (ctx);
418 mutt_unblock_signals ();
422 /* check to see if the mailbox has changed on disk.
425 * M_REOPENED mailbox has been reopened
426 * M_NEW_MAIL new mail has arrived!
427 * M_LOCKED couldn't lock the file
431 static int _mbox_check_mailbox (CONTEXT * ctx, int *index_hint)
434 char buffer[LONG_STRING];
438 if (stat (ctx->path, &st) == 0) {
439 if (st.st_mtime == ctx->mtime && st.st_size == ctx->size)
442 if (st.st_size == ctx->size) {
443 /* the file was touched, but it is still the same length, so just exit */
444 ctx->mtime = st.st_mtime;
448 if (st.st_size > ctx->size) {
449 /* lock the file if it isn't already */
451 mutt_block_signals ();
452 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
453 mutt_unblock_signals ();
454 /* we couldn't lock the mailbox, but nothing serious happened:
455 * probably the new mail arrived: no reason to wait till we can
456 * parse it: we'll get it on the next pass
464 * Check to make sure that the only change to the mailbox is that
465 * message(s) were appended to this file. My heuristic is that we should
466 * see the message separator at *exactly* what used to be the end of the
469 if (fseeko (ctx->fp, ctx->size, SEEK_SET) != 0)
470 debug_print (1, ("fseeko() failed\n"));
471 if (fgets (buffer, sizeof (buffer), ctx->fp) != NULL) {
472 if ((ctx->magic == M_MBOX && str_ncmp ("From ", buffer, 5) == 0)
473 || (ctx->magic == M_MMDF && str_cmp (MMDF_SEP, buffer) == 0)) {
474 if (fseeko (ctx->fp, ctx->size, SEEK_SET) != 0)
475 debug_print (1, ("fseeko() failed\n"));
476 if (ctx->magic == M_MBOX)
477 mbox_parse_mailbox (ctx);
479 mmdf_parse_mailbox (ctx);
481 /* Only unlock the folder if it was locked inside of this routine.
482 * It may have been locked elsewhere, like in
483 * mutt_checkpoint_mailbox().
487 mbox_unlock_mailbox (ctx);
488 mutt_unblock_signals ();
491 return (M_NEW_MAIL); /* signal that new mail arrived */
497 debug_print (1, ("fgets returned NULL.\n"));
506 if (mbox_reopen_mailbox (ctx, index_hint) != -1) {
508 mbox_unlock_mailbox (ctx);
509 mutt_unblock_signals ();
517 mbox_unlock_mailbox (ctx);
518 mx_fastclose_mailbox (ctx);
519 mutt_unblock_signals ();
520 mutt_error _("Mailbox was corrupted!");
525 static int mbox_check_mailbox (CONTEXT* ctx, int* index_hint, int lock) {
529 mutt_block_signals ();
530 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
531 mutt_unblock_signals ();
536 rc = _mbox_check_mailbox (ctx, index_hint);
539 mutt_unblock_signals ();
540 mbox_unlock_mailbox (ctx);
549 static int _mbox_sync_mailbox (CONTEXT * ctx, int unused, int *index_hint)
551 char tempfile[_POSIX_PATH_MAX];
553 int i, j, save_sort = SORT_ORDER;
555 int need_sort = 0; /* flag to resort mailbox if new mail arrives */
556 int first = -1; /* first message to be written */
557 off_t offset; /* location in mailbox to write changed messages */
559 struct utimbuf utimebuf;
560 struct m_update_t *newOffset = NULL;
561 struct m_update_t *oldOffset = NULL;
564 /* sort message by their position in the mailbox on disk */
565 if (Sort != SORT_ORDER) {
568 mutt_sort_headers (ctx, 0);
573 /* need to open the file for writing in such a way that it does not truncate
574 * the file, so use read-write mode.
576 if ((ctx->fp = freopen (ctx->path, "r+", ctx->fp)) == NULL) {
577 mx_fastclose_mailbox (ctx);
578 mutt_error _("Fatal error! Could not reopen mailbox!");
583 mutt_block_signals ();
585 if (mbox_lock_mailbox (ctx, 1, 1) == -1) {
586 mutt_unblock_signals ();
587 mutt_error _("Unable to lock mailbox!");
592 /* Check to make sure that the file hasn't changed on disk */
593 if ((i = _mbox_check_mailbox (ctx, index_hint)) == M_NEW_MAIL
594 || i == M_REOPENED) {
595 /* new mail arrived, or mailbox reopened */
604 /* Create a temporary file to write the new version of the mailbox in. */
605 mutt_mktemp (tempfile);
606 if ((i = open (tempfile, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1 ||
607 (fp = fdopen (i, "w")) == NULL) {
612 mutt_error _("Could not create temporary file!");
618 /* find the first deleted/changed message. we save a lot of time by only
619 * rewriting the mailbox from the point where it has actually changed.
621 for (i = 0; i < ctx->msgcount && !ctx->hdrs[i]->deleted &&
622 !ctx->hdrs[i]->changed && !ctx->hdrs[i]->attach_del; i++);
623 if (i == ctx->msgcount) {
624 /* this means ctx->changed or ctx->deleted was set, but no
625 * messages were found to be changed or deleted. This should
626 * never happen, is we presume it is a bug in mutt.
629 _("sync: mbox modified, but no modified messages! (report this bug)");
630 mutt_sleep (5); /* the mutt_error /will/ get cleared! */
631 debug_print (1, ("no modified messages.\n"));
636 /* save the index of the first changed/deleted message */
638 /* where to start overwriting */
639 offset = ctx->hdrs[i]->offset;
641 /* the offset stored in the header does not include the MMDF_SEP, so make
642 * sure we seek to the correct location
644 if (ctx->magic == M_MMDF)
645 offset -= (sizeof MMDF_SEP - 1);
647 /* allocate space for the new offsets */
648 newOffset = p_new(struct m_update_t, ctx->msgcount - first);
649 oldOffset = p_new(struct m_update_t, ctx->msgcount - first);
651 for (i = first, j = 0; i < ctx->msgcount; i++) {
653 * back up some information which is needed to restore offsets when
657 oldOffset[i - first].valid = 1;
658 oldOffset[i - first].hdr = ctx->hdrs[i]->offset;
659 oldOffset[i - first].body = ctx->hdrs[i]->content->offset;
660 oldOffset[i - first].lines = ctx->hdrs[i]->lines;
661 oldOffset[i - first].length = ctx->hdrs[i]->content->length;
663 if (!ctx->hdrs[i]->deleted) {
665 if (!ctx->quiet && WriteInc && ((i % WriteInc) == 0 || j == 1))
666 mutt_message (_("Writing messages... %d (%d%%)"), i,
667 (int) (ftello (ctx->fp) / (ctx->size / 100 + 1)));
669 if (ctx->magic == M_MMDF) {
670 if (fputs (MMDF_SEP, fp) == EOF) {
671 mutt_perror (tempfile);
679 /* save the new offset for this message. we add `offset' because the
680 * temporary file only contains saved message which are located after
681 * `offset' in the real mailbox
683 newOffset[i - first].hdr = ftello (fp) + offset;
685 if (mutt_copy_message
686 (fp, ctx, ctx->hdrs[i], M_CM_UPDATE,
687 CH_FROM | CH_UPDATE | CH_UPDATE_LEN) == -1) {
688 mutt_perror (tempfile);
694 /* Since messages could have been deleted, the offsets stored in memory
695 * will be wrong, so update what we can, which is the offset of this
696 * message, and the offset of the body. If this is a multipart message,
697 * we just flush the in memory cache so that the message will be reparsed
698 * if the user accesses it later.
700 newOffset[i - first].body =
701 ftello (fp) - ctx->hdrs[i]->content->length + offset;
702 mutt_free_body (&ctx->hdrs[i]->content->parts);
704 switch (ctx->magic) {
706 if (fputs (MMDF_SEP, fp) == EOF) {
707 mutt_perror (tempfile);
714 if (fputs ("\n", fp) == EOF) {
715 mutt_perror (tempfile);
724 if (fclose (fp) != 0) {
726 debug_print (1, ("fclose() returned non-zero.\n"));
728 mutt_perror (tempfile);
734 /* Save the state of this folder. */
735 if (stat (ctx->path, &statbuf) == -1) {
736 mutt_perror (ctx->path);
742 if ((fp = fopen (tempfile, "r")) == NULL) {
743 mutt_unblock_signals ();
744 mx_fastclose_mailbox (ctx);
745 debug_print (1, ("unable to reopen temp copy of mailbox!\n"));
746 mutt_perror (tempfile);
751 if (fseeko (ctx->fp, offset, SEEK_SET) != 0 || /* seek the append location */
752 /* do a sanity check to make sure the mailbox looks ok */
753 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
754 (ctx->magic == M_MBOX && str_ncmp ("From ", buf, 5) != 0) ||
755 (ctx->magic == M_MMDF && str_cmp (MMDF_SEP, buf) != 0)) {
756 debug_print (1, ("message not in expected position.\n"));
757 debug_print (1, ("LINE: %s\n", buf));
761 if (fseeko (ctx->fp, offset, SEEK_SET) != 0) { /* return to proper offset */
763 debug_print (1, ("fseeko() failed\n"));
766 /* copy the temp mailbox back into place starting at the first
767 * change/deleted message
769 mutt_message _("Committing changes...");
771 i = mutt_copy_stream (fp, ctx->fp);
773 if (ferror (ctx->fp))
777 ctx->size = ftello (ctx->fp); /* update the size of the mailbox */
778 ftruncate (fileno (ctx->fp), ctx->size);
784 mbox_unlock_mailbox (ctx);
786 if (fclose (ctx->fp) != 0 || i == -1) {
787 /* error occured while writing the mailbox back, so keep the temp copy
791 char savefile[_POSIX_PATH_MAX];
793 snprintf (savefile, sizeof (savefile), "%s/mutt.%s-%s-%u",
794 NONULL (Tempdir), NONULL (Username), NONULL (Hostname),
795 (unsigned int) getpid ());
796 rename (tempfile, savefile);
797 mutt_unblock_signals ();
798 mx_fastclose_mailbox (ctx);
799 mutt_pretty_mailbox (savefile);
800 mutt_error (_("Write failed! Saved partial mailbox to %s"), savefile);
805 /* Restore the previous access/modification times */
806 utimebuf.actime = statbuf.st_atime;
807 utimebuf.modtime = statbuf.st_mtime;
808 utime (ctx->path, &utimebuf);
810 /* reopen the mailbox in read-only mode */
811 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
813 mutt_unblock_signals ();
814 mx_fastclose_mailbox (ctx);
815 mutt_error _("Fatal error! Could not reopen mailbox!");
819 /* update the offsets of the rewritten messages */
820 for (i = first, j = first; i < ctx->msgcount; i++) {
821 if (!ctx->hdrs[i]->deleted) {
822 ctx->hdrs[i]->offset = newOffset[i - first].hdr;
823 ctx->hdrs[i]->content->hdr_offset = newOffset[i - first].hdr;
824 ctx->hdrs[i]->content->offset = newOffset[i - first].body;
825 ctx->hdrs[i]->index = j++;
828 p_delete(&newOffset);
829 p_delete(&oldOffset);
830 unlink (tempfile); /* remove partial copy of the mailbox */
831 mutt_unblock_signals ();
833 return (0); /* signal success */
835 bail: /* Come here in case of disaster */
839 /* restore offsets, as far as they are valid */
840 if (first >= 0 && oldOffset) {
841 for (i = first; i < ctx->msgcount && oldOffset[i - first].valid; i++) {
842 ctx->hdrs[i]->offset = oldOffset[i - first].hdr;
843 ctx->hdrs[i]->content->hdr_offset = oldOffset[i - first].hdr;
844 ctx->hdrs[i]->content->offset = oldOffset[i - first].body;
845 ctx->hdrs[i]->lines = oldOffset[i - first].lines;
846 ctx->hdrs[i]->content->length = oldOffset[i - first].length;
850 /* this is ok to call even if we haven't locked anything */
851 mbox_unlock_mailbox (ctx);
853 mutt_unblock_signals ();
854 p_delete(&newOffset);
855 p_delete(&oldOffset);
857 if ((ctx->fp = freopen (ctx->path, "r", ctx->fp)) == NULL) {
858 mutt_error _("Could not reopen mailbox!");
860 mx_fastclose_mailbox (ctx);
865 /* if the mailbox was reopened, the thread tree will be invalid so make
866 * sure to start threading from scratch. */
867 mutt_sort_headers (ctx, (need_sort == M_REOPENED));
872 static int mbox_sync_mailbox (CONTEXT * ctx, int unused, int *index_hint) {
876 int rc = _mbox_sync_mailbox (ctx, unused, index_hint);
879 if ((tmp = buffy_find_mailbox (ctx->path)) && tmp->new == 0)
880 buffy_update_mailbox (tmp);
885 /* close a mailbox opened in write-mode */
886 int mbox_close_mailbox (CONTEXT * ctx)
888 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
890 #ifdef USE_COMPRESSED
891 if (ctx->compressinfo)
892 mutt_slow_close_compressed (ctx);
895 mutt_unblock_signals ();
896 mx_fastclose_mailbox (ctx);
900 static int mbox_reopen_mailbox (CONTEXT * ctx, int *index_hint)
902 int (*cmp_headers) (const HEADER *, const HEADER *) = NULL;
910 /* silent operations */
913 mutt_message _("Reopening mailbox...");
915 /* our heuristics require the old mailbox to be unsorted */
916 if (Sort != SORT_ORDER) {
921 mutt_sort_headers (ctx, 1);
928 /* simulate a close */
930 hash_destroy (&ctx->id_hash, NULL);
932 hash_destroy (&ctx->subj_hash, NULL);
933 mutt_clear_threads (ctx);
936 for (i = 0; i < ctx->msgcount; i++)
937 mutt_free_header (&(ctx->hdrs[i])); /* nothing to do! */
938 p_delete(&ctx->hdrs);
941 /* save the old headers */
942 old_msgcount = ctx->msgcount;
943 old_hdrs = ctx->hdrs;
947 ctx->hdrmax = 0; /* force allocation of new headers */
957 ctx->subj_hash = NULL;
959 switch (ctx->magic) {
962 if (fseeko (ctx->fp, 0, SEEK_SET) != 0) {
963 debug_print (1, ("fseeko() failed\n"));
967 cmp_headers = mutt_cmp_header;
968 if (ctx->magic == M_MBOX)
969 rc = mbox_parse_mailbox (ctx);
971 rc = mmdf_parse_mailbox (ctx);
981 /* free the old headers */
982 for (j = 0; j < old_msgcount; j++)
983 mutt_free_header (&(old_hdrs[j]));
990 /* now try to recover the old flags */
992 index_hint_set = (index_hint == NULL);
994 if (!ctx->readonly) {
995 for (i = 0; i < ctx->msgcount; i++) {
998 /* some messages have been deleted, and new messages have been
999 * appended at the end; the heuristic is that old messages have then
1000 * "advanced" towards the beginning of the folder, so we begin the
1001 * search at index "i"
1003 for (j = i; j < old_msgcount; j++) {
1004 if (old_hdrs[j] == NULL)
1006 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
1012 for (j = 0; j < i && j < old_msgcount; j++) {
1013 if (old_hdrs[j] == NULL)
1015 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
1023 /* this is best done here */
1024 if (!index_hint_set && *index_hint == j)
1027 if (old_hdrs[j]->changed) {
1028 /* Only update the flags if the old header was changed;
1029 * otherwise, the header may have been modified externally,
1030 * and we don't want to lose _those_ changes
1032 mutt_set_flag (ctx, ctx->hdrs[i], M_FLAG, old_hdrs[j]->flagged);
1033 mutt_set_flag (ctx, ctx->hdrs[i], M_REPLIED, old_hdrs[j]->replied);
1034 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, old_hdrs[j]->old);
1035 mutt_set_flag (ctx, ctx->hdrs[i], M_READ, old_hdrs[j]->read);
1037 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, old_hdrs[j]->deleted);
1038 mutt_set_flag (ctx, ctx->hdrs[i], M_TAG, old_hdrs[j]->tagged);
1040 /* we don't need this header any more */
1041 mutt_free_header (&(old_hdrs[j]));
1045 /* free the remaining old headers */
1046 for (j = 0; j < old_msgcount; j++) {
1048 mutt_free_header (&(old_hdrs[j]));
1052 p_delete(&old_hdrs);
1057 return ((ctx->changed || msg_mod) ? M_REOPENED : M_NEW_MAIL);
1062 * 1 if the mailbox is not empty
1063 * 0 if the mailbox is empty
1066 int mbox_check_empty (const char *path)
1070 if (stat (path, &st) == -1)
1073 return ((st.st_size == 0));
1076 int mbox_is_magic (const char* path, struct stat* st) {
1079 char tmp[_POSIX_PATH_MAX];
1081 if (S_ISDIR(st->st_mode))
1084 if (st->st_size == 0) {
1085 /* hard to tell what zero-length files are, so assume the default magic */
1086 if (DefaultMagic == M_MBOX || DefaultMagic == M_MMDF)
1087 return (DefaultMagic);
1091 else if ((f = fopen (path, "r")) != NULL) {
1093 struct utimbuf times;
1095 fgets (tmp, sizeof (tmp), f);
1096 if (str_ncmp ("From ", tmp, 5) == 0)
1098 else if (str_cmp (MMDF_SEP, tmp) == 0)
1102 /* need to restore the times here, the file was not really accessed,
1103 * only the type was accessed. This is important, because detection
1104 * of "new mail" depends on those times set correctly.
1106 times.actime = st->st_atime;
1107 times.modtime = st->st_mtime;
1108 utime (path, ×);
1112 return (-1); /* fopen failed */
1115 #ifdef USE_COMPRESSED
1116 if (magic == -1 && mutt_can_read_compressed (path))
1117 return (M_COMPRESSED);
1122 static int commit_message (MESSAGE* msg, CONTEXT* ctx, int mbox) {
1123 if ((mbox && fputc ('\n', msg->fp) == EOF) ||
1124 (!mbox && fputs (MMDF_SEP, msg->fp) == EOF))
1126 if ((fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1)) {
1127 mutt_perror (_("Can't write message"));
1133 static int mbox_commit_message (MESSAGE* msg, CONTEXT* ctx) {
1134 return (commit_message (msg, ctx, 1));
1137 static int mmdf_commit_message (MESSAGE* msg, CONTEXT* ctx) {
1138 return (commit_message (msg, ctx, 0));
1141 static mx_t* reg_mx (void) {
1142 mx_t* fmt = p_new(mx_t, 1);
1144 fmt->mx_check_empty = mbox_check_empty;
1145 fmt->mx_is_magic = mbox_is_magic;
1146 fmt->mx_access = access;
1147 fmt->mx_open_mailbox = mbox_open_mailbox;
1148 fmt->mx_open_new_message = mbox_open_new_message;
1149 fmt->mx_sync_mailbox = mbox_sync_mailbox;
1150 fmt->mx_check_mailbox = mbox_check_mailbox;
1154 mx_t* mbox_reg_mx (void) {
1155 mx_t* fmt = reg_mx ();
1157 fmt->mx_commit_message = mbox_commit_message;
1160 mx_t* mmdf_reg_mx (void) {
1161 mx_t* fmt = reg_mx ();
1163 fmt->mx_commit_message = mmdf_commit_message;