-
Notifications
You must be signed in to change notification settings - Fork 0
/
nomos_utils.c
1030 lines (858 loc) · 24.8 KB
/
nomos_utils.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) 2006-2014 Hewlett-Packard Development Company, L.P.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
***************************************************************/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif /* not defined _GNU_SOURCE */
#include "nomos_utils.h"
#include "nomos.h"
#define FUNCTION
/**
* \file
* \brief Utilities used by nomos
*/
/**
\brief Add a new license to license_ref table
Adds a license to license_ref table.
@param licenseName Name of license
@return rf_pk for success, 0 for failure
*/
FUNCTION long add2license_ref(char *licenseName)
{
PGresult *result;
char query[myBUFSIZ];
char insert[myBUFSIZ];
char escLicName[myBUFSIZ];
char *specialLicenseText;
long rf_pk;
int len;
int error;
int numRows;
// escape the name
len = strlen(licenseName);
PQescapeStringConn(gl.pgConn, escLicName, licenseName, len, &error);
if (error)
LOG_WARNING("Does license name %s have multibyte encoding?", licenseName)
/* verify the license is not already in the table */
sprintf(query, "SELECT rf_pk FROM " LICENSE_REF_TABLE " where rf_shortname='%s'", escLicName);
result = PQexec(gl.pgConn, query);
if (fo_checkPQresult(gl.pgConn, result, query, __FILE__, __LINE__))
return 0;
numRows = PQntuples(result);
if (numRows)
{
rf_pk = atol(PQgetvalue(result, 0, 0));
PQclear(result);
return rf_pk;
}
PQclear(result);
/* Insert the new license */
specialLicenseText = "License by Nomos.";
sprintf(insert, "insert into license_ref(rf_shortname, rf_text, rf_detector_type) values('%s', '%s', 2)", escLicName,
specialLicenseText);
result = PQexec(gl.pgConn, insert);
// ignore duplicate constraint failure (23505), report others
if ((result == 0)
|| ((PQresultStatus(result) != PGRES_COMMAND_OK)
&& (strncmp(PG_ERRCODE_UNIQUE_VIOLATION, PQresultErrorField(result, PG_DIAG_SQLSTATE), 5))))
{
printf("ERROR: %s(%d): Nomos failed to add a new license. %s/n: %s/n",
__FILE__, __LINE__, PQresultErrorMessage(result), insert);
PQclear(result);
return (0);
}
PQclear(result);
/* retrieve the new rf_pk */
result = PQexec(gl.pgConn, query);
if (fo_checkPQresult(gl.pgConn, result, query, __FILE__, __LINE__))
return 0;
numRows = PQntuples(result);
if (numRows)
rf_pk = atol(PQgetvalue(result, 0, 0));
else
{
printf("ERROR: %s:%s:%d Just inserted value is missing. On: %s", __FILE__, "add2license_ref()", __LINE__, query);
PQclear(result);
return (0);
}
PQclear(result);
return (rf_pk);
}
/**
\brief calculate the hash of an rf_shortname
rf_shortname is the key
@param pcroot Root pointer
@param rf_shortname
@return hash value
*/
FUNCTION long lrcache_hash(cacheroot_t *pcroot, char *rf_shortname)
{
long hashval = 0;
int len, i;
/* use the first sizeof(long) bytes for the hash value */
len = (strlen(rf_shortname) < sizeof(long)) ? strlen(rf_shortname) : sizeof(long);
for (i = 0; i < len; i++)
hashval += rf_shortname[i] << 8 * i;
hashval = hashval % pcroot->maxnodes;
return hashval;
}
/**
\brief Print the contents of the hash table
@param pcroot Table root
@return none
*/
FUNCTION void lrcache_print(cacheroot_t *pcroot)
{
cachenode_t *pcnode;
long hashval = 0;
int i;
pcnode = pcroot->nodes;
for (i = 0; i < pcroot->maxnodes; i++)
{
if (pcnode->rf_pk != 0L)
{
hashval = lrcache_hash(pcroot, pcnode->rf_shortname);
printf("%ld, %ld, %s\n", hashval, pcnode->rf_pk, pcnode->rf_shortname);
}
pcnode++;
}
}
/**
\brief free the hash table
@param pcroot Table root
@return none
*/
FUNCTION void lrcache_free(cacheroot_t *pcroot)
{
cachenode_t *pcnode;
int i;
pcnode = pcroot->nodes;
for (i = 0; i < pcroot->maxnodes; i++)
{
if (pcnode->rf_pk != 0L)
{
free(pcnode->rf_shortname);
}
pcnode++;
}
free(pcroot->nodes);
}
/**
\brief add a rf_shortname, rf_pk to the license_ref cache
rf_shortname is the key
@param pcroot Table root
@param rf_pk License ID in DB
@param rf_shortname License shortname (key)
@return -1 for failure, 0 for success
*/
FUNCTION int lrcache_add(cacheroot_t *pcroot, long rf_pk, char *rf_shortname)
{
cachenode_t *pcnode;
long hashval = 0;
int i;
int noden;
hashval = lrcache_hash(pcroot, rf_shortname);
noden = hashval;
for (i = 0; i < pcroot->maxnodes; i++)
{
noden = (hashval + i) & (pcroot->maxnodes - 1);
pcnode = pcroot->nodes + noden;
if (!pcnode->rf_pk)
{
pcnode->rf_shortname = strdup(rf_shortname);
pcnode->rf_pk = rf_pk;
break;
}
}
if (i < pcroot->maxnodes)
return 0;
return -1; /* no space */
}
/**
\brief lookup rf_pk in the license_ref cache
rf_shortname is the key
@param pcroot
@param rf_shortname
@return rf_pk, 0 if the shortname is not in the cache
*/
FUNCTION long lrcache_lookup(cacheroot_t *pcroot, char *rf_shortname)
{
cachenode_t *pcnode;
long hashval = 0;
int i;
int noden;
hashval = lrcache_hash(pcroot, rf_shortname);
noden = hashval;
for (i = 0; i < pcroot->maxnodes; i++)
{
noden = (hashval + i) & (pcroot->maxnodes - 1);
pcnode = pcroot->nodes + noden;
if (!pcnode->rf_pk)
return 0;
if (strcmp(pcnode->rf_shortname, rf_shortname) == 0)
{
return pcnode->rf_pk;
}
}
return 0; /* not found */
}
/**
\brief build a cache the license ref db table.
initLicRefCache builds a cache using the rf_shortname as the key
and the rf_pk as the value. This is an optimization. The cache is used for
reference license lookups instead of querying the db.
@param pcroot
@return 0 for failure, 1 for success
*/
FUNCTION int initLicRefCache(cacheroot_t *pcroot)
{
PGresult *result;
char query[myBUFSIZ];
int row;
int numLics;
if (!pcroot)
return 0;
sprintf(query, "SELECT rf_pk, rf_shortname FROM " LICENSE_REF_TABLE " where rf_detector_type=2");
result = PQexec(gl.pgConn, query);
if (fo_checkPQresult(gl.pgConn, result, query, __FILE__, __LINE__))
return 0;
numLics = PQntuples(result);
/* populate the cache */
for (row = 0; row < numLics; row++)
{
lrcache_add(pcroot, atol(PQgetvalue(result, row, 0)), PQgetvalue(result, row, 1));
}
PQclear(result);
return (1);
} /* initLicRefCache */
/**
\brief Get the rf_pk for rf_shortname
Checks the cache to get the rf_pk for this shortname.
If it doesn't exist, add it to both license_ref and the
license_ref cache (the hash table).
@param pcroot
@param rf_shortname
@return rf_pk of the matched license or 0
*/
FUNCTION long get_rfpk(cacheroot_t *pcroot, char *rf_shortname)
{
long rf_pk;
size_t len;
if ((len = strlen(rf_shortname)) == 0)
{
printf("ERROR! Nomos.c get_rfpk() passed empty name");
return (0);
}
/* is this in the cache? */
rf_pk = lrcache_lookup(pcroot, rf_shortname);
if (rf_pk)
return rf_pk;
/* shortname was not found, so add it */
/* add to the license_ref table */
rf_pk = add2license_ref(rf_shortname);
/* add to the cache */
lrcache_add(pcroot, rf_pk, rf_shortname);
return (rf_pk);
} /* get_rfpk */
/**
\brief Given a string that contains field='value' pairs, save the items.
@return pointer to start of next field, or NULL at \0.
\callgraph
*/
FUNCTION char *getFieldValue(char *inStr, char *field, int fieldMax,
char *value, int valueMax, char separator)
{
int s;
int f;
int v;
int gotQuote;
#ifdef PROC_TRACE
traceFunc("== getFieldValue(inStr= %s fieldMax= %d separator= '%c'\n",
inStr, fieldMax, separator);
#endif /* PROC_TRACE */
memset(field, 0, fieldMax);
memset(value, 0, valueMax);
/* Skip initial spaces */
while (isspace(inStr[0]))
{
inStr++;
}
if (inStr[0] == '\0')
{
return (NULL);
}
f = 0;
v = 0;
/* Skip to end of field name */
for (s = 0; (inStr[s] != '\0') && !isspace(inStr[s]) && (inStr[s] != '='); s++)
{
field[f++] = inStr[s];
}
/* Skip spaces after field name */
while (isspace(inStr[s]))
{
s++;
}
/* If it is not a field, then just return it. */
if (inStr[s] != separator)
{
return (inStr + s);
}
if (inStr[s] == '\0')
{
return (NULL);
}
/* Skip '=' */
s++;
/* Skip spaces after '=' */
while (isspace(inStr[s]))
{
s++;
}
if (inStr[s] == '\0')
{
return (NULL);
}
gotQuote = '\0';
if ((inStr[s] == '\'') || (inStr[s] == '"'))
{
gotQuote = inStr[s];
s++; /* skip quote */
if (inStr[s] == '\0')
{
return (NULL);
}
}
if (gotQuote)
{
for (; (inStr[s] != '\0') && (inStr[s] != gotQuote); s++)
{
if (inStr[s] == '\\')
{
value[v++] = inStr[++s];
}
else
{
value[v++] = inStr[s];
}
}
}
else
{
/* if it gets here, then there is no quote */
for (; (inStr[s] != '\0') && !isspace(inStr[s]); s++)
{
if (inStr[s] == '\\')
{
value[v++] = inStr[++s];
}
else
{
value[v++] = inStr[s];
}
}
}
/* Skip spaces */
while (isspace(inStr[s]))
{
s++;
}
return (inStr + s);
} /* getFieldValue */
/**
\brief parse the comma separated list of license names found
Uses cur.compLic and sets cur.licenseList
*/
FUNCTION void parseLicenseList()
{
int numLics = 0;
/* char saveLics[myBUFSIZ]; */
char *saveptr = 0; /* used for strtok_r */
char *saveLicsPtr;
if ((strlen(cur.compLic)) == 0)
{
return;
}
/* check for a single name FIX THIS!*/
if (strstr(cur.compLic, ",") == NULL)
{
cur.licenseList[0] = cur.compLic;
cur.licenseList[1] = NULL;
return;
}
saveLicsPtr = strcpy(saveLics, cur.compLic);
cur.tmpLics = strtok_r(saveLicsPtr, ",", &saveptr);
cur.licenseList[numLics] = cur.tmpLics;
numLics++;
saveLicsPtr = NULL;
while (cur.tmpLics)
{
cur.tmpLics = strtok_r(saveLicsPtr, ",", &saveptr);
if (cur.tmpLics == NULL)
{
break;
}
cur.licenseList[numLics] = cur.tmpLics;
numLics++;
}
cur.licenseList[numLics] = NULL;
numLics++;
/*
int i;
for(i=0; i<numLics; i++){
printf("cur.licenseList[%d] is:%s\n",i,cur.licenseList[i]);
}
printf("parseLicenseList: returning\n");
*/
return;
} /* parseLicenseList */
/**
* \brief Print nomos usage help
* \param Name Path to nomos binary
*/
FUNCTION void Usage(char *Name)
{
printf("Usage: %s [options] [file [file [...]]\n", Name);
printf(" -h :: help (print this message), then exit.\n");
printf(" -i :: initialize the database, then exit.\n");
printf(" -c :: specify the directory for the system configuration.\n");
printf(" -l :: print full file path (command line only).\n");
printf(" -v :: verbose (-vv = more verbose)\n");
printf(" -J :: output in JSON\n");
printf(" -S :: print Highlightinfo to stdout \n");
printf(" file :: if files are listed, print the licenses detected within them.\n");
printf(" no file :: process data from the scheduler.\n");
printf(" -V :: print the version info, then exit.\n");
printf(" -d :: specify a directory to scan.\n");
printf(" -n :: spaw n - 1 child processes to run, there will be n running processes(the parent and n - 1 children). \n the default n is 2(when n is less than 2 or not setting, will be changed to 2) when -d is specified.\n");
} /* Usage() */
/**
* \brief Close connections and exit
*
* The function closes DB and scheduler connections and calls exit() with the
* return code passed
* \param exitval Return code to pass to exit()
*/
FUNCTION void Bail(int exitval)
{
#ifdef PROC_TRACE
traceFunc("== Bail(%d)\n", exitval);
#endif /* PROC_TRACE */
#if defined(MEMORY_TRACING) && defined(MEM_ACCT)
if (exitval)
{
memCacheDump("Mem-cache @ Bail() time:");
}
#endif /* MEMORY_TRACING && MEM_ACCT */
/* close database and scheduler connections */
if (gl.pgConn) {
fo_dbManager_free(gl.dbManager);
PQfinish(gl.pgConn);
}
fo_scheduler_disconnect(exitval);
exit(exitval);
}
/**
* \brief Check if an CLI option is set
* \param val Binary position to check
* \return > 0 if it is set, 0 otherwise
*/
FUNCTION int optionIsSet(int val)
{
#ifdef PROC_TRACE
traceFunc("== optionIsSet(%x)\n", val);
#endif /* PROC_TRACE */
return (gl.progOpts & val);
} /* optionIsSet */
/**
\brief Initialize the lists: regular-files list cur.regfList and buffer-offset
list cur.offList.
\todo CDB - Could probably roll this back into the main processing
loop or just put in a generic init func that initializes *all*
the lists.
\callgraph
*/
FUNCTION void getFileLists(char *dirpath)
{
#ifdef PROC_TRACE
traceFunc("== getFileLists(%s)\n", dirpath);
#endif /* PROC_TRACE */
/* listInit(&gl.sarchList, 0, "source-archives list & md5sum map"); */
listInit(&cur.regfList, 0, "regular-files list");
listInit(&cur.offList, 0, "buffer-offset list");
#ifdef FLAG_NO_COPYRIGHT
listInit(&gl.nocpyrtList, 0, "no-copyright list");
#endif /* FLAG_NO_COPYRIGHT */
listGetItem(&cur.regfList, cur.targetFile);
return;
} /* getFileLists */
/**
* \brief insert rf_fk, agent_fk and pfile_fk into license_file table
*
* @param rfPK the reference file foreign key
*
* \returns The primary key for the inserted entry (or Negative value on error)
*
* \callgraph
*/
FUNCTION long updateLicenseFile(long rfPk)
{
PGresult *result;
if (rfPk <= 0)
{
return (-2);
}
/* If files are coming from command line instead of fossology repo,
then there are no pfiles. So don't update the db
*/
if (cur.cliMode == 1)
return (-1);
result = fo_dbManager_ExecPrepared(
fo_dbManager_PrepareStamement(
gl.dbManager,
"updateLicenseFile",
"INSERT INTO license_file(rf_fk, agent_fk, pfile_fk) VALUES($1, $2, $3) RETURNING fl_pk",
long, int, long
),
rfPk, gl.agentPk, cur.pFileFk
);
if (result) {
long licenseFileId = -1;
if (PQntuples(result) > 0) {
licenseFileId = atol(PQgetvalue(result, 0, 0));
}
PQclear(result);
return (licenseFileId);
} else {
return (-1);
}
} /* updateLicenseFile */
/**
* \brief Return the highlight type (K|L|0) for a given index
* @param index Index to convert
* @return K if index is between keyword length,\n
* L if index is larger than keyword length,\n
* 0 otherwise
*/
FUNCTION char convertIndexToHighlightType(int index)
{
char type;
if ((index >= _KW_first) && (index <= _KW_last))
type = 'K';
else if (index > _KW_last)
type = 'L';
else type = '0';
return type;
}
/**
* \brief insert rf_fk, agent_fk, offset, len and type into highlight table
*
* @param pcroot The root of hash table
*
* \returns boolean (True or False)
*
* \callgraph
*/
FUNCTION int updateLicenseHighlighting(cacheroot_t *pcroot){
/* If files are coming from command line instead of fossology repo,
then there are no pfiles. So don't update the db
Also if we specifically do not want highlight information in the
database skip this function
*/
if(cur.cliMode == 1 || optionIsSet(OPTS_NO_HIGHLIGHTINFO ) ){
return (TRUE);
}
PGresult *result;
#ifdef GLOBAL_DEBUG
printf("%s %s %i \n", cur.filePath,cur.compLic , cur.theMatches->len);
#endif
// This speeds up the writing to the database and ensures that we have either full highlight information or none
PGresult* begin1 = PQexec(gl.pgConn, "BEGIN");
PQclear(begin1);
fo_dbManager_PreparedStatement* preparedKeywords;
if(cur.keywordPositions->len > 0 ) {
preparedKeywords = fo_dbManager_PrepareStamement(
gl.dbManager,
"updateLicenseHighlighting:keyword",
"INSERT INTO highlight_keyword (pfile_fk, start, len) VALUES($1, $2, $3)",
long, int, int
);
}
int i;
for (i = 0; i < cur.keywordPositions->len; ++i)
{
MatchPositionAndType* ourMatchv = getMatchfromHighlightInfo(cur.keywordPositions, i);
result = fo_dbManager_ExecPrepared(
preparedKeywords,
cur.pFileFk, ourMatchv->start, ourMatchv->end - ourMatchv->start);
if (result)
{
PQclear(result);
}
}
PGresult* commit1 = PQexec(gl.pgConn, "COMMIT");
PQclear(commit1);
PGresult* begin2 =PQexec(gl.pgConn, "BEGIN");
PQclear(begin2);
fo_dbManager_PreparedStatement* preparedLicenses;
if(cur.theMatches->len > 0 ) {
preparedLicenses=fo_dbManager_PrepareStamement(
gl.dbManager,
"updateLicenseHighlighting",
"INSERT INTO highlight (fl_fk, start, len, type) VALUES($1, $2, $3,'L')",
long, int, int
);
}
for (i = 0; i < cur.theMatches->len; ++i)
{
LicenceAndMatchPositions* ourLicence = getLicenceAndMatchPositions(cur.theMatches, i);
int j;
for (j = 0; j < ourLicence->matchPositions->len; ++j)
{
MatchPositionAndType* ourMatchv = getMatchfromHighlightInfo(ourLicence->matchPositions, j);
if(ourLicence->licenseFileId == -1) {
//! the license File ID was never set and we should not insert it in the database
continue;
}
result = fo_dbManager_ExecPrepared(
preparedLicenses,
ourLicence->licenseFileId,
ourMatchv->start, ourMatchv->end - ourMatchv->start
);
if (result == NULL)
{
return (FALSE);
} else {
PQclear(result);
}
}
}
PGresult* commit2 = PQexec(gl.pgConn, "COMMIT");
PQclear(commit2);
return (TRUE);
} /* updateLicenseHighlighting */
/**
* \brief process a single file
* \param fileToScan File path
* \callgraph
*/
FUNCTION void processFile(char *fileToScan)
{
char *pathcopy;
#ifdef PROC_TRACE
traceFunc("== processFile(%s)\n", fileToScan);
#endif /* PROC_TRACE */
/* printf(" LOG: nomos scanning file %s.\n", fileToScan); DEBUG */
(void) strcpy(cur.cwd, gl.initwd);
strcpy(cur.filePath, fileToScan);
pathcopy = g_strdup(fileToScan);
strcpy(cur.targetDir, dirname(pathcopy));
g_free(pathcopy);
strcpy(cur.targetFile, fileToScan);
cur.targetLen = strlen(cur.targetDir);
if (!isFILE(fileToScan))
{
LOG_FATAL("\"%s\" is not a plain file", fileToScan)
Bail(-__LINE__);
}
getFileLists(cur.targetDir);
listInit(&cur.fLicFoundMap, 0, "file-license-found map");
listInit(&cur.parseList, 0, "license-components list");
listInit(&cur.lList, 0, "license-list");
processRawSource();
/* freeAndClearScan(&cur); */
} /* Process File */
/**
* \brief Set the license file id to the highlights
* \param licenseFileId License id
* \param licenseName License name
*/
void setLicenseFileIdInHiglightArray(long licenseFileId, char* licenseName){
int i;
for (i = 0; i < cur.theMatches->len; ++i) {
LicenceAndMatchPositions* ourLicence = getLicenceAndMatchPositions(cur.theMatches, i);
if (strcmp(licenseName, ourLicence->licenceName) == 0)
ourLicence->licenseFileId = licenseFileId;
}
} /* setLicenseFileIdInHiglightArray */
/**
* \brief Add a license to hash table, license table and highlight array
* \param licenseName License name
* \param pcroot Hash table root
* \return True if license is inserted in DB, False otherwise
*/
int updateLicenseFileAndHighlightArray(char* licenseName, cacheroot_t* pcroot) {
long rf_pk = get_rfpk(pcroot, licenseName);
long licenseFileId = updateLicenseFile(rf_pk);
if (licenseFileId > 0) {
setLicenseFileIdInHiglightArray(licenseFileId, licenseName);
return (true);
} else {
return (false);
}
}
/**
\brief Write out the information about the scan to the FOSSology database.
curScan is passed as an arg even though it's available as a global,
in order to facilitate future modularization of the code.
\returns 0 if successful, -1 if not.
\callgraph
*/
FUNCTION int recordScanToDB(cacheroot_t *pcroot, struct curScan *scanRecord)
{
char *noneFound;
int numLicenses;
#ifdef SIMULATESCHED
/* BOBG: This allows a developer to simulate the scheduler
with a load file for testing/debugging, without updating the
database. Like:
cat myloadfile | ./nomos
myloadfile is same as what scheduler sends:
pfile_pk=311667 pfilename=9A96127E7D3B2812B50BF7732A2D0FF685EF6D6A.78073D1CA7B4171F8AFEA1497E4C6B33.183
pfile_pk=311727 pfilename=B7F5EED9ECB679EE0F980599B7AA89DCF8FA86BD.72B00E1B419D2C83D1050C66FA371244.368
etc.
*/
printf("%s\n",scanRecord->compLic);
return(0);
#endif
noneFound = strstr(scanRecord->compLic, LS_NONE);
if (noneFound != NULL)
{
if (!updateLicenseFileAndHighlightArray("No_license_found", pcroot))
return (-1);
return (0);
}
/* we have one or more license names, parse them */
parseLicenseList();
/* loop through the found license names */
for (numLicenses = 0; cur.licenseList[numLicenses] != NULL; numLicenses++)
{
if (!updateLicenseFileAndHighlightArray(cur.licenseList[numLicenses], pcroot))
return (-1);
}
if (updateLicenseHighlighting(pcroot) == FALSE)
{
printf("Failure in update of highlight table \n");
}
return (0);
} /* recordScanToDb */
/**
* \brief Get the MatchPositionAndType for a given index in highlight array
* \param in Highlight array
* \param index Index to fetch
* \return Match position and type
*/
FUNCTION inline MatchPositionAndType* getMatchfromHighlightInfo(GArray* in,
int index)
{
return &g_array_index(in, MatchPositionAndType, index);
}
/**
* \brief Get the LicenceAndMatchPositions for a given index in match array
* \param in Match array
* \param index Index to fetch
* \return License and match position
*/
FUNCTION inline LicenceAndMatchPositions* getLicenceAndMatchPositions(
GArray* in, int index)
{
return &g_array_index(in, LicenceAndMatchPositions, index);
}
/**
* \brief Initialize the scanner
*
* Creates a new index list, match list, keyword position list, doctored buffer
* and license index
* \param cur Current scanner
*/
FUNCTION void initializeCurScan(struct curScan* cur)
{
cur->indexList = g_array_new(FALSE, FALSE, sizeof(int));
cur->theMatches = g_array_new(FALSE, FALSE, sizeof(LicenceAndMatchPositions));
cur->keywordPositions = g_array_new(FALSE, FALSE, sizeof(MatchPositionAndType));
cur->docBufferPositionsAndOffsets = g_array_new(FALSE, FALSE, sizeof(pairPosOff));
cur->currentLicenceIndex=-1;
}
/**
* \brief Clean-up all the per scan data structures, freeing any old data.
* \param thisScan Scanner to clear
* \callgraph
*/
FUNCTION void freeAndClearScan(struct curScan *thisScan)
{
/*
Clear lists
*/
listClear(&thisScan->regfList, DEALLOC_LIST);
listClear(&thisScan->offList, DEALLOC_LIST);
listClear(&thisScan->fLicFoundMap, DEALLOC_LIST);
listClear(&thisScan->parseList, DEALLOC_LIST);
listClear(&thisScan->lList, DEALLOC_LIST);
g_array_free(thisScan->indexList,TRUE);
cleanTheMatches(thisScan->theMatches);
g_array_free(thisScan->keywordPositions, TRUE);
g_array_free(thisScan->docBufferPositionsAndOffsets, TRUE);
/* remove keys, data and hash table */
hdestroy();
}
/**
* \brief Cleans the match array and free the memory
* \param theMatches The matches list
*/
FUNCTION inline void cleanTheMatches(GArray* theMatches){
int i;
for(i=0; i< theMatches->len; ++i) {
cleanLicenceAndMatchPositions(getLicenceAndMatchPositions (theMatches , i));
}
g_array_free( theMatches, TRUE);
}
/**
* \brief Cleans the license and match positions object and free the memory
* \param in The matches object
*/
FUNCTION inline void cleanLicenceAndMatchPositions( LicenceAndMatchPositions* in )
{
if(in->licenceName) g_free(in->licenceName);
g_array_free(in->matchPositions, TRUE);
g_array_free(in->indexList,TRUE);
}
/**
* \brief Add a license to the matches array
* \param[in,out] theMatches The matches array
* \param[in] licenceName License to be added
*/
FUNCTION inline void addLicence(GArray* theMatches, char* licenceName ) {
LicenceAndMatchPositions newMatch;