-
Notifications
You must be signed in to change notification settings - Fork 271
/
index-search.c
1886 lines (1674 loc) · 48.5 KB
/
index-search.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) 2002-2017 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "ioloop.h"
#include "array.h"
#include "istream.h"
#include "utc-offset.h"
#include "str.h"
#include "time-util.h"
#include "unichar.h"
#include "imap-match.h"
#include "message-address.h"
#include "message-date.h"
#include "message-search.h"
#include "message-parser.h"
#include "mail-index-modseq.h"
#include "index-storage.h"
#include "index-mail.h"
#include "index-sort.h"
#include "mail-search.h"
#include "mailbox-search-result-private.h"
#include "index-search-private.h"
#include <ctype.h>
#define SEARCH_NOTIFY_INTERVAL_SECS 10
#define SEARCH_COST_DENTRY 3ULL
#define SEARCH_COST_ATTR 1ULL
#define SEARCH_COST_FILES_READ 25ULL
#define SEARCH_COST_KBYTE 15ULL
#define SEARCH_COST_CACHE 1ULL
#define SEARCH_MIN_NONBLOCK_USECS 200000
#define SEARCH_MAX_NONBLOCK_USECS 250000
#define SEARCH_INITIAL_MAX_COST 30000
#define SEARCH_RECALC_MIN_USECS 50000
struct search_header_context {
struct index_search_context *index_ctx;
struct index_mail *imail;
struct mail_search_arg *args;
struct message_block decoded_block;
bool decoded_block_set;
struct message_header_line *hdr;
unsigned int parse_headers:1;
unsigned int custom_header:1;
unsigned int threading:1;
};
struct search_body_context {
struct index_search_context *index_ctx;
struct istream *input;
struct message_part *part;
};
static void search_parse_msgset_args(unsigned int messages_count,
struct mail_search_arg *args,
uint32_t *seq1_r, uint32_t *seq2_r);
static void ATTR_NULL(2)
search_none(struct mail_search_arg *arg ATTR_UNUSED, void *ctx ATTR_UNUSED)
{
}
static void search_set_failed(struct index_search_context *ctx)
{
if (ctx->failed)
return;
/* remember the first failure */
mail_storage_last_error_push(ctx->box->storage);
ctx->failed = TRUE;
}
static void search_cur_mail_failed(struct index_search_context *ctx)
{
switch (mailbox_get_last_mail_error(ctx->cur_mail->box)) {
case MAIL_ERROR_EXPUNGED:
ctx->mail_ctx.seen_lost_data = TRUE;
break;
case MAIL_ERROR_LOOKUP_ABORTED:
/* expected failure */
break;
default:
search_set_failed(ctx);
break;
}
}
static void search_init_arg(struct mail_search_arg *arg,
struct index_search_context *ctx)
{
struct mailbox_metadata metadata;
bool match;
switch (arg->type) {
case SEARCH_SEQSET:
ctx->have_seqsets = TRUE;
break;
case SEARCH_UIDSET:
case SEARCH_INTHREAD:
case SEARCH_FLAGS:
case SEARCH_KEYWORDS:
case SEARCH_MODSEQ:
if (arg->type == SEARCH_MODSEQ)
mail_index_modseq_enable(ctx->box->index);
ctx->have_index_args = TRUE;
break;
case SEARCH_MAILBOX_GUID:
if (mailbox_get_metadata(ctx->box, MAILBOX_METADATA_GUID,
&metadata) < 0) {
/* result will be unknown */
break;
}
match = strcmp(guid_128_to_string(metadata.guid),
arg->value.str) == 0;
if (match != arg->match_not)
arg->match_always = TRUE;
else
arg->nonmatch_always = TRUE;
break;
case SEARCH_MAILBOX:
case SEARCH_MAILBOX_GLOB:
ctx->have_mailbox_args = TRUE;
break;
case SEARCH_ALL:
if (!arg->match_not)
arg->match_always = TRUE;
else
arg->nonmatch_always = TRUE;
break;
default:
break;
}
}
static void search_seqset_arg(struct mail_search_arg *arg,
struct index_search_context *ctx)
{
if (arg->type == SEARCH_SEQSET) {
if (seq_range_exists(&arg->value.seqset, ctx->mail_ctx.seq))
ARG_SET_RESULT(arg, 1);
else
ARG_SET_RESULT(arg, 0);
}
}
static int search_arg_match_keywords(struct index_search_context *ctx,
struct mail_search_arg *arg)
{
ARRAY_TYPE(keyword_indexes) keyword_indexes_arr;
const struct mail_keywords *search_kws = arg->initialized.keywords;
const unsigned int *keyword_indexes;
unsigned int i, j, count;
if (search_kws->count == 0) {
/* invalid keyword - never matches */
return 0;
}
t_array_init(&keyword_indexes_arr, 128);
mail_index_lookup_keywords(ctx->view, ctx->mail_ctx.seq,
&keyword_indexes_arr);
keyword_indexes = array_get(&keyword_indexes_arr, &count);
/* there probably aren't many keywords, so O(n*m) for now */
for (i = 0; i < search_kws->count; i++) {
for (j = 0; j < count; j++) {
if (search_kws->idx[i] == keyword_indexes[j])
break;
}
if (j == count)
return 0;
}
return 1;
}
static bool
index_search_get_pvt(struct index_search_context *ctx, uint32_t uid)
{
index_transaction_init_pvt(ctx->mail_ctx.transaction);
if (ctx->pvt_uid == uid)
return ctx->pvt_seq != 0;
ctx->pvt_uid = uid;
return mail_index_lookup_seq(ctx->mail_ctx.transaction->view_pvt,
uid, &ctx->pvt_seq);
}
/* Returns >0 = matched, 0 = not matched, -1 = unknown */
static int search_arg_match_index(struct index_search_context *ctx,
struct mail_search_arg *arg,
const struct mail_index_record *rec)
{
enum mail_flags flags, pvt_flags_mask;
uint64_t modseq;
int ret;
switch (arg->type) {
case SEARCH_UIDSET:
case SEARCH_INTHREAD:
return seq_range_exists(&arg->value.seqset, rec->uid);
case SEARCH_FLAGS:
/* recent flag shouldn't be set, but indexes from v1.0.x
may contain it. */
flags = rec->flags & ~MAIL_RECENT;
if ((arg->value.flags & MAIL_RECENT) != 0 &&
mailbox_recent_flags_have_uid(ctx->box, rec->uid))
flags |= MAIL_RECENT;
if (ctx->box->view_pvt == NULL) {
/* no private view (set by view syncing) ->
no private flags */
} else {
pvt_flags_mask = mailbox_get_private_flags_mask(ctx->box);
flags &= ~pvt_flags_mask;
if (index_search_get_pvt(ctx, rec->uid)) {
rec = mail_index_lookup(ctx->mail_ctx.transaction->view_pvt,
ctx->pvt_seq);
flags |= rec->flags & pvt_flags_mask;
}
}
return (flags & arg->value.flags) == arg->value.flags;
case SEARCH_KEYWORDS:
T_BEGIN {
ret = search_arg_match_keywords(ctx, arg);
} T_END;
return ret;
case SEARCH_MODSEQ: {
if (arg->value.flags != 0) {
modseq = mail_index_modseq_lookup_flags(ctx->view,
arg->value.flags, ctx->mail_ctx.seq);
} else if (arg->initialized.keywords != NULL) {
modseq = mail_index_modseq_lookup_keywords(ctx->view,
arg->initialized.keywords, ctx->mail_ctx.seq);
} else {
modseq = mail_index_modseq_lookup(ctx->view,
ctx->mail_ctx.seq);
}
return modseq >= arg->value.modseq->modseq;
}
default:
return -1;
}
}
static void search_index_arg(struct mail_search_arg *arg,
struct index_search_context *ctx)
{
const struct mail_index_record *rec;
rec = mail_index_lookup(ctx->view, ctx->mail_ctx.seq);
switch (search_arg_match_index(ctx, arg, rec)) {
case -1:
/* unknown */
break;
case 0:
ARG_SET_RESULT(arg, 0);
break;
default:
ARG_SET_RESULT(arg, 1);
break;
}
}
/* Returns >0 = matched, 0 = not matched, -1 = unknown */
static int search_arg_match_mailbox(struct index_search_context *ctx,
struct mail_search_arg *arg)
{
struct mailbox *box = ctx->cur_mail->box;
const char *str;
switch (arg->type) {
case SEARCH_MAILBOX:
/* first try to match the mailbox name itself. this is
important when using "mailbox virtual/foo" parameter foin
doveadm's search query, otherwise we can never fetch
anything with doveadm from virtual mailboxes because the
mailbox parameter is compared to the mail's backend
mailbox. */
if (strcmp(box->vname, arg->value.str) == 0)
return 1;
if (mail_get_special(ctx->cur_mail, MAIL_FETCH_MAILBOX_NAME,
&str) < 0) {
search_cur_mail_failed(ctx);
return -1;
}
if (strcasecmp(str, "INBOX") == 0)
return strcasecmp(arg->value.str, "INBOX") == 0;
return strcmp(str, arg->value.str) == 0;
case SEARCH_MAILBOX_GLOB:
if (imap_match(arg->initialized.mailbox_glob, box->vname) == IMAP_MATCH_YES)
return 1;
if (mail_get_special(ctx->cur_mail, MAIL_FETCH_MAILBOX_NAME,
&str) < 0) {
search_cur_mail_failed(ctx);
return -1;
}
return imap_match(arg->initialized.mailbox_glob, str) == IMAP_MATCH_YES;
default:
return -1;
}
}
static void search_mailbox_arg(struct mail_search_arg *arg,
struct index_search_context *ctx)
{
switch (search_arg_match_mailbox(ctx, arg)) {
case -1:
/* unknown */
break;
case 0:
ARG_SET_RESULT(arg, 0);
break;
default:
ARG_SET_RESULT(arg, 1);
break;
}
}
/* Returns >0 = matched, 0 = not matched, -1 = unknown */
static int search_arg_match_cached(struct index_search_context *ctx,
struct mail_search_arg *arg)
{
const char *str;
struct tm *tm;
uoff_t virtual_size;
time_t date;
int tz_offset;
bool have_tz_offset;
switch (arg->type) {
/* internal dates */
case SEARCH_BEFORE:
case SEARCH_ON:
case SEARCH_SINCE:
have_tz_offset = FALSE; tz_offset = 0; date = (time_t)-1;
switch (arg->value.date_type) {
case MAIL_SEARCH_DATE_TYPE_SENT:
if (mail_get_date(ctx->cur_mail, &date, &tz_offset) < 0) {
search_cur_mail_failed(ctx);
return -1;
}
have_tz_offset = TRUE;
break;
case MAIL_SEARCH_DATE_TYPE_RECEIVED:
if (mail_get_received_date(ctx->cur_mail, &date) < 0) {
search_cur_mail_failed(ctx);
return -1;
}
break;
case MAIL_SEARCH_DATE_TYPE_SAVED:
if (mail_get_save_date(ctx->cur_mail, &date) < 0) {
search_cur_mail_failed(ctx);
return -1;
}
break;
}
if ((arg->value.search_flags &
MAIL_SEARCH_ARG_FLAG_USE_TZ) == 0) {
if (!have_tz_offset) {
tm = localtime(&date);
tz_offset = utc_offset(tm, date);
}
date += tz_offset * 60;
}
switch (arg->type) {
case SEARCH_BEFORE:
return date < arg->value.time;
case SEARCH_ON:
return date >= arg->value.time &&
date < arg->value.time + 3600*24;
case SEARCH_SINCE:
return date >= arg->value.time;
default:
/* unreachable */
break;
}
/* sizes */
case SEARCH_SMALLER:
case SEARCH_LARGER:
if (mail_get_virtual_size(ctx->cur_mail, &virtual_size) < 0) {
search_cur_mail_failed(ctx);
return -1;
}
if (arg->type == SEARCH_SMALLER)
return virtual_size < arg->value.size;
else
return virtual_size > arg->value.size;
case SEARCH_GUID:
if (mail_get_special(ctx->cur_mail, MAIL_FETCH_GUID, &str) < 0) {
search_cur_mail_failed(ctx);
return -1;
}
return strcmp(str, arg->value.str) == 0;
case SEARCH_REAL_UID: {
struct mail *real_mail;
if (mail_get_backend_mail(ctx->cur_mail, &real_mail) < 0) {
search_cur_mail_failed(ctx);
return -1;
}
return seq_range_exists(&arg->value.seqset, real_mail->uid);
}
default:
return -1;
}
}
static void search_cached_arg(struct mail_search_arg *arg,
struct index_search_context *ctx)
{
switch (search_arg_match_cached(ctx, arg)) {
case -1:
/* unknown */
break;
case 0:
ARG_SET_RESULT(arg, 0);
break;
default:
ARG_SET_RESULT(arg, 1);
break;
}
}
static int search_sent(enum mail_search_arg_type type, time_t search_time,
const unsigned char *sent_value, size_t sent_value_len)
{
time_t sent_time;
int timezone_offset;
if (sent_value == NULL)
return 0;
/* NOTE: RFC-3501 specifies that timezone is ignored
in searches. sent_time is returned as UTC, so change it. */
if (!message_date_parse(sent_value, sent_value_len,
&sent_time, &timezone_offset))
return 0;
sent_time += timezone_offset * 60;
switch (type) {
case SEARCH_BEFORE:
return sent_time < search_time;
case SEARCH_ON:
return sent_time >= search_time &&
sent_time < search_time + 3600*24;
case SEARCH_SINCE:
return sent_time >= search_time;
default:
i_unreached();
}
}
static struct message_search_context *
msg_search_arg_context(struct index_search_context *ctx,
struct mail_search_arg *arg)
{
enum message_search_flags flags = 0;
if (arg->context == NULL) T_BEGIN {
string_t *dtc = t_str_new(128);
if (ctx->mail_ctx.normalizer(arg->value.str,
strlen(arg->value.str), dtc) < 0)
i_panic("search key not utf8: %s", arg->value.str);
if (arg->type == SEARCH_BODY)
flags |= MESSAGE_SEARCH_FLAG_SKIP_HEADERS;
/* we don't get here if arg is "", but dtc can be "" if it
only contains characters that we need to ignore. handle
those searches by returning them as non-matched. */
if (str_len(dtc) > 0) {
arg->context =
message_search_init(str_c(dtc),
ctx->mail_ctx.normalizer,
flags);
}
} T_END;
return arg->context;
}
static void compress_lwsp(string_t *dest, const unsigned char *src,
size_t src_len)
{
size_t i;
bool prev_lwsp = TRUE;
for (i = 0; i < src_len; i++) {
if (IS_LWSP(src[i])) {
if (!prev_lwsp) {
prev_lwsp = TRUE;
str_append_c(dest, ' ');
}
} else {
prev_lwsp = FALSE;
str_append_c(dest, src[i]);
}
}
}
static void search_header_arg(struct mail_search_arg *arg,
struct search_header_context *ctx)
{
struct message_search_context *msg_search_ctx;
struct message_block block;
struct message_header_line hdr;
int ret;
/* first check that the field name matches to argument. */
switch (arg->type) {
case SEARCH_BEFORE:
case SEARCH_ON:
case SEARCH_SINCE:
if (arg->value.date_type != MAIL_SEARCH_DATE_TYPE_SENT)
return;
/* date is handled differently than others */
if (strcasecmp(ctx->hdr->name, "Date") == 0) {
if (ctx->hdr->continues) {
ctx->hdr->use_full_value = TRUE;
return;
}
ret = search_sent(arg->type, arg->value.time,
ctx->hdr->full_value,
ctx->hdr->full_value_len);
ARG_SET_RESULT(arg, ret);
}
return;
case SEARCH_HEADER:
case SEARCH_HEADER_ADDRESS:
case SEARCH_HEADER_COMPRESS_LWSP:
ctx->custom_header = TRUE;
if (strcasecmp(ctx->hdr->name, arg->hdr_field_name) != 0)
return;
break;
default:
return;
}
if (arg->value.str[0] == '\0') {
/* we're just testing existence of the field. always matches. */
ARG_SET_RESULT(arg, 1);
return;
}
if (ctx->hdr->continues) {
ctx->hdr->use_full_value = TRUE;
return;
}
i_zero(&block);
/* We're searching only for values, so drop header name and middle
parts. We use header searching so that MIME words will be decoded. */
hdr = *ctx->hdr;
hdr.name = ""; hdr.name_len = 0;
hdr.middle_len = 0;
block.hdr = &hdr;
msg_search_ctx = msg_search_arg_context(ctx->index_ctx, arg);
if (msg_search_ctx == NULL)
return;
if (!ctx->decoded_block_set) { T_BEGIN {
struct message_address *addr;
string_t *str;
switch (arg->type) {
case SEARCH_HEADER:
/* simple match */
break;
case SEARCH_HEADER_ADDRESS:
/* we have to match against normalized address */
addr = message_address_parse(pool_datastack_create(),
ctx->hdr->full_value,
ctx->hdr->full_value_len,
UINT_MAX, TRUE);
str = t_str_new(ctx->hdr->value_len);
message_address_write(str, addr);
hdr.value = hdr.full_value = str_data(str);
hdr.value_len = hdr.full_value_len = str_len(str);
break;
case SEARCH_HEADER_COMPRESS_LWSP:
/* convert LWSP to single spaces */
str = t_str_new(hdr.full_value_len);
compress_lwsp(str, hdr.full_value, hdr.full_value_len);
hdr.value = hdr.full_value = str_data(str);
hdr.value_len = hdr.full_value_len = str_len(str);
break;
default:
i_unreached();
}
ret = message_search_more_get_decoded(msg_search_ctx, &block,
&ctx->decoded_block) ? 1 : 0;
ctx->decoded_block_set = TRUE;
} T_END; } else {
/* this block was already decoded and saved by an earlier
search arg. use the already-decoded block to avoid
duplicating work. */
ret = message_search_more_decoded(msg_search_ctx,
&ctx->decoded_block) ? 1 : 0;
}
/* there may be multiple headers. don't mark this failed yet. */
if (ret > 0)
ARG_SET_RESULT(arg, 1);
}
static void search_header_unmatch(struct mail_search_arg *arg,
struct search_header_context *ctx ATTR_UNUSED)
{
switch (arg->type) {
case SEARCH_BEFORE:
case SEARCH_ON:
case SEARCH_SINCE:
if (arg->value.date_type != MAIL_SEARCH_DATE_TYPE_SENT)
break;
if (arg->match_not) {
/* date header not found, so we match only for
NOT searches */
ARG_SET_RESULT(arg, 0);
}
break;
case SEARCH_HEADER:
case SEARCH_HEADER_ADDRESS:
case SEARCH_HEADER_COMPRESS_LWSP:
ARG_SET_RESULT(arg, 0);
break;
default:
break;
}
}
static void search_header(struct message_header_line *hdr,
struct search_header_context *ctx)
{
if (hdr == NULL) {
/* end of headers, mark all unknown SEARCH_HEADERs unmatched */
(void)mail_search_args_foreach(ctx->args, search_header_unmatch,
ctx);
return;
}
if (hdr->eoh)
return;
if (ctx->parse_headers)
index_mail_parse_header(NULL, hdr, ctx->imail);
if (ctx->custom_header || strcasecmp(hdr->name, "Date") == 0) {
ctx->hdr = hdr;
ctx->decoded_block_set = FALSE;
ctx->custom_header = FALSE;
(void)mail_search_args_foreach(ctx->args, search_header_arg, ctx);
}
}
static void search_body(struct mail_search_arg *arg,
struct search_body_context *ctx)
{
struct message_search_context *msg_search_ctx;
const char *error;
int ret;
switch (arg->type) {
case SEARCH_BODY:
case SEARCH_TEXT:
break;
default:
return;
}
msg_search_ctx = msg_search_arg_context(ctx->index_ctx, arg);
if (msg_search_ctx == NULL) {
ARG_SET_RESULT(arg, 0);
return;
}
i_stream_seek(ctx->input, 0);
ret = message_search_msg(msg_search_ctx, ctx->input, ctx->part, &error);
if (ret < 0 && ctx->input->stream_errno == 0) {
/* try again without cached parts */
index_mail_set_message_parts_corrupted(ctx->index_ctx->cur_mail, error);
i_stream_seek(ctx->input, 0);
ret = message_search_msg(msg_search_ctx, ctx->input, NULL, &error);
i_assert(ret >= 0 || ctx->input->stream_errno != 0);
}
if (ctx->input->stream_errno != 0) {
mail_storage_set_critical(ctx->index_ctx->box->storage,
"read(%s) failed: %s", i_stream_get_name(ctx->input),
i_stream_get_error(ctx->input));
}
ARG_SET_RESULT(arg, ret);
}
static int search_arg_match_text(struct mail_search_arg *args,
struct index_search_context *ctx)
{
const enum message_header_parser_flags hdr_parser_flags =
MESSAGE_HEADER_PARSER_FLAG_CLEAN_ONELINE;
struct index_mail *imail = (struct index_mail *)ctx->cur_mail;
struct mail *real_mail;
struct istream *input = NULL;
struct mailbox_header_lookup_ctx *headers_ctx;
struct search_header_context hdr_ctx;
struct search_body_context body_ctx;
const char *const *headers;
bool have_headers, have_body, failed = FALSE;
int ret;
/* first check what we need to use */
headers = mail_search_args_analyze(args, &have_headers, &have_body);
if (!have_headers && !have_body)
return -1;
i_zero(&hdr_ctx);
hdr_ctx.index_ctx = ctx;
/* hdr_ctx.imail is different from imail for mails in
virtual mailboxes */
if (mail_get_backend_mail(ctx->cur_mail, &real_mail) < 0) {
search_cur_mail_failed(ctx);
return -1;
}
hdr_ctx.imail = (struct index_mail *)real_mail;
hdr_ctx.custom_header = TRUE;
hdr_ctx.args = args;
headers_ctx = headers == NULL ? NULL :
mailbox_header_lookup_init(ctx->box, headers);
if (headers != NULL &&
(!have_body ||
ctx->cur_mail->lookup_abort != MAIL_LOOKUP_ABORT_NEVER)) {
/* try to look up the specified headers from cache */
i_assert(*headers != NULL);
if (mail_get_header_stream(ctx->cur_mail, headers_ctx,
&input) < 0) {
search_cur_mail_failed(ctx);
failed = TRUE;
} else {
message_parse_header(input, NULL, hdr_parser_flags,
search_header, &hdr_ctx);
}
input = NULL;
} else if (have_headers) {
/* we need to read the entire header */
ret = have_body ?
mail_get_stream_because(ctx->cur_mail, NULL, NULL, "search", &input) :
mail_get_hdr_stream_because(ctx->cur_mail, NULL, "search", &input);
if (ret < 0) {
search_cur_mail_failed(ctx);
failed = TRUE;
} else {
hdr_ctx.parse_headers =
index_mail_want_parse_headers(hdr_ctx.imail);
if (hdr_ctx.parse_headers) {
index_mail_parse_header_init(hdr_ctx.imail,
headers_ctx);
}
message_parse_header(input, NULL, hdr_parser_flags,
search_header, &hdr_ctx);
if (input->stream_errno != 0) {
mail_storage_set_critical(ctx->box->storage,
"read(%s) failed: %s",
i_stream_get_name(input),
i_stream_get_error(input));
failed = TRUE;
search_set_failed(ctx);
}
}
}
if (headers_ctx != NULL)
mailbox_header_lookup_unref(&headers_ctx);
if (failed) {
/* opening mail failed. maybe because of lookup_abort.
update access_parts for prefetching */
if (have_body)
imail->data.access_part |= READ_HDR | READ_BODY;
else
imail->data.access_part |= READ_HDR;
return -1;
}
if (have_headers) {
/* see if the header search succeeded in finishing the search */
ret = mail_search_args_foreach(args, search_none, (void *)NULL);
if (ret >= 0 || !have_body)
return ret;
}
i_assert(have_body);
if (ctx->cur_mail->lookup_abort != MAIL_LOOKUP_ABORT_NEVER) {
imail->data.access_part |= READ_HDR | READ_BODY;
return -1;
}
if (input == NULL) {
/* we didn't search headers. */
struct message_size hdr_size;
if (mail_get_stream_because(ctx->cur_mail, &hdr_size, NULL, "search", &input) < 0) {
search_cur_mail_failed(ctx);
return -1;
}
i_stream_seek(input, hdr_size.physical_size);
}
i_zero(&body_ctx);
body_ctx.index_ctx = ctx;
body_ctx.input = input;
/* Get parts if they already exist in cache. If they don't,
message-search will parse the mail automatically. */
ctx->cur_mail->lookup_abort = MAIL_LOOKUP_ABORT_NOT_IN_CACHE;
(void)mail_get_parts(ctx->cur_mail, &body_ctx.part);
ctx->cur_mail->lookup_abort = MAIL_LOOKUP_ABORT_NEVER;
return mail_search_args_foreach(args, search_body, &body_ctx);
}
static bool
search_msgset_fix_limits(unsigned int messages_count,
ARRAY_TYPE(seq_range) *seqset, bool match_not)
{
struct seq_range *range;
unsigned int count;
i_assert(messages_count > 0);
range = array_get_modifiable(seqset, &count);
if (count > 0) {
i_assert(range[0].seq1 != 0);
if (range[count-1].seq2 == (uint32_t)-1) {
/* "*" used, make sure the last message is in the range
(e.g. with count+1:* we still want to include it) */
seq_range_array_add(seqset, messages_count);
}
/* remove all nonexistent messages */
seq_range_array_remove_range(seqset, messages_count + 1,
(uint32_t)-1);
}
if (!match_not)
return array_count(seqset) > 0;
else {
/* if all messages are in the range, it can't match */
range = array_get_modifiable(seqset, &count);
return count == 0 || range[0].seq1 != 1 ||
range[count-1].seq2 != messages_count;
}
}
static void
search_msgset_fix(unsigned int messages_count,
ARRAY_TYPE(seq_range) *seqset,
uint32_t *seq1_r, uint32_t *seq2_r, bool match_not)
{
const struct seq_range *range;
unsigned int count;
uint32_t min_seq, max_seq;
if (!search_msgset_fix_limits(messages_count, seqset, match_not)) {
*seq1_r = (uint32_t)-1;
*seq2_r = 0;
return;
}
range = array_get(seqset, &count);
if (!match_not) {
min_seq = range[0].seq1;
max_seq = range[count-1].seq2;
} else if (count == 0) {
/* matches all messages */
min_seq = 1;
max_seq = messages_count;
} else {
min_seq = range[0].seq1 > 1 ? 1 : range[0].seq2 + 1;
max_seq = range[count-1].seq2 < messages_count ?
messages_count : range[count-1].seq1 - 1;
if (min_seq > max_seq) {
*seq1_r = (uint32_t)-1;
*seq2_r = 0;
return;
}
}
if (*seq1_r < min_seq || *seq1_r == 0)
*seq1_r = min_seq;
if (*seq2_r > max_seq)
*seq2_r = max_seq;
}
static void search_or_parse_msgset_args(unsigned int messages_count,
struct mail_search_arg *args,
uint32_t *seq1_r, uint32_t *seq2_r)
{
uint32_t seq1, seq2, min_seq1 = 0, max_seq2 = 0;
for (; args != NULL; args = args->next) {
seq1 = 1; seq2 = messages_count;
switch (args->type) {
case SEARCH_SUB:
i_assert(!args->match_not);
search_parse_msgset_args(messages_count,
args->value.subargs,
&seq1, &seq2);
break;
case SEARCH_OR:
i_assert(!args->match_not);
search_or_parse_msgset_args(messages_count,
args->value.subargs,
&seq1, &seq2);
break;
case SEARCH_SEQSET:
search_msgset_fix(messages_count, &args->value.seqset,
&seq1, &seq2, args->match_not);
break;
default:
break;
}
if (min_seq1 == 0) {
min_seq1 = seq1;
max_seq2 = seq2;
} else {
if (seq1 < min_seq1)
min_seq1 = seq1;
if (seq2 > max_seq2)
max_seq2 = seq2;
}
}
i_assert(min_seq1 != 0);
if (min_seq1 > *seq1_r)
*seq1_r = min_seq1;
if (max_seq2 < *seq2_r)
*seq2_r = max_seq2;
}
static void search_parse_msgset_args(unsigned int messages_count,
struct mail_search_arg *args,
uint32_t *seq1_r, uint32_t *seq2_r)
{
for (; args != NULL; args = args->next) {
switch (args->type) {
case SEARCH_SUB:
i_assert(!args->match_not);
search_parse_msgset_args(messages_count,
args->value.subargs,
seq1_r, seq2_r);
break;
case SEARCH_OR:
/* go through our children and use the widest seqset
range */
i_assert(!args->match_not);
search_or_parse_msgset_args(messages_count,
args->value.subargs,
seq1_r, seq2_r);
break;
case SEARCH_SEQSET:
search_msgset_fix(messages_count, &args->value.seqset,
seq1_r, seq2_r, args->match_not);
break;
default:
break;
}
}
}
static void search_limit_lowwater(struct index_search_context *ctx,
uint32_t uid_lowwater, uint32_t *first_seq)
{
uint32_t seq1, seq2;
if (uid_lowwater == 0)
return;
(void)mail_index_lookup_seq_range(ctx->view, uid_lowwater, (uint32_t)-1,
&seq1, &seq2);
if (*first_seq < seq1)
*first_seq = seq1;
}