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>
25 #include <sys/smack.h>
26 #include <sys/prctl.h>
31 #include <unordered_map>
40 #include "path_manager.h"
42 static bool iCompare(const std::string& a, int aOffset, const std::string& b, int bOffset, int length)
44 return static_cast<int>(a.length()) - length >= aOffset &&
45 static_cast<int>(b.length()) - length >= bOffset &&
46 std::equal(b.begin() + bOffset, b.begin() + bOffset + length, a.begin() + aOffset,
47 [](unsigned char a, unsigned char b)
48 { return std::tolower(a) == std::tolower(b); });
51 bool isManagedAssembly(const std::string& fileName)
53 return (iCompare(fileName, fileName.size()-4, ".dll", 0, 4) ||
54 iCompare(fileName, fileName.size()-4, ".exe", 0, 4)) &&
55 !isNativeImage(fileName);
58 bool isNativeImage(const std::string& fileName)
60 return iCompare(fileName, fileName.size()-7, ".ni", 0, 3);
63 std::string concatPath(const std::string& path1, const std::string& path2)
65 std::string path(path1);
66 if (path.back() == PATH_SEPARATOR) {
69 path += PATH_SEPARATOR;
76 void splitPath(const std::string& path, std::vector<std::string>& out)
78 std::istringstream ss(path);
81 while (std::getline(ss, token, ':')) {
86 std::string getAbsolutePath(const std::string& path)
89 char *realPath = realpath(path.c_str(), NULL);
91 absPath.assign(realPath);
98 std::string getRootPath(const std::string& pkgId)
103 std::string rootPath;
105 if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
106 _ERR("Failed to get UID");
110 pkgmgrinfo_pkginfo_h handle;
112 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle);
113 if (ret != PMINFO_R_OK) {
117 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
118 if (ret != PMINFO_R_OK) {
123 ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
124 if (ret != PMINFO_R_OK) {
125 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
129 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
134 std::string getExecName(const std::string& pkgId)
138 std::string execName;
140 pkgmgrinfo_pkginfo_h pkg_handle;
141 int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &pkg_handle);
142 if (ret != PMINFO_R_OK) {
145 ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId);
146 if (ret != PMINFO_R_OK) {
147 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
151 pkgmgrinfo_appinfo_h app_handle;
152 ret = pkgmgrinfo_appinfo_get_appinfo(appId, &app_handle);
153 if (ret != PMINFO_R_OK) {
154 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
157 ret = pkgmgrinfo_appinfo_get_exec(app_handle, &exec);
158 if (ret != PMINFO_R_OK) {
159 pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
160 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
163 execName = std::string(exec).substr(std::string(exec).rfind('/') + 1);
165 pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
166 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
171 std::string getAppType(const std::string& pkgId)
177 pkgmgrinfo_pkginfo_h pkg_handle;
178 int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &pkg_handle);
179 if (ret != PMINFO_R_OK) {
182 ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId);
183 if (ret != PMINFO_R_OK) {
184 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
188 pkgmgrinfo_appinfo_h app_handle;
189 ret = pkgmgrinfo_appinfo_get_appinfo(appId, &app_handle);
190 if (ret != PMINFO_R_OK) {
191 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
194 ret = pkgmgrinfo_appinfo_get_apptype(app_handle, &type);
195 if (ret != PMINFO_R_OK) {
196 pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
197 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
202 pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
203 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
208 std::string getMetadataValue(const std::string& pkgId, const std::string& key)
212 std::string metadataValue;
214 pkgmgrinfo_pkginfo_h pkg_handle;
215 int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &pkg_handle);
216 if (ret != PMINFO_R_OK) {
217 return metadataValue;
219 ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId);
220 if (ret != PMINFO_R_OK) {
221 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
222 return metadataValue;
225 pkgmgrinfo_appinfo_h app_handle;
226 ret = pkgmgrinfo_appinfo_get_appinfo(appId, &app_handle);
227 if (ret != PMINFO_R_OK) {
228 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
229 return metadataValue;
231 ret = pkgmgrinfo_appinfo_get_metadata_value(app_handle, key.c_str(), &value);
232 if (ret != PMINFO_R_OK) {
233 pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
234 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
235 return metadataValue;
237 metadataValue = std::string(value);
239 pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
240 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
242 return metadataValue;
245 std::string getBaseName(const std::string& path)
247 auto pos = path.find_last_of(PATH_SEPARATOR);
248 if (pos != std::string::npos)
249 return path.substr(0, pos);
251 return std::string(".");
255 std::string replaceAll(const std::string& str, const std::string& pattern, const std::string& replace)
257 std::string result = str;
258 std::string::size_type pos = 0;
259 std::string::size_type offset = 0;
261 while ((pos = result.find(pattern, offset)) != std::string::npos) {
262 result.replace(result.begin() + pos, result.begin() + pos + pattern.size(), replace);
263 offset = pos + replace.size();
269 bool isFile(const std::string& path)
272 return lstat(path.c_str(), &sb) == 0;
275 bool isDirectory(const std::string& path)
278 if (stat(path.c_str(), &sb) != 0) {
281 if (sb.st_mode & S_IFDIR) {
288 std::string getAssemblyNameFromPath(const std::string& path)
290 std::string ret(getFileName(path));
292 if (ret.find_last_of(".") == std::string::npos)
294 ret.erase(ret.find_last_of("."));
296 if (ret.size() > 3 && std::equal(ret.begin() + ret.size() - 3, ret.end(), ".ni"))
297 ret.erase(ret.size() - 3);
302 void addAssembliesFromDirectories(const std::vector<std::string>& directories, std::string& list)
304 std::vector<std::string> assems;
305 std::unordered_map<std::string, std::string> assemPaths;
307 auto reader = [&assems, &assemPaths](const std::string& path, const std::string& filename) {
308 if (isManagedAssembly(filename) || isNativeImage(filename)) {
309 std::string assem = getAssemblyNameFromPath(filename);
311 if (assemPaths.count(assem) == 0) {
312 assems.push_back(assem);
313 assemPaths[assem] = path;
314 } else if (isManagedAssembly(assemPaths[assem]) && isNativeImage(filename)) {
315 // Update only if a native image is found in the same directory.
316 // For example, if we have two directories = { X, Y } where X contains A.dll and
317 // Y contains both A.dll and A.ni.dll, always A.dll in X will be used.
318 if (getBaseName(assemPaths[assem]).compare(getBaseName(path)) == 0)
319 assemPaths[assem] = path;
323 for (auto& directory : directories)
324 scanFilesInDirectory(directory, reader, 0);
326 if (!list.empty() && list.back() != ':')
329 for (auto& assem : assems)
330 list += assemPaths[assem] + ":";
332 if (list.back() == ':')
336 void scanFilesInDirectory(const std::string& directory, FileReader reader, unsigned int depth)
339 struct dirent* entry;
342 dir = opendir(directory.c_str());
347 std::vector<std::string> innerDirectories;
349 while ((entry = readdir(dir)) != nullptr) {
351 std::string path = concatPath(directory, entry->d_name);
352 switch (entry->d_type) {
357 // symlink is added to the list even if there is no original file.
358 // It used to remove broken symlinks related to TAC
367 reader(path, entry->d_name);
368 else if (depth > 0 && strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
369 innerDirectories.push_back(path);
373 for (auto& d : innerDirectories)
374 scanFilesInDirectory(d, reader, depth - 1);
379 void copySmackAndOwnership(const std::string& fromPath, const std::string& toPath, bool isSymlink)
385 // change smack label for symbolic link.
386 if (smack_lgetlabel(fromPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
387 if (smack_lsetlabel(toPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
388 fprintf(stderr, "Fail to set smack label\n");
393 // change owner and groups for symbolic link.
394 if (!stat(fromPath.c_str(), &info)) {
395 if (lchown(toPath.c_str(), info.st_uid, info.st_gid) == -1)
396 fprintf(stderr, "Failed to change owner and group name\n");
399 // change smack label
400 if (smack_getlabel(fromPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
401 if (smack_setlabel(toPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
402 fprintf(stderr, "Fail to set smack label\n");
407 // change owner and groups for generated ni file.
408 if (!stat(fromPath.c_str(), &info)) {
409 if (chown(toPath.c_str(), info.st_uid, info.st_gid) == -1)
410 fprintf(stderr, "Failed to change owner and group name\n");
415 static bool setOwnership(const bf::path& path, uid_t uid, gid_t gid)
417 int fd = open(path.c_str(), O_RDONLY);
419 _ERR("Can't open directory: %s", path.c_str());
422 int ret = fchown(fd, uid, gid);
425 _ERR("Failed to change owner of: %s", path.c_str());
431 static bool setDirPermissions(const bf::path& path, bf::perms permissions)
433 bs::error_code error;
434 bf::permissions(path, permissions, error);
436 _ERR("Failed to set permissions for directory: %s, %s", path.c_str(), error.message().c_str());
442 static bool setDirOwnershipAndPermissions(const bf::path& path, bf::perms permissions, uid_t uid, gid_t gid)
444 if (!setOwnership(path, uid, gid)) {
445 _ERR("Failed to change owner: %s, (uid: %d, gid: %d)", path.c_str(), uid, gid);
448 if (!setDirPermissions(path, permissions)) {
449 _ERR("Failed to change permission: %s, (%d)", path.c_str(), permissions);
455 static bool copyOwnershipAndPermissions(const bf::path& path, const bf::path& path2)
457 if (!bf::exists(path)) {
458 _ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), path2.c_str());
461 bf::perms permissions = bf::status(path).permissions();
463 if (stat(path.c_str(), &stats) != 0) {
466 if (!setDirOwnershipAndPermissions(path2, permissions, stats.st_uid, stats.st_gid)) {
467 _ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), path2.c_str());
473 bool createDir(const bf::path& path)
475 if (bf::exists(path)) {
478 bs::error_code error;
479 bf::create_directories(path, error);
481 _ERR("Failed to create directory: %s", error.message().c_str());
487 bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags)
490 // Check whether the function call is valid
491 if (!bf::exists(path1) || !bf::is_directory(path1)) {
492 _ERR("Source directory %s does not exist or is not a directory", path1.c_str());
495 if (!bf::exists(path2)) {
496 // Create the destination directory
497 if (!createDir(path2)) {
498 _ERR("Unable to create destination directory %s", path2.c_str());
501 if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS) {
502 copyOwnershipAndPermissions(path1, path2);
505 if (!(flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE))) {
506 _ERR("Destination directory %s already exists", path2.c_str());
509 if (flags & (FS_MERGE_OVERWRITE | FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS)) {
510 copyOwnershipAndPermissions(path1, path2);
513 } catch (const bf::filesystem_error& error) {
514 _ERR("Failed to copy directory: %s", error.what());
518 // Iterate through the source directory
519 for (bf::directory_iterator file(path1); file != bf::directory_iterator(); ++file) {
521 bf::path current(file->path());
522 bf::path target = path2 / current.filename();
523 if (bf::is_symlink(symlink_status(current))) {
524 if ((flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE)) && bf::exists(target)) {
527 bs::error_code error;
528 bf::copy_symlink(current, target, error);
530 _ERR("Failed to copy symlink: %s, %s", current.c_str(), error.message().c_str());
533 } else if (bf::is_directory(current)) {
534 // Found directory: Recursion
535 if (!copyDir(current, target, flags)) {
539 if ((flags & FS_MERGE_SKIP) && bf::exists(target)) {
542 bf::path destination = target;
543 if (flags & FS_COMMIT_COPY_FILE) {
544 destination = bf::unique_path(target.parent_path() / "%%%%-%%%%-%%%%-%%%%");
546 if (flags & FS_MERGE_OVERWRITE) {
547 bf::copy_file(current, destination, bf::copy_option::overwrite_if_exists);
549 bf::copy_file(current, destination);
551 if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS) {
552 copyOwnershipAndPermissions(current, destination);
554 if (flags & FS_COMMIT_COPY_FILE) {
555 if (flags & FS_MERGE_OVERWRITE) {
558 bf::rename(destination, target);
561 } catch (const bf::filesystem_error& error) {
562 _ERR("Failed to copy directory: %s", error.what());
569 bool copyFile(const bf::path& path1, const bf::path& path2)
571 bs::error_code error;
572 if (!bf::exists(path1)) {
575 bf::copy_file(path1, path2, bf::copy_option::overwrite_if_exists, error);
577 _ERR("copy file %s due to error [%s]", path1.c_str(), error.message().c_str());
583 bool moveFile(const bf::path& path1, const bf::path& path2)
585 if (!bf::exists(path1) || bf::exists(path2)) {
588 bs::error_code error;
589 bf::rename(path1, path2, error);
591 _ERR("Cannot move file: %s. Will copy/remove... with error [%s]", path1.c_str(), error.message().c_str());
592 bf::copy_file(path1, path2, bf::copy_option::overwrite_if_exists, error);
594 _ERR("Cannot copy file %s due to error [%s]", path1.c_str(), error.message().c_str());
597 bf::remove_all(path1, error);
599 _ERR("Cannot remove old file when coping: %s with error [%s]", path1.c_str(), error.message().c_str());
606 bool removeFile(const bf::path& path)
608 if (!bf::exists(path)) {
611 bs::error_code error;
612 bf::remove(path, error);
614 _ERR("Cannot remove: %s, %s", path.c_str(), error.message().c_str());
620 bool removeAll(const bf::path& path)
625 bs::error_code error;
626 bf::remove_all(path, error);
628 _ERR("Cannot remove: %s, %s", path.c_str(), error.message().c_str());
634 void setCmdName(const std::string& name)
636 #define PRC_NAME_LENGTH 16
638 char processName[PRC_NAME_LENGTH] = {0, };
643 memset(processName, '\0', PRC_NAME_LENGTH);
644 snprintf(processName, PRC_NAME_LENGTH, "%s", name.c_str());
645 prctl(PR_SET_NAME, processName);
648 std::string getFileName(const std::string& path)
650 std::string ret(path);
651 size_t index = ret.find_last_of(PATH_SEPARATOR);
652 return index == std::string::npos ? ret : ret.substr(index + 1);