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 */
30 #include "lib/debug.h"
41 /* struct used by mutt_sync_mailbox() to store new offsets */
51 static int mbox_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
58 static int mbox_reopen_mailbox (CONTEXT*, int*);
61 * ctx - context to lock
62 * excl - exclusive lock?
63 * retry - should retry if unable to lock?
65 int mbox_lock_mailbox (CONTEXT * ctx, int excl, int retry)
69 if ((r = mx_lock_file (ctx->path, fileno (ctx->fp), excl, 1, retry)) == 0)
71 else if (retry && !excl) {
79 void mbox_unlock_mailbox (CONTEXT * ctx)
84 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
89 static int mmdf_parse_mailbox (CONTEXT * ctx)
91 char buf[HUGE_STRING];
92 char return_path[LONG_STRING];
93 int count = 0, oldmsgcount = ctx->msgcount;
100 #ifdef NFS_ATTRIBUTE_HACK
101 struct utimbuf newtime;
104 if (stat (ctx->path, &sb) == -1) {
105 mutt_perror (ctx->path);
108 ctx->mtime = sb.st_mtime;
109 ctx->size = sb.st_size;
111 #ifdef NFS_ATTRIBUTE_HACK
112 if (sb.st_mtime > sb.st_atime) {
113 newtime.modtime = sb.st_mtime;
114 newtime.actime = time (NULL);
115 utime (ctx->path, &newtime);
119 /* precompute the local timezone to speed up calculation of the
121 tz = mutt_local_tz (0);
123 buf[sizeof (buf) - 1] = 0;
126 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
129 if (mutt_strcmp (buf, MMDF_SEP) == 0) {
130 loc = ftell (ctx->fp);
133 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
134 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
135 (int) (loc / (ctx->size / 100 + 1)));
138 if (ctx->msgcount == ctx->hdrmax)
139 mx_alloc_memory (ctx);
140 ctx->hdrs[ctx->msgcount] = hdr = mutt_new_header ();
142 hdr->index = ctx->msgcount;
144 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL) {
145 /* TODO: memory leak??? */
146 debug_print (1, ("unexpected EOF\n"));
152 if (!is_from (buf, return_path, sizeof (return_path), &t)) {
153 if (fseek (ctx->fp, loc, SEEK_SET) != 0) {
154 debug_print (1, ("fseek() failed\n"));
155 mutt_error _("Mailbox is corrupt!");
161 hdr->received = t - tz;
163 hdr->env = mutt_read_rfc822_header (ctx->fp, hdr, 0, 0);
165 loc = ftell (ctx->fp);
167 if (hdr->content->length > 0 && hdr->lines > 0) {
168 tmploc = loc + hdr->content->length;
170 if (0 < tmploc && tmploc < ctx->size) {
171 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0 ||
172 fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL ||
173 mutt_strcmp (MMDF_SEP, buf) != 0) {
174 if (fseek (ctx->fp, loc, SEEK_SET) != 0)
175 debug_print (1, ("fseek() failed\n"));
176 hdr->content->length = -1;
180 hdr->content->length = -1;
183 hdr->content->length = -1;
185 if (hdr->content->length < 0) {
188 loc = ftell (ctx->fp);
189 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
192 } while (mutt_strcmp (buf, MMDF_SEP) != 0);
195 hdr->content->length = loc - hdr->content->offset;
198 if (!hdr->env->return_path && return_path[0])
199 hdr->env->return_path =
200 rfc822_parse_adrlist (hdr->env->return_path, return_path);
203 hdr->env->from = rfc822_cpy_adr (hdr->env->return_path);
208 debug_print (1, ("corrupt mailbox!\n"));
209 mutt_error _("Mailbox is corrupt!");
215 if (ctx->msgcount > oldmsgcount)
216 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
221 /* Note that this function is also called when new mail is appended to the
222 * currently open folder, and NOT just when the mailbox is initially read.
224 * NOTE: it is assumed that the mailbox being read has been locked before
225 * this routine gets called. Strange things could happen if it's not!
227 static int mbox_parse_mailbox (CONTEXT * ctx)
230 char buf[HUGE_STRING], return_path[STRING];
233 int count = 0, lines = 0;
236 #ifdef NFS_ATTRIBUTE_HACK
237 struct utimbuf newtime;
240 /* Save information about the folder at the time we opened it. */
241 if (stat (ctx->path, &sb) == -1) {
242 mutt_perror (ctx->path);
246 ctx->size = sb.st_size;
247 ctx->mtime = sb.st_mtime;
249 #ifdef NFS_ATTRIBUTE_HACK
250 if (sb.st_mtime > sb.st_atime) {
251 newtime.modtime = sb.st_mtime;
252 newtime.actime = time (NULL);
253 utime (ctx->path, &newtime);
258 ctx->readonly = access (ctx->path, W_OK) ? 1 : 0;
260 /* precompute the local timezone to speed up calculation of the
262 tz = mutt_local_tz (0);
264 loc = ftell (ctx->fp);
265 while (fgets (buf, sizeof (buf), ctx->fp) != NULL) {
266 if (is_from (buf, return_path, sizeof (return_path), &t)) {
267 /* Save the Content-Length of the previous message */
269 #define PREV ctx->hdrs[ctx->msgcount-1]
271 if (PREV->content->length < 0) {
272 PREV->content->length = loc - PREV->content->offset - 1;
273 if (PREV->content->length < 0)
274 PREV->content->length = 0;
277 PREV->lines = lines ? lines - 1 : 0;
282 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
283 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
284 (int) (ftell (ctx->fp) / (ctx->size / 100 + 1)));
286 if (ctx->msgcount == ctx->hdrmax)
287 mx_alloc_memory (ctx);
289 curhdr = ctx->hdrs[ctx->msgcount] = mutt_new_header ();
290 curhdr->received = t - tz;
291 curhdr->offset = loc;
292 curhdr->index = ctx->msgcount;
294 curhdr->env = mutt_read_rfc822_header (ctx->fp, curhdr, 0, 0);
296 /* if we know how long this message is, either just skip over the body,
297 * or if we don't know how many lines there are, count them now (this will
298 * save time by not having to search for the next message marker).
300 if (curhdr->content->length > 0) {
303 loc = ftell (ctx->fp);
304 tmploc = loc + curhdr->content->length + 1;
306 if (0 < tmploc && tmploc < ctx->size) {
308 * check to see if the content-length looks valid. we expect to
309 * to see a valid message separator at this point in the stream
311 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0 ||
312 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
313 safe_strncmp ("From ", buf, 5) != 0) {
314 debug_print (1, ("bad content-length in message %d (cl=%ld)\n",
315 curhdr->index, curhdr->content->length));
316 debug_print (1, ("LINE: %s\n", buf));
317 if (fseek (ctx->fp, loc, SEEK_SET) != 0) { /* nope, return the previous position */
318 debug_print (1, ("fseek() failed\n"));
320 curhdr->content->length = -1;
323 else if (tmploc != ctx->size) {
324 /* content-length would put us past the end of the file, so it
327 curhdr->content->length = -1;
330 if (curhdr->content->length != -1) {
331 /* good content-length. check to see if we know how many lines
332 * are in this message.
334 if (curhdr->lines == 0) {
335 int cl = curhdr->content->length;
337 /* count the number of lines in this message */
338 if (fseek (ctx->fp, loc, SEEK_SET) != 0)
339 debug_print (1, ("fseek() failed\n"));
341 if (fgetc (ctx->fp) == '\n')
346 /* return to the offset of the next message separator */
347 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0)
348 debug_print (1, ("fseek() failed\n"));
354 if (!curhdr->env->return_path && return_path[0])
355 curhdr->env->return_path =
356 rfc822_parse_adrlist (curhdr->env->return_path, return_path);
358 if (!curhdr->env->from)
359 curhdr->env->from = rfc822_cpy_adr (curhdr->env->return_path);
366 loc = ftell (ctx->fp);
370 * Only set the content-length of the previous message if we have read more
371 * than one message during _this_ invocation. If this routine is called
372 * when new mail is received, we need to make sure not to clobber what
373 * previously was the last message since the headers may be sorted.
376 if (PREV->content->length < 0) {
377 PREV->content->length = ftell (ctx->fp) - PREV->content->offset - 1;
378 if (PREV->content->length < 0)
379 PREV->content->length = 0;
383 PREV->lines = lines ? lines - 1 : 0;
385 mx_update_context (ctx, count);
393 /* open a mbox or mmdf style mailbox */
394 static int mbox_open_mailbox (CONTEXT * ctx)
398 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
399 mutt_perror (ctx->path);
402 mutt_block_signals ();
403 if (mbox_lock_mailbox (ctx, 0, 1) == -1) {
404 mutt_unblock_signals ();
408 if (ctx->magic == M_MBOX)
409 rc = mbox_parse_mailbox (ctx);
410 else if (ctx->magic == M_MMDF)
411 rc = mmdf_parse_mailbox (ctx);
415 mbox_unlock_mailbox (ctx);
416 mutt_unblock_signals ();
420 /* check to see if the mailbox has changed on disk.
423 * M_REOPENED mailbox has been reopened
424 * M_NEW_MAIL new mail has arrived!
425 * M_LOCKED couldn't lock the file
429 static int _mbox_check_mailbox (CONTEXT * ctx, int *index_hint)
432 char buffer[LONG_STRING];
436 if (stat (ctx->path, &st) == 0) {
437 if (st.st_mtime == ctx->mtime && st.st_size == ctx->size)
440 if (st.st_size == ctx->size) {
441 /* the file was touched, but it is still the same length, so just exit */
442 ctx->mtime = st.st_mtime;
446 if (st.st_size > ctx->size) {
447 /* lock the file if it isn't already */
449 mutt_block_signals ();
450 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
451 mutt_unblock_signals ();
452 /* we couldn't lock the mailbox, but nothing serious happened:
453 * probably the new mail arrived: no reason to wait till we can
454 * parse it: we'll get it on the next pass
462 * Check to make sure that the only change to the mailbox is that
463 * message(s) were appended to this file. My heuristic is that we should
464 * see the message separator at *exactly* what used to be the end of the
467 if (fseek (ctx->fp, ctx->size, SEEK_SET) != 0)
468 debug_print (1, ("fseek() failed\n"));
469 if (fgets (buffer, sizeof (buffer), ctx->fp) != NULL) {
470 if ((ctx->magic == M_MBOX && safe_strncmp ("From ", buffer, 5) == 0)
471 || (ctx->magic == M_MMDF && mutt_strcmp (MMDF_SEP, buffer) == 0)) {
472 if (fseek (ctx->fp, ctx->size, SEEK_SET) != 0)
473 debug_print (1, ("fseek() failed\n"));
474 if (ctx->magic == M_MBOX)
475 mbox_parse_mailbox (ctx);
477 mmdf_parse_mailbox (ctx);
479 /* Only unlock the folder if it was locked inside of this routine.
480 * It may have been locked elsewhere, like in
481 * mutt_checkpoint_mailbox().
485 mbox_unlock_mailbox (ctx);
486 mutt_unblock_signals ();
489 return (M_NEW_MAIL); /* signal that new mail arrived */
495 debug_print (1, ("fgets returned NULL.\n"));
504 if (mbox_reopen_mailbox (ctx, index_hint) != -1) {
506 mbox_unlock_mailbox (ctx);
507 mutt_unblock_signals ();
515 mbox_unlock_mailbox (ctx);
516 mx_fastclose_mailbox (ctx);
517 mutt_unblock_signals ();
518 mutt_error _("Mailbox was corrupted!");
523 static int mbox_check_mailbox (CONTEXT* ctx, int* index_hint, int lock) {
527 mutt_block_signals ();
528 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
529 mutt_unblock_signals ();
534 rc = _mbox_check_mailbox (ctx, index_hint);
537 mutt_unblock_signals ();
538 mbox_unlock_mailbox (ctx);
547 static int _mbox_sync_mailbox (CONTEXT * ctx, int unused, int *index_hint)
549 char tempfile[_POSIX_PATH_MAX];
551 int i, j, save_sort = SORT_ORDER;
553 int need_sort = 0; /* flag to resort mailbox if new mail arrives */
554 int first = -1; /* first message to be written */
555 long offset; /* location in mailbox to write changed messages */
557 struct utimbuf utimebuf;
558 struct m_update_t *newOffset = NULL;
559 struct m_update_t *oldOffset = NULL;
562 /* sort message by their position in the mailbox on disk */
563 if (Sort != SORT_ORDER) {
566 mutt_sort_headers (ctx, 0);
571 /* need to open the file for writing in such a way that it does not truncate
572 * the file, so use read-write mode.
574 if ((ctx->fp = freopen (ctx->path, "r+", ctx->fp)) == NULL) {
575 mx_fastclose_mailbox (ctx);
576 mutt_error _("Fatal error! Could not reopen mailbox!");
581 mutt_block_signals ();
583 if (mbox_lock_mailbox (ctx, 1, 1) == -1) {
584 mutt_unblock_signals ();
585 mutt_error _("Unable to lock mailbox!");
590 /* Check to make sure that the file hasn't changed on disk */
591 if ((i = _mbox_check_mailbox (ctx, index_hint)) == M_NEW_MAIL
592 || i == M_REOPENED) {
593 /* new mail arrived, or mailbox reopened */
602 /* Create a temporary file to write the new version of the mailbox in. */
603 mutt_mktemp (tempfile);
604 if ((i = open (tempfile, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1 ||
605 (fp = fdopen (i, "w")) == NULL) {
610 mutt_error _("Could not create temporary file!");
616 /* find the first deleted/changed message. we save a lot of time by only
617 * rewriting the mailbox from the point where it has actually changed.
619 for (i = 0; i < ctx->msgcount && !ctx->hdrs[i]->deleted &&
620 !ctx->hdrs[i]->changed && !ctx->hdrs[i]->attach_del; i++);
621 if (i == ctx->msgcount) {
622 /* this means ctx->changed or ctx->deleted was set, but no
623 * messages were found to be changed or deleted. This should
624 * never happen, is we presume it is a bug in mutt.
627 _("sync: mbox modified, but no modified messages! (report this bug)");
628 mutt_sleep (5); /* the mutt_error /will/ get cleared! */
629 debug_print (1, ("no modified messages.\n"));
634 /* save the index of the first changed/deleted message */
636 /* where to start overwriting */
637 offset = ctx->hdrs[i]->offset;
639 /* the offset stored in the header does not include the MMDF_SEP, so make
640 * sure we seek to the correct location
642 if (ctx->magic == M_MMDF)
643 offset -= (sizeof MMDF_SEP - 1);
645 /* allocate space for the new offsets */
646 newOffset = safe_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
647 oldOffset = safe_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
649 for (i = first, j = 0; i < ctx->msgcount; i++) {
651 * back up some information which is needed to restore offsets when
655 oldOffset[i - first].valid = 1;
656 oldOffset[i - first].hdr = ctx->hdrs[i]->offset;
657 oldOffset[i - first].body = ctx->hdrs[i]->content->offset;
658 oldOffset[i - first].lines = ctx->hdrs[i]->lines;
659 oldOffset[i - first].length = ctx->hdrs[i]->content->length;
661 if (!ctx->hdrs[i]->deleted) {
663 if (!ctx->quiet && WriteInc && ((i % WriteInc) == 0 || j == 1))
664 mutt_message (_("Writing messages... %d (%d%%)"), i,
665 (int) (ftell (ctx->fp) / (ctx->size / 100 + 1)));
667 if (ctx->magic == M_MMDF) {
668 if (fputs (MMDF_SEP, fp) == EOF) {
669 mutt_perror (tempfile);
677 /* save the new offset for this message. we add `offset' because the
678 * temporary file only contains saved message which are located after
679 * `offset' in the real mailbox
681 newOffset[i - first].hdr = ftell (fp) + offset;
683 if (mutt_copy_message
684 (fp, ctx, ctx->hdrs[i], M_CM_UPDATE,
685 CH_FROM | CH_UPDATE | CH_UPDATE_LEN) == -1) {
686 mutt_perror (tempfile);
692 /* Since messages could have been deleted, the offsets stored in memory
693 * will be wrong, so update what we can, which is the offset of this
694 * message, and the offset of the body. If this is a multipart message,
695 * we just flush the in memory cache so that the message will be reparsed
696 * if the user accesses it later.
698 newOffset[i - first].body =
699 ftell (fp) - ctx->hdrs[i]->content->length + offset;
700 mutt_free_body (&ctx->hdrs[i]->content->parts);
702 switch (ctx->magic) {
704 if (fputs (MMDF_SEP, fp) == EOF) {
705 mutt_perror (tempfile);
712 if (fputs ("\n", fp) == EOF) {
713 mutt_perror (tempfile);
722 if (fclose (fp) != 0) {
724 debug_print (1, ("fclose() returned non-zero.\n"));
726 mutt_perror (tempfile);
732 /* Save the state of this folder. */
733 if (stat (ctx->path, &statbuf) == -1) {
734 mutt_perror (ctx->path);
740 if ((fp = fopen (tempfile, "r")) == NULL) {
741 mutt_unblock_signals ();
742 mx_fastclose_mailbox (ctx);
743 debug_print (1, ("unable to reopen temp copy of mailbox!\n"));
744 mutt_perror (tempfile);
749 if (fseek (ctx->fp, offset, SEEK_SET) != 0 || /* seek the append location */
750 /* do a sanity check to make sure the mailbox looks ok */
751 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
752 (ctx->magic == M_MBOX && safe_strncmp ("From ", buf, 5) != 0) ||
753 (ctx->magic == M_MMDF && mutt_strcmp (MMDF_SEP, buf) != 0)) {
754 debug_print (1, ("message not in expected position.\n"));
755 debug_print (1, ("LINE: %s\n", buf));
759 if (fseek (ctx->fp, offset, SEEK_SET) != 0) { /* return to proper offset */
761 debug_print (1, ("fseek() failed\n"));
764 /* copy the temp mailbox back into place starting at the first
765 * change/deleted message
767 mutt_message _("Committing changes...");
769 i = mutt_copy_stream (fp, ctx->fp);
771 if (ferror (ctx->fp))
775 ctx->size = ftell (ctx->fp); /* update the size of the mailbox */
776 ftruncate (fileno (ctx->fp), ctx->size);
782 mbox_unlock_mailbox (ctx);
784 if (fclose (ctx->fp) != 0 || i == -1) {
785 /* error occured while writing the mailbox back, so keep the temp copy
789 char savefile[_POSIX_PATH_MAX];
791 snprintf (savefile, sizeof (savefile), "%s/mutt.%s-%s-%u",
792 NONULL (Tempdir), NONULL (Username), NONULL (Hostname),
793 (unsigned int) getpid ());
794 rename (tempfile, savefile);
795 mutt_unblock_signals ();
796 mx_fastclose_mailbox (ctx);
797 mutt_pretty_mailbox (savefile);
798 mutt_error (_("Write failed! Saved partial mailbox to %s"), savefile);
803 /* Restore the previous access/modification times */
804 utimebuf.actime = statbuf.st_atime;
805 utimebuf.modtime = statbuf.st_mtime;
806 utime (ctx->path, &utimebuf);
808 /* reopen the mailbox in read-only mode */
809 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
811 mutt_unblock_signals ();
812 mx_fastclose_mailbox (ctx);
813 mutt_error _("Fatal error! Could not reopen mailbox!");
817 /* update the offsets of the rewritten messages */
818 for (i = first, j = first; i < ctx->msgcount; i++) {
819 if (!ctx->hdrs[i]->deleted) {
820 ctx->hdrs[i]->offset = newOffset[i - first].hdr;
821 ctx->hdrs[i]->content->hdr_offset = newOffset[i - first].hdr;
822 ctx->hdrs[i]->content->offset = newOffset[i - first].body;
823 ctx->hdrs[i]->index = j++;
828 unlink (tempfile); /* remove partial copy of the mailbox */
829 mutt_unblock_signals ();
831 return (0); /* signal success */
833 bail: /* Come here in case of disaster */
837 /* restore offsets, as far as they are valid */
838 if (first >= 0 && oldOffset) {
839 for (i = first; i < ctx->msgcount && oldOffset[i - first].valid; i++) {
840 ctx->hdrs[i]->offset = oldOffset[i - first].hdr;
841 ctx->hdrs[i]->content->hdr_offset = oldOffset[i - first].hdr;
842 ctx->hdrs[i]->content->offset = oldOffset[i - first].body;
843 ctx->hdrs[i]->lines = oldOffset[i - first].lines;
844 ctx->hdrs[i]->content->length = oldOffset[i - first].length;
848 /* this is ok to call even if we haven't locked anything */
849 mbox_unlock_mailbox (ctx);
851 mutt_unblock_signals ();
855 if ((ctx->fp = freopen (ctx->path, "r", ctx->fp)) == NULL) {
856 mutt_error _("Could not reopen mailbox!");
858 mx_fastclose_mailbox (ctx);
863 /* if the mailbox was reopened, the thread tree will be invalid so make
864 * sure to start threading from scratch. */
865 mutt_sort_headers (ctx, (need_sort == M_REOPENED));
870 static int mbox_sync_mailbox (CONTEXT * ctx, int unused, int *index_hint) {
874 int rc = _mbox_sync_mailbox (ctx, unused, index_hint);
877 if ((tmp = buffy_find_mailbox (ctx->path)) && tmp->new == 0)
878 buffy_update_mailbox (tmp);
883 /* close a mailbox opened in write-mode */
884 int mbox_close_mailbox (CONTEXT * ctx)
886 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
888 #ifdef USE_COMPRESSED
889 if (ctx->compressinfo)
890 mutt_slow_close_compressed (ctx);
893 mutt_unblock_signals ();
894 mx_fastclose_mailbox (ctx);
898 static int mbox_reopen_mailbox (CONTEXT * ctx, int *index_hint)
900 int (*cmp_headers) (const HEADER *, const HEADER *) = NULL;
908 /* silent operations */
911 mutt_message _("Reopening mailbox...");
913 /* our heuristics require the old mailbox to be unsorted */
914 if (Sort != SORT_ORDER) {
919 mutt_sort_headers (ctx, 1);
926 /* simulate a close */
928 hash_destroy (&ctx->id_hash, NULL);
930 hash_destroy (&ctx->subj_hash, NULL);
931 mutt_clear_threads (ctx);
934 for (i = 0; i < ctx->msgcount; i++)
935 mutt_free_header (&(ctx->hdrs[i])); /* nothing to do! */
939 /* save the old headers */
940 old_msgcount = ctx->msgcount;
941 old_hdrs = ctx->hdrs;
945 ctx->hdrmax = 0; /* force allocation of new headers */
955 ctx->subj_hash = NULL;
957 switch (ctx->magic) {
960 if (fseek (ctx->fp, 0, SEEK_SET) != 0) {
961 debug_print (1, ("fseek() failed\n"));
965 cmp_headers = mutt_cmp_header;
966 if (ctx->magic == M_MBOX)
967 rc = mbox_parse_mailbox (ctx);
969 rc = mmdf_parse_mailbox (ctx);
979 /* free the old headers */
980 for (j = 0; j < old_msgcount; j++)
981 mutt_free_header (&(old_hdrs[j]));
988 /* now try to recover the old flags */
990 index_hint_set = (index_hint == NULL);
992 if (!ctx->readonly) {
993 for (i = 0; i < ctx->msgcount; i++) {
996 /* some messages have been deleted, and new messages have been
997 * appended at the end; the heuristic is that old messages have then
998 * "advanced" towards the beginning of the folder, so we begin the
999 * search at index "i"
1001 for (j = i; j < old_msgcount; j++) {
1002 if (old_hdrs[j] == NULL)
1004 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
1010 for (j = 0; j < i && j < old_msgcount; j++) {
1011 if (old_hdrs[j] == NULL)
1013 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
1021 /* this is best done here */
1022 if (!index_hint_set && *index_hint == j)
1025 if (old_hdrs[j]->changed) {
1026 /* Only update the flags if the old header was changed;
1027 * otherwise, the header may have been modified externally,
1028 * and we don't want to lose _those_ changes
1030 mutt_set_flag (ctx, ctx->hdrs[i], M_FLAG, old_hdrs[j]->flagged);
1031 mutt_set_flag (ctx, ctx->hdrs[i], M_REPLIED, old_hdrs[j]->replied);
1032 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, old_hdrs[j]->old);
1033 mutt_set_flag (ctx, ctx->hdrs[i], M_READ, old_hdrs[j]->read);
1035 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, old_hdrs[j]->deleted);
1036 mutt_set_flag (ctx, ctx->hdrs[i], M_TAG, old_hdrs[j]->tagged);
1038 /* we don't need this header any more */
1039 mutt_free_header (&(old_hdrs[j]));
1043 /* free the remaining old headers */
1044 for (j = 0; j < old_msgcount; j++) {
1046 mutt_free_header (&(old_hdrs[j]));
1055 return ((ctx->changed || msg_mod) ? M_REOPENED : M_NEW_MAIL);
1060 * 1 if the mailbox is not empty
1061 * 0 if the mailbox is empty
1064 int mbox_check_empty (const char *path)
1068 if (stat (path, &st) == -1)
1071 return ((st.st_size == 0));
1074 int mbox_is_magic (const char* path, struct stat* st) {
1077 char tmp[_POSIX_PATH_MAX];
1079 if (S_ISDIR(st->st_mode))
1082 if (st->st_size == 0) {
1083 /* hard to tell what zero-length files are, so assume the default magic */
1084 if (DefaultMagic == M_MBOX || DefaultMagic == M_MMDF)
1085 return (DefaultMagic);
1089 else if ((f = fopen (path, "r")) != NULL) {
1091 struct utimbuf times;
1093 fgets (tmp, sizeof (tmp), f);
1094 if (safe_strncmp ("From ", tmp, 5) == 0)
1096 else if (mutt_strcmp (MMDF_SEP, tmp) == 0)
1100 /* need to restore the times here, the file was not really accessed,
1101 * only the type was accessed. This is important, because detection
1102 * of "new mail" depends on those times set correctly.
1104 times.actime = st->st_atime;
1105 times.modtime = st->st_mtime;
1106 utime (path, ×);
1110 return (-1); /* fopen failed */
1113 #ifdef USE_COMPRESSED
1114 if (magic == -1 && mutt_can_read_compressed (path))
1115 return (M_COMPRESSED);
1120 static int commit_message (MESSAGE* msg, CONTEXT* ctx, int mbox) {
1121 if ((mbox && fputc ('\n', msg->fp) == EOF) ||
1122 (!mbox && fputs (MMDF_SEP, msg->fp) == EOF))
1124 if ((fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1)) {
1125 mutt_perror (_("Can't write message"));
1131 static int mbox_commit_message (MESSAGE* msg, CONTEXT* ctx) {
1132 return (commit_message (msg, ctx, 1));
1135 static int mmdf_commit_message (MESSAGE* msg, CONTEXT* ctx) {
1136 return (commit_message (msg, ctx, 0));
1139 static mx_t* reg_mx (void) {
1140 mx_t* fmt = safe_calloc (1, sizeof (mx_t));
1142 fmt->mx_check_empty = mbox_check_empty;
1143 fmt->mx_is_magic = mbox_is_magic;
1144 fmt->mx_access = access;
1145 fmt->mx_open_mailbox = mbox_open_mailbox;
1146 fmt->mx_open_new_message = mbox_open_new_message;
1147 fmt->mx_sync_mailbox = mbox_sync_mailbox;
1148 fmt->mx_check_mailbox = mbox_check_mailbox;
1152 mx_t* mbox_reg_mx (void) {
1153 mx_t* fmt = reg_mx ();
1155 fmt->mx_commit_message = mbox_commit_message;
1158 mx_t* mmdf_reg_mx (void) {
1159 mx_t* fmt = reg_mx ();
1161 fmt->mx_commit_message = mmdf_commit_message;