Modified to pass the argument by constant reference (#242)
[platform/core/dotnet/launcher.git] / NativeLauncher / util / utils.cc
index 45a61f6..f399b2c 100644 (file)
@@ -22,6 +22,8 @@
 #include <strings.h>
 #include <pkgmgr-info.h>
 #include <pkgmgr_installer_info.h>
+#include <sys/smack.h>
+#include <sys/prctl.h>
 
 #include <cstdlib>
 #include <cstring>
@@ -29,6 +31,7 @@
 #include <unordered_map>
 #include <vector>
 #include <iterator>
+#include <fstream>
 #include <sstream>
 #include <map>
 
@@ -97,17 +100,19 @@ void splitPath(const std::string& path, std::vector<std::string>& out)
        }
 }
 
-std::string absolutePath(const std::string& path)
+std::string getAbsolutePath(const std::string& path)
 {
        std::string absPath;
-       char realPath[PATH_MAX];
-       if (realpath(path.c_str(), realPath) != nullptr && realPath[0] != '\0')
+       char *realPath = realpath(path.c_str(), NULL);
+       if (realPath) {
                absPath.assign(realPath);
+               free(realPath);
+       }
 
        return absPath;
 }
 
-int getRootPath(std::string pkgId, std::string& rootPath)
+int getRootPath(const std::string& pkgId, std::string& rootPath)
 {
        int ret = 0;
        char *path = 0;
@@ -141,7 +146,78 @@ int getRootPath(std::string pkgId, std::string& rootPath)
        return 0;
 }
 
-std::string baseName(const std::string& path)
+int getExecName(const std::string& pkgId, std::string& execName)
+{
+       char *exec = NULL;
+       char *appId = 0;
+
+       pkgmgrinfo_pkginfo_h pkg_handle;
+       int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &pkg_handle);
+       if (ret != PMINFO_R_OK) {
+               return -1;
+       }
+       ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId);
+       if (ret != PMINFO_R_OK) {
+               pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
+               return -1;
+       }
+
+       pkgmgrinfo_appinfo_h app_handle;
+       ret = pkgmgrinfo_appinfo_get_appinfo(appId, &app_handle);
+       if (ret != PMINFO_R_OK) {
+               pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
+               return -1;
+       }
+       ret = pkgmgrinfo_appinfo_get_exec(app_handle, &exec);
+       if (ret != PMINFO_R_OK) {
+               pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
+               pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
+               return -1;
+       }
+       execName = std::string(exec).substr(std::string(exec).rfind('/') + 1);
+
+       pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
+       pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
+       return 0;
+}
+
+int getMetadataValue(const std::string& pkgId, const std::string& metadataKey, std::string& metadataValue)
+{
+       char *value = NULL;
+       char *appId = 0;
+
+       pkgmgrinfo_pkginfo_h pkg_handle;
+       int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &pkg_handle);
+       if (ret != PMINFO_R_OK) {
+               return -1;
+       }
+       ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId);
+       if (ret != PMINFO_R_OK) {
+               pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
+               return -1;
+       }
+
+       pkgmgrinfo_appinfo_h app_handle;
+       ret = pkgmgrinfo_appinfo_get_appinfo(appId, &app_handle);
+       if (ret != PMINFO_R_OK) {
+               pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
+               return -1;
+       }
+       ret = pkgmgrinfo_appinfo_get_metadata_value(app_handle, metadataKey.c_str(), &value);
+       if (ret != PMINFO_R_OK) {
+               pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
+               pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
+               //Does not return error because the metadata key may not exist.
+               return 0;
+       }
+       metadataValue = std::string(value);
+
+       pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
+       pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
+       return 0;
+}
+
+std::string getBaseName(const std::string& path)
 {
        auto pos = path.find_last_of(PATH_SEPARATOR);
        if (pos != std::string::npos)
@@ -151,67 +227,104 @@ std::string baseName(const std::string& path)
        return path;
 }
 
