FOSSology  4.4.0
Open Source License Compliance by Open Source Software
copyrightUtils.cc File Reference

Utilities used by copyright and ecc agent. More...

#include "copyrightUtils.hpp"
#include <boost/program_options.hpp>
#include <iostream>
#include <sstream>
Include dependency graph for copyrightUtils.cc:

Go to the source code of this file.

Macros

#define RGX_FMT_SEPARATOR   "@@"
 

Functions

int queryAgentId (PGconn *dbConn)
 Get agent id, exit if agent id is incorrect. More...
 
int writeARS (int agentId, int arsId, int uploadId, int success, const fo::DbManager &dbManager)
 Call C function fo_WriteARS() and translate the arguments. More...
 
void bail (int exitval)
 Disconnect with scheduler returning an error code and exit. More...
 
bool parseCliOptions (int argc, char **argv, CliOptions &dest, std::vector< std::string > &fileNames, std::string &directoryToScan)
 Parse the options sent by CLI to CliOptions object. More...
 
static void addDefaultScanners (CopyrightState &state)
 Add default scanners to the agent state. More...
 
scannermakeRegexScanner (const std::string &regexDesc, const std::string &defaultType)
 Make a boost regex scanner object based on regex desc and type. More...
 
CopyrightState getState (CliOptions &&cliOptions)
 Create a new state for the current agent based on CliOptions. More...
 
bool saveToDatabase (const string &s, const list< match > &matches, unsigned long pFileId, int agentId, const CopyrightDatabaseHandler &copyrightDatabaseHandler)
 Save findings to the database if agent was called by scheduler. More...
 
void matchFileWithLicenses (const string &sContent, unsigned long pFileId, CopyrightState const &state, int agentId, CopyrightDatabaseHandler &databaseHandler)
 Scan a given file with all available scanners and save findings to database. More...
 
void matchPFileWithLicenses (CopyrightState const &state, int agentId, unsigned long pFileId, CopyrightDatabaseHandler &databaseHandler)
 Get the file contents, scan for statements and save findings to database. More...
 
bool processUploadId (const CopyrightState &state, int agentId, int uploadId, CopyrightDatabaseHandler &databaseHandler, bool ignoreFilesWithMimeType)
 Process a given upload id, scan from statements and add to database. More...
 
pair< string, list< match > > processSingleFile (const CopyrightState &state, const string fileName)
 
void appendToJson (const std::string fileName, const std::pair< string, list< match >> resultPair, bool &printComma)
 
void printResultToStdout (const std::string fileName, const std::pair< string, list< match >> resultPair)
 

Detailed Description

Utilities used by copyright and ecc agent.

Definition in file copyrightUtils.cc.

Function Documentation

◆ addDefaultScanners()

static void addDefaultScanners ( CopyrightState state)
static

Add default scanners to the agent state.

Parameters
stateState to which scanners are being added

Definition at line 208 of file copyrightUtils.cc.

◆ appendToJson()

void appendToJson ( const std::string  fileName,
const std::pair< string, list< match >>  resultPair,
bool &  printComma 
)

Append a new result from scanner to main output json object

Parameters
fileNameFile which was scanned
resultPairThe result pair from scanSingleFile()
printCommaSet true to print comma. Will be set true after first data is printed

Definition at line 469 of file copyrightUtils.cc.

◆ bail()

void bail ( int  exitval)

Disconnect with scheduler returning an error code and exit.

Parameters
exitvalError code

Definition at line 62 of file copyrightUtils.cc.

◆ getState()

CopyrightState getState ( CliOptions &&  cliOptions)

Create a new state for the current agent based on CliOptions.

Called during instantiation of agent.

Parameters
cliOptionsCliOptions passed to the agent
Returns
New CopyrightState object for the agent

Definition at line 279 of file copyrightUtils.cc.

◆ makeRegexScanner()

scanner* makeRegexScanner ( const std::string &  regexDesc,
const std::string &  defaultType 
)

Make a boost regex scanner object based on regex desc and type.

Parameters
regexDescRegex format for scanner
defaultTypeType of scanner
Returns
scanner object or NULL in case of error

Definition at line 248 of file copyrightUtils.cc.

