James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2017 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <Utils.hpp> |
| 18 | #include <experimental/filesystem> |
| 19 | #include <fstream> |
| 20 | #include <regex> |
| 21 | |
| 22 | namespace fs = std::experimental::filesystem; |
| 23 | |
| 24 | bool find_files(const fs::path &dir_path, const std::string &match_string, |
| 25 | std::vector<fs::path> &found_paths, unsigned int symlink_depth) |
| 26 | { |
| 27 | if (!fs::exists(dir_path)) |
| 28 | return false; |
| 29 | |
| 30 | fs::directory_iterator end_itr; |
| 31 | std::regex search(match_string); |
| 32 | std::smatch match; |
| 33 | for (auto &p : fs::recursive_directory_iterator(dir_path)) |
| 34 | { |
| 35 | std::string path = p.path().string(); |
| 36 | if (!is_directory(p)) |
| 37 | { |
| 38 | if (std::regex_search(path, match, search)) |
| 39 | found_paths.emplace_back(p.path()); |
| 40 | } |
| 41 | // since we're using a recursve iterator, these should only be symlink |
| 42 | // dirs |
| 43 | else if (symlink_depth) |
| 44 | { |
| 45 | find_files(p.path(), match_string, found_paths, symlink_depth - 1); |
| 46 | } |
| 47 | } |
| 48 | return true; |
| 49 | } |