FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
nomos_utils.c
Go to the documentation of this file.
1 /***************************************************************
2  Copyright (C) 2006-2014 Hewlett-Packard Development Company, L.P.
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 
17  ***************************************************************/
18 
19 #ifndef _GNU_SOURCE
20 #define _GNU_SOURCE
21 #endif /* not defined _GNU_SOURCE */
22 
23 #include "nomos_utils.h"
24 #include "nomos.h"
25 
26 #define FUNCTION
27 
42 FUNCTION long add2license_ref(char *licenseName)
43 {
44 
45  PGresult *result;
46  char query[myBUFSIZ];
47  char insert[myBUFSIZ];
48  char escLicName[myBUFSIZ];
49  char *specialLicenseText;
50  long rf_pk;
51 
52  int len;
53  int error;
54  int numRows;
55 
56  // escape the name
57  len = strlen(licenseName);
58  PQescapeStringConn(gl.pgConn, escLicName, licenseName, len, &error);
59  if (error)
60  LOG_WARNING("Does license name %s have multibyte encoding?", licenseName)
61 
62  /* verify the license is not already in the table */
63  sprintf(query, "SELECT rf_pk FROM " LICENSE_REF_TABLE " where rf_shortname='%s'", escLicName);
64  result = PQexec(gl.pgConn, query);
65  if (fo_checkPQresult(gl.pgConn, result, query, __FILE__, __LINE__))
66  return 0;
67  numRows = PQntuples(result);
68  if (numRows)
69  {
70  rf_pk = atol(PQgetvalue(result, 0, 0));
71  PQclear(result);
72  return rf_pk;
73  }
74  PQclear(result);
75 
76  /* Insert the new license */
77  specialLicenseText = "License by Nomos.";
78 
79  sprintf(insert, "insert into license_ref(rf_shortname, rf_text, rf_detector_type) values('%s', '%s', 2)", escLicName,
80  specialLicenseText);
81  result = PQexec(gl.pgConn, insert);
82  // ignore duplicate constraint failure (23505), report others
83  if ((result == 0)
84  || ((PQresultStatus(result) != PGRES_COMMAND_OK)
85  && (strncmp(PG_ERRCODE_UNIQUE_VIOLATION, PQresultErrorField(result, PG_DIAG_SQLSTATE), 5))))
86  {
87  printf("ERROR: %s(%d): Nomos failed to add a new license. %s/n: %s/n",
88  __FILE__, __LINE__, PQresultErrorMessage(result), insert);
89  PQclear(result);
90  return (0);
91  }
92  PQclear(result);
93 
94  /* retrieve the new rf_pk */
95  result = PQexec(gl.pgConn, query);
96  if (fo_checkPQresult(gl.pgConn, result, query, __FILE__, __LINE__))
97  return 0;
98  numRows = PQntuples(result);
99  if (numRows)
100  rf_pk = atol(PQgetvalue(result, 0, 0));
101  else
102  {
103  printf("ERROR: %s:%s:%d Just inserted value is missing. On: %s", __FILE__, "add2license_ref()", __LINE__, query);
104  PQclear(result);
105  return (0);
106  }
107  PQclear(result);
108 
109  return (rf_pk);
110 }
111 
121 FUNCTION long lrcache_hash(cacheroot_t *pcroot, char *rf_shortname)
122 {
123  long hashval = 0;
124  int len, i;
125 
126  /* use the first sizeof(long) bytes for the hash value */
127  len = (strlen(rf_shortname) < sizeof(long)) ? strlen(rf_shortname) : sizeof(long);
128  for (i = 0; i < len; i++)
129  hashval += rf_shortname[i] << 8 * i;
130  hashval = hashval % pcroot->maxnodes;
131  return hashval;
132 }
133 
141 FUNCTION void lrcache_print(cacheroot_t *pcroot)
142 {
143  cachenode_t *pcnode;
144  long hashval = 0;
145  int i;
146 
147  pcnode = pcroot->nodes;
148  for (i = 0; i < pcroot->maxnodes; i++)
149  {
150  if (pcnode->rf_pk != 0L)
151  {
152  hashval = lrcache_hash(pcroot, pcnode->rf_shortname);
153  printf("%ld, %ld, %s\n", hashval, pcnode->rf_pk, pcnode->rf_shortname);
154  }
155  pcnode++;
156  }
157 }
158 
166 FUNCTION void lrcache_free(cacheroot_t *pcroot)
167 {
168  cachenode_t *pcnode;
169  int i;
170 
171  pcnode = pcroot->nodes;
172  for (i = 0; i < pcroot->maxnodes; i++)
173  {
174  if (pcnode->rf_pk != 0L)
175  {
176  free(pcnode->rf_shortname);
177  }
178  pcnode++;
179  }
180  free(pcroot->nodes);
181 }
182 
193 FUNCTION int lrcache_add(cacheroot_t *pcroot, long rf_pk, char *rf_shortname)
194 {
195  cachenode_t *pcnode;
196  long hashval = 0;
197  int i;
198  int noden;
199 
200  hashval = lrcache_hash(pcroot, rf_shortname);
201 
202  noden = hashval;
203  for (i = 0; i < pcroot->maxnodes; i++)
204  {
205  noden = (hashval + i) & (pcroot->maxnodes - 1);
206 
207  pcnode = pcroot->nodes + noden;
208  if (!pcnode->rf_pk)
209  {
210  pcnode->rf_shortname = strdup(rf_shortname);
211  pcnode->rf_pk = rf_pk;
212  break;
213  }
214  }
215  if (i < pcroot->maxnodes)
216  return 0;
217 
218  return -1; /* no space */
219 }
220 
230 FUNCTION long lrcache_lookup(cacheroot_t *pcroot, char *rf_shortname)
231 {
232  cachenode_t *pcnode;
233  long hashval = 0;
234  int i;
235  int noden;
236 
237  hashval = lrcache_hash(pcroot, rf_shortname);
238 
239  noden = hashval;
240  for (i = 0; i < pcroot->maxnodes; i++)
241  {
242  noden = (hashval + i) & (pcroot->maxnodes - 1);
243 
244  pcnode = pcroot->nodes + noden;
245  if (!pcnode->rf_pk)
246  return 0;
247  if (strcmp(pcnode->rf_shortname, rf_shortname) == 0)
248  {
249  return pcnode->rf_pk;
250  }
251  }
252 
253  return 0; /* not found */
254 }
255 
268 FUNCTION int initLicRefCache(cacheroot_t *pcroot)
269 {
270 
271  PGresult *result;
272  char query[myBUFSIZ];
273  int row;
274  int numLics;
275 
276  if (!pcroot)
277  return 0;
278 
279  sprintf(query, "SELECT rf_pk, rf_shortname FROM " LICENSE_REF_TABLE " where rf_detector_type=2");
280  result = PQexec(gl.pgConn, query);
281  if (fo_checkPQresult(gl.pgConn, result, query, __FILE__, __LINE__))
282  return 0;
283 
284  numLics = PQntuples(result);
285  /* populate the cache */
286  for (row = 0; row < numLics; row++)
287  {
288  lrcache_add(pcroot, atol(PQgetvalue(result, row, 0)), PQgetvalue(result, row, 1));
289  }
290 
291  PQclear(result);
292 
293  return (1);
294 } /* initLicRefCache */
295 
308 FUNCTION long get_rfpk(cacheroot_t *pcroot, char *rf_shortname)
309 {
310  long rf_pk;
311  size_t len;
312 
313  if ((len = strlen(rf_shortname)) == 0)
314  {
315  printf("ERROR! Nomos.c get_rfpk() passed empty name");
316  return (0);
317  }
318 
319  /* is this in the cache? */
320  rf_pk = lrcache_lookup(pcroot, rf_shortname);
321  if (rf_pk)
322  return rf_pk;
323 
324  /* shortname was not found, so add it */
325  /* add to the license_ref table */
326  rf_pk = add2license_ref(rf_shortname);
327 
328  /* add to the cache */
329  lrcache_add(pcroot, rf_pk, rf_shortname);
330 
331  return (rf_pk);
332 } /* get_rfpk */
333 
341 FUNCTION char *getFieldValue(char *inStr, char *field, int fieldMax,
342  char *value, int valueMax, char separator)
343 {
344  int s;
345  int f;
346  int v;
347  int gotQuote;
348 
349 #ifdef PROC_TRACE
350  traceFunc("== getFieldValue(inStr= %s fieldMax= %d separator= '%c'\n",
351  inStr, fieldMax, separator);
352 #endif /* PROC_TRACE */
353 
354  memset(field, 0, fieldMax);
355  memset(value, 0, valueMax);
356 
357  /* Skip initial spaces */
358  while (isspace(inStr[0]))
359  {
360  inStr++;
361  }
362 
363  if (inStr[0] == '\0')
364  {
365  return (NULL);
366  }
367  f = 0;
368  v = 0;
369 
370  /* Skip to end of field name */
371  for (s = 0; (inStr[s] != '\0') && !isspace(inStr[s]) && (inStr[s] != '='); s++)
372  {
373  field[f++] = inStr[s];
374  }
375 
376  /* Skip spaces after field name */
377  while (isspace(inStr[s]))
378  {
379  s++;
380  }
381  /* If it is not a field, then just return it. */
382  if (inStr[s] != separator)
383  {
384  return (inStr + s);
385  }
386  if (inStr[s] == '\0')
387  {
388  return (NULL);
389  }
390  /* Skip '=' */
391  s++;
392 
393  /* Skip spaces after '=' */
394  while (isspace(inStr[s]))
395  {
396  s++;
397  }
398  if (inStr[s] == '\0')
399  {
400  return (NULL);
401  }
402 
403  gotQuote = '\0';
404  if ((inStr[s] == '\'') || (inStr[s] == '"'))
405  {
406  gotQuote = inStr[s];
407  s++; /* skip quote */
408  if (inStr[s] == '\0')
409  {
410  return (NULL);
411  }
412  }
413 
414  if (gotQuote)
415  {
416  for (; (inStr[s] != '\0') && (inStr[s] != gotQuote); s++)
417  {
418  if (inStr[s] == '\\')
419  {
420  value[v++] = inStr[++s];
421  }
422  else
423  {
424  value[v++] = inStr[s];
425  }
426  }
427  }
428  else
429  {
430  /* if it gets here, then there is no quote */
431  for (; (inStr[s] != '\0') && !isspace(inStr[s]); s++)
432  {
433  if (inStr[s] == '\\')
434  {
435  value[v++] = inStr[++s];
436  }
437  else
438  {
439  value[v++] = inStr[s];
440  }
441  }
442  }
443  /* Skip spaces */
444  while (isspace(inStr[s]))
445  {
446  s++;
447  }
448 
449  return (inStr + s);
450 } /* getFieldValue */
451 
458 FUNCTION void parseLicenseList()
459 {
460 
461  int numLics = 0;
462 
463  /* char saveLics[myBUFSIZ]; */
464  char *saveptr = 0; /* used for strtok_r */
465  char *saveLicsPtr;
466 
467  if ((strlen(cur.compLic)) == 0)
468  {
469  return;
470  }
471 
472  /* check for a single name FIX THIS!*/
473  if (strstr(cur.compLic, ",") == NULL)
474  {
475  cur.licenseList[0] = cur.compLic;
476  cur.licenseList[1] = NULL;
477  return;
478  }
479 
480  saveLicsPtr = strcpy(saveLics, cur.compLic);
481 
482  cur.tmpLics = strtok_r(saveLicsPtr, ",", &saveptr);
483 
484  cur.licenseList[numLics] = cur.tmpLics;
485  numLics++;
486 
487  saveLicsPtr = NULL;
488  while (cur.tmpLics)
489  {
490  cur.tmpLics = strtok_r(saveLicsPtr, ",", &saveptr);
491  if (cur.tmpLics == NULL)
492  {
493  break;
494  }
495  cur.licenseList[numLics] = cur.tmpLics;
496  numLics++;
497  }
498  cur.licenseList[numLics] = NULL;
499  numLics++;
500 
501  /*
502  int i;
503  for(i=0; i<numLics; i++){
504  printf("cur.licenseList[%d] is:%s\n",i,cur.licenseList[i]);
505  }
506 
507  printf("parseLicenseList: returning\n");
508  */
509 
510  return;
511 } /* parseLicenseList */
512 
517 FUNCTION void Usage(char *Name)
518 {
519  printf("Usage: %s [options] [file [file [...]]\n", Name);
520  printf(" -h :: help (print this message), then exit.\n");
521  printf(" -i :: initialize the database, then exit.\n");
522  printf(" -c :: specify the directory for the system configuration.\n");
523  printf(" -l :: print full file path (command line only).\n");
524  printf(" -v :: verbose (-vv = more verbose)\n");
525  printf(" -J :: output in JSON\n");
526  printf(" -S :: print Highlightinfo to stdout \n");
527  printf(" file :: if files are listed, print the licenses detected within them.\n");
528  printf(" no file :: process data from the scheduler.\n");
529  printf(" -V :: print the version info, then exit.\n");
530  printf(" -d :: specify a directory to scan.\n");
531  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");
532 } /* Usage() */
533 
541 FUNCTION void Bail(int exitval)
542 {
543 #ifdef PROC_TRACE
544  traceFunc("== Bail(%d)\n", exitval);
545 #endif /* PROC_TRACE */
546 
547 #if defined(MEMORY_TRACING) && defined(MEM_ACCT)
548  if (exitval)
549  {
550  memCacheDump("Mem-cache @ Bail() time:");
551  }
552 #endif /* MEMORY_TRACING && MEM_ACCT */
553 
554  /* close database and scheduler connections */
555  if (gl.pgConn) {
557  PQfinish(gl.pgConn);
558  }
559  fo_scheduler_disconnect(exitval);
560  exit(exitval);
561 }
562 
568 FUNCTION int optionIsSet(int val)
569 {
570 #ifdef PROC_TRACE
571  traceFunc("== optionIsSet(%x)\n", val);
572 #endif /* PROC_TRACE */
573 
574  return (gl.progOpts & val);
575 } /* optionIsSet */
576 
588 FUNCTION void getFileLists(char *dirpath)
589 {
590 #ifdef PROC_TRACE
591  traceFunc("== getFileLists(%s)\n", dirpath);
592 #endif /* PROC_TRACE */
593 
594  /* listInit(&gl.sarchList, 0, "source-archives list & md5sum map"); */
595  listInit(&cur.regfList, 0, "regular-files list");
596  listInit(&cur.offList, 0, "buffer-offset list");
597 #ifdef FLAG_NO_COPYRIGHT
598  listInit(&gl.nocpyrtList, 0, "no-copyright list");
599 #endif /* FLAG_NO_COPYRIGHT */
600 
601  listGetItem(&cur.regfList, cur.targetFile);
602  return;
603 } /* getFileLists */
604 
614 FUNCTION long updateLicenseFile(long rfPk)
615 {
616 
617  PGresult *result;
618 
619  if (rfPk <= 0)
620  {
621  return (-2);
622  }
623 
624  /* If files are coming from command line instead of fossology repo,
625  then there are no pfiles. So don't update the db
626  */
627  if (cur.cliMode == 1)
628  return (-1);
629 
630  result = fo_dbManager_ExecPrepared(
631  fo_dbManager_PrepareStamement(
632  gl.dbManager,
633  "updateLicenseFile",
634  "INSERT INTO license_file(rf_fk, agent_fk, pfile_fk) VALUES($1, $2, $3) RETURNING fl_pk",
635  long, int, long
636  ),
637  rfPk, gl.agentPk, cur.pFileFk
638  );
639 
640  if (result) {
641  long licenseFileId = -1;
642  if (PQntuples(result) > 0) {
643  licenseFileId = atol(PQgetvalue(result, 0, 0));
644  }
645 
646  PQclear(result);
647  return (licenseFileId);
648  } else {
649  return (-1);
650  }
651 } /* updateLicenseFile */
652 
660 FUNCTION char convertIndexToHighlightType(int index)
661 {
662 
663  char type;
664 
665  if ((index >= _KW_first) && (index <= _KW_last))
666  type = 'K';
667  else if (index > _KW_last)
668  type = 'L';
669  else type = '0';
670 
671  return type;
672 
673 }
674 
685 
686  /* If files are coming from command line instead of fossology repo,
687  then there are no pfiles. So don't update the db
688 
689  Also if we specifically do not want highlight information in the
690  database skip this function
691  */
692  if(cur.cliMode == 1 || optionIsSet(OPTS_NO_HIGHLIGHTINFO ) ){
693  return (TRUE);
694  }
695  PGresult *result;
696 
697 
698 
699 
700 #ifdef GLOBAL_DEBUG
701  printf("%s %s %i \n", cur.filePath,cur.compLic , cur.theMatches->len);
702 #endif
703 
704  // This speeds up the writing to the database and ensures that we have either full highlight information or none
705  PGresult* begin1 = PQexec(gl.pgConn, "BEGIN");
706  PQclear(begin1);
707 
708  fo_dbManager_PreparedStatement* preparedKeywords;
709  if(cur.keywordPositions->len > 0 ) {
710  preparedKeywords = fo_dbManager_PrepareStamement(
711  gl.dbManager,
712  "updateLicenseHighlighting:keyword",
713  "INSERT INTO highlight_keyword (pfile_fk, start, len) VALUES($1, $2, $3)",
714  long, int, int
715  );
716  }
717  int i;
718  for (i = 0; i < cur.keywordPositions->len; ++i)
719  {
721  result = fo_dbManager_ExecPrepared(
722  preparedKeywords,
723  cur.pFileFk, ourMatchv->start, ourMatchv->end - ourMatchv->start);
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  result = fo_dbManager_ExecPrepared(
758  preparedLicenses,
759  ourLicence->licenseFileId,
760  ourMatchv->start, ourMatchv->end - ourMatchv->start
761  );
762  if (result == NULL)
763  {
764  return (FALSE);
765  } else {
766  PQclear(result);
767  }
768  }
769  }
770 
771  PGresult* commit2 = PQexec(gl.pgConn, "COMMIT");
772  PQclear(commit2);
773  return (TRUE);
774 } /* updateLicenseHighlighting */
775 
776 
782 FUNCTION void processFile(char *fileToScan)
783 {
784 
785  char *pathcopy;
786 #ifdef PROC_TRACE
787  traceFunc("== processFile(%s)\n", fileToScan);
788 #endif /* PROC_TRACE */
789 
790  /* printf(" LOG: nomos scanning file %s.\n", fileToScan); DEBUG */
791 
792  (void) strcpy(cur.cwd, gl.initwd);
793 
794  strcpy(cur.filePath, fileToScan);
795  pathcopy = g_strdup(fileToScan);
796  strcpy(cur.targetDir, dirname(pathcopy));
797  g_free(pathcopy);
798  strcpy(cur.targetFile, fileToScan);
799  cur.targetLen = strlen(cur.targetDir);
800 
801  if (!isFILE(fileToScan))
802  {
803  LOG_FATAL("\"%s\" is not a plain file", fileToScan)
804  Bail(-__LINE__);
805  }
806 
807  getFileLists(cur.targetDir);
808  listInit(&cur.fLicFoundMap, 0, "file-license-found map");
809  listInit(&cur.parseList, 0, "license-components list");
810  listInit(&cur.lList, 0, "license-list");
811 
813 
814  /* freeAndClearScan(&cur); */
815 } /* Process File */
816 
822 void setLicenseFileIdInHiglightArray(long licenseFileId, char* licenseName){
823  int i;
824  for (i = 0; i < cur.theMatches->len; ++i) {
826  if (strcmp(licenseName, ourLicence->licenceName) == 0)
827  ourLicence->licenseFileId = licenseFileId;
828  }
829 } /* setLicenseFileIdInHiglightArray */
830 
837 int updateLicenseFileAndHighlightArray(char* licenseName, cacheroot_t* pcroot) {
838  long rf_pk = get_rfpk(pcroot, licenseName);
839  long licenseFileId = updateLicenseFile(rf_pk);
840  if (licenseFileId > 0) {
841  setLicenseFileIdInHiglightArray(licenseFileId, licenseName);
842  return (true);
843  } else {
844  return (false);
845  }
846 }
847 
858 FUNCTION int recordScanToDB(cacheroot_t *pcroot, struct curScan *scanRecord)
859 {
860 
861  char *noneFound;
862  int numLicenses;
863 
864 #ifdef SIMULATESCHED
865  /* BOBG: This allows a developer to simulate the scheduler
866  with a load file for testing/debugging, without updating the
867  database. Like:
868  cat myloadfile | ./nomos
869  myloadfile is same as what scheduler sends:
870  pfile_pk=311667 pfilename=9A96127E7D3B2812B50BF7732A2D0FF685EF6D6A.78073D1CA7B4171F8AFEA1497E4C6B33.183
871  pfile_pk=311727 pfilename=B7F5EED9ECB679EE0F980599B7AA89DCF8FA86BD.72B00E1B419D2C83D1050C66FA371244.368
872  etc.
873  */
874  printf("%s\n",scanRecord->compLic);
875  return(0);
876 #endif
877 
878  noneFound = strstr(scanRecord->compLic, LS_NONE);
879  if (noneFound != NULL)
880  {
881  if (!updateLicenseFileAndHighlightArray("No_license_found", pcroot))
882  return (-1);
883  return (0);
884  }
885 
886  /* we have one or more license names, parse them */
888  /* loop through the found license names */
889  for (numLicenses = 0; cur.licenseList[numLicenses] != NULL; numLicenses++)
890  {
891  if (!updateLicenseFileAndHighlightArray(cur.licenseList[numLicenses], pcroot))
892  return (-1);
893  }
894 
895  if (updateLicenseHighlighting(pcroot) == FALSE)
896  {
897  printf("Failure in update of highlight table \n");
898  }
899 
900  return (0);
901 } /* recordScanToDb */
902 
910  int index)
911 {
912  return &g_array_index(in, MatchPositionAndType, index);
913 }
914 
922  GArray* in, int index)
923 {
924  return &g_array_index(in, LicenceAndMatchPositions, index);
925 }
926 
934 FUNCTION void initializeCurScan(struct curScan* cur)
935 {
936  cur->indexList = g_array_new(FALSE, FALSE, sizeof(int));
937  cur->theMatches = g_array_new(FALSE, FALSE, sizeof(LicenceAndMatchPositions));
938  cur->keywordPositions = g_array_new(FALSE, FALSE, sizeof(MatchPositionAndType));
939  cur->docBufferPositionsAndOffsets = g_array_new(FALSE, FALSE, sizeof(pairPosOff));
940  cur->currentLicenceIndex=-1;
941 }
942 
943 
949 FUNCTION void freeAndClearScan(struct curScan *thisScan)
950 {
951  /*
952  Clear lists
953  */
954  listClear(&thisScan->regfList, DEALLOC_LIST);
955  listClear(&thisScan->offList, DEALLOC_LIST);
956  listClear(&thisScan->fLicFoundMap, DEALLOC_LIST);
957  listClear(&thisScan->parseList, DEALLOC_LIST);
958  listClear(&thisScan->lList, DEALLOC_LIST);
959  g_array_free(thisScan->indexList,TRUE);
960  cleanTheMatches(thisScan->theMatches);
961  g_array_free(thisScan->keywordPositions, TRUE);
962  g_array_free(thisScan->docBufferPositionsAndOffsets, TRUE);
963 
964 
965  /* remove keys, data and hash table */
966  hdestroy();
967 
968 }
969 
974 FUNCTION inline void cleanTheMatches(GArray* theMatches){
975 
976  int i;
977  for(i=0; i< theMatches->len; ++i) {
979  }
980  g_array_free( theMatches, TRUE);
981 }
982 
988 {
989  if(in->licenceName) g_free(in->licenceName);
990  g_array_free(in->matchPositions, TRUE);
991  g_array_free(in->indexList,TRUE);
992 }
993 
999 FUNCTION inline void addLicence(GArray* theMatches, char* licenceName ) {
1000  LicenceAndMatchPositions newMatch;
1001  newMatch.indexList = cur.indexList;
1002  cur.indexList=g_array_new(FALSE, FALSE, sizeof(int));
1003 
1005  newMatch.matchPositions = g_array_new(FALSE, FALSE, sizeof(MatchPositionAndType));
1006  newMatch.licenceName = g_strdup(licenceName);
1007  newMatch.licenseFileId = -1; //initial Value <- check if it was set
1008  g_array_append_val(theMatches , newMatch);
1009 }
1010 
1014 inline void cleanLicenceBuffer(){
1015  g_array_set_size(cur.indexList, 0);
1016 }
1017 
1023  if(cur.indexList->len>0)
1024  g_array_remove_index(cur.indexList, cur.indexList->len -1);
1025  return true;
1026 }
1027 
1028 
1029 
1030 
int isFILE(char *pathname)
Check if an inode is a file.
Definition: util.c:1352
FUNCTION int recordScanToDB(cacheroot_t *pcroot, struct curScan *scanRecord)
Write out the information about the scan to the FOSSology database.
Definition: nomos_utils.c:858
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
Definition: libfossdb.c:181
int maxnodes
No. of nodes in the list.
Definition: liccache.h:53
char * licenseList[512]
Definition: nomos.h:427
char filePath[myBUFSIZ]
Definition: nomos.h:408
cachenode_t * nodes
Array of nodes.
Definition: liccache.h:54
int cliMode
Definition: nomos.h:425
char targetFile[myBUFSIZ]
Definition: nomos.h:407
FUNCTION void getFileLists(char *dirpath)
Initialize the lists: regular-files list cur.regfList and buffer-offset list cur.offList.
Definition: nomos_utils.c:588
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:121
int updateLicenseFileAndHighlightArray(char *licenseName, cacheroot_t *pcroot)
Add a license to hash table, license table and highlight array.
Definition: nomos_utils.c:837
FUNCTION void lrcache_print(cacheroot_t *pcroot)
Print the contents of the hash table.
Definition: nomos_utils.c:141
FUNCTION void processFile(char *fileToScan)
process a single file
Definition: nomos_utils.c:782
FUNCTION void parseLicenseList()
parse the comma separated list of license names found
Definition: nomos_utils.c:458
FUNCTION void cleanLicenceAndMatchPositions(LicenceAndMatchPositions *in)
Cleans the license and match positions object and free the memory.
Definition: nomos_utils.c:987
PGresult * fo_dbManager_ExecPrepared(fo_dbManager_PreparedStatement *preparedStatement,...)
Execute a prepared statement.
Definition: standalone.c:31
int s
The socket that the CLI will use to communicate.
Definition: fo_cli.c:48
GArray * matchPositions
Match positions.
Definition: nomos.h:392
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
void processRawSource()
Definition: process.c:125
int start
Start position of match.
Definition: nomos.h:383
FUNCTION int optionIsSet(int val)
Check if an CLI option is set.
Definition: nomos_utils.c:568
void listInit(list_t *l, int size, char *label)
intialize a list, if the list is not empty, empty it (initialize it to zero&#39;s).
Definition: list.c:67
char initwd[myBUFSIZ]
CDB, would like to workaround/eliminate.
Definition: nomos.h:358
FUNCTION MatchPositionAndType * getMatchfromHighlightInfo(GArray *in, int index)
Get the MatchPositionAndType for a given index in highlight array.
Definition: nomos_utils.c:909
GArray * theMatches
Definition: nomos.h:430
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:259
fo_dbManager * dbManager
FOSSology DB manager.
Definition: nomos.h:376
FUNCTION int updateLicenseHighlighting(cacheroot_t *pcroot)
insert rf_fk, agent_fk, offset, len and type into highlight table
Definition: nomos_utils.c:684
bool clearLastElementOfLicenceBuffer()
Remove the last element from license buffer.
Definition: nomos_utils.c:1022
int progOpts
CLI options.
Definition: nomos.h:360
FUNCTION void addLicence(GArray *theMatches, char *licenceName)
Add a license to the matches array.
Definition: nomos_utils.c:999
FUNCTION void cleanTheMatches(GArray *theMatches)
Cleans the match array and free the memory.
Definition: nomos_utils.c:974
FUNCTION LicenceAndMatchPositions * getLicenceAndMatchPositions(GArray *in, int index)
Get the LicenceAndMatchPositions for a given index in match array.
Definition: nomos_utils.c:921
void setLicenseFileIdInHiglightArray(long licenseFileId, char *licenseName)
Set the license file id to the highlights.
Definition: nomos_utils.c:822
char * licenceName
License names.
Definition: nomos.h:394
FUNCTION void Bail(int exitval)
Close connections and exit.
Definition: nomos_utils.c:541
FUNCTION void freeAndClearScan(struct curScan *thisScan)
Clean-up all the per scan data structures, freeing any old data.
Definition: nomos_utils.c:949
FUNCTION long updateLicenseFile(long rfPk)
insert rf_fk, agent_fk and pfile_fk into license_file table
Definition: nomos_utils.c:614
int end
End position of match.
Definition: nomos.h:384
char * rf_shortname
License shortname.
Definition: liccache.h:42
char compLic[myBUFSIZ]
Definition: nomos.h:422
char * tmpLics
Definition: nomos.h:426
#define LS_NONE
Definition: nomos.h:228
FUNCTION long add2license_ref(char *licenseName)
Add a new license to license_ref table.
Definition: nomos_utils.c:42
GArray * indexList
Definition: nomos.h:429
Nomos header file.
GArray * indexList
License indexes.
Definition: nomos.h:393
void fo_dbManager_free(fo_dbManager *dbManager)
Un-allocate the memory from a DB manager.
Definition: standalone.c:29
FUNCTION char convertIndexToHighlightType(int index)
Return the highlight type (K|L|0) for a given index.
Definition: nomos_utils.c:660
void cleanLicenceBuffer()
Clean the license buffer.
Definition: nomos_utils.c:1014
char targetDir[myBUFSIZ]
Definition: nomos.h:406
char cwd[myBUFSIZ]
Definition: nomos.h:405
FUNCTION void lrcache_free(cacheroot_t *pcroot)
free the hash table
Definition: nomos_utils.c:166
FUNCTION long get_rfpk(cacheroot_t *pcroot, char *rf_shortname)
Get the rf_pk for rf_shortname.
Definition: nomos_utils.c:308
void listClear(list_t *l, int deallocFlag)
Destroy list_t.
Definition: list.c:119
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:230
FUNCTION void Usage(char *Name)
Print nomos usage help.
Definition: nomos_utils.c:517
long rf_pk
License id from database.
Definition: liccache.h:43
Struct that tracks state related to current file being scanned.
Definition: nomos.h:404
GArray * keywordPositions
Definition: nomos.h:431
FUNCTION char * getFieldValue(char *inStr, char *field, int fieldMax, char *value, int valueMax, char separator)
Given a string that contains field=&#39;value&#39; pairs, save the items.
Definition: nomos_utils.c:341
int agentPk
Agent id.
Definition: nomos.h:372
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:193
long pFileFk
Definition: nomos.h:409
char saveLics[myBUFSIZ]
License string.
Definition: nomos.h:161
int licenseFileId
PFile id.
Definition: nomos.h:395
FUNCTION int initLicRefCache(cacheroot_t *pcroot)
build a cache the license ref db table.
Definition: nomos_utils.c:268
FUNCTION void initializeCurScan(struct curScan *cur)
Initialize the scanner.
Definition: nomos_utils.c:934
PGconn * pgConn
DB Connection.
Definition: nomos.h:375