◆ matchFileWithLicenses()

void matchFileWithLicenses ( const string &  sContent,
unsigned long  pFileId,
CopyrightState const &  state,
int  agentId,
CopyrightDatabaseHandler databaseHandler 
)

Scan a given file with all available scanners and save findings to database.

Parameters
sContentContent of file
pFileIdid of the pfile sent for scan
stateState of the agent
agentIdAgent id
databaseHandlerDatabase handler used by agent

Definition at line 337 of file copyrightUtils.cc.

◆ matchPFileWithLicenses()

void matchPFileWithLicenses ( CopyrightState const &  state,
int  agentId,
unsigned long  pFileId,
CopyrightDatabaseHandler databaseHandler 
)

Get the file contents, scan for statements and save findings to database.

Reads the file contents of the pFileId and send it for scanning to matchFileWithLicenses().

If the pfile is not found for pFileId, bails with error code 8.

If the pfile is not found in repository, bails with error code 7.

Parameters
stateState of the agent
agentIdAgent id
pFileIdpFile to be scanned
databaseHandlerDatabase handler used by agent

Definition at line 361 of file copyrightUtils.cc.

◆ parseCliOptions()

bool parseCliOptions ( int  argc,
char **  argv,
CliOptions dest,
std::vector< std::string > &  fileNames,
std::string &  directoryToScan 
)

Parse the options sent by CLI to CliOptions object.

Parameters
[in]argc
[in]argv
[out]destThe parsed CliOptions object
[out]fileNamesList of files to be scanned
[out]directoryToScanDirectory to be scanned
Returns
True if success, false otherwise
Todo:
Change and add help based on IDENTITY

Definition at line 78 of file copyrightUtils.cc.

◆ printResultToStdout()

void printResultToStdout ( const std::string  fileName,
const std::pair< string, list< match >>  resultPair 
)

Print the result of current scan to stdout

Parameters
fileNameFile which was scanned
resultPairResult pair from scanSingleFile()

Definition at line 535 of file copyrightUtils.cc.

◆ processSingleFile()

pair<string, list<match> > processSingleFile ( const CopyrightState state,
const string  fileName 
)

Read a single file and run all scanners on it based of CopyrightState.

Parameters
stateCopyright state
fileNameLocation of the file to be scanned
Returns
A pair of file scanned and list of matches found.

Definition at line 439 of file copyrightUtils.cc.

◆ processUploadId()

bool processUploadId ( const CopyrightState state,
int  agentId,
int  uploadId,
CopyrightDatabaseHandler databaseHandler,
bool  ignoreFilesWithMimeType 
)

Process a given upload id, scan from statements and add to database.

The agent runs in parallel with the help of omp. A new thread is created for every pfile.

Parameters
stateState of the agent
agentIdAgent id
uploadIdUpload id to be processed
databaseHandlerDatabase handler object
ignoreFilesWithMimeTypeTo ignore files with particular mimetype
Returns
True when upload is processed

Definition at line 405 of file copyrightUtils.cc.

◆ queryAgentId()

int queryAgentId ( PGconn *  dbConn)

Get agent id, exit if agent id is incorrect.

Parameters
[in]dbConnDatabase connection object
Returns
ID of the agent

Definition at line 25 of file copyrightUtils.cc.

◆ saveToDatabase()

bool saveToDatabase ( const string &  s,
const list< match > &  matches,
unsigned long  pFileId,
int  agentId,
const CopyrightDatabaseHandler copyrightDatabaseHandler 
)

Save findings to the database if agent was called by scheduler.

Parameters
sStatement found
matchesList of regex matches for highlight
pFileIdId of pfile on which the statement was found
agentIdId of agent who discovered the statements
copyrightDatabaseHandlerDatabase handler object
Returns
True of successful insertion, false otherwise

Definition at line 296 of file copyrightUtils.cc.

◆ writeARS()

int writeARS ( int  agentId,
int  arsId,
int  uploadId,
int  success,
const fo::DbManager dbManager 
)

Call C function fo_WriteARS() and translate the arguments.

See also
fo_WriteARS()

Definition at line 53 of file copyrightUtils.cc.