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 */
25 #include <lib-lib/mem.h>
26 #include <lib-lib/str.h>
27 #include <lib-lib/macros.h>
28 #include <lib-lib/file.h>
29 #include <lib-lib/debug.h>
31 #include <lib-sys/mutt_signal.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 __attribute__ ((unused)))
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 if (stat (ctx->path, &sb) == -1) {
102 mutt_perror (ctx->path);
105 ctx->mtime = sb.st_mtime;
106 ctx->size = sb.st_size;
108 /* precompute the local timezone to speed up calculation of the
110 tz = mutt_local_tz (0);
112 buf[sizeof (buf) - 1] = 0;
115 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
118 if (m_strcmp(buf, MMDF_SEP) == 0) {
119 loc = ftello (ctx->fp);
122 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
123 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
124 (int) (loc / (ctx->size / 100 + 1)));
127 if (ctx->msgcount == ctx->hdrmax)
128 mx_alloc_memory (ctx);
129 ctx->hdrs[ctx->msgcount] = hdr = header_new();
131 hdr->index = ctx->msgcount;
133 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL) {
134 /* TODO: memory leak??? */
135 debug_print (1, ("unexpected EOF\n"));
141 if (!is_from (buf, return_path, sizeof (return_path), &t)) {
142 if (fseeko (ctx->fp, loc, SEEK_SET) != 0) {
143 debug_print (1, ("fseeko() failed\n"));
144 mutt_error _("Mailbox is corrupt!");
150 hdr->received = t - tz;
152 hdr->env = mutt_read_rfc822_header (ctx->fp, hdr, 0, 0);
154 loc = ftello (ctx->fp);
156 if (hdr->content->length > 0 && hdr->lines > 0) {
157 tmploc = loc + hdr->content->length;
159 if (0 < tmploc && tmploc < ctx->size) {
160 if (fseeko (ctx->fp, tmploc, SEEK_SET) != 0 ||
161 fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL ||
162 m_strcmp(MMDF_SEP, buf) != 0) {
163 if (fseeko (ctx->fp, loc, SEEK_SET) != 0)
164 debug_print (1, ("fseeko() failed\n"));
165 hdr->content->length = -1;
169 hdr->content->length = -1;
172 hdr->content->length = -1;
174 if (hdr->content->length < 0) {
177 loc = ftello (ctx->fp);
178 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
181 } while (m_strcmp(buf, MMDF_SEP) != 0);
184 hdr->content->length = loc - hdr->content->offset;
187 if (!hdr->env->return_path && return_path[0])
188 hdr->env->return_path =
189 rfc822_parse_adrlist (hdr->env->return_path, return_path);
192 hdr->env->from = address_list_dup (hdr->env->return_path);
197 debug_print (1, ("corrupt mailbox!\n"));
198 mutt_error _("Mailbox is corrupt!");
204 if (ctx->msgcount > oldmsgcount)
205 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
210 /* Note that this function is also called when new mail is appended to the
211 * currently open folder, and NOT just when the mailbox is initially read.
213 * NOTE: it is assumed that the mailbox being read has been locked before
214 * this routine gets called. Strange things could happen if it's not!
216 static int mbox_parse_mailbox (CONTEXT * ctx)
219 char buf[HUGE_STRING], return_path[STRING];
222 int count = 0, lines = 0;
225 /* Save information about the folder at the time we opened it. */
226 if (stat (ctx->path, &sb) == -1) {
227 mutt_perror (ctx->path);
231 ctx->size = sb.st_size;
232 ctx->mtime = sb.st_mtime;
235 ctx->readonly = access (ctx->path, W_OK) ? 1 : 0;
237 /* precompute the local timezone to speed up calculation of the
239 tz = mutt_local_tz (0);
241 loc = ftello (ctx->fp);
242 while (fgets (buf, sizeof (buf), ctx->fp) != NULL) {
243 if (is_from (buf, return_path, sizeof (return_path), &t)) {
244 /* Save the Content-Length of the previous message */
246 #define PREV ctx->hdrs[ctx->msgcount-1]
248 if (PREV->content->length < 0) {
249 PREV->content->length = loc - PREV->content->offset - 1;
250 if (PREV->content->length < 0)
251 PREV->content->length = 0;
254 PREV->lines = lines ? lines - 1 : 0;
259 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
260 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
261 (int) (ftello (ctx->fp) / (ctx->size / 100 + 1)));
263 if (ctx->msgcount == ctx->hdrmax)
264 mx_alloc_memory (ctx);
266 curhdr = ctx->hdrs[ctx->msgcount] = header_new();
267 curhdr->received = t - tz;
268 curhdr->offset = loc;
269 curhdr->index = ctx->msgcount;
271 curhdr->env = mutt_read_rfc822_header (ctx->fp, curhdr, 0, 0);
273 /* if we know how long this message is, either just skip over the body,
274 * or if we don't know how many lines there are, count them now (this will
275 * save time by not having to search for the next message marker).
277 if (curhdr->content->length > 0) {
280 loc = ftello (ctx->fp);
281 tmploc = loc + curhdr->content->length + 1;
283 if (0 < tmploc && tmploc < ctx->size) {
285 * check to see if the content-length looks valid. we expect to
286 * to see a valid message separator at this point in the stream
288 if (fseeko (ctx->fp, tmploc, SEEK_SET) != 0 ||
289 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
290 m_strncmp("From ", buf, 5) != 0) {
291 debug_print (1, ("bad content-length in message %d (cl=%zd)\n",
292 curhdr->index, curhdr->content->length));
293 debug_print (1, ("LINE: %s\n", buf));
294 if (fseeko (ctx->fp, loc, SEEK_SET) != 0) { /* nope, return the previous position */
295 debug_print (1, ("fseeko() failed\n"));
297 curhdr->content->length = -1;
300 else if (tmploc != ctx->size) {
301 /* content-length would put us past the end of the file, so it
304 curhdr->content->length = -1;
307 if (curhdr->content->length != -1) {
308 /* good content-length. check to see if we know how many lines
309 * are in this message.
311 if (curhdr->lines == 0) {
312 int cl = curhdr->content->length;
314 /* count the number of lines in this message */
315 if (fseeko (ctx->fp, loc, SEEK_SET) != 0)
316 debug_print (1, ("fseeko() failed\n"));
318 if (fgetc (ctx->fp) == '\n')
323 /* return to the offset of the next message separator */
324 if (fseeko (ctx->fp, tmploc, SEEK_SET) != 0)
325 debug_print (1, ("fseeko() failed\n"));
331 if (!curhdr->env->return_path && return_path[0])
332 curhdr->env->return_path =
333 rfc822_parse_adrlist (curhdr->env->return_path, return_path);
335 if (!curhdr->env->from)
336 curhdr->env->from = address_list_dup (curhdr->env->return_path);
343 loc = ftello (ctx->fp);
347 * Only set the content-length of the previous message if we have read more
348 * than one message during _this_ invocation. If this routine is called
349 * when new mail is received, we need to make sure not to clobber what
350 * previously was the last message since the headers may be sorted.
353 if (PREV->content->length < 0) {
354 PREV->content->length = ftello (ctx->fp) - PREV->content->offset - 1;
355 if (PREV->content->length < 0)
356 PREV->content->length = 0;
360 PREV->lines = lines ? lines - 1 : 0;
362 mx_update_context (ctx, count);
370 /* open a mbox or mmdf style mailbox */
371 static int mbox_open_mailbox (CONTEXT * ctx)
375 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
376 mutt_perror (ctx->path);
379 mutt_block_signals ();
380 if (mbox_lock_mailbox (ctx, 0, 1) == -1) {
381 mutt_unblock_signals ();
385 if (ctx->magic == M_MBOX)
386 rc = mbox_parse_mailbox (ctx);
387 else if (ctx->magic == M_MMDF)
388 rc = mmdf_parse_mailbox (ctx);
392 mbox_unlock_mailbox (ctx);
393 mutt_unblock_signals ();
397 /* check to see if the mailbox has changed on disk.
400 * M_REOPENED mailbox has been reopened
401 * M_NEW_MAIL new mail has arrived!
402 * M_LOCKED couldn't lock the file
406 static int _mbox_check_mailbox (CONTEXT * ctx, int *index_hint)
409 char buffer[LONG_STRING];
413 if (stat (ctx->path, &st) == 0) {
414 if (st.st_mtime == ctx->mtime && st.st_size == ctx->size)
417 if (st.st_size == ctx->size) {
418 /* the file was touched, but it is still the same length, so just exit */
419 ctx->mtime = st.st_mtime;
423 if (st.st_size > ctx->size) {
424 /* lock the file if it isn't already */
426 mutt_block_signals ();
427 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
428 mutt_unblock_signals ();
429 /* we couldn't lock the mailbox, but nothing serious happened:
430 * probably the new mail arrived: no reason to wait till we can
431 * parse it: we'll get it on the next pass
439 * Check to make sure that the only change to the mailbox is that
440 * message(s) were appended to this file. My heuristic is that we should
441 * see the message separator at *exactly* what used to be the end of the
444 if (fseeko (ctx->fp, ctx->size, SEEK_SET) != 0)
445 debug_print (1, ("fseeko() failed\n"));
446 if (fgets (buffer, sizeof (buffer), ctx->fp) != NULL) {
447 if ((ctx->magic == M_MBOX && m_strncmp("From ", buffer, 5) == 0)
448 || (ctx->magic == M_MMDF && m_strcmp(MMDF_SEP, buffer) == 0)) {
449 if (fseeko (ctx->fp, ctx->size, SEEK_SET) != 0)
450 debug_print (1, ("fseeko() failed\n"));
451 if (ctx->magic == M_MBOX)
452 mbox_parse_mailbox (ctx);
454 mmdf_parse_mailbox (ctx);
456 /* Only unlock the folder if it was locked inside of this routine.
457 * It may have been locked elsewhere, like in
458 * mutt_checkpoint_mailbox().
462 mbox_unlock_mailbox (ctx);
463 mutt_unblock_signals ();
466 return (M_NEW_MAIL); /* signal that new mail arrived */
472 debug_print (1, ("fgets returned NULL.\n"));
481 if (mbox_reopen_mailbox (ctx, index_hint) != -1) {
483 mbox_unlock_mailbox (ctx);
484 mutt_unblock_signals ();
492 mbox_unlock_mailbox (ctx);
493 mx_fastclose_mailbox (ctx);
494 mutt_unblock_signals ();
495 mutt_error _("Mailbox was corrupted!");
500 static int mbox_check_mailbox (CONTEXT* ctx, int* index_hint, int lock) {
504 mutt_block_signals ();
505 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
506 mutt_unblock_signals ();
511 rc = _mbox_check_mailbox (ctx, index_hint);
514 mutt_unblock_signals ();
515 mbox_unlock_mailbox (ctx);
524 static int _mbox_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)), int *index_hint)
526 char tempfile[_POSIX_PATH_MAX];
528 int i, j, save_sort = SORT_ORDER;
530 int need_sort = 0; /* flag to resort mailbox if new mail arrives */
531 int first = -1; /* first message to be written */
532 off_t offset; /* location in mailbox to write changed messages */
534 struct utimbuf utimebuf;
535 struct m_update_t *newOffset = NULL;
536 struct m_update_t *oldOffset = NULL;
539 /* sort message by their position in the mailbox on disk */
540 if (Sort != SORT_ORDER) {
543 mutt_sort_headers (ctx, 0);
548 /* need to open the file for writing in such a way that it does not truncate
549 * the file, so use read-write mode.
551 if ((ctx->fp = freopen (ctx->path, "r+", ctx->fp)) == NULL) {
552 mx_fastclose_mailbox (ctx);
553 mutt_error _("Fatal error! Could not reopen mailbox!");
558 mutt_block_signals ();
560 if (mbox_lock_mailbox (ctx, 1, 1) == -1) {
561 mutt_unblock_signals ();
562 mutt_error _("Unable to lock mailbox!");
567 /* Check to make sure that the file hasn't changed on disk */
568 if ((i = _mbox_check_mailbox (ctx, index_hint)) == M_NEW_MAIL
569 || i == M_REOPENED) {
570 /* new mail arrived, or mailbox reopened */
579 /* Create a temporary file to write the new version of the mailbox in. */
580 mutt_mktemp (tempfile);
581 if ((i = open (tempfile, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1 ||
582 (fp = fdopen (i, "w")) == NULL) {
587 mutt_error _("Could not create temporary file!");
593 /* find the first deleted/changed message. we save a lot of time by only
594 * rewriting the mailbox from the point where it has actually changed.
596 for (i = 0; i < ctx->msgcount && !ctx->hdrs[i]->deleted &&
597 !ctx->hdrs[i]->changed && !ctx->hdrs[i]->attach_del; i++);
598 if (i == ctx->msgcount) {
599 /* this means ctx->changed or ctx->deleted was set, but no
600 * messages were found to be changed or deleted. This should
601 * never happen, is we presume it is a bug in mutt.
604 _("sync: mbox modified, but no modified messages! (report this bug)");
605 mutt_sleep (5); /* the mutt_error /will/ get cleared! */
606 debug_print (1, ("no modified messages.\n"));
611 /* save the index of the first changed/deleted message */
613 /* where to start overwriting */
614 offset = ctx->hdrs[i]->offset;
616 /* the offset stored in the header does not include the MMDF_SEP, so make
617 * sure we seek to the correct location
619 if (ctx->magic == M_MMDF)
620 offset -= (sizeof MMDF_SEP - 1);
622 /* allocate space for the new offsets */
623 newOffset = p_new(struct m_update_t, ctx->msgcount - first);
624 oldOffset = p_new(struct m_update_t, ctx->msgcount - first);
626 for (i = first, j = 0; i < ctx->msgcount; i++) {
628 * back up some information which is needed to restore offsets when
632 oldOffset[i - first].valid = 1;
633 oldOffset[i - first].hdr = ctx->hdrs[i]->offset;
634 oldOffset[i - first].body = ctx->hdrs[i]->content->offset;
635 oldOffset[i - first].lines = ctx->hdrs[i]->lines;
636 oldOffset[i - first].length = ctx->hdrs[i]->content->length;
638 if (!ctx->hdrs[i]->deleted) {
640 if (!ctx->quiet && WriteInc && ((i % WriteInc) == 0 || j == 1))
641 mutt_message (_("Writing messages... %d (%d%%)"), i,
642 (int) (ftello (ctx->fp) / (ctx->size / 100 + 1)));
644 if (ctx->magic == M_MMDF) {
645 if (fputs (MMDF_SEP, fp) == EOF) {
646 mutt_perror (tempfile);
654 /* save the new offset for this message. we add `offset' because the
655 * temporary file only contains saved message which are located after
656 * `offset' in the real mailbox
658 newOffset[i - first].hdr = ftello (fp) + offset;
660 if (mutt_copy_message
661 (fp, ctx, ctx->hdrs[i], M_CM_UPDATE,
662 CH_FROM | CH_UPDATE | CH_UPDATE_LEN) == -1) {
663 mutt_perror (tempfile);
669 /* Since messages could have been deleted, the offsets stored in memory
670 * will be wrong, so update what we can, which is the offset of this
671 * message, and the offset of the body. If this is a multipart message,
672 * we just flush the in memory cache so that the message will be reparsed
673 * if the user accesses it later.
675 newOffset[i - first].body =
676 ftello (fp) - ctx->hdrs[i]->content->length + offset;
677 mutt_free_body (&ctx->hdrs[i]->content->parts);
679 switch (ctx->magic) {
681 if (fputs (MMDF_SEP, fp) == EOF) {
682 mutt_perror (tempfile);
689 if (fputs ("\n", fp) == EOF) {
690 mutt_perror (tempfile);
699 if (fclose (fp) != 0) {
701 debug_print (1, ("fclose() returned non-zero.\n"));
703 mutt_perror (tempfile);
709 /* Save the state of this folder. */
710 if (stat (ctx->path, &statbuf) == -1) {
711 mutt_perror (ctx->path);
717 if ((fp = fopen (tempfile, "r")) == NULL) {
718 mutt_unblock_signals ();
719 mx_fastclose_mailbox (ctx);
720 debug_print (1, ("unable to reopen temp copy of mailbox!\n"));
721 mutt_perror (tempfile);
726 if (fseeko (ctx->fp, offset, SEEK_SET) != 0 || /* seek the append location */
727 /* do a sanity check to make sure the mailbox looks ok */
728 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
729 (ctx->magic == M_MBOX && m_strncmp("From ", buf, 5) != 0) ||
730 (ctx->magic == M_MMDF && m_strcmp(MMDF_SEP, buf) != 0)) {
731 debug_print (1, ("message not in expected position.\n"));
732 debug_print (1, ("LINE: %s\n", buf));
736 if (fseeko (ctx->fp, offset, SEEK_SET) != 0) { /* return to proper offset */
738 debug_print (1, ("fseeko() failed\n"));
741 /* copy the temp mailbox back into place starting at the first
742 * change/deleted message
744 mutt_message _("Committing changes...");
746 i = mutt_copy_stream (fp, ctx->fp);
748 if (ferror (ctx->fp))
752 ctx->size = ftello (ctx->fp); /* update the size of the mailbox */
753 ftruncate (fileno (ctx->fp), ctx->size);
759 mbox_unlock_mailbox (ctx);
761 if (fclose (ctx->fp) != 0 || i == -1) {
762 /* error occured while writing the mailbox back, so keep the temp copy
766 char savefile[_POSIX_PATH_MAX];
768 snprintf (savefile, sizeof (savefile), "%s/mutt.%s-%s-%u",
769 NONULL (Tempdir), NONULL (Username), NONULL (Hostname),
770 (unsigned int) getpid ());
771 rename (tempfile, savefile);
772 mutt_unblock_signals ();
773 mx_fastclose_mailbox (ctx);
774 mutt_pretty_mailbox (savefile);
775 mutt_error (_("Write failed! Saved partial mailbox to %s"), savefile);
780 /* Restore the previous access/modification times */
781 utimebuf.actime = statbuf.st_atime;
782 utimebuf.modtime = statbuf.st_mtime;
783 utime (ctx->path, &utimebuf);
785 /* reopen the mailbox in read-only mode */
786 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
788 mutt_unblock_signals ();
789 mx_fastclose_mailbox (ctx);
790 mutt_error _("Fatal error! Could not reopen mailbox!");
794 /* update the offsets of the rewritten messages */
795 for (i = first, j = first; i < ctx->msgcount; i++) {
796 if (!ctx->hdrs[i]->deleted) {
797 ctx->hdrs[i]->offset = newOffset[i - first].hdr;
798 ctx->hdrs[i]->content->hdr_offset = newOffset[i - first].hdr;
799 ctx->hdrs[i]->content->offset = newOffset[i - first].body;
800 ctx->hdrs[i]->index = j++;
803 p_delete(&newOffset);
804 p_delete(&oldOffset);
805 unlink (tempfile); /* remove partial copy of the mailbox */
806 mutt_unblock_signals ();
808 return (0); /* signal success */
810 bail: /* Come here in case of disaster */
814 /* restore offsets, as far as they are valid */
815 if (first >= 0 && oldOffset) {
816 for (i = first; i < ctx->msgcount && oldOffset[i - first].valid; i++) {
817 ctx->hdrs[i]->offset = oldOffset[i - first].hdr;
818 ctx->hdrs[i]->content->hdr_offset = oldOffset[i - first].hdr;
819 ctx->hdrs[i]->content->offset = oldOffset[i - first].body;
820 ctx->hdrs[i]->lines = oldOffset[i - first].lines;
821 ctx->hdrs[i]->content->length = oldOffset[i - first].length;
825 /* this is ok to call even if we haven't locked anything */
826 mbox_unlock_mailbox (ctx);
828 mutt_unblock_signals ();
829 p_delete(&newOffset);
830 p_delete(&oldOffset);
832 if ((ctx->fp = freopen (ctx->path, "r", ctx->fp)) == NULL) {
833 mutt_error _("Could not reopen mailbox!");
835 mx_fastclose_mailbox (ctx);
840 /* if the mailbox was reopened, the thread tree will be invalid so make
841 * sure to start threading from scratch. */
842 mutt_sort_headers (ctx, (need_sort == M_REOPENED));
847 static int mbox_sync_mailbox (CONTEXT * ctx, int unused, int *index_hint) {
851 int rc = _mbox_sync_mailbox (ctx, unused, index_hint);
854 if ((tmp = buffy_find_mailbox (ctx->path)) && tmp->new == 0)
855 buffy_update_mailbox (tmp);
860 /* close a mailbox opened in write-mode */
861 int mbox_close_mailbox (CONTEXT * ctx)
863 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
865 if (ctx->compressinfo)
866 mutt_slow_close_compressed (ctx);
868 mutt_unblock_signals ();
869 mx_fastclose_mailbox (ctx);
873 static int mbox_reopen_mailbox (CONTEXT * ctx, int *index_hint)
875 int (*cmp_headers) (const HEADER *, const HEADER *) = NULL;
883 /* silent operations */
886 mutt_message _("Reopening mailbox...");
888 /* our heuristics require the old mailbox to be unsorted */
889 if (Sort != SORT_ORDER) {
894 mutt_sort_headers (ctx, 1);
901 /* simulate a close */
903 hash_destroy (&ctx->id_hash, NULL);
905 hash_destroy (&ctx->subj_hash, NULL);
906 mutt_clear_threads (ctx);
909 for (i = 0; i < ctx->msgcount; i++)
910 header_delete(&(ctx->hdrs[i])); /* nothing to do! */
911 p_delete(&ctx->hdrs);
914 /* save the old headers */
915 old_msgcount = ctx->msgcount;
916 old_hdrs = ctx->hdrs;
920 ctx->hdrmax = 0; /* force allocation of new headers */
930 ctx->subj_hash = NULL;
932 switch (ctx->magic) {
935 if (fseeko (ctx->fp, 0, SEEK_SET) != 0) {
936 debug_print (1, ("fseeko() failed\n"));
940 cmp_headers = mutt_cmp_header;
941 if (ctx->magic == M_MBOX)
942 rc = mbox_parse_mailbox (ctx);
944 rc = mmdf_parse_mailbox (ctx);
954 /* free the old headers */
955 for (j = 0; j < old_msgcount; j++)
956 header_delete(&(old_hdrs[j]));
963 /* now try to recover the old flags */
965 index_hint_set = (index_hint == NULL);
967 if (!ctx->readonly) {
968 for (i = 0; i < ctx->msgcount; i++) {
971 /* some messages have been deleted, and new messages have been
972 * appended at the end; the heuristic is that old messages have then
973 * "advanced" towards the beginning of the folder, so we begin the
974 * search at index "i"
976 for (j = i; j < old_msgcount; j++) {
977 if (old_hdrs[j] == NULL)
979 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
985 for (j = 0; j < i && j < old_msgcount; j++) {
986 if (old_hdrs[j] == NULL)
988 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
996 /* this is best done here */
997 if (!index_hint_set && *index_hint == j)
1000 if (old_hdrs[j]->changed) {
1001 /* Only update the flags if the old header was changed;
1002 * otherwise, the header may have been modified externally,
1003 * and we don't want to lose _those_ changes
1005 mutt_set_flag (ctx, ctx->hdrs[i], M_FLAG, old_hdrs[j]->flagged);
1006 mutt_set_flag (ctx, ctx->hdrs[i], M_REPLIED, old_hdrs[j]->replied);
1007 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, old_hdrs[j]->old);
1008 mutt_set_flag (ctx, ctx->hdrs[i], M_READ, old_hdrs[j]->read);
1010 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, old_hdrs[j]->deleted);
1011 mutt_set_flag (ctx, ctx->hdrs[i], M_TAG, old_hdrs[j]->tagged);
1013 /* we don't need this header any more */
1014 header_delete(&(old_hdrs[j]));
1018 /* free the remaining old headers */
1019 for (j = 0; j < old_msgcount; j++) {
1021 header_delete(&(old_hdrs[j]));
1025 p_delete(&old_hdrs);
1030 return ((ctx->changed || msg_mod) ? M_REOPENED : M_NEW_MAIL);
1035 * 1 if the mailbox is not empty
1036 * 0 if the mailbox is empty
1039 int mbox_check_empty (const char *path)
1043 if (stat (path, &st) == -1)
1046 return ((st.st_size == 0));
1049 int mbox_is_magic (const char* path, struct stat* st) {
1052 char tmp[_POSIX_PATH_MAX];
1054 if (S_ISDIR(st->st_mode))
1057 if (st->st_size == 0) {
1058 /* hard to tell what zero-length files are, so assume the default magic */
1059 if (DefaultMagic == M_MBOX || DefaultMagic == M_MMDF)
1060 return (DefaultMagic);
1064 else if ((f = fopen (path, "r")) != NULL) {
1066 struct utimbuf times;
1068 fgets (tmp, sizeof (tmp), f);
1069 if (m_strncmp("From ", tmp, 5) == 0)
1071 else if (m_strcmp(MMDF_SEP, tmp) == 0)
1075 /* need to restore the times here, the file was not really accessed,
1076 * only the type was accessed. This is important, because detection
1077 * of "new mail" depends on those times set correctly.
1079 times.actime = st->st_atime;
1080 times.modtime = st->st_mtime;
1081 utime (path, ×);
1085 return (-1); /* fopen failed */
1088 if (magic == -1 && mutt_can_read_compressed (path))
1089 return (M_COMPRESSED);
1093 static int commit_message (MESSAGE* msg, CONTEXT* ctx __attribute__ ((unused)), int mbox) {
1094 if ((mbox && fputc ('\n', msg->fp) == EOF) ||
1095 (!mbox && fputs (MMDF_SEP, msg->fp) == EOF))
1097 if ((fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1)) {
1098 mutt_perror (_("Can't write message"));
1104 static int mbox_commit_message (MESSAGE* msg, CONTEXT* ctx) {
1105 return (commit_message (msg, ctx, 1));
1108 static int mmdf_commit_message (MESSAGE* msg, CONTEXT* ctx) {
1109 return (commit_message (msg, ctx, 0));
1112 static mx_t* reg_mx (void) {
1113 mx_t* fmt = p_new(mx_t, 1);
1115 fmt->mx_check_empty = mbox_check_empty;
1116 fmt->mx_is_magic = mbox_is_magic;
1117 fmt->mx_access = access;
1118 fmt->mx_open_mailbox = mbox_open_mailbox;
1119 fmt->mx_open_new_message = mbox_open_new_message;
1120 fmt->mx_sync_mailbox = mbox_sync_mailbox;
1121 fmt->mx_check_mailbox = mbox_check_mailbox;
1125 mx_t* mbox_reg_mx (void) {
1126 mx_t* fmt = reg_mx ();
1128 fmt->mx_commit_message = mbox_commit_message;
1131 mx_t* mmdf_reg_mx (void) {
1132 mx_t* fmt = reg_mx ();
1134 fmt->mx_commit_message = mmdf_commit_message;