Commit 448ea96c 448ea96cd052c70782cb14efb560b3b0102887b3 by Sergey Poznyakoff

Rewritten to use MH sequence numbers as UIDs. This

allows to simplify the code and to get rid
of mh_message_number(), which didn't fit well in the
mailutils framework.
1 parent c0ab0f0c
...@@ -62,6 +62,10 @@ ...@@ -62,6 +62,10 @@
62 62
63 #define MAX_OPEN_STREAMS 16 63 #define MAX_OPEN_STREAMS 16
64 64
65 /* Note: In this particular implementation the message sequence number
66 serves also as its UID. This allows to avoid many problems related
67 to keeping the uids in the headers of the messages. */
68
65 struct _mh_data; 69 struct _mh_data;
66 struct _mh_message 70 struct _mh_message
67 { 71 {
...@@ -70,10 +74,9 @@ struct _mh_message ...@@ -70,10 +74,9 @@ struct _mh_message
70 74
71 stream_t stream; /* Associated file stream */ 75 stream_t stream; /* Associated file stream */
72 off_t body_start; /* Offset of body start in the message file */ 76 off_t body_start; /* Offset of body start in the message file */
73 off_t body_end; /* Offset of body end (size of file, effectively */ 77 off_t body_end; /* Offset of body end (size of file, effectively)*/
74 78
75 size_t seq_number; /* message sequence number */ 79 size_t seq_number; /* message sequence number */
76 size_t uid; /* IMAP uid. */
77 80
78 int attr_flags; /* Attribute flags */ 81 int attr_flags; /* Attribute flags */
79 int deleted; /* Was the message originally deleted */ 82 int deleted; /* Was the message originally deleted */
...@@ -94,7 +97,6 @@ struct _mh_data ...@@ -94,7 +97,6 @@ struct _mh_data
94 struct _mh_message *msg_tail; /* Last */ 97 struct _mh_message *msg_tail; /* Last */
95 98
96 unsigned long uidvalidity; 99 unsigned long uidvalidity;
97 size_t uidnext; /* Next available UID */
98 100
99 char *name; /* Directory name */ 101 char *name; /* Directory name */
100 102
...@@ -517,7 +519,7 @@ _mh_message_save (struct _mh_data *mhd, struct _mh_message *mhm, int expunge) ...@@ -517,7 +519,7 @@ _mh_message_save (struct _mh_data *mhd, struct _mh_message *mhm, int expunge)
517 519
518 if (!(strncasecmp (buf, "status:", 7) == 0 520 if (!(strncasecmp (buf, "status:", 7) == 0
519 || strncasecmp (buf, "x-imapbase:", 11) == 0 521 || strncasecmp (buf, "x-imapbase:", 11) == 0
520 || strncasecmp (buf, "x-iud:", 6) == 0)) 522 || strncasecmp (buf, "x-uid:", 6) == 0))
521 fprintf (fp, "%s", buf); 523 fprintf (fp, "%s", buf);
522 off += n; 524 off += n;
523 } 525 }
...@@ -527,17 +529,13 @@ _mh_message_save (struct _mh_data *mhd, struct _mh_message *mhm, int expunge) ...@@ -527,17 +529,13 @@ _mh_message_save (struct _mh_data *mhd, struct _mh_message *mhm, int expunge)
527 529
528 /* Add imapbase */ 530 /* Add imapbase */
529 if (!mhd->msg_head || (mhd->msg_head == mhm)) /*FIXME*/ 531 if (!mhd->msg_head || (mhd->msg_head == mhm)) /*FIXME*/
530 fprintf (fp, "X-IMAPbase: %lu %u\n", mhd->uidvalidity, mhd->uidnext); 532 fprintf (fp, "X-IMAPbase: %lu %u\n", mhd->uidvalidity, _mh_next_seq(mhd));
531 533
532 /* Add status */ 534 /* Add status */
533 message_get_attribute (msg, &attr); 535 message_get_attribute (msg, &attr);
534 attribute_to_string (attr, buf, bsize, &n); 536 attribute_to_string (attr, buf, bsize, &n);
535 fprintf (fp, "%s", buf); 537 fprintf (fp, "%s", buf);
536 538
537 /* Add UID */
538 fprintf (fp, "X-UID: %d\n", mhm->uid);
539 fprintf (fp, "\n");
540
541 /* Copy message body */ 539 /* Copy message body */
542 540
543 message_get_body (msg, &body); 541 message_get_body (msg, &body);
...@@ -587,7 +585,6 @@ mh_append_message (mailbox_t mailbox, message_t msg) ...@@ -587,7 +585,6 @@ mh_append_message (mailbox_t mailbox, message_t msg)
587 585
588 mhm->mhd = mhd; 586 mhm->mhd = mhd;
589 mhm->seq_number = _mh_next_seq (mhd); 587 mhm->seq_number = _mh_next_seq (mhd);
590 mhm->uid = mhd->uidnext++;
591 mhm->message = msg; 588 mhm->message = msg;
592 status = _mh_message_save (mhd, mhm, 0); 589 status = _mh_message_save (mhd, mhm, 0);
593 mhm->message = NULL; 590 mhm->message = NULL;
...@@ -763,7 +760,7 @@ mh_uidnext (mailbox_t mailbox, size_t *puidnext) ...@@ -763,7 +760,7 @@ mh_uidnext (mailbox_t mailbox, size_t *puidnext)
763 return status; 760 return status;
764 } 761 }
765 if (puidnext) 762 if (puidnext)
766 *puidnext = mhd->uidnext; 763 *puidnext = _mh_next_seq(mhd);
767 return 0; 764 return 0;
768 } 765 }
769 766
...@@ -776,34 +773,6 @@ mh_cleanup (void *arg) ...@@ -776,34 +773,6 @@ mh_cleanup (void *arg)
776 locker_unlock (mailbox->locker); 773 locker_unlock (mailbox->locker);
777 } 774 }
778 775
779 /* Reset the IMAP uids, if necessary. UID according to IMAP RFC is a 32 bit
780 ascending number for each messages */
781 static void
782 mh_reset_imap_uids (struct _mh_data *mhd)
783 {
784 size_t uid;
785 size_t ouid;
786 struct _mh_message *msg;
787
788 for (uid = ouid = 0, msg = mhd->msg_head; msg; msg = msg->next)
789 {
790 uid = msg->uid;
791 if (uid <= ouid)
792 {
793 uid = ouid + 1;
794 msg->uid = ouid = uid;
795 msg->attr_flags |= MU_ATTRIBUTE_MODIFIED;
796 }
797 else
798 ouid = uid;
799 }
800 if (mhd->msg_count > 0 && uid >= mhd->uidnext)
801 {
802 mhd->uidnext = uid + 1;
803 mhd->msg_head->attr_flags |= MU_ATTRIBUTE_MODIFIED;
804 }
805 }
806
807 /* Insert message msg into the message list on the appropriate position */ 776 /* Insert message msg into the message list on the appropriate position */
808 static void 777 static void
809 _mh_message_insert (struct _mh_data *mhd, struct _mh_message *msg) 778 _mh_message_insert (struct _mh_data *mhd, struct _mh_message *msg)
...@@ -911,11 +880,7 @@ mh_scan_message (struct _mh_message *mhm) ...@@ -911,11 +880,7 @@ mh_scan_message (struct _mh_message *mhm)
911 { 880 {
912 char *p; 881 char *p;
913 mhm->mhd->uidvalidity = strtoul (buf + 11, &p, 10); 882 mhm->mhd->uidvalidity = strtoul (buf + 11, &p, 10);
914 mhm->mhd->uidnext = strtoul (p, NULL, 10); 883 /* second number is next uid. Ignored */
915 }
916 else if (strncasecmp (buf, "x-uid:", 6) == 0)
917 {
918 mhm->uid = strtoul (buf + 6, NULL, 10);
919 } 884 }
920 } 885 }
921 else 886 else
...@@ -1030,15 +995,13 @@ mh_scan0 (mailbox_t mailbox, size_t msgno, size_t *pcount) ...@@ -1030,15 +995,13 @@ mh_scan0 (mailbox_t mailbox, size_t msgno, size_t *pcount)
1030 if (mhd->uidvalidity == 0) 995 if (mhd->uidvalidity == 0)
1031 { 996 {
1032 mhd->uidvalidity = (unsigned long)time (NULL); 997 mhd->uidvalidity = (unsigned long)time (NULL);
1033 mhd->uidnext = mhd->msg_count + 1; 998 //FIXME mhd->uidnext = mhd->msg_count + 1;
1034 /* Tell that we have been modified for expunging. */ 999 /* Tell that we have been modified for expunging. */
1035 if (mhd->msg_head) 1000 if (mhd->msg_head)
1036 mhd->msg_head->attr_flags |= MU_ATTRIBUTE_MODIFIED; 1001 mhd->msg_head->attr_flags |= MU_ATTRIBUTE_MODIFIED;
1037 } 1002 }
1038 } 1003 }
1039 1004
1040 mh_reset_imap_uids (mhd);
1041
1042 /* Clean up the things */ 1005 /* Clean up the things */
1043 1006
1044 mh_cleanup (mailbox); 1007 mh_cleanup (mailbox);
...@@ -1292,7 +1255,7 @@ mh_message_uid (message_t msg, size_t *puid) ...@@ -1292,7 +1255,7 @@ mh_message_uid (message_t msg, size_t *puid)
1292 { 1255 {
1293 struct _mh_message *mhm = message_get_owner (msg); 1256 struct _mh_message *mhm = message_get_owner (msg);
1294 if (puid) 1257 if (puid)
1295 *puid = mhm->uid; 1258 *puid = mhm->seq_number;
1296 return 0; 1259 return 0;
1297 } 1260 }
1298 1261
...@@ -1432,8 +1395,7 @@ mh_envelope_sender (envelope_t envelope, char *buf, size_t len, size_t *psize) ...@@ -1432,8 +1395,7 @@ mh_envelope_sender (envelope_t envelope, char *buf, size_t len, size_t *psize)
1432 1395
1433 mh_pool_open (mhm); 1396 mh_pool_open (mhm);
1434 1397
1435 status = stream_readline (mhm->stream, buffer, sizeof(buffer), 1398 status = stream_readline (mhm->stream, buffer, sizeof(buffer), 0, &n);
1436 0, &n);
1437 if (status != 0) 1399 if (status != 0)
1438 { 1400 {
1439 if (psize) 1401 if (psize)
...@@ -1464,11 +1426,4 @@ mh_envelope_sender (envelope_t envelope, char *buf, size_t len, size_t *psize) ...@@ -1464,11 +1426,4 @@ mh_envelope_sender (envelope_t envelope, char *buf, size_t len, size_t *psize)
1464 return 0; 1426 return 0;
1465 } 1427 }
1466 1428
1467 int 1429
1468 mh_message_number (message_t msg, size_t *pnum)
1469 {
1470 struct _mh_message *mhm = message_get_owner (msg);
1471 if (pnum)
1472 *pnum = mhm->seq_number;
1473 return 0;
1474 }
......