-
Notifications
You must be signed in to change notification settings - Fork 271
/
dsync-mailbox-import.c
2849 lines (2549 loc) · 87.8 KB
/
dsync-mailbox-import.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2013-2016 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "hash.h"
#include "str.h"
#include "hex-binary.h"
#include "istream.h"
#include "seq-range-array.h"
#include "imap-util.h"
#include "mail-storage-private.h"
#include "mail-search-build.h"
#include "dsync-transaction-log-scan.h"
#include "dsync-mail.h"
#include "dsync-mailbox.h"
#include "dsync-mailbox-import.h"
struct importer_mail {
const char *guid;
uint32_t uid;
};
struct importer_new_mail {
/* linked list of mails for this GUID */
struct importer_new_mail *next;
/* if non-NULL, this mail exists in both local and remote. this link
points to the other side. */
struct importer_new_mail *link;
const char *guid;
struct dsync_mail_change *change;
/* the final UID for the message */
uint32_t final_uid;
/* the original local UID, or 0 if exists only remotely */
uint32_t local_uid;
/* the original remote UID, or 0 if exists only remotely */
uint32_t remote_uid;
/* UID for the mail in the virtual \All mailbox */
uint32_t virtual_all_uid;
unsigned int uid_in_local:1;
unsigned int uid_is_usable:1;
unsigned int skip:1;
unsigned int expunged:1;
unsigned int copy_failed:1;
};
/* for quickly testing that two-way sync doesn't actually do any unexpected
modifications. */
#define IMPORTER_DEBUG_CHANGE(importer) /*i_assert(!importer->master_brain)*/
HASH_TABLE_DEFINE_TYPE(guid_new_mail, const char *, struct importer_new_mail *);
HASH_TABLE_DEFINE_TYPE(uid_new_mail, void *, struct importer_new_mail *);
struct dsync_mailbox_importer {
pool_t pool;
struct mailbox *box;
uint32_t last_common_uid;
uint64_t last_common_modseq, last_common_pvt_modseq;
uint32_t remote_uid_next;
uint32_t remote_first_recent_uid;
uint64_t remote_highest_modseq, remote_highest_pvt_modseq;
time_t sync_since_timestamp;
enum mailbox_transaction_flags transaction_flags;
unsigned int hdr_hash_version;
enum mail_flags sync_flag;
const char *sync_keyword;
bool sync_flag_dontwant;
struct mailbox_transaction_context *trans, *ext_trans;
struct mail_search_context *search_ctx;
struct mail *mail, *ext_mail;
struct mailbox *virtual_all_box;
struct mailbox_transaction_context *virtual_trans;
struct mail *virtual_mail;
struct mail *cur_mail;
const char *cur_guid;
const char *cur_hdr_hash;
/* UID => struct dsync_mail_change */
HASH_TABLE_TYPE(dsync_uid_mail_change) local_changes;
HASH_TABLE_TYPE(dsync_attr_change) local_attr_changes;
ARRAY_TYPE(seq_range) maybe_expunge_uids;
ARRAY(struct dsync_mail_change *) maybe_saves;
/* GUID => struct importer_new_mail */
HASH_TABLE_TYPE(guid_new_mail) import_guids;
/* UID => struct importer_new_mail */
HASH_TABLE_TYPE(uid_new_mail) import_uids;
ARRAY(struct importer_new_mail *) newmails;
ARRAY_TYPE(uint32_t) wanted_uids;
ARRAY_TYPE(uint32_t) saved_uids;
uint32_t highest_wanted_uid;
ARRAY(struct dsync_mail_request) mail_requests;
unsigned int mail_request_idx;
uint32_t prev_uid, next_local_seq, local_uid_next;
uint64_t local_initial_highestmodseq, local_initial_highestpvtmodseq;
unsigned int import_pos, import_count;
enum mail_error mail_error;
unsigned int failed:1;
unsigned int require_full_resync:1;
unsigned int debug:1;
unsigned int stateful_import:1;
unsigned int last_common_uid_found:1;
unsigned int cur_uid_has_change:1;
unsigned int cur_mail_skip:1;
unsigned int local_expunged_guids_set:1;
unsigned int new_uids_assigned:1;
unsigned int want_mail_requests:1;
unsigned int master_brain:1;
unsigned int revert_local_changes:1;
unsigned int mails_have_guids:1;
unsigned int mails_use_guid128:1;
unsigned int delete_mailbox:1;
};
static bool dsync_mailbox_save_newmails(struct dsync_mailbox_importer *importer,
const struct dsync_mail *mail,
struct importer_new_mail *all_newmails,
bool remote_mail);
static int dsync_mailbox_import_commit(struct dsync_mailbox_importer *importer,
bool final);
static void ATTR_FORMAT(2, 3)
imp_debug(struct dsync_mailbox_importer *importer, const char *fmt, ...)
{
va_list args;
if (importer->debug) T_BEGIN {
va_start(args, fmt);
i_debug("brain %c: Import %s: %s",
importer->master_brain ? 'M' : 'S',
mailbox_get_vname(importer->box),
t_strdup_vprintf(fmt, args));
va_end(args);
} T_END;
}
static void
dsync_import_unexpected_state(struct dsync_mailbox_importer *importer,
const char *error)
{
if (!importer->stateful_import) {
i_error("Mailbox %s: %s", mailbox_get_vname(importer->box),
error);
} else {
i_warning("Mailbox %s doesn't match previous state: %s "
"(dsync must be run again without the state)",
mailbox_get_vname(importer->box), error);
}
importer->require_full_resync = TRUE;
}
static void
dsync_mailbox_import_search_init(struct dsync_mailbox_importer *importer)
{
struct mail_search_args *search_args;
struct mail_search_arg *sarg;
search_args = mail_search_build_init();
sarg = mail_search_build_add(search_args, SEARCH_UIDSET);
p_array_init(&sarg->value.seqset, search_args->pool, 128);
seq_range_array_add_range(&sarg->value.seqset,
importer->last_common_uid+1, (uint32_t)-1);
importer->search_ctx =
mailbox_search_init(importer->trans, search_args, NULL,
0, NULL);
mail_search_args_unref(&search_args);
if (mailbox_search_next(importer->search_ctx, &importer->cur_mail))
importer->next_local_seq = importer->cur_mail->seq;
/* this flag causes cur_guid to be looked up later */
importer->cur_mail_skip = TRUE;
}
static void
dsync_mailbox_import_transaction_begin(struct dsync_mailbox_importer *importer)
{
const enum mailbox_transaction_flags ext_trans_flags =
importer->transaction_flags |
MAILBOX_TRANSACTION_FLAG_EXTERNAL |
MAILBOX_TRANSACTION_FLAG_ASSIGN_UIDS;
importer->trans = mailbox_transaction_begin(importer->box,
importer->transaction_flags);
importer->ext_trans = mailbox_transaction_begin(importer->box,
ext_trans_flags);
importer->mail = mail_alloc(importer->trans, 0, NULL);
importer->ext_mail = mail_alloc(importer->ext_trans, 0, NULL);
}
struct dsync_mailbox_importer *
dsync_mailbox_import_init(struct mailbox *box,
struct mailbox *virtual_all_box,
struct dsync_transaction_log_scan *log_scan,
uint32_t last_common_uid,
uint64_t last_common_modseq,
uint64_t last_common_pvt_modseq,
uint32_t remote_uid_next,
uint32_t remote_first_recent_uid,
uint64_t remote_highest_modseq,
uint64_t remote_highest_pvt_modseq,
time_t sync_since_timestamp, const char *sync_flag,
enum dsync_mailbox_import_flags flags)
{
struct dsync_mailbox_importer *importer;
struct mailbox_status status;
pool_t pool;
pool = pool_alloconly_create(MEMPOOL_GROWING"dsync mailbox importer",
10240);
importer = p_new(pool, struct dsync_mailbox_importer, 1);
importer->pool = pool;
importer->box = box;
importer->virtual_all_box = virtual_all_box;
importer->last_common_uid = last_common_uid;
importer->last_common_modseq = last_common_modseq;
importer->last_common_pvt_modseq = last_common_pvt_modseq;
importer->last_common_uid_found =
last_common_uid != 0 || last_common_modseq != 0;
importer->remote_uid_next = remote_uid_next;
importer->remote_first_recent_uid = remote_first_recent_uid;
importer->remote_highest_modseq = remote_highest_modseq;
importer->remote_highest_pvt_modseq = remote_highest_pvt_modseq;
importer->sync_since_timestamp = sync_since_timestamp;
importer->stateful_import = importer->last_common_uid_found;
if (sync_flag != NULL) {
if (sync_flag[0] == '-') {
importer->sync_flag_dontwant = TRUE;
sync_flag++;
}
if (sync_flag[0] == '\\')
importer->sync_flag = imap_parse_system_flag(sync_flag);
else
importer->sync_keyword = p_strdup(pool, sync_flag);
}
importer->transaction_flags = MAILBOX_TRANSACTION_FLAG_SYNC;
if ((flags & DSYNC_MAILBOX_IMPORT_FLAG_NO_NOTIFY) != 0)
importer->transaction_flags |= MAILBOX_TRANSACTION_FLAG_NO_NOTIFY;
hash_table_create(&importer->import_guids, pool, 0, str_hash, strcmp);
hash_table_create_direct(&importer->import_uids, pool, 0);
i_array_init(&importer->maybe_expunge_uids, 16);
i_array_init(&importer->maybe_saves, 128);
i_array_init(&importer->newmails, 128);
i_array_init(&importer->wanted_uids, 128);
i_array_init(&importer->saved_uids, 128);
dsync_mailbox_import_transaction_begin(importer);
if ((flags & DSYNC_MAILBOX_IMPORT_FLAG_WANT_MAIL_REQUESTS) != 0) {
i_array_init(&importer->mail_requests, 128);
importer->want_mail_requests = TRUE;
}
importer->master_brain =
(flags & DSYNC_MAILBOX_IMPORT_FLAG_MASTER_BRAIN) != 0;
importer->revert_local_changes =
(flags & DSYNC_MAILBOX_IMPORT_FLAG_REVERT_LOCAL_CHANGES) != 0;
importer->debug = (flags & DSYNC_MAILBOX_IMPORT_FLAG_DEBUG) != 0;
importer->mails_have_guids =
(flags & DSYNC_MAILBOX_IMPORT_FLAG_MAILS_HAVE_GUIDS) != 0;
importer->mails_use_guid128 =
(flags & DSYNC_MAILBOX_IMPORT_FLAG_MAILS_USE_GUID128) != 0;
importer->hdr_hash_version =
(flags & DSYNC_MAILBOX_IMPORT_FLAG_HDR_HASH_V2) != 0 ? 2 : 1;
mailbox_get_open_status(importer->box, STATUS_UIDNEXT |
STATUS_HIGHESTMODSEQ | STATUS_HIGHESTPVTMODSEQ,
&status);
importer->local_uid_next = status.uidnext;
importer->local_initial_highestmodseq = status.highest_modseq;
importer->local_initial_highestpvtmodseq = status.highest_pvt_modseq;
dsync_mailbox_import_search_init(importer);
if (!importer->stateful_import)
;
else if (importer->local_uid_next <= last_common_uid) {
dsync_import_unexpected_state(importer, t_strdup_printf(
"local UIDNEXT %u <= last common UID %u",
importer->local_uid_next, last_common_uid));
} else if (importer->local_initial_highestmodseq < last_common_modseq) {
dsync_import_unexpected_state(importer, t_strdup_printf(
"local HIGHESTMODSEQ %llu < last common HIGHESTMODSEQ %llu",
(unsigned long long)importer->local_initial_highestmodseq,
(unsigned long long)last_common_modseq));
} else if (importer->local_initial_highestpvtmodseq < last_common_pvt_modseq) {
dsync_import_unexpected_state(importer, t_strdup_printf(
"local HIGHESTMODSEQ %llu < last common HIGHESTMODSEQ %llu",
(unsigned long long)importer->local_initial_highestpvtmodseq,
(unsigned long long)last_common_pvt_modseq));
}
importer->local_changes = dsync_transaction_log_scan_get_hash(log_scan);
importer->local_attr_changes = dsync_transaction_log_scan_get_attr_hash(log_scan);
return importer;
}
static int
dsync_mailbox_import_lookup_attr(struct dsync_mailbox_importer *importer,
enum mail_attribute_type type, const char *key,
struct dsync_mailbox_attribute **attr_r)
{
struct dsync_mailbox_attribute lookup_attr, *attr;
const struct dsync_mailbox_attribute *attr_change;
struct mail_attribute_value value;
*attr_r = NULL;
if (mailbox_attribute_get_stream(importer->trans, type, key, &value) < 0) {
i_error("Mailbox %s: Failed to get attribute %s: %s",
mailbox_get_vname(importer->box), key,
mailbox_get_last_error(importer->box, &importer->mail_error));
importer->failed = TRUE;
return -1;
}
lookup_attr.type = type;
lookup_attr.key = key;
attr_change = hash_table_lookup(importer->local_attr_changes,
&lookup_attr);
if (attr_change == NULL &&
value.value == NULL && value.value_stream == NULL) {
/* we have no knowledge of this attribute */
return 0;
}
attr = t_new(struct dsync_mailbox_attribute, 1);
attr->type = type;
attr->key = key;
attr->value = value.value;
attr->value_stream = value.value_stream;
attr->last_change = value.last_change;
if (attr_change != NULL) {
attr->deleted = attr_change->deleted &&
!DSYNC_ATTR_HAS_VALUE(attr);
attr->modseq = attr_change->modseq;
}
*attr_r = attr;
return 0;
}
static int
dsync_istreams_cmp(struct istream *input1, struct istream *input2, int *cmp_r)
{
const unsigned char *data1, *data2;
size_t size1, size2, size;
*cmp_r = -1; /* quiet gcc */
for (;;) {
(void)i_stream_read_data(input1, &data1, &size1, 0);
(void)i_stream_read_data(input2, &data2, &size2, 0);
if (size1 == 0 || size2 == 0)
break;
size = I_MIN(size1, size2);
*cmp_r = memcmp(data1, data2, size);
if (*cmp_r != 0)
return 0;
i_stream_skip(input1, size);
i_stream_skip(input2, size);
}
if (input1->stream_errno != 0) {
errno = input1->stream_errno;
i_error("read(%s) failed: %s", i_stream_get_name(input1),
i_stream_get_error(input1));
return -1;
}
if (input2->stream_errno != 0) {
errno = input2->stream_errno;
i_error("read(%s) failed: %s", i_stream_get_name(input2),
i_stream_get_error(input2));
return -1;
}
if (size1 == 0 && size2 == 0)
*cmp_r = 0;
else
*cmp_r = size1 == 0 ? -1 : 1;
return 0;
}
static int
dsync_attributes_cmp_values(const struct dsync_mailbox_attribute *attr1,
const struct dsync_mailbox_attribute *attr2,
int *cmp_r)
{
struct istream *input1, *input2;
int ret;
i_assert(attr1->value_stream != NULL || attr1->value != NULL);
i_assert(attr2->value_stream != NULL || attr2->value != NULL);
if (attr1->value != NULL && attr2->value != NULL) {
*cmp_r = strcmp(attr1->value, attr2->value);
return 0;
}
/* at least one of them is a stream. make both of them streams. */
input1 = attr1->value_stream != NULL ? attr1->value_stream :
i_stream_create_from_data(attr1->value, strlen(attr1->value));
input2 = attr2->value_stream != NULL ? attr2->value_stream :
i_stream_create_from_data(attr2->value, strlen(attr2->value));
i_stream_seek(input1, 0);
i_stream_seek(input2, 0);
ret = dsync_istreams_cmp(input1, input2, cmp_r);
if (attr1->value_stream == NULL)
i_stream_unref(&input1);
if (attr2->value_stream == NULL)
i_stream_unref(&input2);
return ret;
}
static int
dsync_attributes_cmp(const struct dsync_mailbox_attribute *attr,
const struct dsync_mailbox_attribute *local_attr,
int *cmp_r)
{
if (DSYNC_ATTR_HAS_VALUE(attr) &&
!DSYNC_ATTR_HAS_VALUE(local_attr)) {
/* remote has a value and local doesn't -> use it */
*cmp_r = 1;
return 0;
} else if (!DSYNC_ATTR_HAS_VALUE(attr) &&
DSYNC_ATTR_HAS_VALUE(local_attr)) {
/* remote doesn't have a value, bt local does -> skip */
*cmp_r = -1;
return 0;
}
return dsync_attributes_cmp_values(attr, local_attr, cmp_r);
}
static int
dsync_mailbox_import_attribute_real(struct dsync_mailbox_importer *importer,
const struct dsync_mailbox_attribute *attr,
const struct dsync_mailbox_attribute *local_attr,
const char **result_r)
{
struct mail_attribute_value value;
int cmp;
bool ignore = FALSE;
i_assert(DSYNC_ATTR_HAS_VALUE(attr) || attr->deleted);
if (attr->deleted &&
(local_attr == NULL || !DSYNC_ATTR_HAS_VALUE(local_attr))) {
/* attribute doesn't exist on either side -> ignore */
*result_r = "Nonexistent in both sides";
return 0;
}
if (local_attr == NULL) {
/* we haven't seen this locally -> use whatever remote has */
*result_r = "Nonexistent locally";
} else if (local_attr->modseq <= importer->last_common_modseq &&
attr->modseq > importer->last_common_modseq &&
importer->last_common_modseq > 0) {
/* we're doing incremental syncing, and we can see that the
attribute was changed remotely, but not locally -> use it */
*result_r = "Changed remotely";
} else if (local_attr->modseq > importer->last_common_modseq &&
attr->modseq <= importer->last_common_modseq &&
importer->last_common_modseq > 0) {
/* we're doing incremental syncing, and we can see that the
attribute was changed locally, but not remotely -> ignore */
*result_r = "Changed locally";
ignore = TRUE;
} else if (attr->last_change > local_attr->last_change) {
/* remote has a newer timestamp -> use it */
*result_r = "Remote has newer timestamp";
} else if (attr->last_change < local_attr->last_change) {
/* remote has an older timestamp -> ignore */
*result_r = "Local has newer timestamp";
ignore = TRUE;
} else {
/* the timestamps are the same. now we're down to guessing
the right answer, unless the values are actually equal,
so check that first. next try to use modseqs, but if even
they are the same, fallback to just picking one based on the
value. */
if (dsync_attributes_cmp(attr, local_attr, &cmp) < 0) {
importer->mail_error = MAIL_ERROR_TEMP;
importer->failed = TRUE;
return -1;
}
if (cmp == 0) {
/* identical scripts */
*result_r = "Unchanged value";
return 0;
}
if (attr->modseq > local_attr->modseq) {
/* remote has a higher modseq -> use it */
*result_r = "Remote has newer modseq";
} else if (attr->modseq < local_attr->modseq) {
/* remote has an older modseq -> ignore */
*result_r = "Local has newer modseq";
ignore = TRUE;
} else if (cmp < 0) {
ignore = TRUE;
*result_r = "Value changed, but unknown which is newer - picking local";
} else {
*result_r = "Value changed, but unknown which is newer - picking remote";
}
}
if (ignore)
return 0;
memset(&value, 0, sizeof(value));
value.value = attr->value;
value.value_stream = attr->value_stream;
value.last_change = attr->last_change;
if (mailbox_attribute_set(importer->trans, attr->type,
attr->key, &value) < 0) {
i_error("Mailbox %s: Failed to set attribute %s: %s",
mailbox_get_vname(importer->box), attr->key,
mailbox_get_last_error(importer->box, NULL));
/* the attributes aren't vital, don't fail everything just
because of them. */
}
return 0;
}
int dsync_mailbox_import_attribute(struct dsync_mailbox_importer *importer,
const struct dsync_mailbox_attribute *attr)
{
struct dsync_mailbox_attribute *local_attr;
const char *result = "";
int ret;
if (dsync_mailbox_import_lookup_attr(importer, attr->type,
attr->key, &local_attr) < 0)
ret = -1;
else {
ret = dsync_mailbox_import_attribute_real(importer, attr,
local_attr, &result);
if (local_attr != NULL && local_attr->value_stream != NULL)
i_stream_unref(&local_attr->value_stream);
}
imp_debug(importer, "Import attribute %s: %s", attr->key,
ret < 0 ? "failed" : result);
return ret;
}
static void dsync_mail_error(struct dsync_mailbox_importer *importer,
struct mail *mail, const char *field)
{
const char *errstr;
enum mail_error error;
errstr = mailbox_get_last_error(importer->box, &error);
if (error == MAIL_ERROR_EXPUNGED)
return;
i_error("Mailbox %s: Can't lookup %s for UID=%u: %s",
mailbox_get_vname(mail->box), field, mail->uid, errstr);
importer->mail_error = error;
importer->failed = TRUE;
}
static bool
dsync_mail_change_guid_equals(struct dsync_mailbox_importer *importer,
const struct dsync_mail_change *change,
const char *guid, const char **cmp_guid_r)
{
guid_128_t guid_128, change_guid_128;
if (change->type == DSYNC_MAIL_CHANGE_TYPE_EXPUNGE) {
if (guid_128_from_string(change->guid, change_guid_128) < 0)
i_unreached();
} else if (importer->mails_use_guid128) {
mail_generate_guid_128_hash(change->guid, change_guid_128);
} else {
if (cmp_guid_r != NULL)
*cmp_guid_r = change->guid;
return strcmp(change->guid, guid) == 0;
}
mail_generate_guid_128_hash(guid, guid_128);
if (memcmp(change_guid_128, guid_128, GUID_128_SIZE) != 0) {
if (cmp_guid_r != NULL) {
*cmp_guid_r = t_strdup_printf("%s(guid128, orig=%s)",
binary_to_hex(change_guid_128, sizeof(change_guid_128)),
change->guid);
}
return FALSE;
}
return TRUE;
}
static int
importer_try_next_mail(struct dsync_mailbox_importer *importer,
uint32_t wanted_uid)
{
struct mail_private *pmail;
const char *hdr_hash;
if (importer->cur_mail == NULL) {
/* end of search */
return -1;
}
while (importer->cur_mail->seq < importer->next_local_seq ||
importer->cur_mail->uid < wanted_uid) {
if (!importer->cur_uid_has_change &&
!importer->last_common_uid_found) {
/* this message exists locally, but remote didn't send
expunge-change for it. if the message's
uid <= last-common-uid, it should be deleted */
seq_range_array_add(&importer->maybe_expunge_uids,
importer->cur_mail->uid);
}
importer->cur_mail_skip = FALSE;
if (!mailbox_search_next(importer->search_ctx,
&importer->cur_mail)) {
importer->cur_mail = NULL;
importer->cur_guid = NULL;
importer->cur_hdr_hash = NULL;
return -1;
}
importer->cur_uid_has_change = FALSE;
}
importer->cur_uid_has_change = importer->cur_mail->uid == wanted_uid;
if (importer->mails_have_guids) {
if (mail_get_special(importer->cur_mail, MAIL_FETCH_GUID,
&importer->cur_guid) < 0) {
dsync_mail_error(importer, importer->cur_mail, "GUID");
return 0;
}
} else {
if (dsync_mail_get_hdr_hash(importer->cur_mail,
importer->hdr_hash_version,
&hdr_hash) < 0) {
dsync_mail_error(importer, importer->cur_mail,
"header hash");
return 0;
}
pmail = (struct mail_private *)importer->cur_mail;
importer->cur_hdr_hash = p_strdup(pmail->pool, hdr_hash);
importer->cur_guid = "";
}
/* make sure next_local_seq gets updated in case we came here
because of min_uid */
importer->next_local_seq = importer->cur_mail->seq;
return 1;
}
static bool
importer_next_mail(struct dsync_mailbox_importer *importer, uint32_t wanted_uid)
{
int ret;
for (;;) {
T_BEGIN {
ret = importer_try_next_mail(importer, wanted_uid);
} T_END;
if (ret != 0 || importer->failed)
break;
importer->next_local_seq = importer->cur_mail->seq + 1;
}
return ret > 0;
}
static int
importer_mail_cmp(const struct importer_mail *m1,
const struct importer_mail *m2)
{
int ret;
if (m1->guid == NULL)
return 1;
if (m2->guid == NULL)
return -1;
ret = strcmp(m1->guid, m2->guid);
if (ret != 0)
return ret;
if (m1->uid < m2->uid)
return -1;
if (m1->uid > m2->uid)
return 1;
return 0;
}
static void newmail_link(struct dsync_mailbox_importer *importer,
struct importer_new_mail *newmail, uint32_t remote_uid)
{
struct importer_new_mail *first_mail, **last, *mail, *link = NULL;
if (*newmail->guid != '\0') {
first_mail = hash_table_lookup(importer->import_guids,
newmail->guid);
if (first_mail == NULL) {
/* first mail for this GUID */
hash_table_insert(importer->import_guids,
newmail->guid, newmail);
return;
}
} else {
if (remote_uid == 0) {
/* mail exists only locally. we don't want to request
it, and we'll assume it has no duplicate
instances. */
return;
}
first_mail = hash_table_lookup(importer->import_uids,
POINTER_CAST(remote_uid));
if (first_mail == NULL) {
/* first mail for this UID */
hash_table_insert(importer->import_uids,
POINTER_CAST(remote_uid), newmail);
return;
}
}
/* 1) add the newmail to the end of the linked list
2) find our link
FIXME: this loop is slow if the same GUID has a ton of instances.
Could it be improved in some way? */
last = &first_mail->next;
for (mail = first_mail; mail != NULL; mail = mail->next) {
if (mail->final_uid == newmail->final_uid)
mail->uid_is_usable = TRUE;
if (link == NULL && mail->link == NULL &&
mail->uid_in_local != newmail->uid_in_local)
link = mail;
last = &mail->next;
}
*last = newmail;
if (link != NULL && newmail->link == NULL) {
link->link = newmail;
newmail->link = link;
}
}
static void
dsync_mailbox_revert_existing_uid(struct dsync_mailbox_importer *importer,
uint32_t uid, const char *reason)
{
i_assert(importer->revert_local_changes);
/* UID either already exists or UIDNEXT is too high. we can't set the
wanted UID, so we'll need to delete the whole mailbox and resync */
i_warning("Deleting mailbox '%s': UID=%u already exists locally for a different mail: %s",
mailbox_get_vname(importer->box), uid, reason);
importer->delete_mailbox = TRUE;
importer->mail_error = MAIL_ERROR_TEMP;
importer->failed = TRUE;
}
static bool dsync_mailbox_try_save_cur(struct dsync_mailbox_importer *importer,
struct dsync_mail_change *save_change)
{
struct importer_mail m1, m2;
struct importer_new_mail *newmail;
int diff;
bool remote_saved;
memset(&m1, 0, sizeof(m1));
if (importer->cur_mail != NULL) {
m1.guid = importer->mails_have_guids ?
importer->cur_guid : importer->cur_hdr_hash;
m1.uid = importer->cur_mail->uid;
}
memset(&m2, 0, sizeof(m2));
if (save_change != NULL) {
m2.guid = importer->mails_have_guids ?
save_change->guid : save_change->hdr_hash;
m2.uid = save_change->uid;
i_assert(save_change->type != DSYNC_MAIL_CHANGE_TYPE_EXPUNGE);
}
diff = importer_mail_cmp(&m1, &m2);
if (diff < 0) {
/* add a record for local mail */
i_assert(importer->cur_mail != NULL);
if (importer->revert_local_changes) {
if (save_change == NULL) {
dsync_mailbox_revert_existing_uid(importer, importer->cur_mail->uid,
t_strdup_printf("highest than remote's UIDs (remote UIDNEXT=%u)", importer->remote_uid_next));
return TRUE;
}
mail_expunge(importer->cur_mail);
importer->cur_mail_skip = TRUE;
importer->next_local_seq++;
return FALSE;
}
newmail = p_new(importer->pool, struct importer_new_mail, 1);
newmail->guid = p_strdup(importer->pool, importer->cur_guid);
newmail->final_uid = importer->cur_mail->uid;
newmail->local_uid = importer->cur_mail->uid;
newmail->uid_in_local = TRUE;
newmail->uid_is_usable =
newmail->final_uid >= importer->remote_uid_next;
remote_saved = FALSE;
} else if (diff > 0) {
i_assert(save_change != NULL);
newmail = p_new(importer->pool, struct importer_new_mail, 1);
newmail->guid = save_change->guid;
newmail->final_uid = save_change->uid;
newmail->remote_uid = save_change->uid;
newmail->uid_in_local = FALSE;
newmail->uid_is_usable =
newmail->final_uid >= importer->local_uid_next;
if (!newmail->uid_is_usable && importer->revert_local_changes) {
dsync_mailbox_revert_existing_uid(importer, newmail->final_uid,
t_strdup_printf("UID >= local UIDNEXT=%u", importer->local_uid_next));
return TRUE;
}
remote_saved = TRUE;
} else {
/* identical */
i_assert(importer->cur_mail != NULL);
i_assert(save_change != NULL);
newmail = p_new(importer->pool, struct importer_new_mail, 1);
newmail->guid = save_change->guid;
newmail->final_uid = importer->cur_mail->uid;
newmail->local_uid = importer->cur_mail->uid;
newmail->remote_uid = save_change->uid;
newmail->uid_in_local = TRUE;
newmail->uid_is_usable = TRUE;
newmail->link = newmail;
remote_saved = TRUE;
}
if (newmail->uid_in_local) {
importer->cur_mail_skip = TRUE;
importer->next_local_seq++;
}
/* NOTE: assumes save_change is allocated from importer pool */
newmail->change = save_change;
array_append(&importer->newmails, &newmail, 1);
newmail_link(importer, newmail,
save_change == NULL ? 0 : save_change->uid);
return remote_saved;
}
static bool ATTR_NULL(2)
dsync_mailbox_try_save(struct dsync_mailbox_importer *importer,
struct dsync_mail_change *save_change)
{
if (importer->cur_mail_skip) {
if (!importer_next_mail(importer, 0) && save_change == NULL)
return FALSE;
}
return dsync_mailbox_try_save_cur(importer, save_change);
}
static void dsync_mailbox_save(struct dsync_mailbox_importer *importer,
struct dsync_mail_change *save_change)
{
while (!dsync_mailbox_try_save(importer, save_change)) ;
}
static bool
dsync_import_set_mail(struct dsync_mailbox_importer *importer,
const struct dsync_mail_change *change)
{
const char *guid, *cmp_guid;
if (!mail_set_uid(importer->mail, change->uid))
return FALSE;
if (change->guid == NULL) {
/* GUID is unknown */
return TRUE;
}
if (*change->guid == '\0') {
/* backend doesn't support GUIDs. if hdr_hash is set, we could
verify it, but since this message really is supposed to
match, it's probably too much trouble. */
return TRUE;
}
/* verify that GUID matches, just in case */
if (mail_get_special(importer->mail, MAIL_FETCH_GUID, &guid) < 0) {
dsync_mail_error(importer, importer->mail, "GUID");
return FALSE;
}
if (!dsync_mail_change_guid_equals(importer, change, guid, &cmp_guid)) {
dsync_import_unexpected_state(importer, t_strdup_printf(
"Unexpected GUID mismatch for UID=%u: %s != %s",
change->uid, guid, cmp_guid));
return FALSE;
}
return TRUE;
}
static bool dsync_check_cur_guid(struct dsync_mailbox_importer *importer,
const struct dsync_mail_change *change)
{
const char *cmp_guid;
if (change->guid == NULL || change->guid[0] == '\0' ||
importer->cur_guid[0] == '\0')
return TRUE;
if (!dsync_mail_change_guid_equals(importer, change,
importer->cur_guid, &cmp_guid)) {
dsync_import_unexpected_state(importer, t_strdup_printf(
"Unexpected GUID mismatch (2) for UID=%u: %s != %s",
change->uid, importer->cur_guid, cmp_guid));
return FALSE;
}
return TRUE;
}
static void
merge_flags(uint32_t local_final, uint32_t local_add, uint32_t local_remove,
uint32_t remote_final, uint32_t remote_add, uint32_t remote_remove,
uint32_t pvt_mask, bool prefer_remote, bool prefer_pvt_remote,
uint32_t *change_add_r, uint32_t *change_remove_r,
bool *remote_changed, bool *remote_pvt_changed)
{
uint32_t combined_add, combined_remove, conflict_flags;
uint32_t local_wanted, remote_wanted, conflict_pvt_flags;
/* resolve conflicts */
conflict_flags = local_add & remote_remove;
if (conflict_flags != 0) {
conflict_pvt_flags = conflict_flags & pvt_mask;
conflict_flags &= ~pvt_mask;
if (prefer_remote)
local_add &= ~conflict_flags;
else
remote_remove &= ~conflict_flags;
if (prefer_pvt_remote)
local_add &= ~conflict_pvt_flags;
else
remote_remove &= ~conflict_pvt_flags;
}
conflict_flags = local_remove & remote_add;
if (conflict_flags != 0) {
conflict_pvt_flags = conflict_flags & pvt_mask;
conflict_flags &= ~pvt_mask;
if (prefer_remote)
local_remove &= ~conflict_flags;
else
remote_add &= ~conflict_flags;
if (prefer_pvt_remote)
local_remove &= ~conflict_pvt_flags;
else
remote_add &= ~conflict_pvt_flags;
}
combined_add = local_add|remote_add;
combined_remove = local_remove|remote_remove;
i_assert((combined_add & combined_remove) == 0);
/* don't change flags that are currently identical in both sides */
conflict_flags = local_final ^ remote_final;
combined_add &= conflict_flags;
combined_remove &= conflict_flags;
/* see if there are conflicting final flags */
local_wanted = (local_final|combined_add) & ~combined_remove;
remote_wanted = (remote_final|combined_add) & ~combined_remove;
conflict_flags = local_wanted ^ remote_wanted;
if (conflict_flags != 0) {
if (prefer_remote && prefer_pvt_remote)
local_wanted = remote_wanted;
else if (prefer_remote && !prefer_pvt_remote) {
local_wanted = (local_wanted & pvt_mask) |
(remote_wanted & ~pvt_mask);
} else if (!prefer_remote && prefer_pvt_remote) {
local_wanted = (local_wanted & ~pvt_mask) |
(remote_wanted & pvt_mask);
}
}
*change_add_r = local_wanted & ~local_final;
*change_remove_r = local_final & ~local_wanted;
if ((local_wanted & ~pvt_mask) != (remote_final & ~pvt_mask))
*remote_changed = TRUE;
if ((local_wanted & pvt_mask) != (remote_final & pvt_mask))
*remote_pvt_changed = TRUE;
}
static bool
keyword_find(ARRAY_TYPE(const_string) *keywords, const char *name,
unsigned int *idx_r)
{
const char *const *names;
unsigned int i, count;
names = array_get(keywords, &count);
for (i = 0; i < count; i++) {
if (strcmp(names[i], name) == 0) {
*idx_r = i;
return TRUE;