Ethan Yonker | 00028b4 | 2014-04-09 14:29:02 -0500 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2014 TeamWin |
| 3 | This file is part of TWRP/TeamWin Recovery Project. |
| 4 | |
| 5 | TWRP is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | TWRP is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <string> |
| 20 | #include <vector> |
| 21 | #include <dirent.h> |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 22 | #include <stdlib.h> |
Ethan Yonker | 00028b4 | 2014-04-09 14:29:02 -0500 | [diff] [blame] | 23 | #include "find_file.hpp" |
| 24 | #include "twrp-functions.hpp" |
| 25 | #include "twcommon.h" |
| 26 | |
| 27 | using namespace std; |
| 28 | |
| 29 | string Find_File::Find(const string& file_name, const string& start_path) { |
| 30 | return Find_File().Find_Internal(file_name, start_path); |
| 31 | } |
| 32 | |
| 33 | Find_File::Find_File() { |
| 34 | } |
| 35 | |
| 36 | string Find_File::Find_Internal(const string& filename, const string& starting_path) { |
Vojtech Bocek | cbe764c | 2014-05-12 13:10:04 +0200 | [diff] [blame] | 37 | DIR *d; |
Ethan Yonker | 00028b4 | 2014-04-09 14:29:02 -0500 | [diff] [blame] | 38 | string new_path, return_path; |
| 39 | vector<string> dirs; |
| 40 | vector<string> symlinks; |
| 41 | unsigned index; |
| 42 | |
| 43 | // Check to see if we have already searched this directory to prevent infinite loops |
| 44 | if (std::find(searched_dirs.begin(), searched_dirs.end(), starting_path) != searched_dirs.end()) { |
| 45 | return ""; |
| 46 | } |
| 47 | searched_dirs.push_back(starting_path); |
| 48 | |
Vojtech Bocek | cbe764c | 2014-05-12 13:10:04 +0200 | [diff] [blame] | 49 | d = opendir(starting_path.c_str()); |
Ethan Yonker | 00028b4 | 2014-04-09 14:29:02 -0500 | [diff] [blame] | 50 | if (d == NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 51 | LOGINFO("Find_File: Error opening '%s'\n", starting_path.c_str()); |
Ethan Yonker | 00028b4 | 2014-04-09 14:29:02 -0500 | [diff] [blame] | 52 | return ""; |
| 53 | } |
| 54 | |
| 55 | struct dirent *p; |
| 56 | while ((p = readdir(d))) { |
| 57 | if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) |
| 58 | continue; |
| 59 | new_path = starting_path + "/"; |
| 60 | new_path.append(p->d_name); |
| 61 | if (p->d_type == DT_DIR) { |
| 62 | // Add dir to search list for later |
| 63 | dirs.push_back(new_path); |
| 64 | } else if (p->d_type == DT_LNK) { |
| 65 | // Add symlink to search list for later |
| 66 | symlinks.push_back(new_path); |
| 67 | } else if (p->d_type == DT_REG && filename == p->d_name) { |
| 68 | // We found a match! |
| 69 | closedir(d); |
| 70 | return new_path; |
| 71 | } |
| 72 | } |
| 73 | closedir(d); |
| 74 | |
| 75 | // Scan real directories first if no match found in this path |
| 76 | for (index = 0; index < dirs.size(); index++) { |
| 77 | return_path = Find_Internal(filename, dirs.at(index)); |
| 78 | if (!return_path.empty()) return return_path; |
| 79 | } |
| 80 | // Scan symlinks after scanning real directories |
| 81 | for (index = 0; index < symlinks.size(); index++) { |
| 82 | char buf[PATH_MAX]; |
| 83 | // Resolve symlink to a real path |
| 84 | char* ret = realpath(symlinks.at(index).c_str(), buf); |
| 85 | if (ret) { |
| 86 | return_path = Find_Internal(filename, buf); |
| 87 | if (!return_path.empty()) return return_path; |
| 88 | } |
| 89 | } |
| 90 | return ""; |
| 91 | } |