23 #include "nomos_utils.h" 47 char insert[myBUFSIZ];
48 char escLicName[myBUFSIZ];
49 char *specialLicenseText;
57 len = strlen(licenseName);
58 PQescapeStringConn(gl.
pgConn, escLicName, licenseName, len, &error);
60 LOG_WARNING(
"Does license name %s have multibyte encoding?", licenseName)
63 sprintf(query,
"SELECT rf_pk FROM " LICENSE_REF_TABLE
" where rf_shortname='%s'", escLicName);
64 result = PQexec(gl.
pgConn, query);
67 numRows = PQntuples(result);
70 rf_pk = atol(PQgetvalue(result, 0, 0));
77 specialLicenseText =
"License by Nomos.";
79 sprintf(insert,
"insert into license_ref(rf_shortname, rf_text, rf_detector_type) values('%s', '%s', 2)", escLicName,
81 result = PQexec(gl.
pgConn, insert);
84 || ((PQresultStatus(result) != PGRES_COMMAND_OK)
85 && (strncmp(PG_ERRCODE_UNIQUE_VIOLATION, PQresultErrorField(result, PG_DIAG_SQLSTATE), 5))))
87 printf(
"ERROR: %s(%d): Nomos failed to add a new license. %s/n: %s/n",
88 __FILE__, __LINE__, PQresultErrorMessage(result), insert);
95 result = PQexec(gl.
pgConn, query);
98 numRows = PQntuples(result);
100 rf_pk = atol(PQgetvalue(result, 0, 0));
103 printf(
"ERROR: %s:%s:%d Just inserted value is missing. On: %s", __FILE__,
"add2license_ref()", __LINE__, query);
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;
147 pcnode = pcroot->
nodes;
148 for (i = 0; i < pcroot->
maxnodes; i++)
150 if (pcnode->
rf_pk != 0L)
171 pcnode = pcroot->
nodes;
172 for (i = 0; i < pcroot->
maxnodes; i++)
174 if (pcnode->
rf_pk != 0L)
203 for (i = 0; i < pcroot->
maxnodes; i++)
205 noden = (hashval + i) & (pcroot->
maxnodes - 1);
207 pcnode = pcroot->
nodes + noden;
211 pcnode->
rf_pk = rf_pk;
215 if (i < pcroot->maxnodes)
240 for (i = 0; i < pcroot->
maxnodes; i++)
242 noden = (hashval + i) & (pcroot->
maxnodes - 1);
244 pcnode = pcroot->
nodes + noden;
249 return pcnode->
rf_pk;
272 char query[myBUFSIZ];
279 sprintf(query,
"SELECT rf_pk, rf_shortname FROM " LICENSE_REF_TABLE
" where rf_detector_type=2");
280 result = PQexec(gl.
pgConn, query);
284 numLics = PQntuples(result);
286 for (row = 0; row < numLics; row++)
288 lrcache_add(pcroot, atol(PQgetvalue(result, row, 0)), PQgetvalue(result, row, 1));
313 if ((len = strlen(rf_shortname)) == 0)
315 printf(
"ERROR! Nomos.c get_rfpk() passed empty name");
342 char *value,
int valueMax,
char separator)
350 traceFunc(
"== getFieldValue(inStr= %s fieldMax= %d separator= '%c'\n",
351 inStr, fieldMax, separator);
354 memset(field, 0, fieldMax);
355 memset(value, 0, valueMax);
358 while (isspace(inStr[0]))
363 if (inStr[0] ==
'\0')
371 for (s = 0; (inStr[
s] !=
'\0') && !isspace(inStr[s]) && (inStr[
s] !=
'='); s++)
373 field[f++] = inStr[
s];
377 while (isspace(inStr[s]))
382 if (inStr[s] != separator)
386 if (inStr[s] ==
'\0')
394 while (isspace(inStr[s]))
398 if (inStr[s] ==
'\0')
404 if ((inStr[s] ==
'\'') || (inStr[
s] ==
'"'))
408 if (inStr[s] ==
'\0')
416 for (; (inStr[
s] !=
'\0') && (inStr[s] != gotQuote); s++)
418 if (inStr[s] ==
'\\')
420 value[v++] = inStr[++
s];
424 value[v++] = inStr[
s];
431 for (; (inStr[
s] !=
'\0') && !isspace(inStr[s]); s++)
433 if (inStr[s] ==
'\\')
435 value[v++] = inStr[++
s];
439 value[v++] = inStr[
s];
444 while (isspace(inStr[s]))
467 if ((strlen(cur.
compLic)) == 0)
473 if (strstr(cur.
compLic,
",") == NULL)
482 cur.
tmpLics = strtok_r(saveLicsPtr,
",", &saveptr);
490 cur.
tmpLics = strtok_r(saveLicsPtr,
",", &saveptr);
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");
541 FUNCTION
void Bail(
int exitval)
544 traceFunc(
"== Bail(%d)\n", exitval);
547 #if defined(MEMORY_TRACING) && defined(MEM_ACCT) 550 memCacheDump(
"Mem-cache @ Bail() time:");
571 traceFunc(
"== optionIsSet(%x)\n", val);
591 traceFunc(
"== getFileLists(%s)\n", dirpath);
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");
631 fo_dbManager_PrepareStamement(
634 "INSERT INTO license_file(rf_fk, agent_fk, pfile_fk) VALUES($1, $2, $3) RETURNING fl_pk",
641 long licenseFileId = -1;
642 if (PQntuples(result) > 0) {
643 licenseFileId = atol(PQgetvalue(result, 0, 0));
647 return (licenseFileId);
665 if ((index >= _KW_first) && (index <= _KW_last))
667 else if (index > _KW_last)
705 PGresult* begin1 = PQexec(gl.
pgConn,
"BEGIN");
710 preparedKeywords = fo_dbManager_PrepareStamement(
712 "updateLicenseHighlighting:keyword",
713 "INSERT INTO highlight_keyword (pfile_fk, start, len) VALUES($1, $2, $3)",
729 PGresult* commit1 = PQexec(gl.
pgConn,
"COMMIT");
732 PGresult* begin2 =PQexec(gl.
pgConn,
"BEGIN");
737 preparedLicenses=fo_dbManager_PrepareStamement(
739 "updateLicenseHighlighting",
740 "INSERT INTO highlight (fl_fk, start, len, type) VALUES($1, $2, $3,'L')",
771 PGresult* commit2 = PQexec(gl.
pgConn,
"COMMIT");
787 traceFunc(
"== processFile(%s)\n", fileToScan);
795 pathcopy = g_strdup(fileToScan);
796 strcpy(cur.
targetDir, dirname(pathcopy));
803 LOG_FATAL(
"\"%s\" is not a plain file", fileToScan)
808 listInit(&cur.fLicFoundMap, 0,
"file-license-found map");
809 listInit(&cur.parseList, 0,
"license-components list");
810 listInit(&cur.lList, 0,
"license-list");
826 if (strcmp(licenseName, ourLicence->
licenceName) == 0)
838 long rf_pk =
get_rfpk(pcroot, licenseName);
840 if (licenseFileId > 0) {
874 printf(
"%s\n",scanRecord->
compLic);
879 if (noneFound != NULL)
889 for (numLicenses = 0; cur.
licenseList[numLicenses] != NULL; numLicenses++)
897 printf(
"Failure in update of highlight table \n");
922 GArray* in,
int index)
936 cur->
indexList = g_array_new(FALSE, FALSE,
sizeof(
int));
939 cur->docBufferPositionsAndOffsets = g_array_new(FALSE, FALSE,
sizeof(
pairPosOff));
940 cur->currentLicenceIndex=-1;
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);
962 g_array_free(thisScan->docBufferPositionsAndOffsets, TRUE);
977 for(i=0; i< theMatches->len; ++i) {
980 g_array_free( theMatches, TRUE);
999 FUNCTION
inline void addLicence(GArray* theMatches,
char* licenceName ) {
1002 cur.
indexList=g_array_new(FALSE, FALSE,
sizeof(
int));
1008 g_array_append_val(theMatches , newMatch);
int isFILE(char *pathname)
Check if an inode is a file.
FUNCTION int recordScanToDB(cacheroot_t *pcroot, struct curScan *scanRecord)
Write out the information about the scan to the FOSSology database.
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
int maxnodes
No. of nodes in the list.
cachenode_t * nodes
Array of nodes.
char targetFile[myBUFSIZ]
FUNCTION void getFileLists(char *dirpath)
Initialize the lists: regular-files list cur.regfList and buffer-offset list cur.offList.
FUNCTION long lrcache_hash(cacheroot_t *pcroot, char *rf_shortname)
calculate the hash of an rf_shortname rf_shortname is the key
int updateLicenseFileAndHighlightArray(char *licenseName, cacheroot_t *pcroot)
Add a license to hash table, license table and highlight array.
FUNCTION void lrcache_print(cacheroot_t *pcroot)
Print the contents of the hash table.
FUNCTION void processFile(char *fileToScan)
process a single file
FUNCTION void parseLicenseList()
parse the comma separated list of license names found
FUNCTION void cleanLicenceAndMatchPositions(LicenceAndMatchPositions *in)
Cleans the license and match positions object and free the memory.
PGresult * fo_dbManager_ExecPrepared(fo_dbManager_PreparedStatement *preparedStatement,...)
Execute a prepared statement.
int s
The socket that the CLI will use to communicate.
GArray * matchPositions
Match positions.
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
int start
Start position of match.
FUNCTION int optionIsSet(int val)
Check if an CLI option is set.
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).
char initwd[myBUFSIZ]
CDB, would like to workaround/eliminate.
FUNCTION MatchPositionAndType * getMatchfromHighlightInfo(GArray *in, int index)
Get the MatchPositionAndType for a given index in highlight array.
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...
fo_dbManager * dbManager
FOSSology DB manager.
FUNCTION int updateLicenseHighlighting(cacheroot_t *pcroot)
insert rf_fk, agent_fk, offset, len and type into highlight table
bool clearLastElementOfLicenceBuffer()
Remove the last element from license buffer.
FUNCTION void addLicence(GArray *theMatches, char *licenceName)
Add a license to the matches array.
FUNCTION void cleanTheMatches(GArray *theMatches)
Cleans the match array and free the memory.
FUNCTION LicenceAndMatchPositions * getLicenceAndMatchPositions(GArray *in, int index)
Get the LicenceAndMatchPositions for a given index in match array.
void setLicenseFileIdInHiglightArray(long licenseFileId, char *licenseName)
Set the license file id to the highlights.
char * licenceName
License names.
FUNCTION void Bail(int exitval)
Close connections and exit.
FUNCTION void freeAndClearScan(struct curScan *thisScan)
Clean-up all the per scan data structures, freeing any old data.
FUNCTION long updateLicenseFile(long rfPk)
insert rf_fk, agent_fk and pfile_fk into license_file table
int end
End position of match.
char * rf_shortname
License shortname.
FUNCTION long add2license_ref(char *licenseName)
Add a new license to license_ref table.
GArray * indexList
License indexes.
void fo_dbManager_free(fo_dbManager *dbManager)
Un-allocate the memory from a DB manager.
FUNCTION char convertIndexToHighlightType(int index)
Return the highlight type (K|L|0) for a given index.
void cleanLicenceBuffer()
Clean the license buffer.
FUNCTION void lrcache_free(cacheroot_t *pcroot)
free the hash table
FUNCTION long get_rfpk(cacheroot_t *pcroot, char *rf_shortname)
Get the rf_pk for rf_shortname.
void listClear(list_t *l, int deallocFlag)
Destroy list_t.
FUNCTION long lrcache_lookup(cacheroot_t *pcroot, char *rf_shortname)
lookup rf_pk in the license_ref cache rf_shortname is the key
FUNCTION void Usage(char *Name)
Print nomos usage help.
long rf_pk
License id from database.
Struct that tracks state related to current file being scanned.
GArray * keywordPositions
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.
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
char saveLics[myBUFSIZ]
License string.
int licenseFileId
PFile id.
FUNCTION int initLicRefCache(cacheroot_t *pcroot)
build a cache the license ref db table.
FUNCTION void initializeCurScan(struct curScan *cur)
Initialize the scanner.
PGconn * pgConn
DB Connection.