14 #include "serialize.h"
17 void parseArguments(
MonkState* state,
int argc,
char** argv,
int* fileOptInd) {
19 static struct option long_options[] = {{
"config", required_argument, 0,
'c'},
20 {
"userID", required_argument, 0,
'u'},
21 {
"groupID", required_argument, 0,
'g'},
22 {
"scheduler_start", no_argument, 0,
'S'},
23 {
"jobId", required_argument, 0,
'j'},
26 while ((c = getopt_long(argc, argv,
"VvJhIs:k:c:", long_options, &option_index)) != -1) {
35 state->scanMode = MODE_SCHEDULER;
42 printf(AGENT_NAME
" version " VERSION_S
" r(" COMMIT_HASH_S
")\n");
44 printf(AGENT_NAME
" (no version available)\n");
52 state->scanMode = MODE_EXPORT_KOWLEDGEBASE;
53 state->knowledgebaseFile = optarg;
56 state->scanMode = MODE_CLI_OFFLINE;
57 state->knowledgebaseFile = optarg;
60 state->ignoreFilesWithMimeType =
true;
66 "\nAs CLI tool using the licenses from the FOSSology database:\n");
67 printf(
" %s [options] file [file [...]]\n", argv[0]);
69 " -h :: help (print this message), then exit.\n"
70 " -c config :: specify the directory for the system configuration.\n"
71 " -v :: verbose output.\n"
72 " -J :: JSON output.\n"
73 " file :: scan file and print licenses detected within it.\n"
74 " -V :: print the version info, then exit.\n"
75 "\nSave knowledgebase to knowledgebaseFile for offline usage without db:\n");
76 printf(
" %s [options] -s knowledgebaseFile\n", argv[0]);
78 " -c config :: specify the directory for the system configuration.\n"
79 "\nUse previously saved knowledgebaseFile for offline usage without db.\n");
80 printf(
" %s -k knowledgebaseFile [options] file [file [...]]\n", argv[0]);
82 " -J :: JSON output.\n"
83 " file :: scan file and print licenses detected within it.\n"
84 "\nThe following should only be called by the FOSSology scheduler:\n");
85 printf(
" %s --scheduler_start [options]\n", argv[0]);
87 " -c config :: specify the directory for the system configuration.\n"
88 " --userID i :: the id of the user that created the job\n"
89 " --groupID i :: the id of the group of the user that created the job\n"
90 " --jobID i :: the id of the job\n");
96 if (optind < argc && state->scanMode != MODE_CLI_OFFLINE) {
97 state->scanMode = MODE_CLI;
99 if((state->scanMode == MODE_CLI_OFFLINE ||
100 state->scanMode == MODE_EXPORT_KOWLEDGEBASE) &&
101 state->knowledgebaseFile == NULL) {
102 fprintf( stderr,
"necessary path to knowledgebase file not provided\n");
107 int main(
int argc,
char** argv) {
110 MonkState stateStore = { .dbManager = NULL,
114 .knowledgebaseFile = NULL,
116 .ignoreFilesWithMimeType =
false,
119 parseArguments(state, argc, argv, &fileOptInd);
120 int wasSuccessful = 1;
122 if (state->scanMode == 0) {
127 if (state->scanMode != MODE_CLI_OFFLINE) {
130 fileOptInd = fileOptInd - oldArgc + argc;
132 PGresult* licensesResult = queryAllLicenses(state->dbManager);
133 licenses = extractLicenses(state->dbManager, licensesResult, MIN_ADJACENT_MATCHES, MAX_LEADING_DIFF);
134 PQclear(licensesResult);
136 licenses = deserializeFromFile(state->knowledgebaseFile, MIN_ADJACENT_MATCHES, MAX_LEADING_DIFF);
139 if (state->scanMode == MODE_SCHEDULER) {
140 wasSuccessful = handleSchedulerMode(state, licenses);
141 scheduler_disconnect(state, ! wasSuccessful);
142 }
else if (state->scanMode == MODE_CLI ||
143 state->scanMode == MODE_CLI_OFFLINE) {
145 if (state->dbManager != NULL) {
146 scheduler_disconnect(state, 0);
148 state->dbManager = NULL;
150 wasSuccessful = handleCliMode(state, licenses, argc, argv, fileOptInd);
151 }
else if (state->scanMode == MODE_EXPORT_KOWLEDGEBASE) {
152 printf(
"Write knowledgebase to %s\n", state->knowledgebaseFile);
153 wasSuccessful = serializeToFile(licenses, state->knowledgebaseFile);
156 licenses_free(licenses);
158 return ! wasSuccessful;
void fo_scheduler_connect_dbMan(int *argc, char **argv, fo_dbManager **dbManager)
Make a connection from an agent to the scheduler and create a DB manager as well.
Header file for the scheduler.