2 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /* This file contains code to parse ``mbox'' and ``mmdf'' style mailboxes */
44 /* struct used by mutt_sync_mailbox() to store new offsets */
55 * ctx - context to lock
56 * excl - exclusive lock?
57 * retry - should retry if unable to lock?
59 int mbox_lock_mailbox (CONTEXT *ctx, int excl, int retry)
63 if ((r = mx_lock_file (ctx->path, fileno (ctx->fp), excl, 1, retry)) == 0)
65 else if (retry && !excl)
74 void mbox_unlock_mailbox (CONTEXT *ctx)
80 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
85 int mmdf_parse_mailbox (CONTEXT *ctx)
87 char buf[HUGE_STRING];
88 char return_path[LONG_STRING];
89 int count = 0, oldmsgcount = ctx->msgcount;
95 #ifdef NFS_ATTRIBUTE_HACK
96 struct utimbuf newtime;
99 if (stat (ctx->path, &sb) == -1)
101 mutt_perror (ctx->path);
104 ctx->mtime = sb.st_mtime;
105 ctx->size = sb.st_size;
107 #ifdef NFS_ATTRIBUTE_HACK
108 if (sb.st_mtime > sb.st_atime)
110 newtime.modtime = sb.st_mtime;
111 newtime.actime = time (NULL);
112 utime (ctx->path, &newtime);
116 /* precompute the local timezone to speed up calculation of the
118 tz = mutt_local_tz (0);
120 buf[sizeof (buf) - 1] = 0;
124 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
127 if (mutt_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)
145 /* TODO: memory leak??? */
146 dprint (1, (debugfile, "mmdf_parse_mailbox: unexpected EOF\n"));
152 if (!is_from (buf, return_path, sizeof (return_path), &t))
154 if (fseek (ctx->fp, loc, SEEK_SET) != 0)
156 dprint (1, (debugfile, "mmdf_parse_mailbox: fseek() failed\n"));
157 mutt_error _("Mailbox is corrupt!");
162 hdr->received = t - tz;
164 hdr->env = mutt_read_rfc822_header (ctx->fp, hdr, 0, 0);
166 loc = ftell (ctx->fp);
168 if (hdr->content->length > 0 && hdr->lines > 0)
170 tmploc = loc + hdr->content->length;
172 if (0 < tmploc && tmploc < ctx->size)
174 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0 ||
175 fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL ||
176 mutt_strcmp (MMDF_SEP, buf) != 0)
178 if (fseek (ctx->fp, loc, SEEK_SET) != 0)
179 dprint (1, (debugfile, "mmdf_parse_mailbox: fseek() failed\n"));
180 hdr->content->length = -1;
184 hdr->content->length = -1;
187 hdr->content->length = -1;
189 if (hdr->content->length < 0)
193 loc = ftell (ctx->fp);
194 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
197 } while (mutt_strcmp (buf, MMDF_SEP) != 0);
200 hdr->content->length = loc - hdr->content->offset;
203 if (!hdr->env->return_path && return_path[0])
204 hdr->env->return_path = rfc822_parse_adrlist (hdr->env->return_path, return_path);
207 hdr->env->from = rfc822_cpy_adr (hdr->env->return_path);
213 dprint (1, (debugfile, "mmdf_parse_mailbox: corrupt mailbox!\n"));
214 mutt_error _("Mailbox is corrupt!");
219 if (ctx->msgcount > oldmsgcount)
220 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
225 /* Note that this function is also called when new mail is appended to the
226 * currently open folder, and NOT just when the mailbox is initially read.
228 * NOTE: it is assumed that the mailbox being read has been locked before
229 * this routine gets called. Strange things could happen if it's not!
231 int mbox_parse_mailbox (CONTEXT *ctx)
234 char buf[HUGE_STRING], return_path[STRING];
237 int count = 0, lines = 0;
239 #ifdef NFS_ATTRIBUTE_HACK
240 struct utimbuf newtime;
243 /* Save information about the folder at the time we opened it. */
244 if (stat (ctx->path, &sb) == -1)
246 mutt_perror (ctx->path);
250 ctx->size = sb.st_size;
251 ctx->mtime = sb.st_mtime;
253 #ifdef NFS_ATTRIBUTE_HACK
254 if (sb.st_mtime > sb.st_atime)
256 newtime.modtime = sb.st_mtime;
257 newtime.actime = time (NULL);
258 utime (ctx->path, &newtime);
263 ctx->readonly = access (ctx->path, W_OK) ? 1 : 0;
265 /* precompute the local timezone to speed up calculation of the
267 tz = mutt_local_tz (0);
269 loc = ftell (ctx->fp);
270 while (fgets (buf, sizeof (buf), ctx->fp) != NULL)
272 if (is_from (buf, return_path, sizeof (return_path), &t))
274 /* Save the Content-Length of the previous message */
277 #define PREV ctx->hdrs[ctx->msgcount-1]
279 if (PREV->content->length < 0)
281 PREV->content->length = loc - PREV->content->offset - 1;
282 if (PREV->content->length < 0)
283 PREV->content->length = 0;
286 PREV->lines = lines ? lines - 1 : 0;
291 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
292 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
293 (int)(ftell (ctx->fp) / (ctx->size / 100 + 1)));
295 if (ctx->msgcount == ctx->hdrmax)
296 mx_alloc_memory (ctx);
298 curhdr = ctx->hdrs[ctx->msgcount] = mutt_new_header ();
299 curhdr->received = t - tz;
300 curhdr->offset = loc;
301 curhdr->index = ctx->msgcount;
303 curhdr->env = mutt_read_rfc822_header (ctx->fp, curhdr, 0, 0);
305 /* if we know how long this message is, either just skip over the body,
306 * or if we don't know how many lines there are, count them now (this will
307 * save time by not having to search for the next message marker).
309 if (curhdr->content->length > 0)
313 loc = ftell (ctx->fp);
314 tmploc = loc + curhdr->content->length + 1;
316 if (0 < tmploc && tmploc < ctx->size)
319 * check to see if the content-length looks valid. we expect to
320 * to see a valid message separator at this point in the stream
322 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0 ||
323 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
324 mutt_strncmp ("From ", buf, 5) != 0)
326 dprint (1, (debugfile, "mbox_parse_mailbox: bad content-length in message %d (cl=%ld)\n", curhdr->index, curhdr->content->length));
327 dprint (1, (debugfile, "\tLINE: %s", buf));
328 if (fseek (ctx->fp, loc, SEEK_SET) != 0) /* nope, return the previous position */
330 dprint (1, (debugfile, "mbox_parse_mailbox: fseek() failed\n"));
332 curhdr->content->length = -1;
335 else if (tmploc != ctx->size)
337 /* content-length would put us past the end of the file, so it
340 curhdr->content->length = -1;
343 if (curhdr->content->length != -1)
345 /* good content-length. check to see if we know how many lines
346 * are in this message.
348 if (curhdr->lines == 0)
350 int cl = curhdr->content->length;
352 /* count the number of lines in this message */
353 if (fseek (ctx->fp, loc, SEEK_SET) != 0)
354 dprint (1, (debugfile, "mbox_parse_mailbox: fseek() failed\n"));
357 if (fgetc (ctx->fp) == '\n')
362 /* return to the offset of the next message separator */
363 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0)
364 dprint (1, (debugfile, "mbox_parse_mailbox: fseek() failed\n"));
370 if (!curhdr->env->return_path && return_path[0])
371 curhdr->env->return_path = rfc822_parse_adrlist (curhdr->env->return_path, return_path);
373 if (!curhdr->env->from)
374 curhdr->env->from = rfc822_cpy_adr (curhdr->env->return_path);
381 loc = ftell (ctx->fp);
385 * Only set the content-length of the previous message if we have read more
386 * than one message during _this_ invocation. If this routine is called
387 * when new mail is received, we need to make sure not to clobber what
388 * previously was the last message since the headers may be sorted.
392 if (PREV->content->length < 0)
394 PREV->content->length = ftell (ctx->fp) - PREV->content->offset - 1;
395 if (PREV->content->length < 0)
396 PREV->content->length = 0;
400 PREV->lines = lines ? lines - 1 : 0;
402 mx_update_context (ctx, count);
410 /* open a mbox or mmdf style mailbox */
411 int mbox_open_mailbox (CONTEXT *ctx)
415 if ((ctx->fp = fopen (ctx->path, "r")) == NULL)
417 mutt_perror (ctx->path);
420 mutt_block_signals ();
421 if (mbox_lock_mailbox (ctx, 0, 1) == -1)
423 mutt_unblock_signals ();
427 if (ctx->magic == M_MBOX)
428 rc = mbox_parse_mailbox (ctx);
429 else if (ctx->magic == M_MMDF)
430 rc = mmdf_parse_mailbox (ctx);
434 mbox_unlock_mailbox (ctx);
435 mutt_unblock_signals ();
439 /* return 1 if address lists are strictly identical */
440 static int strict_addrcmp (const ADDRESS *a, const ADDRESS *b)
444 if (mutt_strcmp (a->mailbox, b->mailbox) ||
445 mutt_strcmp (a->personal, b->personal))
457 static int strict_cmp_lists (const LIST *a, const LIST *b)
461 if (mutt_strcmp (a->data, b->data))
473 static int strict_cmp_envelopes (const ENVELOPE *e1, const ENVELOPE *e2)
477 if (mutt_strcmp (e1->message_id, e2->message_id) ||
478 mutt_strcmp (e1->subject, e2->subject) ||
479 !strict_cmp_lists (e1->references, e2->references) ||
480 !strict_addrcmp (e1->from, e2->from) ||
481 !strict_addrcmp (e1->sender, e2->sender) ||
482 !strict_addrcmp (e1->reply_to, e2->reply_to) ||
483 !strict_addrcmp (e1->to, e2->to) ||
484 !strict_addrcmp (e1->cc, e2->cc) ||
485 !strict_addrcmp (e1->return_path, e2->return_path))
492 if (e1 == NULL && e2 == NULL)
499 static int strict_cmp_parameters (const PARAMETER *p1, const PARAMETER *p2)
503 if (mutt_strcmp (p1->attribute, p2->attribute) ||
504 mutt_strcmp (p1->value, p2->value))
516 static int strict_cmp_bodies (const BODY *b1, const BODY *b2)
518 if (b1->type != b2->type ||
519 b1->encoding != b2->encoding ||
520 mutt_strcmp (b1->subtype, b2->subtype) ||
521 mutt_strcmp (b1->description, b2->description) ||
522 !strict_cmp_parameters (b1->parameter, b2->parameter) ||
523 b1->length != b2->length)
528 /* return 1 if headers are strictly identical */
529 int mbox_strict_cmp_headers (const HEADER *h1, const HEADER *h2)
533 if (h1->received != h2->received ||
534 h1->date_sent != h2->date_sent ||
535 h1->content->length != h2->content->length ||
536 h1->lines != h2->lines ||
537 h1->zhours != h2->zhours ||
538 h1->zminutes != h2->zminutes ||
539 h1->zoccident != h2->zoccident ||
540 h1->mime != h2->mime ||
541 !strict_cmp_envelopes (h1->env, h2->env) ||
542 !strict_cmp_bodies (h1->content, h2->content))
549 if (h1 == NULL && h2 == NULL)
556 /* check to see if the mailbox has changed on disk.
559 * M_REOPENED mailbox has been reopened
560 * M_NEW_MAIL new mail has arrived!
561 * M_LOCKED couldn't lock the file
565 int mbox_check_mailbox (CONTEXT *ctx, int *index_hint)
568 char buffer[LONG_STRING];
572 if (stat (ctx->path, &st) == 0)
574 if (st.st_mtime == ctx->mtime && st.st_size == ctx->size)
577 if (st.st_size == ctx->size)
579 /* the file was touched, but it is still the same length, so just exit */
580 ctx->mtime = st.st_mtime;
584 if (st.st_size > ctx->size)
586 /* lock the file if it isn't already */
589 mutt_block_signals ();
590 if (mbox_lock_mailbox (ctx, 0, 0) == -1)
592 mutt_unblock_signals ();
593 /* we couldn't lock the mailbox, but nothing serious happened:
594 * probably the new mail arrived: no reason to wait till we can
595 * parse it: we'll get it on the next pass
603 * Check to make sure that the only change to the mailbox is that
604 * message(s) were appended to this file. My heuristic is that we should
605 * see the message separator at *exactly* what used to be the end of the
608 if (fseek (ctx->fp, ctx->size, SEEK_SET) != 0)
609 dprint (1, (debugfile, "mbox_check_mailbox: fseek() failed\n"));
610 if (fgets (buffer, sizeof (buffer), ctx->fp) != NULL)
612 if ((ctx->magic == M_MBOX && mutt_strncmp ("From ", buffer, 5) == 0) ||
613 (ctx->magic == M_MMDF && mutt_strcmp (MMDF_SEP, buffer) == 0))
615 if (fseek (ctx->fp, ctx->size, SEEK_SET) != 0)
616 dprint (1, (debugfile, "mbox_check_mailbox: fseek() failed\n"));
617 if (ctx->magic == M_MBOX)
618 mbox_parse_mailbox (ctx);
620 mmdf_parse_mailbox (ctx);
622 /* Only unlock the folder if it was locked inside of this routine.
623 * It may have been locked elsewhere, like in
624 * mutt_checkpoint_mailbox().
629 mbox_unlock_mailbox (ctx);
630 mutt_unblock_signals ();
633 return (M_NEW_MAIL); /* signal that new mail arrived */
640 dprint (1, (debugfile, "mbox_check_mailbox: fgets returned NULL.\n"));
650 if (mutt_reopen_mailbox (ctx, index_hint) != -1)
654 mbox_unlock_mailbox (ctx);
655 mutt_unblock_signals ();
663 mbox_unlock_mailbox (ctx);
664 mx_fastclose_mailbox (ctx);
665 mutt_unblock_signals ();
666 mutt_error _("Mailbox was corrupted!");
674 int mbox_sync_mailbox (CONTEXT *ctx, int *index_hint)
676 char tempfile[_POSIX_PATH_MAX];
678 int i, j, save_sort = SORT_ORDER;
680 int need_sort = 0; /* flag to resort mailbox if new mail arrives */
681 int first = -1; /* first message to be written */
682 long offset; /* location in mailbox to write changed messages */
684 struct utimbuf utimebuf;
685 struct m_update_t *newOffset = NULL;
686 struct m_update_t *oldOffset = NULL;
689 /* sort message by their position in the mailbox on disk */
690 if (Sort != SORT_ORDER)
694 mutt_sort_headers (ctx, 0);
697 /* need to open the file for writing in such a way that it does not truncate
698 * the file, so use read-write mode.
700 if ((ctx->fp = freopen (ctx->path, "r+", ctx->fp)) == NULL)
702 mx_fastclose_mailbox (ctx);
703 mutt_error _("Fatal error! Could not reopen mailbox!");
707 mutt_block_signals ();
709 if (mbox_lock_mailbox (ctx, 1, 1) == -1)
711 mutt_unblock_signals ();
712 mutt_error _("Unable to lock mailbox!");
716 /* Check to make sure that the file hasn't changed on disk */
717 if ((i = mbox_check_mailbox (ctx, index_hint)) == M_NEW_MAIL || i == M_REOPENED)
719 /* new mail arrived, or mailbox reopened */
731 /* Create a temporary file to write the new version of the mailbox in. */
732 mutt_mktemp (tempfile);
733 if ((i = open (tempfile, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1 ||
734 (fp = fdopen (i, "w")) == NULL)
741 mutt_error _("Could not create temporary file!");
746 /* find the first deleted/changed message. we save a lot of time by only
747 * rewriting the mailbox from the point where it has actually changed.
749 for (i = 0 ; i < ctx->msgcount && !ctx->hdrs[i]->deleted &&
750 !ctx->hdrs[i]->changed && !ctx->hdrs[i]->attach_del; i++)
752 if (i == ctx->msgcount)
754 /* this means ctx->changed or ctx->deleted was set, but no
755 * messages were found to be changed or deleted. This should
756 * never happen, is we presume it is a bug in mutt.
758 mutt_error _("sync: mbox modified, but no modified messages! (report this bug)");
759 mutt_sleep(5); /* the mutt_error /will/ get cleared! */
760 dprint(1, (debugfile, "mbox_sync_mailbox(): no modified messages.\n"));
765 /* save the index of the first changed/deleted message */
767 /* where to start overwriting */
768 offset = ctx->hdrs[i]->offset;
770 /* the offset stored in the header does not include the MMDF_SEP, so make
771 * sure we seek to the correct location
773 if (ctx->magic == M_MMDF)
774 offset -= (sizeof MMDF_SEP - 1);
776 /* allocate space for the new offsets */
777 newOffset = safe_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
778 oldOffset = safe_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
780 for (i = first, j = 0; i < ctx->msgcount; i++)
783 * back up some information which is needed to restore offsets when
787 oldOffset[i-first].valid = 1;
788 oldOffset[i-first].hdr = ctx->hdrs[i]->offset;
789 oldOffset[i-first].body = ctx->hdrs[i]->content->offset;
790 oldOffset[i-first].lines = ctx->hdrs[i]->lines;
791 oldOffset[i-first].length = ctx->hdrs[i]->content->length;
793 if (! ctx->hdrs[i]->deleted)
796 if (!ctx->quiet && WriteInc && ((i % WriteInc) == 0 || j == 1))
797 mutt_message (_("Writing messages... %d (%d%%)"), i,
798 (int)(ftell (ctx->fp) / (ctx->size / 100 + 1)));
800 if (ctx->magic == M_MMDF)
802 if (fputs (MMDF_SEP, fp) == EOF)
804 mutt_perror (tempfile);
812 /* save the new offset for this message. we add `offset' because the
813 * temporary file only contains saved message which are located after
814 * `offset' in the real mailbox
816 newOffset[i - first].hdr = ftell (fp) + offset;
818 if (mutt_copy_message (fp, ctx, ctx->hdrs[i], M_CM_UPDATE, CH_FROM | CH_UPDATE | CH_UPDATE_LEN) == -1)
820 mutt_perror (tempfile);
826 /* Since messages could have been deleted, the offsets stored in memory
827 * will be wrong, so update what we can, which is the offset of this
828 * message, and the offset of the body. If this is a multipart message,
829 * we just flush the in memory cache so that the message will be reparsed
830 * if the user accesses it later.
832 newOffset[i - first].body = ftell (fp) - ctx->hdrs[i]->content->length + offset;
833 mutt_free_body (&ctx->hdrs[i]->content->parts);
838 if(fputs(MMDF_SEP, fp) == EOF)
840 mutt_perror (tempfile);
847 if(fputs("\n", fp) == EOF)
849 mutt_perror (tempfile);
858 if (fclose (fp) != 0)
861 dprint(1, (debugfile, "mbox_sync_mailbox: fclose() returned non-zero.\n"));
863 mutt_perror (tempfile);
869 /* Save the state of this folder. */
870 if (stat (ctx->path, &statbuf) == -1)
872 mutt_perror (ctx->path);
878 if ((fp = fopen (tempfile, "r")) == NULL)
880 mutt_unblock_signals ();
881 mx_fastclose_mailbox (ctx);
882 dprint (1, (debugfile, "mbox_sync_mailbox: unable to reopen temp copy of mailbox!\n"));
883 mutt_perror (tempfile);
888 if (fseek (ctx->fp, offset, SEEK_SET) != 0 || /* seek the append location */
889 /* do a sanity check to make sure the mailbox looks ok */
890 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
891 (ctx->magic == M_MBOX && mutt_strncmp ("From ", buf, 5) != 0) ||
892 (ctx->magic == M_MMDF && mutt_strcmp (MMDF_SEP, buf) != 0))
894 dprint (1, (debugfile, "mbox_sync_mailbox: message not in expected position."));
895 dprint (1, (debugfile, "\tLINE: %s\n", buf));
900 if (fseek (ctx->fp, offset, SEEK_SET) != 0) /* return to proper offset */
903 dprint (1, (debugfile, "mbox_sync_mailbox: fseek() failed\n"));
907 /* copy the temp mailbox back into place starting at the first
908 * change/deleted message
910 mutt_message _("Committing changes...");
911 i = mutt_copy_stream (fp, ctx->fp);
913 if (ferror (ctx->fp))
918 ctx->size = ftell (ctx->fp); /* update the size of the mailbox */
919 ftruncate (fileno (ctx->fp), ctx->size);
925 mbox_unlock_mailbox (ctx);
927 if (fclose (ctx->fp) != 0 || i == -1)
929 /* error occured while writing the mailbox back, so keep the temp copy
933 char savefile[_POSIX_PATH_MAX];
935 snprintf (savefile, sizeof (savefile), "%s/mutt.%s-%s-%u",
936 NONULL (Tempdir), NONULL(Username), NONULL(Hostname), (unsigned int)getpid ());
937 rename (tempfile, savefile);
938 mutt_unblock_signals ();
939 mx_fastclose_mailbox (ctx);
940 mutt_pretty_mailbox (savefile);
941 mutt_error (_("Write failed! Saved partial mailbox to %s"), savefile);
946 /* Restore the previous access/modification times */
947 utimebuf.actime = statbuf.st_atime;
948 utimebuf.modtime = statbuf.st_mtime;
949 utime (ctx->path, &utimebuf);
951 /* reopen the mailbox in read-only mode */
952 if ((ctx->fp = fopen (ctx->path, "r")) == NULL)
955 mutt_unblock_signals ();
956 mx_fastclose_mailbox (ctx);
957 mutt_error _("Fatal error! Could not reopen mailbox!");
962 /* update the offsets of the rewritten messages */
963 for (i = first, j = first; i < ctx->msgcount; i++)
965 if (!ctx->hdrs[i]->deleted)
967 ctx->hdrs[i]->offset = newOffset[i - first].hdr;
968 ctx->hdrs[i]->content->hdr_offset = newOffset[i - first].hdr;
969 ctx->hdrs[i]->content->offset = newOffset[i - first].body;
970 ctx->hdrs[i]->index = j++;
975 unlink (tempfile); /* remove partial copy of the mailbox */
976 mutt_unblock_signals ();
977 Sort = save_sort; /* Restore the default value. */
979 return (0); /* signal success */
981 bail: /* Come here in case of disaster */
985 /* restore offsets, as far as they are valid */
986 if (first >= 0 && oldOffset)
988 for (i = first; i < ctx->msgcount && oldOffset[i-first].valid; i++)
990 ctx->hdrs[i]->offset = oldOffset[i-first].hdr;
991 ctx->hdrs[i]->content->hdr_offset = oldOffset[i-first].hdr;
992 ctx->hdrs[i]->content->offset = oldOffset[i-first].body;
993 ctx->hdrs[i]->lines = oldOffset[i-first].lines;
994 ctx->hdrs[i]->content->length = oldOffset[i-first].length;
998 /* this is ok to call even if we haven't locked anything */
999 mbox_unlock_mailbox (ctx);
1001 mutt_unblock_signals ();
1005 if ((ctx->fp = freopen (ctx->path, "r", ctx->fp)) == NULL)
1007 mutt_error _("Could not reopen mailbox!");
1008 mx_fastclose_mailbox (ctx);
1012 if (need_sort || save_sort != Sort)
1015 /* if the mailbox was reopened, the thread tree will be invalid so make
1016 * sure to start threading from scratch. */
1017 mutt_sort_headers (ctx, (need_sort == M_REOPENED));
1023 /* close a mailbox opened in write-mode */
1024 int mbox_close_mailbox (CONTEXT *ctx)
1026 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
1028 #ifdef USE_COMPRESSED
1029 if (ctx->compressinfo)
1030 mutt_slow_close_compressed (ctx);
1033 mutt_unblock_signals ();
1034 mx_fastclose_mailbox (ctx);
1038 int mutt_reopen_mailbox (CONTEXT *ctx, int *index_hint)
1040 int (*cmp_headers) (const HEADER *, const HEADER *) = NULL;
1048 /* silent operations */
1051 mutt_message _("Reopening mailbox...");
1053 /* our heuristics require the old mailbox to be unsorted */
1054 if (Sort != SORT_ORDER)
1060 mutt_sort_headers (ctx, 1);
1067 /* simulate a close */
1069 hash_destroy (&ctx->id_hash, NULL);
1071 hash_destroy (&ctx->subj_hash, NULL);
1072 mutt_clear_threads (ctx);
1076 for (i = 0; i < ctx->msgcount; i++)
1077 mutt_free_header (&(ctx->hdrs[i])); /* nothing to do! */
1082 /* save the old headers */
1083 old_msgcount = ctx->msgcount;
1084 old_hdrs = ctx->hdrs;
1088 ctx->hdrmax = 0; /* force allocation of new headers */
1097 ctx->id_hash = NULL;
1098 ctx->subj_hash = NULL;
1104 if (fseek (ctx->fp, 0, SEEK_SET) != 0)
1106 dprint (1, (debugfile, "mutt_reopen_mailbox: fseek() failed\n"));
1111 cmp_headers = mbox_strict_cmp_headers;
1112 rc = ((ctx->magic == M_MBOX) ? mbox_parse_mailbox
1113 : mmdf_parse_mailbox) (ctx);
1124 /* free the old headers */
1125 for (j = 0; j < old_msgcount; j++)
1126 mutt_free_header (&(old_hdrs[j]));
1133 /* now try to recover the old flags */
1135 index_hint_set = (index_hint == NULL);
1139 for (i = 0; i < ctx->msgcount; i++)
1143 /* some messages have been deleted, and new messages have been
1144 * appended at the end; the heuristic is that old messages have then
1145 * "advanced" towards the beginning of the folder, so we begin the
1146 * search at index "i"
1148 for (j = i; j < old_msgcount; j++)
1150 if (old_hdrs[j] == NULL)
1152 if (cmp_headers (ctx->hdrs[i], old_hdrs[j]))
1160 for (j = 0; j < i && j < old_msgcount; j++)
1162 if (old_hdrs[j] == NULL)
1164 if (cmp_headers (ctx->hdrs[i], old_hdrs[j]))
1174 /* this is best done here */
1175 if (!index_hint_set && *index_hint == j)
1178 if (old_hdrs[j]->changed)
1180 /* Only update the flags if the old header was changed;
1181 * otherwise, the header may have been modified externally,
1182 * and we don't want to lose _those_ changes
1184 mutt_set_flag (ctx, ctx->hdrs[i], M_FLAG, old_hdrs[j]->flagged);
1185 mutt_set_flag (ctx, ctx->hdrs[i], M_REPLIED, old_hdrs[j]->replied);
1186 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, old_hdrs[j]->old);
1187 mutt_set_flag (ctx, ctx->hdrs[i], M_READ, old_hdrs[j]->read);
1189 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, old_hdrs[j]->deleted);
1190 mutt_set_flag (ctx, ctx->hdrs[i], M_TAG, old_hdrs[j]->tagged);
1192 /* we don't need this header any more */
1193 mutt_free_header (&(old_hdrs[j]));
1197 /* free the remaining old headers */
1198 for (j = 0; j < old_msgcount; j++)
1202 mutt_free_header (&(old_hdrs[j]));
1211 return ((ctx->changed || msg_mod) ? M_REOPENED : M_NEW_MAIL);
1216 * 1 if the mailbox is not empty
1217 * 0 if the mailbox is empty
1220 int mbox_check_empty (const char *path)
1224 if (stat (path, &st) == -1)
1227 return ((st.st_size == 0));