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  * Author: Gaurav Mishra <mishra.gaurav@siemens.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2
7  * as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  * See the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
24 #include "directoryScan.hpp"
25 
26 using namespace std;
27 namespace fs = boost::filesystem;
28 
29 void scanDirectory(const CopyrightState& state, const bool json,
30  const string directoryPath)
31 {
32  fs::recursive_directory_iterator dirIterator(directoryPath);
33  fs::recursive_directory_iterator end;
34 
35  vector<string> filePaths;
36 
37  for (fs::path const &p : boost::make_iterator_range(dirIterator, {}))
38  {
39  if (fs::is_directory(p))
40  {
41  // Can not do anything with a directory
42  continue;
43  }
44  // Store the paths in a vector as of now since we can not `#pragma omp for`
45  // on recursive_directory_iterator
46  filePaths.push_back(p.string());
47  }
48  const unsigned long filePathsSize = filePaths.size();
49  bool printComma = false;
50 
51  if (json)
52  {
53  cout << "[" << endl;
54  }
55 
56 #pragma omp parallel
57  {
58 #pragma omp for
59  for (unsigned int i = 0; i < filePathsSize; i++)
60  {
61  string fileName = filePaths[i];
62  pair<string, list<match>> scanResult = processSingleFile(state, fileName);
63  if (json)
64  {
65  appendToJson(fileName, scanResult, printComma);
66  }
67  else
68  {
69  printResultToStdout(fileName, scanResult);
70  }
71  }
72  }
73  if (json)
74  {
75  cout << endl << "]" << endl;
76  }
77 }
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)
Holds information about state of one agent.
pair< string, list< match > > processSingleFile(const CopyrightState &state, const string fileName)