* limitations under the License.
*/
-
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <vector>
#include <iterator>
#include <sstream>
+#include <map>
#include "utils.h"
+#include "path_manager.h"
-bool ICompare(const std::string& a, const std::string& b)
+static bool iCompare(const std::string& a, int aOffset, const std::string& b, int bOffset, int length)
{
- return a.length() == b.length() &&
- std::equal(b.begin(), b.end(), a.begin(),
- [](unsigned char a, unsigned char b)
- { return std::tolower(a) == std::tolower(b); });
+ return static_cast<int>(a.length()) - length >= aOffset &&
+ static_cast<int>(b.length()) - length >= bOffset &&
+ std::equal(b.begin() + bOffset, b.begin() + bOffset + length, a.begin() + aOffset,
+ [](unsigned char a, unsigned char b)
+ { return std::tolower(a) == std::tolower(b); });
}
-bool ICompare(const std::string& a, int a_offset, const std::string& b, int b_offset, int length)
+bool isManagedAssembly(const std::string& fileName)
{
- return static_cast<int>(a.length()) - length >= a_offset &&
- static_cast<int>(b.length()) - length >= b_offset &&
- std::equal(b.begin() + b_offset, b.begin() + b_offset + length, a.begin() + a_offset,
- [](unsigned char a, unsigned char b)
- { return std::tolower(a) == std::tolower(b); });
+ return (iCompare(fileName, fileName.size()-4, ".dll", 0, 4) ||
+ iCompare(fileName, fileName.size()-4, ".exe", 0, 4)) &&
+ !isNativeImage(fileName);
}
-bool IsManagedAssembly(const std::string& filename)
+bool isNativeImage(const std::string& fileName)
{
- return ICompare(filename, filename.size()-4, ".dll", 0, 4) ||
- ICompare(filename, filename.size()-4, ".exe", 0, 4);
+ return iCompare(fileName, fileName.size()-7, ".ni", 0, 3);
}
-bool IsNativeImage(const std::string& filename)
+std::string readSelfPath()
{
- return ICompare(filename, filename.size()-7, ".ni", 0, 3);
-}
+ char buff[PATH_MAX];
+ ssize_t len = ::readlink("/proc/self/exe", buff, sizeof(buff)-1);
+ if (len != -1) {
+ buff[len] = '\0';
+ return std::string(buff);
+ }
-std::string ReadSelfPath()
-{
- char buff[PATH_MAX];
- ssize_t len = ::readlink("/proc/self/exe", buff, sizeof(buff)-1);
- if (len != -1) {
- buff[len] = '\0';
- return std::string(buff);
- }
-
- return "";
+ return "";
}
-std::string ConcatPath(const std::string& path1, const std::string& path2)
+std::string concatPath(const std::string& path1, const std::string& path2)
{
- std::string path(path1);
- if (path.back() == PATH_SEPARATOR)
- {
- path.append(path2);
- }
- else
- {
- path += PATH_SEPARATOR;
- path.append(path2);
- }
-
- return path;
-}
+ std::string path(path1);
+ if (path.back() == PATH_SEPARATOR) {
+ path.append(path2);
+ } else {
+ path += PATH_SEPARATOR;
+ path.append(path2);
+ }
-void AppendPath(std::string& path1, const std::string& path2)
-{
- if (path1.back() == PATH_SEPARATOR)
- {
- path1.append(path2);
- }
- else
- {
- path1 += PATH_SEPARATOR;
- path1.append(path2);
- }
+ return path;
}
-std::string AbsolutePath(const std::string& path)
+void splitPath(const std::string& path, std::vector<std::string>& out)
{
- std::string absPath;
-
- char realPath[PATH_MAX];
- if (realpath(path.c_str(), realPath) != nullptr && realPath[0] != '\0')
- {
- absPath.assign(realPath);
- }
+ std::istringstream ss(path);
+ std::string token;
- return absPath;
+ while (std::getline(ss, token, ':')) {
+ out.push_back(token);
+ }
}
-std::string Basename(const std::string& path)
+std::string absolutePath(const std::string& path)
{
- auto pos = path.find_last_of(PATH_SEPARATOR);
- if (pos != std::string::npos)
- {
- return path.substr(0, pos);
- }
- else
- {
- return std::string(".");
- }
- return path;
-}
+ std::string absPath;
+ char realPath[PATH_MAX];
+ if (realpath(path.c_str(), realPath) != nullptr && realPath[0] != '\0')
+ absPath.assign(realPath);
-bool EndWithIgnoreCase(const std::string& str1, const std::string& str2, std::string& filename)
-{
- std::string::size_type len1 = str1.length();
- std::string::size_type len2 = str2.length();
- if (len2 > len1) return false;
-
- int i = 0;
- bool result = std::all_of(str1.cend() - len2, str1.end(),
- [&i, &str2] (char x) {
- return std::tolower(x) == std::tolower(str2[i++]);
- });
- if (result)
- {
- filename = str1.substr(0, len1 - len2);
- }
- return result;
+ return absPath;
}
-bool FileNotExist(const std::string& path)
+std::string baseName(const std::string& path)
{
- struct stat sb;
- return stat(path.c_str(), &sb) != 0;
+ auto pos = path.find_last_of(PATH_SEPARATOR);
+ if (pos != std::string::npos)
+ return path.substr(0, pos);
+ else
+ return std::string(".");
+ return path;
}
-static bool ExtCheckAndGetFileNameIfExist(const std::string& dir, const std::string& ext, struct dirent* entry, std::string& filename)
+bool isFileExist(const std::string& path)
{
- std::string fname(entry->d_name);
- if (fname.length() < ext.length() ||
- fname.compare(fname.length() - ext.length(), ext.length(), ext) != 0)
- {
- return false;
- }
- std::string fullname = ConcatPath(dir, entry->d_name);
- switch (entry->d_type)
- {
- case DT_REG: break;
- case DT_LNK:
- case DT_UNKNOWN:
- if (FileNotExist(fullname))
- {
- return false;
- }
- default:
- return false;
- }
-
- filename = fullname;
-
- return true;
+ struct stat sb;
+ return stat(path.c_str(), &sb) == 0;
}
-std::string StripNIDLL(const std::string& path)
+std::string stripNiDLL(const std::string& path)
{
- std::string npath(path);
- if (path.size() < 5) return npath;
- if (!strncasecmp(path.c_str() + path.size() - 4, ".dll", 4))
- {
- npath = path.substr(0, path.size()-4);
- }else if (!strncasecmp(path.c_str() + path.size() - 4, ".exe", 4))
- {
- npath = path.substr(0, path.size()-4);
- }
- if (!strncasecmp(npath.c_str() + npath.size() - 3, ".ni", 3))
- {
- return npath.substr(0, npath.size()-3);
- }
- return npath;
-}
+ std::string niPath(path);
+ if (path.size() < 5) return niPath;
+ if (!strncasecmp(path.c_str() + path.size() - 4, ".dll", 4))
+ niPath = path.substr(0, path.size()-4);
+ else if (!strncasecmp(path.c_str() + path.size() - 4, ".exe", 4))
+ niPath = path.substr(0, path.size()-4);
-std::string JoinStrings(const std::vector<std::string>& strings, const char* const delimeter)
-{
- switch (strings.size())
- {
- case 0:
- return "";
- case 1:
- return strings[0];
- default:
- std::ostringstream os;
- std::copy(strings.begin(), strings.end()-1, std::ostream_iterator<std::string>(os, delimeter));
- os << *strings.rbegin();
- return os.str();
- }
+ if (!strncasecmp(niPath.c_str() + niPath.size() - 3, ".ni", 3))
+ return niPath.substr(0, niPath.size()-3);
+
+ return niPath;
}
-struct AssemblyFile
+void assembliesInDirectory(const std::vector<std::string>& directories, std::string& tpaList)
{
- std::string noext;
- std::string ext;
-};
+ std::map<std::string, std::string> assemblyList;
+ std::map<std::string, std::string> tmpList;
-bool operator == (const AssemblyFile& lhs, const AssemblyFile& rhs)
-{
- return lhs.noext == rhs.noext && lhs.ext == rhs.ext;
-}
+ auto reader = [&assemblyList, &tmpList] (const std::string& path, const char* name) {
+ if (isManagedAssembly(path) || isNativeImage(path)) {
+ std::string dllName = stripNiDLL(name);
+ std::pair<std::map<std::string, std::string>::iterator, bool> ret;
+ ret = tmpList.insert(std::pair<std::string, std::string>(dllName, path));
+ if (ret.second == false) {
+ if (isNativeImage(path))
+ tmpList[dllName] = path;
+ }
+ }
+ };
-namespace std
-{
- template<>
- struct hash<AssemblyFile>
- {
- std::size_t operator () (const AssemblyFile& f) const
- {
- const std::size_t h1 = std::hash<std::string>{}(f.noext);
- const std::size_t h2 = std::hash<std::string>{}(f.ext);
-
- return h1 ^ (h2 << 1);
- }
- };
+ for (auto directory : directories) {
+ scanFilesInDir(directory.c_str(), reader, 1);
+ // merge scaned dll list to tpa list.
+ // if the dll is already exist in the list, that is skipped.
+ assemblyList.insert(tmpList.begin(), tmpList.end());
+ }
+
+ std::map<std::string, std::string>::iterator it;
+ for (it = assemblyList.begin(); it != assemblyList.end(); it++)
+ tpaList += it->second + ':';
+
+ if (tpaList.back() == ':')
+ tpaList.pop_back();
}
-void AssembliesInDirectory(const std::vector<std::string>& directories, std::string& tpaList)
+void scanFilesInDir(const std::string& directory, FileReader reader, unsigned int depth)
{
- std::map<std::string, std::string> assemblyList;
- std::map<std::string, std::string> tmpList;
-
- auto reader = [&assemblyList, &tmpList] (const char* path, const char* name)
- {
- std::string _path(path);
- if (IsManagedAssembly(_path))
- {
- std::string dll_name = StripNIDLL(name);
- std::pair<std::map<std::string, std::string>::iterator, bool> ret;
- ret = tmpList.insert(std::pair<std::string, std::string>(dll_name, _path));
- if (ret.second == false)
- {
- if (IsNativeImage(_path))
- {
- tmpList[dll_name] = _path;
- }
- }
- }
- };
-
- for (auto directory : directories)
- {
- ScanFilesInDir(directory.c_str(), reader, 1);
- // merge scaned dll list to tpa list.
- // if the dll is already exist in the list, that is skipped.
- assemblyList.insert(tmpList.begin(), tmpList.end());
- }
-
- std::map<std::string, std::string>::iterator it;
- for (it = assemblyList.begin(); it != assemblyList.end(); it++)
- {
- tpaList += it->second + ':';
- }
- if (tpaList.back() == ':')
- {
- tpaList.pop_back();
- }
+ DIR *dir;
+ struct dirent* entry;
+ bool isDir;
+
+ dir = opendir(directory.c_str());
+
+ if (dir == nullptr)
+ return;
+
+ std::vector<std::string> innerDirectories;
+
+ while ((entry = readdir(dir)) != nullptr) {
+ isDir = false;
+ std::string path = concatPath(directory, entry->d_name);
+ switch (entry->d_type) {
+ case DT_REG: break;
+ case DT_DIR:
+ isDir = true;
+ break;
+ case DT_LNK:
+ case DT_UNKNOWN:
+ struct stat sb;
+ if (stat(path.c_str(), &sb) == -1)
+ continue;
+
+ if (S_ISREG(sb.st_mode) || S_ISDIR(sb.st_mode))
+ break;
+ default:
+ continue;
+ }
+ if (!isDir)
+ reader(path, entry->d_name);
+ else if (depth > 1 && strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
+ innerDirectories.push_back(path);
+ }
+
+ if (depth != 0)
+ for (auto& d : innerDirectories)
+ scanFilesInDir(d.c_str(), reader, depth-1);
+
+ closedir(dir);
}
-void ScanFilesInDir(const char* directory, FileReader reader, unsigned int depth)
-{
- DIR *dir;
- struct dirent* entry;
- bool isDir;
-
- dir = opendir(directory);
-
- if (dir == nullptr)
- {
- //_ERR("Can not open directory : %s", directory);
- return;
- }
-
- std::vector<std::string> innerDirectories;
-
- while ((entry = readdir(dir)) != nullptr)
- {
- isDir = false;
- std::string path = ConcatPath(directory, entry->d_name);
- switch (entry->d_type)
- {
- case DT_REG: break;
- case DT_DIR:
- isDir = true;
- break;
- case DT_LNK:
- case DT_UNKNOWN:
- struct stat sb;
- if (stat(path.c_str(), &sb) == -1)
- {
- continue;
- }
-
- if (S_ISREG(sb.st_mode) || S_ISDIR(sb.st_mode))
- {
- break;
- }
- default:
- continue;
- }
- if (!isDir)
- {
- reader(path.c_str(), entry->d_name);
- }
- else if (depth > 1 && strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
- {
- innerDirectories.push_back(path);
- }
- }
-
- if (depth != 0)
- {
- for (auto& d : innerDirectories)
- {
- ScanFilesInDir(d.c_str(), reader, depth-1);
- }
- }
-
- closedir(dir);
-}
\ No newline at end of file