FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ojos.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019, Siemens AG
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * version 2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16  */
53 #include "ojos.hpp"
54 
55 using namespace fo;
56 
61 #define return_sched(retval) \
62  do {\
63  fo_scheduler_disconnect((retval));\
64  return (retval);\
65  } while(0)
66 
67 int main(int argc, char **argv)
68 {
69  OjoCliOptions cliOptions;
70  vector<string> fileNames;
71  string directoryToScan;
72  if (!parseCliOptions(argc, argv, cliOptions, fileNames, directoryToScan))
73  {
74  return_sched(1);
75  }
76 
77  bool json = cliOptions.doJsonOutput();
78  bool ignoreFilesWithMimeType = cliOptions.doignoreFilesWithMimeType();
79  OjoState state = getState(std::move(cliOptions));
80 
81  if (!fileNames.empty())
82  {
83  const unsigned long fileNamesCount = fileNames.size();
84  bool fileError = false;
85  bool printComma = false;
86  OjoAgent agentObj = state.getOjoAgent();
87 
88  if (json)
89  {
90  cout << "[" << endl;
91  }
92 
93 #pragma omp parallel shared(printComma)
94  {
95 #pragma omp for
96  for (unsigned int argn = 0; argn < fileNamesCount; ++argn)
97  {
98  const string fileName = fileNames[argn];
99 
100  vector<ojomatch> l;
101  try
102  {
103  l = agentObj.processFile(fileName);
104  }
105  catch (std::runtime_error &e)
106  {
107  cerr << "Unable to read " << e.what();
108  fileError = true;
109  continue;
110  }
111  pair<string, vector<ojomatch>> scanResult(fileName, l);
112  if (json)
113  {
114  appendToJson(fileName, scanResult, printComma);
115  }
116  else
117  {
118  printResultToStdout(fileName, scanResult);
119  }
120  }
121  }
122  if (json)
123  {
124  cout << endl << "]" << endl;
125  }
126  return fileError ? 1 : 0;
127  }
128  else if (directoryToScan.length() > 0)
129  {
130  scanDirectory(json, directoryToScan);
131  }
132  else
133  {
134  DbManager dbManager(&argc, argv);
135  OjosDatabaseHandler databaseHandler(dbManager);
136 
137  state.setAgentId(queryAgentId(dbManager));
138 
139  while (fo_scheduler_next() != NULL)
140  {
141  int uploadId = atoi(fo_scheduler_current());
142 
143  if (uploadId == 0)
144  continue;
145 
146  int arsId = writeARS(state, 0, uploadId, 0, dbManager);
147 
148  if (arsId <= 0)
149  bail(5);
150 
151  if (!processUploadId(state, uploadId, databaseHandler, ignoreFilesWithMimeType))
152  bail(2);
153 
155  writeARS(state, arsId, uploadId, 1, dbManager);
156  }
158 
159  /* do not use bail, as it would prevent the destructors from running */
161  }
162  return 0;
163 }
bool doignoreFilesWithMimeType() const
Check ignore files with particular mimetype is required.
Definition: OjoState.cc:107
void bail(int exitval)
Disconnect with scheduler returning an error code and exit.
#define return_sched(retval)
Definition: ojos.cc:61
void printResultToStdout(const std::string fileName, const std::pair< string, list< match >> resultPair)
DB wrapper for agents.
void scanDirectory(const bool json, const string &directoryPath)
char * fo_scheduler_current()
Get the last read string from the scheduler.
CopyrightState getState(CliOptions &&cliOptions)
Create a new state for the current agent based on CliOptions.
const OjoAgent & getOjoAgent() const
Definition: OjoState.cc:52
void appendToJson(const std::string fileName, const std::pair< string, list< match >> resultPair, bool &printComma)
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
Store the state of the agent.
Definition: OjoState.hpp:54
void setAgentId(const int agentId)
Definition: OjoState.cc:34
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.
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.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
int queryAgentId(PGconn *dbConn)
Get agent id, exit if agent id is incorrect.
fo namespace holds the FOSSology library functions.
char * fo_scheduler_next()
Get the next data to process from the scheduler.
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...
bool doJsonOutput() const
Check if JSON output is required.
Definition: OjoState.cc:98
int writeARS(int agentId, int arsId, int uploadId, int success, const fo::DbManager &dbManager)
Call C function fo_WriteARS() and translate the arguments.
Store the options sent through the CLI.
Definition: OjoState.hpp:34