FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ojos.cc
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2019 Siemens AG
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
42 #include "ojos.hpp"
43 
44 using namespace fo;
45 
50 #define return_sched(retval) \
51  do {\
52  fo_scheduler_disconnect((retval));\
53  return (retval);\
54  } while(0)
55 
56 int main(int argc, char **argv)
57 {
58  OjoCliOptions cliOptions;
59  vector<string> fileNames;
60  string directoryToScan;
61  if (!parseCliOptions(argc, argv, cliOptions, fileNames, directoryToScan))
62  {
63  return_sched(1);
64  }
65 
66  bool json = cliOptions.doJsonOutput();
67  bool ignoreFilesWithMimeType = cliOptions.doignoreFilesWithMimeType();
68  OjoState state = getState(std::move(cliOptions));
69 
70  if (!fileNames.empty())
71  {
72  const unsigned long fileNamesCount = fileNames.size();
73  bool fileError = false;
74  bool printComma = false;
75  OjoAgent agentObj = state.getOjoAgent();
76 
77  if (json)
78  {
79  cout << "[" << endl;
80  }
81 
82 #pragma omp parallel shared(printComma)
83  {
84 #pragma omp for
85  for (unsigned int argn = 0; argn < fileNamesCount; ++argn)
86  {
87  const string fileName = fileNames[argn];
88 
89  vector<ojomatch> l;
90  try
91  {
92  l = agentObj.processFile(fileName);
93  }
94  catch (std::runtime_error &e)
95  {
96  cerr << "Unable to read " << e.what();
97  fileError = true;
98  continue;
99  }
100  pair<string, vector<ojomatch>> scanResult(fileName, l);
101  if (json)
102  {
103  appendToJson(fileName, scanResult, printComma);
104  }
105  else
106  {
107  printResultToStdout(fileName, scanResult);
108  }
109  }
110  }
111  if (json)
112  {
113  cout << endl << "]" << endl;
114  }
115  return fileError ? 1 : 0;
116  }
117  else if (directoryToScan.length() > 0)
118  {
119  scanDirectory(json, directoryToScan);
120  }
121  else
122  {
123  DbManager dbManager(&argc, argv);
124  OjosDatabaseHandler databaseHandler(dbManager);
125 
127 
128  while (fo_scheduler_next() != NULL)
129  {
130  int uploadId = atoi(fo_scheduler_current());
131 
132  if (uploadId == 0)
133  continue;
134 
135  int arsId = writeARS(state, 0, uploadId, 0, dbManager);
136 
137  if (arsId <= 0)
138  bail(5);
139 
140  if (!processUploadId(state, uploadId, databaseHandler, ignoreFilesWithMimeType))
141  bail(2);
142 
144  writeARS(state, arsId, uploadId, 1, dbManager);
145  }
147 
148  /* do not use bail, as it would prevent the destructors from running */
150  }
151  return 0;
152 }
bool processUploadId(const CompatibilityState &state, int uploadId, CompatibilityDatabaseHandler &databaseHandler, int groupId)
bool parseCliOptions(int argc, char **argv, CompatibilityCliOptions &dest, std::string &types, std::string &rules, string &jFile, string &mainLicense)
Parse the options sent by CLI to CliOptions object.
void printResultToStdout(const std::vector< tuple< string, string, bool >> &resultPair, const std::string &fileName)
CompatibilityState getState(DbManager &dbManager, CompatibilityCliOptions &&cliOptions)
Create a new state for the current agent based on CliOptions.
int writeARS(const CompatibilityState &state, int arsId, int uploadId, int success, DbManager &dbManager)
int queryAgentId(DbManager &dbManager)
void appendToJson(const std::vector< tuple< string, string, bool >> &resultPair, const std::string &fileName, bool &printComma)
void bail(int exitval)
Store the options sent through the CLI.
Definition: OjoState.hpp:24
bool doJsonOutput() const
Check if JSON output is required.
Definition: OjoState.cc:89
bool doignoreFilesWithMimeType() const
Check ignore files with particular mimetype is required.
Definition: OjoState.cc:98
Store the state of the agent.
Definition: OjoState.hpp:51
const OjoAgent & getOjoAgent() const
Definition: OjoState.cc:41
void setAgentId(const int agentId)
Definition: OjoState.cc:23
DB wrapper for agents.
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_scheduler_current()
Get the last read string from the scheduler.
char * fo_scheduler_next()
Get the next data to process from the scheduler.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
fo namespace holds the FOSSology library functions.
void scanDirectory(const bool json, const string &directoryPath)
#define return_sched(retval)
Definition: ojos.cc:50