/
main.c
2915 lines (2246 loc) · 75.4 KB
/
main.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
#include <mysql.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/file.h> //flock
#include <dlfcn.h> /* defines dlopen(), etc. */
#include <sys/types.h>
#include <dirent.h>
#include <time.h> // time(), localtime()
#include <unistd.h> // getpid()
#include <signal.h> // kill()
#include <err.h>
#if WITH_PATHACCESS_CACHE
#include <libmemcached/memcached.h>
#endif
#include "../logger/logger.h"
#include "../crawl/crawl.h"
#include "../key/key.h"
#include "../common/collection.h"
#include "../common/config.h"
#include "../common/define.h"
#include "../common/bstr.h"
#include "../common/daemon.h"
#include "../common/error.h"
#include "../common/timediff.h"
#include "../common/boithohome.h"
#include "../common/ht.h"
#include "../common/nice.h"
#include "../common/stdlib.h"
#include "../common/bfileutil.h"
#include "../maincfg/maincfg.h"
#include "../boitho-bbdn/bbdnclient.h"
#include "../common/strlcpy.h"
#include "../common/boithohome.h"
#include "../bbdocument/bbdocument.h"
#include "../3pLibs/keyValueHash/hashtable.h"
#include "../3pLibs/keyValueHash/hashtable_itr.h"
#include "../common/pidfile.h"
#include "../boithoadClientLib/boithoad.h"
#include "../boithoadClientLib/liboithoaut.h"
#include "../acls/acls.h"
#include "perlcrawl.h"
#include "../perlembed/perlembed.h"
#include "usersystem.h"
#include "shortenurl.h"
#define crawl_crawl 1
#define crawl_recrawl 2
/* MYSQL login information */
#define MYSQL_HOST "localhost"
#define MYSQL_USER "boitho"
#define MYSQL_PASS "G7J7v5L5Y7"
#define CRAWL_CRAWLING 1
#define CRAWL_DONE 2
#define CRAWL_ERROR 3
#define CRAWL_NOLAST 4
#define TEST_COLL_NAME "_%s_TestCollection" // %s is connector name.
#define BB_DATA_DIR "/boithoData"
#define MIN_DISK_REQUIRED 4194304 // 4GB in KB
struct hashtable *global_h, *usersystemshash;
struct {
char *addr;
int port;
} *memcache_servers;
int global_bbdnport;
#ifdef WITH_PATHACCESS_CACHE
void mc_add_servers(void);
#endif
#ifdef WITH_US_CACHE
#include "../libcache/libcache.h"
cache_t *cache;
cache_t *cache_groups;
#endif
static unsigned int cm_hashfromkey(void *k) {
return ((int) ((char *)k)[0]);
}
static int cm_equalkeys(void *k1, void *k2) {
return (0 == strcmp(k1,k2));
}
int file_exist (char file[]) {
FILE *FH;
if ((FH = fopen(file,"rb")) == NULL) {
return 0;
}
fclose(FH);
return 1;
}
void my_freevalue(void *p)
{
// free(p);
}
void groups_freevalue(void *p)
{
printf("groups_freevalue(%p, %i)\n",p, (*(char *)p));
free(p);
printf("~groups_freevalue\n");
}
void sql_connect(MYSQL *db) {
if (!mysql_init(db)) {
bblog(ERROR, "mysql_init, out of memory");
exit(1);
}
if (!mysql_real_connect(db, MYSQL_HOST, MYSQL_USER,
MYSQL_PASS, BOITHO_MYSQL_DB, 3306, NULL, 0)) {
bblog(INFO, "%s", mysql_error(db));
bblog(ERROR, "Mysql connect error: '%s'. Exiting.", mysql_error(db));
exit(1);
}
}
void sql_set_crawl_pid(MYSQL *db , int *pid, unsigned int coll_id) {
char query[1024];
char pid_str[64];
if (pid == NULL)
sprintf(pid_str, "NULL");
else
snprintf(pid_str, sizeof pid_str, "'%d'", *pid);
snprintf(query, sizeof query, "UPDATE shares SET crawl_pid = %s WHERE id = '%d'",
pid_str, coll_id);
if (mysql_real_query(db, query, strlen(query)))
bblog(ERROR, "mysql Error: %s", mysql_error(db));
}
int sql_coll_by_pid(int crawl_pid) {
char query[1024];
int coll_id;
MYSQL db;
sql_connect(&db);
snprintf(query, sizeof query,
"SELECT id FROM shares WHERE crawl_pid = '%d'",
crawl_pid);
MYSQL_ROW row;
if (mysql_real_query(&db, query, strlen(query))) {
bblog(ERROR, "mysql error: '%s'", mysql_error(&db));
exit(1);
}
MYSQL_RES * res = mysql_store_result(&db);
row = mysql_fetch_row(res);
coll_id = (row == NULL) ? -1 : atoi(row[0]);
mysql_free_result(res);
mysql_close(&db);
return coll_id;
}
void sql_fetch_params(MYSQL *db, struct hashtable ** h, unsigned int coll_id) {
char mysql_query[512];
MYSQL_ROW row;
(*h) = create_hashtable(7, cm_hashfromkey, cm_equalkeys);
if ((*h) == NULL) {
perror("Fail to creat hash table to hold sql params");
exit(-1);
}
snprintf(mysql_query, sizeof mysql_query, "\
SELECT shareParam.value, param.param \
FROM shareParam, param \
WHERE shareParam.param = param.id \
AND shareParam.share = %d", coll_id);
if (mysql_real_query(db, mysql_query, strlen(mysql_query))) {
bblog(INFO, mysql_error(db));
bblog(ERROR, "MySQL Error: \"%s\".",mysql_error(db));
exit(1);
}
MYSQL_RES * res = mysql_store_result(db);
while ((row = mysql_fetch_row(res)) != NULL) {
if (!hashtable_insert(*h, strdup(row[1]), strdup(row[0]))) {
bblog(INFO, "can't insert param into ht");
exit(1);
}
}
mysql_free_result(res);
}
int cm_collectionFetchUsers(struct collectionFormat *collection, MYSQL *db)
{
char mysql_query [2048];
MYSQL_RES *mysqlres; /* To be used to fetch information into */
MYSQL_ROW mysqlrow;
int i, numUsers;
sprintf(mysql_query, "SELECT name FROM shareUsers WHERE share = %d",
collection->id);
if(mysql_real_query(db, mysql_query, strlen(mysql_query))){ /* Make query */
bblog(INFO, "%s", mysql_error(db));
bblog(ERROR, "MySQL Error: \"%s\".",mysql_error(db));
return 0;
}
mysqlres=mysql_store_result(db); /* Download result from server */
numUsers = mysql_num_rows(mysqlres);
if (numUsers == 0) {
collection->users = NULL;
//returner lengder nede, så for vi også frigjort resursen
}
else {
bblog(DEBUGINFO, "nrofrows %i", numUsers);
collection->users = malloc((numUsers+1) * sizeof(char *));
if (collection->users == NULL)
return 0;
i=0;
while ((mysqlrow=mysql_fetch_row(mysqlres)) != NULL) { /* Get a row from the results */
collection->users[i] = strdup(mysqlrow[0]);
i++;
}
collection->users[i] = NULL;
}
//mysql freeing
mysql_free_result(mysqlres);
return 1;
}
int cm_searchForCollection(MYSQL *db, char cvalue[],struct collectionFormat *collection[],int *nrofcollections) {
char mysql_query [2048];
MYSQL_RES *mysqlres; /* To be used to fetch information into */
MYSQL_ROW mysqlrow;
int i;
if (cvalue != NULL) {
char cvalue_esc[128];
mysql_real_escape_string(db, cvalue_esc, cvalue, strlen(cvalue));
sprintf(mysql_query, "\
select \
resource, \
connectors.name, \
collection_name, \
UNIX_TIMESTAMP(last), \
query1, \
query2, \
auth_id, \
shares.id, \
shares.userprefix, \
shares.rate, \
shares.system, \
shares.alias, \
shares.crawl_pid \
from \
shares,connectors \
where \
connectors.ID = shares.connector \
AND collection_name='%s' \
",cvalue_esc);
}
else {
sprintf(mysql_query, "\
select \
resource, \
connectors.name, \
collection_name, \
UNIX_TIMESTAMP(last), \
query1, \
query2, \
auth_id, \
shares.id, \
shares.userprefix, \
shares.rate, \
shares.system, \
shares.alias, \
shares.crawl_pid \
from \
shares,connectors \
where \
connectors.ID = shares.connector \
");
}
bblog(DEBUGINFO, "mysql_query: %s",mysql_query);
if(mysql_real_query(db, mysql_query, strlen(mysql_query))){ /* Make query */
bblog(INFO, "%s", mysql_error(db));
bblog(ERROR, "MySQL Error: \"%s\" '''%s'''.",mysql_error(db), mysql_query);
exit(1);
}
mysqlres=mysql_store_result(db); /* Download result from server */
(*nrofcollections) = (int)mysql_num_rows(mysqlres);
if ((*nrofcollections) == 0) {
bblog(INFO, "dident find any rows");
(*collection) = NULL;
return 0;
}
else {
bblog(DEBUGINFO, "nrofrows %i",*nrofcollections);
(*collection) = malloc(sizeof(struct collectionFormat) * (*nrofcollections));
//resetter minne
for (i=0;i<(*nrofcollections);i++) {
collectionReset (collection[i]);
}
i=0;
while ((mysqlrow=mysql_fetch_row(mysqlres)) != NULL) { /* Get a row from the results */
bblog(DEBUGINFO, " data resource: %s, connector: %s, collection_name: %s, lastCrawl: %s, userprefix: %s, auth_id: %s",mysqlrow[0],mysqlrow[1],mysqlrow[2],mysqlrow[3],mysqlrow[8],mysqlrow[6]);
//lagger inn i info struct
(*collection)[i].resource = strdup(mysqlrow[0]);
(*collection)[i].connector = strdup(mysqlrow[1]);
(*collection)[i].collection_name = strdup(mysqlrow[2]);
if (mysqlrow[3] == NULL) {
(*collection)[i].lastCrawl = 0;
}
else {
(*collection)[i].lastCrawl = strtoul(mysqlrow[3], (char **)NULL, 10);
}
(*collection)[i].query1 = strdup(mysqlrow[4]);
(*collection)[i].query2 = strdup(mysqlrow[5]);
if (mysqlrow[6] == NULL) {
(*collection)[i].auth_id = 0;
}
else {
(*collection)[i].auth_id = strtoul(mysqlrow[6], (char **)NULL, 10);
}
(*collection)[i].id = strtoul(mysqlrow[7], (char **)NULL, 10);
(*collection)[i].userprefix = strdupnul(mysqlrow[8]);
(*collection)[i].rate = strtoul(mysqlrow[9], (char **)NULL, 10);
//runarb: 19 jul 2012 usikker på om dette er riktig. Vi sjekker ikke noen plass om usersystem er -1.
if (mysqlrow[10] == NULL) {
(*collection)[i].usersystem = -1;
}
else {
(*collection)[i].usersystem = strtoul(mysqlrow[10], (char **)NULL, 10);
}
if (mysqlrow[11] == NULL) {
(*collection)[i].alias = NULL;
} else {
(*collection)[i].alias = strdup(mysqlrow[11]);
}
(*collection)[i].extra = NULL;
if (mysqlrow[12] == NULL) {
(*collection)[i].pid = 0;
}
else {
(*collection)[i].pid = strtoul(mysqlrow[12], (char **)NULL, 10);
}
//normaliserer collection navn, ved å fjenre ting som space og - \ / etc
collection_normalize_name((*collection)[i].collection_name,strlen((*collection)[i].collection_name));
bblog(DEBUGINFO, "resource \"%s\", connector \"%s\", collection_name \"%s\"",(*collection)[i].resource,(*collection)[i].connector,(*collection)[i].collection_name);
cm_collectionFetchUsers(collection[i], db);
sql_fetch_params(db, &(*collection)[i].params, (*collection)[i].id);
//crawler ny
++i;
}
mysql_free_result(mysqlres);
/***********************************************************************/
// slår opp bruker id hvis vi har det, hvis ikke skal user og password være NULL
for (i=0;i<(*nrofcollections);i++) {
sprintf(mysql_query,"select username,password from collectionAuth where id = %i",(*collection)[i].auth_id);
bblog(DEBUGINFO, "mysql_query: %s",mysql_query);
if(mysql_real_query(db, mysql_query, strlen(mysql_query))){ /* Make query */
bblog(INFO, "%s", mysql_error(db));
exit(1);
}
mysqlres=mysql_store_result(db); /* Download result from server */
(*collection)[i].user = NULL;
(*collection)[i].password = NULL;
while ((mysqlrow=mysql_fetch_row(mysqlres)) != NULL) { /* Get a row from the results */
bblog(DEBUGINFO, "\tUser \"%s\", Password \"%s\"",mysqlrow[0],mysqlrow[1]);
(*collection)[i].user = strdup(mysqlrow[0]);
(*collection)[i].password = strdup(mysqlrow[1]);
}
}
mysql_free_result(mysqlres);
//inaliserer andre ting
for (i=0;i<(*nrofcollections);i++) {
(*collection)[i].errormsg[0] = '\0';
(*collection)[i].docsRemaining = -1;
(*collection)[i].docsCount = 0;
key_get((*collection)[i].systemkey);
}
/***********************************************************************/
}
bblog(DEBUGINFO, "cm_searchForCollection: end");
return 1;
}
//rutine for å laste et crawler biblotek
int cm_loadCrawlLib(struct hashtable **h, char name[]) {
bblog(DEBUGINFO, "cm_loadCrawlLib(name=%s)",name);
char libpath[PATH_MAX];
char perlpath[PATH_MAX];
char folderpath[PATH_MAX];
void* lib_handle; /* handle of the opened library */
struct crawlLibInfoFormat *crawlLibInfo;
const char* error_msg;
sprintf(libpath,"%s/%s/%s.so",bfile("crawlers"),name,name);
sprintf(perlpath,"%s/%s/main.pm",bfile("crawlers"),name);
sprintf(folderpath,"%s/%s/",bfile("crawlers"),name);
if (file_exist(libpath)) {
bblog(INFO, "loading path \"%s\"", libpath);
lib_handle = dlopen(libpath, RTLD_LAZY);
if (!lib_handle) {
bblog(ERROR, "Error: during dlopen(): %s. File %s.",dlerror(),libpath);
//exit(1);
return 0;
}
crawlLibInfo = dlsym(lib_handle, "crawlLibInfo");
/* check that no error occured */
error_msg = dlerror();
if (error_msg) {
bblog(ERROR, "Error: Error locating '%s' - %s.",name, error_msg);
exit(1);
}
bblog(INFO, "loaded \"%s\"", (*crawlLibInfo).shortname);
if ((*crawlLibInfo).crawlinit == NULL) {
}
else if (!(*crawlLibInfo).crawlinit()) {
bblog(INFO, "crawlinit dident return 1");
bblog(ERROR, "Error: crawlinit dident return 1.");
exit(1);
}
//sjekk at den ikke finnes i hashen fra før
if (hashtable_search((*h),(*crawlLibInfo).shortname) != NULL) {
bblog(INFO, "all redy have module with shortname \"%s\"", (*crawlLibInfo).shortname);
return 1;
}
if (! hashtable_insert((*h),(*crawlLibInfo).shortname,crawlLibInfo) ) {
bblog(ERROR, "Error: can't hastable insert.");
exit(-1);
}
//alt ok. Legg det i en hash
}
else if (file_exist(perlpath)) {
crawlLibInfo = perlCrawlStart(folderpath, name);
bblog(INFO, "loaded \"%s\"", (*crawlLibInfo).shortname);
//sjekk at den ikke finnes i hashen fra før
if (hashtable_search((*h),(*crawlLibInfo).shortname) != NULL) {
bblog(INFO, "all redy have module with shortname \"%s\"", (*crawlLibInfo).shortname);
return 1;
}
if (! hashtable_insert((*h),(*crawlLibInfo).shortname,crawlLibInfo) ) {
bblog(ERROR, "Error: can't hastable insert.");
exit(-1);
}
}
else {
bblog(ERROR, "can't load \"%s\" crawler.",name);
return 0;
}
return 1;
}
int cm_getCrawlLibInfo(struct hashtable *h,struct crawlLibInfoFormat **crawlLibInfo,char shortname[]) {
bblog(DEBUGINFO, "cm_getCrawlLibInfo: start");
bblog(DEBUGINFO, "wil search for \"%s\"",shortname);
if (((*crawlLibInfo) = (struct crawlLibInfoFormat *)hashtable_search(h,shortname)) != NULL) {
return 1;
}
else if ((cm_loadCrawlLib(&h, shortname)) && (((*crawlLibInfo) = (struct crawlLibInfoFormat *)hashtable_search(h,shortname)) != NULL)) {
//hvis vi ikke kunne finne det i hashen prøver vi å laset på ny
bblog(DEBUGINFO, "Hadde ikke %s fra før, men fikk til å laste den.",shortname);
return 1;
}
else {
berror("don't have a crawler for \"%s\"\n",shortname);
bblog(ERROR, "don't have a crawler for \"%s\"",shortname);
return 0;
}
}
usersystem_t * get_usersystem(MYSQL *db, unsigned int _id, usersystem_data_t *data) {
MYSQL_RES *res, *resparam;
MYSQL_ROW row, rowparam;
char query[1024];
size_t querylen;
int n;
usersystem_container_t *usc;
usersystem_t *us;
// if someone askes for system nr 0, we shal return the primary user system
if (_id == 0) {
querylen = snprintf(query, sizeof(query), "SELECT is_primary, connector, id FROM system WHERE is_primary = 1");
}
else {
querylen = snprintf(query, sizeof(query), "SELECT is_primary, connector,id FROM system WHERE id = %d", _id);
}
if (mysql_real_query(db, query, querylen)) {
bblog(ERROR, "Mysql error: %s", mysql_error(db));
return NULL;
}
res = mysql_store_result(db);
n = mysql_num_rows(res);
if (n == 0) {
mysql_free_result(res);
return NULL;
}
row = mysql_fetch_row(res);
if (row == NULL) {
bblog(ERROR, "Unable to fetch mysql row ar %s:%d",__FILE__,__LINE__);
mysql_free_result(res);
return NULL;
}
data->is_primary = atoi(row[0]);
data->type = atoi(row[1]);
data->id = atoi(row[2]);;
usc = hashtable_search(usersystemshash, &data->type);
if (usc == NULL) {
bblog(ERROR, "No usersystem module for this type: %d", data->type);
return NULL;
}
switch (usc->moduletype) {
case USC_TYPE_C:
us = usc->usersystem.us_c;
break;
case USC_TYPE_PERL:
us = usc->usersystem.us_perl->us;
break;
}
data->parameters = create_hashtable(7, ht_stringhash, ht_stringcmp);
querylen = snprintf(query, sizeof(query), "SELECT param, value FROM systemParamValue WHERE system = %d", data->id);
if (mysql_real_query(db, query, querylen)) {
bblog(ERROR, "Mysql error: %s", mysql_error(db));
return NULL;
}
resparam = mysql_store_result(db);
n = mysql_num_rows(resparam);
while ((rowparam = mysql_fetch_row(resparam)) != NULL) {
if (!strcmp(rowparam[0], "password")) bblog(INFO, "Param: password => ********");
else bblog(INFO, "Param: %s => %s", rowparam[0], rowparam[1]);
hashtable_insert(data->parameters, strdup(rowparam[0]), strdup(rowparam[1]));
}
mysql_free_result(res);
mysql_free_result(resparam);
data->usc = usc;
return us;
}
void
free_usersystem_data(usersystem_data_t *data)
{
hashtable_destroy(data->parameters, 1);
}
int documentContinue(struct collectionFormat *collection) {
bblog(DEBUGINFO, "documentContinue: start");
if ((collection->docsRemaining != -1) && (collection->docsCount >= collection->docsRemaining)) {
snprintf(collection->errormsg, sizeof collection->errormsg,
"User specified document limit reached.");
return 0;
}
int recrawl_schedule_start, recrawl_schedule_end;
struct tm *t;
time_t now;
//sjekker om vi har nokk plass lokalt. Sjkker førte gang så hver hundrende gang.
if ((collection->docsCount == 0) || ((collection->docsCount % 100) == 0)) {
if (!bbdn_HasSufficientSpace(collection->socketha, collection->collection_name)) {
snprintf(collection->errormsg, sizeof collection->errormsg,
"Insufficient disk space. Can't crawl any more documents.");
return 0;
}
}
//hvis vi skal crawl oftere en hvert dågn bruker vi ikke schedule time, men tilater å crawl hele tiden.
if ( (collection->rate != 0) && (collection->rate < 1440)) {
bblog(INFO, "documentContinue: Collection is set to be recrawled every %i min, ignoring schedule time", collection->rate);
return 1;
}
bconfig_flush(CONFIG_CACHE_IS_OK);
if (!bconfig_getentryint("recrawl_schedule_start",&recrawl_schedule_start)) {
recrawl_schedule_start = 0;
}
if (!bconfig_getentryint("recrawl_schedule_end",&recrawl_schedule_end)) {
recrawl_schedule_end = 0;
}
now = time(NULL);
t = localtime(&now);
bblog(DEBUGINFO, "now: %i,recrawl_schedule_start %i,recrawl_schedule_end %i",t->tm_hour,recrawl_schedule_start,recrawl_schedule_end);
//hvis vi ikke har noen begrensning så er det bare å crawler på
if ((recrawl_schedule_start == 0) || (recrawl_schedule_end == 0)) {
bblog(DEBUGINFO, "We can crawl att all times.");
return 1;
}
//tar en avgjørelse om vi skal fortsette å crawle
//vi har to scenarioer,
// 1: start er større en slutt tidspungete, og er dermed i fremtiden
// start 10, end 07
// kl nå 14
// kl nå 06
//
// 2: start og slutt tidspungtet er etter hverandre, og derfor innen samme dag
// start 10, end 12
// kl nå 14
//
int sched_cont = 1;
if ( recrawl_schedule_start > recrawl_schedule_end ) {
if ((t->tm_hour < recrawl_schedule_start) && (t->tm_hour >= recrawl_schedule_end)) {
bblog(INFO, "scenario 1: to early, wont crawl");
sched_cont = 0;
}
}
else {
if (t->tm_hour < recrawl_schedule_start) {
bblog(INFO, "scenario 2: to early, wont crawl");
sched_cont = 0;
}
else if (t->tm_hour >= recrawl_schedule_end) {
bblog(INFO, "scenario 2: to late, wont crawl");
sched_cont = 0;
}
}
if (!sched_cont) {
snprintf(collection->errormsg, sizeof collection->errormsg,
"Crawl is pending. Waiting for schedule time.");
return 0;
}
bblog(DEBUGINFO, "hour is now %i, will crawl",t->tm_hour);
bblog(DEBUGINFO, "documentContinue: end");
return 1;
}
int documentExist(struct collectionFormat *collection, struct crawldocumentExistFormat *crawldocumentExist) {
int ret;
#ifdef DEBUG
bblog(DEBUGINFO, "documentExist: start");
#endif
ret = bbdocument_exist(collection->collection_name, crawldocumentExist->documenturi, crawldocumentExist->lastmodified);
#ifdef DEBUG
bblog(DEBUGINFO, "documentExist: end");
#endif
return ret;
}
char *documentErrorGetlast(struct collectionFormat *collection) {
return collection->errormsg;
}
int documentError(struct collectionFormat *collection,int level, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
bblog(ERROR, "documentError: level: %d", level);
bblogv(ERROR, fmt, ap);
vsprintf(collection->errormsg ,fmt,ap);
va_end(ap);
return 1;
}
int documentAdd(struct collectionFormat *collection, struct crawldocumentAddFormat *crawldocumentAdd) {
#ifdef DEBUG
bblog(DEBUGINFO, "documentAdd start");
#endif
#ifdef DEBUG
bblog(DEBUGINFO, "documentAdd: uri %s, title %s",(*crawldocumentAdd).documenturi,(*crawldocumentAdd).title);
#endif
collection->docsCount++;
#ifdef DEBUG_TIME
struct timeval start_time, end_time;
gettimeofday(&start_time, NULL);
#endif
//send it inn
if (!bbdn_docadd(collection->socketha,
collection->collection_name,
crawldocumentAdd->documenturi,
crawldocumentAdd->documenttype,
crawldocumentAdd->document,
crawldocumentAdd->dokument_size,
crawldocumentAdd->lastmodified,
crawldocumentAdd->acl_allow,
crawldocumentAdd->acl_denied,
crawldocumentAdd->title,
crawldocumentAdd->doctype,
crawldocumentAdd->attributes)
) {
bblog(ERROR, "can't send document to document manager! Tried to send document \"%s\" Will sleep and then reconnect. Won't send the same document again.",(*crawldocumentAdd).documenturi);
//ber om å lokke sokketen. Dette er ikke det samme som å steneg kollectionen.
bbdn_close((*collection).socketha);
sleep(10);
if (!bbdn_conect(&(*collection).socketha,"",global_bbdnport)) {
bblog(ERROR, "can't connect to bbdn (boitho backend document server)");
return 0;
}
}
else {
bblog(CLEAN, "crawled url: \"%s\", size: %i b, ACL: allow \"%s\", denied \"%s\"",(*crawldocumentAdd).documenturi,(*crawldocumentAdd).dokument_size,(*crawldocumentAdd).acl_allow,(*crawldocumentAdd).acl_denied);
}
printf("** Added url: \"%s\"\n", (*crawldocumentAdd).documenturi);
#ifdef DEBUG_TIME
gettimeofday(&end_time, NULL);
bblog(DEBUGINFO, "Time debug: bbdn_docadd() time: %f",getTimeDifference(&start_time, &end_time));
#endif
#ifdef DEBUG
bblog(DEBUGINFO, "documentAdd end");
#endif
return 1;
}
int
collectionsforuser_collection(struct hashtable *collections, char *user, struct userToSubnameDbFormat *usertosubname)
{
char subnamebuf[maxSubnameLength];
int n_collections, i;
char **list;
#ifdef DEBUG
bblog(DEBUGINFO, "collectionsforuser_collection: user=%s", user);
#endif
if (!userToSubname_getsubnamesAsString(usertosubname, user, subnamebuf, sizeof(subnamebuf)))
return 0;
n_collections = split(subnamebuf, ",", &list);
#ifdef DEBUG
bblog(DEBUGINFO, " n_collections=%i", n_collections);
#endif
for (i = 0; i < n_collections; i++) {
#ifdef DEBUG
bblog(DEBUGINFO, " collection[%i] = %s", i, list[i]);
#endif
if (!hashtable_search(collections, list[i]))
hashtable_insert(collections, strdup(list[i]), (void*)0x1);
}
FreeSplitList(list);
return 0;
}
int _listGroupsForUser(usersystem_t *us, usersystem_data_t *data, const char *user, char ***groups, int *n_groups) {
int j;
int res;
char *gstring, *ps;
#ifdef WITH_US_CACHE
char *ckey;
asprintf(&ckey,"%s:%p",user,us);
ps = cache_fetch(cache_groups, "groupsForUser", ckey, 300);
if (ps != NULL) {
printf("_listGroupsForUser: Hav groups cached: \"%s\"\n",ps);
char **Data;
int Count;
*n_groups = split(ps, ",", &Data);
Count = 0;
(*groups) = malloc(((*n_groups)+1) * sizeof(char*));
while( (Data[Count] != NULL) ) {
(*groups)[Count] = strdup(Data[Count]);
Count++;
}
res = 1;
}
else {
#endif
printf("_listGroupsForUser(\"%s\"): No cache!\n", user);
res = (us->us_listGroupsForUser)(data, user, groups, n_groups);
if ((res == 1) && (n_groups > 0)) {
gstring = malloc( (*n_groups) * MAX_LDAP_ATTR_LEN );
gstring[0] = '\0';
ps = gstring;
for (j = 0; j < *n_groups; j++) {
if (j!=0) {
ps += strlcpy(ps, ",", MAX_LDAP_ATTR_LEN);
}
ps += strlcpy(ps, (*groups)[j], MAX_LDAP_ATTR_LEN);
}
printf("gstring: %s\n",gstring);
}
#ifdef WITH_US_CACHE
if ((res == 1) && (n_groups > 0)) {
if (cache_add(cache_groups, "groupsForUser", ckey, gstring, strlen(gstring) +1) != 1) { // +1 to save \0
printf("_listGroupsForUser: Can't add to cache");
}
}
}
free(ckey);
#endif
return res;
}
int
collectionsforuser(char *user, char **_collections, MYSQL *db)
{
MYSQL_RES *res;
MYSQL_ROW row;
char query[2][1024], *p;
size_t querylen[2];
int n;
struct hashtable *collections;
struct hashtable_itr *itr;
struct userToSubnameDbFormat userToSubnameDb;
int i, j;
/* XXX: In lack of union select */
querylen[0] = snprintf(query[0], sizeof(query[0]), "SELECT secnd_usr, system FROM systemMapping WHERE prim_usr = '%s'", user);
querylen[1] = snprintf(query[1], sizeof(query[1]), "SELECT '%s' AS secnd_usr, id AS 'system' FROM system WHERE is_primary = 1", user);
collections = create_hashtable(13, ht_stringhash, ht_stringcmp);
for (i = 0; i < 2; i++) {
if (mysql_real_query(db, query[i], querylen[i])) {
bblog(ERROR, "Mysql error: %s", mysql_error(db));
continue;
}
res = mysql_store_result(db);
n = mysql_num_rows(res);
if (n == 0) {
mysql_free_result(res);
continue;
}
if (!userToSubname_open(&userToSubnameDb,'r')) {
bblog(WARN, "Warning! Can't open users.db");
continue;
}
while ((row = mysql_fetch_row(res)) != NULL) {
char **groups;
int n_groups;
usersystem_t *us;
usersystem_data_t data;
if ((us = get_usersystem(db, atoi(row[1]), &data)) == NULL) {
bblog(WARN, "No usersystem: %s %s", row[0], row[1]);
continue;
}