2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <unordered_map>
33 #include "path_manager.h"
35 static bool iCompare(const std::string& a, int aOffset, const std::string& b, int bOffset, int length)
37 return static_cast<int>(a.length()) - length >= aOffset &&
38 static_cast<int>(b.length()) - length >= bOffset &&
39 std::equal(b.begin() + bOffset, b.begin() + bOffset + length, a.begin() + aOffset,
40 [](unsigned char a, unsigned char b)
41 { return std::tolower(a) == std::tolower(b); });
44 bool isManagedAssembly(const std::string& fileName)
46 return (iCompare(fileName, fileName.size()-4, ".dll", 0, 4) ||
47 iCompare(fileName, fileName.size()-4, ".exe", 0, 4)) &&
48 !isNativeImage(fileName);
51 bool isNativeImage(const std::string& fileName)
53 return iCompare(fileName, fileName.size()-7, ".ni", 0, 3);
56 std::string readSelfPath()
59 ssize_t len = ::readlink("/proc/self/exe", buff, sizeof(buff)-1);
62 return std::string(buff);
68 std::string concatPath(const std::string& path1, const std::string& path2)
70 std::string path(path1);
71 if (path.back() == PATH_SEPARATOR) {
74 path += PATH_SEPARATOR;
81 void splitPath(const std::string& path, std::vector<std::string>& out)
83 std::istringstream ss(path);
86 while (std::getline(ss, token, ':')) {
91 std::string absolutePath(const std::string& path)
94 char realPath[PATH_MAX];
95 if (realpath(path.c_str(), realPath) != nullptr && realPath[0] != '\0')
96 absPath.assign(realPath);
101 std::string baseName(const std::string& path)
103 auto pos = path.find_last_of(PATH_SEPARATOR);
104 if (pos != std::string::npos)
105 return path.substr(0, pos);
107 return std::string(".");
111 bool isFileExist(const std::string& path)
114 return stat(path.c_str(), &sb) == 0;
117 std::string stripNiDLL(const std::string& path)
119 std::string niPath(path);
120 if (path.size() < 5) return niPath;
121 if (!strncasecmp(path.c_str() + path.size() - 4, ".dll", 4))
122 niPath = path.substr(0, path.size()-4);
123 else if (!strncasecmp(path.c_str() + path.size() - 4, ".exe", 4))
124 niPath = path.substr(0, path.size()-4);
126 if (!strncasecmp(niPath.c_str() + niPath.size() - 3, ".ni", 3))
127 return niPath.substr(0, niPath.size()-3);
132 void assembliesInDirectory(const std::vector<std::string>& directories, std::string& tpaList)
134 std::map<std::string, std::string> assemblyList;
135 std::map<std::string, std::string> tmpList;
137 auto reader = [&assemblyList, &tmpList] (const std::string& path, const char* name) {
138 if (isManagedAssembly(path) || isNativeImage(path)) {
139 std::string dllName = stripNiDLL(name);
140 std::pair<std::map<std::string, std::string>::iterator, bool> ret;
141 ret = tmpList.insert(std::pair<std::string, std::string>(dllName, path));
142 if (ret.second == false) {
143 if (isNativeImage(path))
144 tmpList[dllName] = path;
149 for (auto directory : directories) {
150 scanFilesInDir(directory.c_str(), reader, 1);
151 // merge scaned dll list to tpa list.
152 // if the dll is already exist in the list, that is skipped.
153 assemblyList.insert(tmpList.begin(), tmpList.end());
156 std::map<std::string, std::string>::iterator it;
157 for (it = assemblyList.begin(); it != assemblyList.end(); it++)
158 tpaList += it->second + ':';
160 if (tpaList.back() == ':')
164 void scanFilesInDir(const std::string& directory, FileReader reader, unsigned int depth)
167 struct dirent* entry;
170 dir = opendir(directory.c_str());
175 std::vector<std::string> innerDirectories;
177 while ((entry = readdir(dir)) != nullptr) {
179 std::string path = concatPath(directory, entry->d_name);
180 switch (entry->d_type) {
188 if (stat(path.c_str(), &sb) == -1)
191 if (S_ISREG(sb.st_mode) || S_ISDIR(sb.st_mode))
197 reader(path, entry->d_name);
198 else if (depth > 1 && strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
199 innerDirectories.push_back(path);
203 for (auto& d : innerDirectories)
204 scanFilesInDir(d.c_str(), reader, depth-1);