FOSSology  4.4.0
Open Source License Compliance by Open Source Software
utils.cc
1 /*
2  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
7 #include <iostream>
8 #include "ninkawrapper.hpp"
9 #include "utils.hpp"
10 
11 using namespace fo;
12 
14 {
15  int agentId = queryAgentId(dbManager);
16  return State(agentId);
17 }
18 
20 {
21  char* COMMIT_HASH = fo_sysconfig(AGENT_NAME, "COMMIT_HASH");
22  char* VERSION = fo_sysconfig(AGENT_NAME, "VERSION");
23  char* agentRevision;
24 
25  if (!asprintf(&agentRevision, "%s.%s", VERSION, COMMIT_HASH))
26  bail(-1);
27 
28  int agentId = fo_GetAgentKey(dbManager.getConnection(), AGENT_NAME, 0, agentRevision, AGENT_DESC);
29  free(agentRevision);
30 
31  if (agentId <= 0)
32  bail(1);
33 
34  return agentId;
35 }
36 
37 int writeARS(const State& state, int arsId, int uploadId, int success, DbManager& dbManager)
38 {
39  PGconn* connection = dbManager.getConnection();
40  int agentId = state.getAgentId();
41 
42  return fo_WriteARS(connection, arsId, uploadId, agentId, AGENT_ARS, NULL, success);
43 }
44 
45 void bail(int exitval)
46 {
47  fo_scheduler_disconnect(exitval);
48  exit(exitval);
49 }
50 
51 bool processUploadId(const State& state, int uploadId, NinkaDatabaseHandler& databaseHandler)
52 {
53  vector<unsigned long> fileIds = databaseHandler.queryFileIdsForUpload(uploadId);
54 
55  bool errors = false;
56 #pragma omp parallel
57  {
58  NinkaDatabaseHandler threadLocalDatabaseHandler(databaseHandler.spawn());
59 
60  size_t pFileCount = fileIds.size();
61 #pragma omp for
62  for (size_t it = 0; it < pFileCount; ++it)
63  {
64  if (errors)
65  continue;
66 
67  unsigned long pFileId = fileIds[it];
68 
69  if (pFileId == 0)
70  continue;
71 
72  if (!matchPFileWithLicenses(state, pFileId, threadLocalDatabaseHandler))
73  {
74  errors = true;
75  }
76 
78  }
79  }
80 
81  return !errors;
82 }
83 
84 bool matchPFileWithLicenses(const State& state, unsigned long pFileId, NinkaDatabaseHandler& databaseHandler)
85 {
86  char* pFile = databaseHandler.getPFileNameForFileId(pFileId);
87 
88  if (!pFile)
89  {
90  cout << "File not found " << pFileId << endl;
91  bail(8);
92  }
93 
94  char* fileName = NULL;
95  {
96 #pragma omp critical (repo_mk_path)
97  fileName = fo_RepMkPath("files", pFile);
98  }
99  if (fileName)
100  {
101  fo::File file(pFileId, fileName);
102 
103  if (!matchFileWithLicenses(state, file, databaseHandler))
104  return false;
105 
106  free(fileName);
107  free(pFile);
108  }
109  else
110  {
111  cout << "PFile not found in repo " << pFileId << endl;
112  bail(7);
113  }
114 
115  return true;
116 }
117 
118 bool matchFileWithLicenses(const State& state, const fo::File& file, NinkaDatabaseHandler& databaseHandler)
119 {
120  string ninkaResult = scanFileWithNinka(state, file);
121  vector<string> ninkaLicenseNames = extractLicensesFromNinkaResult(ninkaResult);
122  vector<LicenseMatch> matches = createMatches(ninkaLicenseNames);
123  return saveLicenseMatchesToDatabase(state, matches, file.getId(), databaseHandler);
124 }
125 
126 bool saveLicenseMatchesToDatabase(const State& state, const vector<LicenseMatch>& matches, unsigned long pFileId, NinkaDatabaseHandler& databaseHandler)
127 {
128  for (vector<LicenseMatch>::const_iterator it = matches.begin(); it != matches.end(); ++it)
129  {
130  const LicenseMatch& match = *it;
131  databaseHandler.insertOrCacheLicenseIdForName(match.getLicenseName());
132  }
133 
134  if (!databaseHandler.begin())
135  return false;
136 
137  for (vector<LicenseMatch>::const_iterator it = matches.begin(); it != matches.end(); ++it)
138  {
139  const LicenseMatch& match = *it;
140 
141  int agentId = state.getAgentId();
142  string rfShortname = match.getLicenseName();
143  unsigned percent = match.getPercentage();
144 
145  unsigned long licenseId = databaseHandler.getCachedLicenseIdForName(rfShortname);
146 
147  if (licenseId == 0)
148  {
149  databaseHandler.rollback();
150  cout << "cannot get licenseId for shortname '" + rfShortname + "'" << endl;
151  return false;
152  }
153 
154 
155  if (!databaseHandler.saveLicenseMatch(agentId, pFileId, licenseId, percent))
156  {
157  databaseHandler.rollback();
158  cout << "failing save licenseMatch" << endl;
159  return false;
160  };
161  }
162 
163  return databaseHandler.commit();
164 }
Definition: state.hpp:16
int getAgentId() const
getter function for agent Id
Definition: state.cc:14
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.
DB wrapper for agents.
Class to handle file related operations.
Definition: files.hpp:26
unsigned long getId() const
Definition: files.cc:115
void matchPFileWithLicenses(CopyrightState const &state, int agentId, unsigned long pFileId, CopyrightDatabaseHandler &databaseHandler)
Get the file contents, scan for statements and save findings to database.
int queryAgentId(PGconn *dbConn)
Get agent id, exit if agent id is incorrect.
void matchFileWithLicenses(const string &sContent, unsigned long pFileId, CopyrightState const &state, int agentId, CopyrightDatabaseHandler &databaseHandler)
Scan a given file with all available scanners and save findings to database.
int writeARS(int agentId, int arsId, int uploadId, int success, const fo::DbManager &dbManager)
Call C function fo_WriteARS() and translate the arguments.
CopyrightState getState(CliOptions &&cliOptions)
Create a new state for the current agent based on CliOptions.
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.
void bail(int exitval)
Disconnect with scheduler returning an error code and exit.
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.
Definition: libfossagent.c:214
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.
Definition: libfossagent.c:158
char * fo_RepMkPath(const char *Type, char *Filename)
Given a filename, construct the full path to the file.
Definition: libfossrepo.c:352
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
Definition: process.c:16
fo namespace holds the FOSSology library functions.
Store the results of a regex match.
Definition: scanners.hpp:28