-bool isFileExist(const std::string& path)
+std::string replaceAll(const std::string& str, const std::string& pattern, const std::string& replace)
+{
+       std::string result = str;
+       std::string::size_type pos = 0;
+       std::string::size_type offset = 0;
+
+       while ((pos = result.find(pattern, offset)) != std::string::npos) {
+               result.replace(result.begin() + pos, result.begin() + pos + pattern.size(), replace);
+               offset = pos + replace.size();
+       }
+
+       return result;
+}
+
+bool isFile(const std::string& path)
 {
        struct stat sb;
        return stat(path.c_str(), &sb) == 0;
 }
 
-std::string stripNiDLL(const std::string& path)
+bool isDirectory(const std::string& path)
 {
-       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);
+       struct stat sb;
+       if (stat(path.c_str(), &sb) != 0) {
+               return false;
+       } else if (sb.st_mode & S_IFDIR) {
+               return true;
+       } else {
+               return false;
+       }
+}
 
-       if (!strncasecmp(niPath.c_str() + niPath.size() - 3, ".ni", 3))
-               return niPath.substr(0, niPath.size()-3);
+uintptr_t getFileSize(const std::string& path)
+{
+       struct stat sb;
 
-       return niPath;
+       if (stat(path.c_str(), &sb) == 0) {
+               return sb.st_size;
+       }
+
+       return 0;
 }
 
-void assembliesInDirectory(const std::vector<std::string>& directories, std::string& tpaList)
+std::string getAssemblyNameFromPath(const std::string& path)
 {
-       std::map<std::string, std::string> assemblyList;
-       std::map<std::string, std::string> tmpList;
-
-       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;
+       std::string ret(getFileName(path));
+
+       if (ret.find_last_of(".") == std::string::npos)
+               return ret;
+       ret.erase(ret.find_last_of("."));
+
+       if (ret.size() > 3 && std::equal(ret.begin() + ret.size() - 3, ret.end(), ".ni"))
+               ret.erase(ret.size() - 3);
+
+       return ret;
+}
+
+void addAssembliesFromDirectories(const std::vector<std::string>& directories, std::string& list) {
+       std::vector<std::string> assems;
+       std::unordered_map<std::string, std::string> assemPaths;
+
+       auto reader = [&assems, &assemPaths](const std::string& path, const std::string& filename) {
+               if (isManagedAssembly(filename) || isNativeImage(filename)) {
+                       std::string assem = getAssemblyNameFromPath(filename);
+
+                       if (assemPaths.count(assem) == 0) {
+                               assems.push_back(assem);
+                               assemPaths[assem] = path;
+                       } else if (isManagedAssembly(assemPaths[assem]) && isNativeImage(filename)) {
+                               // Update only if a native image is found in the same directory.
+                               // For example, if we have two directories = { X, Y } where X contains A.dll and
+                               // Y contains both A.dll and A.ni.dll, always A.dll in X will be used.
+                               if (getBaseName(assemPaths[assem]).compare(getBaseName(path)) == 0)
+                                       assemPaths[assem] = path;
                        }
                }
        };
+       for (auto& directory : directories)
+               scanFilesInDirectory(directory, reader, 0);
 
-       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());
-       }
+       if (!list.empty() && list.back() != ':')
+               list.push_back(':');
 
-       std::map<std::string, std::string>::iterator it;
-       for (it = assemblyList.begin(); it != assemblyList.end(); it++)
-               tpaList += it->second + ':';
+       for (auto& assem : assems)
+               list += assemPaths[assem] + ":";
 
-       if (tpaList.back() == ':')
-               tpaList.pop_back();
+       if (list.back() == ':')
+               list.pop_back();
 }
 
