Updated with hash related strings.
[doldaconnect.git] / daemon / search.c
CommitLineData
d3372da9 1/*
2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
19#include <stdlib.h>
20#include <malloc.h>
21#include <wchar.h>
22#include <wctype.h>
23#include <errno.h>
24#include <regex.h>
25#include <string.h>
26#include <time.h>
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31#include "utils.h"
32#include "log.h"
33#include "sysevents.h"
34#include "filenet.h"
7dd2a79b 35#include "client.h"
d3372da9 36#include "search.h"
37
38#define TOK_STR 0
39#define TOK_SE 1
40#define TOK_OP 2
41#define TOK_CP 3
42
43struct srchlist
44{
45 struct srchlist *next, *prev;
46 struct search *srch;
47};
48
49struct tok
50{
51 struct tok *next;
52 int type;
53 union
54 {
55 wchar_t *str;
56 struct sexpr *se;
57 } d;
58};
59
60struct reinfo
61{
62 wchar_t *begstr, *endstr, *onestr;
63 struct wcslist *strs;
64};
65
66struct bound
67{
68 int min, max;
69};
70
71static void trycommit(void);
72
73struct search *searches = NULL;
74static struct srchlist *searchqueue = NULL;
75static struct timer *committimer = NULL;
76GCBCHAIN(newsrchcb, struct search *);
77
78wchar_t *regexunquotesimple(wchar_t *re)
79{
80 wchar_t *specials, *buf, *p;
81
82 specials = L"\\^$.*+?[{()|";
83 buf = smalloc((wcslen(re) + 1) * sizeof(wchar_t));
84 p = buf;
85 for(; *re != L'\0'; re++)
86 {
87 if(*re == L'\\')
88 {
89 re++;
90 if(!*re)
91 {
92 *p = L'\0';
93 return(buf);
94 }
95 *(p++) = *re;
96 } else {
97 if(wcschr(specials, *re) != NULL)
98 {
99 free(buf);
100 return(NULL);
101 }
102 *(p++) = *re;
103 }
104 }
105 *p = L'\0';
106 return(buf);
107}
108
109static void freesln(struct wcslist *ln, struct wcslist **list)
110{
111 if(ln->prev != NULL)
112 ln->prev->next = ln->next;
113 if(ln->next != NULL)
114 ln->next->prev = ln->prev;
115 if(ln == *list)
116 *list = ln->next;
117 free(ln->str);
118 free(ln);
119}
120
121void freesl(struct wcslist **list)
122{
123 while(*list != NULL)
124 freesln(*list, list);
125}
126
127static struct wcslist *newsl(struct wcslist **list, wchar_t *str)
128{
129 struct wcslist *ln;
130
131 ln = smalloc(sizeof(*ln));
132 memset(ln, 0, sizeof(*ln));
133 ln->str = swcsdup(str);
134 ln->len = wcslen(str);
135 ln->next = *list;
136 ln->prev = NULL;
137 if(*list != NULL)
138 (*list)->prev = ln;
139 *list = ln;
140 return(ln);
141}
142
143static void slmerge1(struct wcslist **list, wchar_t *str)
144{
145 size_t len;
146 struct wcslist *cur, *next;
147
148 len = wcslen(str);
149 for(cur = *list; cur != NULL; cur = next)
150 {
151 next = cur->next;
152 if(len <= cur->len)
153 {
154 if(((len < cur->len) && wcsexists(cur->str, str)) || !wcscmp(str, cur->str))
155 return;
156 } else if(len > cur->len) {
157 if(wcsexists(str, cur->str))
158 freesln(cur, list);
159 }
160 }
161 newsl(list, str);
162}
163
164void slmergemax(struct wcslist **dest, struct wcslist *src)
165{
166 for(; src != NULL; src = src->next)
167 slmerge1(dest, src->str);
168}
169
170static struct wcslist *makeminlist1(wchar_t *s1, wchar_t *s2)
171{
172 int i;
173 wchar_t *p1, *p2, c;
174 struct wcslist *list;
175
176 list = NULL;
177 for(p1 = s1; *p1 != L'\0'; p1++)
178 {
179 for(p2 = s2; *p2 != L'\0'; p2++)
180 {
181 for(i = 0; (p1[i] != L'\0') && (p2[i] != L'\0') && (towlower(p1[i]) == towlower(p2[i])); i++);
182 if(i > 0)
183 {
184 c = p2[i];
185 p2[i] = L'\0';
186 slmerge1(&list, p2);
187 p2[i] = c;
188 }
189 }
190 }
191 return(list);
192}
193
194struct wcslist *slmergemin(struct wcslist *l1, struct wcslist *l2)
195{
196 struct wcslist *cur1, *cur2, *list, *rlist;
197
198 list = NULL;
199 for(cur1 = l1; cur1 != NULL; cur1 = cur1->next)
200 {
201 for(cur2 = l2; cur2 != NULL; cur2 = cur2->next)
202 {
203 rlist = makeminlist1(cur1->str, cur2->str);
204 slmergemax(&list, rlist);
205 freesl(&rlist);
206 }
207 }
208 return(list);
209}
210
211static struct bound readbound(wchar_t *p, wchar_t **endret)
212{
213 struct bound ret;
214
215 switch(*p)
216 {
217 case L'?':
218 ret.min = 0;
219 ret.max = 1;
220 p++;
221 break;
222 case L'*':
223 ret.min = 0;
224 ret.max = -1;
225 p++;
226 break;
227 case L'+':
228 ret.min = 1;
229 ret.max = -1;
230 p++;
231 break;
232 case L'{':
233 p++;
234 ret.min = wcstol(p, &p, 10);
235 if(*p == L',')
236 {
237 p++;
238 if(*p == L'}')
239 ret.max = -1;
240 else
241 ret.max = wcstol(p, &p, 10);
242 } else {
243 ret.max = ret.min;
244 }
245 if(*p != L'}')
246 {
247 /* Cannot happen in a validated regex... */
248 flog(LOG_WARNING, "BUG? \"Impossible\" case encountered in search.c:readbound() (No `}' after `{'-type bound!");
249 } else {
250 p++;
251 }
252 break;
253 default:
254 ret.min = 1;
255 ret.max = 1;
256 break;
257 }
258 if(endret != NULL)
259 *endret = p;
260 return(ret);
261}
262
263#define fnc(p) do {if((p) != NULL) free(p); (p) = NULL; p ## size = 0; p ## data = 0;} while(0)
264
265static struct reinfo analyzere(wchar_t *re, wchar_t **endret, wchar_t endc)
266{
267 int i, commit, parsealt;
268 struct reinfo ret, sinf;
269 struct bound b;
270 wchar_t *cs, *ns;
271 size_t cssize, csdata, nssize, nsdata, len1, len2, maxlen, minlen;
272 wchar_t beg, c;
273 struct wcslist *list;
274
275 memset(&ret, 0, sizeof(ret));
276 commit = parsealt = 0;
277 beg = 1;
278 cs = ns = NULL;
279 cssize = csdata = nssize = nsdata = 0;
280 while((*re != endc) && !parsealt)
281 {
282 switch(*re)
283 {
284 case L'$':
285 case L'^':
286 re++;
287 commit = 1;
288 break;
289 case L'.':
290 re++;
291 b = readbound(re, &re);
292 if(b.max != 0)
293 commit = 1;
294 break;
295 case L'(':
296 re++;
297 sinf = analyzere(re, &re, L')');
298 re++;
299 b = readbound(re, &re);
300 if(sinf.onestr != NULL)
301 {
302 for(i = 0; i < b.min; i++)
303 {
304 bufcat(cs, sinf.onestr, wcslen(sinf.onestr));
305 bufcat(ns, sinf.onestr, wcslen(sinf.onestr));
306 }
307 if((b.max == -1) || (b.max > b.min))
308 commit = 1;
309 } else {
310 commit = 1;
311 if(b.min > 0)
312 {
313 if(sinf.begstr != NULL)
314 bufcat(cs, sinf.begstr, wcslen(sinf.begstr));
315 if(sinf.endstr != NULL)
316 bufcat(ns, sinf.endstr, wcslen(sinf.endstr));
317 }
318 }
319 if(sinf.begstr != NULL)
320 free(sinf.begstr);
321 if(sinf.endstr != NULL)
322 free(sinf.endstr);
323 if(sinf.onestr != NULL)
324 free(sinf.onestr);
325 if(b.min > 0)
326 slmergemax(&ret.strs, sinf.strs);
327 freesl(&sinf.strs);
328 break;
329 case L'[':
330 c = L'\0';
331 re += 2;
332 /* Must exist in a validated RE... */
333 while(*(re++) != L']');
334 readbound(re, &re);
335 commit = 1;
336 break;
337 case L'|':
338 re++;
339 parsealt = 1;
340 break;
341 case L'\\':
342 re++;
343 /* A validated RE cannot end in a backslash, so fall
344 * through */
345 default:
346 c = *(re++);
347 b = readbound(re, &re);
348 for(i = 0; i < b.min; i++)
349 addtobuf(cs, c);
350 if((b.max == -1) || (b.max > b.min))
351 commit = 1;
352 break;
353 }
354 if(commit)
355 {
356 if(cs != NULL)
357 addtobuf(cs, L'\0');
358 if(beg)
359 {
360 if(cs != NULL)
361 ret.begstr = swcsdup(cs);
362 beg = 0;
363 }
364 if(cs != NULL)
365 slmerge1(&ret.strs, cs);
366 fnc(cs);
367 commit = 0;
368 if(ns != NULL)
369 {
370 cs = swcsdup(ns);
371 cssize = nssize;
372 csdata = nsdata;
373 fnc(ns);
374 }
375 }
376 }
377 if(cs != NULL)
378 {
379 addtobuf(cs, L'\0');
380 if(beg)
381 ret.onestr = swcsdup(cs);
382 else
383 ret.endstr = swcsdup(cs);
384 slmerge1(&ret.strs, cs);
385 fnc(cs);
386 }
387 if(parsealt)
388 {
389 sinf = analyzere(re, &re, endc);
390 list = slmergemin(ret.strs, sinf.strs);
391 freesl(&ret.strs);
392 freesl(&sinf.strs);
393 ret.strs = list;
394 if(sinf.begstr != NULL)
395 {
396 if(ret.begstr != NULL)
397 {
398 for(i = 0; (sinf.begstr[i] != L'\0') && (ret.begstr != L'\0') && (ret.begstr[i] == sinf.begstr[i]); i++);
399 if(i == 0)
400 free(ret.begstr);
401 else
402 ret.begstr[i] = L'\0';
403 }
404 free(sinf.begstr);
405 } else {
406 if(ret.begstr != NULL)
407 {
408 free(ret.begstr);
409 ret.begstr = NULL;
410 }
411 }
412 if(sinf.endstr != NULL)
413 {
414 if(ret.endstr != NULL)
415 {
416 len1 = wcslen(ret.endstr);
417 len2 = wcslen(sinf.endstr);
418 if(len1 < len2)
419 {
420 minlen = len1;
421 maxlen = len2;
422 } else {
423 minlen = len2;
424 maxlen = len1;
425 }
426 for(i = 1; (i <= minlen) && (ret.endstr[len1 - i] == sinf.endstr[len2 - i]); i++);
427 if(i == 1)
428 free(ret.endstr);
429 else if(i <= maxlen)
430 wmemmove(ret.endstr, ret.endstr + (len1 - i) + 1, i);
431 }
432 free(sinf.endstr);
433 } else {
434 if(ret.endstr != NULL)
435 {
436 free(ret.endstr);
437 ret.endstr = NULL;
438 }
439 }
440 if(sinf.onestr != NULL)
441 {
442 if(ret.onestr != NULL)
443 {
444 /* XXX: Comparing beginning and end of the onestrs and
445 * create begstr and endstr if there isn't an exact
446 * match.*/
447 if(wcscmp(ret.onestr, sinf.onestr))
448 {
449 free(ret.onestr);
450 ret.onestr = NULL;
451 }
452 }
453 free(sinf.onestr);
454 } else {
455 if(ret.onestr != NULL)
456 {
457 free(ret.onestr);
458 ret.onestr = NULL;
459 }
460 }
461 }
462 if(endret != NULL)
463 *endret = re;
464 return(ret);
465}
466
467#undef fnc
468
469struct wcslist *regexfindstrings(wchar_t *re)
470{
471 struct reinfo i;
472
473 i = analyzere(re, NULL, L'\0');
474 if(i.begstr != NULL)
475 free(i.begstr);
476 if(i.endstr != NULL)
477 free(i.endstr);
478 if(i.onestr != NULL)
479 free(i.onestr);
480 return(i.strs);
481}
482
483static struct sexpr *newsexpr(void)
484{
485 struct sexpr *sexpr;
486
487 sexpr = smalloc(sizeof(*sexpr));
488 memset(sexpr, 0, sizeof(*sexpr));
489 sexpr->refcount = 1;
490 return(sexpr);
491}
492
493void getsexpr(struct sexpr *sexpr)
494{
495 sexpr->refcount++;
496}
497
498void putsexpr(struct sexpr *sexpr)
499{
500 if(--sexpr->refcount != 0)
501 return;
502 if(sexpr->l != NULL)
503 putsexpr(sexpr->l);
504 if(sexpr->r != NULL)
505 putsexpr(sexpr->r);
506 if((sexpr->op == SOP_NAMERE) || (sexpr->op == SOP_LINKRE))
507 {
508 if(sexpr->d.re.sre != NULL)
509 free(sexpr->d.re.sre);
510 if(sexpr->d.re.inited)
511 regfree(&sexpr->d.re.cre);
512 }
513 if((sexpr->op == SOP_NAMESS) || (sexpr->op == SOP_LINKSS))
514 {
515 if(sexpr->d.s != NULL)
516 free(sexpr->d.s);
517 }
9d8cf743 518 if(sexpr->op == SOP_HASHIS)
519 {
520 if(sexpr->d.hash != NULL)
521 freehash(sexpr->d.hash);
522 }
d3372da9 523 free(sexpr);
524}
525
526static struct tok *newtok(void)
527{
528 struct tok *tok;
529
530 tok = smalloc(sizeof(*tok));
531 memset(tok, 0, sizeof(*tok));
532 tok->next = NULL;
533 return(tok);
534}
535
536static void freetok(struct tok *tok)
537{
538 if((tok->type == TOK_STR) && (tok->d.str != NULL))
539 free(tok->d.str);
540 if((tok->type == TOK_SE) && (tok->d.se != NULL))
541 putsexpr(tok->d.se);
542 free(tok);
543}
544
545static void pushtok(struct tok *tok, struct tok **st)
546{
547 tok->next = *st;
548 *st = tok;
549}
550
551static struct tok *poptok(struct tok **st)
552{
553 struct tok *tok;
554
555 tok = *st;
556 *st = (*st)->next;
557 return(tok);
558}
559
560int calccost(struct sexpr *sexpr)
561{
562 sexpr->tcost = sexpr->cost;
563 if(sexpr->l != NULL)
564 sexpr->tcost += calccost(sexpr->l);
565 if(sexpr->r != NULL)
566 sexpr->tcost += calccost(sexpr->r);
567 return(sexpr->tcost);
568}
569
570struct sexpr *parsesexpr(int argc, wchar_t **argv)
571{
572 int i, done, std;
573 struct tok *st, *tok, *tok2;
574 struct sexpr *sexpr;
575 char *buf;
576 wchar_t *wbuf;
577
578 std = 0;
579 st = NULL;
580 for(i = 0; i < argc; i++)
581 {
582 pushtok(tok = newtok(), &st);
583 tok->type = TOK_STR;
584 tok->d.str = swcsdup(argv[i]);
585 std++;
586 do
587 {
588 done = 1;
589 if((st->type == TOK_STR) && !wcscmp(st->d.str, L"("))
590 {
591 freetok(poptok(&st));
592 pushtok(tok = newtok(), &st);
593 tok->type = TOK_OP;
594 done = 0;
595 } else if((st->type == TOK_STR) && !wcscmp(st->d.str, L")")) {
596 freetok(poptok(&st));
597 pushtok(tok = newtok(), &st);
598 tok->type = TOK_CP;
599 done = 0;
600 } else if((st->type == TOK_STR) && (!wcsncmp(st->d.str, L"N~", 2) || !wcsncmp(st->d.str, L"L~", 2))) {
601 tok2 = poptok(&st);
602 pushtok(tok = newtok(), &st);
603 tok->type = TOK_SE;
604 sexpr = newsexpr();
605 if((wbuf = regexunquotesimple(tok2->d.str + 2)) != NULL)
606 {
607 if(tok2->d.str[0] == L'N')
608 sexpr->op = SOP_NAMESS;
609 else
610 sexpr->op = SOP_LINKSS;
611 sexpr->d.s = wbuf;
612 sexpr->cost = 5;
613 } else {
614 if(tok2->d.str[0] == L'N')
615 sexpr->op = SOP_NAMERE;
616 else
617 sexpr->op = SOP_LINKRE;
618 sexpr->cost = 20;
619 if((buf = icwcstombs(tok2->d.str + 2, "UTF-8")) == NULL)
620 {
621 freetok(tok2);
622 putsexpr(sexpr);
623 goto out_err;
624 }
625 if(regcomp(&sexpr->d.re.cre, buf, REG_EXTENDED | REG_ICASE | REG_NOSUB))
626 {
627 freetok(tok2);
628 free(buf);
629 putsexpr(sexpr);
630 goto out_err;
631 }
632 free(buf);
633 sexpr->d.re.inited = 1;
634 sexpr->d.re.sre = swcsdup(tok2->d.str + 2);
635 }
636 getsexpr(tok->d.se = sexpr);
637 freetok(tok2);
638 putsexpr(sexpr);
639 done = 0;
640 } else if((st->type == TOK_STR) && (!wcsncmp(st->d.str, L"S<", 2) || !wcsncmp(st->d.str, L"S=", 2) || !wcsncmp(st->d.str, L"S>", 2))) {
641 tok2 = poptok(&st);
642 pushtok(tok = newtok(), &st);
643 tok->type = TOK_SE;
644 sexpr = newsexpr();
645 if(tok2->d.str[1] == L'<')
646 sexpr->op = SOP_SIZELT;
647 else if(tok2->d.str[1] == L'=')
648 sexpr->op = SOP_SIZEEQ;
649 else
650 sexpr->op = SOP_SIZEGT;
651 sexpr->d.n = wcstol(tok2->d.str + 2, NULL, 0);
652 sexpr->cost = 0;
653 getsexpr(tok->d.se = sexpr);
654 freetok(tok2);
655 putsexpr(sexpr);
656 done = 0;
9d8cf743 657 } else if((st->type == TOK_STR) && !wcsncmp(st->d.str, L"H=", 2)) {
658 tok2 = poptok(&st);
659 pushtok(tok = newtok(), &st);
660 tok->type = TOK_SE;
661 sexpr = newsexpr();
662 sexpr->op = SOP_HASHIS;
663 if((sexpr->d.hash = parsehash(tok2->d.str + 2)) == NULL)
664 {
665 freetok(tok2);
666 putsexpr(sexpr);
667 goto out_err;
668 }
669 sexpr->cost = 2;
670 getsexpr(tok->d.se = sexpr);
671 freetok(tok2);
672 putsexpr(sexpr);
673 done = 0;
d3372da9 674 } else if((std >= 3) && (st->type == TOK_CP) && (st->next->type == TOK_SE) && (st->next->next->type == TOK_OP)) {
675 freetok(poptok(&st));
676 tok = poptok(&st);
677 freetok(poptok(&st));
678 pushtok(tok, &st);
679 std -= 2;
680 done = 0;
681 } else if((std >= 2) && (st->type == TOK_SE) && (st->next->type == TOK_STR) && !wcscmp(st->next->d.str, L"!")) {
682 sexpr = newsexpr();
683 sexpr->op = SOP_NOT;
684 sexpr->cost = 0;
685 getsexpr(sexpr->l = st->d.se);
686 freetok(poptok(&st));
687 freetok(poptok(&st));
688 pushtok(tok = newtok(), &st);
689 tok->type = TOK_SE;
690 getsexpr(tok->d.se = sexpr);
691 putsexpr(sexpr);
692 std -= 1;
693 done = 0;
694 } else if((std >= 3) && (st->type == TOK_SE) && (st->next->type == TOK_STR) && (!wcscmp(st->next->d.str, L"&") || !wcscmp(st->next->d.str, L"|")) && (st->next->next->type == TOK_SE)) {
695 sexpr = newsexpr();
696 if(!wcscmp(st->next->d.str, L"&"))
697 sexpr->op = SOP_AND;
698 else
699 sexpr->op = SOP_OR;
700 sexpr->cost = 0;
701 getsexpr(sexpr->l = st->next->next->d.se);
702 getsexpr(sexpr->r = st->d.se);
703 freetok(poptok(&st));
704 freetok(poptok(&st));
705 freetok(poptok(&st));
706 pushtok(tok = newtok(), &st);
707 tok->type = TOK_SE;
708 getsexpr(tok->d.se = sexpr);
709 putsexpr(sexpr);
710 std -= 2;
711 done = 0;
712 }
713 } while(!done);
714 }
715 if((st == NULL) || (st->next != NULL) || (st->type != TOK_SE))
716 goto out_err;
717 getsexpr(sexpr = st->d.se);
718 freetok(st);
719 calccost(sexpr);
720 return(sexpr);
721
722 out_err:
723 while(st != NULL)
724 freetok(poptok(&st));
725 return(NULL);
726}
727
728void optsexpr(struct sexpr *sexpr)
729{
730 struct sexpr *buf;
731
732 if((sexpr->l != NULL) && (sexpr->r != NULL))
733 {
734 if(sexpr->l->tcost > sexpr->r->tcost)
735 {
736 buf = sexpr->r;
737 sexpr->r = sexpr->l;
738 sexpr->l = buf;
739 }
740 }
741 if(sexpr->l != NULL)
742 optsexpr(sexpr->l);
743 if(sexpr->r != NULL)
744 optsexpr(sexpr->r);
745}
746
747struct wcslist *findsexprstrs(struct sexpr *sexpr)
748{
749 struct wcslist *list, *l1, *l2;
750
751 list = NULL;
752 switch(sexpr->op)
753 {
754 case SOP_AND:
755 list = findsexprstrs(sexpr->l);
756 l1 = findsexprstrs(sexpr->r);
757 slmergemax(&list, l1);
758 freesl(&l1);
759 break;
760 case SOP_OR:
761 l1 = findsexprstrs(sexpr->l);
762 l2 = findsexprstrs(sexpr->r);
763 list = slmergemin(l1, l2);
764 freesl(&l1);
765 freesl(&l2);
766 break;
d3372da9 767 case SOP_NAMERE:
768 case SOP_LINKRE:
769 list = regexfindstrings(sexpr->d.re.sre);
770 break;
771 case SOP_NAMESS:
772 case SOP_LINKSS:
773 slmerge1(&list, sexpr->d.s);
774 break;
775 default:
776 break;
777 }
778 return(list);
779}
780
781static void unlinksqueue(struct srchlist *ln)
782{
783 if(ln->prev != NULL)
784 ln->prev->next = ln->next;
785 if(ln->next != NULL)
786 ln->next->prev = ln->prev;
787 if(ln == searchqueue)
788 searchqueue = ln->next;
789 free(ln);
790}
791
792static void ctexpire(int cancelled, void *data)
793{
794 committimer = NULL;
795 if(!cancelled)
796 trycommit();
797}
798
799static void estimatequeue(void)
800{
801 struct srchlist *cur;
802 struct srchfnnlist *ln;
803 time_t now, start;
804
805 if(searchqueue == NULL)
806 return;
807 start = now = time(NULL);
808 for(ln = searchqueue->srch->fnl; ln != NULL; ln = ln->next)
809 {
810 if((ln->fn->lastsrch != 0) && (ln->fn->lastsrch + ln->fn->srchwait > start))
811 start = ln->fn->lastsrch + ln->fn->srchwait;
812 }
813 if(start != searchqueue->srch->eta)
814 {
815 searchqueue->srch->eta = start;
816 CBCHAINDOCB(searchqueue->srch, search_eta, searchqueue->srch);
817 }
818 for(cur = searchqueue->next; cur != NULL; cur = cur->next)
819 {
820 now = start;
821 for(ln = cur->srch->fnl; ln != NULL; ln = ln->next)
822 {
823 if(now + ln->fn->srchwait > start)
824 start = now + ln->fn->srchwait;
825 }
826 if(start != cur->srch->eta)
827 {
828 cur->srch->eta = start;
829 CBCHAINDOCB(cur->srch, search_eta, cur->srch);
830 }
831 }
832 if((committimer == NULL) || ((time_t)committimer->at != searchqueue->srch->eta))
833 {
834 if(committimer != NULL)
835 canceltimer(committimer);
836 committimer = timercallback(searchqueue->srch->eta, ctexpire, NULL);
837 }
838}
839
840struct search *findsearch(int id)
841{
842 struct search *srch;
843
844 for(srch = searches; srch != NULL; srch = srch->next)
845 {
846 if(srch->id == id)
847 break;
848 }
849 return(srch);
850}
851
852struct search *newsearch(wchar_t *owner, struct sexpr *sexpr)
853{
854 struct search *srch;
855 static int id = 0;
856
857 srch = smalloc(sizeof(*srch));
858 memset(srch, 0, sizeof(*srch));
859 srch->id = id++;
860 srch->owner = swcsdup(owner);
861 if((srch->sexpr = sexpr) != NULL)
862 getsexpr(srch->sexpr);
863 CBCHAININIT(srch, search_eta);
864 CBCHAININIT(srch, search_commit);
865 CBCHAININIT(srch, search_result);
866 CBCHAININIT(srch, search_destroy);
867 srch->next = searches;
868 srch->prev = NULL;
869 if(searches != NULL)
870 searches->prev = srch;
871 searches = srch;
872 return(srch);
873}
874
875static void srchexpire(int cancelled, struct search *srch)
876{
877 srch->freetimer = NULL;
878 if(!cancelled)
879 freesearch(srch);
880}
881
882static void trycommit(void)
883{
884 struct srchfnnlist *ln;
885 struct search *srch;
886 time_t now;
887
888 if(searchqueue == NULL)
889 return;
890 srch = searchqueue->srch;
891 now = time(NULL);
892 for(ln = srch->fnl; ln != NULL; ln = ln->next)
893 {
894 if(now < ln->fn->lastsrch + ln->fn->srchwait)
895 break;
896 }
897 if(ln != NULL)
898 return;
899 unlinksqueue(searchqueue);
900 srch->state = SRCH_RUN;
901 srch->eta = time(NULL);
902 srch->committime = ntime();
903 srch->freetimer = timercallback(ntime() + 300, (void (*)(int, void *))srchexpire, srch);
904 CBCHAINDOCB(srch, search_commit, srch);
905 for(ln = srch->fnl; ln != NULL; ln = ln->next)
906 fnetsearch(ln->fn, srch, ln);
907 estimatequeue();
908}
909
910void freesearch(struct search *srch)
911{
912 struct srchfnnlist *ln;
913 struct srchlist *sln;
914
915 if(srch->prev != NULL)
916 srch->prev->next = srch->next;
917 if(srch->next != NULL)
918 srch->next->prev = srch->prev;
919 if(srch == searches)
920 searches = srch->next;
921 estimatequeue();
922 if(srch->freetimer != NULL)
923 canceltimer(srch->freetimer);
924 CBCHAINDOCB(srch, search_destroy, srch);
925 CBCHAINFREE(srch, search_eta);
926 CBCHAINFREE(srch, search_commit);
927 CBCHAINFREE(srch, search_result);
928 CBCHAINFREE(srch, search_destroy);
929 while(srch->results != NULL)
930 freesrchres(srch->results);
931 for(sln = searchqueue; sln != NULL; sln = sln->next)
932 {
933 if(sln->srch == srch)
934 {
935 unlinksqueue(sln);
936 break;
937 }
938 }
939 while(srch->fnl != NULL)
940 {
941 ln = srch->fnl;
942 srch->fnl = ln->next;
943 CBCHAINDOCB(ln, searchfnl_destroy, ln);
944 CBCHAINFREE(ln, searchfnl_destroy);
945 putfnetnode(ln->fn);
946 free(ln);
947 }
948 if(srch->sexpr != NULL)
949 putsexpr(srch->sexpr);
950 if(srch->owner != NULL)
951 free(srch->owner);
952 free(srch);
953}
954
955void searchaddfn(struct search *srch, struct fnetnode *fn)
956{
957 struct srchfnnlist *ln;
958
959 for(ln = srch->fnl; ln != NULL; ln = ln->next)
960 {
961 if(ln->fn == fn)
962 return;
963 }
964 ln = smalloc(sizeof(*ln));
965 memset(ln, 0, sizeof(*ln));
966 getfnetnode(ln->fn = fn);
967 CBCHAININIT(ln, searchfnl_destroy);
968 ln->next = srch->fnl;
969 srch->fnl = ln;
970}
971
972static void linksearch(struct search *srch, struct srchlist *prev)
973{
974 struct srchlist *new;
975
976 new = smalloc(sizeof(*new));
977 new->srch = srch;
978 if(prev == NULL)
979 {
980 new->prev = NULL;
981 new->next = searchqueue;
982 if(searchqueue != NULL)
983 searchqueue->prev = new;
984 searchqueue = new;
985 } else {
986 new->prev = prev;
987 if((new->next = prev->next) != NULL)
988 new->next->prev = new;
989 prev->next = new;
990 }
991 GCBCHAINDOCB(newsrchcb, srch);
992 estimatequeue();
993}
994
995/*
996 * queuesearch is also the "scheduler" function - it finds a suitable
997 * place in the queue for the new search. I'll make a weak attempt at
998 * describing the algorithm:
999 * First, we find the first search that doesn't have a lower priority
1000 * than this one. If there is no such, we just link this one onto the
1001 * end of the queue.
1002 * Then, if we have a search of this priority in the queue with the
1003 * same owner as the new search, we set lastmine to the search after
1004 * that one, otherwise, lastmine is the first search of this
1005 * priority. If lastmine is discovered either to not exist (that is,
1006 * our last search is at the end of the queue), or to be of lower
1007 * priority (higher number), we link it in at the appropriate end.
1008 * Then, we find the next search of the same priority and owner as
1009 * lastmine, and link this search in before it. That should yield a
1010 * 'round-robin-like' scheduling within priority boundaries. I think.
1011 */
1012void queuesearch(struct search *srch)
1013{
1014 struct srchlist *cur, *lastmine, *prev;
1015 wchar_t *nexto;
1016
1017 for(prev = NULL, cur = searchqueue; cur != NULL; prev = cur, cur = cur->next)
1018 {
1019 if(cur->srch->prio >= srch->prio)
1020 break;
1021 }
1022 if(cur == NULL)
1023 {
1024 linksearch(srch, prev);
1025 return;
1026 }
1027 lastmine = cur;
1028 for(; cur != NULL; prev = cur, cur = cur->next)
1029 {
1030 if(!wcscmp(cur->srch->owner, srch->owner))
1031 lastmine = cur->next;
1032 if(cur->srch->prio > srch->prio)
1033 break;
1034 }
1035 if((lastmine == NULL) || (lastmine->srch->prio > srch->prio))
1036 {
1037 linksearch(srch, prev);
1038 return;
1039 }
1040 nexto = lastmine->srch->owner;
1041 for(cur = lastmine->next; cur != NULL; prev = cur, cur = cur->next)
1042 {
1043 if(!wcscmp(cur->srch->owner, nexto))
1044 break;
1045 if(cur->srch->prio > srch->prio)
1046 break;
1047 }
1048 if(cur == NULL)
1049 {
1050 linksearch(srch, prev);
1051 return;
1052 }
1053 linksearch(srch, prev);
1054}
1055
1056static int srisvalid(struct srchres *sr, struct sexpr *sexpr)
1057{
1058 int ret;
1059 char *buf;
1060 wchar_t *p;
1061
1062 switch(sexpr->op)
1063 {
1064 case SOP_FALSE:
1065 return(0);
1066 case SOP_TRUE:
1067 return(1);
1068 case SOP_AND:
1069 if(!srisvalid(sr, sexpr->l))
1070 return(0);
1071 return(srisvalid(sr, sexpr->r));
1072 case SOP_OR:
1073 if(srisvalid(sr, sexpr->l))
1074 return(1);
1075 return(srisvalid(sr, sexpr->r));
1076 case SOP_NOT:
1077 return(!srisvalid(sr, sexpr->l));
1078 case SOP_NAMERE:
1079 if((buf = icwcstombs(sr->filename, "UTF-8")) == NULL)
1080 return(0);
1081 ret = regexec(&sexpr->d.re.cre, buf, 0, NULL, 0);
1082 free(buf);
1083 return(!ret);
1084 case SOP_LINKRE:
1085 p = sr->filename;
1086 if(sr->fnet->filebasename != NULL)
1087 p = sr->fnet->filebasename(p);
1088 if((buf = icwcstombs(p, "UTF-8")) == NULL)
1089 return(0);
1090 ret = regexec(&sexpr->d.re.cre, buf, 0, NULL, 0);
1091 free(buf);
1092 return(!ret);
1093 case SOP_NAMESS:
1094 return(wcsexists(sr->filename, sexpr->d.s));
1095 case SOP_LINKSS:
1096 p = sr->filename;
1097 if(sr->fnet->filebasename != NULL)
1098 p = sr->fnet->filebasename(p);
1099 return(wcsexists(p, sexpr->d.s));
1100 case SOP_SIZELT:
1101 return(sr->size < sexpr->d.n);
1102 case SOP_SIZEEQ:
1103 return(sr->size == sexpr->d.n);
1104 case SOP_SIZEGT:
1105 return(sr->size > sexpr->d.n);
9d8cf743 1106 case SOP_HASHIS:
1107 if(sr->hash == NULL)
1108 return(0);
1109 return(hashcmp(sr->hash, sexpr->d.hash));
d3372da9 1110 }
1111 return(0);
1112}
1113
1114struct srchres *newsrchres(struct fnet *fnet, wchar_t *filename, wchar_t *peerid)
1115{
1116 struct srchres *sr;
1117
1118 sr = smalloc(sizeof(*sr));
1119 memset(sr, 0, sizeof(*sr));
1120 sr->size = -1;
1121 sr->slots = -1;
1122 sr->fnet = fnet;
1123 sr->filename = swcsdup(filename);
1124 sr->peerid = swcsdup(peerid);
1125 return(sr);
1126}
1127
1128void freesrchres(struct srchres *sr)
1129{
1130 if(sr->next != NULL)
1131 sr->next->prev = sr->prev;
1132 if(sr->prev != NULL)
1133 sr->prev->next = sr->next;
1134 if(sr->srch != NULL)
1135 {
1136 if(sr->srch->results == sr)
1137 sr->srch->results = sr->next;
1138 sr->srch->numres--;
1139 }
7dd2a79b 1140 if(sr->hash != NULL)
1141 freehash(sr->hash);
d3372da9 1142 if(sr->filename != NULL)
1143 free(sr->filename);
1144 if(sr->peerid != NULL)
1145 free(sr->peerid);
1146 if(sr->peernick != NULL)
1147 free(sr->peernick);
1148 if(sr->fn != NULL)
1149 putfnetnode(sr->fn);
1150 free(sr);
1151}
1152
1153struct srchres *dupsrchres(struct srchres *sr)
1154{
1155 struct srchres *new;
1156
1157 new = smalloc(sizeof(*new));
1158 memset(new, 0, sizeof(*new));
1159 new->size = sr->size;
1160 new->slots = sr->slots;
1161 new->fnet = sr->fnet;
1162 if(sr->peerid != NULL)
1163 new->peerid = swcsdup(sr->peerid);
1164 if(sr->peernick != NULL)
1165 new->peernick = swcsdup(sr->peernick);
1166 if(sr->filename != NULL)
1167 new->filename = swcsdup(sr->filename);
1168 if(sr->fn != NULL)
1169 getfnetnode(new->fn = sr->fn);
7dd2a79b 1170 if(sr->hash != NULL)
1171 new->hash = duphash(sr->hash);
d3372da9 1172 return(new);
1173}
1174
1175static void linksr(struct search *srch, struct srchres *sr)
1176{
1177 sr->prev = NULL;
1178 sr->next = srch->results;
1179 if(srch->results != NULL)
1180 srch->results->prev = sr;
1181 srch->results = sr;
1182 sr->srch = srch;
1183 sr->time = ntime() - srch->committime;
1184 srch->numres++;
1185}
1186
1187void submitsrchres(struct srchres *sr)
1188{
1189 struct search *srch;
1190 struct srchfnnlist *ln;
1191 struct srchres *dsr;
1192
1193 for(srch = searches; srch != NULL; srch = srch->next)
1194 {
1195 if(srch->state == SRCH_RUN)
1196 {
1197 if(!srisvalid(sr, srch->sexpr))
1198 continue;
1199 for(ln = srch->fnl; ln != NULL; ln = ln->next)
1200 {
1201 if(((sr->fn != NULL) && (ln->fn == sr->fn)) || ((sr->fn == NULL) && (sr->fnet == ln->fn->fnet)))
1202 break;
1203 }
1204 if(ln != NULL)
1205 {
1206 dsr = dupsrchres(sr);
1207 linksr(srch, dsr);
1208 CBCHAINDOCB(srch, search_result, srch, dsr);
1209 }
1210 }
1211 }
1212}