FOSSology  4.4.0
Open Source License Compliance by Open Source Software
nomos_utils.c File Reference

Utilities used by nomos. More...

#include "nomos_utils.h"
#include "nomos.h"
Include dependency graph for nomos_utils.c:

Go to the source code of this file.

Functions

FUNCTION long add2license_ref (char *licenseName)
 Add a new license to license_ref table. More...
 
FUNCTION long lrcache_hash (cacheroot_t *pcroot, char *rf_shortname)
 calculate the hash of an rf_shortname rf_shortname is the key More...
 
FUNCTION void lrcache_print (cacheroot_t *pcroot)
 Print the contents of the hash table. More...
 
FUNCTION void lrcache_free (cacheroot_t *pcroot)
 free the hash table More...
 
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 More...
 
FUNCTION long lrcache_lookup (cacheroot_t *pcroot, char *rf_shortname)
 lookup rf_pk in the license_ref cache rf_shortname is the key More...
 
FUNCTION int initLicRefCache (cacheroot_t *pcroot)
 build a cache the license ref db table. More...
 
FUNCTION long get_rfpk (cacheroot_t *pcroot, char *rf_shortname)
 Get the rf_pk for rf_shortname. More...
 
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. More...
 
FUNCTION void parseLicenseList ()
 parse the comma separated list of license names found More...
 
FUNCTION void Usage (char *Name)
 Print nomos usage help. More...
 
FUNCTION void Bail (int exitval)
 Close connections and exit. More...
 
FUNCTION int optionIsSet (int val)
 Check if an CLI option is set. More...
 
FUNCTION void getFileLists (char *dirpath)
 Initialize the lists: regular-files list cur.regfList and buffer-offset list cur.offList. More...
 
FUNCTION long updateLicenseFile (long rfPk)
 insert rf_fk, agent_fk and pfile_fk into license_file table More...
 
FUNCTION char convertIndexToHighlightType (int index)
 Return the highlight type (K|L|0) for a given index. More...
 
FUNCTION int updateLicenseHighlighting (cacheroot_t *pcroot)
 insert rf_fk, agent_fk, offset, len and type into highlight table More...
 
FUNCTION void processFile (char *fileToScan)
 process a single file More...
 
void setLicenseFileIdInHiglightArray (long licenseFileId, char *licenseName)
 Set the license file id to the highlights. More...
 
int updateLicenseFileAndHighlightArray (char *licenseName, cacheroot_t *pcroot)
 Add a license to hash table, license table and highlight array. More...
 
FUNCTION int recordScanToDB (cacheroot_t *pcroot, struct curScan *scanRecord)
 Write out the information about the scan to the FOSSology database. More...
 
FUNCTION MatchPositionAndTypegetMatchfromHighlightInfo (GArray *in, int index)
 Get the MatchPositionAndType for a given index in highlight array. More...
 
FUNCTION LicenceAndMatchPositionsgetLicenceAndMatchPositions (GArray *in, int index)
 Get the LicenceAndMatchPositions for a given index in match array. More...
 
FUNCTION void initializeCurScan (struct curScan *cur)
 Initialize the scanner. More...
 
FUNCTION void freeAndClearScan (struct curScan *thisScan)
 Clean-up all the per scan data structures, freeing any old data. More...
 
FUNCTION void cleanTheMatches (GArray *theMatches)
 Cleans the match array and free the memory. More...
 
FUNCTION void cleanLicenceAndMatchPositions (LicenceAndMatchPositions *in)
 Cleans the license and match positions object and free the memory. More...
 
FUNCTION void addLicence (GArray *theMatches, char *licenceName)
 Add a license to the matches array. More...
 
void cleanLicenceBuffer ()
 Clean the license buffer.
 
bool clearLastElementOfLicenceBuffer ()
 Remove the last element from license buffer. More...
 

Variables

sem_t * mutexJson
 Mutex to handle JSON writes.
 
gboolean * printcomma
 True to print comma while printing JSON object.
 
char saveLics [myBUFSIZ]
 License string.
 

Detailed Description

Utilities used by nomos.

Definition in file nomos_utils.c.

Function Documentation

◆ add2license_ref()

FUNCTION long add2license_ref ( char *  licenseName)

Add a new license to license_ref table.

