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 */
40 /* struct used by mutt_sync_mailbox() to store new offsets */
50 static int mbox_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
57 static int mbox_reopen_mailbox (CONTEXT*, int*);
60 * ctx - context to lock
61 * excl - exclusive lock?
62 * retry - should retry if unable to lock?
64 int mbox_lock_mailbox (CONTEXT * ctx, int excl, int retry)
68 if ((r = mx_lock_file (ctx->path, fileno (ctx->fp), excl, 1, retry)) == 0)
70 else if (retry && !excl) {
78 void mbox_unlock_mailbox (CONTEXT * ctx)
83 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
88 static int mmdf_parse_mailbox (CONTEXT * ctx)
90 char buf[HUGE_STRING];
91 char return_path[LONG_STRING];
92 int count = 0, oldmsgcount = ctx->msgcount;
99 #ifdef NFS_ATTRIBUTE_HACK
100 struct utimbuf newtime;
103 if (stat (ctx->path, &sb) == -1) {
104 mutt_perror (ctx->path);
107 ctx->mtime = sb.st_mtime;
108 ctx->size = sb.st_size;
110 #ifdef NFS_ATTRIBUTE_HACK
111 if (sb.st_mtime > sb.st_atime) {
112 newtime.modtime = sb.st_mtime;
113 newtime.actime = time (NULL);
114 utime (ctx->path, &newtime);
118 /* precompute the local timezone to speed up calculation of the
120 tz = mutt_local_tz (0);
122 buf[sizeof (buf) - 1] = 0;
125 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
128 if (safe_strcmp (buf, MMDF_SEP) == 0) {
129 loc = ftell (ctx->fp);
132 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
133 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
134 (int) (loc / (ctx->size / 100 + 1)));
137 if (ctx->msgcount == ctx->hdrmax)
138 mx_alloc_memory (ctx);
139 ctx->hdrs[ctx->msgcount] = hdr = mutt_new_header ();
141 hdr->index = ctx->msgcount;
143 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL) {
144 /* TODO: memory leak??? */
145 dprint (1, (debugfile, "mmdf_parse_mailbox: unexpected EOF\n"));
151 if (!is_from (buf, return_path, sizeof (return_path), &t)) {
152 if (fseek (ctx->fp, loc, SEEK_SET) != 0) {
153 dprint (1, (debugfile, "mmdf_parse_mailbox: fseek() failed\n"));
154 mutt_error _("Mailbox is corrupt!");
160 hdr->received = t - tz;
162 hdr->env = mutt_read_rfc822_header (ctx->fp, hdr, 0, 0);
164 loc = ftell (ctx->fp);
166 if (hdr->content->length > 0 && hdr->lines > 0) {
167 tmploc = loc + hdr->content->length;
169 if (0 < tmploc && tmploc < ctx->size) {
170 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0 ||
171 fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL ||
172 safe_strcmp (MMDF_SEP, buf) != 0) {
173 if (fseek (ctx->fp, loc, SEEK_SET) != 0)
174 dprint (1, (debugfile, "mmdf_parse_mailbox: fseek() failed\n"));
175 hdr->content->length = -1;
179 hdr->content->length = -1;
182 hdr->content->length = -1;
184 if (hdr->content->length < 0) {
187 loc = ftell (ctx->fp);
188 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
191 } while (safe_strcmp (buf, MMDF_SEP) != 0);
194 hdr->content->length = loc - hdr->content->offset;
197 if (!hdr->env->return_path && return_path[0])
198 hdr->env->return_path =
199 rfc822_parse_adrlist (hdr->env->return_path, return_path);
202 hdr->env->from = rfc822_cpy_adr (hdr->env->return_path);
207 dprint (1, (debugfile, "mmdf_parse_mailbox: corrupt mailbox!\n"));
208 mutt_error _("Mailbox is corrupt!");
214 if (ctx->msgcount > oldmsgcount)
215 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
220 /* Note that this function is also called when new mail is appended to the
221 * currently open folder, and NOT just when the mailbox is initially read.
223 * NOTE: it is assumed that the mailbox being read has been locked before
224 * this routine gets called. Strange things could happen if it's not!
226 static int mbox_parse_mailbox (CONTEXT * ctx)
229 char buf[HUGE_STRING], return_path[STRING];
232 int count = 0, lines = 0;
235 #ifdef NFS_ATTRIBUTE_HACK
236 struct utimbuf newtime;
239 /* Save information about the folder at the time we opened it. */
240 if (stat (ctx->path, &sb) == -1) {
241 mutt_perror (ctx->path);
245 ctx->size = sb.st_size;
246 ctx->mtime = sb.st_mtime;
248 #ifdef NFS_ATTRIBUTE_HACK
249 if (sb.st_mtime > sb.st_atime) {
250 newtime.modtime = sb.st_mtime;
251 newtime.actime = time (NULL);
252 utime (ctx->path, &newtime);
257 ctx->readonly = access (ctx->path, W_OK) ? 1 : 0;
259 /* precompute the local timezone to speed up calculation of the
261 tz = mutt_local_tz (0);
263 loc = ftell (ctx->fp);
264 while (fgets (buf, sizeof (buf), ctx->fp) != NULL) {
265 if (is_from (buf, return_path, sizeof (return_path), &t)) {
266 /* Save the Content-Length of the previous message */
268 #define PREV ctx->hdrs[ctx->msgcount-1]
270 if (PREV->content->length < 0) {
271 PREV->content->length = loc - PREV->content->offset - 1;
272 if (PREV->content->length < 0)
273 PREV->content->length = 0;
276 PREV->lines = lines ? lines - 1 : 0;
281 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
282 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
283 (int) (ftell (ctx->fp) / (ctx->size / 100 + 1)));
285 if (ctx->msgcount == ctx->hdrmax)
286 mx_alloc_memory (ctx);
288 curhdr = ctx->hdrs[ctx->msgcount] = mutt_new_header ();
289 curhdr->received = t - tz;
290 curhdr->offset = loc;
291 curhdr->index = ctx->msgcount;
293 curhdr->env = mutt_read_rfc822_header (ctx->fp, curhdr, 0, 0);
295 /* if we know how long this message is, either just skip over the body,
296 * or if we don't know how many lines there are, count them now (this will
297 * save time by not having to search for the next message marker).
299 if (curhdr->content->length > 0) {
302 loc = ftell (ctx->fp);
303 tmploc = loc + curhdr->content->length + 1;
305 if (0 < tmploc && tmploc < ctx->size) {
307 * check to see if the content-length looks valid. we expect to
308 * to see a valid message separator at this point in the stream
310 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0 ||
311 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
312 safe_strncmp ("From ", buf, 5) != 0) {
315 "mbox_parse_mailbox: bad content-length in message %d (cl=%ld)\n",
316 curhdr->index, curhdr->content->length));
317 dprint (1, (debugfile, "\tLINE: %s", buf));
318 if (fseek (ctx->fp, loc, SEEK_SET) != 0) { /* nope, return the previous position */
319 dprint (1, (debugfile, "mbox_parse_mailbox: fseek() failed\n"));
321 curhdr->content->length = -1;
324 else if (tmploc != ctx->size) {
325 /* content-length would put us past the end of the file, so it
328 curhdr->content->length = -1;
331 if (curhdr->content->length != -1) {
332 /* good content-length. check to see if we know how many lines
333 * are in this message.
335 if (curhdr->lines == 0) {
336 int cl = curhdr->content->length;
338 /* count the number of lines in this message */
339 if (fseek (ctx->fp, loc, SEEK_SET) != 0)
340 dprint (1, (debugfile, "mbox_parse_mailbox: fseek() failed\n"));
342 if (fgetc (ctx->fp) == '\n')
347 /* return to the offset of the next message separator */
348 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0)
349 dprint (1, (debugfile, "mbox_parse_mailbox: fseek() failed\n"));
355 if (!curhdr->env->return_path && return_path[0])
356 curhdr->env->return_path =
357 rfc822_parse_adrlist (curhdr->env->return_path, return_path);
359 if (!curhdr->env->from)
360 curhdr->env->from = rfc822_cpy_adr (curhdr->env->return_path);
367 loc = ftell (ctx->fp);
371 * Only set the content-length of the previous message if we have read more
372 * than one message during _this_ invocation. If this routine is called
373 * when new mail is received, we need to make sure not to clobber what
374 * previously was the last message since the headers may be sorted.
377 if (PREV->content->length < 0) {
378 PREV->content->length = ftell (ctx->fp) - PREV->content->offset - 1;
379 if (PREV->content->length < 0)
380 PREV->content->length = 0;
384 PREV->lines = lines ? lines - 1 : 0;
386 mx_update_context (ctx, count);
394 /* open a mbox or mmdf style mailbox */
395 static int mbox_open_mailbox (CONTEXT * ctx)
399 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
400 mutt_perror (ctx->path);
403 mutt_block_signals ();
404 if (mbox_lock_mailbox (ctx, 0, 1) == -1) {
405 mutt_unblock_signals ();
409 if (ctx->magic == M_MBOX)
410 rc = mbox_parse_mailbox (ctx);
411 else if (ctx->magic == M_MMDF)
412 rc = mmdf_parse_mailbox (ctx);
416 mbox_unlock_mailbox (ctx);
417 mutt_unblock_signals ();
421 /* return 1 if address lists are strictly identical */
422 static int strict_addrcmp (const ADDRESS * a, const ADDRESS * b)
425 if (safe_strcmp (a->mailbox, b->mailbox) ||
426 safe_strcmp (a->personal, b->personal))
438 static int strict_cmp_lists (const LIST * a, const LIST * b)
441 if (safe_strcmp (a->data, b->data))
453 static int strict_cmp_envelopes (const ENVELOPE * e1, const ENVELOPE * e2)
456 if (safe_strcmp (e1->message_id, e2->message_id) ||
457 safe_strcmp (e1->subject, e2->subject) ||
458 !strict_cmp_lists (e1->references, e2->references) ||
459 !strict_addrcmp (e1->from, e2->from) ||
460 !strict_addrcmp (e1->sender, e2->sender) ||
461 !strict_addrcmp (e1->reply_to, e2->reply_to) ||
462 !strict_addrcmp (e1->to, e2->to) ||
463 !strict_addrcmp (e1->cc, e2->cc) ||
464 !strict_addrcmp (e1->return_path, e2->return_path))
470 if (e1 == NULL && e2 == NULL)
477 static int strict_cmp_parameters (const PARAMETER * p1, const PARAMETER * p2)
480 if (safe_strcmp (p1->attribute, p2->attribute) ||
481 safe_strcmp (p1->value, p2->value))
493 static int strict_cmp_bodies (const BODY * b1, const BODY * b2)
495 if (b1->type != b2->type ||
496 b1->encoding != b2->encoding ||
497 safe_strcmp (b1->subtype, b2->subtype) ||
498 safe_strcmp (b1->description, b2->description) ||
499 !strict_cmp_parameters (b1->parameter, b2->parameter) ||
500 b1->length != b2->length)
505 /* return 1 if headers are strictly identical */
506 int mbox_strict_cmp_headers (const HEADER * h1, const HEADER * h2)
509 if (h1->received != h2->received ||
510 h1->date_sent != h2->date_sent ||
511 h1->content->length != h2->content->length ||
512 h1->lines != h2->lines ||
513 h1->zhours != h2->zhours ||
514 h1->zminutes != h2->zminutes ||
515 h1->zoccident != h2->zoccident ||
516 h1->mime != h2->mime ||
517 !strict_cmp_envelopes (h1->env, h2->env) ||
518 !strict_cmp_bodies (h1->content, h2->content))
524 if (h1 == NULL && h2 == NULL)
531 /* check to see if the mailbox has changed on disk.
534 * M_REOPENED mailbox has been reopened
535 * M_NEW_MAIL new mail has arrived!
536 * M_LOCKED couldn't lock the file
540 int mbox_check_mailbox (CONTEXT * ctx, int *index_hint)
543 char buffer[LONG_STRING];
547 if (stat (ctx->path, &st) == 0) {
548 if (st.st_mtime == ctx->mtime && st.st_size == ctx->size)
551 if (st.st_size == ctx->size) {
552 /* the file was touched, but it is still the same length, so just exit */
553 ctx->mtime = st.st_mtime;
557 if (st.st_size > ctx->size) {
558 /* lock the file if it isn't already */
560 mutt_block_signals ();
561 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
562 mutt_unblock_signals ();
563 /* we couldn't lock the mailbox, but nothing serious happened:
564 * probably the new mail arrived: no reason to wait till we can
565 * parse it: we'll get it on the next pass
573 * Check to make sure that the only change to the mailbox is that
574 * message(s) were appended to this file. My heuristic is that we should
575 * see the message separator at *exactly* what used to be the end of the
578 if (fseek (ctx->fp, ctx->size, SEEK_SET) != 0)
579 dprint (1, (debugfile, "mbox_check_mailbox: fseek() failed\n"));
580 if (fgets (buffer, sizeof (buffer), ctx->fp) != NULL) {
581 if ((ctx->magic == M_MBOX && safe_strncmp ("From ", buffer, 5) == 0)
582 || (ctx->magic == M_MMDF && safe_strcmp (MMDF_SEP, buffer) == 0)) {
583 if (fseek (ctx->fp, ctx->size, SEEK_SET) != 0)
584 dprint (1, (debugfile, "mbox_check_mailbox: fseek() failed\n"));
585 if (ctx->magic == M_MBOX)
586 mbox_parse_mailbox (ctx);
588 mmdf_parse_mailbox (ctx);
590 /* Only unlock the folder if it was locked inside of this routine.
591 * It may have been locked elsewhere, like in
592 * mutt_checkpoint_mailbox().
596 mbox_unlock_mailbox (ctx);
597 mutt_unblock_signals ();
600 return (M_NEW_MAIL); /* signal that new mail arrived */
606 dprint (1, (debugfile, "mbox_check_mailbox: fgets returned NULL.\n"));
615 if (mbox_reopen_mailbox (ctx, index_hint) != -1) {
617 mbox_unlock_mailbox (ctx);
618 mutt_unblock_signals ();
626 mbox_unlock_mailbox (ctx);
627 mx_fastclose_mailbox (ctx);
628 mutt_unblock_signals ();
629 mutt_error _("Mailbox was corrupted!");
638 static int _mbox_sync_mailbox (CONTEXT * ctx, int unused, int *index_hint)
640 char tempfile[_POSIX_PATH_MAX];
642 int i, j, save_sort = SORT_ORDER;
644 int need_sort = 0; /* flag to resort mailbox if new mail arrives */
645 int first = -1; /* first message to be written */
646 long offset; /* location in mailbox to write changed messages */
648 struct utimbuf utimebuf;
649 struct m_update_t *newOffset = NULL;
650 struct m_update_t *oldOffset = NULL;
653 /* sort message by their position in the mailbox on disk */
654 if (Sort != SORT_ORDER) {
657 mutt_sort_headers (ctx, 0);
660 /* need to open the file for writing in such a way that it does not truncate
661 * the file, so use read-write mode.
663 if ((ctx->fp = freopen (ctx->path, "r+", ctx->fp)) == NULL) {
664 mx_fastclose_mailbox (ctx);
665 mutt_error _("Fatal error! Could not reopen mailbox!");
670 mutt_block_signals ();
672 if (mbox_lock_mailbox (ctx, 1, 1) == -1) {
673 mutt_unblock_signals ();
674 mutt_error _("Unable to lock mailbox!");
679 /* Check to make sure that the file hasn't changed on disk */
680 if ((i = mbox_check_mailbox (ctx, index_hint)) == M_NEW_MAIL
681 || i == M_REOPENED) {
682 /* new mail arrived, or mailbox reopened */
693 /* Create a temporary file to write the new version of the mailbox in. */
694 mutt_mktemp (tempfile);
695 if ((i = open (tempfile, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1 ||
696 (fp = fdopen (i, "w")) == NULL) {
701 mutt_error _("Could not create temporary file!");
707 /* find the first deleted/changed message. we save a lot of time by only
708 * rewriting the mailbox from the point where it has actually changed.
710 for (i = 0; i < ctx->msgcount && !ctx->hdrs[i]->deleted &&
711 !ctx->hdrs[i]->changed && !ctx->hdrs[i]->attach_del; i++);
712 if (i == ctx->msgcount) {
713 /* this means ctx->changed or ctx->deleted was set, but no
714 * messages were found to be changed or deleted. This should
715 * never happen, is we presume it is a bug in mutt.
718 _("sync: mbox modified, but no modified messages! (report this bug)");
719 mutt_sleep (5); /* the mutt_error /will/ get cleared! */
720 dprint (1, (debugfile, "_mbox_sync_mailbox(): no modified messages.\n"));
725 /* save the index of the first changed/deleted message */
727 /* where to start overwriting */
728 offset = ctx->hdrs[i]->offset;
730 /* the offset stored in the header does not include the MMDF_SEP, so make
731 * sure we seek to the correct location
733 if (ctx->magic == M_MMDF)
734 offset -= (sizeof MMDF_SEP - 1);
736 /* allocate space for the new offsets */
737 newOffset = safe_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
738 oldOffset = safe_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
740 for (i = first, j = 0; i < ctx->msgcount; i++) {
742 * back up some information which is needed to restore offsets when
746 oldOffset[i - first].valid = 1;
747 oldOffset[i - first].hdr = ctx->hdrs[i]->offset;
748 oldOffset[i - first].body = ctx->hdrs[i]->content->offset;
749 oldOffset[i - first].lines = ctx->hdrs[i]->lines;
750 oldOffset[i - first].length = ctx->hdrs[i]->content->length;
752 if (!ctx->hdrs[i]->deleted) {
754 if (!ctx->quiet && WriteInc && ((i % WriteInc) == 0 || j == 1))
755 mutt_message (_("Writing messages... %d (%d%%)"), i,
756 (int) (ftell (ctx->fp) / (ctx->size / 100 + 1)));
758 if (ctx->magic == M_MMDF) {
759 if (fputs (MMDF_SEP, fp) == EOF) {
760 mutt_perror (tempfile);
768 /* save the new offset for this message. we add `offset' because the
769 * temporary file only contains saved message which are located after
770 * `offset' in the real mailbox
772 newOffset[i - first].hdr = ftell (fp) + offset;
774 if (mutt_copy_message
775 (fp, ctx, ctx->hdrs[i], M_CM_UPDATE,
776 CH_FROM | CH_UPDATE | CH_UPDATE_LEN) == -1) {
777 mutt_perror (tempfile);
783 /* Since messages could have been deleted, the offsets stored in memory
784 * will be wrong, so update what we can, which is the offset of this
785 * message, and the offset of the body. If this is a multipart message,
786 * we just flush the in memory cache so that the message will be reparsed
787 * if the user accesses it later.
789 newOffset[i - first].body =
790 ftell (fp) - ctx->hdrs[i]->content->length + offset;
791 mutt_free_body (&ctx->hdrs[i]->content->parts);
793 switch (ctx->magic) {
795 if (fputs (MMDF_SEP, fp) == EOF) {
796 mutt_perror (tempfile);
803 if (fputs ("\n", fp) == EOF) {
804 mutt_perror (tempfile);
813 if (fclose (fp) != 0) {
816 (debugfile, "_mbox_sync_mailbox: fclose() returned non-zero.\n"));
818 mutt_perror (tempfile);
824 /* Save the state of this folder. */
825 if (stat (ctx->path, &statbuf) == -1) {
826 mutt_perror (ctx->path);
832 if ((fp = fopen (tempfile, "r")) == NULL) {
833 mutt_unblock_signals ();
834 mx_fastclose_mailbox (ctx);
837 "_mbox_sync_mailbox: unable to reopen temp copy of mailbox!\n"));
838 mutt_perror (tempfile);
843 if (fseek (ctx->fp, offset, SEEK_SET) != 0 || /* seek the append location */
844 /* do a sanity check to make sure the mailbox looks ok */
845 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
846 (ctx->magic == M_MBOX && safe_strncmp ("From ", buf, 5) != 0) ||
847 (ctx->magic == M_MMDF && safe_strcmp (MMDF_SEP, buf) != 0)) {
850 "_mbox_sync_mailbox: message not in expected position."));
851 dprint (1, (debugfile, "\tLINE: %s\n", buf));
855 if (fseek (ctx->fp, offset, SEEK_SET) != 0) { /* return to proper offset */
857 dprint (1, (debugfile, "_mbox_sync_mailbox: fseek() failed\n"));
860 /* copy the temp mailbox back into place starting at the first
861 * change/deleted message
863 mutt_message _("Committing changes...");
865 i = mutt_copy_stream (fp, ctx->fp);
867 if (ferror (ctx->fp))
871 ctx->size = ftell (ctx->fp); /* update the size of the mailbox */
872 ftruncate (fileno (ctx->fp), ctx->size);
878 mbox_unlock_mailbox (ctx);
880 if (fclose (ctx->fp) != 0 || i == -1) {
881 /* error occured while writing the mailbox back, so keep the temp copy
885 char savefile[_POSIX_PATH_MAX];
887 snprintf (savefile, sizeof (savefile), "%s/mutt.%s-%s-%u",
888 NONULL (Tempdir), NONULL (Username), NONULL (Hostname),
889 (unsigned int) getpid ());
890 rename (tempfile, savefile);
891 mutt_unblock_signals ();
892 mx_fastclose_mailbox (ctx);
893 mutt_pretty_mailbox (savefile);
894 mutt_error (_("Write failed! Saved partial mailbox to %s"), savefile);
899 /* Restore the previous access/modification times */
900 utimebuf.actime = statbuf.st_atime;
901 utimebuf.modtime = statbuf.st_mtime;
902 utime (ctx->path, &utimebuf);
904 /* reopen the mailbox in read-only mode */
905 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
907 mutt_unblock_signals ();
908 mx_fastclose_mailbox (ctx);
909 mutt_error _("Fatal error! Could not reopen mailbox!");
915 /* update the offsets of the rewritten messages */
916 for (i = first, j = first; i < ctx->msgcount; i++) {
917 if (!ctx->hdrs[i]->deleted) {
918 ctx->hdrs[i]->offset = newOffset[i - first].hdr;
919 ctx->hdrs[i]->content->hdr_offset = newOffset[i - first].hdr;
920 ctx->hdrs[i]->content->offset = newOffset[i - first].body;
921 ctx->hdrs[i]->index = j++;
926 unlink (tempfile); /* remove partial copy of the mailbox */
927 mutt_unblock_signals ();
928 Sort = save_sort; /* Restore the default value. */
930 return (0); /* signal success */
932 bail: /* Come here in case of disaster */
936 /* restore offsets, as far as they are valid */
937 if (first >= 0 && oldOffset) {
938 for (i = first; i < ctx->msgcount && oldOffset[i - first].valid; i++) {
939 ctx->hdrs[i]->offset = oldOffset[i - first].hdr;
940 ctx->hdrs[i]->content->hdr_offset = oldOffset[i - first].hdr;
941 ctx->hdrs[i]->content->offset = oldOffset[i - first].body;
942 ctx->hdrs[i]->lines = oldOffset[i - first].lines;
943 ctx->hdrs[i]->content->length = oldOffset[i - first].length;
947 /* this is ok to call even if we haven't locked anything */
948 mbox_unlock_mailbox (ctx);
950 mutt_unblock_signals ();
954 if ((ctx->fp = freopen (ctx->path, "r", ctx->fp)) == NULL) {
955 mutt_error _("Could not reopen mailbox!");
957 mx_fastclose_mailbox (ctx);
961 if (need_sort || save_sort != Sort) {
963 /* if the mailbox was reopened, the thread tree will be invalid so make
964 * sure to start threading from scratch. */
965 mutt_sort_headers (ctx, (need_sort == M_REOPENED));
971 static int mbox_sync_mailbox (CONTEXT * ctx, int unused, int *index_hint) {
975 int rc = _mbox_sync_mailbox (ctx, unused, index_hint);
978 if ((tmp = buffy_find_mailbox (ctx->path)) && tmp->new == 0)
979 buffy_update_mailbox (tmp);
984 /* close a mailbox opened in write-mode */
985 int mbox_close_mailbox (CONTEXT * ctx)
987 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
989 #ifdef USE_COMPRESSED
990 if (ctx->compressinfo)
991 mutt_slow_close_compressed (ctx);
994 mutt_unblock_signals ();
995 mx_fastclose_mailbox (ctx);
999 static int mbox_reopen_mailbox (CONTEXT * ctx, int *index_hint)
1001 int (*cmp_headers) (const HEADER *, const HEADER *) = NULL;
1009 /* silent operations */
1012 mutt_message _("Reopening mailbox...");
1014 /* our heuristics require the old mailbox to be unsorted */
1015 if (Sort != SORT_ORDER) {
1020 mutt_sort_headers (ctx, 1);
1027 /* simulate a close */
1029 hash_destroy (&ctx->id_hash, NULL);
1031 hash_destroy (&ctx->subj_hash, NULL);
1032 mutt_clear_threads (ctx);
1034 if (ctx->readonly) {
1035 for (i = 0; i < ctx->msgcount; i++)
1036 mutt_free_header (&(ctx->hdrs[i])); /* nothing to do! */
1040 /* save the old headers */
1041 old_msgcount = ctx->msgcount;
1042 old_hdrs = ctx->hdrs;
1046 ctx->hdrmax = 0; /* force allocation of new headers */
1055 ctx->id_hash = NULL;
1056 ctx->subj_hash = NULL;
1058 switch (ctx->magic) {
1061 if (fseek (ctx->fp, 0, SEEK_SET) != 0) {
1062 dprint (1, (debugfile, "mbox_reopen_mailbox: fseek() failed\n"));
1066 cmp_headers = mbox_strict_cmp_headers;
1067 if (ctx->magic == M_MBOX)
1068 rc = mbox_parse_mailbox (ctx);
1070 rc = mmdf_parse_mailbox (ctx);
1080 /* free the old headers */
1081 for (j = 0; j < old_msgcount; j++)
1082 mutt_free_header (&(old_hdrs[j]));
1089 /* now try to recover the old flags */
1091 index_hint_set = (index_hint == NULL);
1093 if (!ctx->readonly) {
1094 for (i = 0; i < ctx->msgcount; i++) {
1097 /* some messages have been deleted, and new messages have been
1098 * appended at the end; the heuristic is that old messages have then
1099 * "advanced" towards the beginning of the folder, so we begin the
1100 * search at index "i"
1102 for (j = i; j < old_msgcount; j++) {
1103 if (old_hdrs[j] == NULL)
1105 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
1111 for (j = 0; j < i && j < old_msgcount; j++) {
1112 if (old_hdrs[j] == NULL)
1114 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
1122 /* this is best done here */
1123 if (!index_hint_set && *index_hint == j)
1126 if (old_hdrs[j]->changed) {
1127 /* Only update the flags if the old header was changed;
1128 * otherwise, the header may have been modified externally,
1129 * and we don't want to lose _those_ changes
1131 mutt_set_flag (ctx, ctx->hdrs[i], M_FLAG, old_hdrs[j]->flagged);
1132 mutt_set_flag (ctx, ctx->hdrs[i], M_REPLIED, old_hdrs[j]->replied);
1133 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, old_hdrs[j]->old);
1134 mutt_set_flag (ctx, ctx->hdrs[i], M_READ, old_hdrs[j]->read);
1136 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, old_hdrs[j]->deleted);
1137 mutt_set_flag (ctx, ctx->hdrs[i], M_TAG, old_hdrs[j]->tagged);
1139 /* we don't need this header any more */
1140 mutt_free_header (&(old_hdrs[j]));
1144 /* free the remaining old headers */
1145 for (j = 0; j < old_msgcount; j++) {
1147 mutt_free_header (&(old_hdrs[j]));
1156 return ((ctx->changed || msg_mod) ? M_REOPENED : M_NEW_MAIL);
1161 * 1 if the mailbox is not empty
1162 * 0 if the mailbox is empty
1165 int mbox_check_empty (const char *path)
1169 if (stat (path, &st) == -1)
1172 return ((st.st_size == 0));
1175 int mbox_is_magic (const char* path, struct stat* st) {
1178 char tmp[_POSIX_PATH_MAX];
1180 if (S_ISDIR(st->st_mode))
1183 if (st->st_size == 0) {
1184 /* hard to tell what zero-length files are, so assume the default magic */
1185 if (DefaultMagic == M_MBOX || DefaultMagic == M_MMDF)
1186 return (DefaultMagic);
1190 else if ((f = fopen (path, "r")) != NULL) {
1192 struct utimbuf times;
1194 fgets (tmp, sizeof (tmp), f);
1195 if (safe_strncmp ("From ", tmp, 5) == 0)
1197 else if (safe_strcmp (MMDF_SEP, tmp) == 0)
1201 /* need to restore the times here, the file was not really accessed,
1202 * only the type was accessed. This is important, because detection
1203 * of "new mail" depends on those times set correctly.
1205 times.actime = st->st_atime;
1206 times.modtime = st->st_mtime;
1207 utime (path, ×);
1211 return (-1); /* fopen failed */
1214 #ifdef USE_COMPRESSED
1215 if (magic == -1 && mutt_can_read_compressed (path))
1216 return (M_COMPRESSED);
1221 static mx_t* reg_mx (void) {
1222 mx_t* fmt = safe_calloc (1, sizeof (mx_t));
1224 fmt->mx_check_empty = mbox_check_empty;
1225 fmt->mx_is_magic = mbox_is_magic;
1226 fmt->mx_access = access;
1227 fmt->mx_open_mailbox = mbox_open_mailbox;
1228 fmt->mx_open_new_message = mbox_open_new_message;
1229 fmt->mx_sync_mailbox = mbox_sync_mailbox;
1233 mx_t* mbox_reg_mx (void) {
1234 mx_t* fmt = reg_mx ();
1238 mx_t* mmdf_reg_mx (void) {
1239 mx_t* fmt = reg_mx ();