13 #include "OjoUtils.hpp"
28 return OjoState(agentId, std::move(cliOptions));
38 return OjoState(-1, std::move(cliOptions));
48 char* COMMIT_HASH =
fo_sysconfig(AGENT_NAME,
"COMMIT_HASH");
52 if (!asprintf(&agentRevision,
"%s.%s", VERSION, COMMIT_HASH))
56 agentRevision, AGENT_DESC);
77 PGconn *connection =
dbManager.getConnection();
80 return fo_WriteARS(connection, arsId, uploadId, agentId, AGENT_ARS, NULL,
106 uploadId, state.
getAgentId(), ignoreFilesWithMimeType);
107 char const *repoArea =
"files";
114 size_t pFileCount = fileIds.size();
117 for (
size_t it = 0; it < pFileCount; ++it)
122 unsigned long pFileId = fileIds[it];
129 char *filePath = NULL;
130 #pragma omp critical (repo_mk_path)
136 AGENT_NAME
" was unable to derive a file path for pfile %ld. Check your HOSTS configuration.",
141 vector<ojomatch> identified;
144 identified = agentObj.processFile(filePath, threadLocalDatabaseHandler,
148 catch (std::runtime_error &e)
150 LOG_FATAL(
"Unable to read %s.", e.what());
157 LOG_FATAL(
"Unable to store results in database for pfile %ld.",
184 if (!databaseHandle.
begin())
190 if (matches.size() == 0)
194 return databaseHandle.
commit();
196 for (
auto m : matches)
216 return databaseHandle.
commit();
229 std::vector<std::string> &fileNames,
string &directoryToScan)
231 boost::program_options::options_description desc(
232 AGENT_NAME
": recognized options");
235 "help,h",
"shows help"
238 "verbose,v",
"increase verbosity"
242 boost::program_options::value<vector<string> >(),
246 "json,J",
"output JSON"
249 "ignoreFilesWithMimeType,I",
"ignoreFilesWithMimeType"
253 boost::program_options::value<string>(),
254 "path to the sysconfigdir"
258 "specifies, that the command was called by the scheduler"
262 boost::program_options::value<int>(),
263 "the id of the user that created the job (only in combination with --scheduler_start)"
267 boost::program_options::value<int>(),
268 "the id of the group of the user that created the job (only in combination with --scheduler_start)"
272 boost::program_options::value<int>(),
273 "the id of the job (only in combination with --scheduler_start)"
277 boost::program_options::value<string>(),
278 "directory to scan (recursive)"
282 boost::program_options::positional_options_description p;
285 boost::program_options::variables_map vm;
289 boost::program_options::store(
290 boost::program_options::command_line_parser(argc, argv).options(desc).positional(
293 if (vm.count(
"help") > 0)
295 cout << desc << endl;
299 if (vm.count(
"files"))
301 fileNames = vm[
"files"].as<std::vector<string> >();
304 unsigned long verbosity = vm.count(
"verbose");
305 bool json = vm.count(
"json") > 0 ? true :
false;
306 bool ignoreFilesWithMimeType = vm.count(
"ignoreFilesWithMimeType") > 0 ? true :
false;
308 dest =
OjoCliOptions(verbosity, json, ignoreFilesWithMimeType);
310 if (vm.count(
"userID") > 0)
312 dest.setUserId(vm[
"userID"].as<int>());
315 if (vm.count(
"groupID") > 0)
317 dest.setGroupId(vm[
"groupID"].as<int>());
320 if (vm.count(
"directory"))
322 if (vm.count(
"files"))
324 cout <<
"cannot pass files and directory at the same time" << endl;
325 cout << desc << endl;
329 directoryToScan = vm[
"directory"].as<std::string>();
334 catch (boost::bad_any_cast&)
336 cout <<
"wrong parameter type" << endl;
337 cout << desc << endl;
340 catch (boost::program_options::error&)
342 cout <<
"wrong command line arguments" << endl;
343 cout << desc << endl;
356 const std::pair<
string, vector<ojomatch>> resultPair,
360 #if JSONCPP_VERSION_HEXA < ((1 << 24) | (4 << 16))
362 Json::FastWriter jsonWriter;
366 Json::StreamWriterBuilder jsonWriter;
367 jsonWriter[
"commentStyle"] =
"None";
368 jsonWriter[
"indentation"] =
"";
370 if (resultPair.first.empty())
372 result[
"file"] = fileName;
373 result[
"results"] =
"Unable to read file";
377 vector<ojomatch> resultList = resultPair.second;
379 for (
auto m : resultList)
382 j[
"start"] = Json::Value::UInt(m.start);
383 j[
"end"] = Json::Value::UInt(m.end);
384 j[
"len"] = Json::Value::UInt(m.len);
385 j[
"license"] = m.content;
388 result[
"file"] = fileName;
389 result[
"results"] = results;
392 #pragma omp critical (jsonPrinter)
403 #if JSONCPP_VERSION_HEXA < ((1 << 24) | (4 << 16))
406 jsonString = jsonWriter.write(result);
407 jsonString.replace(jsonString.find(
"\n"),
string(
"\n").length(),
"");
410 jsonString = Json::writeString(jsonWriter, result);
412 cout <<
" " << jsonString << flush;
422 const std::pair<
string, vector<ojomatch>> resultPair)
424 if (resultPair.first.empty())
426 cout << fileName <<
" :: Unable to read file" << endl;
430 ss << fileName <<
" ::" << endl;
432 vector<ojomatch> resultList = resultPair.second;
433 for (
auto m : resultList)
435 ss <<
"\t[" << m.start <<
':' << m.end <<
"]: '" << m.content <<
"'" << endl;
void appendToJson(const std::string fileName, const std::pair< string, vector< ojomatch >> resultPair, bool &printComma)
bool parseCliOptions(int argc, char **argv, OjoCliOptions &dest, std::vector< std::string > &fileNames, string &directoryToScan)
Parse the options sent by CLI to CliOptions object.
void printResultToStdout(const std::string fileName, const std::pair< string, vector< ojomatch >> resultPair)
bool storeResultInDb(const vector< ojomatch > &matches, OjosDatabaseHandler &databaseHandle, const int agent_fk, const int pfile_fk)
Store the results from scan to DB.
OjoState getState(DbManager &dbManager, OjoCliOptions &&cliOptions)
Create a new state for the current agent based on CliOptions.
int queryAgentId(DbManager &dbManager)
int writeARS(const OjoState &state, int arsId, int uploadId, int success, DbManager &dbManager)
void bail(int exitval)
Disconnect with scheduler returning an error code and exit.
bool processUploadId(const OjoState &state, int uploadId, OjosDatabaseHandler &databaseHandler, bool ignoreFilesWithMimeType)
Store the options sent through the CLI.
int getGroupId() const
Get the group running the agent.
int getUserId() const
Get the user running the agent.
Store the state of the agent.
const OjoAgent & getOjoAgent() const
const OjoCliOptions & getCliOptions() const
Get the OjoCliOptions set by user.
OjosDatabaseHandler spawn() const
std::vector< unsigned long > queryFileIdsForUpload(int uploadId, int agentId, bool ignoreFilesWithMimeType)
bool insertNoResultInDatabase(OjoDatabaseEntry &entry) const
Save no result to the database.
bool saveHighlightToDatabase(const ojomatch &match, const unsigned long fl_fk) const
unsigned long saveLicenseToDatabase(OjoDatabaseEntry &entry) const
Save findings to the database if agent was called by scheduler.
bool commit() const
COMMIT a transaction block in DB.
bool begin() const
BEGIN a transaction block in DB.
char * getPFileNameForFileId(unsigned long pfileId) const
Get the file name of a give pfile id.
bool rollback() const
ROLLBACK a transaction block in DB.
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.
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.
char * fo_RepMkPath(const char *Type, char *Filename)
Given a filename, construct the full path to the file.
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
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...
char * fo_sysconfig(const char *sectionname, const char *variablename)
gets a system configuration variable from the configuration data.
fo_dbManager * dbManager
fo_dbManager object
fo namespace holds the FOSSology library functions.
const unsigned long int license_fk