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>
30 #include <lib-sys/mutt_signal.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 __attribute__ ((unused)))
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 if (stat (ctx->path, &sb) == -1) {
101 mutt_perror (ctx->path);
104 ctx->mtime = sb.st_mtime;
105 ctx->size = sb.st_size;
107 /* precompute the local timezone to speed up calculation of the
109 tz = mutt_local_tz (0);
111 buf[sizeof (buf) - 1] = 0;
114 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
117 if (m_strcmp(buf, MMDF_SEP) == 0) {
118 loc = ftello (ctx->fp);
121 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
122 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
123 (int) (loc / (ctx->size / 100 + 1)));
126 if (ctx->msgcount == ctx->hdrmax)
127 mx_alloc_memory (ctx);
128 ctx->hdrs[ctx->msgcount] = hdr = header_new();
130 hdr->index = ctx->msgcount;
132 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL) {
133 /* TODO: memory leak??? */
139 if (!is_from (buf, return_path, sizeof (return_path), &t)) {
140 if (fseeko (ctx->fp, loc, SEEK_SET) != 0) {
141 mutt_error _("Mailbox is corrupt!");
147 hdr->received = t - tz;
149 hdr->env = mutt_read_rfc822_header (ctx->fp, hdr, 0, 0);
151 loc = ftello (ctx->fp);
153 if (hdr->content->length > 0 && hdr->lines > 0) {
154 tmploc = loc + hdr->content->length;
156 if (0 < tmploc && tmploc < ctx->size) {
157 if (fseeko (ctx->fp, tmploc, SEEK_SET) != 0 ||
158 fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL ||
159 m_strcmp(MMDF_SEP, buf) != 0) {
160 fseeko (ctx->fp, loc, SEEK_SET);
161 hdr->content->length = -1;
165 hdr->content->length = -1;
168 hdr->content->length = -1;
170 if (hdr->content->length < 0) {
173 loc = ftello (ctx->fp);
174 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
177 } while (m_strcmp(buf, MMDF_SEP) != 0);
180 hdr->content->length = loc - hdr->content->offset;
183 if (!hdr->env->return_path && return_path[0])
184 hdr->env->return_path =
185 rfc822_parse_adrlist (hdr->env->return_path, return_path);
188 hdr->env->from = address_list_dup (hdr->env->return_path);
193 mutt_error _("Mailbox is corrupt!");
199 if (ctx->msgcount > oldmsgcount)
200 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
205 /* Note that this function is also called when new mail is appended to the
206 * currently open folder, and NOT just when the mailbox is initially read.
208 * NOTE: it is assumed that the mailbox being read has been locked before
209 * this routine gets called. Strange things could happen if it's not!
211 static int mbox_parse_mailbox (CONTEXT * ctx)
214 char buf[HUGE_STRING], return_path[STRING];
217 int count = 0, lines = 0;
220 /* Save information about the folder at the time we opened it. */
221 if (stat (ctx->path, &sb) == -1) {
222 mutt_perror (ctx->path);
226 ctx->size = sb.st_size;
227 ctx->mtime = sb.st_mtime;
230 ctx->readonly = access (ctx->path, W_OK) ? 1 : 0;
232 /* precompute the local timezone to speed up calculation of the
234 tz = mutt_local_tz (0);
236 loc = ftello (ctx->fp);
237 while (fgets (buf, sizeof (buf), ctx->fp) != NULL) {
238 if (is_from (buf, return_path, sizeof (return_path), &t)) {
239 /* Save the Content-Length of the previous message */
241 #define PREV ctx->hdrs[ctx->msgcount-1]
243 if (PREV->content->length < 0) {
244 PREV->content->length = loc - PREV->content->offset - 1;
245 if (PREV->content->length < 0)
246 PREV->content->length = 0;
249 PREV->lines = lines ? lines - 1 : 0;
254 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
255 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
256 (int) (ftello (ctx->fp) / (ctx->size / 100 + 1)));
258 if (ctx->msgcount == ctx->hdrmax)
259 mx_alloc_memory (ctx);
261 curhdr = ctx->hdrs[ctx->msgcount] = header_new();
262 curhdr->received = t - tz;
263 curhdr->offset = loc;
264 curhdr->index = ctx->msgcount;
266 curhdr->env = mutt_read_rfc822_header (ctx->fp, curhdr, 0, 0);
268 /* if we know how long this message is, either just skip over the body,
269 * or if we don't know how many lines there are, count them now (this will
270 * save time by not having to search for the next message marker).
272 if (curhdr->content->length > 0) {
275 loc = ftello (ctx->fp);
276 tmploc = loc + curhdr->content->length + 1;
278 if (0 < tmploc && tmploc < ctx->size) {
280 * check to see if the content-length looks valid. we expect to
281 * to see a valid message separator at this point in the stream
283 if (fseeko (ctx->fp, tmploc, SEEK_SET) != 0 ||
284 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
285 m_strncmp("From ", buf, 5) != 0) {
286 fseeko (ctx->fp, loc, SEEK_SET); /* nope, return the previous position */
287 curhdr->content->length = -1;
290 else if (tmploc != ctx->size) {
291 /* content-length would put us past the end of the file, so it
294 curhdr->content->length = -1;
297 if (curhdr->content->length != -1) {
298 /* good content-length. check to see if we know how many lines
299 * are in this message.
301 if (curhdr->lines == 0) {
302 int cl = curhdr->content->length;
304 /* count the number of lines in this message */
305 fseeko (ctx->fp, loc, SEEK_SET);
307 if (fgetc (ctx->fp) == '\n')
312 /* return to the offset of the next message separator */
313 fseeko(ctx->fp, tmploc, SEEK_SET);
319 if (!curhdr->env->return_path && return_path[0])
320 curhdr->env->return_path =
321 rfc822_parse_adrlist (curhdr->env->return_path, return_path);
323 if (!curhdr->env->from)
324 curhdr->env->from = address_list_dup (curhdr->env->return_path);
331 loc = ftello (ctx->fp);
335 * Only set the content-length of the previous message if we have read more
336 * than one message during _this_ invocation. If this routine is called
337 * when new mail is received, we need to make sure not to clobber what
338 * previously was the last message since the headers may be sorted.
341 if (PREV->content->length < 0) {
342 PREV->content->length = ftello (ctx->fp) - PREV->content->offset - 1;
343 if (PREV->content->length < 0)
344 PREV->content->length = 0;
348 PREV->lines = lines ? lines - 1 : 0;
350 mx_update_context (ctx, count);
358 /* open a mbox or mmdf style mailbox */
359 static int mbox_open_mailbox (CONTEXT * ctx)
363 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
364 mutt_perror (ctx->path);
367 mutt_block_signals ();
368 if (mbox_lock_mailbox (ctx, 0, 1) == -1) {
369 mutt_unblock_signals ();
373 if (ctx->magic == M_MBOX)
374 rc = mbox_parse_mailbox (ctx);
375 else if (ctx->magic == M_MMDF)
376 rc = mmdf_parse_mailbox (ctx);
380 mbox_unlock_mailbox (ctx);
381 mutt_unblock_signals ();
385 /* check to see if the mailbox has changed on disk.
388 * M_REOPENED mailbox has been reopened
389 * M_NEW_MAIL new mail has arrived!
390 * M_LOCKED couldn't lock the file
394 static int _mbox_check_mailbox (CONTEXT * ctx, int *index_hint)
397 char buffer[LONG_STRING];
401 if (stat (ctx->path, &st) == 0) {
402 if (st.st_mtime == ctx->mtime && st.st_size == ctx->size)
405 if (st.st_size == ctx->size) {
406 /* the file was touched, but it is still the same length, so just exit */
407 ctx->mtime = st.st_mtime;
411 if (st.st_size > ctx->size) {
412 /* lock the file if it isn't already */
414 mutt_block_signals ();
415 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
416 mutt_unblock_signals ();
417 /* we couldn't lock the mailbox, but nothing serious happened:
418 * probably the new mail arrived: no reason to wait till we can
419 * parse it: we'll get it on the next pass
427 * Check to make sure that the only change to the mailbox is that
428 * message(s) were appended to this file. My heuristic is that we should
429 * see the message separator at *exactly* what used to be the end of the
432 fseeko (ctx->fp, ctx->size, SEEK_SET);
433 if (fgets (buffer, sizeof (buffer), ctx->fp) != NULL) {
434 if ((ctx->magic == M_MBOX && m_strncmp("From ", buffer, 5) == 0)
435 || (ctx->magic == M_MMDF && m_strcmp(MMDF_SEP, buffer) == 0)) {
436 fseeko (ctx->fp, ctx->size, SEEK_SET);
437 if (ctx->magic == M_MBOX)
438 mbox_parse_mailbox (ctx);
440 mmdf_parse_mailbox (ctx);
442 /* Only unlock the folder if it was locked inside of this routine.
443 * It may have been locked elsewhere, like in
444 * mutt_checkpoint_mailbox().
448 mbox_unlock_mailbox (ctx);
449 mutt_unblock_signals ();
452 return (M_NEW_MAIL); /* signal that new mail arrived */
465 if (mbox_reopen_mailbox (ctx, index_hint) != -1) {
467 mbox_unlock_mailbox (ctx);
468 mutt_unblock_signals ();
476 mbox_unlock_mailbox (ctx);
477 mx_fastclose_mailbox (ctx);
478 mutt_unblock_signals ();
479 mutt_error _("Mailbox was corrupted!");
484 static int mbox_check_mailbox (CONTEXT* ctx, int* index_hint, int lock) {
488 mutt_block_signals ();
489 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
490 mutt_unblock_signals ();
495 rc = _mbox_check_mailbox (ctx, index_hint);
498 mutt_unblock_signals ();
499 mbox_unlock_mailbox (ctx);
508 static int _mbox_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)), int *index_hint)
510 char tempfile[_POSIX_PATH_MAX];
512 int i, j, save_sort = SORT_ORDER;
514 int need_sort = 0; /* flag to resort mailbox if new mail arrives */
515 int first = -1; /* first message to be written */
516 off_t offset; /* location in mailbox to write changed messages */
518 struct utimbuf utimebuf;
519 struct m_update_t *newOffset = NULL;
520 struct m_update_t *oldOffset = NULL;
523 /* sort message by their position in the mailbox on disk */
524 if (Sort != SORT_ORDER) {
527 mutt_sort_headers (ctx, 0);
532 /* need to open the file for writing in such a way that it does not truncate
533 * the file, so use read-write mode.
535 if ((ctx->fp = freopen (ctx->path, "r+", ctx->fp)) == NULL) {
536 mx_fastclose_mailbox (ctx);
537 mutt_error _("Fatal error! Could not reopen mailbox!");
542 mutt_block_signals ();
544 if (mbox_lock_mailbox (ctx, 1, 1) == -1) {
545 mutt_unblock_signals ();
546 mutt_error _("Unable to lock mailbox!");
551 /* Check to make sure that the file hasn't changed on disk */
552 if ((i = _mbox_check_mailbox (ctx, index_hint)) == M_NEW_MAIL
553 || i == M_REOPENED) {
554 /* new mail arrived, or mailbox reopened */
563 /* Create a temporary file to write the new version of the mailbox in. */
564 mutt_mktemp (tempfile);
565 if ((i = open (tempfile, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1 ||
566 (fp = fdopen (i, "w")) == NULL) {
571 mutt_error _("Could not create temporary file!");
577 /* find the first deleted/changed message. we save a lot of time by only
578 * rewriting the mailbox from the point where it has actually changed.
580 for (i = 0; i < ctx->msgcount && !ctx->hdrs[i]->deleted &&
581 !ctx->hdrs[i]->changed && !ctx->hdrs[i]->attach_del; i++);
582 if (i == ctx->msgcount) {
583 /* this means ctx->changed or ctx->deleted was set, but no
584 * messages were found to be changed or deleted. This should
585 * never happen, is we presume it is a bug in mutt.
588 _("sync: mbox modified, but no modified messages! (report this bug)");
589 mutt_sleep (5); /* the mutt_error /will/ get cleared! */
594 /* save the index of the first changed/deleted message */
596 /* where to start overwriting */
597 offset = ctx->hdrs[i]->offset;
599 /* the offset stored in the header does not include the MMDF_SEP, so make
600 * sure we seek to the correct location
602 if (ctx->magic == M_MMDF)
603 offset -= (sizeof MMDF_SEP - 1);
605 /* allocate space for the new offsets */
606 newOffset = p_new(struct m_update_t, ctx->msgcount - first);
607 oldOffset = p_new(struct m_update_t, ctx->msgcount - first);
609 for (i = first, j = 0; i < ctx->msgcount; i++) {
611 * back up some information which is needed to restore offsets when
615 oldOffset[i - first].valid = 1;
616 oldOffset[i - first].hdr = ctx->hdrs[i]->offset;
617 oldOffset[i - first].body = ctx->hdrs[i]->content->offset;
618 oldOffset[i - first].lines = ctx->hdrs[i]->lines;
619 oldOffset[i - first].length = ctx->hdrs[i]->content->length;
621 if (!ctx->hdrs[i]->deleted) {
623 if (!ctx->quiet && WriteInc && ((i % WriteInc) == 0 || j == 1))
624 mutt_message (_("Writing messages... %d (%d%%)"), i,
625 (int) (ftello (ctx->fp) / (ctx->size / 100 + 1)));
627 if (ctx->magic == M_MMDF) {
628 if (fputs (MMDF_SEP, fp) == EOF) {
629 mutt_perror (tempfile);
637 /* save the new offset for this message. we add `offset' because the
638 * temporary file only contains saved message which are located after
639 * `offset' in the real mailbox
641 newOffset[i - first].hdr = ftello (fp) + offset;
643 if (mutt_copy_message
644 (fp, ctx, ctx->hdrs[i], M_CM_UPDATE,
645 CH_FROM | CH_UPDATE | CH_UPDATE_LEN) == -1) {
646 mutt_perror (tempfile);
652 /* Since messages could have been deleted, the offsets stored in memory
653 * will be wrong, so update what we can, which is the offset of this
654 * message, and the offset of the body. If this is a multipart message,
655 * we just flush the in memory cache so that the message will be reparsed
656 * if the user accesses it later.
658 newOffset[i - first].body =
659 ftello (fp) - ctx->hdrs[i]->content->length + offset;
660 body_list_wipe(&ctx->hdrs[i]->content->parts);
662 switch (ctx->magic) {
664 if (fputs (MMDF_SEP, fp) == EOF) {
665 mutt_perror (tempfile);
672 if (fputs ("\n", fp) == EOF) {
673 mutt_perror (tempfile);
682 if (fclose (fp) != 0) {
685 mutt_perror (tempfile);
691 /* Save the state of this folder. */
692 if (stat (ctx->path, &statbuf) == -1) {
693 mutt_perror (ctx->path);
699 if ((fp = fopen (tempfile, "r")) == NULL) {
700 mutt_unblock_signals ();
701 mx_fastclose_mailbox (ctx);
702 mutt_perror (tempfile);
707 if (fseeko (ctx->fp, offset, SEEK_SET) != 0 || /* seek the append location */
708 /* do a sanity check to make sure the mailbox looks ok */
709 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
710 (ctx->magic == M_MBOX && m_strncmp("From ", buf, 5) != 0) ||
711 (ctx->magic == M_MMDF && m_strcmp(MMDF_SEP, buf) != 0)) {
715 if (fseeko (ctx->fp, offset, SEEK_SET) != 0) { /* return to proper offset */
718 /* copy the temp mailbox back into place starting at the first
719 * change/deleted message
721 mutt_message _("Committing changes...");
723 i = mutt_copy_stream (fp, ctx->fp);
725 if (ferror (ctx->fp))
729 ctx->size = ftello (ctx->fp); /* update the size of the mailbox */
730 ftruncate (fileno (ctx->fp), ctx->size);
736 mbox_unlock_mailbox (ctx);
738 if (fclose (ctx->fp) != 0 || i == -1) {
739 /* error occured while writing the mailbox back, so keep the temp copy
743 char savefile[_POSIX_PATH_MAX];
745 snprintf (savefile, sizeof (savefile), "%s/mutt.%s-%s-%u",
746 NONULL (Tempdir), NONULL (Username), NONULL (Hostname),
747 (unsigned int) getpid ());
748 rename (tempfile, savefile);
749 mutt_unblock_signals ();
750 mx_fastclose_mailbox (ctx);
751 mutt_pretty_mailbox (savefile);
752 mutt_error (_("Write failed! Saved partial mailbox to %s"), savefile);
757 /* Restore the previous access/modification times */
758 utimebuf.actime = statbuf.st_atime;
759 utimebuf.modtime = statbuf.st_mtime;
760 utime (ctx->path, &utimebuf);
762 /* reopen the mailbox in read-only mode */
763 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
765 mutt_unblock_signals ();
766 mx_fastclose_mailbox (ctx);
767 mutt_error _("Fatal error! Could not reopen mailbox!");
771 /* update the offsets of the rewritten messages */
772 for (i = first, j = first; i < ctx->msgcount; i++) {
773 if (!ctx->hdrs[i]->deleted) {
774 ctx->hdrs[i]->offset = newOffset[i - first].hdr;
775 ctx->hdrs[i]->content->hdr_offset = newOffset[i - first].hdr;
776 ctx->hdrs[i]->content->offset = newOffset[i - first].body;
777 ctx->hdrs[i]->index = j++;
780 p_delete(&newOffset);
781 p_delete(&oldOffset);
782 unlink (tempfile); /* remove partial copy of the mailbox */
783 mutt_unblock_signals ();
785 return (0); /* signal success */
787 bail: /* Come here in case of disaster */
791 /* restore offsets, as far as they are valid */
792 if (first >= 0 && oldOffset) {
793 for (i = first; i < ctx->msgcount && oldOffset[i - first].valid; i++) {
794 ctx->hdrs[i]->offset = oldOffset[i - first].hdr;
795 ctx->hdrs[i]->content->hdr_offset = oldOffset[i - first].hdr;
796 ctx->hdrs[i]->content->offset = oldOffset[i - first].body;
797 ctx->hdrs[i]->lines = oldOffset[i - first].lines;
798 ctx->hdrs[i]->content->length = oldOffset[i - first].length;
802 /* this is ok to call even if we haven't locked anything */
803 mbox_unlock_mailbox (ctx);
805 mutt_unblock_signals ();
806 p_delete(&newOffset);
807 p_delete(&oldOffset);
809 if ((ctx->fp = freopen (ctx->path, "r", ctx->fp)) == NULL) {
810 mutt_error _("Could not reopen mailbox!");
812 mx_fastclose_mailbox (ctx);
817 /* if the mailbox was reopened, the thread tree will be invalid so make
818 * sure to start threading from scratch. */
819 mutt_sort_headers (ctx, (need_sort == M_REOPENED));
824 static int mbox_sync_mailbox (CONTEXT * ctx, int unused, int *index_hint) {
828 int rc = _mbox_sync_mailbox (ctx, unused, index_hint);
831 if ((tmp = buffy_find_mailbox (ctx->path)) && tmp->new == 0)
832 buffy_update_mailbox (tmp);
837 /* close a mailbox opened in write-mode */
838 int mbox_close_mailbox (CONTEXT * ctx)
840 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
842 if (ctx->compressinfo)
843 mutt_slow_close_compressed (ctx);
845 mutt_unblock_signals ();
846 mx_fastclose_mailbox (ctx);
850 static int mbox_reopen_mailbox (CONTEXT * ctx, int *index_hint)
852 int (*cmp_headers) (const HEADER *, const HEADER *) = NULL;
860 /* silent operations */
863 mutt_message _("Reopening mailbox...");
865 /* our heuristics require the old mailbox to be unsorted */
866 if (Sort != SORT_ORDER) {
871 mutt_sort_headers (ctx, 1);
878 /* simulate a close */
880 hash_destroy (&ctx->id_hash, NULL);
882 hash_destroy (&ctx->subj_hash, NULL);
883 mutt_clear_threads (ctx);
886 for (i = 0; i < ctx->msgcount; i++)
887 header_delete(&(ctx->hdrs[i])); /* nothing to do! */
888 p_delete(&ctx->hdrs);
891 /* save the old headers */
892 old_msgcount = ctx->msgcount;
893 old_hdrs = ctx->hdrs;
897 ctx->hdrmax = 0; /* force allocation of new headers */
907 ctx->subj_hash = NULL;
909 switch (ctx->magic) {
912 if (fseeko (ctx->fp, 0, SEEK_SET) != 0) {
916 cmp_headers = mutt_cmp_header;
917 if (ctx->magic == M_MBOX)
918 rc = mbox_parse_mailbox (ctx);
920 rc = mmdf_parse_mailbox (ctx);
930 /* free the old headers */
931 for (j = 0; j < old_msgcount; j++)
932 header_delete(&(old_hdrs[j]));
939 /* now try to recover the old flags */
941 index_hint_set = (index_hint == NULL);
943 if (!ctx->readonly) {
944 for (i = 0; i < ctx->msgcount; i++) {
947 /* some messages have been deleted, and new messages have been
948 * appended at the end; the heuristic is that old messages have then
949 * "advanced" towards the beginning of the folder, so we begin the
950 * search at index "i"
952 for (j = i; j < old_msgcount; j++) {
953 if (old_hdrs[j] == NULL)
955 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
961 for (j = 0; j < i && j < old_msgcount; j++) {
962 if (old_hdrs[j] == NULL)
964 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
972 /* this is best done here */
973 if (!index_hint_set && *index_hint == j)
976 if (old_hdrs[j]->changed) {
977 /* Only update the flags if the old header was changed;
978 * otherwise, the header may have been modified externally,
979 * and we don't want to lose _those_ changes
981 mutt_set_flag (ctx, ctx->hdrs[i], M_FLAG, old_hdrs[j]->flagged);
982 mutt_set_flag (ctx, ctx->hdrs[i], M_REPLIED, old_hdrs[j]->replied);
983 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, old_hdrs[j]->old);
984 mutt_set_flag (ctx, ctx->hdrs[i], M_READ, old_hdrs[j]->read);
986 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, old_hdrs[j]->deleted);
987 mutt_set_flag (ctx, ctx->hdrs[i], M_TAG, old_hdrs[j]->tagged);
989 /* we don't need this header any more */
990 header_delete(&(old_hdrs[j]));
994 /* free the remaining old headers */
995 for (j = 0; j < old_msgcount; j++) {
997 header_delete(&(old_hdrs[j]));
1001 p_delete(&old_hdrs);
1006 return ((ctx->changed || msg_mod) ? M_REOPENED : M_NEW_MAIL);
1011 * 1 if the mailbox is not empty
1012 * 0 if the mailbox is empty
1015 int mbox_check_empty (const char *path)
1019 if (stat (path, &st) == -1)
1022 return ((st.st_size == 0));
1025 int mbox_is_magic (const char* path, struct stat* st) {
1028 char tmp[_POSIX_PATH_MAX];
1030 if (S_ISDIR(st->st_mode))
1033 if (st->st_size == 0) {
1034 /* hard to tell what zero-length files are, so assume the default magic */
1035 if (DefaultMagic == M_MBOX || DefaultMagic == M_MMDF)
1036 return (DefaultMagic);
1040 else if ((f = fopen (path, "r")) != NULL) {
1042 struct utimbuf times;
1044 fgets (tmp, sizeof (tmp), f);
1045 if (m_strncmp("From ", tmp, 5) == 0)
1047 else if (m_strcmp(MMDF_SEP, tmp) == 0)
1051 /* need to restore the times here, the file was not really accessed,
1052 * only the type was accessed. This is important, because detection
1053 * of "new mail" depends on those times set correctly.
1055 times.actime = st->st_atime;
1056 times.modtime = st->st_mtime;
1057 utime (path, ×);
1061 return (-1); /* fopen failed */
1064 if (magic == -1 && mutt_can_read_compressed (path))
1065 return (M_COMPRESSED);
1069 static int commit_message (MESSAGE* msg, CONTEXT* ctx __attribute__ ((unused)), int mbox) {
1070 if ((mbox && fputc ('\n', msg->fp) == EOF) ||
1071 (!mbox && fputs (MMDF_SEP, msg->fp) == EOF))
1073 if ((fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1)) {
1074 mutt_perror (_("Can't write message"));
1080 static int mbox_commit_message (MESSAGE* msg, CONTEXT* ctx) {
1081 return (commit_message (msg, ctx, 1));
1084 static int mmdf_commit_message (MESSAGE* msg, CONTEXT* ctx) {
1085 return (commit_message (msg, ctx, 0));
1088 static mx_t* reg_mx (void) {
1089 mx_t* fmt = p_new(mx_t, 1);
1091 fmt->mx_check_empty = mbox_check_empty;
1092 fmt->mx_is_magic = mbox_is_magic;
1093 fmt->mx_access = access;
1094 fmt->mx_open_mailbox = mbox_open_mailbox;
1095 fmt->mx_open_new_message = mbox_open_new_message;
1096 fmt->mx_sync_mailbox = mbox_sync_mailbox;
1097 fmt->mx_check_mailbox = mbox_check_mailbox;
1101 mx_t* mbox_reg_mx (void) {
1102 mx_t* fmt = reg_mx ();
1104 fmt->mx_commit_message = mbox_commit_message;
1107 mx_t* mmdf_reg_mx (void) {
1108 mx_t* fmt = reg_mx ();
1110 fmt->mx_commit_message = mmdf_commit_message;