Adds a license to license_ref table.

Parameters
licenseNameName of license
Returns
rf_pk for success, 0 for failure

Definition at line 34 of file nomos_utils.c.

◆ addLicence()

FUNCTION void addLicence ( GArray *  theMatches,
char *  licenceName 
)
inline

Add a license to the matches array.

Parameters
[in,out]theMatchesThe matches array
[in]licenceNameLicense to be added

fill this later

Definition at line 991 of file nomos_utils.c.

◆ Bail()

FUNCTION void Bail ( int  exitval)

Close connections and exit.

The function closes DB and scheduler connections and calls exit() with the return code passed

Parameters
exitvalReturn code to pass to exit()

Definition at line 533 of file nomos_utils.c.

◆ cleanLicenceAndMatchPositions()

FUNCTION void cleanLicenceAndMatchPositions ( LicenceAndMatchPositions in)
inline

Cleans the license and match positions object and free the memory.

Parameters
inThe matches object

Definition at line 979 of file nomos_utils.c.

◆ cleanTheMatches()

FUNCTION void cleanTheMatches ( GArray *  theMatches)
inline

Cleans the match array and free the memory.

Parameters
theMatchesThe matches list

Definition at line 966 of file nomos_utils.c.

◆ clearLastElementOfLicenceBuffer()

bool clearLastElementOfLicenceBuffer ( )
inline

Remove the last element from license buffer.

Returns
True always

Definition at line 1014 of file nomos_utils.c.

◆ convertIndexToHighlightType()

FUNCTION char convertIndexToHighlightType ( int  index)

Return the highlight type (K|L|0) for a given index.

Parameters
indexIndex to convert
Returns
K if index is between keyword length,
L if index is larger than keyword length,
0 otherwise

Definition at line 652 of file nomos_utils.c.

◆ freeAndClearScan()

FUNCTION void freeAndClearScan ( struct curScan thisScan)

Clean-up all the per scan data structures, freeing any old data.

Parameters
thisScanScanner to clear

Definition at line 941 of file nomos_utils.c.

◆ get_rfpk()

FUNCTION long get_rfpk ( cacheroot_t pcroot,
char *  rf_shortname 
)

Get the rf_pk for rf_shortname.

Checks the cache to get the rf_pk for this shortname. If it doesn't exist, add it to both license_ref and the license_ref cache (the hash table).

Parameters
pcroot
rf_shortname
Returns
rf_pk of the matched license or 0

Definition at line 300 of file nomos_utils.c.

◆ getFieldValue()

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.

Returns
pointer to start of next field, or NULL at \0.

Definition at line 333 of file nomos_utils.c.

◆ getFileLists()

FUNCTION void getFileLists ( char *  dirpath)

Initialize the lists: regular-files list cur.regfList and buffer-offset list cur.offList.

Todo:
CDB - Could probably roll this back into the main processing loop or just put in a generic init func that initializes all the lists.

Definition at line 580 of file nomos_utils.c.

◆ getLicenceAndMatchPositions()

FUNCTION LicenceAndMatchPositions* getLicenceAndMatchPositions ( GArray *  in,
int  index 
)
inline

Get the LicenceAndMatchPositions for a given index in match array.

Parameters
inMatch array
indexIndex to fetch
Returns
License and match position

Definition at line 913 of file nomos_utils.c.

◆ getMatchfromHighlightInfo()

FUNCTION MatchPositionAndType* getMatchfromHighlightInfo ( GArray *  in,
int  index 
)
inline

Get the MatchPositionAndType for a given index in highlight array.

Parameters
inHighlight array
indexIndex to fetch
Returns
Match position and type

Definition at line 901 of file nomos_utils.c.

◆ initializeCurScan()

FUNCTION void initializeCurScan ( struct curScan cur)

Initialize the scanner.

Creates a new index list, match list, keyword position list, doctored buffer and license index

Parameters
curCurrent scanner

Definition at line 926 of file nomos_utils.c.

◆ initLicRefCache()

FUNCTION int initLicRefCache ( cacheroot_t pcroot)

build a cache the license ref db table.

initLicRefCache builds a cache using the rf_shortname as the key and the rf_pk as the value. This is an optimization. The cache is used for reference license lookups instead of querying the db.

