FOSSology  4.4.0
Open Source License Compliance by Open Source Software
copyright.cc
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2014,2022, Siemens AG
3  Author: Daniele Fognini, Andreas Wuerl, Johannes Najjar
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
40 #include <stdio.h>
41 #include <iostream>
42 #include <sstream>
43 
44 #include "copyright.hpp"
45 
46 using namespace std;
47 using namespace fo;
48 
49 #define return_sched(retval) \
50  do {\
51  fo_scheduler_disconnect((retval));\
52  return (retval);\
53  } while(0)
54 
55 int main(int argc, char** argv)
56 {
57  /* before parsing argv and argc make sure */
58  /* to initialize the scheduler connection */
59 
60  CliOptions cliOptions;
61  vector<string> fileNames;
62  string directoryToScan;
63  if (!parseCliOptions(argc, argv, cliOptions, fileNames, directoryToScan))
64  {
65  return_sched(1);
66  }
67 
68  bool json = cliOptions.doJsonOutput();
69  bool ignoreFilesWithMimeType = cliOptions.doignoreFilesWithMimeType();
70 
71  if (!fileNames.empty())
72  {
73  const unsigned long fileNamesCount = fileNames.size();
74  bool fileError = false;
75  bool printComma = false;
76  CopyrightState state = getState(std::move(cliOptions));
77 
78  if (json)
79  {
80  cout << "[" << endl;
81  }
82 
83 #pragma omp parallel num_threads(THREADS)
84  {
85 #pragma omp for
86  for (unsigned int argn = 0; argn < fileNamesCount; ++argn)
87  {
88  const string fileName = fileNames[argn];
89  pair<string, list<match>> scanResult = processSingleFile(state, fileName);
90  if (json)
91  {
92  appendToJson(fileName, scanResult, printComma);
93  }
94  else
95  {
96  printResultToStdout(fileName, scanResult);
97  }
98  if (scanResult.first.empty())
99  {
100  fileError = true;
101  }
102  }
103  }
104  if (json)
105  {
106  cout << endl << "]" << endl;
107  }
108  return fileError ? 1 : 0;
109  }
110  else if (directoryToScan.length() > 0)
111  {
112  CopyrightState state = getState(std::move(cliOptions));
113  scanDirectory(state, json, directoryToScan);
114  }
115  else
116  {
117  DbManager dbManager(&argc, argv);
118  int agentId = queryAgentId(dbManager.getConnection());
119  CopyrightState state = getState(std::move(cliOptions));
120 
121  CopyrightDatabaseHandler copyrightDatabaseHandler(dbManager);
122  if (!copyrightDatabaseHandler.createTables())
123  {
124  std::cout << "FATAL: initialization failed" << std::endl;
125  return_sched(9);
126  }
127 
128  while (fo_scheduler_next() != NULL)
129  {
130  int uploadId = atoi(fo_scheduler_current());
131 
132  if (uploadId <= 0) continue;
133 
134  int arsId = writeARS(agentId, 0, uploadId, 0, dbManager);
135 
136  if (arsId <= 0)
137  return_sched(5);
138 
139  if (!processUploadId(state, agentId, uploadId, copyrightDatabaseHandler, ignoreFilesWithMimeType))
140  return_sched(2);
141 
143  writeARS(agentId, arsId, uploadId, 1, dbManager);
144  }
146  /* do not use bail, as it would prevent the destructors from running */
147  return_sched(0);
148  }
149 }
150 
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)
Store the options sent through the CLI.
bool doJsonOutput() const
Check if JSON output is required.
bool doignoreFilesWithMimeType() const
Check to ignore files with particular mimetype.
Manages database related requests for agent.
Definition: database.hpp:53
Holds information about state of one agent.
DB wrapper for agents.
#define return_sched(retval)
pair< string, list< match > > processSingleFile(const CopyrightState &state, const string fileName)
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)