FOSSology  4.4.0
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 #define FUNCTION
15 
21 sem_t* mutexJson;
22 gboolean* printcomma;
23 char saveLics[myBUFSIZ];
24 
34 FUNCTION long add2license_ref(char *licenseName)
35 {
36 
37  PGresult *result;
38  char query[myBUFSIZ];
39  char insert[myBUFSIZ];
40  char escLicName[myBUFSIZ];
41  char *specialLicenseText;
42  long rf_pk;
43 
44  int len;
45  int error;
46  int numRows;
47 
48  // escape the name
49  len = strlen(licenseName);
50  PQescapeStringConn(gl.pgConn, escLicName, licenseName, len, &error);
51  if (error)
52  LOG_WARNING("Does license name %s have multibyte encoding?", licenseName)
53 
54  /* verify the license is not already in the table */
55  snprintf(query, myBUFSIZ - 1, "SELECT rf_pk FROM " LICENSE_REF_TABLE " where rf_shortname='%s'", escLicName);
56  result = PQexec(gl.pgConn, query);
57  if (fo_checkPQresult(gl.pgConn, result, query, __FILE__, __LINE__))
58  return 0;
59  numRows = PQntuples(result);
60  if (numRows)
61  {
62  rf_pk = atol(PQgetvalue(result, 0, 0));
63  PQclear(result);
64  return rf_pk;
65  }
66  PQclear(result);
67 
68  /* Insert the new license */
69  specialLicenseText = "License by Nomos.";
70 
71  snprintf(insert, myBUFSIZ - 1, "insert into license_ref(rf_shortname, rf_text, rf_detector_type) values('%s', '%s', 2)", escLicName,
72  specialLicenseText);
73  result = PQexec(gl.pgConn, insert);
74  // ignore duplicate constraint failure (23505), report others
75  if ((result == 0)
76  || ((PQresultStatus(result) != PGRES_COMMAND_OK)
77  && (strncmp(PG_ERRCODE_UNIQUE_VIOLATION, PQresultErrorField(result, PG_DIAG_SQLSTATE), 5))))
78  {
79  printf("ERROR: %s(%d): Nomos failed to add a new license. %s/n: %s/n",
80  __FILE__, __LINE__, PQresultErrorMessage(result), insert);
81  PQclear(result);
82  return (0);
83  }
84  PQclear(result);
85 
86  /* retrieve the new rf_pk */
87  result = PQexec(gl.pgConn, query);
88  if (fo_checkPQresult(gl.pgConn, result, query, __FILE__, __LINE__))
89  return 0;
90  numRows = PQntuples(result);
91  if (numRows)
92  rf_pk = atol(PQgetvalue(result, 0, 0));
93  else
94  {
95  printf("ERROR: %s:%s:%d Just inserted value is missing. On: %s", __FILE__, "add2license_ref()", __LINE__, query);
96  PQclear(result);
97  return (0);
98  }
99  PQclear(result);
100 
101  return (rf_pk);
102 }
103 
113 FUNCTION long lrcache_hash(cacheroot_t *pcroot, char *rf_shortname)
114 {
115  long hashval = 0;
116  int len, i;
117 
118  /* use the first sizeof(long) bytes for the hash value */
119  len = (strlen(rf_shortname) < sizeof(long)) ? strlen(rf_shortname) : sizeof(long);
120  for (i = 0; i < len; i++)
121  hashval += rf_shortname[i] << 8 * i;
122  hashval = hashval % pcroot->maxnodes;
123  return hashval;
124 }
125 
133 FUNCTION void lrcache_print(cacheroot_t *pcroot)
134 {
135  cachenode_t *pcnode;
136  long hashval = 0;
137  int i;
138 
139  pcnode = pcroot->nodes;
140  for (i = 0; i < pcroot->maxnodes; i++)
141  {
142  if (pcnode->rf_pk != 0L)
143  {
144  hashval = lrcache_hash(pcroot, pcnode->rf_shortname);
145  printf("%ld, %ld, %s\n", hashval, pcnode->rf_pk, pcnode->rf_shortname);
146  }
147  pcnode++;
148  }
149 }
150 
158 FUNCTION void lrcache_free(cacheroot_t *pcroot)
159 {
160  cachenode_t *pcnode;
161  int i;
162 
163  pcnode = pcroot->nodes;
164  for (i = 0; i < pcroot->maxnodes; i++)
165  {
166  if (pcnode->rf_pk != 0L)
167  {
168  free(pcnode->rf_shortname);
169  }
170  pcnode++;
171  }
172  free(pcroot->nodes);
173 }
174 
185 FUNCTION int lrcache_add(cacheroot_t *pcroot, long rf_pk, char *rf_shortname)
186 {
187  cachenode_t *pcnode;
188  long hashval = 0;
189  int i;
190  int noden;
191 
192  hashval = lrcache_hash(pcroot, rf_shortname);
193 
194  noden = hashval;
195  for (i = 0; i < pcroot->maxnodes; i++)
196  {
197  noden = (hashval + i) & (pcroot->maxnodes - 1);
198 
199  pcnode = pcroot->nodes + noden;
200  if (!pcnode->rf_pk)
201  {
202  pcnode->rf_shortname = strdup(rf_shortname);
203  pcnode->rf_pk = rf_pk;
204  break;
205  }
206  }
207  if (i < pcroot->maxnodes)
208  return 0;
209 
210  return -1; /* no space */
211 }
212 
222 FUNCTION long lrcache_lookup(cacheroot_t *pcroot, char *rf_shortname)
223 {
224  cachenode_t *pcnode;
225  long hashval = 0;
226  int i;
227  int noden;
228 
229  hashval = lrcache_hash(pcroot, rf_shortname);
230 
231  noden = hashval;
232  for (i = 0; i < pcroot->maxnodes; i++)
233  {
234  noden = (hashval + i) & (pcroot->maxnodes - 1);
235 
236  pcnode = pcroot->nodes + noden;
237  if (!pcnode->rf_pk)
238  return 0;
239  if (strcmp(pcnode->rf_shortname, rf_shortname) == 0)
240  {
241  return pcnode->rf_pk;
242  }
243  }
244 
245  return 0; /* not found */
246 }
247 
260 FUNCTION int initLicRefCache(cacheroot_t *pcroot)
261 {
262 
263  PGresult *result;
264  char query[myBUFSIZ];
265  int row;
266  int numLics;
267 
268  if (!pcroot)
269  return 0;
270 
271  sprintf(query, "SELECT rf_pk, rf_shortname FROM " LICENSE_REF_TABLE " where rf_detector_type=2");
272  result = PQexec(gl.pgConn, query);
273  if (fo_checkPQresult(gl.pgConn, result, query, __FILE__, __LINE__))
274  return 0;
275 
276  numLics = PQntuples(result);
277  /* populate the cache */
278  for (row = 0; row < numLics; row++)
279  {
280  lrcache_add(pcroot, atol(PQgetvalue(result, row, 0)), PQgetvalue(result, row, 1));
281  }
282 
283  PQclear(result);
284 
285  return (1);
286 } /* initLicRefCache */
287 
300 FUNCTION long get_rfpk(cacheroot_t *pcroot, char *rf_shortname)
301 {
302  long rf_pk;
303  size_t len;
304 
305  if ((len = strlen(rf_shortname)) == 0)
306  {
307  printf("ERROR! Nomos.c get_rfpk() passed empty name");
308  return (0);
309  }
310 
311  /* is this in the cache? */
312  rf_pk = lrcache_lookup(pcroot, rf_shortname);
313  if (rf_pk)
314  return rf_pk;
315 
316  /* shortname was not found, so add it */
317  /* add to the license_ref table */
318  rf_pk = add2license_ref(rf_shortname);
319 
320  /* add to the cache */
321  lrcache_add(pcroot, rf_pk, rf_shortname);
322 
323  return (rf_pk);
324 } /* get_rfpk */
325 
333 FUNCTION char *getFieldValue(char *inStr, char *field, int fieldMax,
334  char *value, int valueMax, char separator)
335 {
336  int s;
337  int f;
338  int v;
339  int gotQuote;
340 
341 #ifdef PROC_TRACE
342  traceFunc("== getFieldValue(inStr= %s fieldMax= %d separator= '%c'\n",
343  inStr, fieldMax, separator);
344 #endif /* PROC_TRACE */
345 
346  memset(field, 0, fieldMax);
347  memset(value, 0, valueMax);
348 
349  /* Skip initial spaces */
350  while (isspace(inStr[0]))
351  {
352  inStr++;
353  }
354 
355  if (inStr[0] == '\0')
356  {
357  return (NULL);
358  }
359  f = 0;
360  v = 0;
361 
362  /* Skip to end of field name */
363  for (s = 0; (inStr[s] != '\0') && !isspace(inStr[s]) && (inStr[s] != '='); s++)
364  {
365  field[f++] = inStr[s];
366  }
367 
368  /* Skip spaces after field name */
369  while (isspace(inStr[s]))
370  {
371  s++;
372  }
373  /* If it is not a field, then just return it. */
374  if (inStr[s] != separator)
375  {
376  return (inStr + s);
377  }
378  if (inStr[s] == '\0')
379  {
380  return (NULL);
381  }
382  /* Skip '=' */
383  s++;
384 
385  /* Skip spaces after '=' */
386  while (isspace(inStr[s]))
387  {
388  s++;
389  }
390  if (inStr[s] == '\0')
391  {
392  return (NULL);
393  }
394 
395  gotQuote = '\0';
396  if ((inStr[s] == '\'') || (inStr[s] == '"'))
397  {
398  gotQuote = inStr[s];
399  s++; /* skip quote */
400  if (inStr[s] == '\0')
401  {
402  return (NULL);
403  }
404  }
405 
406  if (gotQuote)
407  {
408  for (; (inStr[s] != '\0') && (inStr[s] != gotQuote); s++)
409  {
410  if (inStr[s] == '\\')
411  {
412  value[v++] = inStr[++s];
413  }
414  else
415  {
416  value[v++] = inStr[s];
417  }
418  }
419  }
420  else
421  {
422  /* if it gets here, then there is no quote */
423  for (; (inStr[s] != '\0') && !isspace(inStr[s]); s++)
424  {
425  if (inStr[s] == '\\')
426  {
427  value[v++] = inStr[++s];
428  }
429  else
430  {
431  value[v++] = inStr[s];
432  }
433  }
434  }
435  /* Skip spaces */
436  while (isspace(inStr[s]))
437  {
438  s++;
439  }
440 
441  return (inStr + s);
442 } /* getFieldValue */
443 
450 FUNCTION void parseLicenseList()
451 {
452 
453  int numLics = 0;
454 
455  /* char saveLics[myBUFSIZ]; */
456  char *saveptr = 0; /* used for strtok_r */
457  char *saveLicsPtr;
458 
459  if ((strlen(cur.compLic)) == 0)
460  {
461  return;
462  }
463 
464  /* check for a single name FIX THIS!*/
465  if (strstr(cur.compLic, ",") == NULL)
466  {
467  cur.licenseList[0] = cur.compLic;
468  cur.licenseList[1] = NULL;
469  return;
470  }
471 
472  saveLicsPtr = strcpy(saveLics, cur.compLic);
473 
474  cur.tmpLics = strtok_r(saveLicsPtr, ",", &saveptr);
475 
476  cur.licenseList[numLics] = cur.tmpLics;
477  numLics++;
478 
479  saveLicsPtr = NULL;
480  while (cur.tmpLics)
481  {
482  cur.tmpLics = strtok_r(saveLicsPtr, ",", &saveptr);
483  if (cur.tmpLics == NULL)
484  {
485  break;
486  }
487  cur.licenseList[numLics] = cur.tmpLics;
488  numLics++;
489  }
490  cur.licenseList[numLics] = NULL;
491  numLics++;
492 
493  /*
494  int i;
495  for(i=0; i<numLics; i++){
496  printf("cur.licenseList[%d] is:%s\n",i,cur.licenseList[i]);
497  }
498 
499  printf("parseLicenseList: returning\n");
500  */
501 
502  return;
503 } /* parseLicenseList */
504 
509 FUNCTION void Usage(char *Name)
510 {
511  printf("Usage: %s [options] [file [file [...]]\n", Name);
512  printf(" -h :: help (print this message), then exit.\n");
513  printf(" -i :: initialize the database, then exit.\n");
514  printf(" -c :: specify the directory for the system configuration.\n");
515  printf(" -l :: print full file path (command line only).\n");
516  printf(" -v :: verbose (-vv = more verbose)\n");
517  printf(" -J :: output in JSON\n");
518  printf(" -S :: print Highlightinfo to stdout \n");
519  printf(" file :: if files are listed, print the licenses detected within them.\n");
520  printf(" no file :: process data from the scheduler.\n");
521  printf(" -V :: print the version info, then exit.\n");
522  printf(" -d :: specify a directory to scan.\n");
523  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");
524 } /* Usage() */
525 
533 FUNCTION void Bail(int exitval)
534 {
535 #ifdef PROC_TRACE
536  traceFunc("== Bail(%d)\n", exitval);
537 #endif /* PROC_TRACE */
538 
539 #if defined(MEMORY_TRACING) && defined(MEM_ACCT)
540  if (exitval)
541  {
542  memCacheDump("Mem-cache @ Bail() time:");
543  }
544 #endif /* MEMORY_TRACING && MEM_ACCT */
545 
546  /* close database and scheduler connections */
547  if (gl.pgConn) {
549  PQfinish(gl.pgConn);
550  }
551  fo_scheduler_disconnect(exitval);
552  exit(exitval);
553 }
554 
560 FUNCTION int optionIsSet(int val)
561 {
562 #ifdef PROC_TRACE
563  traceFunc("== optionIsSet(%x)\n", val);
564 #endif /* PROC_TRACE */
565 
566  return (gl.progOpts & val);
567 } /* optionIsSet */
568 
580 FUNCTION void getFileLists(char *dirpath)
581 {
582 #ifdef PROC_TRACE
583  traceFunc("== getFileLists(%s)\n", dirpath);
584 #endif /* PROC_TRACE */
585 
586  /* listInit(&gl.sarchList, 0, "source-archives list & md5sum map"); */
587  listInit(&cur.regfList, 0, "regular-files list");
588  listInit(&cur.offList, 0, "buffer-offset list");
589 #ifdef FLAG_NO_COPYRIGHT
590  listInit(&gl.nocpyrtList, 0, "no-copyright list");
591 #endif /* FLAG_NO_COPYRIGHT */
592 
593  listGetItem(&cur.regfList, cur.targetFile);
594  return;
595 } /* getFileLists */
596 
606 FUNCTION long updateLicenseFile(long rfPk)
607 {
608 
609  PGresult *result;
610 
611  if (rfPk <= 0)
612  {
613  return (-2);
614  }
615 
616  /* If files are coming from command line instead of fossology repo,
617  then there are no pfiles. So don't update the db
618  */
619  if (cur.cliMode == 1)
620  return (-1);
621 
622  result = fo_dbManager_ExecPrepared(
623  fo_dbManager_PrepareStamement(
624  gl.dbManager,
625  "updateLicenseFile",
626  "INSERT INTO license_file(rf_fk, agent_fk, pfile_fk) VALUES($1, $2, $3) RETURNING fl_pk",
627  long, int, long
628  ),
629  rfPk, gl.agentPk, cur.pFileFk
630  );
631 
632  if (result) {
633  long licenseFileId = -1;
634  if (PQntuples(result) > 0) {
635  licenseFileId = atol(PQgetvalue(result, 0, 0));
636  }
637 
638  PQclear(result);
639  return (licenseFileId);
640  } else {
641  return (-1);
642  }
643 } /* updateLicenseFile */
644 
652 FUNCTION char convertIndexToHighlightType(int index)
653 {
654 
655  char type;
656 
657  if ((index >= _KW_first) && (index <= _KW_last))
658  type = 'K';
659  else if (index > _KW_last)
660  type = 'L';
661  else type = '0';
662 
663  return type;
664 
665 }
666 
677 
678  /* If files are coming from command line instead of fossology repo,
679  then there are no pfiles. So don't update the db
680 
681  Also if we specifically do not want highlight information in the
682  database skip this function
683  */
684  if(cur.cliMode == 1 || optionIsSet(OPTS_NO_HIGHLIGHTINFO ) ){
685  return (TRUE);
686  }
687  PGresult *result;
688 
689 
690 
691 
692 #ifdef GLOBAL_DEBUG
693  printf("%s %s %i \n", cur.filePath,cur.compLic , cur.theMatches->len);
694 #endif
695 
696  // This speeds up the writing to the database and ensures that we have either full highlight information or none
697  PGresult* begin1 = PQexec(gl.pgConn, "BEGIN");
698  PQclear(begin1);
699 
700  fo_dbManager_PreparedStatement* preparedKeywords;
701  if(cur.keywordPositions->len > 0 ) {
702  preparedKeywords = fo_dbManager_PrepareStamement(
703  gl.dbManager,
704  "updateLicenseHighlighting:keyword",
705  "INSERT INTO highlight_keyword (pfile_fk, start, len) VALUES($1, $2, $3)",
706  long, int, int
707  );
708  }
709  int i;
710  for (i = 0; i < cur.keywordPositions->len; ++i)
711  {
713  result = fo_dbManager_ExecPrepared(
714  preparedKeywords,
715  cur.pFileFk, ourMatchv->start, ourMatchv->end - ourMatchv->start);
716  if (result)
717  {
718  PQclear(result);
719  }
720  }
721  PGresult* commit1 = PQexec(gl.pgConn, "COMMIT");
722  PQclear(commit1);
723 
724  PGresult* begin2 =PQexec(gl.pgConn, "BEGIN");
725  PQclear(begin2);
726  fo_dbManager_PreparedStatement* preparedLicenses;
727 
728  if(cur.theMatches->len > 0 ) {
729  preparedLicenses=fo_dbManager_PrepareStamement(
730  gl.dbManager,
731  "updateLicenseHighlighting",
732  "INSERT INTO highlight (fl_fk, start, len, type) VALUES($1, $2, $3,'L')",
733  long, int, int
734  );
735  }
736 
737  for (i = 0; i < cur.theMatches->len; ++i)
738  {
740 
741  int j;
742  for (j = 0; j < ourLicence->matchPositions->len; ++j)
743  {
745  if(ourLicence->licenseFileId == -1) {
747  continue;
748  }
749  result = fo_dbManager_ExecPrepared(
750  preparedLicenses,
751  ourLicence->licenseFileId,
752  ourMatchv->start, ourMatchv->end - ourMatchv->start
753  );
754  if (result == NULL)
755  {
756  return (FALSE);
757  } else {
758  PQclear(result);
759  }
760  }
761  }
762 
763  PGresult* commit2 = PQexec(gl.pgConn, "COMMIT");
764  PQclear(commit2);
765  return (TRUE);
766 } /* updateLicenseHighlighting */
767 
768 
774 FUNCTION void processFile(char *fileToScan)
775 {
776 
777  char *pathcopy;
778 #ifdef PROC_TRACE
779  traceFunc("== processFile(%s)\n", fileToScan);
780 #endif /* PROC_TRACE */
781 
782  /* printf(" LOG: nomos scanning file %s.\n", fileToScan); DEBUG */
783 
784  (void) strcpy(cur.cwd, gl.initwd);
785 
786  strcpy(cur.filePath, fileToScan);
787  pathcopy = g_strdup(fileToScan);
788  strcpy(cur.targetDir, dirname(pathcopy));
789  g_free(pathcopy);
790  strcpy(cur.targetFile, fileToScan);
791  cur.targetLen = strlen(cur.targetDir);
792 
793  if (!isFILE(fileToScan))
794  {
795  LOG_FATAL("\"%s\" is not a plain file", fileToScan)
796  Bail(-__LINE__);
797  }
798 
799  getFileLists(cur.targetDir);
800  listInit(&cur.fLicFoundMap, 0, "file-license-found map");
801  listInit(&cur.parseList, 0, "license-components list");
802  listInit(&cur.lList, 0, "license-list");
803 
805 
806  /* freeAndClearScan(&cur); */
807 } /* Process File */
808 
814 void setLicenseFileIdInHiglightArray(long licenseFileId, char* licenseName){
815  int i;
816  for (i = 0; i < cur.theMatches->len; ++i) {
818  if (strcmp(licenseName, ourLicence->licenceName) == 0)
819  ourLicence->licenseFileId = licenseFileId;
820  }
821 } /* setLicenseFileIdInHiglightArray */
822 
829 int updateLicenseFileAndHighlightArray(char* licenseName, cacheroot_t* pcroot) {
830  long rf_pk = get_rfpk(pcroot, licenseName);
831  long licenseFileId = updateLicenseFile(rf_pk);
832  if (licenseFileId > 0) {
833  setLicenseFileIdInHiglightArray(licenseFileId, licenseName);
834  return (true);
835  } else {
836  return (false);
837  }
838 }
839 
850 FUNCTION int recordScanToDB(cacheroot_t *pcroot, struct curScan *scanRecord)
851 {
852 
853  char *noneFound;
854  int numLicenses;
855 
856 #ifdef SIMULATESCHED
857  /* BOBG: This allows a developer to simulate the scheduler
858  with a load file for testing/debugging, without updating the
859  database. Like:
860  cat myloadfile | ./nomos
861  myloadfile is same as what scheduler sends:
862  pfile_pk=311667 pfilename=9A96127E7D3B2812B50BF7732A2D0FF685EF6D6A.78073D1CA7B4171F8AFEA1497E4C6B33.183
863  pfile_pk=311727 pfilename=B7F5EED9ECB679EE0F980599B7AA89DCF8FA86BD.72B00E1B419D2C83D1050C66FA371244.368
864  etc.
865  */
866  printf("%s\n",scanRecord->compLic);
867  return(0);
868 #endif
869 
870  noneFound = strstr(scanRecord->compLic, LS_NONE);
871  if (noneFound != NULL)
872  {
873  if (!updateLicenseFileAndHighlightArray("No_license_found", pcroot))
874  return (-1);
875  return (0);
876  }
877 
878  /* we have one or more license names, parse them */
880  /* loop through the found license names */
881  for (numLicenses = 0; cur.licenseList[numLicenses] != NULL; numLicenses++)
882  {
883  if (!updateLicenseFileAndHighlightArray(cur.licenseList[numLicenses], pcroot))
884  return (-1);
885  }
886 
887  if (updateLicenseHighlighting(pcroot) == FALSE)
888  {
889  printf("Failure in update of highlight table \n");
890  }
891 
892  return (0);
893 } /* recordScanToDb */
894 
902  int index)
903 {
904  return &g_array_index(in, MatchPositionAndType, index);
905 }
906 
914  GArray* in, int index)
915 {
916  return &g_array_index(in, LicenceAndMatchPositions, index);
917 }
918 
926 FUNCTION void initializeCurScan(struct curScan* cur)
927 {
928  cur->indexList = g_array_new(FALSE, FALSE, sizeof(int));
929  cur->theMatches = g_array_new(FALSE, FALSE, sizeof(LicenceAndMatchPositions));
930  cur->keywordPositions = g_array_new(FALSE, FALSE, sizeof(MatchPositionAndType));
931  cur->docBufferPositionsAndOffsets = g_array_new(FALSE, FALSE, sizeof(pairPosOff));
932  cur->currentLicenceIndex=-1;
933 }
934 
935 
941 FUNCTION void freeAndClearScan(struct curScan *thisScan)
942 {
943  /*
944  Clear lists
945  */
946  listClear(&thisScan->regfList, DEALLOC_LIST);
947  listClear(&thisScan->offList, DEALLOC_LIST);
948  listClear(&thisScan->fLicFoundMap, DEALLOC_LIST);
949  listClear(&thisScan->parseList, DEALLOC_LIST);
950  listClear(&thisScan->lList, DEALLOC_LIST);
951  g_array_free(thisScan->indexList,TRUE);
952  cleanTheMatches(thisScan->theMatches);
953  g_array_free(thisScan->keywordPositions, TRUE);
954  g_array_free(thisScan->docBufferPositionsAndOffsets, TRUE);
955 
956 
957  /* remove keys, data and hash table */
958  hdestroy();
959 
960 }
961 
966 FUNCTION inline void cleanTheMatches(GArray* theMatches){
967 
968  int i;
969  for(i=0; i< theMatches->len; ++i) {
971  }
972  g_array_free( theMatches, TRUE);
973 }
974 
980 {
981  if(in->licenceName) g_free(in->licenceName);
982  g_array_free(in->matchPositions, TRUE);
983  g_array_free(in->indexList,TRUE);
984 }
985 
991 FUNCTION inline void addLicence(GArray* theMatches, char* licenceName ) {
992  LicenceAndMatchPositions newMatch;
993  newMatch.indexList = cur.indexList;
994  cur.indexList=g_array_new(FALSE, FALSE, sizeof(int));
995 
997  newMatch.matchPositions = g_array_new(FALSE, FALSE, sizeof(MatchPositionAndType));
998  newMatch.licenceName = g_strdup(licenceName);
999  newMatch.licenseFileId = -1; //initial Value <- check if it was set
1000  g_array_append_val(theMatches , newMatch);
1001 }
1002 
1006 inline void cleanLicenceBuffer(){
1007  g_array_set_size(cur.indexList, 0);
1008 }
1009 
1015  if(cur.indexList->len>0)
1016  g_array_remove_index(cur.indexList, cur.indexList->len -1);
1017  return true;
1018 }
1019 
1020 
1021 
1022 
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:36
void fo_dbManager_free(fo_dbManager *dbManager)
Un-allocate the memory from a DB manager.
Definition: standalone.c:34
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:133
FUNCTION void freeAndClearScan(struct curScan *thisScan)
Clean-up all the per scan data structures, freeing any old data.
Definition: nomos_utils.c:941
FUNCTION void lrcache_free(cacheroot_t *pcroot)
free the hash table
Definition: nomos_utils.c:158
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:333
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:222
FUNCTION void Usage(char *Name)
Print nomos usage help.
Definition: nomos_utils.c:509
sem_t * mutexJson
Mutex to handle JSON writes.
Definition: nomos_utils.c:21
FUNCTION void processFile(char *fileToScan)
process a single file
Definition: nomos_utils.c:774
FUNCTION void cleanTheMatches(GArray *theMatches)
Cleans the match array and free the memory.
Definition: nomos_utils.c:966
FUNCTION void parseLicenseList()
parse the comma separated list of license names found
Definition: nomos_utils.c:450
FUNCTION long updateLicenseFile(long rfPk)
insert rf_fk, agent_fk and pfile_fk into license_file table
Definition: nomos_utils.c:606
FUNCTION void initializeCurScan(struct curScan *cur)
Initialize the scanner.
Definition: nomos_utils.c:926
FUNCTION int recordScanToDB(cacheroot_t *pcroot, struct curScan *scanRecord)
Write out the information about the scan to the FOSSology database.
Definition: nomos_utils.c:850
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:185
void cleanLicenceBuffer()
Clean the license buffer.
Definition: nomos_utils.c:1006
FUNCTION void addLicence(GArray *theMatches, char *licenceName)
Add a license to the matches array.
Definition: nomos_utils.c:991
FUNCTION MatchPositionAndType * getMatchfromHighlightInfo(GArray *in, int index)
Get the MatchPositionAndType for a given index in highlight array.
Definition: nomos_utils.c:901
FUNCTION void getFileLists(char *dirpath)
Initialize the lists: regular-files list cur.regfList and buffer-offset list cur.offList.
Definition: nomos_utils.c:580
FUNCTION LicenceAndMatchPositions * getLicenceAndMatchPositions(GArray *in, int index)
Get the LicenceAndMatchPositions for a given index in match array.
Definition: nomos_utils.c:913
gboolean * printcomma
True to print comma while printing JSON object.
Definition: nomos_utils.c:22
FUNCTION void cleanLicenceAndMatchPositions(LicenceAndMatchPositions *in)
Cleans the license and match positions object and free the memory.
Definition: nomos_utils.c:979
FUNCTION int initLicRefCache(cacheroot_t *pcroot)
build a cache the license ref db table.
Definition: nomos_utils.c:260
char saveLics[myBUFSIZ]
License string.
Definition: nomos_utils.c:23
int updateLicenseFileAndHighlightArray(char *licenseName, cacheroot_t *pcroot)
Add a license to hash table, license table and highlight array.
Definition: nomos_utils.c:829
FUNCTION int optionIsSet(int val)
Check if an CLI option is set.
Definition: nomos_utils.c:560
bool clearLastElementOfLicenceBuffer()
Remove the last element from license buffer.
Definition: nomos_utils.c:1014
void setLicenseFileIdInHiglightArray(long licenseFileId, char *licenseName)
Set the license file id to the highlights.
Definition: nomos_utils.c:814
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:113
FUNCTION void Bail(int exitval)
Close connections and exit.
Definition: nomos_utils.c:533
FUNCTION char convertIndexToHighlightType(int index)
Return the highlight type (K|L|0) for a given index.
Definition: nomos_utils.c:652
FUNCTION long get_rfpk(cacheroot_t *pcroot, char *rf_shortname)
Get the rf_pk for rf_shortname.
Definition: nomos_utils.c:300
FUNCTION long add2license_ref(char *licenseName)
Add a new license to license_ref table.
Definition: nomos_utils.c:34
FUNCTION int updateLicenseHighlighting(cacheroot_t *pcroot)
insert rf_fk, agent_fk, offset, len and type into highlight table
Definition: nomos_utils.c:676
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