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 */
39 /* struct used by mutt_sync_mailbox() to store new offsets */
49 int mbox_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
56 * ctx - context to lock
57 * excl - exclusive lock?
58 * retry - should retry if unable to lock?
60 int mbox_lock_mailbox (CONTEXT * ctx, int excl, int retry)
64 if ((r = mx_lock_file (ctx->path, fileno (ctx->fp), excl, 1, retry)) == 0)
66 else if (retry && !excl) {
74 void mbox_unlock_mailbox (CONTEXT * ctx)
79 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
84 int mmdf_parse_mailbox (CONTEXT * ctx)
86 char buf[HUGE_STRING];
87 char return_path[LONG_STRING];
88 int count = 0, oldmsgcount = ctx->msgcount;
95 #ifdef NFS_ATTRIBUTE_HACK
96 struct utimbuf newtime;
99 if (stat (ctx->path, &sb) == -1) {
100 mutt_perror (ctx->path);
103 ctx->mtime = sb.st_mtime;
104 ctx->size = sb.st_size;
106 #ifdef NFS_ATTRIBUTE_HACK
107 if (sb.st_mtime > sb.st_atime) {
108 newtime.modtime = sb.st_mtime;
109 newtime.actime = time (NULL);
110 utime (ctx->path, &newtime);
114 /* precompute the local timezone to speed up calculation of the
116 tz = mutt_local_tz (0);
118 buf[sizeof (buf) - 1] = 0;
121 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
124 if (safe_strcmp (buf, MMDF_SEP) == 0) {
125 loc = ftell (ctx->fp);
128 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
129 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
130 (int) (loc / (ctx->size / 100 + 1)));
133 if (ctx->msgcount == ctx->hdrmax)
134 mx_alloc_memory (ctx);
135 ctx->hdrs[ctx->msgcount] = hdr = mutt_new_header ();
137 hdr->index = ctx->msgcount;
139 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL) {
140 /* TODO: memory leak??? */
141 dprint (1, (debugfile, "mmdf_parse_mailbox: unexpected EOF\n"));
147 if (!is_from (buf, return_path, sizeof (return_path), &t)) {
148 if (fseek (ctx->fp, loc, SEEK_SET) != 0) {
149 dprint (1, (debugfile, "mmdf_parse_mailbox: fseek() failed\n"));
150 mutt_error _("Mailbox is corrupt!");
156 hdr->received = t - tz;
158 hdr->env = mutt_read_rfc822_header (ctx->fp, hdr, 0, 0);
160 loc = ftell (ctx->fp);
162 if (hdr->content->length > 0 && hdr->lines > 0) {
163 tmploc = loc + hdr->content->length;
165 if (0 < tmploc && tmploc < ctx->size) {
166 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0 ||
167 fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL ||
168 safe_strcmp (MMDF_SEP, buf) != 0) {
169 if (fseek (ctx->fp, loc, SEEK_SET) != 0)
170 dprint (1, (debugfile, "mmdf_parse_mailbox: fseek() failed\n"));
171 hdr->content->length = -1;
175 hdr->content->length = -1;
178 hdr->content->length = -1;
180 if (hdr->content->length < 0) {
183 loc = ftell (ctx->fp);
184 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
187 } while (safe_strcmp (buf, MMDF_SEP) != 0);
190 hdr->content->length = loc - hdr->content->offset;
193 if (!hdr->env->return_path && return_path[0])
194 hdr->env->return_path =
195 rfc822_parse_adrlist (hdr->env->return_path, return_path);
198 hdr->env->from = rfc822_cpy_adr (hdr->env->return_path);
203 dprint (1, (debugfile, "mmdf_parse_mailbox: corrupt mailbox!\n"));
204 mutt_error _("Mailbox is corrupt!");
210 if (ctx->msgcount > oldmsgcount)
211 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
216 /* Note that this function is also called when new mail is appended to the
217 * currently open folder, and NOT just when the mailbox is initially read.
219 * NOTE: it is assumed that the mailbox being read has been locked before
220 * this routine gets called. Strange things could happen if it's not!
222 int mbox_parse_mailbox (CONTEXT * ctx)
225 char buf[HUGE_STRING], return_path[STRING];
228 int count = 0, lines = 0;
231 #ifdef NFS_ATTRIBUTE_HACK
232 struct utimbuf newtime;
235 /* Save information about the folder at the time we opened it. */
236 if (stat (ctx->path, &sb) == -1) {
237 mutt_perror (ctx->path);
241 ctx->size = sb.st_size;
242 ctx->mtime = sb.st_mtime;
244 #ifdef NFS_ATTRIBUTE_HACK
245 if (sb.st_mtime > sb.st_atime) {
246 newtime.modtime = sb.st_mtime;
247 newtime.actime = time (NULL);
248 utime (ctx->path, &newtime);
253 ctx->readonly = access (ctx->path, W_OK) ? 1 : 0;
255 /* precompute the local timezone to speed up calculation of the
257 tz = mutt_local_tz (0);
259 loc = ftell (ctx->fp);
260 while (fgets (buf, sizeof (buf), ctx->fp) != NULL) {
261 if (is_from (buf, return_path, sizeof (return_path), &t)) {
262 /* Save the Content-Length of the previous message */
264 #define PREV ctx->hdrs[ctx->msgcount-1]
266 if (PREV->content->length < 0) {
267 PREV->content->length = loc - PREV->content->offset - 1;
268 if (PREV->content->length < 0)
269 PREV->content->length = 0;
272 PREV->lines = lines ? lines - 1 : 0;
277 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
278 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
279 (int) (ftell (ctx->fp) / (ctx->size / 100 + 1)));
281 if (ctx->msgcount == ctx->hdrmax)
282 mx_alloc_memory (ctx);
284 curhdr = ctx->hdrs[ctx->msgcount] = mutt_new_header ();
285 curhdr->received = t - tz;
286 curhdr->offset = loc;
287 curhdr->index = ctx->msgcount;
289 curhdr->env = mutt_read_rfc822_header (ctx->fp, curhdr, 0, 0);
291 /* if we know how long this message is, either just skip over the body,
292 * or if we don't know how many lines there are, count them now (this will
293 * save time by not having to search for the next message marker).
295 if (curhdr->content->length > 0) {
298 loc = ftell (ctx->fp);
299 tmploc = loc + curhdr->content->length + 1;
301 if (0 < tmploc && tmploc < ctx->size) {
303 * check to see if the content-length looks valid. we expect to
304 * to see a valid message separator at this point in the stream
306 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0 ||
307 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
308 safe_strncmp ("From ", buf, 5) != 0) {
311 "mbox_parse_mailbox: bad content-length in message %d (cl=%ld)\n",
312 curhdr->index, curhdr->content->length));
313 dprint (1, (debugfile, "\tLINE: %s", buf));
314 if (fseek (ctx->fp, loc, SEEK_SET) != 0) { /* nope, return the previous position */
315 dprint (1, (debugfile, "mbox_parse_mailbox: fseek() failed\n"));
317 curhdr->content->length = -1;
320 else if (tmploc != ctx->size) {
321 /* content-length would put us past the end of the file, so it
324 curhdr->content->length = -1;
327 if (curhdr->content->length != -1) {
328 /* good content-length. check to see if we know how many lines
329 * are in this message.
331 if (curhdr->lines == 0) {
332 int cl = curhdr->content->length;
334 /* count the number of lines in this message */
335 if (fseek (ctx->fp, loc, SEEK_SET) != 0)
336 dprint (1, (debugfile, "mbox_parse_mailbox: fseek() failed\n"));
338 if (fgetc (ctx->fp) == '\n')
343 /* return to the offset of the next message separator */
344 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0)
345 dprint (1, (debugfile, "mbox_parse_mailbox: fseek() failed\n"));
351 if (!curhdr->env->return_path && return_path[0])
352 curhdr->env->return_path =
353 rfc822_parse_adrlist (curhdr->env->return_path, return_path);
355 if (!curhdr->env->from)
356 curhdr->env->from = rfc822_cpy_adr (curhdr->env->return_path);
363 loc = ftell (ctx->fp);
367 * Only set the content-length of the previous message if we have read more
368 * than one message during _this_ invocation. If this routine is called
369 * when new mail is received, we need to make sure not to clobber what
370 * previously was the last message since the headers may be sorted.
373 if (PREV->content->length < 0) {
374 PREV->content->length = ftell (ctx->fp) - PREV->content->offset - 1;
375 if (PREV->content->length < 0)
376 PREV->content->length = 0;
380 PREV->lines = lines ? lines - 1 : 0;
382 mx_update_context (ctx, count);
390 /* open a mbox or mmdf style mailbox */
391 int mbox_open_mailbox (CONTEXT * ctx)
395 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
396 mutt_perror (ctx->path);
399 mutt_block_signals ();
400 if (mbox_lock_mailbox (ctx, 0, 1) == -1) {
401 mutt_unblock_signals ();
405 if (ctx->magic == M_MBOX)
406 rc = mbox_parse_mailbox (ctx);
407 else if (ctx->magic == M_MMDF)
408 rc = mmdf_parse_mailbox (ctx);
412 mbox_unlock_mailbox (ctx);
413 mutt_unblock_signals ();
417 /* return 1 if address lists are strictly identical */
418 static int strict_addrcmp (const ADDRESS * a, const ADDRESS * b)
421 if (safe_strcmp (a->mailbox, b->mailbox) ||
422 safe_strcmp (a->personal, b->personal))
434 static int strict_cmp_lists (const LIST * a, const LIST * b)
437 if (safe_strcmp (a->data, b->data))
449 static int strict_cmp_envelopes (const ENVELOPE * e1, const ENVELOPE * e2)
452 if (safe_strcmp (e1->message_id, e2->message_id) ||
453 safe_strcmp (e1->subject, e2->subject) ||
454 !strict_cmp_lists (e1->references, e2->references) ||
455 !strict_addrcmp (e1->from, e2->from) ||
456 !strict_addrcmp (e1->sender, e2->sender) ||
457 !strict_addrcmp (e1->reply_to, e2->reply_to) ||
458 !strict_addrcmp (e1->to, e2->to) ||
459 !strict_addrcmp (e1->cc, e2->cc) ||
460 !strict_addrcmp (e1->return_path, e2->return_path))
466 if (e1 == NULL && e2 == NULL)
473 static int strict_cmp_parameters (const PARAMETER * p1, const PARAMETER * p2)
476 if (safe_strcmp (p1->attribute, p2->attribute) ||
477 safe_strcmp (p1->value, p2->value))
489 static int strict_cmp_bodies (const BODY * b1, const BODY * b2)
491 if (b1->type != b2->type ||
492 b1->encoding != b2->encoding ||
493 safe_strcmp (b1->subtype, b2->subtype) ||
494 safe_strcmp (b1->description, b2->description) ||
495 !strict_cmp_parameters (b1->parameter, b2->parameter) ||
496 b1->length != b2->length)
501 /* return 1 if headers are strictly identical */
502 int mbox_strict_cmp_headers (const HEADER * h1, const HEADER * h2)
505 if (h1->received != h2->received ||
506 h1->date_sent != h2->date_sent ||
507 h1->content->length != h2->content->length ||
508 h1->lines != h2->lines ||
509 h1->zhours != h2->zhours ||
510 h1->zminutes != h2->zminutes ||
511 h1->zoccident != h2->zoccident ||
512 h1->mime != h2->mime ||
513 !strict_cmp_envelopes (h1->env, h2->env) ||
514 !strict_cmp_bodies (h1->content, h2->content))
520 if (h1 == NULL && h2 == NULL)
527 /* check to see if the mailbox has changed on disk.
530 * M_REOPENED mailbox has been reopened
531 * M_NEW_MAIL new mail has arrived!
532 * M_LOCKED couldn't lock the file
536 int mbox_check_mailbox (CONTEXT * ctx, int *index_hint)
539 char buffer[LONG_STRING];
543 if (stat (ctx->path, &st) == 0) {
544 if (st.st_mtime == ctx->mtime && st.st_size == ctx->size)
547 if (st.st_size == ctx->size) {
548 /* the file was touched, but it is still the same length, so just exit */
549 ctx->mtime = st.st_mtime;
553 if (st.st_size > ctx->size) {
554 /* lock the file if it isn't already */
556 mutt_block_signals ();
557 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
558 mutt_unblock_signals ();
559 /* we couldn't lock the mailbox, but nothing serious happened:
560 * probably the new mail arrived: no reason to wait till we can
561 * parse it: we'll get it on the next pass
569 * Check to make sure that the only change to the mailbox is that
570 * message(s) were appended to this file. My heuristic is that we should
571 * see the message separator at *exactly* what used to be the end of the
574 if (fseek (ctx->fp, ctx->size, SEEK_SET) != 0)
575 dprint (1, (debugfile, "mbox_check_mailbox: fseek() failed\n"));
576 if (fgets (buffer, sizeof (buffer), ctx->fp) != NULL) {
577 if ((ctx->magic == M_MBOX && safe_strncmp ("From ", buffer, 5) == 0)
578 || (ctx->magic == M_MMDF && safe_strcmp (MMDF_SEP, buffer) == 0)) {
579 if (fseek (ctx->fp, ctx->size, SEEK_SET) != 0)
580 dprint (1, (debugfile, "mbox_check_mailbox: fseek() failed\n"));
581 if (ctx->magic == M_MBOX)
582 mbox_parse_mailbox (ctx);
584 mmdf_parse_mailbox (ctx);
586 /* Only unlock the folder if it was locked inside of this routine.
587 * It may have been locked elsewhere, like in
588 * mutt_checkpoint_mailbox().
592 mbox_unlock_mailbox (ctx);
593 mutt_unblock_signals ();
596 return (M_NEW_MAIL); /* signal that new mail arrived */
602 dprint (1, (debugfile, "mbox_check_mailbox: fgets returned NULL.\n"));
611 if (mbox_reopen_mailbox (ctx, index_hint) != -1) {
613 mbox_unlock_mailbox (ctx);
614 mutt_unblock_signals ();
622 mbox_unlock_mailbox (ctx);
623 mx_fastclose_mailbox (ctx);
624 mutt_unblock_signals ();
625 mutt_error _("Mailbox was corrupted!");
634 int mbox_sync_mailbox (CONTEXT * ctx, int *index_hint)
636 char tempfile[_POSIX_PATH_MAX];
638 int i, j, save_sort = SORT_ORDER;
640 int need_sort = 0; /* flag to resort mailbox if new mail arrives */
641 int first = -1; /* first message to be written */
642 long offset; /* location in mailbox to write changed messages */
644 struct utimbuf utimebuf;
645 struct m_update_t *newOffset = NULL;
646 struct m_update_t *oldOffset = NULL;
649 /* sort message by their position in the mailbox on disk */
650 if (Sort != SORT_ORDER) {
653 mutt_sort_headers (ctx, 0);
656 /* need to open the file for writing in such a way that it does not truncate
657 * the file, so use read-write mode.
659 if ((ctx->fp = freopen (ctx->path, "r+", ctx->fp)) == NULL) {
660 mx_fastclose_mailbox (ctx);
661 mutt_error _("Fatal error! Could not reopen mailbox!");
666 mutt_block_signals ();
668 if (mbox_lock_mailbox (ctx, 1, 1) == -1) {
669 mutt_unblock_signals ();
670 mutt_error _("Unable to lock mailbox!");
675 /* Check to make sure that the file hasn't changed on disk */
676 if ((i = mbox_check_mailbox (ctx, index_hint)) == M_NEW_MAIL
677 || i == M_REOPENED) {
678 /* new mail arrived, or mailbox reopened */
689 /* Create a temporary file to write the new version of the mailbox in. */
690 mutt_mktemp (tempfile);
691 if ((i = open (tempfile, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1 ||
692 (fp = fdopen (i, "w")) == NULL) {
697 mutt_error _("Could not create temporary file!");
703 /* find the first deleted/changed message. we save a lot of time by only
704 * rewriting the mailbox from the point where it has actually changed.
706 for (i = 0; i < ctx->msgcount && !ctx->hdrs[i]->deleted &&
707 !ctx->hdrs[i]->changed && !ctx->hdrs[i]->attach_del; i++);
708 if (i == ctx->msgcount) {
709 /* this means ctx->changed or ctx->deleted was set, but no
710 * messages were found to be changed or deleted. This should
711 * never happen, is we presume it is a bug in mutt.
714 _("sync: mbox modified, but no modified messages! (report this bug)");
715 mutt_sleep (5); /* the mutt_error /will/ get cleared! */
716 dprint (1, (debugfile, "mbox_sync_mailbox(): no modified messages.\n"));
721 /* save the index of the first changed/deleted message */
723 /* where to start overwriting */
724 offset = ctx->hdrs[i]->offset;
726 /* the offset stored in the header does not include the MMDF_SEP, so make
727 * sure we seek to the correct location
729 if (ctx->magic == M_MMDF)
730 offset -= (sizeof MMDF_SEP - 1);
732 /* allocate space for the new offsets */
733 newOffset = safe_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
734 oldOffset = safe_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
736 for (i = first, j = 0; i < ctx->msgcount; i++) {
738 * back up some information which is needed to restore offsets when
742 oldOffset[i - first].valid = 1;
743 oldOffset[i - first].hdr = ctx->hdrs[i]->offset;
744 oldOffset[i - first].body = ctx->hdrs[i]->content->offset;
745 oldOffset[i - first].lines = ctx->hdrs[i]->lines;
746 oldOffset[i - first].length = ctx->hdrs[i]->content->length;
748 if (!ctx->hdrs[i]->deleted) {
750 if (!ctx->quiet && WriteInc && ((i % WriteInc) == 0 || j == 1))
751 mutt_message (_("Writing messages... %d (%d%%)"), i,
752 (int) (ftell (ctx->fp) / (ctx->size / 100 + 1)));
754 if (ctx->magic == M_MMDF) {
755 if (fputs (MMDF_SEP, fp) == EOF) {
756 mutt_perror (tempfile);
764 /* save the new offset for this message. we add `offset' because the
765 * temporary file only contains saved message which are located after
766 * `offset' in the real mailbox
768 newOffset[i - first].hdr = ftell (fp) + offset;
770 if (mutt_copy_message
771 (fp, ctx, ctx->hdrs[i], M_CM_UPDATE,
772 CH_FROM | CH_UPDATE | CH_UPDATE_LEN) == -1) {
773 mutt_perror (tempfile);
779 /* Since messages could have been deleted, the offsets stored in memory
780 * will be wrong, so update what we can, which is the offset of this
781 * message, and the offset of the body. If this is a multipart message,
782 * we just flush the in memory cache so that the message will be reparsed
783 * if the user accesses it later.
785 newOffset[i - first].body =
786 ftell (fp) - ctx->hdrs[i]->content->length + offset;
787 mutt_free_body (&ctx->hdrs[i]->content->parts);
789 switch (ctx->magic) {
791 if (fputs (MMDF_SEP, fp) == EOF) {
792 mutt_perror (tempfile);
799 if (fputs ("\n", fp) == EOF) {
800 mutt_perror (tempfile);
809 if (fclose (fp) != 0) {
812 (debugfile, "mbox_sync_mailbox: fclose() returned non-zero.\n"));
814 mutt_perror (tempfile);
820 /* Save the state of this folder. */
821 if (stat (ctx->path, &statbuf) == -1) {
822 mutt_perror (ctx->path);
828 if ((fp = fopen (tempfile, "r")) == NULL) {
829 mutt_unblock_signals ();
830 mx_fastclose_mailbox (ctx);
833 "mbox_sync_mailbox: unable to reopen temp copy of mailbox!\n"));
834 mutt_perror (tempfile);
839 if (fseek (ctx->fp, offset, SEEK_SET) != 0 || /* seek the append location */
840 /* do a sanity check to make sure the mailbox looks ok */
841 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
842 (ctx->magic == M_MBOX && safe_strncmp ("From ", buf, 5) != 0) ||
843 (ctx->magic == M_MMDF && safe_strcmp (MMDF_SEP, buf) != 0)) {
846 "mbox_sync_mailbox: message not in expected position."));
847 dprint (1, (debugfile, "\tLINE: %s\n", buf));
851 if (fseek (ctx->fp, offset, SEEK_SET) != 0) { /* return to proper offset */
853 dprint (1, (debugfile, "mbox_sync_mailbox: fseek() failed\n"));
856 /* copy the temp mailbox back into place starting at the first
857 * change/deleted message
859 mutt_message _("Committing changes...");
861 i = mutt_copy_stream (fp, ctx->fp);
863 if (ferror (ctx->fp))
867 ctx->size = ftell (ctx->fp); /* update the size of the mailbox */
868 ftruncate (fileno (ctx->fp), ctx->size);
874 mbox_unlock_mailbox (ctx);
876 if (fclose (ctx->fp) != 0 || i == -1) {
877 /* error occured while writing the mailbox back, so keep the temp copy
881 char savefile[_POSIX_PATH_MAX];
883 snprintf (savefile, sizeof (savefile), "%s/mutt.%s-%s-%u",
884 NONULL (Tempdir), NONULL (Username), NONULL (Hostname),
885 (unsigned int) getpid ());
886 rename (tempfile, savefile);
887 mutt_unblock_signals ();
888 mx_fastclose_mailbox (ctx);
889 mutt_pretty_mailbox (savefile);
890 mutt_error (_("Write failed! Saved partial mailbox to %s"), savefile);
895 /* Restore the previous access/modification times */
896 utimebuf.actime = statbuf.st_atime;
897 utimebuf.modtime = statbuf.st_mtime;
898 utime (ctx->path, &utimebuf);
900 /* reopen the mailbox in read-only mode */
901 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
903 mutt_unblock_signals ();
904 mx_fastclose_mailbox (ctx);
905 mutt_error _("Fatal error! Could not reopen mailbox!");
911 /* update the offsets of the rewritten messages */
912 for (i = first, j = first; i < ctx->msgcount; i++) {
913 if (!ctx->hdrs[i]->deleted) {
914 ctx->hdrs[i]->offset = newOffset[i - first].hdr;
915 ctx->hdrs[i]->content->hdr_offset = newOffset[i - first].hdr;
916 ctx->hdrs[i]->content->offset = newOffset[i - first].body;
917 ctx->hdrs[i]->index = j++;
922 unlink (tempfile); /* remove partial copy of the mailbox */
923 mutt_unblock_signals ();
924 Sort = save_sort; /* Restore the default value. */
926 return (0); /* signal success */
928 bail: /* Come here in case of disaster */
932 /* restore offsets, as far as they are valid */
933 if (first >= 0 && oldOffset) {
934 for (i = first; i < ctx->msgcount && oldOffset[i - first].valid; i++) {
935 ctx->hdrs[i]->offset = oldOffset[i - first].hdr;
936 ctx->hdrs[i]->content->hdr_offset = oldOffset[i - first].hdr;
937 ctx->hdrs[i]->content->offset = oldOffset[i - first].body;
938 ctx->hdrs[i]->lines = oldOffset[i - first].lines;
939 ctx->hdrs[i]->content->length = oldOffset[i - first].length;
943 /* this is ok to call even if we haven't locked anything */
944 mbox_unlock_mailbox (ctx);
946 mutt_unblock_signals ();
950 if ((ctx->fp = freopen (ctx->path, "r", ctx->fp)) == NULL) {
951 mutt_error _("Could not reopen mailbox!");
953 mx_fastclose_mailbox (ctx);
957 if (need_sort || save_sort != Sort) {
959 /* if the mailbox was reopened, the thread tree will be invalid so make
960 * sure to start threading from scratch. */
961 mutt_sort_headers (ctx, (need_sort == M_REOPENED));
967 /* close a mailbox opened in write-mode */
968 int mbox_close_mailbox (CONTEXT * ctx)
970 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
972 #ifdef USE_COMPRESSED
973 if (ctx->compressinfo)
974 mutt_slow_close_compressed (ctx);
977 mutt_unblock_signals ();
978 mx_fastclose_mailbox (ctx);
982 int mbox_reopen_mailbox (CONTEXT * ctx, int *index_hint)
984 int (*cmp_headers) (const HEADER *, const HEADER *) = NULL;
992 /* silent operations */
995 mutt_message _("Reopening mailbox...");
997 /* our heuristics require the old mailbox to be unsorted */
998 if (Sort != SORT_ORDER) {
1003 mutt_sort_headers (ctx, 1);
1010 /* simulate a close */
1012 hash_destroy (&ctx->id_hash, NULL);
1014 hash_destroy (&ctx->subj_hash, NULL);
1015 mutt_clear_threads (ctx);
1017 if (ctx->readonly) {
1018 for (i = 0; i < ctx->msgcount; i++)
1019 mutt_free_header (&(ctx->hdrs[i])); /* nothing to do! */
1023 /* save the old headers */
1024 old_msgcount = ctx->msgcount;
1025 old_hdrs = ctx->hdrs;
1029 ctx->hdrmax = 0; /* force allocation of new headers */
1038 ctx->id_hash = NULL;
1039 ctx->subj_hash = NULL;
1041 switch (ctx->magic) {
1044 if (fseek (ctx->fp, 0, SEEK_SET) != 0) {
1045 dprint (1, (debugfile, "mbox_reopen_mailbox: fseek() failed\n"));
1049 cmp_headers = mbox_strict_cmp_headers;
1050 if (ctx->magic == M_MBOX)
1051 rc = mbox_parse_mailbox (ctx);
1053 rc = mmdf_parse_mailbox (ctx);
1063 /* free the old headers */
1064 for (j = 0; j < old_msgcount; j++)
1065 mutt_free_header (&(old_hdrs[j]));
1072 /* now try to recover the old flags */
1074 index_hint_set = (index_hint == NULL);
1076 if (!ctx->readonly) {
1077 for (i = 0; i < ctx->msgcount; i++) {
1080 /* some messages have been deleted, and new messages have been
1081 * appended at the end; the heuristic is that old messages have then
1082 * "advanced" towards the beginning of the folder, so we begin the
1083 * search at index "i"
1085 for (j = i; j < old_msgcount; j++) {
1086 if (old_hdrs[j] == NULL)
1088 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
1094 for (j = 0; j < i && j < old_msgcount; j++) {
1095 if (old_hdrs[j] == NULL)
1097 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
1105 /* this is best done here */
1106 if (!index_hint_set && *index_hint == j)
1109 if (old_hdrs[j]->changed) {
1110 /* Only update the flags if the old header was changed;
1111 * otherwise, the header may have been modified externally,
1112 * and we don't want to lose _those_ changes
1114 mutt_set_flag (ctx, ctx->hdrs[i], M_FLAG, old_hdrs[j]->flagged);
1115 mutt_set_flag (ctx, ctx->hdrs[i], M_REPLIED, old_hdrs[j]->replied);
1116 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, old_hdrs[j]->old);
1117 mutt_set_flag (ctx, ctx->hdrs[i], M_READ, old_hdrs[j]->read);
1119 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, old_hdrs[j]->deleted);
1120 mutt_set_flag (ctx, ctx->hdrs[i], M_TAG, old_hdrs[j]->tagged);
1122 /* we don't need this header any more */
1123 mutt_free_header (&(old_hdrs[j]));
1127 /* free the remaining old headers */
1128 for (j = 0; j < old_msgcount; j++) {
1130 mutt_free_header (&(old_hdrs[j]));
1139 return ((ctx->changed || msg_mod) ? M_REOPENED : M_NEW_MAIL);
1144 * 1 if the mailbox is not empty
1145 * 0 if the mailbox is empty
1148 int mbox_check_empty (const char *path)
1152 if (stat (path, &st) == -1)
1155 return ((st.st_size == 0));
1158 int mbox_is_magic (const char* path, struct stat* st) {
1161 char tmp[_POSIX_PATH_MAX];
1163 if (S_ISDIR(st->st_mode))
1166 if (st->st_size == 0) {
1167 /* hard to tell what zero-length files are, so assume the default magic */
1168 if (DefaultMagic == M_MBOX || DefaultMagic == M_MMDF)
1169 return (DefaultMagic);
1173 else if ((f = fopen (path, "r")) != NULL) {
1175 struct utimbuf times;
1177 fgets (tmp, sizeof (tmp), f);
1178 if (safe_strncmp ("From ", tmp, 5) == 0)
1180 else if (safe_strcmp (MMDF_SEP, tmp) == 0)
1184 /* need to restore the times here, the file was not really accessed,
1185 * only the type was accessed. This is important, because detection
1186 * of "new mail" depends on those times set correctly.
1188 times.actime = st->st_atime;
1189 times.modtime = st->st_mtime;
1190 utime (path, ×);
1194 return (-1); /* fopen failed */
1197 #ifdef USE_COMPRESSED
1198 if (magic == -1 && mutt_can_read_compressed (path))
1199 return (M_COMPRESSED);
1204 static mx_t* reg_mx (void) {
1205 mx_t* fmt = safe_calloc (1, sizeof (mx_t));
1207 fmt->mx_check_empty = mbox_check_empty;
1208 fmt->mx_is_magic = mbox_is_magic;
1209 fmt->mx_access = access;
1210 fmt->mx_open_mailbox = mbox_open_mailbox;
1214 mx_t* mbox_reg_mx (void) {
1215 mx_t* fmt = reg_mx ();
1219 mx_t* mmdf_reg_mx (void) {
1220 mx_t* fmt = reg_mx ();