FOSSology  4.5.1
Open Source License Compliance by Open Source Software
nomos_utils.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2006-2014 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
7 #ifndef _GNU_SOURCE
8 #define _GNU_SOURCE
9 #endif /* not defined _GNU_SOURCE */
10 
11 #include "nomos_utils.h"
12 #include "nomos.h"
13 
14 extern int should_connect_to_db; /* Global variable to control DB connection */
15 
16 #define FUNCTION
17 
23 sem_t* mutexJson;
24 gboolean* printcomma;
25 char saveLics[myBUFSIZ];
26 
36 FUNCTION long add2license_ref(char *licenseName)
37 {
38 
39  PGresult *result;
40  char query[myBUFSIZ];
41  char insert[myBUFSIZ];
42  char escLicName[myBUFSIZ];
43  char *specialLicenseText;
44  long rf_pk;
45 
46  int len;
47  int error;
48  int numRows;
49 
50  // escape the name
51  len = strlen(licenseName);
52  PQescapeStringConn(gl.pgConn, escLicName, licenseName, len, &error);
53  if (error)
54  LOG_WARNING("Does license name %s have multibyte encoding?", licenseName)
55 
56  /* verify the license is not already in the table */
57  snprintf(query, myBUFSIZ - 1, "SELECT rf_pk FROM " LICENSE_REF_TABLE " where rf_shortname='%s'", escLicName);
58  result = PQexec(gl.pgConn, query);
59  if (fo_checkPQresult(gl.pgConn, result, query, __FILE__, __LINE__))
60  return 0;
61  numRows = PQntuples(result);
62  if (numRows)
63  {
64  rf_pk = atol(PQgetvalue(result, 0, 0));
65  PQclear(result);
66  return rf_pk;
67  }
68  PQclear(result);
69 
70  /* Insert the new license */
71  specialLicenseText = "License by Nomos.";
72 
73  snprintf(insert, myBUFSIZ - 1, "insert into license_ref(rf_shortname, rf_text, rf_detector_type) values('%s', '%s', 2)", escLicName,
74  specialLicenseText);
75  result = PQexec(gl.pgConn, insert);
76  // ignore duplicate constraint failure (23505), report others
77  if ((result == 0)
78  || ((PQresultStatus(result) != PGRES_COMMAND_OK)
79  && (strncmp(PG_ERRCODE_UNIQUE_VIOLATION, PQresultErrorField(result, PG_DIAG_SQLSTATE), 5))))
80  {
81  printf("ERROR: %s(%d): Nomos failed to add a new license. %s/n: %s/n",
82  __FILE__, __LINE__, PQresultErrorMessage(result), insert);
83  PQclear(result);
84  return (0);
85  }
86  PQclear(result);
87 
88  /* retrieve the new rf_pk */
89  result = PQexec(gl.pgConn, query);
90  if (fo_checkPQresult(gl.pgConn, result, query, __FILE__, __LINE__))
91  return 0;
92  numRows = PQntuples(result);
93  if (numRows)
94  rf_pk = atol(PQgetvalue(result, 0, 0));
95  else
96  {
97  printf("ERROR: %s:%s:%d Just inserted value is missing. On: %s", __FILE__, "add2license_ref()", __LINE__, query);
98  PQclear(result);
99  return (0);
100  }
101  PQclear(result);
102 
103  return (rf_pk);
104 }
105 
115 FUNCTION long lrcache_hash(cacheroot_t *pcroot, char *rf_shortname)
116 {
117  long hashval = 0;
118  int len, i;
119 
120  /* use the first sizeof(long) bytes for the hash value */
121  len = (strlen(rf_shortname) < sizeof(long)) ? strlen(rf_shortname) : sizeof(long);
122  for (i = 0; i < len; i++)
123  hashval += rf_shortname[i] << 8 * i;
124  hashval = hashval % pcroot->maxnodes;
125  return hashval;
126 }
127 
135 FUNCTION void lrcache_print(cacheroot_t *pcroot)
136 {
137  cachenode_t *pcnode;
138  long hashval = 0;
139  int i;
140 
141  pcnode = pcroot->nodes;
142  for (i = 0; i < pcroot->maxnodes; i++)
143  {
144  if (pcnode->rf_pk != 0L)
145  {
146  hashval = lrcache_hash(pcroot, pcnode->rf_shortname);
147  printf("%ld, %ld, %s\n", hashval, pcnode->rf_pk, pcnode->rf_shortname);
148  }
149  pcnode++;
150  }
151 }
152 
160 FUNCTION void lrcache_free(cacheroot_t *pcroot)
161 {
162  cachenode_t *pcnode;
163  int i;
164 
165  pcnode = pcroot->nodes;
166  for (i = 0; i < pcroot->maxnodes; i++)
167  {
168  if (pcnode->rf_pk != 0L)
169  {
170  free(pcnode->rf_shortname);
171  }
172  pcnode++;
173  }
174  free(pcroot->nodes);
175 }
176 
187 FUNCTION int lrcache_add(cacheroot_t *pcroot, long rf_pk, char *rf_shortname)
188 {
189  cachenode_t *pcnode;
190  long hashval = 0;
191  int i;
192  int noden;
193 
194  hashval = lrcache_hash(pcroot, rf_shortname);
195 
196  noden = hashval;
197  for (i = 0; i < pcroot->maxnodes; i++)
198  {
199  noden = (hashval + i) & (pcroot->maxnodes - 1);
200 
201  pcnode = pcroot->nodes + noden;
202  if (!pcnode->rf_pk)
203  {
204  pcnode->rf_shortname = strdup(rf_shortname);
205  pcnode->rf_pk = rf_pk;
206  break;
207  }
208  }
209  if (i < pcroot->maxnodes)
210  return 0;
211 
212  return -1; /* no space */
213 }
214 
224 FUNCTION long lrcache_lookup(cacheroot_t *pcroot, char *rf_shortname)
225 {
226  cachenode_t *pcnode;
227  long hashval = 0;
228  int i;
229  int noden;
230 
231  hashval = lrcache_hash(pcroot, rf_shortname);
232 
233  noden = hashval;
234  for (i = 0; i < pcroot->maxnodes; i++)
235  {
236  noden = (hashval + i) & (pcroot->maxnodes - 1);
237 
238  pcnode = pcroot->nodes + noden;
239  if (!pcnode->rf_pk)
240  return 0;
241  if (strcmp(pcnode->rf_shortname, rf_shortname) == 0)
242  {
243  return pcnode->rf_pk;
244  }
245  }
246 
247  return 0; /* not found */
248 }
249 
262 FUNCTION int initLicRefCache(cacheroot_t *pcroot)
263 {
264 
265  PGresult *result;
266  char query[myBUFSIZ];
267  int row;
268  int numLics;
269 
270  if (!pcroot)
271  return 0;
272 
273  sprintf(query, "SELECT rf_pk, rf_shortname FROM " LICENSE_REF_TABLE " where rf_detector_type=2");
274  result = PQexec(gl.pgConn, query);
275  if (fo_checkPQresult(gl.pgConn, result, query, __FILE__, __LINE__))
276  return 0;
277 
278  numLics = PQntuples(result);
279  /* populate the cache */
280  for (row = 0; row < numLics; row++)
281  {
282  lrcache_add(pcroot, atol(PQgetvalue(result, row, 0)), PQgetvalue(result, row, 1));
283  }
284 
285  PQclear(result);
286 
287  return (1);
288 } /* initLicRefCache */
289 
302 FUNCTION long get_rfpk(cacheroot_t *pcroot, char *rf_shortname)
303 {
304  long rf_pk;
305  size_t len;
306 
307  if ((len = strlen(rf_shortname)) == 0)
308  {
309  printf("ERROR! Nomos.c get_rfpk() passed empty name");
310  return (0);
311  }
312 
313  /* is this in the cache? */
314  rf_pk = lrcache_lookup(pcroot, rf_shortname);
315  if (rf_pk)
316  return rf_pk;
317 
318  /* shortname was not found, so add it */
319  /* add to the license_ref table */
320  rf_pk = add2license_ref(rf_shortname);
321 
322  /* add to the cache */
323  lrcache_add(pcroot, rf_pk, rf_shortname);
324 
325  return (rf_pk);
326 } /* get_rfpk */
327 
335 FUNCTION char *getFieldValue(char *inStr, char *field, int fieldMax,
336  char *value, int valueMax, char separator)
337 {
338  int s;
339  int f;
340  int v;
341  int gotQuote;
342 
343 #ifdef PROC_TRACE
344  traceFunc("== getFieldValue(inStr= %s fieldMax= %d separator= '%c'\n",
345  inStr, fieldMax, separator);
346 #endif /* PROC_TRACE */
347 
348  memset(field, 0, fieldMax);
349  memset(value, 0, valueMax);
350 
351  /* Skip initial spaces */
352  while (isspace(inStr[0]))
353  {
354  inStr++;
355  }
356 
357  if (inStr[0] == '\0')
358  {
359  return (NULL);
360  }
361  f = 0;
362  v = 0;
363 
364  /* Skip to end of field name */
365  for (s = 0; (inStr[s] != '\0') && !isspace(inStr[s]) && (inStr[s] != '='); s++)
366  {
367  field[f++] = inStr[s];
368  }
369 
370  /* Skip spaces after field name */
371  while (isspace(inStr[s]))
372  {
373  s++;
374  }
375  /* If it is not a field, then just return it. */
376  if (inStr[s] != separator)
377  {
378  return (inStr + s);
379  }
380  if (inStr[s] == '\0')
381  {
382  return (NULL);
383  }
384  /* Skip '=' */
385  s++;
386 
387  /* Skip spaces after '=' */
388  while (isspace(inStr[s]))
389  {
390  s++;
391  }
392  if (inStr[s] == '\0')
393  {
394  return (NULL);
395  }
396 
397  gotQuote = '\0';
398  if ((inStr[s] == '\'') || (inStr[s] == '"'))
399  {
400  gotQuote = inStr[s];
401  s++; /* skip quote */
402  if (inStr[s] == '\0')
403  {
404  return (NULL);
405  }
406  }
407 
408  if (gotQuote)
409  {
410  for (; (inStr[s] != '\0') && (inStr[s] != gotQuote); s++)
411  {
412  if (inStr[s] == '\\')
413  {
414  value[v++] = inStr[++s];
415  }
416  else
417  {
418  value[v++] = inStr[s];
419  }
420  }
421  }
422  else
423  {
424  /* if it gets here, then there is no quote */
425  for (; (inStr[s] != '\0') && !isspace(inStr[s]); s++)
426  {
427  if (inStr[s] == '\\')
428  {
429  value[v++] = inStr[++s];
430  }
431  else
432  {
433  value[v++] = inStr[s];
434  }
435  }
436  }
437  /* Skip spaces */
438  while (isspace(inStr[s]))
439  {
440  s++;
441  }
442 
443  return (inStr + s);
444 } /* getFieldValue */
445 
452 FUNCTION void parseLicenseList()
453 {
454 
455  int numLics = 0;
456 
457  /* char saveLics[myBUFSIZ]; */
458  char *saveptr = 0; /* used for strtok_r */
459  char *saveLicsPtr;
460 
461  if ((strlen(cur.compLic)) == 0)
462  {
463  return;
464  }
465 
466  /* check for a single name FIX THIS!*/
467  if (strstr(cur.compLic, ",") == NULL)
468  {
469  cur.licenseList[0] = cur.compLic;
470  cur.licenseList[1] = NULL;
471  return;
472  }
473 
474  saveLicsPtr = strcpy(saveLics, cur.compLic);
475 
476  cur.tmpLics = strtok_r(saveLicsPtr, ",", &saveptr);
477 
478  cur.licenseList[numLics] = cur.tmpLics;
479  numLics++;
480 
481  saveLicsPtr = NULL;
482  while (cur.tmpLics)
483  {
484  cur.tmpLics = strtok_r(saveLicsPtr, ",", &saveptr);
485  if (cur.tmpLics == NULL)
486  {
487  break;
488  }
489  cur.licenseList[numLics] = cur.tmpLics;
490  numLics++;
491  }
492  cur.licenseList[numLics] = NULL;
493  numLics++;
494 
495  /*
496  int i;
497  for(i=0; i<numLics; i++){
498  printf("cur.licenseList[%d] is:%s\n",i,cur.licenseList[i]);
499  }
500 
501  printf("parseLicenseList: returning\n");
502  */
503 
504  return;
505 } /* parseLicenseList */
506 
511 FUNCTION void Usage(char *Name)
512 {
513  /* Disable database connection when showing usage */
514  should_connect_to_db = 0;
515  printf("Usage: %s [options] [file [file [...]]\n", Name);
516  printf(" -h :: help (print this message), then exit.\n");
517  printf(" -i :: initialize the database, then exit.\n");
518  printf(" -c :: specify the directory for the system configuration.\n");
519  printf(" -l :: print full file path (command line only).\n");
520  printf(" -v :: verbose (-vv = more verbose)\n");
521  printf(" -J :: output in JSON\n");
522  printf(" -S :: print Highlightinfo to stdout \n");
523  printf(" file :: if files are listed, print the licenses detected within them.\n");
524  printf(" no file :: process data from the scheduler.\n");
525  printf(" -V :: print the version info, then exit.\n");
526  printf(" -d :: specify a directory to scan.\n");
527  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");
528 } /* Usage() */
529 
537 FUNCTION void Bail(int exitval)
538 {
539 #ifdef PROC_TRACE
540  traceFunc("== Bail(%d)\n", exitval);
541 #endif /* PROC_TRACE */
542 
543 #if defined(MEMORY_TRACING) && defined(MEM_ACCT)
544  if (exitval)
545  {
546  memCacheDump("Mem-cache @ Bail() time:");
547  }
548 #endif /* MEMORY_TRACING && MEM_ACCT */
549 
550  /* close database and scheduler connections */
551  if (gl.pgConn) {
553  PQfinish(gl.pgConn);
554  }
555  fo_scheduler_disconnect(exitval);
556  exit(exitval);
557 }
558 
564 FUNCTION int optionIsSet(int val)
565 {
566 #ifdef PROC_TRACE
567  traceFunc("== optionIsSet(%x)\n", val);
568 #endif /* PROC_TRACE */
569 
570  return (gl.progOpts & val);
571 } /* optionIsSet */
572 
584 FUNCTION void getFileLists(char *dirpath)
585 {
586 #ifdef PROC_TRACE
587  traceFunc("== getFileLists(%s)\n", dirpath);
588 #endif /* PROC_TRACE */
589 
590  /* listInit(&gl.sarchList, 0, "source-archives list & md5sum map"); */
591  listInit(&cur.regfList, 0, "regular-files list");
592  listInit(&cur.offList, 0, "buffer-offset list");
593 #ifdef FLAG_NO_COPYRIGHT
594  listInit(&gl.nocpyrtList, 0, "no-copyright list");
595 #endif /* FLAG_NO_COPYRIGHT */
596 
597  listGetItem(&cur.regfList, cur.targetFile);
598  return;
599 } /* getFileLists */
600 
610 FUNCTION long updateLicenseFile(long rfPk)
611 {
612 
613  PGresult *result;
614 
615  if (rfPk <= 0)
616  {
617  return (-2);
618  }
619 
620  /* If files are coming from command line instead of fossology repo,
621  then there are no pfiles. So don't update the db
622  */
623  if (cur.cliMode == 1)
624  return (-1);
625 
626  result = fo_dbManager_ExecPrepared(
627  fo_dbManager_PrepareStamement(
628  gl.dbManager,
629  "updateLicenseFile",
630  "INSERT INTO license_file(rf_fk, agent_fk, pfile_fk) VALUES($1, $2, $3) RETURNING fl_pk",
631  long, int, long
632  ),
633  rfPk, gl.agentPk, cur.pFileFk
634  );
635 
636  if (result) {
637  long licenseFileId = -1;
638  if (PQntuples(result) > 0) {
639  licenseFileId = atol(PQgetvalue(result, 0, 0));
640  }
641 
642  PQclear(result);
643  return (licenseFileId);
644  } else {
645  return (-1);
646  }
647 } /* updateLicenseFile */
648 
656 FUNCTION char convertIndexToHighlightType(int index)
657 {
658 
659  char type;
660 
661  if ((index >= _KW_first) && (index <= _KW_last))
662  type = 'K';
663  else if (index > _KW_last)
664  type = 'L';
665  else type = '0';
666 
667  return type;
668 
669 }
670 
681 
682  /* If files are coming from command line instead of fossology repo,
683  then there are no pfiles. So don't update the db
684 
685  Also if we specifically do not want highlight information in the
686  database skip this function
687  */
688  if(cur.cliMode == 1 || optionIsSet(OPTS_NO_HIGHLIGHTINFO ) ){
689  return (TRUE);
690  }
691  PGresult *result;
692 
693 #ifdef GLOBAL_DEBUG
694  printf("%s %s %i \n", cur.filePath,cur.compLic , cur.theMatches->len);
695 #endif
696 
697  // This speeds up the writing to the database and ensures that we have either full highlight information or none
698  PGresult* begin1 = PQexec(gl.pgConn, "BEGIN");
699  PQclear(begin1);
700 
701  fo_dbManager_PreparedStatement* preparedKeywords;
702  if(cur.keywordPositions->len > 0 ) {
703  preparedKeywords = fo_dbManager_PrepareStamement(
704  gl.dbManager,
705  "updateLicenseHighlighting:keyword",
706  "INSERT INTO highlight_keyword (pfile_fk, start, len) VALUES($1, $2, $3)",
707  long, int, int
708  );
709  }
710  int i;
711  for (i = 0; i < cur.keywordPositions->len; ++i)
712  {
714 #pragma GCC diagnostic push
715 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
716  // If uninitialized, the loop will not start
717  result = fo_dbManager_ExecPrepared(
718  preparedKeywords,
719  cur.pFileFk, ourMatchv->start, ourMatchv->end - ourMatchv->start);
720 #pragma GCC diagnostic pop
721  if (result)
722  {
723  PQclear(result);
724  }
725  }
726  PGresult* commit1 = PQexec(gl.pgConn, "COMMIT");
727  PQclear(commit1);
728 
729  PGresult* begin2 =PQexec(gl.pgConn, "BEGIN");
730  PQclear(begin2);
731  fo_dbManager_PreparedStatement* preparedLicenses;
732 
733  if(cur.theMatches->len > 0 ) {
734  preparedLicenses=fo_dbManager_PrepareStamement(
735  gl.dbManager,
736  "updateLicenseHighlighting",
737  "INSERT INTO highlight (fl_fk, start, len, type) VALUES($1, $2, $3,'L')",
738  long, int, int
739  );
740  }
741 
742  for (i = 0; i < cur.theMatches->len; ++i)
743  {
745 
746  int j;
747  for (j = 0; j < ourLicence->matchPositions->len; ++j)
748  {
750  if(ourLicence->licenseFileId == -1) {
752  continue;
753  }
754 #pragma GCC diagnostic push
755 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
756  // If uninitialized, the loop will not start
757  result = fo_dbManager_ExecPrepared(
758  preparedLicenses,
759  ourLicence->licenseFileId,
760  ourMatchv->start, ourMatchv->end - ourMatchv->start
761  );
762 #pragma GCC diagnostic pop
763  if (result == NULL)
764  {
765  return (FALSE);
766  } else {
767  PQclear(result);
768  }
769  }
770  }
771 
772  PGresult* commit2 = PQexec(gl.pgConn, "COMMIT");
773  PQclear(commit2);
774  return (TRUE);
775 } /* updateLicenseHighlighting */
776 
777 
783 FUNCTION void processFile(char *fileToScan)
784 {
785 
786  char *pathcopy;
787 #ifdef PROC_TRACE
788  traceFunc("== processFile(%s)\n", fileToScan);
789 #endif /* PROC_TRACE */
790 
791  /* printf(" LOG: nomos scanning file %s.\n", fileToScan); DEBUG */
792 
793  (void) strcpy(cur.cwd, gl.initwd);
794 
795  strcpy(cur.filePath, fileToScan);
796  pathcopy = g_strdup(fileToScan);
797  strcpy(cur.targetDir, dirname(pathcopy));
798  g_free(pathcopy);
799  strcpy(cur.targetFile, fileToScan);
800  cur.targetLen = strlen(cur.targetDir);
801 
802  if (!isFILE(fileToScan))
803  {
804  LOG_FATAL("\"%s\" is not a plain file", fileToScan)
805  Bail(-__LINE__);
806  }
807 
808  getFileLists(cur.targetDir);
809  listInit(&cur.fLicFoundMap, 0, "file-license-found map");
810  listInit(&cur.parseList, 0, "license-components list");
811  listInit(&cur.lList, 0, "license-list");
812 
814 
815  /* freeAndClearScan(&cur); */
816 } /* Process File */
817 
823 void setLicenseFileIdInHiglightArray(long licenseFileId, char* licenseName){
824  int i;
825  for (i = 0; i < cur.theMatches->len; ++i) {
827  if (strcmp(licenseName, ourLicence->licenceName) == 0)
828  ourLicence->licenseFileId = licenseFileId;
829  }
830 } /* setLicenseFileIdInHiglightArray */
831 
838 int updateLicenseFileAndHighlightArray(char* licenseName, cacheroot_t* pcroot) {
839  long rf_pk = get_rfpk(pcroot, licenseName);
840  long licenseFileId = updateLicenseFile(rf_pk);
841  if (licenseFileId > 0) {
842  setLicenseFileIdInHiglightArray(licenseFileId, licenseName);
843  return (true);
844  } else {
845  return (false);
846  }
847 }
848 
859 FUNCTION int recordScanToDB(cacheroot_t *pcroot, struct curScan *scanRecord)
860 {
861 
862  char *noneFound;
863  int numLicenses;
864 
865 #ifdef SIMULATESCHED
866  /* BOBG: This allows a developer to simulate the scheduler
867  with a load file for testing/debugging, without updating the
868  database. Like:
869  cat myloadfile | ./nomos
870  myloadfile is same as what scheduler sends:
871  pfile_pk=311667 pfilename=9A96127E7D3B2812B50BF7732A2D0FF685EF6D6A.78073D1CA7B4171F8AFEA1497E4C6B33.183
872  pfile_pk=311727 pfilename=B7F5EED9ECB679EE0F980599B7AA89DCF8FA86BD.72B00E1B419D2C83D1050C66FA371244.368
873  etc.
874  */
875  printf("%s\n",scanRecord->compLic);
876  return(0);
877 #endif
878 
879  noneFound = strstr(scanRecord->compLic, LS_NONE);
880  if (noneFound != NULL)
881  {
882  if (!updateLicenseFileAndHighlightArray("No_license_found", pcroot))
883  return (-1);
884  return (0);
885  }
886 
887  /* we have one or more license names, parse them */
889  /* loop through the found license names */
890  for (numLicenses = 0; cur.licenseList[numLicenses] != NULL; numLicenses++)
891  {
892  if (!updateLicenseFileAndHighlightArray(cur.licenseList[numLicenses], pcroot))
893  return (-1);
894  }
895 
896  if (updateLicenseHighlighting(pcroot) == FALSE)
897  {
898  printf("Failure in update of highlight table \n");
899  }
900 
901  return (0);
902 } /* recordScanToDb */
903 
911  int index)
912 {
913  return &g_array_index(in, MatchPositionAndType, index);
914 }
915 
923  GArray* in, int index)
924 {
925  return &g_array_index(in, LicenceAndMatchPositions, index);
926 }
927 
935 FUNCTION void initializeCurScan(struct curScan* cur)
936 {
937  cur->indexList = g_array_new(FALSE, FALSE, sizeof(int));
938  cur->theMatches = g_array_new(FALSE, FALSE, sizeof(LicenceAndMatchPositions));
939  cur->keywordPositions = g_array_new(FALSE, FALSE, sizeof(MatchPositionAndType));
940  cur->docBufferPositionsAndOffsets = g_array_new(FALSE, FALSE, sizeof(pairPosOff));
941  cur->currentLicenceIndex=-1;
942 }
943 
944 
950 FUNCTION void freeAndClearScan(struct curScan *thisScan)
951 {
952  /*
953  Clear lists
954  */
955  listClear(&thisScan->regfList, DEALLOC_LIST);
956  listClear(&thisScan->offList, DEALLOC_LIST);
957  listClear(&thisScan->fLicFoundMap, DEALLOC_LIST);
958  listClear(&thisScan->parseList, DEALLOC_LIST);
959  listClear(&thisScan->lList, DEALLOC_LIST);
960  g_array_free(thisScan->indexList,TRUE);
961  cleanTheMatches(thisScan->theMatches);
962  g_array_free(thisScan->keywordPositions, TRUE);
963  g_array_free(thisScan->docBufferPositionsAndOffsets, TRUE);
964 
965 
966  /* remove keys, data and hash table */
967  hdestroy();
968 
969 }
970 
975 FUNCTION inline void cleanTheMatches(GArray* theMatches){
976 
977  int i;
978  for(i=0; i< theMatches->len; ++i) {
980  }
981  g_array_free( theMatches, TRUE);
982 }
983 
989 {
990  if(in->licenceName) g_free(in->licenceName);
991  g_array_free(in->matchPositions, TRUE);
992  g_array_free(in->indexList,TRUE);
993 }
994 
1000 FUNCTION inline void addLicence(GArray* theMatches, char* licenceName ) {
1001  LicenceAndMatchPositions newMatch;
1002  newMatch.indexList = cur.indexList;
1003  cur.indexList=g_array_new(FALSE, FALSE, sizeof(int));
1004 
1006  newMatch.matchPositions = g_array_new(FALSE, FALSE, sizeof(MatchPositionAndType));
1007  newMatch.licenceName = g_strdup(licenceName);
1008  newMatch.licenseFileId = -1; //initial Value <- check if it was set
1009  g_array_append_val(theMatches , newMatch);
1010 }
1011 
1015 inline void cleanLicenceBuffer(){
1016  g_array_set_size(cur.indexList, 0);
1017 }
1018 
1024  if(cur.indexList->len>0)
1025  g_array_remove_index(cur.indexList, cur.indexList->len -1);
1026  return true;
1027 }
int s
The socket that the CLI will use to communicate.
Definition: fo_cli.c:37
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
Definition: libfossdb.c:170
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
item_t * listGetItem(list_t *l, char *s)
get an item from the itemlist. If the item is not in the itemlist, then add it to the itemlist.
Definition: list.c:246
void listInit(list_t *l, int size, char *label)
intialize a list, if the list is not empty, empty it (initialize it to zero's).
Definition: list.c:54
void listClear(list_t *l, int deallocFlag)
Destroy list_t.
Definition: list.c:106
void processRawSource()
Definition: process.c:113
PGresult * fo_dbManager_ExecPrepared(fo_dbManager_PreparedStatement *preparedStatement,...)
Execute a prepared statement.
Definition: standalone.c:37
void fo_dbManager_free(fo_dbManager *dbManager)
Un-allocate the memory from a DB manager.
Definition: standalone.c:35
int isFILE(char *pathname)
Check if an inode is a file.
Definition: util.c:1340
Nomos header file.
#define LS_NONE
Definition: nomos.h:215
FUNCTION void lrcache_print(cacheroot_t *pcroot)
Print the contents of the hash table.
Definition: nomos_utils.c:135
FUNCTION void freeAndClearScan(struct curScan *thisScan)
Clean-up all the per scan data structures, freeing any old data.
Definition: nomos_utils.c:950
FUNCTION void lrcache_free(cacheroot_t *pcroot)
free the hash table
Definition: nomos_utils.c:160
FUNCTION char * getFieldValue(char *inStr, char *field, int fieldMax, char *value, int valueMax, char separator)
Given a string that contains field='value' pairs, save the items.
Definition: nomos_utils.c:335
FUNCTION long lrcache_lookup(cacheroot_t *pcroot, char *rf_shortname)
lookup rf_pk in the license_ref cache rf_shortname is the key
Definition: nomos_utils.c:224
FUNCTION void Usage(char *Name)
Print nomos usage help.
Definition: nomos_utils.c:511
sem_t * mutexJson
Mutex to handle JSON writes.
Definition: nomos_utils.c:23
FUNCTION void processFile(char *fileToScan)
process a single file
Definition: nomos_utils.c:783
FUNCTION void cleanTheMatches(GArray *theMatches)
Cleans the match array and free the memory.
Definition: nomos_utils.c:975
FUNCTION void parseLicenseList()
parse the comma separated list of license names found
Definition: nomos_utils.c:452
FUNCTION long updateLicenseFile(long rfPk)
insert rf_fk, agent_fk and pfile_fk into license_file table
Definition: nomos_utils.c:610
FUNCTION void initializeCurScan(struct curScan *cur)
Initialize the scanner.
Definition: nomos_utils.c:935
FUNCTION int recordScanToDB(cacheroot_t *pcroot, struct curScan *scanRecord)
Write out the information about the scan to the FOSSology database.
Definition: nomos_utils.c:859
FUNCTION int lrcache_add(cacheroot_t *pcroot, long rf_pk, char *rf_shortname)
add a rf_shortname, rf_pk to the license_ref cache rf_shortname is the key
Definition: nomos_utils.c:187
void cleanLicenceBuffer()
Clean the license buffer.
Definition: nomos_utils.c:1015
FUNCTION void addLicence(GArray *theMatches, char *licenceName)
Add a license to the matches array.
Definition: nomos_utils.c:1000
FUNCTION MatchPositionAndType * getMatchfromHighlightInfo(GArray *in, int index)
Get the MatchPositionAndType for a given index in highlight array.
Definition: nomos_utils.c:910
FUNCTION void getFileLists(char *dirpath)
Initialize the lists: regular-files list cur.regfList and buffer-offset list cur.offList.
Definition: nomos_utils.c:584
FUNCTION LicenceAndMatchPositions * getLicenceAndMatchPositions(GArray *in, int index)
Get the LicenceAndMatchPositions for a given index in match array.
Definition: nomos_utils.c:922
gboolean * printcomma
True to print comma while printing JSON object.
Definition: nomos_utils.c:24
FUNCTION void cleanLicenceAndMatchPositions(LicenceAndMatchPositions *in)
Cleans the license and match positions object and free the memory.
Definition: nomos_utils.c:988
FUNCTION int initLicRefCache(cacheroot_t *pcroot)
build a cache the license ref db table.
Definition: nomos_utils.c:262
char saveLics[myBUFSIZ]
License string.
Definition: nomos_utils.c:25
int updateLicenseFileAndHighlightArray(char *licenseName, cacheroot_t *pcroot)
Add a license to hash table, license table and highlight array.
Definition: nomos_utils.c:838
FUNCTION int optionIsSet(int val)
Check if an CLI option is set.
Definition: nomos_utils.c:564
bool clearLastElementOfLicenceBuffer()
Remove the last element from license buffer.
Definition: nomos_utils.c:1023
void setLicenseFileIdInHiglightArray(long licenseFileId, char *licenseName)
Set the license file id to the highlights.
Definition: nomos_utils.c:823
FUNCTION long lrcache_hash(cacheroot_t *pcroot, char *rf_shortname)
calculate the hash of an rf_shortname rf_shortname is the key
Definition: nomos_utils.c:115
FUNCTION void Bail(int exitval)
Close connections and exit.
Definition: nomos_utils.c:537
FUNCTION char convertIndexToHighlightType(int index)
Return the highlight type (K|L|0) for a given index.
Definition: nomos_utils.c:656
FUNCTION long get_rfpk(cacheroot_t *pcroot, char *rf_shortname)
Get the rf_pk for rf_shortname.
Definition: nomos_utils.c:302
FUNCTION long add2license_ref(char *licenseName)
Add a new license to license_ref table.
Definition: nomos_utils.c:36
FUNCTION int updateLicenseHighlighting(cacheroot_t *pcroot)
insert rf_fk, agent_fk, offset, len and type into highlight table
Definition: nomos_utils.c:680
GArray * matchPositions
Match positions.
Definition: nomos.h:379
GArray * indexList
License indexes.
Definition: nomos.h:380
char * licenceName
License names.
Definition: nomos.h:381
int licenseFileId
PFile id.
Definition: nomos.h:382
int start
Start position of match.
Definition: nomos.h:370
int end
End position of match.
Definition: nomos.h:371
long rf_pk
License id from database.
Definition: liccache.h:32
char * rf_shortname
License shortname.
Definition: liccache.h:31
int maxnodes
No. of nodes in the list.
Definition: liccache.h:42
cachenode_t * nodes
Array of nodes.
Definition: liccache.h:43
Struct that tracks state related to current file being scanned.
Definition: nomos.h:391
GArray * indexList
Definition: nomos.h:416
char cwd[myBUFSIZ]
Definition: nomos.h:392
char * tmpLics
Definition: nomos.h:413
char filePath[myBUFSIZ]
Definition: nomos.h:395
long pFileFk
Definition: nomos.h:396
char targetDir[myBUFSIZ]
Definition: nomos.h:393
GArray * theMatches
Definition: nomos.h:417
GArray * keywordPositions
Definition: nomos.h:418
char compLic[myBUFSIZ]
Definition: nomos.h:409
char targetFile[myBUFSIZ]
Definition: nomos.h:394
char * licenseList[512]
Definition: nomos.h:414
int cliMode
Definition: nomos.h:412
PGconn * pgConn
DB Connection.
Definition: nomos.h:362
int agentPk
Agent id.
Definition: nomos.h:359
fo_dbManager * dbManager
FOSSology DB manager.
Definition: nomos.h:363
int progOpts
CLI options.
Definition: nomos.h:347
char initwd[myBUFSIZ]
CDB, would like to workaround/eliminate.
Definition: nomos.h:345