#include <pkgmgr-info.h>
#include <pkgmgr_installer_info.h>
#include <sys/smack.h>
-#include <json/json.h>
+#include <sys/prctl.h>
#include <cstdlib>
#include <cstring>
return iCompare(fileName, fileName.size()-7, ".ni", 0, 3);
}
-bool cmdOptionExists(char** begin, char** end, const std::string& option)
-{
- return std::find(begin, end, option) != end;
-}
-
-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 "";
-}
-
std::string concatPath(const std::string& path1, const std::string& path2)
{
std::string path(path1);
}
}
-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)
+std::string getRootPath(const std::string& pkgId)
{
int ret = 0;
char *path = 0;
uid_t uid = 0;
+ std::string rootPath;
if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
_ERR("Failed to get UID");
- return -1;
+ return rootPath;
}
pkgmgrinfo_pkginfo_h handle;
if (uid == 0) {
ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle);
if (ret != PMINFO_R_OK) {
- return -1;
+ return rootPath;
}
} else {
ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
if (ret != PMINFO_R_OK) {
- return -1;
+ return rootPath;
}
}
ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
if (ret != PMINFO_R_OK) {
pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
- return -1;
+ return rootPath;
}
rootPath = path;
pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
- return 0;
+
+ return rootPath;
}
-int getExecName(std::string pkgId, std::string& execName)
+std::string getExecName(const std::string& pkgId)
{
char *exec = NULL;
char *appId = 0;
+ std::string execName;
pkgmgrinfo_pkginfo_h pkg_handle;
int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &pkg_handle);
if (ret != PMINFO_R_OK) {
- return -1;
+ return execName;
}
ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId);
if (ret != PMINFO_R_OK) {
pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
- return -1;
+ return execName;
}
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;
+ return execName;
}
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;
+ return execName;
}
execName = std::string(exec).substr(std::string(exec).rfind('/') + 1);
pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
- return 0;
+
+ return execName;
+}
+
+std::string getAppType(const std::string& pkgId)
+{
+ char *appId = 0;
+ char *type = 0;
+ std::string appType;
+
+ pkgmgrinfo_pkginfo_h pkg_handle;
+ int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &pkg_handle);
+ if (ret != PMINFO_R_OK) {
+ return appType;
+ }
+ ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId);
+ if (ret != PMINFO_R_OK) {
+ pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
+ return appType;
+ }
+
+ 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 appType;
+ }
+ ret = pkgmgrinfo_appinfo_get_apptype(app_handle, &type);
+ if (ret != PMINFO_R_OK) {
+ pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
+ pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
+ return appType;
+ }
+ appType = type;
+
+ pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
+ pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
+
+ return appType;
}
-int getMetadataValue(std::string pkgId, std::string metadataKey, std::string& metadataValue)
+std::string getMetadataValue(const std::string& pkgId, const std::string& key)
{
char *value = NULL;
char *appId = 0;
+ std::string metadataValue;
pkgmgrinfo_pkginfo_h pkg_handle;
int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &pkg_handle);
if (ret != PMINFO_R_OK) {
- return -1;
+ return metadataValue;
}
ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId);
if (ret != PMINFO_R_OK) {
pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
- return -1;
+ return metadataValue;
}
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;
+ return metadataValue;
}
- ret = pkgmgrinfo_appinfo_get_metadata_value(app_handle, metadataKey.c_str(), &value);
+ ret = pkgmgrinfo_appinfo_get_metadata_value(app_handle, key.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;
+ return metadataValue;
}
metadataValue = std::string(value);
pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
- return 0;
+
+ return metadataValue;
}
-std::string baseName(const std::string& path)
+std::string getBaseName(const std::string& path)
{
auto pos = path.find_last_of(PATH_SEPARATOR);
if (pos != std::string::npos)
return path;
}
-std::string replaceAll(const std::string &str, const std::string &pattern, const std::string &replace)
+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;
return result;
}
-bool isFileExist(const std::string& path)
+bool isFile(const std::string& path)
{
struct stat sb;
return stat(path.c_str(), &sb) == 0;
}
-uintptr_t getFileSize(const std::string& path)
+bool isDirectory(const std::string& path)
{
struct stat sb;
-
- if (stat(path.c_str(), &sb) == 0) {
- return sb.st_size;
+ if (stat(path.c_str(), &sb) != 0) {
+ return false;
+ }
+ if (sb.st_mode & S_IFDIR) {
+ return true;
+ } else {
+ return false;
}
-
- return 0;
}
-std::string stripNiDLL(const std::string& path)
+std::string getAssemblyNameFromPath(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);
+ std::string ret(getFileName(path));
- if (!strncasecmp(niPath.c_str() + niPath.size() - 3, ".ni", 3))
- return niPath.substr(0, niPath.size()-3);
+ if (ret.find_last_of(".") == std::string::npos)
+ return ret;
+ ret.erase(ret.find_last_of("."));
- return niPath;
+ if (ret.size() > 3 && std::equal(ret.begin() + ret.size() - 3, ret.end(), ".ni"))
+ ret.erase(ret.size() - 3);
+
+ return ret;
}
-void assembliesInDirectory(const std::vector<std::string>& directories, std::string& tpaList)
+void addAssembliesFromDirectories(const std::vector<std::string>& directories, std::string& list)
{
- 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::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;
}
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 updateAssemblyInfo(const std::string& getPath, const std::string& setPath, bool isSymlink)
+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(getPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
- if (smack_lsetlabel(setPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
+ 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(getPath.c_str(), &info)) {
- if (lchown(setPath.c_str(), info.st_uid, info.st_gid) == -1)
+ 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(getPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
- if (smack_setlabel(setPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
+ 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(getPath.c_str(), &info)) {
- if (chown(setPath.c_str(), info.st_uid, info.st_gid) == -1)
+ 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) {
+static bool setOwnership(const bf::path& path, uid_t uid, gid_t gid)
+{
int fd = open(path.c_str(), O_RDONLY);
if (fd < 0) {
_ERR("Can't open directory: %s", path.c_str());
return true;
}
-static bool setDirPermissions(const bf::path& path, bf::perms permissions) {
+static bool setDirPermissions(const bf::path& path, bf::perms permissions)
+{
bs::error_code error;
bf::permissions(path, permissions, error);
if (error) {
return true;
}
-static bool setDirOwnershipAndPermissions(const bf::path& path, bf::perms permissions, uid_t uid, gid_t gid) {
+static bool setDirOwnershipAndPermissions(const bf::path& path, bf::perms permissions, uid_t uid, gid_t gid)
+{
if (!setOwnership(path, uid, gid)) {
_ERR("Failed to change owner: %s, (uid: %d, gid: %d)", path.c_str(), uid, gid);
return false;
return true;
}
-static bool copyOwnershipAndPermissions(const bf::path& path, const bf::path& path2) {
+static bool copyOwnershipAndPermissions(const bf::path& path, const bf::path& path2)
+{
if (!bf::exists(path)) {
_ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), path2.c_str());
return false;
return true;
}
-bool createDir(const bf::path& path) {
+bool createDir(const bf::path& path)
+{
if (bf::exists(path)) {
return true;
}
return true;
}
-bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags) {
+bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags)
+{
try {
// Check whether the function call is valid
if (!bf::exists(path1) || !bf::is_directory(path1)) {
return true;
}
-bool copyFile(const bf::path& path1, const bf::path& path2) {
+bool copyFile(const bf::path& path1, const bf::path& path2)
+{
bs::error_code error;
if (!bf::exists(path1)) {
return false;
return true;
}
-bool moveFile(const bf::path& path1, const bf::path& path2) {
+bool moveFile(const bf::path& path1, const bf::path& path2)
+{
if (!bf::exists(path1) || bf::exists(path2)) {
return false;
}
return true;
}
-bool removeFile(const bf::path& path) {
+bool removeFile(const bf::path& path)
+{
if (!bf::exists(path)) {
return true;
}
return true;
}
-bool removeAll(const bf::path& path) {
+bool removeAll(const bf::path& path)
+{
if (!exists(path)) {
return true;
}
return true;
}
-//Parser the .deps.json file to get nuget information.
-std::vector<std::string> depsJsonParser(std::string rootPath, std::string execName, std::string tpaList)
+void setCmdName(const std::string& name)
{
- std::vector<std::string> tpaAssemblies;
- splitPath(tpaList, tpaAssemblies);
-
- std::vector<std::string> parserData;
- std::string depsJsonName = execName.substr(0, execName.rfind(".dll")) + ".deps.json";
- std::string depsJsonPath = concatPath(rootPath, depsJsonName);
- if (bf::exists(depsJsonPath)) {
- std::ifstream ifs(depsJsonPath);
- Json::CharReaderBuilder reader;
- Json::Value root;
- std::string error;
- if (ifs.is_open()) {
- if (!Json::parseFromStream(reader, ifs, &root, &error)) {
- _ERR("Failed to parse of deps.json");
- ifs.close();
- tpaAssemblies.clear();
- return parserData;
- }
- const Json::Value runtimeTargetName = root["runtimeTarget"]["name"];
- const Json::Value nugetPackages = root["targets"][runtimeTargetName.asString().c_str()];
- std::vector<std::string> appDependencies;
- for (auto& nuget : nugetPackages.getMemberNames()) {
- if (strstr(nuget.c_str(), (execName.substr(0, execName.find(".Tizen."))).c_str()) != NULL ||
- strstr(nuget.c_str(), (execName.substr(0, execName.find(".dll"))).c_str()) != NULL) {
- const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
- if (assemblies != Json::nullValue) {
- const Json::Value dependencies = nugetPackages[nuget.c_str()]["dependencies"];
- for (auto& dependency : dependencies.getMemberNames()) {
- appDependencies.push_back(dependency);
- }
- }
- }
- }
- for (auto& nuget : nugetPackages.getMemberNames()) {
- //Skip the nuget package related to Tizen
- if (strstr(nuget.c_str(), TIZEN_DOTNET_NUGET) == NULL &&
- strstr(nuget.c_str(), TIZEN_DOTNET_SDK_NUGET) == NULL &&
- strstr(nuget.c_str(), (execName.substr(0, execName.find(".Tizen."))).c_str()) == NULL &&
- strstr(nuget.c_str(), (execName.substr(0, execName.find(".dll"))).c_str()) == NULL) {
- const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
- if (assemblies != Json::nullValue) {
- const Json::Value dependencies = nugetPackages[nuget.c_str()]["dependencies"];
- bool hasDependency = false;
- for (auto& dependency : dependencies.getMemberNames()) {
- //Skip the nugget package that is dependent on another nuget package
- if (strstr(dependency.c_str(), TIZEN_DOTNET_NUGET) == NULL &&
- strstr(dependency.c_str(), NET_STANDARD_LIBRARY_NUGET) == NULL) {
- hasDependency = true;
- for (auto& ad : appDependencies) {
- if (!strcmp(ad.c_str(), dependency.c_str())) {
- hasDependency = true;
- break;
- } else {
- hasDependency = false;
- }
- }
- }
- }
- if (!hasDependency) {
- bool isExistTpaAssembly = false;
- for (auto& assembly : assemblies.getMemberNames()) {
- std::string assemblyName = assembly.substr(assembly.rfind('/') + 1);
- //Skip the assembly present in the TPA list
- for (auto& tpa : tpaAssemblies) {
- if (!strcmp(replaceAll(tpa, ".ni.dll", ".dll").c_str(), assembly.c_str())) {
- isExistTpaAssembly = true;
- break;
- }
- }
- if (isExistTpaAssembly) break;
- }
- if (!isExistTpaAssembly) {
- for (auto& assembly : assemblies.getMemberNames()) {
- std::string assemblyName = assembly.substr(assembly.rfind('/') + 1);
- parserData.push_back(nuget + ":" + assemblyName);
- _INFO("Nuget : [%s] / Assembly : [%s]", nuget.c_str(), assemblyName.c_str());
- }
- }
- }
- }
- }
- }
- appDependencies.clear();
- ifs.close();
- }
- }
- tpaAssemblies.clear();
- return parserData;
-}
\ No newline at end of file
+ #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);
+}