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.
27 #include <unordered_map>
35 #include "path_manager.h"
37 static bool iCompare(const std::string& a, int aOffset, const std::string& b, int bOffset, int length)
39 return static_cast<int>(a.length()) - length >= aOffset &&
40 static_cast<int>(b.length()) - length >= bOffset &&
41 std::equal(b.begin() + bOffset, b.begin() + bOffset + length, a.begin() + aOffset,
42 [](unsigned char a, unsigned char b)
43 { return std::tolower(a) == std::tolower(b); });
46 bool isManagedAssembly(const std::string& fileName)
48 return (iCompare(fileName, fileName.size()-4, ".dll", 0, 4) ||
49 iCompare(fileName, fileName.size()-4, ".exe", 0, 4)) &&
50 !isNativeImage(fileName);
53 bool isNativeImage(const std::string& fileName)
55 return iCompare(fileName, fileName.size()-7, ".ni", 0, 3);
58 std::string readSelfPath()
61 ssize_t len = ::readlink("/proc/self/exe", buff, sizeof(buff)-1);
64 return std::string(buff);
70 std::string concatPath(const std::string& path1, const std::string& path2)
72 std::string path(path1);
73 if (path.back() == PATH_SEPARATOR) {
76 path += PATH_SEPARATOR;
83 void splitPath(const std::string& path, std::vector<std::string>& out)
85 std::istringstream ss(path);
88 while (std::getline(ss, token, ':')) {
93 std::string absolutePath(const std::string& path)
96 char realPath[PATH_MAX];
97 if (realpath(path.c_str(), realPath) != nullptr && realPath[0] != '\0')
98 absPath.assign(realPath);
103 std::string baseName(const std::string& path)
105 auto pos = path.find_last_of(PATH_SEPARATOR);
106 if (pos != std::string::npos)
107 return path.substr(0, pos);
109 return std::string(".");
113 bool isFileExist(const std::string& path)
116 return stat(path.c_str(), &sb) == 0;
119 std::string stripNiDLL(const std::string& path)
121 std::string niPath(path);
122 if (path.size() < 5) return niPath;
123 if (!strncasecmp(path.c_str() + path.size() - 4, ".dll", 4))
124 niPath = path.substr(0, path.size()-4);
125 else if (!strncasecmp(path.c_str() + path.size() - 4, ".exe", 4))
126 niPath = path.substr(0, path.size()-4);
128 if (!strncasecmp(niPath.c_str() + niPath.size() - 3, ".ni", 3))
129 return niPath.substr(0, niPath.size()-3);
134 void assembliesInDirectory(const std::vector<std::string>& directories, std::string& tpaList)
136 std::map<std::string, std::string> assemblyList;
137 std::map<std::string, std::string> tmpList;
139 auto reader = [&assemblyList, &tmpList] (const std::string& path, const char* name) {
140 if (isManagedAssembly(path) || isNativeImage(path)) {
141 std::string dllName = stripNiDLL(name);
142 std::pair<std::map<std::string, std::string>::iterator, bool> ret;
143 ret = tmpList.insert(std::pair<std::string, std::string>(dllName, path));
144 if (ret.second == false) {
145 if (isNativeImage(path))
146 tmpList[dllName] = path;
151 for (auto directory : directories) {
152 scanFilesInDir(directory.c_str(), reader, 1);
153 // merge scaned dll list to tpa list.
154 // if the dll is already exist in the list, that is skipped.
155 assemblyList.insert(tmpList.begin(), tmpList.end());
158 std::map<std::string, std::string>::iterator it;
159 for (it = assemblyList.begin(); it != assemblyList.end(); it++)
160 tpaList += it->second + ':';
162 if (tpaList.back() == ':')
166 void scanFilesInDir(const std::string& directory, FileReader reader, unsigned int depth)
169 struct dirent* entry;
172 if (strstr(directory.c_str(), "TAC.Release") != NULL)
173 return; // skip nitool --regen-all-app (--r2r)
175 dir = opendir(directory.c_str());
180 std::vector<std::string> innerDirectories;
182 while ((entry = readdir(dir)) != nullptr) {
184 std::string path = concatPath(directory, entry->d_name);
185 switch (entry->d_type) {
193 if (stat(path.c_str(), &sb) == -1)
196 if (S_ISREG(sb.st_mode) || S_ISDIR(sb.st_mode))
202 reader(path, entry->d_name);
203 else if (depth > 1 && strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
204 innerDirectories.push_back(path);
208 for (auto& d : innerDirectories)
209 scanFilesInDir(d.c_str(), reader, depth - 1);
214 static bool setOwnership(const bf::path& path, uid_t uid, gid_t gid) {
215 int fd = open(path.c_str(), O_RDONLY);
217 _ERR("Can't open directory: %s", path.c_str());
220 int ret = fchown(fd, uid, gid);
223 _ERR("Failed to change owner of: %s", path.c_str());
229 static bool setDirPermissions(const bf::path& path, bf::perms permissions) {
230 bs::error_code error;
231 bf::permissions(path, permissions, error);
233 _ERR("Failed to set permissions for directory: %s, %s", path.c_str(), error.message().c_str());
239 static bool setDirOwnershipAndPermissions(const bf::path& path, bf::perms permissions, uid_t uid, gid_t gid) {
240 if (!setOwnership(path, uid, gid)) {
241 _ERR("Failed to change owner: %s, (uid: %d, gid: %d)", path.c_str(), uid, gid);
244 if (!setDirPermissions(path, permissions)) {
245 _ERR("Failed to change permission: %s, (%d)", path.c_str(), permissions);
251 static bool copyOwnershipAndPermissions(const bf::path& path, const bf::path& path2) {
252 if (!bf::exists(path)) {
253 _ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), path2.c_str());
256 bf::perms permissions = bf::status(path).permissions();
258 if (stat(path.c_str(), &stats) != 0) {
261 if (!setDirOwnershipAndPermissions(path2, permissions, stats.st_uid, stats.st_gid)) {
262 _ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), path2.c_str());
268 bool createDir(const bf::path& path) {
269 if (bf::exists(path)) {
272 bs::error_code error;
273 bf::create_directories(path, error);
275 _ERR("Failed to create directory: %s", error.message().c_str());
281 bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags) {
283 // Check whether the function call is valid
284 if (!bf::exists(path1) || !bf::is_directory(path1)) {
285 _ERR("Source directory %s does not exist or is not a directory", path1.c_str());
288 if (!bf::exists(path2)) {
289 // Create the destination directory
290 if (!createDir(path2)) {
291 _ERR("Unable to create destination directory %s", path2.c_str());
294 if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS) {
295 copyOwnershipAndPermissions(path1, path2);
298 if (!(flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE))) {
299 _ERR("Destination directory %s already exists", path2.c_str());
302 if (flags & (FS_MERGE_OVERWRITE | FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS)) {
303 copyOwnershipAndPermissions(path1, path2);
306 } catch (const bf::filesystem_error& error) {
307 _ERR("Failed to copy directory: %s", error.what());
311 // Iterate through the source directory
312 for (bf::directory_iterator file(path1); file != bf::directory_iterator(); ++file) {
314 bf::path current(file->path());
315 bf::path target = path2 / current.filename();
316 if (bf::is_symlink(symlink_status(current))) {
317 if ((flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE)) && bf::exists(target)) {
320 bs::error_code error;
321 bf::copy_symlink(current, target, error);
323 _ERR("Failed to copy symlink: %s, %s", current.c_str(), error.message().c_str());
326 } else if (bf::is_directory(current)) {
327 // Found directory: Recursion
328 if (!copyDir(current, target, flags)) {
332 if ((flags & FS_MERGE_SKIP) && bf::exists(target)) {
335 bf::path destination = target;
336 if (flags & FS_COMMIT_COPY_FILE) {
337 destination = bf::unique_path(target.parent_path() / "%%%%-%%%%-%%%%-%%%%");
339 if (flags & FS_MERGE_OVERWRITE) {
340 bf::copy_file(current, destination, bf::copy_option::overwrite_if_exists);
342 bf::copy_file(current, destination);
344 if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS) {
345 copyOwnershipAndPermissions(current, destination);
347 if (flags & FS_COMMIT_COPY_FILE) {
348 if (flags & FS_MERGE_OVERWRITE) {
351 bf::rename(destination, target);
354 } catch (const bf::filesystem_error& error) {
355 _ERR("Failed to copy directory: %s", error.what());
362 bool copyFile(const bf::path& path1, const bf::path& path2) {
363 bs::error_code error;
364 bf::copy_file(path1, path2, bf::copy_option::overwrite_if_exists, error);
366 _ERR("copy file %s due to error [%s]", path1.c_str(), error.message().c_str());
372 bool moveFile(const bf::path& path1, const bf::path& path2) {
373 if (bf::exists(path2)) {
376 bs::error_code error;
377 bf::rename(path1, path2, error);
379 _ERR("Cannot move file: %s. Will copy/remove... with error [%s]", path1.c_str(), error.message().c_str());
380 bf::copy_file(path1, path2, bf::copy_option::overwrite_if_exists, error);
382 _ERR("Cannot copy file %s due to error [%s]", path1.c_str(), error.message().c_str());
385 bf::remove_all(path1, error);
387 _ERR("Cannot remove old file when coping: %s with error [%s]", path1.c_str(), error.message().c_str());
394 bool removeFile(const bf::path& path) {
395 if (!bf::exists(path)) {
398 bs::error_code error;
399 bf::remove(path, error);
401 _ERR("Cannot remove: %s, %s", path.c_str(), error.message().c_str());
407 bool removeAll(const bf::path& path) {
411 bs::error_code error;
412 bf::remove_all(path, error);
414 _ERR("Cannot remove: %s, %s", path.c_str(), error.message().c_str());