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