mail-index.c revision a050ca9def13949dbaa67bd6574a41c4f397ae26
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Copyright (C) 2003-2004 Timo Sirainen */
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenstatic int mail_index_try_open_only(struct mail_index *index);
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainenstatic void mail_index_create_in_memory(struct mail_index *index,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstruct mail_index *mail_index_alloc(const char *dir, const char *prefix)
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen index->extension_pool = pool_alloconly_create("extension", 512);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen ARRAY_CREATE(&index->extensions, index->extension_pool,
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen ARRAY_CREATE(&index->sync_lost_handlers, default_pool,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen mail_index_ext_register(index, "keywords", 128, 2, 1);
41e1c7380edda701719d8ce1fb4d465d2ec4c84dTimo Sirainen index->keywords_pool = pool_alloconly_create("keywords", 512);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen ARRAY_CREATE(&index->keywords, default_pool, const char *, 16);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen hash_create(default_pool, index->keywords_pool, 0,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen strcase_hash, (hash_cmp_callback_t *)strcasecmp);
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainenvoid mail_index_set_permissions(struct mail_index *index,
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainenuint32_t mail_index_ext_register(struct mail_index *index, const char *name,
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen const struct mail_index_registered_ext *extensions;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen unsigned int i, ext_count;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen extensions = array_get(&index->extensions, &ext_count);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen /* see if it's already there */
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen for (i = 0; i < ext_count; i++) {
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen rext.name = p_strdup(index->extension_pool, name);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainenvoid mail_index_register_expunge_handler(struct mail_index *index,
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen rext = array_idx_modifyable(&index->extensions, ext_id);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainenvoid mail_index_unregister_expunge_handler(struct mail_index *index,
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen rext = array_idx_modifyable(&index->extensions, ext_id);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainenvoid mail_index_register_sync_handler(struct mail_index *index, uint32_t ext_id,
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen rext = array_idx_modifyable(&index->extensions, ext_id);
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen i_assert(rext->sync_handler.callback == NULL);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainenvoid mail_index_unregister_sync_handler(struct mail_index *index,
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen rext = array_idx_modifyable(&index->extensions, ext_id);
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen i_assert(rext->sync_handler.callback != NULL);
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainenvoid mail_index_register_sync_lost_handler(struct mail_index *index,
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen array_append(&index->sync_lost_handlers, &cb, 1);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainenvoid mail_index_unregister_sync_lost_handler(struct mail_index *index,
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen mail_index_sync_lost_handler_t *const *handlers;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen unsigned int i, count;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen handlers = array_get(&index->sync_lost_handlers, &count);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen for (i = 0; i < count; i++) {
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen array_delete(&index->sync_lost_handlers, i, 1);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainenstatic void mail_index_map_init_extbufs(struct mail_index_map *map,
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen size = (sizeof(array_t) + BUFFER_APPROX_SIZE) * 2 +
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen sizeof(struct mail_index_ext) +
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen ARRAY_CREATE(&map->extensions, map->extension_pool,
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen ARRAY_CREATE(&map->ext_id_map, map->extension_pool,
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainenuint32_t mail_index_map_lookup_ext(struct mail_index_map *map, const char *name)
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen unsigned int i, size;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen extensions = array_get(&map->extensions, &size);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen for (i = 0; i < size; i++) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainenmail_index_map_register_ext(struct mail_index *index,
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen i_assert(mail_index_map_lookup_ext(map, name) == (uint32_t)-1);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen ext->name = p_strdup(map->extension_pool, name);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen ext->index_idx = mail_index_ext_register(index, name, hdr_size,
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen /* Update index ext_id -> map ext_id mapping. Fill non-used
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen ext_ids with (uint32_t)-1 */
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen while (array_count(&map->ext_id_map) < ext->index_idx)
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen array_append(&map->ext_id_map, &empty_idx, 1);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen array_idx_set(&map->ext_id_map, ext->index_idx, &idx);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainenstatic int size_check(size_t *size_left, size_t size)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen size_t size = sizeof(struct mail_index_ext_header) + name_len;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return MAIL_INDEX_HEADER_SIZE_ALIGN(size) - size;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainenstatic int mail_index_read_extensions(struct mail_index *index,
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen unsigned int i, old_count;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen /* extension headers always start from 64bit offsets, so if base header
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen doesn't happen to be 64bit aligned we'll skip some bytes */
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen offset = MAIL_INDEX_HEADER_SIZE_ALIGN(map->hdr.base_header_size);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen if (offset >= map->hdr.header_size && map->extension_pool == NULL) {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* nothing to do, skip allocatations and all */
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen mail_index_map_init_extbufs(map, old_count + 5);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen for (i = 0; i < old_count; i++)
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen ext_hdr = CONST_PTR_OFFSET(map->hdr_base, offset);
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen /* Extension header contains:
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen - struct mail_index_ext_header
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen - name (not 0-terminated)
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen - 64bit alignment padding
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen - extension header contents
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen - 64bit alignment padding
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (!size_check(&size_left, sizeof(*ext_hdr)) ||
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen !size_check(&size_left, ext_hdr->name_size) ||
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen !size_check(&size_left, get_align(ext_hdr->name_size)) ||
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen "Header extension goes outside header",
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen offset += ext_hdr->name_size + get_align(ext_hdr->name_size);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen name = t_strndup(CONST_PTR_OFFSET(map->hdr_base, name_offset),
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen if (mail_index_map_lookup_ext(map, name) != (uint32_t)-1) {
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen "Duplicate header extension %s",
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen if ((ext_hdr->record_offset % ext_hdr->record_align) != 0 ||
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen (map->hdr.record_size % ext_hdr->record_align) != 0) {
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen "Record field %s alignmentation %u not used",
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen index->filepath, name, ext_hdr->record_align);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen offset += MAIL_INDEX_HEADER_SIZE_ALIGN(ext_hdr->hdr_size);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenint mail_index_keyword_lookup(struct mail_index *index,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen unsigned int *idx_r)
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen /* keywords_hash keeps a name => index mapping of keywords.
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen Keywords are never removed from it, so the index values are valid
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen for the lifetime of the mail_index. */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (hash_lookup_full(index->keywords_hash, keyword, NULL, &value)) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen *idx_r = POINTER_CAST_TO(value, unsigned int);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen keyword = keyword_dup = p_strdup(index->keywords_pool, keyword);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen hash_insert(index->keywords_hash, keyword_dup, POINTER_CAST(*idx_r));
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenint mail_index_map_read_keywords(struct mail_index *index,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen const struct mail_index_keyword_header *kw_hdr;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen const struct mail_index_keyword_header_rec *kw_rec;
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen unsigned int i, name_area_end_offset, old_count;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen ext_id = mail_index_map_lookup_ext(map, "keywords");
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen /* Extension header contains:
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen - struct mail_index_keyword_header
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen - struct mail_index_keyword_header_rec * keywords_count
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen - const char names[] * keywords_count
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen kw_hdr = CONST_PTR_OFFSET(map->hdr_base, ext->hdr_offset);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen name = (const char *)(kw_rec + kw_hdr->keywords_count);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen old_count = !array_is_created(&map->keyword_idx_map) ? 0 :
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen /* Keywords can only be added into same mapping. Removing requires a
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen new mapping (recreating the index file) */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* nothing changed */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* make sure the header is valid */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen "Keywords removed unexpectedly",
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if ((size_t)(name - (const char *)kw_hdr) > ext->hdr_size) {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen "keywords_count larger than header size",
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen name_area_end_offset = (const char *)kw_hdr + ext->hdr_size - name;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen for (i = 0; i < kw_hdr->keywords_count; i++) {
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen if (kw_rec[i].name_offset > name_area_end_offset) {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen "name_offset points outside allocated header",
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
871c7b8969e8627dc4c8b3e56fd126f948e6bce6Timo Sirainen "Keyword header doesn't end with NUL",
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* create file -> index mapping */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (!array_is_created(&map->keyword_idx_map)) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen ARRAY_CREATE(&map->keyword_idx_map, default_pool,
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen /* Check that existing headers are still the same. It's behind DEBUG
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen since it's pretty useless waste of CPU normally. */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen for (i = 0; i < array_count(&map->keyword_idx_map); i++) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen const char *keyword = name + kw_rec[i].name_offset;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen const unsigned int *old_idx;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen unsigned int idx;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen old_idx = array_idx(&map->keyword_idx_map, i);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (!mail_index_keyword_lookup(index, keyword, FALSE, &idx) ||
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen "Keywords changed unexpectedly",
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen /* Register the newly seen keywords */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen const char *keyword = name + kw_rec[i].name_offset;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen unsigned int idx;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen (void)mail_index_keyword_lookup(index, keyword, TRUE, &idx);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenconst array_t *mail_index_get_keywords(struct mail_index *index)
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen /* Make sure all the keywords are in index->keywords. It's quick to do
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen if nothing has changed. */
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen (void)mail_index_map_read_keywords(index, index->map);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic int mail_index_check_header(struct mail_index *index,
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen const struct mail_index_header *hdr = &map->hdr;
811f2e26d9782d9cb99fdf82e18ffa0a77564fe2Timo Sirainen enum mail_index_header_compat_flags compat_flags = 0;
811f2e26d9782d9cb99fdf82e18ffa0a77564fe2Timo Sirainen compat_flags |= MAIL_INDEX_COMPAT_LITTLE_ENDIAN;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (hdr->major_version != MAIL_INDEX_MAJOR_VERSION) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* major version change - handle silently(?) */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* architecture change - handle silently(?) */
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen if ((map->hdr.flags & MAIL_INDEX_HDR_FLAG_CORRUPTED) != 0) {
8830fab191cab8440281eb641dfdd93974b2933bTimo Sirainen /* we've already complained about it */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* following some extra checks that only take a bit of CPU */
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen if (hdr->uid_validity == 0 && hdr->next_uid != 1) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen "uid_validity = 0, next_uid = %u",
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen if (hdr->record_size < sizeof(struct mail_index_record)) {
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen sizeof(struct mail_index_record));
e12648867876aaec17e06ee4caef0bb60363449dTimo Sirainen if ((hdr->flags & MAIL_INDEX_HDR_FLAG_FSCK) != 0)
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen if (hdr->recent_messages_count > hdr->messages_count ||
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen hdr->seen_messages_count > hdr->messages_count ||
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen hdr->deleted_messages_count > hdr->messages_count)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (hdr->first_recent_uid_lowwater > hdr->next_uid ||
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen hdr->first_unseen_uid_lowwater > hdr->next_uid ||
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen hdr->first_deleted_uid_lowwater > hdr->next_uid)
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen return mail_index_read_extensions(index, map);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainenstatic void mail_index_map_clear(struct mail_index *index,
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (munmap(map->mmap_base, map->mmap_size) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_set_syscall_error(index, "munmap()");
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainenvoid mail_index_unmap(struct mail_index *index, struct mail_index_map *map)
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainenstatic void mail_index_map_copy_hdr(struct mail_index_map *map,
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen if (hdr->base_header_size < sizeof(map->hdr)) {
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen /* header smaller than ours, make a copy so our newer headers
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen won't have garbage in them */
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen memcpy(&map->hdr, hdr, hdr->base_header_size);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainenstatic int mail_index_mmap(struct mail_index *index, struct mail_index_map *map)
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen /* we had temporarily used a buffer, eg. for updating index */
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen mail_index_set_syscall_error(index, "mmap()");
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen offsetof(struct mail_index_header, major_version) &&
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen hdr->major_version != MAIL_INDEX_MAJOR_VERSION) {
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen /* major version change - handle silently */
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (map->mmap_size < MAIL_INDEX_HEADER_MIN_SIZE) {
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen records_count = (map->mmap_size - hdr->header_size) /
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen "messages_count too large (%u > %u)",
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen map->records = PTR_OFFSET(map->mmap_base, map->hdr.header_size);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainenstatic int mail_index_read_header(struct mail_index *index,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen for (pos = 0; ret > 0 && pos < sizeof(*hdr); ) {
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainenstatic int mail_index_read_map(struct mail_index *index,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen ret = mail_index_read_header(index, &hdr, &pos);
05751847e5e493fecfd0634d35d21722aad44a0bTimo Sirainen if (pos > (ssize_t)offsetof(struct mail_index_header, major_version) &&
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen hdr.major_version != MAIL_INDEX_MAJOR_VERSION) {
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen /* major version change - handle silently */
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (ret >= 0 && pos >= MAIL_INDEX_HEADER_MIN_SIZE &&
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (hdr.base_header_size < MAIL_INDEX_HEADER_MIN_SIZE ||
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen mail_index_set_error(index, "Corrupted index file %s: "
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen "Corrupted header sizes (base %u, full %u)",
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen buffer_append(map->hdr_copy_buf, &hdr, hdr.base_header_size);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen /* @UNSAFE */
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen data = buffer_append_space_unsafe(map->hdr_copy_buf,
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen records_size = hdr.messages_count * hdr.record_size;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen map->buffer = buffer_create_dynamic(default_pool,
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen /* @UNSAFE */
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen data = buffer_append_space_unsafe(map->buffer, records_size);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen ret = pread_full(index->fd, data, records_size,
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen mail_index_set_syscall_error(index, "pread_full()");
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen "Corrupted index file %s: File too small",
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen index->sync_log_file_offset = hdr.log_file_int_offset;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainenstatic int mail_index_sync_from_transactions(struct mail_index *index,
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen const struct mail_index_header *map_hdr = &(*map)->hdr;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* read the real log position where we are supposed to be
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen ret = mail_index_read_header(index, &hdr, &pos);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen mail_index_set_syscall_error(index, "pread()");
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen if (map_hdr->log_file_seq == hdr.log_file_seq &&
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen map_hdr->log_file_int_offset == hdr.log_file_int_offset) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* nothing to do */
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen if (map_hdr->log_file_seq > hdr.log_file_seq ||
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen map_hdr->log_file_int_offset > hdr.log_file_int_offset)) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* we went too far, have to re-read the file */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen hdr.log_file_ext_offset != hdr.log_file_int_offset) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* too much trouble to get this right. */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* sync everything there is */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen log_view = mail_transaction_log_view_open(index->log);
a050ca9def13949dbaa67bd6574a41c4f397ae26Timo Sirainen /* can't use it. sync by re-reading index. */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen while ((ret = mail_transaction_log_view_next(log_view, &thdr, &tdata,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (mail_index_sync_record(&sync_map_ctx, thdr, tdata) < 0) {
0b38c17fb10e2ad1d081369d34b93d0448d971a2Timo Sirainen mail_transaction_log_view_get_prev_pos(log_view, &prev_seq,
f2786c07cbd4a7a0a6a46c3e06dc4545aaf2f278Timo Sirainen (prev_seq != max_seq || prev_offset <= max_offset));
0b38c17fb10e2ad1d081369d34b93d0448d971a2Timo Sirainen index->map->hdr.log_file_ext_offset = prev_offset;
a80241dc44ee43bc809ed5ec6dedb74711966dafTimo Sirainen /* make sure we did everything right. note that although the
a80241dc44ee43bc809ed5ec6dedb74711966dafTimo Sirainen message counts should be equal, the flag counters may not */
5e2cf2e665bb0c3c68fa34bc6d0e9ce93426fdd0Timo Sirainen i_assert(hdr.messages_count == (*map)->hdr.messages_count);
a80241dc44ee43bc809ed5ec6dedb74711966dafTimo Sirainen i_assert(hdr.log_file_seq == (*map)->hdr.log_file_seq);
a80241dc44ee43bc809ed5ec6dedb74711966dafTimo Sirainen i_assert(hdr.log_file_int_offset == (*map)->hdr.log_file_int_offset);
a80241dc44ee43bc809ed5ec6dedb74711966dafTimo Sirainen i_assert(hdr.log_file_ext_offset == (*map)->hdr.log_file_ext_offset);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainenstatic int mail_index_read_map_with_retry(struct mail_index *index,
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen mail_index_sync_lost_handler_t *const *handlers;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen unsigned int i, count;
673ab5815277e6f9ac09227c866c90ab57438912Timo Sirainen /* we're most likely syncing the index and we really don't
673ab5815277e6f9ac09227c866c90ab57438912Timo Sirainen want to read more than what was synced last time. */
2ef8fac74c9d0bfc330bf53bb06df7e9d5f08e7dTimo Sirainen if ((*map)->hdr.indexid != 0 && index->log != NULL) {
2ef8fac74c9d0bfc330bf53bb06df7e9d5f08e7dTimo Sirainen /* we're not creating the index, or opening transaction log.
2ef8fac74c9d0bfc330bf53bb06df7e9d5f08e7dTimo Sirainen sync this as a view from transaction log. */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen ret = mail_index_sync_from_transactions(index, map,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* transaction log lost/broken, fallback to re-reading it */
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen /* notify all "sync lost" handlers */
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen handlers = array_get(&index->sync_lost_handlers, &count);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen for (i = 0; i < count; i++)
e4fb5bfcdff32d337d054cce36e00e1cdfaae9f8Timo Sirainen for (i = 0; i < MAIL_INDEX_ESTALE_RETRY_COUNT; i++) {
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen ret = mail_index_read_map(index, *map, &retry);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen /* ESTALE - reopen index file */
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen mail_index_set_syscall_error(index, "close()");
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen /* the file was lost */
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen mail_index_set_syscall_error(index, "open()");
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen /* Too many ESTALE retries */
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen mail_index_set_syscall_error(index, "read_map()");
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainenstatic int mail_index_map_try_existing(struct mail_index_map *map)
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen /* always check corrupted-flag to avoid errors later */
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen if ((hdr->flags & MAIL_INDEX_HDR_FLAG_CORRUPTED) != 0)
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen used_size = hdr->header_size + hdr->messages_count * hdr->record_size;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen if (map->mmap_size >= used_size && map->hdr_base == hdr) {
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainenint mail_index_map(struct mail_index *index, int force)
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen i_assert(index->map == NULL || index->map->refcount > 0);
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen ret = mail_index_map_try_existing(index->map);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen /* we're syncing, don't break the mapping */
c48140ca508ebc9642737e7fd6c8d9e52f95df32Timo Sirainen if (index->map != NULL && index->map->refcount > 1) {
c48140ca508ebc9642737e7fd6c8d9e52f95df32Timo Sirainen /* this map is already used by some views and they may have
c48140ca508ebc9642737e7fd6c8d9e52f95df32Timo Sirainen pointers into it. leave them and create a new mapping. */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* create a copy of the mapping instead so we don't
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen have to re-read it */
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen buffer_create_dynamic(default_pool, sizeof(map->hdr));
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen } else if (MAIL_INDEX_MAP_IS_IN_MEMORY(map)) {
19593623a2278039be23fc93e3404c012920b664Timo Sirainen if (munmap(map->mmap_base, map->mmap_size) < 0)
19593623a2278039be23fc93e3404c012920b664Timo Sirainen mail_index_set_syscall_error(index, "munmap()");
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen ret = mail_index_read_map_with_retry(index, &map, force);
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen else if (ret == 0) {
20c26f4fcf9ef87434761829cc209c2f84ff5716Timo Sirainen i_assert(map->hdr.messages_count == map->records_count);
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainenint mail_index_get_latest_header(struct mail_index *index,
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen ret = mail_index_read_header(index, hdr_r, &pos);
d7095f3a4466fbb78b2d5eb3d322bc15a5b0ab1fTimo Sirainenmail_index_map_clone(struct mail_index_map *map, uint32_t new_record_size)
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen unsigned int i, count;
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainen mem_map->buffer = buffer_create_dynamic(default_pool, size);
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen buffer_append(mem_map->buffer, map->records, size);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen copy_size = I_MIN(map->hdr.record_size, new_record_size);
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen dest = buffer_append_space_unsafe(mem_map->buffer,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mem_map->records = buffer_get_modifyable_data(mem_map->buffer, NULL);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen buffer_create_dynamic(default_pool, map->hdr.header_size);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen buffer_append_zero(mem_map->hdr_copy_buf, sizeof(*hdr));
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen map->hdr.header_size - map->hdr.base_header_size);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen hdr = buffer_get_modifyable_data(mem_map->hdr_copy_buf, NULL);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen /* copy extensions */
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen mail_index_map_init_extbufs(mem_map, count + 2);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen array_append_array(&mem_map->extensions, &map->extensions);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen array_append_array(&mem_map->ext_id_map, &map->ext_id_map);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* fix the name pointers to use our own pool */
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen extensions = array_get_modifyable(&mem_map->extensions, &count);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen for (i = 0; i < count; i++) {
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen extensions[i].name = p_strdup(mem_map->extension_pool,
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainenint mail_index_map_get_ext_idx(struct mail_index_map *map,
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenstatic int mail_index_try_open_only(struct mail_index *index)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen for (i = 0; i < 3; i++) {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen /* May happen with some OSes with NFS. Try again, although
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen there's still a race condition with another computer
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen creating the index file again. However, we can't try forever
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen as ESTALE happens also if index directory has been deleted
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen from server.. */
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen return mail_index_set_syscall_error(index, "open()");
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen /* have to create it */
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenmail_index_try_open(struct mail_index *index, unsigned int *lock_id_r)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen unsigned int lock_id;
c9f1a617593eb569fb02f45041bad3c13e534496Timo Sirainen if (mail_index_lock_shared(index, FALSE, &lock_id) < 0) {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen /* it's corrupted - recreate it */
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainenint mail_index_write_base_header(struct mail_index *index,
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen hdr_size = I_MIN(sizeof(*hdr), hdr->base_header_size);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if (!MAIL_INDEX_MAP_IS_IN_MEMORY(index->map)) {
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen if (msync(index->map->mmap_base, hdr_size, MS_SYNC) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return mail_index_set_syscall_error(index, "msync()");
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (pwrite_full(index->fd, hdr, hdr_size, 0) < 0) {
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen "pwrite_full()");
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen buffer_write(index->map->hdr_copy_buf, 0, hdr, hdr_size);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint mail_index_create_tmp_file(struct mail_index *index, const char **path_r)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen path = *path_r = t_strconcat(index->filepath, ".tmp", NULL);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen fd = open(path, O_RDWR|O_CREAT|O_TRUNC, index->mode);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return mail_index_file_set_syscall_error(index, path, "open()");
d22390f33eedbd2413debabc0662dde5241b1aa6Timo Sirainen if (index->gid != (gid_t)-1 && fchown(fd, (uid_t)-1, index->gid) < 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_file_set_syscall_error(index, path, "fchown()");
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainenstatic int mail_index_create(struct mail_index *index,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* log file lock protects index creation */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (mail_transaction_log_sync_lock(index->log, &seq, &offset) < 0)
9fc077d3886b1584019bcc9b92c717cfffce5c67Timo Sirainen /* mark the existing log file as synced */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* create it fully in index.tmp first */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->fd = mail_index_create_tmp_file(index, &path);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen else if (write_full(index->fd, hdr, sizeof(*hdr)) < 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_file_set_syscall_error(index, path, "write_full()");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* it's corrupted even while we just created it,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen should never happen unless someone pokes the file directly */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "Newly created index file is corrupted: %s", path);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_file_set_syscall_error(index, path,
86bc22621dcce6d9f1ca7216c9cc958381a61277Timo Sirainen /* make it visible to others */
86bc22621dcce6d9f1ca7216c9cc958381a61277Timo Sirainen mail_index_set_error(index, "rename(%s, %s) failed: %m",
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainenstatic void mail_index_header_init(struct mail_index_header *hdr)
264bc938e956288ec7a2a43417c01c3a3ce840fdTimo Sirainen i_assert((sizeof(*hdr) % sizeof(uint64_t)) == 0);
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen hdr->major_version = MAIL_INDEX_MAJOR_VERSION;
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen hdr->minor_version = MAIL_INDEX_MINOR_VERSION;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen hdr->record_size = sizeof(struct mail_index_record);
811f2e26d9782d9cb99fdf82e18ffa0a77564fe2Timo Sirainen hdr->compat_flags |= MAIL_INDEX_COMPAT_LITTLE_ENDIAN;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainenstatic void mail_index_create_in_memory(struct mail_index *index,
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen /* a bit kludgy way to do this, but it initializes everything
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen nicely and correctly */
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen index->map = mail_index_map_clone(&tmp_map, hdr->record_size);
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen/* returns -1 = error, 0 = won't create, 1 = ok */
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainenstatic int mail_index_open_files(struct mail_index *index,
40ef82c46f6652412b068ebcdac7c3e74840a284Timo Sirainen unsigned int lock_id = 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen else if (ret == 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* doesn't exist, or corrupted */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if ((flags & MAIL_INDEX_OPEN_FLAG_CREATE) == 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen } else if (ret < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->log = mail_transaction_log_open_or_create(index);
67b9db4e0bd93ffe24cc95f609193f6f642f6448Timo Sirainen /* looks like someone else created the transaction log
67b9db4e0bd93ffe24cc95f609193f6f642f6448Timo Sirainen before we had the chance. use its indexid so we
67b9db4e0bd93ffe24cc95f609193f6f642f6448Timo Sirainen don't try to create conflicting ones. */
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen if (mail_index_lock_shared(index, FALSE, &lock_id) < 0)
f239eb76f77afcbc0bfc97c9b52b4407d1bc3fe6Timo Sirainen index->cache = created ? mail_cache_create(index) :
a53cb86b4d733d9c48ee4d285bed477c80825804Timo Sirainenint mail_index_open(struct mail_index *index, enum mail_index_open_flags flags,
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainen (index->hdr->flags & MAIL_INDEX_HDR_FLAG_CORRUPTED) != 0) {
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainen /* corrupted, reopen files */
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen index->filepath = MAIL_INDEX_IS_IN_MEMORY(index) ?
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen i_strconcat(index->dir, "/", index->prefix, NULL);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen (flags & MAIL_INDEX_OPEN_FLAG_MMAP_DISABLE) != 0;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen (flags & MAIL_INDEX_OPEN_FLAG_MMAP_NO_WRITE) != 0;
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen /* don't even bother to handle dotlocking without mmap being
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen disabled. that combination simply doesn't make any sense */
6fcaeede31ef2292b3ff59c461eb6ef4ae989dfdTimo Sirainen i_fatal("lock_method=dotlock and mmap_disable=no "
6fcaeede31ef2292b3ff59c461eb6ef4ae989dfdTimo Sirainen "combination isn't supported. "
6fcaeede31ef2292b3ff59c461eb6ef4ae989dfdTimo Sirainen "You don't _really_ want it anyway.");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* completely broken, reopen */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* too many tries */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid mail_index_close(struct mail_index *index)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_set_syscall_error(index, "close()");
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenint mail_index_reopen(struct mail_index *index, int fd)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen unsigned int old_shared_locks, old_lock_id, lock_id = 0;
325d4ad220bd13f6d176391d962a0e33c856a7f6Timo Sirainen /* new file, new locks. the old fd can keep its locks, they don't
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen matter anymore as no-one's going to modify the file. */
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen ret = mail_index_lock_shared(index, FALSE, &lock_id);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen else if (ret == 0) {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen /* index file is lost */
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen /* read the new mapping. note that with mmap_disable we want
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen to keep the old mapping in index->map so we can update it
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen by reading transaction log. */
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen mail_index_set_syscall_error(index, "close()");
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen mail_index_set_syscall_error(index, "close()");
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainenint mail_index_reopen_if_needed(struct mail_index *index)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen return mail_index_set_syscall_error(index, "fstat()");
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen mail_index_set_syscall_error(index, "stat()");
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen /* lost it? recreate */
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainenint mail_index_refresh(struct mail_index *index)
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen unsigned int lock_id;
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen /* we have index exclusively locked, nothing could
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen have changed. */
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen /* reopening is all we need */
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen /* mail_index_map() simply reads latest changes from transaction log,
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen which makes us fully refreshed. */
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen if (mail_index_lock_shared(index, TRUE, &lock_id) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstruct mail_cache *mail_index_get_cache(struct mail_index *index)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint mail_index_set_error(struct mail_index *index, const char *fmt, ...)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainenvoid mail_index_set_inconsistent(struct mail_index *index)
87712707722ef7d73acb065546e61afa4455cd9eTimo Sirainenvoid mail_index_mark_corrupted(struct mail_index *index)
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen if (mail_index_write_base_header(index, &hdr) == 0) {
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (!MAIL_INDEX_IS_IN_MEMORY(index) && fsync(index->fd) < 0)
87712707722ef7d73acb065546e61afa4455cd9eTimo Sirainen mail_index_set_syscall_error(index, "fsync()");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint mail_index_set_syscall_error(struct mail_index *index,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return mail_index_set_error(index, "%s failed with index file %s: %m",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint mail_index_file_set_syscall_error(struct mail_index *index,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return mail_index_set_error(index, "%s failed with file %s: %m",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenenum mail_index_error mail_index_get_last_error(struct mail_index *index)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenconst char *mail_index_get_error_message(struct mail_index *index)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid mail_index_reset_error(struct mail_index *index)
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainenuint32_t mail_index_uint32_to_offset(uint32_t offset)
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen buf[0] = 0x80 | ((offset & 0x0fe00000) >> 21);
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen buf[1] = 0x80 | ((offset & 0x001fc000) >> 14);
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainenuint32_t mail_index_offset_to_uint32(uint32_t offset)
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen const unsigned char *buf = (const unsigned char *) &offset;