Parameters
pcroot
Returns
0 for failure, 1 for success

Definition at line 260 of file nomos_utils.c.

◆ lrcache_add()

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

Add a rf_shortname, rf_pk to the license_ref cache rf_shortname is the key.

Parameters
pcrootTable root
rf_pkLicense ID in DB
rf_shortnameLicense shortname (key)
Returns
-1 for failure, 0 for success

Definition at line 185 of file nomos_utils.c.

◆ lrcache_free()

FUNCTION void lrcache_free ( cacheroot_t pcroot)

free the hash table

Free the hash table.

Parameters
pcrootTable root
Returns
none

Definition at line 158 of file nomos_utils.c.

◆ lrcache_hash()

FUNCTION long lrcache_hash ( cacheroot_t pcroot,
char *  rf_shortname 
)

calculate the hash of an rf_shortname rf_shortname is the key

Calculate the hash of an rf_shortname rf_shortname is the key.

Parameters
pcrootRoot pointer
rf_shortname
Returns
hash value

Definition at line 113 of file nomos_utils.c.

◆ lrcache_lookup()

FUNCTION long lrcache_lookup ( cacheroot_t pcroot,
char *  rf_shortname 
)

lookup rf_pk in the license_ref cache rf_shortname is the key

Lookup rf_pk in the license_ref cache rf_shortname is the key.

Parameters
pcroot
rf_shortname
Returns
rf_pk, 0 if the shortname is not in the cache

Definition at line 222 of file nomos_utils.c.

◆ lrcache_print()

FUNCTION void lrcache_print ( cacheroot_t pcroot)

Print the contents of the hash table.

Parameters
pcrootTable root
Returns
none

Definition at line 133 of file nomos_utils.c.

◆ optionIsSet()

FUNCTION int optionIsSet ( int  val)

Check if an CLI option is set.

Parameters
valBinary position to check
Returns
> 0 if it is set, 0 otherwise

Definition at line 560 of file nomos_utils.c.

◆ parseLicenseList()

FUNCTION void parseLicenseList ( )

parse the comma separated list of license names found

Uses cur.compLic and sets cur.licenseList

Definition at line 450 of file nomos_utils.c.

◆ processFile()

FUNCTION void processFile ( char *  fileToScan)

process a single file

Parameters
fileToScanFile path

Definition at line 774 of file nomos_utils.c.

◆ recordScanToDB()

FUNCTION int recordScanToDB ( cacheroot_t pcroot,
struct curScan scanRecord 
)

Write out the information about the scan to the FOSSology database.

curScan is passed as an arg even though it's available as a global, in order to facilitate future modularization of the code.

Returns
0 if successful, -1 if not.

Definition at line 850 of file nomos_utils.c.

◆ setLicenseFileIdInHiglightArray()

void setLicenseFileIdInHiglightArray ( long  licenseFileId,
char *  licenseName 
)

Set the license file id to the highlights.

Parameters
licenseFileIdLicense id
licenseNameLicense name

Definition at line 814 of file nomos_utils.c.

◆ updateLicenseFile()

FUNCTION long updateLicenseFile ( long  rfPk)

insert rf_fk, agent_fk and pfile_fk into license_file table

Parameters
rfPKthe reference file foreign key
Returns
The primary key for the inserted entry (or Negative value on error)

Definition at line 606 of file nomos_utils.c.

◆ updateLicenseFileAndHighlightArray()

int updateLicenseFileAndHighlightArray ( char *  licenseName,
cacheroot_t pcroot 
)

Add a license to hash table, license table and highlight array.

Parameters
licenseNameLicense name
pcrootHash table root
Returns
True if license is inserted in DB, False otherwise

Definition at line 829 of file nomos_utils.c.

◆ updateLicenseHighlighting()

FUNCTION int updateLicenseHighlighting ( cacheroot_t pcroot)

insert rf_fk, agent_fk, offset, len and type into highlight table

Parameters
pcrootThe root of hash table
Returns
boolean (True or False)

the license File ID was never set and we should not insert it in the database

Definition at line 676 of file nomos_utils.c.

◆ Usage()

FUNCTION void Usage ( char *  Name)

Print nomos usage help.

Say how to run this program.

Parameters
NamePath to nomos binary

Definition at line 509 of file nomos_utils.c.