blob: 8ae0a51b4afebc83e9a76f26f9636a4432ec5a2d [file] [log] [blame]
Ethan Yonker00028b42014-04-09 14:29:02 -05001/*
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>
22#include "find_file.hpp"
23#include "twrp-functions.hpp"
24#include "twcommon.h"
25
26using namespace std;
27
28string Find_File::Find(const string& file_name, const string& start_path) {
29 return Find_File().Find_Internal(file_name, start_path);
30}
31
32Find_File::Find_File() {
33}
34
35string Find_File::Find_Internal(const string& filename, const string& starting_path) {
Vojtech Bocekcbe764c2014-05-12 13:10:04 +020036 DIR *d;
Ethan Yonker00028b42014-04-09 14:29:02 -050037 string new_path, return_path;
38 vector<string> dirs;
39 vector<string> symlinks;
40 unsigned index;
41
42 // Check to see if we have already searched this directory to prevent infinite loops
43 if (std::find(searched_dirs.begin(), searched_dirs.end(), starting_path) != searched_dirs.end()) {
44 return "";
45 }
46 searched_dirs.push_back(starting_path);
47
Vojtech Bocekcbe764c2014-05-12 13:10:04 +020048 d = opendir(starting_path.c_str());
Ethan Yonker00028b42014-04-09 14:29:02 -050049 if (d == NULL) {
50 LOGERR("Find_File: Error opening '%s'\n", starting_path.c_str());
51 return "";
52 }
53
54 struct dirent *p;
55 while ((p = readdir(d))) {
56 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
57 continue;
58 new_path = starting_path + "/";
59 new_path.append(p->d_name);
60 if (p->d_type == DT_DIR) {
61 // Add dir to search list for later
62 dirs.push_back(new_path);
63 } else if (p->d_type == DT_LNK) {
64 // Add symlink to search list for later
65 symlinks.push_back(new_path);
66 } else if (p->d_type == DT_REG && filename == p->d_name) {
67 // We found a match!
68 closedir(d);
69 return new_path;
70 }
71 }
72 closedir(d);
73
74 // Scan real directories first if no match found in this path
75 for (index = 0; index < dirs.size(); index++) {
76 return_path = Find_Internal(filename, dirs.at(index));
77 if (!return_path.empty()) return return_path;
78 }
79 // Scan symlinks after scanning real directories
80 for (index = 0; index < symlinks.size(); index++) {
81 char buf[PATH_MAX];
82 // Resolve symlink to a real path
83 char* ret = realpath(symlinks.at(index).c_str(), buf);
84 if (ret) {
85 return_path = Find_Internal(filename, buf);
86 if (!return_path.empty()) return return_path;
87 }
88 }
89 return "";
90}