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 */
31 #include "lib/debug.h"
42 /* struct used by mutt_sync_mailbox() to store new offsets */
52 static int mbox_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
59 static int mbox_reopen_mailbox (CONTEXT*, int*);
62 * ctx - context to lock
63 * excl - exclusive lock?
64 * retry - should retry if unable to lock?
66 int mbox_lock_mailbox (CONTEXT * ctx, int excl, int retry)
70 if ((r = mx_lock_file (ctx->path, fileno (ctx->fp), excl, 1, retry)) == 0)
72 else if (retry && !excl) {
80 void mbox_unlock_mailbox (CONTEXT * ctx)
85 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
90 static int mmdf_parse_mailbox (CONTEXT * ctx)
92 char buf[HUGE_STRING];
93 char return_path[LONG_STRING];
94 int count = 0, oldmsgcount = ctx->msgcount;
101 #ifdef NFS_ATTRIBUTE_HACK
102 struct utimbuf newtime;
105 if (stat (ctx->path, &sb) == -1) {
106 mutt_perror (ctx->path);
109 ctx->mtime = sb.st_mtime;
110 ctx->size = sb.st_size;
112 #ifdef NFS_ATTRIBUTE_HACK
113 if (sb.st_mtime > sb.st_atime) {
114 newtime.modtime = sb.st_mtime;
115 newtime.actime = time (NULL);
116 utime (ctx->path, &newtime);
120 /* precompute the local timezone to speed up calculation of the
122 tz = mutt_local_tz (0);
124 buf[sizeof (buf) - 1] = 0;
127 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
130 if (str_cmp (buf, MMDF_SEP) == 0) {
131 loc = ftell (ctx->fp);
134 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
135 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
136 (int) (loc / (ctx->size / 100 + 1)));
139 if (ctx->msgcount == ctx->hdrmax)
140 mx_alloc_memory (ctx);
141 ctx->hdrs[ctx->msgcount] = hdr = mutt_new_header ();
143 hdr->index = ctx->msgcount;
145 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL) {
146 /* TODO: memory leak??? */
147 debug_print (1, ("unexpected EOF\n"));
153 if (!is_from (buf, return_path, sizeof (return_path), &t)) {
154 if (fseek (ctx->fp, loc, SEEK_SET) != 0) {
155 debug_print (1, ("fseek() failed\n"));
156 mutt_error _("Mailbox is corrupt!");
162 hdr->received = t - tz;
164 hdr->env = mutt_read_rfc822_header (ctx->fp, hdr, 0, 0);
166 loc = ftell (ctx->fp);
168 if (hdr->content->length > 0 && hdr->lines > 0) {
169 tmploc = loc + hdr->content->length;
171 if (0 < tmploc && tmploc < ctx->size) {
172 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0 ||
173 fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL ||
174 str_cmp (MMDF_SEP, buf) != 0) {
175 if (fseek (ctx->fp, loc, SEEK_SET) != 0)
176 debug_print (1, ("fseek() failed\n"));
177 hdr->content->length = -1;
181 hdr->content->length = -1;
184 hdr->content->length = -1;
186 if (hdr->content->length < 0) {
189 loc = ftell (ctx->fp);
190 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
193 } while (str_cmp (buf, MMDF_SEP) != 0);
196 hdr->content->length = loc - hdr->content->offset;
199 if (!hdr->env->return_path && return_path[0])
200 hdr->env->return_path =
201 rfc822_parse_adrlist (hdr->env->return_path, return_path);
204 hdr->env->from = rfc822_cpy_adr (hdr->env->return_path);
209 debug_print (1, ("corrupt mailbox!\n"));
210 mutt_error _("Mailbox is corrupt!");
216 if (ctx->msgcount > oldmsgcount)
217 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
222 /* Note that this function is also called when new mail is appended to the
223 * currently open folder, and NOT just when the mailbox is initially read.
225 * NOTE: it is assumed that the mailbox being read has been locked before
226 * this routine gets called. Strange things could happen if it's not!
228 static int mbox_parse_mailbox (CONTEXT * ctx)
231 char buf[HUGE_STRING], return_path[STRING];
234 int count = 0, lines = 0;
237 #ifdef NFS_ATTRIBUTE_HACK
238 struct utimbuf newtime;
241 /* Save information about the folder at the time we opened it. */
242 if (stat (ctx->path, &sb) == -1) {
243 mutt_perror (ctx->path);
247 ctx->size = sb.st_size;
248 ctx->mtime = sb.st_mtime;
250 #ifdef NFS_ATTRIBUTE_HACK
251 if (sb.st_mtime > sb.st_atime) {
252 newtime.modtime = sb.st_mtime;
253 newtime.actime = time (NULL);
254 utime (ctx->path, &newtime);
259 ctx->readonly = access (ctx->path, W_OK) ? 1 : 0;
261 /* precompute the local timezone to speed up calculation of the
263 tz = mutt_local_tz (0);
265 loc = ftell (ctx->fp);
266 while (fgets (buf, sizeof (buf), ctx->fp) != NULL) {
267 if (is_from (buf, return_path, sizeof (return_path), &t)) {
268 /* Save the Content-Length of the previous message */
270 #define PREV ctx->hdrs[ctx->msgcount-1]
272 if (PREV->content->length < 0) {
273 PREV->content->length = loc - PREV->content->offset - 1;
274 if (PREV->content->length < 0)
275 PREV->content->length = 0;
278 PREV->lines = lines ? lines - 1 : 0;
283 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
284 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
285 (int) (ftell (ctx->fp) / (ctx->size / 100 + 1)));
287 if (ctx->msgcount == ctx->hdrmax)
288 mx_alloc_memory (ctx);
290 curhdr = ctx->hdrs[ctx->msgcount] = mutt_new_header ();
291 curhdr->received = t - tz;
292 curhdr->offset = loc;
293 curhdr->index = ctx->msgcount;
295 curhdr->env = mutt_read_rfc822_header (ctx->fp, curhdr, 0, 0);
297 /* if we know how long this message is, either just skip over the body,
298 * or if we don't know how many lines there are, count them now (this will
299 * save time by not having to search for the next message marker).
301 if (curhdr->content->length > 0) {
304 loc = ftell (ctx->fp);
305 tmploc = loc + curhdr->content->length + 1;
307 if (0 < tmploc && tmploc < ctx->size) {
309 * check to see if the content-length looks valid. we expect to
310 * to see a valid message separator at this point in the stream
312 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0 ||
313 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
314 str_ncmp ("From ", buf, 5) != 0) {
315 debug_print (1, ("bad content-length in message %d (cl=%ld)\n",
316 curhdr->index, curhdr->content->length));
317 debug_print (1, ("LINE: %s\n", buf));
318 if (fseek (ctx->fp, loc, SEEK_SET) != 0) { /* nope, return the previous position */
319 debug_print (1, ("fseek() failed\n"));
321 curhdr->content->length = -1;
324 else if (tmploc != ctx->size) {
325 /* content-length would put us past the end of the file, so it
328 curhdr->content->length = -1;
331 if (curhdr->content->length != -1) {
332 /* good content-length. check to see if we know how many lines
333 * are in this message.
335 if (curhdr->lines == 0) {
336 int cl = curhdr->content->length;
338 /* count the number of lines in this message */
339 if (fseek (ctx->fp, loc, SEEK_SET) != 0)
340 debug_print (1, ("fseek() failed\n"));
342 if (fgetc (ctx->fp) == '\n')
347 /* return to the offset of the next message separator */
348 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0)
349 debug_print (1, ("fseek() failed\n"));
355 if (!curhdr->env->return_path && return_path[0])
356 curhdr->env->return_path =
357 rfc822_parse_adrlist (curhdr->env->return_path, return_path);
359 if (!curhdr->env->from)
360 curhdr->env->from = rfc822_cpy_adr (curhdr->env->return_path);
367 loc = ftell (ctx->fp);
371 * Only set the content-length of the previous message if we have read more
372 * than one message during _this_ invocation. If this routine is called
373 * when new mail is received, we need to make sure not to clobber what
374 * previously was the last message since the headers may be sorted.
377 if (PREV->content->length < 0) {
378 PREV->content->length = ftell (ctx->fp) - PREV->content->offset - 1;
379 if (PREV->content->length < 0)
380 PREV->content->length = 0;
384 PREV->lines = lines ? lines - 1 : 0;
386 mx_update_context (ctx, count);
394 /* open a mbox or mmdf style mailbox */
395 static int mbox_open_mailbox (CONTEXT * ctx)
399 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
400 mutt_perror (ctx->path);
403 mutt_block_signals ();
404 if (mbox_lock_mailbox (ctx, 0, 1) == -1) {
405 mutt_unblock_signals ();
409 if (ctx->magic == M_MBOX)
410 rc = mbox_parse_mailbox (ctx);
411 else if (ctx->magic == M_MMDF)
412 rc = mmdf_parse_mailbox (ctx);
416 mbox_unlock_mailbox (ctx);
417 mutt_unblock_signals ();
421 /* check to see if the mailbox has changed on disk.
424 * M_REOPENED mailbox has been reopened
425 * M_NEW_MAIL new mail has arrived!
426 * M_LOCKED couldn't lock the file
430 static int _mbox_check_mailbox (CONTEXT * ctx, int *index_hint)
433 char buffer[LONG_STRING];
437 if (stat (ctx->path, &st) == 0) {
438 if (st.st_mtime == ctx->mtime && st.st_size == ctx->size)
441 if (st.st_size == ctx->size) {
442 /* the file was touched, but it is still the same length, so just exit */
443 ctx->mtime = st.st_mtime;
447 if (st.st_size > ctx->size) {
448 /* lock the file if it isn't already */
450 mutt_block_signals ();
451 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
452 mutt_unblock_signals ();
453 /* we couldn't lock the mailbox, but nothing serious happened:
454 * probably the new mail arrived: no reason to wait till we can
455 * parse it: we'll get it on the next pass
463 * Check to make sure that the only change to the mailbox is that
464 * message(s) were appended to this file. My heuristic is that we should
465 * see the message separator at *exactly* what used to be the end of the
468 if (fseek (ctx->fp, ctx->size, SEEK_SET) != 0)
469 debug_print (1, ("fseek() failed\n"));
470 if (fgets (buffer, sizeof (buffer), ctx->fp) != NULL) {
471 if ((ctx->magic == M_MBOX && str_ncmp ("From ", buffer, 5) == 0)
472 || (ctx->magic == M_MMDF && str_cmp (MMDF_SEP, buffer) == 0)) {
473 if (fseek (ctx->fp, ctx->size, SEEK_SET) != 0)
474 debug_print (1, ("fseek() failed\n"));
475 if (ctx->magic == M_MBOX)
476 mbox_parse_mailbox (ctx);
478 mmdf_parse_mailbox (ctx);
480 /* Only unlock the folder if it was locked inside of this routine.
481 * It may have been locked elsewhere, like in
482 * mutt_checkpoint_mailbox().
486 mbox_unlock_mailbox (ctx);
487 mutt_unblock_signals ();
490 return (M_NEW_MAIL); /* signal that new mail arrived */
496 debug_print (1, ("fgets returned NULL.\n"));
505 if (mbox_reopen_mailbox (ctx, index_hint) != -1) {
507 mbox_unlock_mailbox (ctx);
508 mutt_unblock_signals ();
516 mbox_unlock_mailbox (ctx);
517 mx_fastclose_mailbox (ctx);
518 mutt_unblock_signals ();
519 mutt_error _("Mailbox was corrupted!");
524 static int mbox_check_mailbox (CONTEXT* ctx, int* index_hint, int lock) {
528 mutt_block_signals ();
529 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
530 mutt_unblock_signals ();
535 rc = _mbox_check_mailbox (ctx, index_hint);
538 mutt_unblock_signals ();
539 mbox_unlock_mailbox (ctx);
548 static int _mbox_sync_mailbox (CONTEXT * ctx, int unused, int *index_hint)
550 char tempfile[_POSIX_PATH_MAX];
552 int i, j, save_sort = SORT_ORDER;
554 int need_sort = 0; /* flag to resort mailbox if new mail arrives */
555 int first = -1; /* first message to be written */
556 long offset; /* location in mailbox to write changed messages */
558 struct utimbuf utimebuf;
559 struct m_update_t *newOffset = NULL;
560 struct m_update_t *oldOffset = NULL;
563 /* sort message by their position in the mailbox on disk */
564 if (Sort != SORT_ORDER) {
567 mutt_sort_headers (ctx, 0);
572 /* need to open the file for writing in such a way that it does not truncate
573 * the file, so use read-write mode.
575 if ((ctx->fp = freopen (ctx->path, "r+", ctx->fp)) == NULL) {
576 mx_fastclose_mailbox (ctx);
577 mutt_error _("Fatal error! Could not reopen mailbox!");
582 mutt_block_signals ();
584 if (mbox_lock_mailbox (ctx, 1, 1) == -1) {
585 mutt_unblock_signals ();
586 mutt_error _("Unable to lock mailbox!");
591 /* Check to make sure that the file hasn't changed on disk */
592 if ((i = _mbox_check_mailbox (ctx, index_hint)) == M_NEW_MAIL
593 || i == M_REOPENED) {
594 /* new mail arrived, or mailbox reopened */
603 /* Create a temporary file to write the new version of the mailbox in. */
604 mutt_mktemp (tempfile);
605 if ((i = open (tempfile, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1 ||
606 (fp = fdopen (i, "w")) == NULL) {
611 mutt_error _("Could not create temporary file!");
617 /* find the first deleted/changed message. we save a lot of time by only
618 * rewriting the mailbox from the point where it has actually changed.
620 for (i = 0; i < ctx->msgcount && !ctx->hdrs[i]->deleted &&
621 !ctx->hdrs[i]->changed && !ctx->hdrs[i]->attach_del; i++);
622 if (i == ctx->msgcount) {
623 /* this means ctx->changed or ctx->deleted was set, but no
624 * messages were found to be changed or deleted. This should
625 * never happen, is we presume it is a bug in mutt.
628 _("sync: mbox modified, but no modified messages! (report this bug)");
629 mutt_sleep (5); /* the mutt_error /will/ get cleared! */
630 debug_print (1, ("no modified messages.\n"));
635 /* save the index of the first changed/deleted message */
637 /* where to start overwriting */
638 offset = ctx->hdrs[i]->offset;
640 /* the offset stored in the header does not include the MMDF_SEP, so make
641 * sure we seek to the correct location
643 if (ctx->magic == M_MMDF)
644 offset -= (sizeof MMDF_SEP - 1);
646 /* allocate space for the new offsets */
647 newOffset = mem_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
648 oldOffset = mem_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
650 for (i = first, j = 0; i < ctx->msgcount; i++) {
652 * back up some information which is needed to restore offsets when
656 oldOffset[i - first].valid = 1;
657 oldOffset[i - first].hdr = ctx->hdrs[i]->offset;
658 oldOffset[i - first].body = ctx->hdrs[i]->content->offset;
659 oldOffset[i - first].lines = ctx->hdrs[i]->lines;
660 oldOffset[i - first].length = ctx->hdrs[i]->content->length;
662 if (!ctx->hdrs[i]->deleted) {
664 if (!ctx->quiet && WriteInc && ((i % WriteInc) == 0 || j == 1))
665 mutt_message (_("Writing messages... %d (%d%%)"), i,
666 (int) (ftell (ctx->fp) / (ctx->size / 100 + 1)));
668 if (ctx->magic == M_MMDF) {
669 if (fputs (MMDF_SEP, fp) == EOF) {
670 mutt_perror (tempfile);
678 /* save the new offset for this message. we add `offset' because the
679 * temporary file only contains saved message which are located after
680 * `offset' in the real mailbox
682 newOffset[i - first].hdr = ftell (fp) + offset;
684 if (mutt_copy_message
685 (fp, ctx, ctx->hdrs[i], M_CM_UPDATE,
686 CH_FROM | CH_UPDATE | CH_UPDATE_LEN) == -1) {
687 mutt_perror (tempfile);
693 /* Since messages could have been deleted, the offsets stored in memory
694 * will be wrong, so update what we can, which is the offset of this
695 * message, and the offset of the body. If this is a multipart message,
696 * we just flush the in memory cache so that the message will be reparsed
697 * if the user accesses it later.
699 newOffset[i - first].body =
700 ftell (fp) - ctx->hdrs[i]->content->length + offset;
701 mutt_free_body (&ctx->hdrs[i]->content->parts);
703 switch (ctx->magic) {
705 if (fputs (MMDF_SEP, fp) == EOF) {
706 mutt_perror (tempfile);
713 if (fputs ("\n", fp) == EOF) {
714 mutt_perror (tempfile);
723 if (fclose (fp) != 0) {
725 debug_print (1, ("fclose() returned non-zero.\n"));
727 mutt_perror (tempfile);
733 /* Save the state of this folder. */
734 if (stat (ctx->path, &statbuf) == -1) {
735 mutt_perror (ctx->path);
741 if ((fp = fopen (tempfile, "r")) == NULL) {
742 mutt_unblock_signals ();
743 mx_fastclose_mailbox (ctx);
744 debug_print (1, ("unable to reopen temp copy of mailbox!\n"));
745 mutt_perror (tempfile);
750 if (fseek (ctx->fp, offset, SEEK_SET) != 0 || /* seek the append location */
751 /* do a sanity check to make sure the mailbox looks ok */
752 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
753 (ctx->magic == M_MBOX && str_ncmp ("From ", buf, 5) != 0) ||
754 (ctx->magic == M_MMDF && str_cmp (MMDF_SEP, buf) != 0)) {
755 debug_print (1, ("message not in expected position.\n"));
756 debug_print (1, ("LINE: %s\n", buf));
760 if (fseek (ctx->fp, offset, SEEK_SET) != 0) { /* return to proper offset */
762 debug_print (1, ("fseek() failed\n"));
765 /* copy the temp mailbox back into place starting at the first
766 * change/deleted message
768 mutt_message _("Committing changes...");
770 i = mutt_copy_stream (fp, ctx->fp);
772 if (ferror (ctx->fp))
776 ctx->size = ftell (ctx->fp); /* update the size of the mailbox */
777 ftruncate (fileno (ctx->fp), ctx->size);
783 mbox_unlock_mailbox (ctx);
785 if (fclose (ctx->fp) != 0 || i == -1) {
786 /* error occured while writing the mailbox back, so keep the temp copy
790 char savefile[_POSIX_PATH_MAX];
792 snprintf (savefile, sizeof (savefile), "%s/mutt.%s-%s-%u",
793 NONULL (Tempdir), NONULL (Username), NONULL (Hostname),
794 (unsigned int) getpid ());
795 rename (tempfile, savefile);
796 mutt_unblock_signals ();
797 mx_fastclose_mailbox (ctx);
798 mutt_pretty_mailbox (savefile);
799 mutt_error (_("Write failed! Saved partial mailbox to %s"), savefile);
804 /* Restore the previous access/modification times */
805 utimebuf.actime = statbuf.st_atime;
806 utimebuf.modtime = statbuf.st_mtime;
807 utime (ctx->path, &utimebuf);
809 /* reopen the mailbox in read-only mode */
810 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
812 mutt_unblock_signals ();
813 mx_fastclose_mailbox (ctx);
814 mutt_error _("Fatal error! Could not reopen mailbox!");
818 /* update the offsets of the rewritten messages */
819 for (i = first, j = first; i < ctx->msgcount; i++) {
820 if (!ctx->hdrs[i]->deleted) {
821 ctx->hdrs[i]->offset = newOffset[i - first].hdr;
822 ctx->hdrs[i]->content->hdr_offset = newOffset[i - first].hdr;
823 ctx->hdrs[i]->content->offset = newOffset[i - first].body;
824 ctx->hdrs[i]->index = j++;
827 mem_free (&newOffset);
828 mem_free (&oldOffset);
829 unlink (tempfile); /* remove partial copy of the mailbox */
830 mutt_unblock_signals ();
832 return (0); /* signal success */
834 bail: /* Come here in case of disaster */
838 /* restore offsets, as far as they are valid */
839 if (first >= 0 && oldOffset) {
840 for (i = first; i < ctx->msgcount && oldOffset[i - first].valid; i++) {
841 ctx->hdrs[i]->offset = oldOffset[i - first].hdr;
842 ctx->hdrs[i]->content->hdr_offset = oldOffset[i - first].hdr;
843 ctx->hdrs[i]->content->offset = oldOffset[i - first].body;
844 ctx->hdrs[i]->lines = oldOffset[i - first].lines;
845 ctx->hdrs[i]->content->length = oldOffset[i - first].length;
849 /* this is ok to call even if we haven't locked anything */
850 mbox_unlock_mailbox (ctx);
852 mutt_unblock_signals ();
853 mem_free (&newOffset);
854 mem_free (&oldOffset);
856 if ((ctx->fp = freopen (ctx->path, "r", ctx->fp)) == NULL) {
857 mutt_error _("Could not reopen mailbox!");
859 mx_fastclose_mailbox (ctx);
864 /* if the mailbox was reopened, the thread tree will be invalid so make
865 * sure to start threading from scratch. */
866 mutt_sort_headers (ctx, (need_sort == M_REOPENED));
871 static int mbox_sync_mailbox (CONTEXT * ctx, int unused, int *index_hint) {
875 int rc = _mbox_sync_mailbox (ctx, unused, index_hint);
878 if ((tmp = buffy_find_mailbox (ctx->path)) && tmp->new == 0)
879 buffy_update_mailbox (tmp);
884 /* close a mailbox opened in write-mode */
885 int mbox_close_mailbox (CONTEXT * ctx)
887 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
889 #ifdef USE_COMPRESSED
890 if (ctx->compressinfo)
891 mutt_slow_close_compressed (ctx);
894 mutt_unblock_signals ();
895 mx_fastclose_mailbox (ctx);
899 static int mbox_reopen_mailbox (CONTEXT * ctx, int *index_hint)
901 int (*cmp_headers) (const HEADER *, const HEADER *) = NULL;
909 /* silent operations */
912 mutt_message _("Reopening mailbox...");
914 /* our heuristics require the old mailbox to be unsorted */
915 if (Sort != SORT_ORDER) {
920 mutt_sort_headers (ctx, 1);
927 /* simulate a close */
929 hash_destroy (&ctx->id_hash, NULL);
931 hash_destroy (&ctx->subj_hash, NULL);
932 mutt_clear_threads (ctx);
933 mem_free (&ctx->v2r);
935 for (i = 0; i < ctx->msgcount; i++)
936 mutt_free_header (&(ctx->hdrs[i])); /* nothing to do! */
937 mem_free (&ctx->hdrs);
940 /* save the old headers */
941 old_msgcount = ctx->msgcount;
942 old_hdrs = ctx->hdrs;
946 ctx->hdrmax = 0; /* force allocation of new headers */
956 ctx->subj_hash = NULL;
958 switch (ctx->magic) {
961 if (fseek (ctx->fp, 0, SEEK_SET) != 0) {
962 debug_print (1, ("fseek() failed\n"));
966 cmp_headers = mutt_cmp_header;
967 if (ctx->magic == M_MBOX)
968 rc = mbox_parse_mailbox (ctx);
970 rc = mmdf_parse_mailbox (ctx);
980 /* free the old headers */
981 for (j = 0; j < old_msgcount; j++)
982 mutt_free_header (&(old_hdrs[j]));
983 mem_free (&old_hdrs);
989 /* now try to recover the old flags */
991 index_hint_set = (index_hint == NULL);
993 if (!ctx->readonly) {
994 for (i = 0; i < ctx->msgcount; i++) {
997 /* some messages have been deleted, and new messages have been
998 * appended at the end; the heuristic is that old messages have then
999 * "advanced" towards the beginning of the folder, so we begin the
1000 * search at index "i"
1002 for (j = i; j < old_msgcount; j++) {
1003 if (old_hdrs[j] == NULL)
1005 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
1011 for (j = 0; j < i && j < old_msgcount; j++) {
1012 if (old_hdrs[j] == NULL)
1014 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
1022 /* this is best done here */
1023 if (!index_hint_set && *index_hint == j)
1026 if (old_hdrs[j]->changed) {
1027 /* Only update the flags if the old header was changed;
1028 * otherwise, the header may have been modified externally,
1029 * and we don't want to lose _those_ changes
1031 mutt_set_flag (ctx, ctx->hdrs[i], M_FLAG, old_hdrs[j]->flagged);
1032 mutt_set_flag (ctx, ctx->hdrs[i], M_REPLIED, old_hdrs[j]->replied);
1033 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, old_hdrs[j]->old);
1034 mutt_set_flag (ctx, ctx->hdrs[i], M_READ, old_hdrs[j]->read);
1036 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, old_hdrs[j]->deleted);
1037 mutt_set_flag (ctx, ctx->hdrs[i], M_TAG, old_hdrs[j]->tagged);
1039 /* we don't need this header any more */
1040 mutt_free_header (&(old_hdrs[j]));
1044 /* free the remaining old headers */
1045 for (j = 0; j < old_msgcount; j++) {
1047 mutt_free_header (&(old_hdrs[j]));
1051 mem_free (&old_hdrs);
1056 return ((ctx->changed || msg_mod) ? M_REOPENED : M_NEW_MAIL);
1061 * 1 if the mailbox is not empty
1062 * 0 if the mailbox is empty
1065 int mbox_check_empty (const char *path)
1069 if (stat (path, &st) == -1)
1072 return ((st.st_size == 0));
1075 int mbox_is_magic (const char* path, struct stat* st) {
1078 char tmp[_POSIX_PATH_MAX];
1080 if (S_ISDIR(st->st_mode))
1083 if (st->st_size == 0) {
1084 /* hard to tell what zero-length files are, so assume the default magic */
1085 if (DefaultMagic == M_MBOX || DefaultMagic == M_MMDF)
1086 return (DefaultMagic);
1090 else if ((f = fopen (path, "r")) != NULL) {
1092 struct utimbuf times;
1094 fgets (tmp, sizeof (tmp), f);
1095 if (str_ncmp ("From ", tmp, 5) == 0)
1097 else if (str_cmp (MMDF_SEP, tmp) == 0)
1101 /* need to restore the times here, the file was not really accessed,
1102 * only the type was accessed. This is important, because detection
1103 * of "new mail" depends on those times set correctly.
1105 times.actime = st->st_atime;
1106 times.modtime = st->st_mtime;
1107 utime (path, ×);
1111 return (-1); /* fopen failed */
1114 #ifdef USE_COMPRESSED
1115 if (magic == -1 && mutt_can_read_compressed (path))
1116 return (M_COMPRESSED);
1121 static int commit_message (MESSAGE* msg, CONTEXT* ctx, int mbox) {
1122 if ((mbox && fputc ('\n', msg->fp) == EOF) ||
1123 (!mbox && fputs (MMDF_SEP, msg->fp) == EOF))
1125 if ((fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1)) {
1126 mutt_perror (_("Can't write message"));
1132 static int mbox_commit_message (MESSAGE* msg, CONTEXT* ctx) {
1133 return (commit_message (msg, ctx, 1));
1136 static int mmdf_commit_message (MESSAGE* msg, CONTEXT* ctx) {
1137 return (commit_message (msg, ctx, 0));
1140 static mx_t* reg_mx (void) {
1141 mx_t* fmt = mem_calloc (1, sizeof (mx_t));
1143 fmt->mx_check_empty = mbox_check_empty;
1144 fmt->mx_is_magic = mbox_is_magic;
1145 fmt->mx_access = access;
1146 fmt->mx_open_mailbox = mbox_open_mailbox;
1147 fmt->mx_open_new_message = mbox_open_new_message;
1148 fmt->mx_sync_mailbox = mbox_sync_mailbox;
1149 fmt->mx_check_mailbox = mbox_check_mailbox;
1153 mx_t* mbox_reg_mx (void) {
1154 mx_t* fmt = reg_mx ();
1156 fmt->mx_commit_message = mbox_commit_message;
1159 mx_t* mmdf_reg_mx (void) {
1160 mx_t* fmt = reg_mx ();
1162 fmt->mx_commit_message = mmdf_commit_message;