FOSSology  4.4.0
Open Source License Compliance by Open Source Software
directoryScan.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 */
6 
12 #include "directoryScan.hpp"
13 
14 using namespace std;
15 namespace fs = boost::filesystem;
16 
23 void scanDirectory(const bool json, const string &directoryPath)
24 {
25  fs::recursive_directory_iterator dirIterator(directoryPath);
26  fs::recursive_directory_iterator end;
27 
28  OjoAgent agentObj;
29 
30  vector<string> filePaths;
31 
32  for (fs::path const &p : boost::make_iterator_range(dirIterator, {}))
33  {
34  if (fs::is_directory(p))
35  {
36  // Can not do anything with a directory
37  continue;
38  }
39  // Store the paths in a vector as of now since we can not `#pragma omp for`
40  // on recursive_directory_iterator
41  filePaths.push_back(p.string());
42  }
43  const unsigned long filePathsSize = filePaths.size();
44  bool printComma = false;
45 
46  if (json)
47  {
48  cout << "[" << endl;
49  }
50 #pragma omp parallel shared(printComma)
51  {
52 #pragma omp for
53  for (unsigned int i = 0; i < filePathsSize; i++)
54  {
55  const string fileName = filePaths[i];
56 
57  vector<ojomatch> l;
58  try
59  {
60  l = agentObj.processFile(fileName);
61  }
62  catch (std::runtime_error &e)
63  {
64  cerr << "Unable to read " << e.what();
65  continue;
66  }
67  pair<string, vector<ojomatch>> scanResult(fileName, l);
68  if (json)
69  {
70  appendToJson(fileName, scanResult, printComma);
71  }
72  else
73  {
74  printResultToStdout(fileName, scanResult);
75  }
76  }
77  }
78  if (json)
79  {
80  cout << endl << "]" << endl;
81  }
82 }
void printResultToStdout(const std::vector< tuple< string, string, bool >> &resultPair, const std::string &fileName)
void appendToJson(const std::vector< tuple< string, string, bool >> &resultPair, const std::string &fileName, bool &printComma)
void scanDirectory(const bool json, const string &directoryPath)