blob: 78db5343dd6c59fd70172df970b6f470b72871d1 [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>
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050022#include <stdlib.h>
Ethan Yonker00028b42014-04-09 14:29:02 -050023#include "find_file.hpp"
24#include "twrp-functions.hpp"
25#include "twcommon.h"
26
27using namespace std;
28
29string Find_File::Find(const string& file_name, const string& start_path) {
30 return Find_File().Find_Internal(file_name, start_path);
31}
32
33Find_File::Find_File() {
34}
35
36string Find_File::Find_Internal(const string& filename, const string& starting_path) {
Vojtech Bocekcbe764c2014-05-12 13:10:04 +020037 DIR *d;
Ethan Yonker00028b42014-04-09 14:29:02 -050038 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 Bocekcbe764c2014-05-12 13:10:04 +020049 d = opendir(starting_path.c_str());
Ethan Yonker00028b42014-04-09 14:29:02 -050050 if (d == NULL) {
51 LOGERR("Find_File: Error opening '%s'\n", starting_path.c_str());
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}