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.
23 #include <pkgmgr-info.h>
24 #include <pkgmgr_installer_info.h>
29 #include <unordered_map>
37 #include "path_manager.h"
39 static bool iCompare(const std::string& a, int aOffset, const std::string& b, int bOffset, int length)
41 return static_cast<int>(a.length()) - length >= aOffset &&
42 static_cast<int>(b.length()) - length >= bOffset &&
43 std::equal(b.begin() + bOffset, b.begin() + bOffset + length, a.begin() + aOffset,
44 [](unsigned char a, unsigned char b)
45 { return std::tolower(a) == std::tolower(b); });
48 bool isManagedAssembly(const std::string& fileName)
50 return (iCompare(fileName, fileName.size()-4, ".dll", 0, 4) ||
51 iCompare(fileName, fileName.size()-4, ".exe", 0, 4)) &&
52 !isNativeImage(fileName);
55 bool isNativeImage(const std::string& fileName)
57 return iCompare(fileName, fileName.size()-7, ".ni", 0, 3);
60 bool cmdOptionExists(char** begin, char** end, const std::string& option)
62 return std::find(begin, end, option) != end;
65 std::string readSelfPath()
68 ssize_t len = ::readlink("/proc/self/exe", buff, sizeof(buff)-1);
71 return std::string(buff);
77 std::string concatPath(const std::string& path1, const std::string& path2)
79 std::string path(path1);
80 if (path.back() == PATH_SEPARATOR) {
83 path += PATH_SEPARATOR;
90 void splitPath(const std::string& path, std::vector<std::string>& out)
92 std::istringstream ss(path);
95 while (std::getline(ss, token, ':')) {
100 std::string absolutePath(const std::string& path)
103 char realPath[PATH_MAX];
104 if (realpath(path.c_str(), realPath) != nullptr && realPath[0] != '\0')
105 absPath.assign(realPath);
110 int getRootPath(std::string pkgId, std::string& rootPath)
116 if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
117 _ERR("Failed to get UID");
121 pkgmgrinfo_pkginfo_h handle;
123 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle);
124 if (ret != PMINFO_R_OK) {
128 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
129 if (ret != PMINFO_R_OK) {
134 ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
135 if (ret != PMINFO_R_OK) {
136 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
140 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
144 std::string baseName(const std::string& path)
146 auto pos = path.find_last_of(PATH_SEPARATOR);
147 if (pos != std::string::npos)
148 return path.substr(0, pos);
150 return std::string(".");
154 bool isFileExist(const std::string& path)
157 return stat(path.c_str(), &sb) == 0;
160 std::string stripNiDLL(const std::string& path)
162 std::string niPath(path);
163 if (path.size() < 5) return niPath;
164 if (!strncasecmp(path.c_str() + path.size() - 4, ".dll", 4))
165 niPath = path.substr(0, path.size()-4);
166 else if (!strncasecmp(path.c_str() + path.size() - 4, ".exe", 4))
167 niPath = path.substr(0, path.size()-4);
169 if (!strncasecmp(niPath.c_str() + niPath.size() - 3, ".ni", 3))
170 return niPath.substr(0, niPath.size()-3);
175 void assembliesInDirectory(const std::vector<std::string>& directories, std::string& tpaList)
177 std::map<std::string, std::string> assemblyList;
178 std::map<std::string, std::string> tmpList;
180 auto reader = [&assemblyList, &tmpList] (const std::string& path, const char* name) {
181 if (isManagedAssembly(path) || isNativeImage(path)) {
182 std::string dllName = stripNiDLL(name);
183 std::pair<std::map<std::string, std::string>::iterator, bool> ret;
184 ret = tmpList.insert(std::pair<std::string, std::string>(dllName, path));
185 if (ret.second == false) {
186 if (isNativeImage(path))
187 tmpList[dllName] = path;
192 for (auto directory : directories) {
193 scanFilesInDir(directory.c_str(), reader, 1);
194 // merge scaned dll list to tpa list.
195 // if the dll is already exist in the list, that is skipped.
196 assemblyList.insert(tmpList.begin(), tmpList.end());
199 std::map<std::string, std::string>::iterator it;
200 for (it = assemblyList.begin(); it != assemblyList.end(); it++)
201 tpaList += it->second + ':';
203 if (tpaList.back() == ':')
207 void scanFilesInDir(const std::string& directory, FileReader reader, unsigned int depth)
210 struct dirent* entry;
213 if (strstr(directory.c_str(), ".TAC.Release") != NULL)
214 return; // skip nitool --regen-all-app (--r2r)
216 dir = opendir(directory.c_str());
221 std::vector<std::string> innerDirectories;
223 while ((entry = readdir(dir)) != nullptr) {
225 std::string path = concatPath(directory, entry->d_name);
226 switch (entry->d_type) {
234 if (stat(path.c_str(), &sb) == -1)
237 if (S_ISREG(sb.st_mode) || S_ISDIR(sb.st_mode))
243 reader(path, entry->d_name);
244 else if (depth > 1 && strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
245 innerDirectories.push_back(path);
249 for (auto& d : innerDirectories)
250 scanFilesInDir(d.c_str(), reader, depth - 1);
255 static bool setOwnership(const bf::path& path, uid_t uid, gid_t gid) {
256 int fd = open(path.c_str(), O_RDONLY);
258 _ERR("Can't open directory: %s", path.c_str());
261 int ret = fchown(fd, uid, gid);
264 _ERR("Failed to change owner of: %s", path.c_str());
270 static bool setDirPermissions(const bf::path& path, bf::perms permissions) {
271 bs::error_code error;
272 bf::permissions(path, permissions, error);
274 _ERR("Failed to set permissions for directory: %s, %s", path.c_str(), error.message().c_str());
280 static bool setDirOwnershipAndPermissions(const bf::path& path, bf::perms permissions, uid_t uid, gid_t gid) {
281 if (!setOwnership(path, uid, gid)) {
282 _ERR("Failed to change owner: %s, (uid: %d, gid: %d)", path.c_str(), uid, gid);
285 if (!setDirPermissions(path, permissions)) {
286 _ERR("Failed to change permission: %s, (%d)", path.c_str(), permissions);
292 static bool copyOwnershipAndPermissions(const bf::path& path, const bf::path& path2) {
293 if (!bf::exists(path)) {
294 _ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), path2.c_str());
297 bf::perms permissions = bf::status(path).permissions();
299 if (stat(path.c_str(), &stats) != 0) {
302 if (!setDirOwnershipAndPermissions(path2, permissions, stats.st_uid, stats.st_gid)) {
303 _ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), path2.c_str());
309 bool createDir(const bf::path& path) {
310 if (bf::exists(path)) {
313 bs::error_code error;
314 bf::create_directories(path, error);
316 _ERR("Failed to create directory: %s", error.message().c_str());
322 bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags) {
324 // Check whether the function call is valid
325 if (!bf::exists(path1) || !bf::is_directory(path1)) {
326 _ERR("Source directory %s does not exist or is not a directory", path1.c_str());
329 if (!bf::exists(path2)) {
330 // Create the destination directory
331 if (!createDir(path2)) {
332 _ERR("Unable to create destination directory %s", path2.c_str());
335 if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS) {
336 copyOwnershipAndPermissions(path1, path2);
339 if (!(flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE))) {
340 _ERR("Destination directory %s already exists", path2.c_str());
343 if (flags & (FS_MERGE_OVERWRITE | FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS)) {
344 copyOwnershipAndPermissions(path1, path2);
347 } catch (const bf::filesystem_error& error) {
348 _ERR("Failed to copy directory: %s", error.what());
352 // Iterate through the source directory
353 for (bf::directory_iterator file(path1); file != bf::directory_iterator(); ++file) {
355 bf::path current(file->path());
356 bf::path target = path2 / current.filename();
357 if (bf::is_symlink(symlink_status(current))) {
358 if ((flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE)) && bf::exists(target)) {
361 bs::error_code error;
362 bf::copy_symlink(current, target, error);
364 _ERR("Failed to copy symlink: %s, %s", current.c_str(), error.message().c_str());
367 } else if (bf::is_directory(current)) {
368 // Found directory: Recursion
369 if (!copyDir(current, target, flags)) {
373 if ((flags & FS_MERGE_SKIP) && bf::exists(target)) {
376 bf::path destination = target;
377 if (flags & FS_COMMIT_COPY_FILE) {
378 destination = bf::unique_path(target.parent_path() / "%%%%-%%%%-%%%%-%%%%");
380 if (flags & FS_MERGE_OVERWRITE) {
381 bf::copy_file(current, destination, bf::copy_option::overwrite_if_exists);
383 bf::copy_file(current, destination);
385 if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS) {
386 copyOwnershipAndPermissions(current, destination);
388 if (flags & FS_COMMIT_COPY_FILE) {
389 if (flags & FS_MERGE_OVERWRITE) {
392 bf::rename(destination, target);
395 } catch (const bf::filesystem_error& error) {
396 _ERR("Failed to copy directory: %s", error.what());
403 bool copyFile(const bf::path& path1, const bf::path& path2) {
404 bs::error_code error;
405 bf::copy_file(path1, path2, bf::copy_option::overwrite_if_exists, error);
407 _ERR("copy file %s due to error [%s]", path1.c_str(), error.message().c_str());
413 bool moveFile(const bf::path& path1, const bf::path& path2) {
414 if (bf::exists(path2)) {
417 bs::error_code error;
418 bf::rename(path1, path2, error);
420 _ERR("Cannot move file: %s. Will copy/remove... with error [%s]", path1.c_str(), error.message().c_str());
421 bf::copy_file(path1, path2, bf::copy_option::overwrite_if_exists, error);
423 _ERR("Cannot copy file %s due to error [%s]", path1.c_str(), error.message().c_str());
426 bf::remove_all(path1, error);
428 _ERR("Cannot remove old file when coping: %s with error [%s]", path1.c_str(), error.message().c_str());
435 bool removeFile(const bf::path& path) {
436 if (!bf::exists(path)) {
439 bs::error_code error;
440 bf::remove(path, error);
442 _ERR("Cannot remove: %s, %s", path.c_str(), error.message().c_str());
448 bool removeAll(const bf::path& path) {
452 bs::error_code error;
453 bf::remove_all(path, error);
455 _ERR("Cannot remove: %s, %s", path.c_str(), error.message().c_str());