FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
directoryScan.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 modify
5  * it under the terms of the GNU General Public License version 2
6  * 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.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software Foundation,
15  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16  */
17 
23 #include "directoryScan.hpp"
24 
25 using namespace std;
26 namespace fs = boost::filesystem;
27 
34 void scanDirectory(const bool json, const string &directoryPath)
35 {
36  fs::recursive_directory_iterator dirIterator(directoryPath);
37  fs::recursive_directory_iterator end;
38 
39  OjoAgent agentObj;
40 
41  vector<string> filePaths;
42 
43  for (fs::path const &p : boost::make_iterator_range(dirIterator, {}))
44  {
45  if (fs::is_directory(p))
46  {
47  // Can not do anything with a directory
48  continue;
49  }
50  // Store the paths in a vector as of now since we can not `#pragma omp for`
51  // on recursive_directory_iterator
52  filePaths.push_back(p.string());
53  }
54  const unsigned long filePathsSize = filePaths.size();
55  bool printComma = false;
56 
57  if (json)
58  {
59  cout << "[" << endl;
60  }
61 #pragma omp parallel shared(printComma)
62  {
63 #pragma omp for
64  for (unsigned int i = 0; i < filePathsSize; i++)
65  {
66  const string fileName = filePaths[i];
67 
68  vector<ojomatch> l;
69  try
70  {
71  l = agentObj.processFile(fileName);
72  }
73  catch (std::runtime_error &e)
74  {
75  cerr << "Unable to read " << e.what();
76  continue;
77  }
78  pair<string, vector<ojomatch>> scanResult(fileName, l);
79  if (json)
80  {
81  appendToJson(fileName, scanResult, printComma);
82  }
83  else
84  {
85  printResultToStdout(fileName, scanResult);
86  }
87  }
88  }
89  if (json)
90  {
91  cout << endl << "]" << endl;
92  }
93 }
void printResultToStdout(const std::string fileName, const std::pair< string, list< match >> resultPair)
void scanDirectory(const bool json, const string &directoryPath)
void appendToJson(const std::string fileName, const std::pair< string, list< match >> resultPair, bool &printComma)