Skip to content

Commit f9aa2a1

Browse files
committed
mailbox.c: store INTERNALDATE as nanoseconds since epoch
1 parent e162801 commit f9aa2a1

23 files changed

+209
-116
lines changed

Diff for: cunit/annotate.testc

+5-3
Original file line numberDiff line numberDiff line change
@@ -2002,12 +2002,14 @@ static int create_messages(struct mailbox *mailbox, int count)
20022002
struct appendstate as;
20032003
quota_t qdiffs[QUOTA_NUMRESOURCES] = QUOTA_DIFFS_DONTCARE_INITIALIZER;
20042004
FILE *fp;
2005-
time_t internaldate = time(NULL);
2005+
struct timespec internaldate;
20062006
struct body *body = NULL;
20072007
struct buf buf = BUF_INITIALIZER;
20082008

2009+
clock_gettime(CLOCK_REALTIME, &internaldate);
2010+
20092011
/* Write the message to the filesystem */
2010-
if (!(fp = append_newstage(mailbox_name(mailbox), internaldate, 0, &stage))) {
2012+
if (!(fp = append_newstage(mailbox_name(mailbox), internaldate.tv_sec, 0, &stage))) {
20112013
fprintf(stderr, "append_newstage(%s) failed", mailbox_name(mailbox));
20122014
return IMAP_IOERROR;
20132015
}
@@ -2028,7 +2030,7 @@ static int create_messages(struct mailbox *mailbox, int count)
20282030
strerror(errno));
20292031
return r;
20302032
}
2031-
r = append_fromstage(&as, &body, stage, internaldate, 0, NULL, 0, NULL);
2033+
r = append_fromstage(&as, &body, stage, &internaldate, 0, NULL, 0, NULL);
20322034
if (r) {
20332035
fprintf(stderr, "append_fromstage(%s) failed: %s", mailbox_name(mailbox),
20342036
error_message(r));

Diff for: imap/append.c

+15-7
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ static int findstage_cb(const conv_guidrec_t *rec, void *vrock)
935935
*/
936936
EXPORTED int append_fromstage_full(struct appendstate *as, struct body **body,
937937
struct stagemsg *stage,
938-
time_t internaldate, time_t savedate,
938+
struct timespec *internaldate, time_t savedate,
939939
modseq_t createdmodseq,
940940
const strarray_t *flags, int nolink,
941941
struct entryattlist **user_annotsp)
@@ -1057,8 +1057,6 @@ EXPORTED int append_fromstage_full(struct appendstate *as, struct body **body,
10571057
msgrec = msgrecord_new(mailbox);
10581058
r = msgrecord_set_uid(msgrec, uid);
10591059
if (r) goto out;
1060-
r = msgrecord_set_internaldate(msgrec, internaldate);
1061-
if (r) goto out;
10621060
r = msgrecord_set_createdmodseq(msgrec, createdmodseq);
10631061
if (r) goto out;
10641062
r = msgrecord_set_bodystructure(msgrec, *body);
@@ -1069,9 +1067,12 @@ EXPORTED int append_fromstage_full(struct appendstate *as, struct body **body,
10691067
}
10701068

10711069
/* And make sure it has a timestamp */
1072-
r = msgrecord_get_internaldate(msgrec, &internaldate);
1073-
if (!r && !internaldate)
1074-
r = msgrecord_set_internaldate(msgrec, time(NULL));
1070+
struct timespec now;
1071+
if (!internaldate) {
1072+
clock_gettime(CLOCK_REALTIME, &now);
1073+
internaldate = &now;
1074+
}
1075+
r = msgrecord_set_internaldate(msgrec, internaldate);
10751076
if (r) goto out;
10761077

10771078
/* should we archive it straight away? */
@@ -1252,7 +1253,7 @@ EXPORTED int append_removestage(struct stagemsg *stage)
12521253
EXPORTED int append_fromstream(struct appendstate *as, struct body **body,
12531254
struct protstream *messagefile,
12541255
unsigned long size,
1255-
time_t internaldate,
1256+
struct timespec *internaldate,
12561257
const strarray_t *flags)
12571258
{
12581259
struct mailbox *mailbox = as->mailbox;
@@ -1268,6 +1269,13 @@ EXPORTED int append_fromstream(struct appendstate *as, struct body **body,
12681269
msgrec = msgrecord_new(mailbox);
12691270
r = msgrecord_set_uid(msgrec, as->baseuid + as->nummsg);
12701271
if (r) goto out;
1272+
1273+
/* And make sure it has a timestamp */
1274+
struct timespec now;
1275+
if (!internaldate) {
1276+
clock_gettime(CLOCK_REALTIME, &now);
1277+
internaldate = &now;
1278+
}
12711279
r = msgrecord_set_internaldate(msgrec, internaldate);
12721280
if (r) goto out;
12731281

Diff for: imap/append.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ extern FILE *append_newstage_full(const char *mailboxname, time_t internaldate,
121121
/* adds a new mailbox to the stage initially created by append_newstage() */
122122
extern int append_fromstage_full(struct appendstate *mailbox, struct body **body,
123123
struct stagemsg *stage,
124-
time_t internaldate, time_t savedate,
124+
struct timespec *internaldate, time_t savedate,
125125
modseq_t createdmodseq,
126126
const strarray_t *flags, int nolink,
127127
struct entryattlist **annotations);
@@ -133,7 +133,7 @@ extern int append_removestage(struct stagemsg *stage);
133133

134134
extern int append_fromstream(struct appendstate *as, struct body **body,
135135
struct protstream *messagefile,
136-
unsigned long size, time_t internaldate,
136+
unsigned long size, struct timespec *internaldate,
137137
const strarray_t *flags);
138138

139139
extern int append_copy(struct mailbox *mailbox,

Diff for: imap/caldav_alarm.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1521,7 +1521,7 @@ static int move_to_mailboxid(struct mailbox *srcmbox,
15211521
if (r) goto done;
15221522

15231523
/* Append the message to the mailbox */
1524-
r = append_fromstage_full(&as, &body, stage, record->internaldate.tv_sec,
1524+
r = append_fromstage_full(&as, &body, stage, &record->internaldate,
15251525
savedate, 0, flags, 0, &annots);
15261526
if (r) {
15271527
append_abort(&as);

Diff for: imap/carddav_db.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,7 @@ static int _carddav_store(struct mailbox *mailbox, struct buf *vcard,
12101210
char *header;
12111211
quota_t qdiffs[QUOTA_NUMRESOURCES] = QUOTA_DIFFS_DONTCARE_INITIALIZER;
12121212
struct appendstate as;
1213-
time_t now = time(0);
1213+
struct timespec now;
12141214
char *freeme = NULL;
12151215
char datestr[80];
12161216
static int64_t vcard_max_size = -1;
@@ -1223,8 +1223,10 @@ static int _carddav_store(struct mailbox *mailbox, struct buf *vcard,
12231223

12241224
init_internal();
12251225

1226+
clock_gettime(CLOCK_REALTIME, &now);
1227+
12261228
/* Prepare to stage the message */
1227-
if (!(f = append_newstage(mailbox_name(mailbox), now, 0, &stage))) {
1229+
if (!(f = append_newstage(mailbox_name(mailbox), now.tv_sec, 0, &stage))) {
12281230
syslog(LOG_ERR, "append_newstage(%s) failed", mailbox_name(mailbox));
12291231
return -1;
12301232
}
@@ -1243,7 +1245,7 @@ static int _carddav_store(struct mailbox *mailbox, struct buf *vcard,
12431245
if (!resource) resource = freeme = strconcat(uid, ".vcf", (char *)NULL);
12441246
mbuserid = mboxname_to_userid(mailbox_name(mailbox));
12451247

1246-
time_to_rfc5322(now, datestr, sizeof(datestr));
1248+
time_to_rfc5322(now.tv_sec, datestr, sizeof(datestr));
12471249

12481250
/* XXX This needs to be done via an LDAP/DB lookup */
12491251
header = charset_encode_mimeheader(mbuserid, 0, 0);
@@ -1299,7 +1301,7 @@ static int _carddav_store(struct mailbox *mailbox, struct buf *vcard,
12991301

13001302
struct body *body = NULL;
13011303

1302-
r = append_fromstage(&as, &body, stage, now, createdmodseq, flags, 0, annots);
1304+
r = append_fromstage(&as, &body, stage, &now, createdmodseq, flags, 0, annots);
13031305
if (body) {
13041306
message_free_body(body);
13051307
free(body);

Diff for: imap/cyr_virusscan.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ static void append_notifications(const struct buf *template)
717717
if (i_mbox->msgs) {
718718
FILE *f = NULL;
719719
struct infected_msg *msg;
720-
time_t t;
720+
struct timespec now;
721721
struct protstream *pout;
722722
struct appendstate as;
723723
struct body *body = NULL;
@@ -739,8 +739,8 @@ static void append_notifications(const struct buf *template)
739739
goto user_done;
740740
}
741741

742-
t = time(NULL);
743-
put_notification_headers(f, outgoing_count++, t, owner);
742+
clock_gettime(CLOCK_REALTIME, &now);
743+
put_notification_headers(f, outgoing_count++, now.tv_sec, owner);
744744

745745
first = 1;
746746
while ((msg = i_mbox->msgs)) {
@@ -804,7 +804,7 @@ static void append_notifications(const struct buf *template)
804804
if (!r) {
805805
pout = prot_new(fd, 0);
806806
prot_rewind(pout);
807-
r = append_fromstream(&as, &body, pout, msgsize, t, NULL);
807+
r = append_fromstream(&as, &body, pout, msgsize, &now, NULL);
808808
/* n.b. append_fromstream calls append_abort itself if it fails */
809809
if (!r) r = append_commit(&as);
810810

Diff for: imap/dav_util.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ EXPORTED int dav_store_resource(struct transaction_t *txn,
257257
}
258258

259259
/* Append the message to the mailbox */
260-
if ((r = append_fromstage(&as, &body, stage, now,
260+
if ((r = append_fromstage(&as, &body, stage, NULL,
261261
createdmodseq, flaglist, 0, &annots))) {
262262
syslog(LOG_ERR, "append_fromstage(%s) failed: %s",
263263
mailbox_name(mailbox), error_message(r));

Diff for: imap/http_jmap.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ static int jmap_upload(struct transaction_t *txn)
10251025
struct stagemsg *stage = NULL;
10261026
FILE *f = NULL;
10271027
const char **hdr;
1028-
time_t now = time(NULL);
1028+
struct timespec now;
10291029
struct appendstate as;
10301030
char *accountid = NULL;
10311031
char *normalisedtype = NULL;
@@ -1079,8 +1079,10 @@ static int jmap_upload(struct transaction_t *txn)
10791079
goto done;
10801080
}
10811081

1082+
clock_gettime(CLOCK_REALTIME, &now);
1083+
10821084
/* Prepare to stage the message */
1083-
if (!(f = append_newstage(mailbox_name(mailbox), now, 0, &stage))) {
1085+
if (!(f = append_newstage(mailbox_name(mailbox), now.tv_sec, 0, &stage))) {
10841086
syslog(LOG_ERR, "append_newstage(%s) failed", mailbox_name(mailbox));
10851087
txn->error.desc = "append_newstage() failed";
10861088
ret = HTTP_SERVER_ERROR;
@@ -1156,7 +1158,7 @@ static int jmap_upload(struct transaction_t *txn)
11561158
}
11571159
else {
11581160
char datestr[80];
1159-
time_to_rfc5322(now, datestr, sizeof(datestr));
1161+
time_to_rfc5322(now.tv_sec, datestr, sizeof(datestr));
11601162
fprintf(f, "Date: %s\r\n", datestr);
11611163
}
11621164

@@ -1203,7 +1205,7 @@ static int jmap_upload(struct transaction_t *txn)
12031205
/* Append the message to the mailbox */
12041206
strarray_append(&flags, "\\Deleted");
12051207
strarray_append(&flags, "\\Expunged"); // custom flag to insta-expunge!
1206-
r = append_fromstage(&as, &body, stage, now, 0, &flags, 0, /*annots*/NULL);
1208+
r = append_fromstage(&as, &body, stage, &now, 0, &flags, 0, /*annots*/NULL);
12071209

12081210
if (r) {
12091211
append_abort(&as);
@@ -1231,7 +1233,7 @@ static int jmap_upload(struct transaction_t *txn)
12311233
}
12321234

12331235
char datestr[RFC3339_DATETIME_MAX];
1234-
time_to_rfc3339(now + 86400, datestr, RFC3339_DATETIME_MAX);
1236+
time_to_rfc3339(now.tv_sec + 86400, datestr, RFC3339_DATETIME_MAX);
12351237

12361238
char blob_id[JMAP_BLOBID_SIZE];
12371239
jmap_set_blobid(rawmessage ? &body->guid : &body->content_guid, blob_id);

Diff for: imap/imapd.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ struct appendstage {
254254
struct stagemsg *stage;
255255
FILE *f;
256256
strarray_t flags;
257-
time_t internaldate;
257+
struct timespec internaldate;
258258
int binary;
259259
struct entryattlist *annotations;
260260
};
@@ -4310,6 +4310,9 @@ static int cmd_append(char *tag, char *name, const char *cur_name, int isreplace
43104310
curstage = xzmalloc(sizeof(*curstage));
43114311
ptrarray_push(&stages, curstage);
43124312

4313+
/* Initialize the internaldate to "now" */
4314+
clock_gettime(CLOCK_REALTIME, &curstage->internaldate);
4315+
43134316
/* Set limit on the total number of bytes allowed for mailbox+append-opts */
43144317
maxargssize_mark = prot_bytes_in(imapd_in) + (maxargssize - strlen(name));
43154318

@@ -4349,7 +4352,7 @@ static int cmd_append(char *tag, char *name, const char *cur_name, int isreplace
43494352
/* Parse internaldate */
43504353
if (c == '\"' && !arg.s[0]) {
43514354
prot_ungetc(c, imapd_in);
4352-
c = getdatetime(&(curstage->internaldate));
4355+
c = getdatetime(&(curstage->internaldate.tv_sec));
43534356
if (c != ' ') {
43544357
parseerr = "Invalid date-time in Append command";
43554358
r = IMAP_PROTOCOL_ERROR;
@@ -4517,7 +4520,7 @@ static int cmd_append(char *tag, char *name, const char *cur_name, int isreplace
45174520
}
45184521
if (!r) {
45194522
r = append_fromstage(&appendstate, &body, curstage->stage,
4520-
curstage->internaldate, /*createdmodseq*/0,
4523+
&curstage->internaldate, /*createdmodseq*/0,
45214524
&curstage->flags, 0,
45224525
&curstage->annotations);
45234526
}

Diff for: imap/jmap_backup.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,8 @@ static int recreate_resource(message_t *msg, struct mailbox *tomailbox,
562562
strarray_add(flags, "$restored");
563563

564564
/* append the message to the mailbox. */
565-
r = append_fromstage(&as, &body, stage, record->internaldate.tv_sec,
565+
r = append_fromstage(&as, &body, stage,
566+
(struct timespec *) &record->internaldate,
566567
is_update ? record->createdmodseq : 0,
567568
flags, /*nolink*/0, &annots);
568569

0 commit comments

Comments
 (0)