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.
28 #include <unordered_map>
37 static pthread_t loggingThread;
39 bool iCompare(const std::string& a, const std::string& b)
41 return a.length() == b.length() &&
42 std::equal(b.begin(), b.end(), a.begin(),
43 [](unsigned char a, unsigned char b)
44 { return std::tolower(a) == std::tolower(b); });
47 bool iCompare(const std::string& a, int aOffset, const std::string& b, int bOffset, int length)
49 return static_cast<int>(a.length()) - length >= aOffset &&
50 static_cast<int>(b.length()) - length >= bOffset &&
51 std::equal(b.begin() + bOffset, b.begin() + bOffset + length, a.begin() + aOffset,
52 [](unsigned char a, unsigned char b)
53 { return std::tolower(a) == std::tolower(b); });
56 bool isManagedAssembly(const std::string& fileName)
58 return iCompare(fileName, fileName.size()-4, ".dll", 0, 4) ||
59 iCompare(fileName, fileName.size()-4, ".exe", 0, 4);
62 bool isNativeImage(const std::string& fileName)
64 return iCompare(fileName, fileName.size()-7, ".ni", 0, 3);
67 std::string readSelfPath()
70 ssize_t len = ::readlink("/proc/self/exe", buff, sizeof(buff)-1);
73 return std::string(buff);
79 std::string concatPath(const std::string& path1, const std::string& path2)
81 std::string path(path1);
82 if (path.back() == PATH_SEPARATOR) {
85 path += PATH_SEPARATOR;
92 void appendPath(std::string& path1, const std::string& path2)
94 if (path1.back() == PATH_SEPARATOR) {
97 path1 += PATH_SEPARATOR;
102 std::string absolutePath(const std::string& path)
105 char realPath[PATH_MAX];
106 if (realpath(path.c_str(), realPath) != nullptr && realPath[0] != '\0')
107 absPath.assign(realPath);
112 std::string baseName(const std::string& path)
114 auto pos = path.find_last_of(PATH_SEPARATOR);
115 if (pos != std::string::npos)
116 return path.substr(0, pos);
118 return std::string(".");
122 bool endWithIgnoreCase(const std::string& str1, const std::string& str2, std::string& fileName)
124 std::string::size_type len1 = str1.length();
125 std::string::size_type len2 = str2.length();
130 bool result = std::all_of(str1.cend() - len2, str1.end(),
131 [&i, &str2] (char x) {
132 return std::tolower(x) == std::tolower(str2[i++]);
135 fileName = str1.substr(0, len1 - len2);
140 bool fileNotExist(const std::string& path)
143 return stat(path.c_str(), &sb) != 0;
146 #ifdef NOT_USE_FUNCTION
147 static bool extCheckAndGetFileNameIfExist(const std::string& dir, const std::string& ext, struct dirent* entry, std::string& fileName)
149 std::string fName(entry->d_name);
150 if (fName.length() < ext.length() ||
151 fHame.compare(fName.length() - ext.length(), ext.length(), ext) != 0) {
155 std::string fullName = concatPath(dir, entry->d_name);
156 switch (entry->d_type) {
160 if (fileNotExist(fullName))
172 std::string stripNiDLL(const std::string& path)
174 std::string niPath(path);
175 if (path.size() < 5) return niPath;
176 if (!strncasecmp(path.c_str() + path.size() - 4, ".dll", 4))
177 niPath = path.substr(0, path.size()-4);
178 else if (!strncasecmp(path.c_str() + path.size() - 4, ".exe", 4))
179 niPath = path.substr(0, path.size()-4);
181 if (!strncasecmp(niPath.c_str() + niPath.size() - 3, ".ni", 3))
182 return niPath.substr(0, niPath.size()-3);
187 std::string joinStrings(const std::vector<std::string>& strings, const char* const delimeter)
189 switch (strings.size()) {
195 std::ostringstream os;
196 std::copy(strings.begin(), strings.end()-1, std::ostream_iterator<std::string>(os, delimeter));
197 os << *strings.rbegin();
202 struct AssemblyFile {
207 bool operator == (const AssemblyFile& lhs, const AssemblyFile& rhs)
209 return lhs.noExt == rhs.noExt && lhs.ext == rhs.ext;
214 struct hash<AssemblyFile> {
215 std::size_t operator () (const AssemblyFile& f) const {
216 const std::size_t h1 = std::hash<std::string>{}(f.noExt);
217 const std::size_t h2 = std::hash<std::string>{}(f.ext);
219 return h1 ^ (h2 << 1);
224 void assembliesInDirectory(const std::vector<std::string>& directories, std::string& tpaList)
226 std::map<std::string, std::string> assemblyList;
227 std::map<std::string, std::string> tmpList;
229 auto reader = [&assemblyList, &tmpList] (const char* path, const char* name) {
230 std::string pathStr(path);
231 if (isManagedAssembly(pathStr)) {
232 std::string dllName = stripNiDLL(name);
233 std::pair<std::map<std::string, std::string>::iterator, bool> ret;
234 ret = tmpList.insert(std::pair<std::string, std::string>(dllName, pathStr));
235 if (ret.second == false) {
236 if (isNativeImage(pathStr))
237 tmpList[dllName] = pathStr;
242 for (auto directory : directories) {
243 scanFilesInDir(directory.c_str(), reader, 1);
244 // merge scaned dll list to tpa list.
245 // if the dll is already exist in the list, that is skipped.
246 assemblyList.insert(tmpList.begin(), tmpList.end());
249 std::map<std::string, std::string>::iterator it;
250 for (it = assemblyList.begin(); it != assemblyList.end(); it++)
251 tpaList += it->second + ':';
253 if (tpaList.back() == ':')
257 void scanFilesInDir(const char* directory, FileReader reader, unsigned int depth)
260 struct dirent* entry;
263 dir = opendir(directory);
268 std::vector<std::string> innerDirectories;
270 while ((entry = readdir(dir)) != nullptr) {
272 std::string path = concatPath(directory, entry->d_name);
273 switch (entry->d_type) {
281 if (stat(path.c_str(), &sb) == -1)
284 if (S_ISREG(sb.st_mode) || S_ISDIR(sb.st_mode))
290 reader(path.c_str(), entry->d_name);
291 else if (depth > 1 && strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
292 innerDirectories.push_back(path);
296 for (auto& d : innerDirectories)
297 scanFilesInDir(d.c_str(), reader, depth-1);
302 static void *stdlog(void*)
307 while ((readSize = read(pfd[0], buf, sizeof buf - 1)) > 0) {
308 if (buf[readSize - 1] == '\n') {
320 int runLoggingThread() { // run this function to redirect your output to android log
321 if (setvbuf(stdout, NULL, _IOLBF, 0) < 0) {
322 _DBG("fail to make stdout line-buffered");
326 if (setvbuf(stderr, NULL, _IONBF, 0) < 0) {
327 _DBG("make stderr unbuffered");
331 /* create the pipe and redirect stdout and stderr */
333 _DBG("fail to create pipe for logging");
337 if (dup2(pfd[1], fileno(stdout)) == -1) {
338 _DBG("fail to duplicate fd to stdout");
342 if (dup2(pfd[1], fileno(stderr)) == -1) {
343 _DBG("fail to duplicate fd to stderr");
347 /* spawn the logging thread */
348 if (pthread_create(&loggingThread, 0, stdlog, 0) != 0) {
349 _DBG("fail to create pthread");
353 if (pthread_detach(loggingThread) != 0) {
354 _DBG("fail to detach pthread");