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 /* return 1 if address lists are strictly identical */
421 static int strict_addrcmp (const ADDRESS * a, const ADDRESS * b)
424 if (mutt_strcmp (a->mailbox, b->mailbox) ||
425 mutt_strcmp (a->personal, b->personal))
437 static int strict_cmp_lists (const LIST * a, const LIST * b)
440 if (mutt_strcmp (a->data, b->data))
452 static int strict_cmp_envelopes (const ENVELOPE * e1, const ENVELOPE * e2)
455 if (mutt_strcmp (e1->message_id, e2->message_id) ||
456 mutt_strcmp (e1->subject, e2->subject) ||
457 !strict_cmp_lists (e1->references, e2->references) ||
458 !strict_addrcmp (e1->from, e2->from) ||
459 !strict_addrcmp (e1->sender, e2->sender) ||
460 !strict_addrcmp (e1->reply_to, e2->reply_to) ||
461 !strict_addrcmp (e1->to, e2->to) ||
462 !strict_addrcmp (e1->cc, e2->cc) ||
463 !strict_addrcmp (e1->return_path, e2->return_path))
469 if (e1 == NULL && e2 == NULL)
476 static int strict_cmp_parameters (const PARAMETER * p1, const PARAMETER * p2)
479 if (mutt_strcmp (p1->attribute, p2->attribute) ||
480 mutt_strcmp (p1->value, p2->value))
492 static int strict_cmp_bodies (const BODY * b1, const BODY * b2)
494 if (b1->type != b2->type ||
495 b1->encoding != b2->encoding ||
496 mutt_strcmp (b1->subtype, b2->subtype) ||
497 mutt_strcmp (b1->description, b2->description) ||
498 !strict_cmp_parameters (b1->parameter, b2->parameter) ||
499 b1->length != b2->length)
504 /* return 1 if headers are strictly identical */
505 int mbox_strict_cmp_headers (const HEADER * h1, const HEADER * h2)
508 if (h1->received != h2->received ||
509 h1->date_sent != h2->date_sent ||
510 h1->content->length != h2->content->length ||
511 h1->lines != h2->lines ||
512 h1->zhours != h2->zhours ||
513 h1->zminutes != h2->zminutes ||
514 h1->zoccident != h2->zoccident ||
515 h1->mime != h2->mime ||
516 !strict_cmp_envelopes (h1->env, h2->env) ||
517 !strict_cmp_bodies (h1->content, h2->content))
523 if (h1 == NULL && h2 == NULL)
530 /* check to see if the mailbox has changed on disk.
533 * M_REOPENED mailbox has been reopened
534 * M_NEW_MAIL new mail has arrived!
535 * M_LOCKED couldn't lock the file
539 static int _mbox_check_mailbox (CONTEXT * ctx, int *index_hint)
542 char buffer[LONG_STRING];
546 if (stat (ctx->path, &st) == 0) {
547 if (st.st_mtime == ctx->mtime && st.st_size == ctx->size)
550 if (st.st_size == ctx->size) {
551 /* the file was touched, but it is still the same length, so just exit */
552 ctx->mtime = st.st_mtime;
556 if (st.st_size > ctx->size) {
557 /* lock the file if it isn't already */
559 mutt_block_signals ();
560 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
561 mutt_unblock_signals ();
562 /* we couldn't lock the mailbox, but nothing serious happened:
563 * probably the new mail arrived: no reason to wait till we can
564 * parse it: we'll get it on the next pass
572 * Check to make sure that the only change to the mailbox is that
573 * message(s) were appended to this file. My heuristic is that we should
574 * see the message separator at *exactly* what used to be the end of the
577 if (fseek (ctx->fp, ctx->size, SEEK_SET) != 0)
578 debug_print (1, ("fseek() failed\n"));
579 if (fgets (buffer, sizeof (buffer), ctx->fp) != NULL) {
580 if ((ctx->magic == M_MBOX && safe_strncmp ("From ", buffer, 5) == 0)
581 || (ctx->magic == M_MMDF && mutt_strcmp (MMDF_SEP, buffer) == 0)) {
582 if (fseek (ctx->fp, ctx->size, SEEK_SET) != 0)
583 debug_print (1, ("fseek() failed\n"));
584 if (ctx->magic == M_MBOX)
585 mbox_parse_mailbox (ctx);
587 mmdf_parse_mailbox (ctx);
589 /* Only unlock the folder if it was locked inside of this routine.
590 * It may have been locked elsewhere, like in
591 * mutt_checkpoint_mailbox().
595 mbox_unlock_mailbox (ctx);
596 mutt_unblock_signals ();
599 return (M_NEW_MAIL); /* signal that new mail arrived */
605 debug_print (1, ("fgets returned NULL.\n"));
614 if (mbox_reopen_mailbox (ctx, index_hint) != -1) {
616 mbox_unlock_mailbox (ctx);
617 mutt_unblock_signals ();
625 mbox_unlock_mailbox (ctx);
626 mx_fastclose_mailbox (ctx);
627 mutt_unblock_signals ();
628 mutt_error _("Mailbox was corrupted!");
633 static int mbox_check_mailbox (CONTEXT* ctx, int* index_hint, int lock) {
637 mutt_block_signals ();
638 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
639 mutt_unblock_signals ();
644 rc = _mbox_check_mailbox (ctx, index_hint);
647 mutt_unblock_signals ();
648 mbox_unlock_mailbox (ctx);
657 static int _mbox_sync_mailbox (CONTEXT * ctx, int unused, int *index_hint)
659 char tempfile[_POSIX_PATH_MAX];
661 int i, j, save_sort = SORT_ORDER;
663 int need_sort = 0; /* flag to resort mailbox if new mail arrives */
664 int first = -1; /* first message to be written */
665 long offset; /* location in mailbox to write changed messages */
667 struct utimbuf utimebuf;
668 struct m_update_t *newOffset = NULL;
669 struct m_update_t *oldOffset = NULL;
672 /* sort message by their position in the mailbox on disk */
673 if (Sort != SORT_ORDER) {
676 mutt_sort_headers (ctx, 0);
679 /* need to open the file for writing in such a way that it does not truncate
680 * the file, so use read-write mode.
682 if ((ctx->fp = freopen (ctx->path, "r+", ctx->fp)) == NULL) {
683 mx_fastclose_mailbox (ctx);
684 mutt_error _("Fatal error! Could not reopen mailbox!");
689 mutt_block_signals ();
691 if (mbox_lock_mailbox (ctx, 1, 1) == -1) {
692 mutt_unblock_signals ();
693 mutt_error _("Unable to lock mailbox!");
698 /* Check to make sure that the file hasn't changed on disk */
699 if ((i = _mbox_check_mailbox (ctx, index_hint)) == M_NEW_MAIL
700 || i == M_REOPENED) {
701 /* new mail arrived, or mailbox reopened */
712 /* Create a temporary file to write the new version of the mailbox in. */
713 mutt_mktemp (tempfile);
714 if ((i = open (tempfile, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1 ||
715 (fp = fdopen (i, "w")) == NULL) {
720 mutt_error _("Could not create temporary file!");
726 /* find the first deleted/changed message. we save a lot of time by only
727 * rewriting the mailbox from the point where it has actually changed.
729 for (i = 0; i < ctx->msgcount && !ctx->hdrs[i]->deleted &&
730 !ctx->hdrs[i]->changed && !ctx->hdrs[i]->attach_del; i++);
731 if (i == ctx->msgcount) {
732 /* this means ctx->changed or ctx->deleted was set, but no
733 * messages were found to be changed or deleted. This should
734 * never happen, is we presume it is a bug in mutt.
737 _("sync: mbox modified, but no modified messages! (report this bug)");
738 mutt_sleep (5); /* the mutt_error /will/ get cleared! */
739 debug_print (1, ("no modified messages.\n"));
744 /* save the index of the first changed/deleted message */
746 /* where to start overwriting */
747 offset = ctx->hdrs[i]->offset;
749 /* the offset stored in the header does not include the MMDF_SEP, so make
750 * sure we seek to the correct location
752 if (ctx->magic == M_MMDF)
753 offset -= (sizeof MMDF_SEP - 1);
755 /* allocate space for the new offsets */
756 newOffset = safe_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
757 oldOffset = safe_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
759 for (i = first, j = 0; i < ctx->msgcount; i++) {
761 * back up some information which is needed to restore offsets when
765 oldOffset[i - first].valid = 1;
766 oldOffset[i - first].hdr = ctx->hdrs[i]->offset;
767 oldOffset[i - first].body = ctx->hdrs[i]->content->offset;
768 oldOffset[i - first].lines = ctx->hdrs[i]->lines;
769 oldOffset[i - first].length = ctx->hdrs[i]->content->length;
771 if (!ctx->hdrs[i]->deleted) {
773 if (!ctx->quiet && WriteInc && ((i % WriteInc) == 0 || j == 1))
774 mutt_message (_("Writing messages... %d (%d%%)"), i,
775 (int) (ftell (ctx->fp) / (ctx->size / 100 + 1)));
777 if (ctx->magic == M_MMDF) {
778 if (fputs (MMDF_SEP, fp) == EOF) {
779 mutt_perror (tempfile);
787 /* save the new offset for this message. we add `offset' because the
788 * temporary file only contains saved message which are located after
789 * `offset' in the real mailbox
791 newOffset[i - first].hdr = ftell (fp) + offset;
793 if (mutt_copy_message
794 (fp, ctx, ctx->hdrs[i], M_CM_UPDATE,
795 CH_FROM | CH_UPDATE | CH_UPDATE_LEN) == -1) {
796 mutt_perror (tempfile);
802 /* Since messages could have been deleted, the offsets stored in memory
803 * will be wrong, so update what we can, which is the offset of this
804 * message, and the offset of the body. If this is a multipart message,
805 * we just flush the in memory cache so that the message will be reparsed
806 * if the user accesses it later.
808 newOffset[i - first].body =
809 ftell (fp) - ctx->hdrs[i]->content->length + offset;
810 mutt_free_body (&ctx->hdrs[i]->content->parts);
812 switch (ctx->magic) {
814 if (fputs (MMDF_SEP, fp) == EOF) {
815 mutt_perror (tempfile);
822 if (fputs ("\n", fp) == EOF) {
823 mutt_perror (tempfile);
832 if (fclose (fp) != 0) {
834 debug_print (1, ("fclose() returned non-zero.\n"));
836 mutt_perror (tempfile);
842 /* Save the state of this folder. */
843 if (stat (ctx->path, &statbuf) == -1) {
844 mutt_perror (ctx->path);
850 if ((fp = fopen (tempfile, "r")) == NULL) {
851 mutt_unblock_signals ();
852 mx_fastclose_mailbox (ctx);
853 debug_print (1, ("unable to reopen temp copy of mailbox!\n"));
854 mutt_perror (tempfile);
859 if (fseek (ctx->fp, offset, SEEK_SET) != 0 || /* seek the append location */
860 /* do a sanity check to make sure the mailbox looks ok */
861 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
862 (ctx->magic == M_MBOX && safe_strncmp ("From ", buf, 5) != 0) ||
863 (ctx->magic == M_MMDF && mutt_strcmp (MMDF_SEP, buf) != 0)) {
864 debug_print (1, ("message not in expected position.\n"));
865 debug_print (1, ("LINE: %s\n", buf));
869 if (fseek (ctx->fp, offset, SEEK_SET) != 0) { /* return to proper offset */
871 debug_print (1, ("fseek() failed\n"));
874 /* copy the temp mailbox back into place starting at the first
875 * change/deleted message
877 mutt_message _("Committing changes...");
879 i = mutt_copy_stream (fp, ctx->fp);
881 if (ferror (ctx->fp))
885 ctx->size = ftell (ctx->fp); /* update the size of the mailbox */
886 ftruncate (fileno (ctx->fp), ctx->size);
892 mbox_unlock_mailbox (ctx);
894 if (fclose (ctx->fp) != 0 || i == -1) {
895 /* error occured while writing the mailbox back, so keep the temp copy
899 char savefile[_POSIX_PATH_MAX];
901 snprintf (savefile, sizeof (savefile), "%s/mutt.%s-%s-%u",
902 NONULL (Tempdir), NONULL (Username), NONULL (Hostname),
903 (unsigned int) getpid ());
904 rename (tempfile, savefile);
905 mutt_unblock_signals ();
906 mx_fastclose_mailbox (ctx);
907 mutt_pretty_mailbox (savefile);
908 mutt_error (_("Write failed! Saved partial mailbox to %s"), savefile);
913 /* Restore the previous access/modification times */
914 utimebuf.actime = statbuf.st_atime;
915 utimebuf.modtime = statbuf.st_mtime;
916 utime (ctx->path, &utimebuf);
918 /* reopen the mailbox in read-only mode */
919 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
921 mutt_unblock_signals ();
922 mx_fastclose_mailbox (ctx);
923 mutt_error _("Fatal error! Could not reopen mailbox!");
929 /* update the offsets of the rewritten messages */
930 for (i = first, j = first; i < ctx->msgcount; i++) {
931 if (!ctx->hdrs[i]->deleted) {
932 ctx->hdrs[i]->offset = newOffset[i - first].hdr;
933 ctx->hdrs[i]->content->hdr_offset = newOffset[i - first].hdr;
934 ctx->hdrs[i]->content->offset = newOffset[i - first].body;
935 ctx->hdrs[i]->index = j++;
940 unlink (tempfile); /* remove partial copy of the mailbox */
941 mutt_unblock_signals ();
942 Sort = save_sort; /* Restore the default value. */
944 return (0); /* signal success */
946 bail: /* Come here in case of disaster */
950 /* restore offsets, as far as they are valid */
951 if (first >= 0 && oldOffset) {
952 for (i = first; i < ctx->msgcount && oldOffset[i - first].valid; i++) {
953 ctx->hdrs[i]->offset = oldOffset[i - first].hdr;
954 ctx->hdrs[i]->content->hdr_offset = oldOffset[i - first].hdr;
955 ctx->hdrs[i]->content->offset = oldOffset[i - first].body;
956 ctx->hdrs[i]->lines = oldOffset[i - first].lines;
957 ctx->hdrs[i]->content->length = oldOffset[i - first].length;
961 /* this is ok to call even if we haven't locked anything */
962 mbox_unlock_mailbox (ctx);
964 mutt_unblock_signals ();
968 if ((ctx->fp = freopen (ctx->path, "r", ctx->fp)) == NULL) {
969 mutt_error _("Could not reopen mailbox!");
971 mx_fastclose_mailbox (ctx);
975 if (need_sort || save_sort != Sort) {
977 /* if the mailbox was reopened, the thread tree will be invalid so make
978 * sure to start threading from scratch. */
979 mutt_sort_headers (ctx, (need_sort == M_REOPENED));
985 static int mbox_sync_mailbox (CONTEXT * ctx, int unused, int *index_hint) {
989 int rc = _mbox_sync_mailbox (ctx, unused, index_hint);
992 if ((tmp = buffy_find_mailbox (ctx->path)) && tmp->new == 0)
993 buffy_update_mailbox (tmp);
998 /* close a mailbox opened in write-mode */
999 int mbox_close_mailbox (CONTEXT * ctx)
1001 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
1003 #ifdef USE_COMPRESSED
1004 if (ctx->compressinfo)
1005 mutt_slow_close_compressed (ctx);
1008 mutt_unblock_signals ();
1009 mx_fastclose_mailbox (ctx);
1013 static int mbox_reopen_mailbox (CONTEXT * ctx, int *index_hint)
1015 int (*cmp_headers) (const HEADER *, const HEADER *) = NULL;
1023 /* silent operations */
1026 mutt_message _("Reopening mailbox...");
1028 /* our heuristics require the old mailbox to be unsorted */
1029 if (Sort != SORT_ORDER) {
1034 mutt_sort_headers (ctx, 1);
1041 /* simulate a close */
1043 hash_destroy (&ctx->id_hash, NULL);
1045 hash_destroy (&ctx->subj_hash, NULL);
1046 mutt_clear_threads (ctx);
1048 if (ctx->readonly) {
1049 for (i = 0; i < ctx->msgcount; i++)
1050 mutt_free_header (&(ctx->hdrs[i])); /* nothing to do! */
1054 /* save the old headers */
1055 old_msgcount = ctx->msgcount;
1056 old_hdrs = ctx->hdrs;
1060 ctx->hdrmax = 0; /* force allocation of new headers */
1069 ctx->id_hash = NULL;
1070 ctx->subj_hash = NULL;
1072 switch (ctx->magic) {
1075 if (fseek (ctx->fp, 0, SEEK_SET) != 0) {
1076 debug_print (1, ("fseek() failed\n"));
1080 cmp_headers = mbox_strict_cmp_headers;
1081 if (ctx->magic == M_MBOX)
1082 rc = mbox_parse_mailbox (ctx);
1084 rc = mmdf_parse_mailbox (ctx);
1094 /* free the old headers */
1095 for (j = 0; j < old_msgcount; j++)
1096 mutt_free_header (&(old_hdrs[j]));
1103 /* now try to recover the old flags */
1105 index_hint_set = (index_hint == NULL);
1107 if (!ctx->readonly) {
1108 for (i = 0; i < ctx->msgcount; i++) {
1111 /* some messages have been deleted, and new messages have been
1112 * appended at the end; the heuristic is that old messages have then
1113 * "advanced" towards the beginning of the folder, so we begin the
1114 * search at index "i"
1116 for (j = i; j < old_msgcount; j++) {
1117 if (old_hdrs[j] == NULL)
1119 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
1125 for (j = 0; j < i && j < old_msgcount; j++) {
1126 if (old_hdrs[j] == NULL)
1128 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
1136 /* this is best done here */
1137 if (!index_hint_set && *index_hint == j)
1140 if (old_hdrs[j]->changed) {
1141 /* Only update the flags if the old header was changed;
1142 * otherwise, the header may have been modified externally,
1143 * and we don't want to lose _those_ changes
1145 mutt_set_flag (ctx, ctx->hdrs[i], M_FLAG, old_hdrs[j]->flagged);
1146 mutt_set_flag (ctx, ctx->hdrs[i], M_REPLIED, old_hdrs[j]->replied);
1147 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, old_hdrs[j]->old);
1148 mutt_set_flag (ctx, ctx->hdrs[i], M_READ, old_hdrs[j]->read);
1150 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, old_hdrs[j]->deleted);
1151 mutt_set_flag (ctx, ctx->hdrs[i], M_TAG, old_hdrs[j]->tagged);
1153 /* we don't need this header any more */
1154 mutt_free_header (&(old_hdrs[j]));
1158 /* free the remaining old headers */
1159 for (j = 0; j < old_msgcount; j++) {
1161 mutt_free_header (&(old_hdrs[j]));
1170 return ((ctx->changed || msg_mod) ? M_REOPENED : M_NEW_MAIL);
1175 * 1 if the mailbox is not empty
1176 * 0 if the mailbox is empty
1179 int mbox_check_empty (const char *path)
1183 if (stat (path, &st) == -1)
1186 return ((st.st_size == 0));
1189 int mbox_is_magic (const char* path, struct stat* st) {
1192 char tmp[_POSIX_PATH_MAX];
1194 if (S_ISDIR(st->st_mode))
1197 if (st->st_size == 0) {
1198 /* hard to tell what zero-length files are, so assume the default magic */
1199 if (DefaultMagic == M_MBOX || DefaultMagic == M_MMDF)
1200 return (DefaultMagic);
1204 else if ((f = fopen (path, "r")) != NULL) {
1206 struct utimbuf times;
1208 fgets (tmp, sizeof (tmp), f);
1209 if (safe_strncmp ("From ", tmp, 5) == 0)
1211 else if (mutt_strcmp (MMDF_SEP, tmp) == 0)
1215 /* need to restore the times here, the file was not really accessed,
1216 * only the type was accessed. This is important, because detection
1217 * of "new mail" depends on those times set correctly.
1219 times.actime = st->st_atime;
1220 times.modtime = st->st_mtime;
1221 utime (path, ×);
1225 return (-1); /* fopen failed */
1228 #ifdef USE_COMPRESSED
1229 if (magic == -1 && mutt_can_read_compressed (path))
1230 return (M_COMPRESSED);
1235 static mx_t* reg_mx (void) {
1236 mx_t* fmt = safe_calloc (1, sizeof (mx_t));
1238 fmt->mx_check_empty = mbox_check_empty;
1239 fmt->mx_is_magic = mbox_is_magic;
1240 fmt->mx_access = access;
1241 fmt->mx_open_mailbox = mbox_open_mailbox;
1242 fmt->mx_open_new_message = mbox_open_new_message;
1243 fmt->mx_sync_mailbox = mbox_sync_mailbox;
1244 fmt->mx_check_mailbox = mbox_check_mailbox;
1248 mx_t* mbox_reg_mx (void) {
1249 mx_t* fmt = reg_mx ();
1253 mx_t* mmdf_reg_mx (void) {
1254 mx_t* fmt = reg_mx ();