11 #include "nomos_utils.h"
39 char insert[myBUFSIZ];
40 char escLicName[myBUFSIZ];
41 char *specialLicenseText;
49 len = strlen(licenseName);
50 PQescapeStringConn(gl.
pgConn, escLicName, licenseName, len, &error);
52 LOG_WARNING(
"Does license name %s have multibyte encoding?", licenseName)
55 snprintf(query, myBUFSIZ - 1,
"SELECT rf_pk FROM " LICENSE_REF_TABLE
" where rf_shortname='%s'", escLicName);
56 result = PQexec(gl.
pgConn, query);
59 numRows = PQntuples(result);
62 rf_pk = atol(PQgetvalue(result, 0, 0));
69 specialLicenseText =
"License by Nomos.";
71 snprintf(insert, myBUFSIZ - 1,
"insert into license_ref(rf_shortname, rf_text, rf_detector_type) values('%s', '%s', 2)", escLicName,
73 result = PQexec(gl.
pgConn, insert);
76 || ((PQresultStatus(result) != PGRES_COMMAND_OK)
77 && (strncmp(PG_ERRCODE_UNIQUE_VIOLATION, PQresultErrorField(result, PG_DIAG_SQLSTATE), 5))))
79 printf(
"ERROR: %s(%d): Nomos failed to add a new license. %s/n: %s/n",
80 __FILE__, __LINE__, PQresultErrorMessage(result), insert);
87 result = PQexec(gl.
pgConn, query);
90 numRows = PQntuples(result);
92 rf_pk = atol(PQgetvalue(result, 0, 0));
95 printf(
"ERROR: %s:%s:%d Just inserted value is missing. On: %s", __FILE__,
"add2license_ref()", __LINE__, query);
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;
139 pcnode = pcroot->
nodes;
140 for (i = 0; i < pcroot->
maxnodes; i++)
142 if (pcnode->
rf_pk != 0L)
163 pcnode = pcroot->
nodes;
164 for (i = 0; i < pcroot->
maxnodes; i++)
166 if (pcnode->
rf_pk != 0L)
195 for (i = 0; i < pcroot->
maxnodes; i++)
197 noden = (hashval + i) & (pcroot->
maxnodes - 1);
199 pcnode = pcroot->
nodes + noden;
203 pcnode->
rf_pk = rf_pk;
207 if (i < pcroot->maxnodes)
232 for (i = 0; i < pcroot->
maxnodes; i++)
234 noden = (hashval + i) & (pcroot->
maxnodes - 1);
236 pcnode = pcroot->
nodes + noden;
241 return pcnode->
rf_pk;
264 char query[myBUFSIZ];
271 sprintf(query,
"SELECT rf_pk, rf_shortname FROM " LICENSE_REF_TABLE
" where rf_detector_type=2");
272 result = PQexec(gl.
pgConn, query);
276 numLics = PQntuples(result);
278 for (row = 0; row < numLics; row++)
280 lrcache_add(pcroot, atol(PQgetvalue(result, row, 0)), PQgetvalue(result, row, 1));
305 if ((len = strlen(rf_shortname)) == 0)
307 printf(
"ERROR! Nomos.c get_rfpk() passed empty name");
334 char *value,
int valueMax,
char separator)
342 traceFunc(
"== getFieldValue(inStr= %s fieldMax= %d separator= '%c'\n",
343 inStr, fieldMax, separator);
346 memset(field, 0, fieldMax);
347 memset(value, 0, valueMax);
350 while (isspace(inStr[0]))
355 if (inStr[0] ==
'\0')
363 for (
s = 0; (inStr[
s] !=
'\0') && !isspace(inStr[
s]) && (inStr[
s] !=
'=');
s++)
365 field[f++] = inStr[
s];
369 while (isspace(inStr[
s]))
374 if (inStr[
s] != separator)
378 if (inStr[
s] ==
'\0')
386 while (isspace(inStr[
s]))
390 if (inStr[
s] ==
'\0')
396 if ((inStr[
s] ==
'\'') || (inStr[
s] ==
'"'))
400 if (inStr[
s] ==
'\0')
408 for (; (inStr[
s] !=
'\0') && (inStr[
s] != gotQuote);
s++)
410 if (inStr[
s] ==
'\\')
412 value[v++] = inStr[++
s];
416 value[v++] = inStr[
s];
423 for (; (inStr[
s] !=
'\0') && !isspace(inStr[
s]);
s++)
425 if (inStr[
s] ==
'\\')
427 value[v++] = inStr[++
s];
431 value[v++] = inStr[
s];
436 while (isspace(inStr[
s]))
459 if ((strlen(cur.
compLic)) == 0)
465 if (strstr(cur.
compLic,
",") == NULL)
474 cur.
tmpLics = strtok_r(saveLicsPtr,
",", &saveptr);
482 cur.
tmpLics = strtok_r(saveLicsPtr,
",", &saveptr);
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");
533 FUNCTION
void Bail(
int exitval)
536 traceFunc(
"== Bail(%d)\n", exitval);
539 #if defined(MEMORY_TRACING) && defined(MEM_ACCT)
542 memCacheDump(
"Mem-cache @ Bail() time:");
563 traceFunc(
"== optionIsSet(%x)\n", val);
583 traceFunc(
"== getFileLists(%s)\n", dirpath);
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");
623 fo_dbManager_PrepareStamement(
626 "INSERT INTO license_file(rf_fk, agent_fk, pfile_fk) VALUES($1, $2, $3) RETURNING fl_pk",
633 long licenseFileId = -1;
634 if (PQntuples(result) > 0) {
635 licenseFileId = atol(PQgetvalue(result, 0, 0));
639 return (licenseFileId);
657 if ((index >= _KW_first) && (index <= _KW_last))
659 else if (index > _KW_last)
697 PGresult* begin1 = PQexec(gl.
pgConn,
"BEGIN");
702 preparedKeywords = fo_dbManager_PrepareStamement(
704 "updateLicenseHighlighting:keyword",
705 "INSERT INTO highlight_keyword (pfile_fk, start, len) VALUES($1, $2, $3)",
721 PGresult* commit1 = PQexec(gl.
pgConn,
"COMMIT");
724 PGresult* begin2 =PQexec(gl.
pgConn,
"BEGIN");
729 preparedLicenses=fo_dbManager_PrepareStamement(
731 "updateLicenseHighlighting",
732 "INSERT INTO highlight (fl_fk, start, len, type) VALUES($1, $2, $3,'L')",
763 PGresult* commit2 = PQexec(gl.
pgConn,
"COMMIT");
779 traceFunc(
"== processFile(%s)\n", fileToScan);
787 pathcopy = g_strdup(fileToScan);
788 strcpy(cur.
targetDir, dirname(pathcopy));
795 LOG_FATAL(
"\"%s\" is not a plain file", fileToScan)
800 listInit(&cur.fLicFoundMap, 0,
"file-license-found map");
801 listInit(&cur.parseList, 0,
"license-components list");
802 listInit(&cur.lList, 0,
"license-list");
818 if (strcmp(licenseName, ourLicence->
licenceName) == 0)
830 long rf_pk =
get_rfpk(pcroot, licenseName);
832 if (licenseFileId > 0) {
866 printf(
"%s\n",scanRecord->
compLic);
871 if (noneFound != NULL)
881 for (numLicenses = 0; cur.
licenseList[numLicenses] != NULL; numLicenses++)
889 printf(
"Failure in update of highlight table \n");
914 GArray* in,
int index)
928 cur->
indexList = g_array_new(FALSE, FALSE,
sizeof(
int));
931 cur->docBufferPositionsAndOffsets = g_array_new(FALSE, FALSE,
sizeof(
pairPosOff));
932 cur->currentLicenceIndex=-1;
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);
954 g_array_free(thisScan->docBufferPositionsAndOffsets, TRUE);
969 for(i=0; i< theMatches->len; ++i) {
972 g_array_free( theMatches, TRUE);
981 if(in->licenceName) g_free(in->licenceName);
982 g_array_free(in->matchPositions, TRUE);
983 g_array_free(in->indexList,TRUE);
991 FUNCTION
inline void addLicence(GArray* theMatches,
char* licenceName ) {
994 cur.
indexList=g_array_new(FALSE, FALSE,
sizeof(
int));
1000 g_array_append_val(theMatches , newMatch);
int s
The socket that the CLI will use to communicate.
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
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.
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).
void listClear(list_t *l, int deallocFlag)
Destroy list_t.
PGresult * fo_dbManager_ExecPrepared(fo_dbManager_PreparedStatement *preparedStatement,...)
Execute a prepared statement.
void fo_dbManager_free(fo_dbManager *dbManager)
Un-allocate the memory from a DB manager.
int isFILE(char *pathname)
Check if an inode is a file.
FUNCTION void lrcache_print(cacheroot_t *pcroot)
Print the contents of the hash table.
FUNCTION void freeAndClearScan(struct curScan *thisScan)
Clean-up all the per scan data structures, freeing any old data.
FUNCTION void lrcache_free(cacheroot_t *pcroot)
free the hash table
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 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.
sem_t * mutexJson
Mutex to handle JSON writes.
FUNCTION void processFile(char *fileToScan)
process a single file
FUNCTION void cleanTheMatches(GArray *theMatches)
Cleans the match array and free the memory.
FUNCTION void parseLicenseList()
parse the comma separated list of license names found
FUNCTION long updateLicenseFile(long rfPk)
insert rf_fk, agent_fk and pfile_fk into license_file table
FUNCTION void initializeCurScan(struct curScan *cur)
Initialize the scanner.
FUNCTION int recordScanToDB(cacheroot_t *pcroot, struct curScan *scanRecord)
Write out the information about the scan to the FOSSology database.
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
void cleanLicenceBuffer()
Clean the license buffer.
FUNCTION void addLicence(GArray *theMatches, char *licenceName)
Add a license to the matches array.
FUNCTION MatchPositionAndType * getMatchfromHighlightInfo(GArray *in, int index)
Get the MatchPositionAndType for a given index in highlight array.
FUNCTION void getFileLists(char *dirpath)
Initialize the lists: regular-files list cur.regfList and buffer-offset list cur.offList.
FUNCTION LicenceAndMatchPositions * getLicenceAndMatchPositions(GArray *in, int index)
Get the LicenceAndMatchPositions for a given index in match array.
gboolean * printcomma
True to print comma while printing JSON object.
FUNCTION void cleanLicenceAndMatchPositions(LicenceAndMatchPositions *in)
Cleans the license and match positions object and free the memory.
FUNCTION int initLicRefCache(cacheroot_t *pcroot)
build a cache the license ref db table.
char saveLics[myBUFSIZ]
License string.
int updateLicenseFileAndHighlightArray(char *licenseName, cacheroot_t *pcroot)
Add a license to hash table, license table and highlight array.
FUNCTION int optionIsSet(int val)
Check if an CLI option is set.
bool clearLastElementOfLicenceBuffer()
Remove the last element from license buffer.
void setLicenseFileIdInHiglightArray(long licenseFileId, char *licenseName)
Set the license file id to the highlights.
FUNCTION long lrcache_hash(cacheroot_t *pcroot, char *rf_shortname)
calculate the hash of an rf_shortname rf_shortname is the key
FUNCTION void Bail(int exitval)
Close connections and exit.
FUNCTION char convertIndexToHighlightType(int index)
Return the highlight type (K|L|0) for a given index.
FUNCTION long get_rfpk(cacheroot_t *pcroot, char *rf_shortname)
Get the rf_pk for rf_shortname.
FUNCTION long add2license_ref(char *licenseName)
Add a new license to license_ref table.
FUNCTION int updateLicenseHighlighting(cacheroot_t *pcroot)
insert rf_fk, agent_fk, offset, len and type into highlight table
GArray * matchPositions
Match positions.
GArray * indexList
License indexes.
char * licenceName
License names.
int licenseFileId
PFile id.
int start
Start position of match.
int end
End position of match.
long rf_pk
License id from database.
char * rf_shortname
License shortname.
int maxnodes
No. of nodes in the list.
cachenode_t * nodes
Array of nodes.
Struct that tracks state related to current file being scanned.
GArray * keywordPositions
char targetFile[myBUFSIZ]
PGconn * pgConn
DB Connection.
fo_dbManager * dbManager
FOSSology DB manager.
char initwd[myBUFSIZ]
CDB, would like to workaround/eliminate.