2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 1999-2002 Thomas Roessler <roessler@does-not-exist.org>
6 * This file is part of mutt-ng, see http://www.muttng.org/.
7 * It's licensed under the GNU General Public License,
8 * please see the file GPL in the top level source directory.
12 * This file contains routines specific to MH and ``maildir'' style
33 #include <sys/types.h>
53 unsigned header_parsed:1;
56 #endif /* USE_INODESORT */
65 /* mh_sequences support */
67 #define MH_SEQ_UNSEEN (1 << 0)
68 #define MH_SEQ_REPLIED (1 << 1)
69 #define MH_SEQ_FLAGGED (1 << 2)
71 static void mhs_alloc (struct mh_sequences *mhs, int i)
76 if (i > mhs->max || !mhs->flags) {
78 safe_realloc (&mhs->flags, sizeof (mhs->flags[0]) * (newmax + 1));
79 for (j = mhs->max + 1; j <= newmax; j++)
86 static void mhs_free_sequences (struct mh_sequences *mhs)
91 static short mhs_check (struct mh_sequences *mhs, int i)
93 if (!mhs->flags || i > mhs->max)
99 static short mhs_set (struct mh_sequences *mhs, int i, short f)
103 return mhs->flags[i];
110 static short mhs_unset (struct mh_sequences *mhs, int i, short f)
114 return mhs->flags[i];
119 static void mh_read_token (char *t, int *first, int *last)
123 if ((p = strchr (t, '-'))) {
129 *first = *last = atoi (t);
132 static void mh_read_sequences (struct mh_sequences *mhs, const char *path)
143 char pathname[_POSIX_PATH_MAX];
145 snprintf (pathname, sizeof (pathname), "%s/.mh_sequences", path);
147 if (!(fp = fopen (pathname, "r")))
150 while ((buff = mutt_read_line (buff, &sz, fp, &line))) {
151 if (!(t = strtok (buff, " \t:")))
154 if (!safe_strcmp (t, MhUnseen))
156 else if (!safe_strcmp (t, MhFlagged))
158 else if (!safe_strcmp (t, MhReplied))
160 else /* unknown sequence */
163 while ((t = strtok (NULL, " \t:"))) {
164 mh_read_token (t, &first, &last);
165 for (; first <= last; first++)
166 mhs_set (mhs, first, f);
174 int mh_buffy (const char *path)
177 struct mh_sequences mhs;
179 memset (&mhs, 0, sizeof (mhs));
181 mh_read_sequences (&mhs, path);
182 for (i = 0; !r && i <= mhs.max; i++)
183 if (mhs_check (&mhs, i) & MH_SEQ_UNSEEN)
185 mhs_free_sequences (&mhs);
189 static int mh_mkstemp (CONTEXT * dest, FILE ** fp, char **tgt)
192 char path[_POSIX_PATH_MAX];
195 snprintf (path, _POSIX_PATH_MAX, "%s/.mutt-%s-%d-%d",
196 dest->path, NONULL (Hostname), (int) getpid (), Counter++);
198 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1) {
199 if (errno != EEXIST) {
205 *tgt = safe_strdup (path);
210 if ((*fp = fdopen (fd, "w")) == NULL) {
220 static void mhs_write_one_sequence (FILE * fp, struct mh_sequences *mhs,
221 short f, const char *tag)
226 fprintf (fp, "%s:", tag);
231 for (i = 0; i <= mhs->max; i++) {
232 if ((mhs_check (mhs, i) & f)) {
238 else if (first >= 0) {
240 fprintf (fp, " %d", first);
242 fprintf (fp, " %d-%d", first, last);
251 fprintf (fp, " %d", first);
253 fprintf (fp, " %d-%d", first, last);
259 /* XXX - we don't currently remove deleted messages from sequences we don't know. Should we? */
261 void mh_update_sequences (CONTEXT * ctx)
265 char sequences[_POSIX_PATH_MAX];
277 char seq_unseen[STRING];
278 char seq_replied[STRING];
279 char seq_flagged[STRING];
282 struct mh_sequences mhs;
284 memset (&mhs, 0, sizeof (mhs));
286 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
287 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
288 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
290 if (mh_mkstemp (ctx, &nfp, &tmpfname) != 0) {
295 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
298 /* first, copy unknown sequences */
299 if ((ofp = fopen (sequences, "r"))) {
300 while ((buff = mutt_read_line (buff, &s, ofp, &l))) {
301 if (!safe_strncmp (buff, seq_unseen, safe_strlen (seq_unseen)))
303 if (!safe_strncmp (buff, seq_flagged, safe_strlen (seq_flagged)))
305 if (!safe_strncmp (buff, seq_replied, safe_strlen (seq_replied)))
308 fprintf (nfp, "%s\n", buff);
313 /* now, update our unseen, flagged, and replied sequences */
314 for (l = 0; l < ctx->msgcount; l++) {
315 if (ctx->hdrs[l]->deleted)
318 if ((p = strrchr (ctx->hdrs[l]->path, '/')))
321 p = ctx->hdrs[l]->path;
325 if (!ctx->hdrs[l]->read) {
326 mhs_set (&mhs, i, MH_SEQ_UNSEEN);
329 if (ctx->hdrs[l]->flagged) {
330 mhs_set (&mhs, i, MH_SEQ_FLAGGED);
333 if (ctx->hdrs[l]->replied) {
334 mhs_set (&mhs, i, MH_SEQ_REPLIED);
339 /* write out the new sequences */
341 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_UNSEEN, NONULL (MhUnseen));
343 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_FLAGGED, NONULL (MhFlagged));
345 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_REPLIED, NONULL (MhReplied));
347 mhs_free_sequences (&mhs);
350 /* try to commit the changes - no guarantee here */
354 if (safe_rename (tmpfname, sequences) != 0) {
355 /* report an error? */
362 static void mh_sequences_add_one (CONTEXT * ctx, int n, short unseen,
363 short flagged, short replied)
365 short unseen_done = 0;
366 short flagged_done = 0;
367 short replied_done = 0;
369 FILE *ofp = NULL, *nfp = NULL;
372 char sequences[_POSIX_PATH_MAX];
374 char seq_unseen[STRING];
375 char seq_replied[STRING];
376 char seq_flagged[STRING];
382 if (mh_mkstemp (ctx, &nfp, &tmpfname) == -1)
385 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
386 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
387 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
389 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
390 if ((ofp = fopen (sequences, "r"))) {
391 while ((buff = mutt_read_line (buff, &sz, ofp, &line))) {
392 if (unseen && !strncmp (buff, seq_unseen, safe_strlen (seq_unseen))) {
393 fprintf (nfp, "%s %d\n", buff, n);
397 && !strncmp (buff, seq_flagged, safe_strlen (seq_flagged))) {
398 fprintf (nfp, "%s %d\n", buff, n);
402 && !strncmp (buff, seq_replied, safe_strlen (seq_replied))) {
403 fprintf (nfp, "%s %d\n", buff, n);
407 fprintf (nfp, "%s\n", buff);
413 if (!unseen_done && unseen)
414 fprintf (nfp, "%s: %d\n", NONULL (MhUnseen), n);
415 if (!flagged_done && flagged)
416 fprintf (nfp, "%s: %d\n", NONULL (MhFlagged), n);
417 if (!replied_done && replied)
418 fprintf (nfp, "%s: %d\n", NONULL (MhReplied), n);
423 if (safe_rename (tmpfname, sequences) != 0)
429 static void mh_update_maildir (struct maildir *md, struct mh_sequences *mhs)
435 for (; md; md = md->next) {
436 if ((p = strrchr (md->h->path, '/')))
442 f = mhs_check (mhs, i);
444 md->h->read = (f & MH_SEQ_UNSEEN) ? 0 : 1;
445 md->h->flagged = (f & MH_SEQ_FLAGGED) ? 1 : 0;
446 md->h->replied = (f & MH_SEQ_REPLIED) ? 1 : 0;
450 /* maildir support */
452 static void maildir_free_entry (struct maildir **md)
457 FREE (&(*md)->canon_fname);
459 mutt_free_header (&(*md)->h);
464 static void maildir_free_maildir (struct maildir **md)
466 struct maildir *p, *q;
471 for (p = *md; p; p = q) {
473 maildir_free_entry (&p);
477 static void maildir_parse_flags (HEADER * h, const char *path)
485 if ((p = strrchr (path, ':')) != NULL && safe_strncmp (p + 1, "2,", 2) == 0) {
488 str_replace (&h->maildir_flags, p);
489 q = h->maildir_flags;
503 case 'R': /* replied */
508 case 'T': /* trashed */
521 if (q == h->maildir_flags)
522 FREE (&h->maildir_flags);
527 static void maildir_update_mtime (CONTEXT * ctx)
529 char buf[_POSIX_PATH_MAX];
532 if (ctx->magic == M_MAILDIR) {
533 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "cur");
534 if (stat (buf, &st) == 0)
535 ctx->mtime_cur = st.st_mtime;
536 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "new");
539 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
540 if (stat (buf, &st) == 0)
541 ctx->mtime_cur = st.st_mtime;
543 strfcpy (buf, ctx->path, sizeof (buf));
546 if (stat (buf, &st) == 0)
547 ctx->mtime = st.st_mtime;
551 * Actually parse a maildir message. This may also be used to fill
552 * out a fake header structure generated by lazy maildir parsing.
554 static HEADER *maildir_parse_message (int magic, const char *fname,
555 int is_old, HEADER * _h)
561 if ((f = fopen (fname, "r")) != NULL) {
563 h = mutt_new_header ();
564 h->env = mutt_read_rfc822_header (f, h, 0, 0);
566 fstat (fileno (f), &st);
570 h->received = h->date_sent;
572 if (h->content->length <= 0)
573 h->content->length = st.st_size - h->content->offset;
577 if (magic == M_MAILDIR) {
579 * maildir stores its flags in the filename, so ignore the
580 * flags in the header of the message
584 maildir_parse_flags (h, fname);
592 * Note that this routine will _not_ modify the context given by
595 * It's used in the first parsing pass on maildir and MH folders.
596 * In the MH case, this means full parsing of the folder. In the
597 * maildir case, it means that we only look at flags, and create a
598 * fake HEADER structure, which may later be filled in by
599 * maildir_parse_message(), when called from
600 * maildir_delayed_parsing().
604 static int maildir_parse_entry (CONTEXT * ctx, struct maildir ***last,
605 const char *subdir, const char *fname,
606 int *count, int is_old, ino_t inode)
608 struct maildir *entry;
610 char buf[_POSIX_PATH_MAX];
613 snprintf (buf, sizeof (buf), "%s/%s/%s", ctx->path, subdir, fname);
615 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, fname);
617 if (ctx->magic == M_MH)
618 h = maildir_parse_message (ctx->magic, buf, is_old, NULL);
620 h = mutt_new_header ();
622 maildir_parse_flags (h, buf);
628 if (!ctx->quiet && ReadInc && ((*count % ReadInc) == 0 || *count == 1))
629 mutt_message (_("Reading %s... %d"), ctx->path, *count);
633 snprintf (buf, sizeof (buf), "%s/%s", subdir, fname);
634 h->path = safe_strdup (buf);
637 h->path = safe_strdup (fname);
639 entry = safe_calloc (sizeof (struct maildir), 1);
641 entry->header_parsed = (ctx->magic == M_MH);
643 entry->inode = inode;
644 #endif /* USE_INODESORT */
646 *last = &entry->next;
656 /* Ignore the garbage files. A valid MH message consists of only
657 * digits. Deleted message get moved to a filename with a comma before
661 int mh_valid_message (const char *s)
664 if (!isdigit ((unsigned char) *s))
670 static int maildir_parse_dir (CONTEXT * ctx, struct maildir ***last,
671 const char *subdir, int *count)
675 char buf[_POSIX_PATH_MAX];
679 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, subdir);
680 is_old = (safe_strcmp ("cur", subdir) == 0);
683 strfcpy (buf, ctx->path, sizeof (buf));
685 if ((dirp = opendir (buf)) == NULL)
688 while ((de = readdir (dirp)) != NULL) {
690 if ((ctx->magic == M_MH && !mh_valid_message (de->d_name))
691 || (ctx->magic == M_MAILDIR && *de->d_name == '.'))
694 /* FOO - really ignore the return value? */
697 (debugfile, "%s:%d: parsing %s\n", __FILE__, __LINE__,
699 maildir_parse_entry (ctx, last, subdir, de->d_name, count, is_old,
707 static int maildir_add_to_context (CONTEXT * ctx, struct maildir *md)
709 int oldmsgcount = ctx->msgcount;
713 dprint (2, (debugfile, "%s:%d maildir_add_to_context(): Considering %s\n",
714 __FILE__, __LINE__, NONULL (md->canon_fname)));
719 "%s:%d Adding header structure. Flags: %s%s%s%s%s\n", __FILE__,
720 __LINE__, md->h->flagged ? "f" : "", md->h->deleted ? "D" : "",
721 md->h->replied ? "r" : "", md->h->old ? "O" : "",
722 md->h->read ? "R" : ""));
723 if (ctx->msgcount == ctx->hdrmax)
724 mx_alloc_memory (ctx);
726 ctx->hdrs[ctx->msgcount] = md->h;
727 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
729 md->h->content->length + md->h->content->offset -
730 md->h->content->hdr_offset;
738 if (ctx->msgcount > oldmsgcount) {
739 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
745 static int maildir_move_to_context (CONTEXT * ctx, struct maildir **md)
749 r = maildir_add_to_context (ctx, *md);
750 maildir_free_maildir (md);
756 * Merge two maildir lists according to the inode numbers.
758 static struct maildir *maildir_merge_inode (struct maildir *left,
759 struct maildir *right)
761 struct maildir *head;
762 struct maildir *tail;
765 if (left->inode < right->inode) {
783 while (left && right) {
784 if (left->inode < right->inode) {
806 * Sort maildir list according to inode.
808 static struct maildir *maildir_sort_inode (struct maildir *list)
810 struct maildir *left = list;
811 struct maildir *right = list;
813 if (!list || !list->next) {
818 while (list && list->next) {
820 list = list->next->next;
827 left = maildir_sort_inode (left);
828 right = maildir_sort_inode (right);
829 return maildir_merge_inode (left, right);
831 #endif /* USE_INODESORT */
834 static size_t maildir_hcache_keylen (const char *fn)
836 const char *p = strchr (fn, ':');
838 return p ? (size_t) (p - fn) : safe_strlen (fn);
843 * This function does the second parsing pass for a maildir-style
846 void maildir_delayed_parsing (CONTEXT * ctx, struct maildir *md)
849 char fn[_POSIX_PATH_MAX];
855 struct timeval *when = NULL;
856 struct stat lastchanged;
859 hc = mutt_hcache_open (HeaderCache, ctx->path);
862 for (p = md, count = 0; p; p = p->next, count++) {
863 if (!(p && p->h && !p->header_parsed))
867 data = mutt_hcache_fetch (hc, p->h->path + 3, &maildir_hcache_keylen);
868 when = (struct timeval *) data;
871 if (!ctx->quiet && ReadInc && ((count % ReadInc) == 0 || count == 1))
872 mutt_message (_("Reading %s... %d"), ctx->path, count);
873 snprintf (fn, sizeof (fn), "%s/%s", ctx->path, p->h->path);
876 if (option (OPTHCACHEVERIFY)) {
877 ret = stat (fn, &lastchanged);
880 lastchanged.st_mtime = 0;
884 if (data != NULL && !ret && lastchanged.st_mtime <= when->tv_sec) {
885 p->h = mutt_hcache_restore ((unsigned char *) data, &p->h);
886 maildir_parse_flags (p->h, fn);
890 if (maildir_parse_message (ctx->magic, fn, p->h->old, p->h)) {
891 p->header_parsed = 1;
892 maildir_parse_flags (p->h, fn);
894 mutt_hcache_store (hc, p->h->path + 3, p->h, 0, &maildir_hcache_keylen);
898 mutt_free_header (&p->h);
904 mutt_hcache_close (hc);
908 /* Read a MH/maildir style mailbox.
911 * ctx [IN/OUT] context for this mailbox
912 * subdir [IN] NULL for MH mailboxes, otherwise the subdir of the
913 * maildir mailbox to read from
915 int mh_read_dir (CONTEXT * ctx, const char *subdir)
918 struct mh_sequences mhs;
919 struct maildir **last;
923 memset (&mhs, 0, sizeof (mhs));
925 maildir_update_mtime (ctx);
930 if (maildir_parse_dir (ctx, &last, subdir, &count) == -1)
933 if (ctx->magic == M_MH) {
934 mh_read_sequences (&mhs, ctx->path);
935 mh_update_maildir (md, &mhs);
936 mhs_free_sequences (&mhs);
940 md = maildir_sort_inode (md);
941 #endif /* USE_INODESORT */
943 if (ctx->magic == M_MAILDIR)
944 maildir_delayed_parsing (ctx, md);
946 maildir_move_to_context (ctx, &md);
950 /* read a maildir style mailbox */
951 int maildir_read_dir (CONTEXT * ctx)
953 /* maildir looks sort of like MH, except that there are two subdirectories
954 * of the main folder path from which to read messages
956 if (mh_read_dir (ctx, "new") == -1 || mh_read_dir (ctx, "cur") == -1)
963 * Open a new (temporary) message in an MH folder.
966 int mh_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
968 return mh_mkstemp (dest, &msg->fp, &msg->path);
971 int ch_compar (const void *a, const void *b)
973 return (int) (*((const char *) a) - *((const char *) b));
976 static void maildir_flags (char *dest, size_t destlen, HEADER * hdr)
981 * The maildir specification requires that all files in the cur
982 * subdirectory have the :unique string appeneded, regardless of whether
983 * or not there are any flags. If .old is set, we know that this message
984 * will end up in the cur directory, so we include it in the following
985 * test even though there is no associated flag.
989 && (hdr->flagged || hdr->replied || hdr->read || hdr->deleted
990 || hdr->old || hdr->maildir_flags)) {
991 char tmp[LONG_STRING];
993 snprintf (tmp, sizeof (tmp),
995 hdr->flagged ? "F" : "",
996 hdr->replied ? "R" : "",
997 hdr->read ? "S" : "", hdr->deleted ? "T" : "",
998 NONULL (hdr->maildir_flags));
999 if (hdr->maildir_flags)
1000 qsort (tmp, safe_strlen (tmp), 1, ch_compar);
1001 snprintf (dest, destlen, ":2,%s", tmp);
1007 * Open a new (temporary) message in a maildir folder.
1009 * Note that this uses _almost_ the maildir file name format, but
1010 * with a {cur,new} prefix.
1014 int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1017 char path[_POSIX_PATH_MAX];
1022 short deleted = hdr->deleted;
1026 maildir_flags (suffix, sizeof (suffix), hdr);
1028 hdr->deleted = deleted;
1033 if (hdr && (hdr->read || hdr->old))
1034 strfcpy (subdir, "cur", sizeof (subdir));
1036 strfcpy (subdir, "new", sizeof (subdir));
1039 snprintf (path, _POSIX_PATH_MAX, "%s/tmp/%s.%ld.%u_%d.%s%s",
1040 dest->path, subdir, (long) time (NULL),
1041 (unsigned int) getpid (), Counter++, NONULL (Hostname), suffix);
1043 dprint (2, (debugfile, "maildir_open_new_message (): Trying %s.\n",
1047 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1) {
1048 if (errno != EEXIST) {
1054 dprint (2, (debugfile, "maildir_open_new_message (): Success.\n"));
1055 msg->path = safe_strdup (path);
1060 if ((msg->fp = fdopen (fd, "w")) == NULL) {
1073 * Commit a message to a maildir folder.
1075 * msg->path contains the file name of a file in tmp/. We take the
1076 * flags from this file's name.
1078 * ctx is the mail folder we commit to.
1080 * hdr is a header structure to which we write the message's new
1081 * file name. This is used in the mh and maildir folder synch
1082 * routines. When this routine is invoked from mx_commit_message,
1085 * msg->path looks like this:
1087 * tmp/{cur,new}.mutt-HOSTNAME-PID-COUNTER:flags
1089 * See also maildir_open_new_message().
1093 int maildir_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
1097 char path[_POSIX_PATH_MAX];
1098 char full[_POSIX_PATH_MAX];
1101 if (safe_fclose (&msg->fp) != 0)
1104 /* extract the subdir */
1105 s = strrchr (msg->path, '/') + 1;
1106 strfcpy (subdir, s, 4);
1108 /* extract the flags */
1109 if ((s = strchr (s, ':')))
1110 strfcpy (suffix, s, sizeof (suffix));
1114 /* construct a new file name. */
1116 snprintf (path, _POSIX_PATH_MAX, "%s/%ld.%u_%d.%s%s", subdir,
1117 (long) time (NULL), (unsigned int) getpid (), Counter++,
1118 NONULL (Hostname), suffix);
1119 snprintf (full, _POSIX_PATH_MAX, "%s/%s", ctx->path, path);
1121 dprint (2, (debugfile, "maildir_commit_message (): renaming %s to %s.\n",
1124 if (safe_rename (msg->path, full) == 0) {
1126 str_replace (&hdr->path, path);
1130 * Adjust the mtime on the file to match the time at which this
1131 * message was received. Currently this is only set when copying
1132 * messages between mailboxes, so we test to ensure that it is
1135 if (msg->received) {
1138 ut.actime = msg->received;
1139 ut.modtime = msg->received;
1140 if (utime (full, &ut)) {
1142 ("maildir_commit_message(): unable to set time on file"));
1149 else if (errno != EEXIST) {
1150 mutt_perror (ctx->path);
1157 * commit a message to an MH folder.
1162 static int _mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr,
1168 unsigned int n, hi = 0;
1169 char path[_POSIX_PATH_MAX];
1172 if (safe_fclose (&msg->fp) != 0)
1175 if ((dirp = opendir (ctx->path)) == NULL) {
1176 mutt_perror (ctx->path);
1180 /* figure out what the next message number is */
1181 while ((de = readdir (dirp)) != NULL) {
1187 if (!isdigit ((unsigned char) *cp))
1200 * Now try to rename the file to the proper name.
1202 * Note: We may have to try multiple times, until we find a free
1208 snprintf (tmp, sizeof (tmp), "%d", hi);
1209 snprintf (path, sizeof (path), "%s/%s", ctx->path, tmp);
1210 if (safe_rename (msg->path, path) == 0) {
1212 str_replace (&hdr->path, tmp);
1216 else if (errno != EEXIST) {
1217 mutt_perror (ctx->path);
1222 mh_sequences_add_one (ctx, hi, !msg->flags.read, msg->flags.flagged,
1223 msg->flags.replied);
1227 int mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
1229 return _mh_commit_message (ctx, msg, hdr, 1);
1233 /* Sync a message in an MH folder.
1235 * This code is also used for attachment deletion in maildir
1239 static int mh_rewrite_message (CONTEXT * ctx, int msgno)
1241 HEADER *h = ctx->hdrs[msgno];
1246 char oldpath[_POSIX_PATH_MAX];
1247 char newpath[_POSIX_PATH_MAX];
1248 char partpath[_POSIX_PATH_MAX];
1250 long old_body_offset = h->content->offset;
1251 long old_body_length = h->content->length;
1252 long old_hdr_lines = h->lines;
1254 if ((dest = mx_open_new_message (ctx, h, 0)) == NULL)
1257 if ((rc = mutt_copy_message (dest->fp, ctx, h,
1258 M_CM_UPDATE, CH_UPDATE | CH_UPDATE_LEN)) == 0)
1260 snprintf (oldpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1261 strfcpy (partpath, h->path, _POSIX_PATH_MAX);
1263 if (ctx->magic == M_MAILDIR)
1264 rc = maildir_commit_message (ctx, dest, h);
1266 rc = _mh_commit_message (ctx, dest, h, 0);
1268 mx_close_message (&dest);
1276 * Try to move the new message to the old place.
1279 * This is important when we are just updating flags.
1281 * Note that there is a race condition against programs which
1282 * use the first free slot instead of the maximum message
1283 * number. Mutt does _not_ behave like this.
1285 * Anyway, if this fails, the message is in the folder, so
1286 * all what happens is that a concurrently runnung mutt will
1287 * lose flag modifications.
1290 if (ctx->magic == M_MH && rc == 0) {
1291 snprintf (newpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1292 if ((rc = safe_rename (newpath, oldpath)) == 0)
1293 str_replace (&h->path, partpath);
1297 mx_close_message (&dest);
1299 if (rc == -1 && restore) {
1300 h->content->offset = old_body_offset;
1301 h->content->length = old_body_length;
1302 h->lines = old_hdr_lines;
1305 mutt_free_body (&h->content->parts);
1309 static int mh_sync_message (CONTEXT * ctx, int msgno)
1311 HEADER *h = ctx->hdrs[msgno];
1313 if (h->attach_del || h->refs_changed || h->irt_changed)
1314 if (mh_rewrite_message (ctx, msgno) != 0)
1320 static int maildir_sync_message (CONTEXT * ctx, int msgno)
1322 HEADER *h = ctx->hdrs[msgno];
1324 if (h->attach_del || h->refs_changed || h->irt_changed) {
1325 /* when doing attachment deletion/rethreading, fall back to the MH case. */
1326 if (mh_rewrite_message (ctx, msgno) != 0)
1330 /* we just have to rename the file. */
1332 char newpath[_POSIX_PATH_MAX];
1333 char partpath[_POSIX_PATH_MAX];
1334 char fullpath[_POSIX_PATH_MAX];
1335 char oldpath[_POSIX_PATH_MAX];
1339 if ((p = strrchr (h->path, '/')) == NULL) {
1342 "maildir_sync_message: %s: unable to find subdir!\n",
1347 strfcpy (newpath, p, sizeof (newpath));
1349 /* kill the previous flags */
1350 if ((p = strchr (newpath, ':')) != NULL)
1353 maildir_flags (suffix, sizeof (suffix), h);
1355 snprintf (partpath, sizeof (partpath), "%s/%s%s",
1356 (h->read || h->old) ? "cur" : "new", newpath, suffix);
1357 snprintf (fullpath, sizeof (fullpath), "%s/%s", ctx->path, partpath);
1358 snprintf (oldpath, sizeof (oldpath), "%s/%s", ctx->path, h->path);
1360 if (safe_strcmp (fullpath, oldpath) == 0) {
1361 /* message hasn't really changed */
1365 /* record that the message is possibly marked as trashed on disk */
1366 h->trash = h->deleted;
1368 if (rename (oldpath, fullpath) != 0) {
1369 mutt_perror ("rename");
1372 str_replace (&h->path, partpath);
1377 int mh_sync_mailbox (CONTEXT * ctx, int *index_hint)
1379 char path[_POSIX_PATH_MAX], tmp[_POSIX_PATH_MAX];
1384 #endif /* USE_HCACHE */
1386 if (ctx->magic == M_MH)
1387 i = mh_check_mailbox (ctx, index_hint);
1389 i = maildir_check_mailbox (ctx, index_hint);
1395 if (ctx->magic == M_MAILDIR)
1396 hc = mutt_hcache_open (HeaderCache, ctx->path);
1397 #endif /* USE_HCACHE */
1399 for (i = 0; i < ctx->msgcount; i++) {
1400 if (ctx->hdrs[i]->deleted
1401 && (ctx->magic != M_MAILDIR || !option (OPTMAILDIRTRASH))) {
1402 snprintf (path, sizeof (path), "%s/%s", ctx->path, ctx->hdrs[i]->path);
1403 if (ctx->magic == M_MAILDIR
1404 || (option (OPTMHPURGE) && ctx->magic == M_MH)) {
1406 if (ctx->magic == M_MAILDIR)
1407 mutt_hcache_delete (hc, ctx->hdrs[i]->path + 3,
1408 &maildir_hcache_keylen);
1409 #endif /* USE_HCACHE */
1412 else if (ctx->magic == M_MH) {
1413 /* MH just moves files out of the way when you delete them */
1414 if (*ctx->hdrs[i]->path != ',') {
1415 snprintf (tmp, sizeof (tmp), "%s/,%s", ctx->path,
1416 ctx->hdrs[i]->path);
1423 else if (ctx->hdrs[i]->changed || ctx->hdrs[i]->attach_del ||
1424 (ctx->magic == M_MAILDIR
1425 && (option (OPTMAILDIRTRASH) || ctx->hdrs[i]->trash)
1426 && (ctx->hdrs[i]->deleted != ctx->hdrs[i]->trash))) {
1427 if (ctx->magic == M_MAILDIR) {
1428 if (maildir_sync_message (ctx, i) == -1)
1432 if (mh_sync_message (ctx, i) == -1)
1439 if (ctx->magic == M_MAILDIR)
1440 mutt_hcache_close (hc);
1441 #endif /* USE_HCACHE */
1443 if (ctx->magic == M_MH)
1444 mh_update_sequences (ctx);
1446 /* XXX race condition? */
1448 maildir_update_mtime (ctx);
1450 /* adjust indices */
1453 for (i = 0, j = 0; i < ctx->msgcount; i++) {
1454 if (!ctx->hdrs[i]->deleted
1455 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1456 ctx->hdrs[i]->index = j++;
1464 if (ctx->magic == M_MAILDIR)
1465 mutt_hcache_close (hc);
1466 #endif /* USE_HCACHE */
1470 static char *maildir_canon_filename (char *dest, const char *src, size_t l)
1474 if ((t = strrchr (src, '/')))
1477 strfcpy (dest, src, l);
1478 if ((u = strrchr (dest, ':')))
1484 static void maildir_update_tables (CONTEXT * ctx, int *index_hint)
1490 if (Sort != SORT_ORDER) {
1493 mutt_sort_headers (ctx, 1);
1497 old_count = ctx->msgcount;
1498 for (i = 0, j = 0; i < old_count; i++) {
1499 if (ctx->hdrs[i]->active && index_hint && *index_hint == i)
1502 if (ctx->hdrs[i]->active)
1503 ctx->hdrs[i]->index = j++;
1506 mx_update_tables (ctx, 0);
1507 mutt_clear_threads (ctx);
1510 static void maildir_update_flags (CONTEXT * ctx, HEADER * o, HEADER * n)
1512 /* save the global state here so we can reset it at the
1513 * end of list block if required.
1515 int context_changed = ctx->changed;
1517 /* user didn't modify this message. alter the flags to
1518 * match the current state on disk. This may not actually
1519 * do anything, but we can't tell right now. mutt_set_flag()
1520 * will just ignore the call if the status bits are
1521 * already properly set.
1523 mutt_set_flag (ctx, o, M_FLAG, n->flagged);
1524 mutt_set_flag (ctx, o, M_REPLIED, n->replied);
1525 mutt_set_flag (ctx, o, M_READ, n->read);
1526 mutt_set_flag (ctx, o, M_OLD, n->old);
1528 /* mutt_set_flag() will set this, but we don't need to
1529 * sync the changes we made because we just updated the
1530 * context to match the current on-disk state of the
1535 /* if the mailbox was not modified before we made these
1536 * changes, unset the changed flag since nothing needs to
1539 if (!context_changed)
1544 /* This function handles arrival of new mail and reopening of
1545 * maildir folders. The basic idea here is we check to see if either
1546 * the new or cur subdirectories have changed, and if so, we scan them
1547 * for the list of files. We check for newly added messages, and
1548 * then merge the flags messages we already knew about. We don't treat
1549 * either subdirectory differently, as mail could be copied directly into
1550 * the cur directory from another agent.
1552 int maildir_check_mailbox (CONTEXT * ctx, int *index_hint)
1554 struct stat st_new; /* status of the "new" subdirectory */
1555 struct stat st_cur; /* status of the "cur" subdirectory */
1556 char buf[_POSIX_PATH_MAX];
1557 int changed = 0; /* bitmask representing which subdirectories
1558 have changed. 0x1 = new, 0x2 = cur */
1559 int occult = 0; /* messages were removed from the mailbox */
1560 int have_new = 0; /* messages were added to the mailbox */
1561 struct maildir *md; /* list of messages in the mailbox */
1562 struct maildir **last, *p;
1564 HASH *fnames; /* hash table for quickly looking up the base filename
1565 for a maildir message */
1567 /* XXX seems like this check belongs in mx_check_mailbox()
1570 if (!option (OPTCHECKNEW))
1573 snprintf (buf, sizeof (buf), "%s/new", ctx->path);
1574 if (stat (buf, &st_new) == -1)
1577 snprintf (buf, sizeof (buf), "%s/cur", ctx->path);
1578 if (stat (buf, &st_cur) == -1)
1581 /* determine which subdirectories need to be scanned */
1582 if (st_new.st_mtime > ctx->mtime)
1584 if (st_cur.st_mtime > ctx->mtime_cur)
1588 return 0; /* nothing to do */
1590 /* update the modification times on the mailbox */
1591 ctx->mtime_cur = st_cur.st_mtime;
1592 ctx->mtime = st_new.st_mtime;
1594 /* do a fast scan of just the filenames in
1595 * the subdirectories that have changed.
1600 maildir_parse_dir (ctx, &last, "new", NULL);
1602 maildir_parse_dir (ctx, &last, "cur", NULL);
1604 /* we create a hash table keyed off the canonical (sans flags) filename
1605 * of each message we scanned. This is used in the loop over the
1606 * existing messages below to do some correlation.
1608 fnames = hash_create (1031);
1610 for (p = md; p; p = p->next) {
1611 maildir_canon_filename (buf, p->h->path, sizeof (buf));
1612 p->canon_fname = safe_strdup (buf);
1613 hash_insert (fnames, p->canon_fname, p, 0);
1616 /* check for modifications and adjust flags */
1617 for (i = 0; i < ctx->msgcount; i++) {
1618 ctx->hdrs[i]->active = 0;
1619 maildir_canon_filename (buf, ctx->hdrs[i]->path, sizeof (buf));
1620 p = hash_find (fnames, buf);
1622 /* message already exists, merge flags */
1623 ctx->hdrs[i]->active = 1;
1625 /* check to see if the message has moved to a different
1626 * subdirectory. If so, update the associated filename.
1628 if (safe_strcmp (ctx->hdrs[i]->path, p->h->path))
1629 str_replace (&ctx->hdrs[i]->path, p->h->path);
1631 /* if the user hasn't modified the flags on this message, update
1632 * the flags we just detected.
1634 if (!ctx->hdrs[i]->changed)
1635 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1637 if (ctx->hdrs[i]->deleted == ctx->hdrs[i]->trash)
1638 ctx->hdrs[i]->deleted = p->h->deleted;
1639 ctx->hdrs[i]->trash = p->h->trash;
1641 /* this is a duplicate of an existing header, so remove it */
1642 mutt_free_header (&p->h);
1644 /* This message was not in the list of messages we just scanned.
1645 * Check to see if we have enough information to know if the
1646 * message has disappeared out from underneath us.
1648 else if (((changed & 1) && (!strncmp (ctx->hdrs[i]->path, "new/", 4))) ||
1649 ((changed & 2) && (!strncmp (ctx->hdrs[i]->path, "cur/", 4)))) {
1650 /* This message disappeared, so we need to simulate a "reopen"
1651 * event. We know it disappeared because we just scanned the
1652 * subdirectory it used to reside in.
1657 /* This message resides in a subdirectory which was not
1658 * modified, so we assume that it is still present and
1661 ctx->hdrs[i]->active = 1;
1665 /* destroy the file name hash */
1666 hash_destroy (&fnames, NULL);
1668 /* If we didn't just get new mail, update the tables. */
1670 maildir_update_tables (ctx, index_hint);
1672 /* do any delayed parsing we need to do. */
1673 maildir_delayed_parsing (ctx, md);
1675 /* Incorporate new messages */
1676 have_new = maildir_move_to_context (ctx, &md);
1678 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1682 * This function handles arrival of new mail and reopening of
1683 * mh/maildir folders. Things are getting rather complex because we
1684 * don't have a well-defined "mailbox order", so the tricks from
1685 * mbox.c and mx.c won't work here.
1687 * Don't change this code unless you _really_ understand what
1692 int mh_check_mailbox (CONTEXT * ctx, int *index_hint)
1694 char buf[_POSIX_PATH_MAX];
1695 struct stat st, st_cur;
1696 short modified = 0, have_new = 0, occult = 0;
1697 struct maildir *md, *p;
1698 struct maildir **last = NULL;
1699 struct mh_sequences mhs;
1703 if (!option (OPTCHECKNEW))
1706 strfcpy (buf, ctx->path, sizeof (buf));
1707 if (stat (buf, &st) == -1)
1710 /* create .mh_sequences when there isn't one. */
1711 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
1712 if ((i = stat (buf, &st_cur) == -1) && errno == ENOENT) {
1716 if (mh_mkstemp (ctx, &fp, &tmp) == 0) {
1718 if (safe_rename (tmp, buf) == -1)
1724 if (i == -1 && stat (buf, &st_cur) == -1)
1727 if (st.st_mtime > ctx->mtime || st_cur.st_mtime > ctx->mtime_cur)
1733 ctx->mtime_cur = st_cur.st_mtime;
1734 ctx->mtime = st.st_mtime;
1736 memset (&mhs, 0, sizeof (mhs));
1740 maildir_parse_dir (ctx, &last, NULL, NULL);
1741 mh_read_sequences (&mhs, ctx->path);
1742 mh_update_maildir (md, &mhs);
1743 mhs_free_sequences (&mhs);
1745 /* check for modifications and adjust flags */
1746 fnames = hash_create (1031);
1748 for (p = md; p; p = p->next)
1749 hash_insert (fnames, p->h->path, p, 0);
1751 for (i = 0; i < ctx->msgcount; i++) {
1752 ctx->hdrs[i]->active = 0;
1754 if ((p = hash_find (fnames, ctx->hdrs[i]->path)) && p->h &&
1755 (mbox_strict_cmp_headers (ctx->hdrs[i], p->h))) {
1756 ctx->hdrs[i]->active = 1;
1757 /* found the right message */
1758 if (!ctx->hdrs[i]->changed)
1759 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1761 mutt_free_header (&p->h);
1763 else /* message has disappeared */
1767 /* destroy the file name hash */
1769 hash_destroy (&fnames, NULL);
1771 /* If we didn't just get new mail, update the tables. */
1773 maildir_update_tables (ctx, index_hint);
1775 /* Incorporate new messages */
1776 have_new = maildir_move_to_context (ctx, &md);
1778 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1785 * These functions try to find a message in a maildir folder when it
1786 * has moved under our feet. Note that this code is rather expensive, but
1787 * then again, it's called rarely.
1790 FILE *_maildir_open_find_message (const char *folder, const char *unique,
1791 const char *subfolder)
1793 char dir[_POSIX_PATH_MAX];
1794 char tunique[_POSIX_PATH_MAX];
1795 char fname[_POSIX_PATH_MAX];
1803 snprintf (dir, sizeof (dir), "%s/%s", folder, subfolder);
1805 if ((dp = opendir (dir)) == NULL) {
1810 while ((de = readdir (dp))) {
1811 maildir_canon_filename (tunique, de->d_name, sizeof (tunique));
1813 if (!safe_strcmp (tunique, unique)) {
1814 snprintf (fname, sizeof (fname), "%s/%s/%s", folder, subfolder,
1816 fp = fopen (fname, "r"); /* __FOPEN_CHECKED__ */
1828 FILE *maildir_open_find_message (const char *folder, const char *msg)
1830 char unique[_POSIX_PATH_MAX];
1833 static unsigned int new_hits = 0, cur_hits = 0; /* simple dynamic optimization */
1835 maildir_canon_filename (unique, msg, sizeof (unique));
1838 _maildir_open_find_message (folder, unique,
1839 new_hits > cur_hits ? "new" : "cur"))
1840 || errno != ENOENT) {
1841 if (new_hits < UINT_MAX && cur_hits < UINT_MAX) {
1842 new_hits += (new_hits > cur_hits ? 1 : 0);
1843 cur_hits += (new_hits > cur_hits ? 0 : 1);
1849 _maildir_open_find_message (folder, unique,
1850 new_hits > cur_hits ? "cur" : "new"))
1851 || errno != ENOENT) {
1852 if (new_hits < UINT_MAX && cur_hits < UINT_MAX) {
1853 new_hits += (new_hits > cur_hits ? 0 : 1);
1854 cur_hits += (new_hits > cur_hits ? 1 : 0);
1866 * 1 if there are no messages in the mailbox
1867 * 0 if there are messages in the mailbox
1870 int maildir_check_empty (const char *path)
1874 int r = 1; /* assume empty until we find a message */
1875 char realpath[_POSIX_PATH_MAX];
1878 /* Strategy here is to look for any file not beginning with a period */
1881 /* we do "cur" on the first iteration since its more likely that we'll
1882 * find old messages without having to scan both subdirs
1884 snprintf (realpath, sizeof (realpath), "%s/%s", path,
1885 iter == 0 ? "cur" : "new");
1886 if ((dp = opendir (realpath)) == NULL)
1888 while ((de = readdir (dp))) {
1889 if (*de->d_name != '.') {
1896 } while (r && iter < 2);
1903 * 1 if there are no messages in the mailbox
1904 * 0 if there are messages in the mailbox
1907 int mh_check_empty (const char *path)
1911 int r = 1; /* assume empty until we find a message */
1913 if ((dp = opendir (path)) == NULL)
1915 while ((de = readdir (dp))) {
1916 if (mh_valid_message (de->d_name)) {