24 #include "OjoUtils.hpp" 39 return OjoState(agentId, std::move(cliOptions));
49 return OjoState(-1, std::move(cliOptions));
59 char* COMMIT_HASH =
fo_sysconfig(AGENT_NAME,
"COMMIT_HASH");
63 if (!asprintf(&agentRevision,
"%s.%s", VERSION, COMMIT_HASH))
67 agentRevision, AGENT_DESC);
91 return fo_WriteARS(connection, arsId, uploadId, agentId, AGENT_ARS, NULL,
117 uploadId, ignoreFilesWithMimeType);
118 char const *repoArea =
"files";
125 size_t pFileCount = fileIds.size();
128 for (
size_t it = 0; it < pFileCount; ++it)
133 unsigned long pFileId = fileIds[it];
138 char *fileName = threadLocalDatabaseHandler.getPFileNameForFileId(
140 char *filePath = NULL;
141 #pragma omp critical (repo_mk_path) 147 AGENT_NAME
" was unable to derive a file path for pfile %ld. Check your HOSTS configuration.",
152 vector<ojomatch> identified;
155 identified = agentObj.processFile(filePath, threadLocalDatabaseHandler);
157 catch (std::runtime_error &e)
159 LOG_FATAL(
"Unable to read %s.", e.what());
166 LOG_FATAL(
"Unable to store results in database for pfile %ld.",
193 if (!databaseHandle.
begin())
199 if (matches.size() == 0)
203 return databaseHandle.
commit();
205 for (
auto m : matches)
225 return databaseHandle.
commit();
238 std::vector<std::string> &fileNames,
string &directoryToScan)
240 boost::program_options::options_description desc(
241 AGENT_NAME
": recognized options");
244 "help,h",
"shows help" 247 "verbose,v",
"increase verbosity" 251 boost::program_options::value<vector<string> >(),
255 "json,J",
"output JSON" 258 "ignoreFilesWithMimeType,I",
"ignoreFilesWithMimeType" 262 boost::program_options::value<string>(),
263 "path to the sysconfigdir" 267 "specifies, that the command was called by the scheduler" 271 boost::program_options::value<int>(),
272 "the id of the user that created the job (only in combination with --scheduler_start)" 276 boost::program_options::value<int>(),
277 "the id of the group of the user that created the job (only in combination with --scheduler_start)" 281 boost::program_options::value<int>(),
282 "the id of the job (only in combination with --scheduler_start)" 286 boost::program_options::value<string>(),
287 "directory to scan (recursive)" 291 boost::program_options::positional_options_description p;
294 boost::program_options::variables_map vm;
298 boost::program_options::store(
299 boost::program_options::command_line_parser(argc, argv).options(desc).positional(
302 if (vm.count(
"help") > 0)
304 cout << desc << endl;
308 if (vm.count(
"files"))
310 fileNames = vm[
"files"].as<std::vector<string> >();
313 unsigned long verbosity = vm.count(
"verbose");
314 bool json = vm.count(
"json") > 0 ?
true :
false;
315 bool ignoreFilesWithMimeType = vm.count(
"ignoreFilesWithMimeType") > 0 ?
true :
false;
317 dest =
OjoCliOptions(verbosity, json, ignoreFilesWithMimeType);
319 if (vm.count(
"directory"))
321 if (vm.count(
"files"))
323 cout <<
"cannot pass files and directory at the same time" << endl;
324 cout << desc << endl;
328 directoryToScan = vm[
"directory"].as<std::string>();
333 catch (boost::bad_any_cast&)
335 cout <<
"wrong parameter type" << endl;
336 cout << desc << endl;
339 catch (boost::program_options::error&)
341 cout <<
"wrong command line arguments" << endl;
342 cout << desc << endl;
355 const std::pair<
string, vector<ojomatch>> resultPair,
359 #if JSONCPP_VERSION_HEXA < ((1 << 24) | (4 << 16)) 361 Json::FastWriter jsonWriter;
365 Json::StreamWriterBuilder jsonWriter;
366 jsonWriter[
"commentStyle"] =
"None";
367 jsonWriter[
"indentation"] =
"";
369 if (resultPair.first.empty())
371 result[
"file"] = fileName;
372 result[
"results"] =
"Unable to read file";
376 vector<ojomatch> resultList = resultPair.second;
378 for (
auto m : resultList)
381 j[
"start"] = Json::Value::UInt(m.start);
382 j[
"end"] = Json::Value::UInt(m.end);
383 j[
"len"] = Json::Value::UInt(m.len);
384 j[
"license"] = m.content;
387 result[
"file"] = fileName;
388 result[
"results"] = results;
391 #pragma omp critical (jsonPrinter) 402 #if JSONCPP_VERSION_HEXA < ((1 << 24) | (4 << 16)) 405 jsonString = jsonWriter.write(result);
406 jsonString.replace(jsonString.find(
"\n"), string(
"\n").length(),
"");
409 jsonString = Json::writeString(jsonWriter, result);
411 cout <<
" " << jsonString << flush;
421 const std::pair<
string, vector<ojomatch>> resultPair)
423 if (resultPair.first.empty())
425 cout << fileName <<
" :: Unable to read file" << endl;
429 ss << fileName <<
" ::" << endl;
431 vector<ojomatch> resultList = resultPair.second;
432 for (
auto m : resultList)
434 ss <<
"\t[" << m.start <<
':' << m.end <<
"]: '" << m.content <<
"'" << endl;
int queryAgentId(DbManager &dbManager)
unsigned long saveLicenseToDatabase(OjoDatabaseEntry &entry) const
Save findings to the database if agent was called by scheduler.
bool storeResultInDb(const vector< ojomatch > &matches, OjosDatabaseHandler &databaseHandle, const int agent_fk, const int pfile_fk)
Store the results from scan to DB.
bool commit() const
COMMIT a transaction block in DB.
bool rollback() const
ROLLBACK a transaction block in DB.
const OjoAgent & getOjoAgent() const
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
OjoState getState(DbManager &dbManager, OjoCliOptions &&cliOptions)
Create a new state for the current agent based on CliOptions.
PGconn * getConnection() const
void printResultToStdout(const std::string fileName, const std::pair< string, vector< ojomatch >> resultPair)
void bail(int exitval)
Disconnect with scheduler returning an error code and exit.
Store the state of the agent.
bool saveHighlightToDatabase(const ojomatch &match, const unsigned long fl_fk) const
int writeARS(const OjoState &state, int arsId, int uploadId, int success, DbManager &dbManager)
OjosDatabaseHandler spawn() const
bool processUploadId(const OjoState &state, int uploadId, OjosDatabaseHandler &databaseHandler, bool ignoreFilesWithMimeType)
void appendToJson(const std::string fileName, const std::pair< string, vector< ojomatch >> resultPair, bool &printComma)
bool begin() const
BEGIN a transaction block in DB.
char * fo_RepMkPath(const char *Type, char *Filename)
Given a filename, construct the full path to the file.
fo_dbManager * dbManager
fo_dbManager object
FUNCTION int fo_GetAgentKey(PGconn *pgConn, const char *agent_name, long Upload_pk, const char *rev, const char *agent_desc)
Get the latest enabled agent key (agent_pk) from the database.
fo namespace holds the FOSSology library functions.
FUNCTION int fo_WriteARS(PGconn *pgConn, int ars_pk, int upload_pk, int agent_pk, const char *tableName, const char *ars_status, int ars_success)
Write ars record.
bool insertNoResultInDatabase(OjoDatabaseEntry &entry) const
Save no result to the database.
void fo_scheduler_heart(int i)
This function must be called by agents to let the scheduler know they are alive and how many items th...
const unsigned long int license_fk
bool parseCliOptions(int argc, char **argv, OjoCliOptions &dest, std::vector< std::string > &fileNames, string &directoryToScan)
Parse the options sent by CLI to CliOptions object.
char * fo_sysconfig(const char *sectionname, const char *variablename)
gets a system configuration variable from the configuration data.
std::vector< unsigned long > queryFileIdsForUpload(int uploadId, bool ignoreFilesWithMimeType)
Store the options sent through the CLI.