-void scanFilesInDir(const std::string& directory, FileReader reader, unsigned int depth)
+void scanFilesInDirectory(const std::string& directory, FileReader reader, unsigned int depth)
 {
        DIR *dir;
        struct dirent* entry;
        bool isDir;
 
-       if (strstr(directory.c_str(), ".TAC.Release") != NULL)
-               return; // skip nitool --regen-all-app (--r2r)
+       if (strstr(directory.c_str(), TAC_SYMLINK_SUB_DIR) != NULL)
+               return;
 
        dir = opendir(directory.c_str());
 
@@ -241,17 +354,53 @@ void scanFilesInDir(const std::string& directory, FileReader reader, unsigned in
                }
                if (!isDir)
                        reader(path, entry->d_name);
-               else if (depth > 1 && strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
+               else if (depth > 0 && strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
                        innerDirectories.push_back(path);
        }
 
-       if (depth != 0)
+       if (depth > 0)
                for (auto& d : innerDirectories)
-                       scanFilesInDir(d.c_str(), reader, depth - 1);
+                       scanFilesInDirectory(d, reader, depth - 1);
 
        closedir(dir);
 }
 
+void copySmackAndOwnership(const std::string& fromPath, const std::string& toPath, bool isSymlink)
+{
+       char* label = NULL;
+       struct stat info;
+
+       if (isSymlink) {
+               // change smack label for symbolic link.
+               if (smack_lgetlabel(fromPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
+                       if (smack_lsetlabel(toPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
+                               fprintf(stderr, "Fail to set smack label\n");
+                       }
+                       free(label);
+               }
+
+               // change owner and groups for symbolic link.
+               if (!stat(fromPath.c_str(), &info)) {
+                       if (lchown(toPath.c_str(), info.st_uid, info.st_gid) == -1)
+                               fprintf(stderr, "Failed to change owner and group name\n");
+               }
+       } else {
+               // change smack label
+               if (smack_getlabel(fromPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
+                       if (smack_setlabel(toPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
+                               fprintf(stderr, "Fail to set smack label\n");
+                       }
+                       free(label);
+               }
+
+               // change owner and groups for generated ni file.
+               if (!stat(fromPath.c_str(), &info)) {
+                       if (chown(toPath.c_str(), info.st_uid, info.st_gid) == -1)
+                               fprintf(stderr, "Failed to change owner and group name\n");
+               }
+       }
+}
+
 static bool setOwnership(const bf::path& path, uid_t uid, gid_t gid) {
        int fd = open(path.c_str(), O_RDONLY);
        if (fd < 0) {
@@ -402,6 +551,9 @@ bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags) {
 
 bool copyFile(const bf::path& path1, const bf::path& path2) {
        bs::error_code error;
+       if (!bf::exists(path1)) {
+               return false;
+       }
        bf::copy_file(path1, path2, bf::copy_option::overwrite_if_exists, error);
        if (error) {
                _ERR("copy file %s due to error [%s]", path1.c_str(), error.message().c_str());
@@ -411,7 +563,7 @@ bool copyFile(const bf::path& path1, const bf::path& path2) {
 }
 
 bool moveFile(const bf::path& path1, const bf::path& path2) {
-       if (bf::exists(path2)) {
+       if (!bf::exists(path1) || bf::exists(path2)) {
                return false;
        }
        bs::error_code error;
@@ -456,4 +608,25 @@ bool removeAll(const bf::path& path) {
                return false;
        }
        return true;
-}
\ No newline at end of file
+}
+
+void setCmdName(const std::string& name)
+{
+       #define PRC_NAME_LENGTH         16
+
+       char processName[PRC_NAME_LENGTH] = {0, };
+
+       if (name.empty())
+               return;
+
+       memset(processName, '\0', PRC_NAME_LENGTH);
+       snprintf(processName, PRC_NAME_LENGTH, "%s", name.c_str());
+       prctl(PR_SET_NAME, processName);
+}
+
+std::string getFileName(const std::string& path)
+{
+       std::string ret(path);
+       size_t index = ret.find_last_of(PATH_SEPARATOR);
+       return index == std::string::npos ? ret : ret.substr(index + 1);
+}