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 */
12 #include <lib-lib/lib-lib.h>
14 #include <lib-ui/curses.h>
15 #include <lib-sys/mutt_signal.h>
26 /* struct used by mutt_sync_mailbox() to store new offsets */
36 static int mbox_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr __attribute__ ((unused)))
43 static int mbox_reopen_mailbox (CONTEXT*, int*);
46 * ctx - context to lock
47 * excl - exclusive lock?
48 * retry - should retry if unable to lock?
50 int mbox_lock_mailbox (CONTEXT * ctx, int excl, int retry)
54 if ((r = mx_lock_file (ctx->path, fileno (ctx->fp), excl, 1, retry)) == 0)
56 else if (retry && !excl) {
64 static void mbox_unlock_mailbox (CONTEXT * ctx)
69 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
74 static int mmdf_parse_mailbox (CONTEXT * ctx)
76 char buf[HUGE_STRING];
77 char return_path[LONG_STRING];
78 int count = 0, oldmsgcount = ctx->msgcount;
85 if (stat (ctx->path, &sb) == -1) {
86 mutt_perror (ctx->path);
89 ctx->mtime = sb.st_mtime;
90 ctx->size = sb.st_size;
92 /* precompute the local timezone to speed up calculation of the
94 tz = mutt_local_tz (0);
96 buf[sizeof (buf) - 1] = 0;
99 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
102 if (m_strcmp(buf, MMDF_SEP) == 0) {
103 loc = ftello (ctx->fp);
106 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
107 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
108 (int) (loc / (ctx->size / 100 + 1)));
111 if (ctx->msgcount == ctx->hdrmax)
112 mx_alloc_memory (ctx);
113 ctx->hdrs[ctx->msgcount] = hdr = header_new();
115 hdr->index = ctx->msgcount;
117 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL) {
118 /* TODO: memory leak??? */
124 if (!is_from (buf, return_path, sizeof (return_path), &t)) {
125 if (fseeko (ctx->fp, loc, SEEK_SET) != 0) {
126 mutt_error _("Mailbox is corrupt!");
132 hdr->received = t - tz;
134 hdr->env = mutt_read_rfc822_header (ctx->fp, hdr, 0, 0);
136 loc = ftello (ctx->fp);
138 if (hdr->content->length > 0 && hdr->lines > 0) {
139 tmploc = loc + hdr->content->length;
141 if (0 < tmploc && tmploc < ctx->size) {
142 if (fseeko (ctx->fp, tmploc, SEEK_SET) != 0 ||
143 fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL ||
144 m_strcmp(MMDF_SEP, buf) != 0) {
145 fseeko (ctx->fp, loc, SEEK_SET);
146 hdr->content->length = -1;
150 hdr->content->length = -1;
153 hdr->content->length = -1;
155 if (hdr->content->length < 0) {
158 loc = ftello (ctx->fp);
159 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
162 } while (m_strcmp(buf, MMDF_SEP) != 0);
165 hdr->content->length = loc - hdr->content->offset;
168 if (!hdr->env->return_path && return_path[0])
169 hdr->env->return_path =
170 rfc822_parse_adrlist (hdr->env->return_path, return_path);
173 hdr->env->from = address_list_dup (hdr->env->return_path);
178 mutt_error _("Mailbox is corrupt!");
184 if (ctx->msgcount > oldmsgcount)
185 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
190 /* Note that this function is also called when new mail is appended to the
191 * currently open folder, and NOT just when the mailbox is initially read.
193 * NOTE: it is assumed that the mailbox being read has been locked before
194 * this routine gets called. Strange things could happen if it's not!
196 static int mbox_parse_mailbox (CONTEXT * ctx)
199 char buf[HUGE_STRING], return_path[STRING];
202 int count = 0, lines = 0;
205 /* Save information about the folder at the time we opened it. */
206 if (stat (ctx->path, &sb) == -1) {
207 mutt_perror (ctx->path);
211 ctx->size = sb.st_size;
212 ctx->mtime = sb.st_mtime;
215 ctx->readonly = access (ctx->path, W_OK) ? 1 : 0;
217 /* precompute the local timezone to speed up calculation of the
219 tz = mutt_local_tz (0);
221 loc = ftello (ctx->fp);
222 while (fgets (buf, sizeof (buf), ctx->fp) != NULL) {
223 if (is_from (buf, return_path, sizeof (return_path), &t)) {
224 /* Save the Content-Length of the previous message */
226 #define PREV ctx->hdrs[ctx->msgcount-1]
228 if (PREV->content->length < 0) {
229 PREV->content->length = loc - PREV->content->offset - 1;
230 if (PREV->content->length < 0)
231 PREV->content->length = 0;
234 PREV->lines = lines ? lines - 1 : 0;
239 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
240 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
241 (int) (ftello (ctx->fp) / (ctx->size / 100 + 1)));
243 if (ctx->msgcount == ctx->hdrmax)
244 mx_alloc_memory (ctx);
246 curhdr = ctx->hdrs[ctx->msgcount] = header_new();
247 curhdr->received = t - tz;
248 curhdr->offset = loc;
249 curhdr->index = ctx->msgcount;
251 curhdr->env = mutt_read_rfc822_header (ctx->fp, curhdr, 0, 0);
253 /* if we know how long this message is, either just skip over the body,
254 * or if we don't know how many lines there are, count them now (this will
255 * save time by not having to search for the next message marker).
257 if (curhdr->content->length > 0) {
260 loc = ftello (ctx->fp);
261 tmploc = loc + curhdr->content->length + 1;
263 if (0 < tmploc && tmploc < ctx->size) {
265 * check to see if the content-length looks valid. we expect to
266 * to see a valid message separator at this point in the stream
268 if (fseeko (ctx->fp, tmploc, SEEK_SET) != 0 ||
269 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
270 m_strncmp("From ", buf, 5) != 0) {
271 fseeko (ctx->fp, loc, SEEK_SET); /* nope, return the previous position */
272 curhdr->content->length = -1;
275 else if (tmploc != ctx->size) {
276 /* content-length would put us past the end of the file, so it
279 curhdr->content->length = -1;
282 if (curhdr->content->length != -1) {
283 /* good content-length. check to see if we know how many lines
284 * are in this message.
286 if (curhdr->lines == 0) {
287 int cl = curhdr->content->length;
289 /* count the number of lines in this message */
290 fseeko (ctx->fp, loc, SEEK_SET);
292 if (fgetc (ctx->fp) == '\n')
297 /* return to the offset of the next message separator */
298 fseeko(ctx->fp, tmploc, SEEK_SET);
304 if (!curhdr->env->return_path && return_path[0])
305 curhdr->env->return_path =
306 rfc822_parse_adrlist (curhdr->env->return_path, return_path);
308 if (!curhdr->env->from)
309 curhdr->env->from = address_list_dup (curhdr->env->return_path);
316 loc = ftello (ctx->fp);
320 * Only set the content-length of the previous message if we have read more
321 * than one message during _this_ invocation. If this routine is called
322 * when new mail is received, we need to make sure not to clobber what
323 * previously was the last message since the headers may be sorted.
326 if (PREV->content->length < 0) {
327 PREV->content->length = ftello (ctx->fp) - PREV->content->offset - 1;
328 if (PREV->content->length < 0)
329 PREV->content->length = 0;
333 PREV->lines = lines ? lines - 1 : 0;
335 mx_update_context (ctx, count);
343 /* open a mbox or mmdf style mailbox */
344 static int mbox_open_mailbox (CONTEXT * ctx)
348 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
349 mutt_perror (ctx->path);
352 mutt_block_signals ();
353 if (mbox_lock_mailbox (ctx, 0, 1) == -1) {
354 mutt_unblock_signals ();
358 if (ctx->magic == M_MBOX)
359 rc = mbox_parse_mailbox (ctx);
360 else if (ctx->magic == M_MMDF)
361 rc = mmdf_parse_mailbox (ctx);
365 mbox_unlock_mailbox (ctx);
366 mutt_unblock_signals ();
370 /* check to see if the mailbox has changed on disk.
373 * M_REOPENED mailbox has been reopened
374 * M_NEW_MAIL new mail has arrived!
375 * M_LOCKED couldn't lock the file
379 static int _mbox_check_mailbox (CONTEXT * ctx, int *index_hint)
382 char buffer[LONG_STRING];
386 if (stat (ctx->path, &st) == 0) {
387 if (st.st_mtime == ctx->mtime && st.st_size == ctx->size)
390 if (st.st_size == ctx->size) {
391 /* the file was touched, but it is still the same length, so just exit */
392 ctx->mtime = st.st_mtime;
396 if (st.st_size > ctx->size) {
397 /* lock the file if it isn't already */
399 mutt_block_signals ();
400 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
401 mutt_unblock_signals ();
402 /* we couldn't lock the mailbox, but nothing serious happened:
403 * probably the new mail arrived: no reason to wait till we can
404 * parse it: we'll get it on the next pass
412 * Check to make sure that the only change to the mailbox is that
413 * message(s) were appended to this file. My heuristic is that we should
414 * see the message separator at *exactly* what used to be the end of the
417 fseeko (ctx->fp, ctx->size, SEEK_SET);
418 if (fgets (buffer, sizeof (buffer), ctx->fp) != NULL) {
419 if ((ctx->magic == M_MBOX && m_strncmp("From ", buffer, 5) == 0)
420 || (ctx->magic == M_MMDF && m_strcmp(MMDF_SEP, buffer) == 0)) {
421 fseeko (ctx->fp, ctx->size, SEEK_SET);
422 if (ctx->magic == M_MBOX)
423 mbox_parse_mailbox (ctx);
425 mmdf_parse_mailbox (ctx);
427 /* Only unlock the folder if it was locked inside of this routine.
428 * It may have been locked elsewhere, like in
429 * mutt_checkpoint_mailbox().
433 mbox_unlock_mailbox (ctx);
434 mutt_unblock_signals ();
437 return (M_NEW_MAIL); /* signal that new mail arrived */
450 if (mbox_reopen_mailbox (ctx, index_hint) != -1) {
452 mbox_unlock_mailbox (ctx);
453 mutt_unblock_signals ();
461 mbox_unlock_mailbox (ctx);
462 mx_fastclose_mailbox (ctx);
463 mutt_unblock_signals ();
464 mutt_error _("Mailbox was corrupted!");
469 static int mbox_check_mailbox (CONTEXT* ctx, int* index_hint, int lock) {
473 mutt_block_signals ();
474 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
475 mutt_unblock_signals ();
480 rc = _mbox_check_mailbox (ctx, index_hint);
483 mutt_unblock_signals ();
484 mbox_unlock_mailbox (ctx);
493 static int mbox_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)), int *index_hint)
495 char tempfile[_POSIX_PATH_MAX];
497 int i, j, save_sort = SORT_ORDER;
499 int need_sort = 0; /* flag to resort mailbox if new mail arrives */
500 int first = -1; /* first message to be written */
501 off_t offset; /* location in mailbox to write changed messages */
503 struct utimbuf utimebuf;
504 struct m_update_t *newOffset = NULL;
505 struct m_update_t *oldOffset = NULL;
508 /* sort message by their position in the mailbox on disk */
509 if (Sort != SORT_ORDER) {
512 mutt_sort_headers (ctx, 0);
517 /* need to open the file for writing in such a way that it does not truncate
518 * the file, so use read-write mode.
520 if ((ctx->fp = freopen (ctx->path, "r+", ctx->fp)) == NULL) {
521 mx_fastclose_mailbox (ctx);
522 mutt_error _("Fatal error! Could not reopen mailbox!");
527 mutt_block_signals ();
529 if (mbox_lock_mailbox (ctx, 1, 1) == -1) {
530 mutt_unblock_signals ();
531 mutt_error _("Unable to lock mailbox!");
536 /* Check to make sure that the file hasn't changed on disk */
537 if ((i = _mbox_check_mailbox (ctx, index_hint)) == M_NEW_MAIL
538 || i == M_REOPENED) {
539 /* new mail arrived, or mailbox reopened */
548 /* Create a temporary file to write the new version of the mailbox in. */
549 mutt_mktemp (tempfile);
550 if ((i = open (tempfile, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1 ||
551 (fp = fdopen (i, "w")) == NULL) {
556 mutt_error _("Could not create temporary file!");
562 /* find the first deleted/changed message. we save a lot of time by only
563 * rewriting the mailbox from the point where it has actually changed.
565 for (i = 0; i < ctx->msgcount && !ctx->hdrs[i]->deleted &&
566 !ctx->hdrs[i]->changed && !ctx->hdrs[i]->attach_del; i++);
567 if (i == ctx->msgcount) {
568 /* this means ctx->changed or ctx->deleted was set, but no
569 * messages were found to be changed or deleted. This should
570 * never happen, is we presume it is a bug in mutt.
573 _("sync: mbox modified, but no modified messages! (report this bug)");
574 mutt_sleep (5); /* the mutt_error /will/ get cleared! */
579 /* save the index of the first changed/deleted message */
581 /* where to start overwriting */
582 offset = ctx->hdrs[i]->offset;
584 /* the offset stored in the header does not include the MMDF_SEP, so make
585 * sure we seek to the correct location
587 if (ctx->magic == M_MMDF)
588 offset -= (sizeof MMDF_SEP - 1);
590 /* allocate space for the new offsets */
591 newOffset = p_new(struct m_update_t, ctx->msgcount - first);
592 oldOffset = p_new(struct m_update_t, ctx->msgcount - first);
594 for (i = first, j = 0; i < ctx->msgcount; i++) {
596 * back up some information which is needed to restore offsets when
600 oldOffset[i - first].valid = 1;
601 oldOffset[i - first].hdr = ctx->hdrs[i]->offset;
602 oldOffset[i - first].body = ctx->hdrs[i]->content->offset;
603 oldOffset[i - first].lines = ctx->hdrs[i]->lines;
604 oldOffset[i - first].length = ctx->hdrs[i]->content->length;
606 if (!ctx->hdrs[i]->deleted) {
608 if (!ctx->quiet && WriteInc && ((i % WriteInc) == 0 || j == 1))
609 mutt_message (_("Writing messages... %d (%d%%)"), i,
610 (int) (ftello (ctx->fp) / (ctx->size / 100 + 1)));
612 if (ctx->magic == M_MMDF) {
613 if (fputs (MMDF_SEP, fp) == EOF) {
614 mutt_perror (tempfile);
622 /* save the new offset for this message. we add `offset' because the
623 * temporary file only contains saved message which are located after
624 * `offset' in the real mailbox
626 newOffset[i - first].hdr = ftello (fp) + offset;
628 if (mutt_copy_message
629 (fp, ctx, ctx->hdrs[i], M_CM_UPDATE,
630 CH_FROM | CH_UPDATE | CH_UPDATE_LEN) == -1) {
631 mutt_perror (tempfile);
637 /* Since messages could have been deleted, the offsets stored in memory
638 * will be wrong, so update what we can, which is the offset of this
639 * message, and the offset of the body. If this is a multipart message,
640 * we just flush the in memory cache so that the message will be reparsed
641 * if the user accesses it later.
643 newOffset[i - first].body =
644 ftello (fp) - ctx->hdrs[i]->content->length + offset;
645 body_list_wipe(&ctx->hdrs[i]->content->parts);
647 switch (ctx->magic) {
649 if (fputs (MMDF_SEP, fp) == EOF) {
650 mutt_perror (tempfile);
657 if (fputs ("\n", fp) == EOF) {
658 mutt_perror (tempfile);
667 if (fclose (fp) != 0) {
670 mutt_perror (tempfile);
676 /* Save the state of this folder. */
677 if (stat (ctx->path, &statbuf) == -1) {
678 mutt_perror (ctx->path);
684 if ((fp = fopen (tempfile, "r")) == NULL) {
685 mutt_unblock_signals ();
686 mx_fastclose_mailbox (ctx);
687 mutt_perror (tempfile);
692 if (fseeko (ctx->fp, offset, SEEK_SET) != 0 || /* seek the append location */
693 /* do a sanity check to make sure the mailbox looks ok */
694 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
695 (ctx->magic == M_MBOX && m_strncmp("From ", buf, 5) != 0) ||
696 (ctx->magic == M_MMDF && m_strcmp(MMDF_SEP, buf) != 0)) {
700 if (fseeko (ctx->fp, offset, SEEK_SET) != 0) { /* return to proper offset */
703 /* copy the temp mailbox back into place starting at the first
704 * change/deleted message
706 mutt_message _("Committing changes...");
708 i = mutt_copy_stream (fp, ctx->fp);
710 if (ferror (ctx->fp))
714 ctx->size = ftello (ctx->fp); /* update the size of the mailbox */
715 ftruncate (fileno (ctx->fp), ctx->size);
721 mbox_unlock_mailbox (ctx);
723 if (fclose (ctx->fp) != 0 || i == -1) {
724 /* error occured while writing the mailbox back, so keep the temp copy
728 char savefile[_POSIX_PATH_MAX];
730 snprintf (savefile, sizeof (savefile), "%s/mutt.%s-%s-%u",
731 NONULL (Tempdir), NONULL (Username), NONULL (Hostname),
732 (unsigned int) getpid ());
733 rename (tempfile, savefile);
734 mutt_unblock_signals ();
735 mx_fastclose_mailbox (ctx);
736 mutt_pretty_mailbox (savefile);
737 mutt_error (_("Write failed! Saved partial mailbox to %s"), savefile);
742 /* Restore the previous access/modification times */
743 utimebuf.actime = statbuf.st_atime;
744 utimebuf.modtime = statbuf.st_mtime;
745 utime (ctx->path, &utimebuf);
747 /* reopen the mailbox in read-only mode */
748 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
750 mutt_unblock_signals ();
751 mx_fastclose_mailbox (ctx);
752 mutt_error _("Fatal error! Could not reopen mailbox!");
756 /* update the offsets of the rewritten messages */
757 for (i = first, j = first; i < ctx->msgcount; i++) {
758 if (!ctx->hdrs[i]->deleted) {
759 ctx->hdrs[i]->offset = newOffset[i - first].hdr;
760 ctx->hdrs[i]->content->hdr_offset = newOffset[i - first].hdr;
761 ctx->hdrs[i]->content->offset = newOffset[i - first].body;
762 ctx->hdrs[i]->index = j++;
765 p_delete(&newOffset);
766 p_delete(&oldOffset);
767 unlink (tempfile); /* remove partial copy of the mailbox */
768 mutt_unblock_signals ();
770 return (0); /* signal success */
772 bail: /* Come here in case of disaster */
776 /* restore offsets, as far as they are valid */
777 if (first >= 0 && oldOffset) {
778 for (i = first; i < ctx->msgcount && oldOffset[i - first].valid; i++) {
779 ctx->hdrs[i]->offset = oldOffset[i - first].hdr;
780 ctx->hdrs[i]->content->hdr_offset = oldOffset[i - first].hdr;
781 ctx->hdrs[i]->content->offset = oldOffset[i - first].body;
782 ctx->hdrs[i]->lines = oldOffset[i - first].lines;
783 ctx->hdrs[i]->content->length = oldOffset[i - first].length;
787 /* this is ok to call even if we haven't locked anything */
788 mbox_unlock_mailbox (ctx);
790 mutt_unblock_signals ();
791 p_delete(&newOffset);
792 p_delete(&oldOffset);
794 if ((ctx->fp = freopen (ctx->path, "r", ctx->fp)) == NULL) {
795 mutt_error _("Could not reopen mailbox!");
797 mx_fastclose_mailbox (ctx);
802 /* if the mailbox was reopened, the thread tree will be invalid so make
803 * sure to start threading from scratch. */
804 mutt_sort_headers (ctx, (need_sort == M_REOPENED));
809 /* close a mailbox opened in write-mode */
810 int mbox_close_mailbox (CONTEXT * ctx)
812 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
814 if (ctx->compressinfo)
815 mutt_slow_close_compressed (ctx);
817 mutt_unblock_signals ();
818 mx_fastclose_mailbox (ctx);
822 static int mbox_reopen_mailbox (CONTEXT * ctx, int *index_hint)
824 int (*cmp_headers) (const HEADER *, const HEADER *) = NULL;
832 /* silent operations */
835 mutt_message _("Reopening mailbox...");
837 /* our heuristics require the old mailbox to be unsorted */
838 if (Sort != SORT_ORDER) {
843 mutt_sort_headers (ctx, 1);
850 /* simulate a close */
852 hash_destroy (&ctx->id_hash, NULL);
854 hash_destroy (&ctx->subj_hash, NULL);
855 mutt_clear_threads (ctx);
858 for (i = 0; i < ctx->msgcount; i++)
859 header_delete(&(ctx->hdrs[i])); /* nothing to do! */
860 p_delete(&ctx->hdrs);
863 /* save the old headers */
864 old_msgcount = ctx->msgcount;
865 old_hdrs = ctx->hdrs;
869 ctx->hdrmax = 0; /* force allocation of new headers */
879 ctx->subj_hash = NULL;
881 switch (ctx->magic) {
884 if (fseeko (ctx->fp, 0, SEEK_SET) != 0) {
888 cmp_headers = mutt_cmp_header;
889 if (ctx->magic == M_MBOX)
890 rc = mbox_parse_mailbox (ctx);
892 rc = mmdf_parse_mailbox (ctx);
902 /* free the old headers */
903 for (j = 0; j < old_msgcount; j++)
904 header_delete(&(old_hdrs[j]));
911 /* now try to recover the old flags */
913 index_hint_set = (index_hint == NULL);
915 if (!ctx->readonly) {
916 for (i = 0; i < ctx->msgcount; i++) {
919 /* some messages have been deleted, and new messages have been
920 * appended at the end; the heuristic is that old messages have then
921 * "advanced" towards the beginning of the folder, so we begin the
922 * search at index "i"
924 for (j = i; j < old_msgcount; j++) {
925 if (old_hdrs[j] == NULL)
927 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
933 for (j = 0; j < i && j < old_msgcount; j++) {
934 if (old_hdrs[j] == NULL)
936 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
944 /* this is best done here */
945 if (!index_hint_set && *index_hint == j)
948 if (old_hdrs[j]->changed) {
949 /* Only update the flags if the old header was changed;
950 * otherwise, the header may have been modified externally,
951 * and we don't want to lose _those_ changes
953 mutt_set_flag (ctx, ctx->hdrs[i], M_FLAG, old_hdrs[j]->flagged);
954 mutt_set_flag (ctx, ctx->hdrs[i], M_REPLIED, old_hdrs[j]->replied);
955 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, old_hdrs[j]->old);
956 mutt_set_flag (ctx, ctx->hdrs[i], M_READ, old_hdrs[j]->read);
958 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, old_hdrs[j]->deleted);
959 mutt_set_flag (ctx, ctx->hdrs[i], M_TAG, old_hdrs[j]->tagged);
961 /* we don't need this header any more */
962 header_delete(&(old_hdrs[j]));
966 /* free the remaining old headers */
967 for (j = 0; j < old_msgcount; j++) {
969 header_delete(&(old_hdrs[j]));
978 return ((ctx->changed || msg_mod) ? M_REOPENED : M_NEW_MAIL);
983 * 1 if the mailbox is not empty
984 * 0 if the mailbox is empty
987 int mbox_check_empty (const char *path)
991 if (stat (path, &st) == -1)
994 return ((st.st_size == 0));
997 int mbox_is_magic (const char* path, struct stat* st) {
1000 char tmp[_POSIX_PATH_MAX];
1002 if (S_ISDIR(st->st_mode))
1005 if (st->st_size == 0) {
1006 /* hard to tell what zero-length files are, so assume the default magic */
1007 if (DefaultMagic == M_MBOX || DefaultMagic == M_MMDF)
1008 return (DefaultMagic);
1012 else if ((f = fopen (path, "r")) != NULL) {
1013 struct utimbuf times;
1015 fgets (tmp, sizeof (tmp), f);
1016 if (m_strncmp("From ", tmp, 5) == 0)
1018 else if (m_strcmp(MMDF_SEP, tmp) == 0)
1022 /* need to restore the times here, the file was not really accessed,
1023 * only the type was accessed. This is important, because detection
1024 * of "new mail" depends on those times set correctly.
1026 times.actime = st->st_atime;
1027 times.modtime = st->st_mtime;
1028 utime (path, ×);
1031 return (-1); /* fopen failed */
1034 if (magic == -1 && mutt_can_read_compressed (path))
1035 return (M_COMPRESSED);
1039 static int commit_message (MESSAGE* msg, CONTEXT* ctx __attribute__ ((unused)), int mbox) {
1040 if ((mbox && fputc ('\n', msg->fp) == EOF) ||
1041 (!mbox && fputs (MMDF_SEP, msg->fp) == EOF))
1043 if ((fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1)) {
1044 mutt_perror (_("Can't write message"));
1050 static int mbox_commit_message (MESSAGE* msg, CONTEXT* ctx) {
1051 return (commit_message (msg, ctx, 1));
1054 static int mmdf_commit_message (MESSAGE* msg, CONTEXT* ctx) {
1055 return (commit_message (msg, ctx, 0));
1058 mx_t const mbox_mx = {
1065 mbox_open_new_message,
1070 mbox_commit_message,
1073 mx_t const mmdf_mx = {
1080 mbox_open_new_message,
1085 mmdf_commit_message,