Add 'Tizen Assembly Cache' feature
authorj-h.choi <j-h.choi@samsung.com>
Tue, 11 Jun 2019 06:26:37 +0000 (15:26 +0900)
committerj-h.choi <j-h.choi@samsung.com>
Tue, 11 Jun 2019 08:28:15 +0000 (17:28 +0900)
NativeLauncher/inc/db_manager.h
NativeLauncher/inc/utils.h
NativeLauncher/installer-plugin/prefer_nuget_cache_plugin.cc
NativeLauncher/launcher/dotnet/dotnet_launcher.cc
NativeLauncher/tool/ni_common.cc
NativeLauncher/tool/nitool.cc
NativeLauncher/tool/tactool.cc
NativeLauncher/util/db_manager.cc
NativeLauncher/util/utils.cc

index dbedde6..59c23d5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0 (the License);
  * you may not use this file except in compliance with the License.
@@ -31,8 +31,8 @@
 
 sqlite3* dbCreate(std::string path);
 bool dbOpen(sqlite3 *tac_db, std::string path);
-bool dbClose(sqlite3 *tac_db);
-bool dbRollback(sqlite3 *tac_db);
+void dbClose(sqlite3 *tac_db);
+void dbRollback(sqlite3 *tac_db);
 bool dbUpdate(sqlite3 *tac_db, std::string path, std::string query);
 bool dbInsert(sqlite3 *tac_db, std::string path, std::string query);
 std::vector<std::string> dbSelect(sqlite3 *tac_db, std::string path, std::string query);
index 089f78c..9a4aced 100644 (file)
@@ -37,6 +37,8 @@ enum FSFlag : int {
   FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS = (1 << 3)
 };
 
+bool cmdOptionExists(char** begin, char** end, const std::string& option);
+
 /**
  * @brief get current executable path
  * return std::string path
@@ -66,6 +68,13 @@ std::string absolutePath(const std::string& path);
 std::string baseName(const std::string& path);
 
 /**
+ * @brief get root path
+ * @param[in] package id
+ * @param[out] root path
+ */
+int getRootPath(std::string pkgId, std::string& rootPath);
+
+/**
  * @brief split path with ":" delimiter and put that in the vector
  * @param[in] source path
  * @param[out] string vector
index e5269a6..64afc73 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0 (the License);
  * you may not use this file except in compliance with the License.
@@ -31,9 +31,9 @@
 #include <boost/filesystem.hpp>
 #include <glib.h>
 #include <json/json.h>
-#include <openssl/sha.h>
 #include <pkgmgr-info.h>
 #include <pkgmgr_installer_info.h>
+#include <openssl/sha.h>
 
 typedef struct Metadata {
        const char *key;
@@ -44,7 +44,7 @@ const std::string mOptUsrDotnet = "/opt/usr/dotnet";
 const std::string mTizenNET = "Tizen.NET";
 const std::string mTizenNETSdk = "Tizen.NET.Sdk";
 const std::string mNETStandardLibrary = "NETStandard.Library";
-const std::string mTacRelease = "TAC.Release";
+const std::string mTacRelease = ".TAC.Release";
 const std::string mDepsJson = ".deps.json";
 const std::string mBackup = ".bck";
 const std::string mdValue = "true";
@@ -56,12 +56,10 @@ std::vector<std::string> tacDB;
 std::vector<std::string> createDirectories;
 std::vector<std::string> updateTac;
 std::string status = "";
-
-bool isCreateDirectory = false;
-bf::path rootPath;
-bf::path execPath;
+std::string rootPath;
+std::string execName;
 bf::path binPath;
-
+bool isCreateDirectory = false;
 static sqlite3 *tac_db = NULL;
 
 int metadataCheck(GList *list)
@@ -78,7 +76,7 @@ int metadataCheck(GList *list)
        }
 }
 
-int getAppType(std::string pkgId)
+int appTypeCheck(std::string pkgId)
 {
        uid_t uid = 0;
        if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
@@ -93,7 +91,7 @@ int getAppType(std::string pkgId)
                return 0;
        }
 
-       bool dotnetExist = false;
+       bool isDotnetAppType = false;
        auto dotnetAppCounter = [] (pkgmgrinfo_appinfo_h handle, void *userData) -> int {
                char* type = nullptr;
                bool* dotnet = static_cast<bool*>(userData);
@@ -101,23 +99,22 @@ int getAppType(std::string pkgId)
                        _ERR("Failed to get app type : %s", type);
                        return 0;
                }
-               _INFO("App type : %s", type);
                if (strcmp(type, "dotnet") == 0) {
                        *dotnet = true;
                }
                return 0;
        };
 
-       if (pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP, dotnetAppCounter, &dotnetExist, uid) != PMINFO_R_OK) {
+       if (pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP, dotnetAppCounter, &isDotnetAppType, uid) != PMINFO_R_OK) {
                _ERR("Failed to get list of app in pkg : %s", pkgId.c_str());
                return -1;
        }
 
        pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-       return dotnetExist;
+       return isDotnetAppType;
 }
 
-int getExecPath(std::string pkgId)
+int getExecName(std::string pkgId)
 {
        uid_t uid = 0;
        if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
@@ -138,45 +135,7 @@ int getExecPath(std::string pkgId)
                        _ERR("Failed to get exec : %s", exec);
                        return 0;
                }
-               _INFO("exec : %s", exec);
-               execPath = bf::path(exec);
-               return 0;
-       };
-
-       if (pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP, dotnetAppCounter, NULL, uid) != PMINFO_R_OK) {
-               _ERR("Failed to get list of app in pkg : %s", pkgId.c_str());
-               return -1;
-       }
-
-       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-       return 0;
-}
-
-int getRootPath(std::string pkgId)
-{
-       uid_t uid = 0;
-       if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
-               _ERR("Failed to get UID");
-               return 0;
-       }
-
-       pkgmgrinfo_pkginfo_h handle;
-       int ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
-       if (ret != PMINFO_R_OK) {
-               _ERR("Failed to get pkg info");
-               return 0;
-       }
-
-       auto dotnetAppCounter = [] (pkgmgrinfo_appinfo_h handle, void *userData) -> int {
-               char* root = nullptr;
-               if (pkgmgrinfo_appinfo_get_root_path(handle, &root) != PMINFO_R_OK) {
-                       _ERR("Failed to get root path : %s", root);
-                       return 0;
-               }
-               _INFO("Root path : %s", root);
-               rootPath = bf::path(root);
-               binPath = root / bf::path("bin");
-               _INFO("Bin path : %s", binPath.c_str());
+               execName = std::string(exec).substr(std::string(exec).rfind('/') + 1);
                return 0;
        };
 
@@ -223,10 +182,9 @@ int SHA256(std::string path, char outputBuffer[65])
 
 int depsJsonParser()
 {
-       std::string exec_name = execPath.filename().string();
-       std::string deps_json_name = exec_name.substr(0, exec_name.rfind(".dll")) + mDepsJson;
-       if (bf::exists(rootPath / deps_json_name)) {
-               std::string deps_json_path = (rootPath / deps_json_name).string();
+       std::string deps_json_name = execName.substr(0, execName.rfind(".dll")) + mDepsJson;
+       if (bf::exists(rootPath + "/" + deps_json_name)) {
+               std::string deps_json_path = rootPath + "/" + deps_json_name;
                std::ifstream ifs(deps_json_path);
                Json::CharReaderBuilder reader;
                Json::Value root;
@@ -243,8 +201,8 @@ int depsJsonParser()
                        for (auto& nuget : nugetPackages.getMemberNames()) {
                                if (strstr(nuget.c_str(), mTizenNET.c_str()) != NULL ||
                                        strstr(nuget.c_str(), mTizenNETSdk.c_str()) != NULL ||
-                                       strstr(nuget.c_str(), exec_name.c_str()) != NULL ||
-                                       strstr(nuget.c_str(), (exec_name.substr(0, exec_name.find('.'))).c_str()) != 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) {
                                        continue;
                                } else {
                                        const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
@@ -366,15 +324,17 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *app
        _DBG("[===== PKGMGR_MDPARSER_PLUGIN_INSTALL =====]");
        _INFO("PackageID : %s", pkgId);
 
-       if (!getAppType(std::string(pkgId))) {
+       if (!appTypeCheck(std::string(pkgId))) {
                _INFO("App type is not dotnet");
                return 0;
        }
-       if (getExecPath(std::string(pkgId))) {
+       if (getExecName(std::string(pkgId)) < 0) {
                return 0;
        }
-       if (getRootPath(std::string(pkgId))) {
+       if (getRootPath(std::string(pkgId), rootPath) < 0) {
                return 0;
+       } else {
+               binPath = rootPath / bf::path("bin");
        }
        if (!metadataCheck(list)) {
                if (depsJsonParser()) {
@@ -540,15 +500,17 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *app
        _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UPGRADE =====]");
        _INFO("PackageID : %s", pkgId);
 
-       if (!getAppType(std::string(pkgId))) {
+       if (!appTypeCheck(std::string(pkgId))) {
                _INFO("App type is not dotnet");
                return 0;
        }
-       if (getExecPath(std::string(pkgId))) {
+       if (getExecName(std::string(pkgId)) < 0) {
                return 0;
        }
-       if (getRootPath(std::string(pkgId))) {
+       if (getRootPath(std::string(pkgId), rootPath) < 0) {
                return 0;
+       } else {
+               binPath = rootPath / bf::path("bin");
        }
        if (!strcmp("removed", status.c_str())) {
                _INFO("Skipped to parse of deps.json");
index e95ee60..2d10210 100644 (file)
@@ -392,7 +392,7 @@ int CoreRuntime::initialize(bool standalone)
        std::string appRoot = std::string("/proc/self/fd/") + std::to_string(fd);
        std::string appBin = concatPath(appRoot, "bin");
        std::string appLib = concatPath(appRoot, "lib");
-       std::string appTAC = concatPath(appBin, "TAC.Release");
+       std::string appTAC = concatPath(appBin, ".TAC.Release");
        std::string probePath = appBin + ":" + appLib + ":" + appTAC;
        std::string tpa = getTPA();
        std::string nativeLibPath = getExtraNativeLibDirs(appRoot) + ":" + appBin + ":" + appLib + ":" + __nativeLibDirectory;
index c6f5695..3811e97 100644 (file)
@@ -208,40 +208,6 @@ static int crossgen(const std::string& dllPath, const std::string& appPath, bool
        return 0;
 }
 
-static int getRootPath(std::string pkgId, std::string& rootPath)
-{
-       int ret = 0;
-       char *path = 0;
-
-       uid_t uid = 0;
-
-       if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
-               _ERR("Failed to get UID");
-               return -1;
-       }
-
-       pkgmgrinfo_pkginfo_h handle;
-       if (uid == 0) {
-               ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle);
-               if (ret != PMINFO_R_OK)
-                       return -1;
-       } else {
-               ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
-               if (ret != PMINFO_R_OK)
-                       return -1;
-       }
-
-       ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
-       if (ret != PMINFO_R_OK) {
-               pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-               return -1;
-       }
-       rootPath = path;
-       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-
-       return 0;
-}
-
 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
 {
        char *pkgId = NULL;
@@ -366,17 +332,17 @@ void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R)
        }
 }
 
-int createNiUnderPkgRoot(const std::string& pkgName, bool enableR2R)
+int createNiUnderPkgRoot(const std::string& pkgId, bool enableR2R)
 {
        std::string pkgRoot;
-       if (getRootPath(pkgName, pkgRoot) < 0) {
-               fprintf(stderr, "Failed to get root path from [%s]\n", pkgName.c_str());
+       if (getRootPath(pkgId, pkgRoot) < 0) {
+               fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
                return -1;
        }
 
        std::string binDir = concatPath(pkgRoot, "bin");
        std::string libDir = concatPath(pkgRoot, "lib");
-       std::string appTAC = concatPath(binDir, "TAC.Release");
+       std::string appTAC = concatPath(binDir, ".TAC.Release");
        std::string paths[] = {binDir, libDir, appTAC};
 
        createNiUnderDirs(paths, 3, enableR2R);
@@ -384,11 +350,11 @@ int createNiUnderPkgRoot(const std::string& pkgName, bool enableR2R)
        return 0;
 }
 
-int createNiDllUnderPkgRoot(const std::string& pkgName, const std::string& dllPath, bool enableR2R)
+int createNiDllUnderPkgRoot(const std::string& pkgId, const std::string& dllPath, bool enableR2R)
 {
        std::string pkgRoot;
-       if (getRootPath(pkgName, pkgRoot) < 0) {
-               fprintf(stderr, "Failed to get root path from [%s]\n", pkgName.c_str());
+       if (getRootPath(pkgId, pkgRoot) < 0) {
+               fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
                return -1;
        }
 
@@ -435,11 +401,11 @@ void removeNiUnderDirs(const std::string rootPaths[], int count)
                scanFilesInDir(rootPaths[i], convert, -1);
 }
 
-int removeNiUnderPkgRoot(const std::string& pkgName)
+int removeNiUnderPkgRoot(const std::string& pkgId)
 {
        std::string pkgRoot;
-       if (getRootPath(pkgName, pkgRoot) < 0) {
-               fprintf(stderr, "Failed to get root path from [%s]\n", pkgName.c_str());
+       if (getRootPath(pkgId, pkgRoot) < 0) {
+               fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
                return -1;
        }
 
index aac3d0e..16b6693 100644 (file)
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include "utils.h"
 #include "ni_common.h"
 
 #include <cstdio>
@@ -32,11 +33,6 @@ std::vector<std::string> getCmdArgs(char** begin, char** end)
        return list;
 }
 
-bool cmdOptionExists(char** begin, char** end, const std::string& option)
-{
-       return std::find(begin, end, option) != end;
-}
-
 static void help(const char *argv0)
 {
        const char* helpDesc =
index e932dc0..98be18c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0 (the License);
  * you may not use this file except in compliance with the License.
@@ -24,9 +24,9 @@
 #include <fstream>
 #include <vector>
 
+#include <json/json.h>
 #include <pkgmgr-info.h>
 #include <pkgmgr_installer_info.h>
-#include <json/json.h>
 
 #ifdef  LOG_TAG
 #undef  LOG_TAG
@@ -46,121 +46,48 @@ const std::string mdKey = "http://tizen.org/metadata/prefer_nuget_cache";
 const std::string mdValue = "true";
 
 static sqlite3 *tac_db = NULL;
-std::string execPath;
 std::vector<std::string> restoreNuget;
 
-std::vector<std::string> getCmdArgs(char** begin, char** end)
-{
-       std::vector<std::string> list;
-       for (char** itr = begin+1; itr != end; itr++) {
-               if (strncmp(*itr, "--", 2) != 0) {
-                       list.push_back(*itr);
-               }
-       }
-       return list;
-}
-
-bool cmdOptionExists(char** begin, char** end, const std::string& option)
-{
-       return std::find(begin, end, option) != end;
-}
-
 static void help(const char *argv0)
 {
        const char* helpDesc =
                "Usage: %s [args] <root paths or pkg name>\n"
-               "       --help              - Display this screen\n"
-               "       --restore-db        - Restore TAC Database\n"
+               "          --help                       - Display this screen\n"
+               "          --restore-db         - Restore TAC Database\n"
                "\n";
        printf(helpDesc, argv0, argv0, argv0, argv0, argv0);
 }
 
-static int getRootPath(std::string pkgId, std::string& rootPath)
+void cleanupDirectory()
 {
-       int ret = 0;
-       char *path = 0;
-       uid_t uid = 0;
-
-       if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
-               _ERR("Failed to get UID");
-               return -1;
-       }
-
-       pkgmgrinfo_pkginfo_h handle;
-       if (uid == 0) {
-               ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle);
-               if (ret != PMINFO_R_OK) {
-                       return -1;
-               }
-       } else {
-               ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
-               if (ret != PMINFO_R_OK) {
-                       return -1;
-               }
-       }
-
-       ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
-       if (ret != PMINFO_R_OK) {
-               pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-               return -1;
-       }
-       rootPath = path;
-       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-       return 0;
-}
-
-static int getExecName(std::string pkgId, std::string& execName)
-{
-       int ret = 0;
-       uid_t uid = 0;
-
-       if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
-               _ERR("Failed to get UID");
-               return 0;
-       }
-
-       pkgmgrinfo_pkginfo_h handle;
-       if (uid == 0) {
-               ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle);
-               if (ret != PMINFO_R_OK) {
-                       return -1;
+       std::vector<std::string> removeNuget;
+       for (auto& nuget : bf::recursive_directory_iterator(bf::path(mOptUsrDotnet))) {
+               bool isExist = false;
+               for (auto& restore : restoreNuget) {
+                       if (!bf::is_directory(nuget.path())) {
+                               isExist = true;
+                       }
+                       if (strstr(nuget.path().c_str(), restore.c_str()) != NULL) {
+                               isExist = true;
+                               break;
+                       }
                }
-       } else {
-               ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
-               if (ret != PMINFO_R_OK) {
-                       return -1;
+               if (!isExist) {
+                       removeNuget.push_back(nuget.path().string());
                }
        }
 
-       auto execCB = [](pkgmgrinfo_appinfo_h handle, void *userData) -> int {
-               int ret = 0;
-               char *exec = 0;
-               ret = pkgmgrinfo_appinfo_get_exec(handle, &exec);
-               if (ret != PMINFO_R_OK) {
-                       _ERR("Failed to get exec : %s", exec);
-                       return 0;
-               }
-               execPath = exec;
-               return 0;
-       };
-
-       if (uid == 0) {
-               ret = pkgmgrinfo_appinfo_get_list(handle, PMINFO_ALL_APP, execCB, NULL);
-               if (ret != PMINFO_R_OK) {
-                       return -1;
-               }
-       } else {
-               ret = pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP, execCB, NULL, uid);
-               if (ret != PMINFO_R_OK) {
-                       return -1;
+       for (auto& rm : removeNuget) {
+               if (bf::exists(rm)) {
+                       if (!removeAll(rm)) {
+                               _ERR("Failed to remove of %s", rm.c_str());
+                       }
                }
        }
-       execName = bf::path(execPath).filename().string();
-       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-       return 0;
+       removeNuget.clear();
 }
 
-static int restoreDB(std::string pkgId, std::string depsJsonPath, std::string execName)
+void restoreTACDB(std::string pkgId, std::string depsJsonPath, std::string execName)
 {
        std::ifstream ifs(depsJsonPath);
        Json::CharReaderBuilder reader;
@@ -170,7 +97,7 @@ static int restoreDB(std::string pkgId, std::string depsJsonPath, std::string ex
                if (!Json::parseFromStream(reader, ifs, &root, &error)) {
                        _INFO("Failed to parse of deps.json");
                        ifs.close();
-                       return -1;
+                       return;
                }
                const Json::Value runtimeTargetName = root["runtimeTarget"]["name"];
                std::string runtimeTarget_name = runtimeTargetName.asString();
@@ -178,28 +105,28 @@ static int restoreDB(std::string pkgId, std::string depsJsonPath, std::string ex
                for (auto& nuget : nugetPackages.getMemberNames()) {
                        if (strstr(nuget.c_str(), mTizenNET.c_str()) != NULL ||
                                strstr(nuget.c_str(), mTizenNETSdk.c_str()) != NULL ||
-                               strstr(nuget.c_str(), execName.c_str()) != NULL ||
-                               strstr(nuget.c_str(), (execName.substr(0, execName.find('.'))).c_str()) != 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) {
                                continue;
                        } else {
                                const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
                                if (assemblies != Json::nullValue) {
                                        const Json::Value dependencies = nugetPackages[nuget.c_str()]["dependencies"];
-                                       std::string r2r = "";
+                                       bool isDependency = false;
                                        for (auto& dependency : dependencies.getMemberNames()) {
                                                if (strstr(dependency.c_str(), mTizenNET.c_str()) != NULL ||
                                                        strstr(dependency.c_str(), mNETStandardLibrary.c_str()) != NULL) {
                                                        continue;
                                                } else {
-                                                       r2r = "--r2r";
+                                                       isDependency = true;
                                                }
                                        }
-                                       if (strcmp(r2r.c_str(), "--r2r")) {
-                                               _INFO("Nuget package : %s", nuget.c_str());
+                                       if (!isDependency) {
+                                               _INFO("PackageId : [%s] / Nuget package : [%s]", pkgId.c_str(), nuget.c_str());
                                                std::string name = nuget.substr(0, nuget.find('/'));
                                                std::string version = nuget.substr(nuget.rfind('/') + 1);
                                                std::string sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
-                                                               "VALUES ('" + std::string(pkgId) + "', '" + nuget + "', '" + name + "', '" + version + "');";
+                                                               "VALUES ('" + pkgId + "', '" + nuget + "', '" + name + "', '" + version + "');";
                                                dbInsert(tac_db, tacAppListRestoreDB, sql);
                                                restoreNuget.push_back(mOptUsrDotnet + "/" + name);
                                        }
@@ -208,13 +135,16 @@ static int restoreDB(std::string pkgId, std::string depsJsonPath, std::string ex
                }
                ifs.close();
        }
-       return 0;
 }
 
-static int tacDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
+static int restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
 {
        int ret = 0;
        char *pkgId = NULL;
+       char *root = NULL;
+       char *exec = NULL;
+       std::string rootPath;
+       std::string execName;
 
        ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
        if (ret != PMINFO_R_OK) {
@@ -222,25 +152,29 @@ static int tacDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
                return -1;
        }
 
-       std::string rootPath;
-       if (getRootPath(pkgId, rootPath) < 0) {
+       ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
+       if (ret != PMINFO_R_OK) {
+               fprintf(stderr, "Failed to get root path\n");
                return -1;
        }
+       rootPath = root;
 
-       std::string execName;
-       if (getExecName(pkgId, execName) < 0) {
+       ret = pkgmgrinfo_appinfo_get_exec(handle, &exec);
+       if (ret != PMINFO_R_OK) {
+               fprintf(stderr, "Failed to get exec name\n");
                return -1;
        }
+       execName = std::string(exec).substr(std::string(exec).rfind('/') + 1);
 
        std::string depsJsonName = execName.substr(0, execName.rfind(".dll")) + mDepsJson;
        std::string depsJsonPath = rootPath + "/" + depsJsonName;
        if (bf::exists(depsJsonPath)) {
-               restoreDB(pkgId, depsJsonPath, execName);
+               restoreTACDB(pkgId, depsJsonPath, execName);
        }
        return 0;
 }
 
-int restoreTACDB()
+int restoreDB()
 {
        if (bf::exists(tacAppListRestoreDB)) {
                if (!removeFile(tacAppListRestoreDB)) {
@@ -278,7 +212,7 @@ int restoreTACDB()
                return -1;
        }
 
-       ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, tacDBCb, NULL);
+       ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, restoreDBCb, NULL);
        if (ret != PMINFO_R_OK) {
                fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
                pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
@@ -313,41 +247,20 @@ int restoreTACDB()
                }
        }
 
-       std::vector<std::string> removeNuget;
-       for (auto& nuget : bf::recursive_directory_iterator(bf::path(mOptUsrDotnet))) {
-               bool isExist = false;
-               for (auto& restore : restoreNuget) {
-                       if (!bf::is_directory(nuget.path())) {
-                               isExist = true;
-                       }
-                       if (strstr(nuget.path().c_str(), restore.c_str()) != NULL) {
-                               isExist = true;
-                               break;
-                       }
-               }
-               if (!isExist) {
-                       removeNuget.push_back(nuget.path().string());
-               }
-       }
-
-       for (auto& rm : removeNuget) {
-               if (bf::exists(rm)) {
-                       if (!removeAll(rm)) {
-                               _ERR("Failed to remove of %s", rm.c_str());
-                               return -1;
-                       }
-               }
-       }
-       removeNuget.clear();
+    cleanupDirectory();
        return 0;
 }
 
+// step 1. Remove original DB
+// step 2. Parsing the .deps.json for all apps
+// step 3. Create new DB
+// step 4. Cleanup unnecessary TAC directory
 int main(int argc, char* argv[])
 {
        if (cmdOptionExists(argv, argv + argc, "--help")) {
                help(argv[0]);
        } else if (cmdOptionExists(argv, argv + argc, "--restore-db")) {
-               restoreTACDB();
+               restoreDB();
        } else {
                help(argv[0]);
        }
index a57e6d4..f18eb5a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0 (the License);
  * you may not use this file except in compliance with the License.
 #include "db_manager.h"
 #include "log.h"
 
+#ifdef  LOG_TAG
+#undef  LOG_TAG
+#endif
+#define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
+
 sqlite3* dbCreate(std::string path)
 {
        sqlite3 *sqlite = NULL;
-       char *error;
        int ret = sqlite3_open(path.c_str(), &sqlite);
        if (ret != SQLITE_OK) {
                _ERR("Sqlite error : [%d] : path [%s]", ret, path.c_str());
-               return 0;
+               return NULL;
        }
-       ret = sqlite3_exec(sqlite, "PRAGMA journal_mode = PERSIST", NULL, NULL, &error);
+       ret = sqlite3_exec(sqlite, "PRAGMA journal_mode = PERSIST", NULL, NULL, NULL);
        if (ret != SQLITE_OK) {
-               _ERR("Sqlite error(%d) : %s", ret, error);
-               sqlite3_free(error);
-               return 0;
+               _ERR("Sqlite error : [%d]", ret);
+               return NULL;
        }
-       ret = sqlite3_exec(sqlite, CREATE_TAC_DB_TABLE, NULL, NULL, &error);
+       ret = sqlite3_exec(sqlite, CREATE_TAC_DB_TABLE, NULL, NULL, NULL);
        if (ret != SQLITE_OK) {
-               _ERR("Sqlite error(%d) : %s", ret, error);
-               sqlite3_free(error);
-               return 0;
+               _ERR("Sqlite error : [%d] : path [%s]", ret, path.c_str());
+               return NULL;
        }
        return sqlite;
 }
@@ -53,62 +55,59 @@ bool dbOpen(sqlite3 *tac_db, std::string path)
        return true;
 }
 
-bool dbClose(sqlite3 *tac_db)
+void dbFinalize(sqlite3_stmt *stmt)
+{
+       if (stmt) {
+               sqlite3_finalize(stmt);
+               stmt = NULL;
+       }
+}
+
+void dbClose(sqlite3 *tac_db)
 {
        if (tac_db) {
                sqlite3_exec(tac_db, "COMMIT;", NULL, NULL, NULL);
                sqlite3_close(tac_db);
                tac_db = NULL;
        }
-       return true;
 }
 
-bool dbRollback(sqlite3 *tac_db)
+void dbRollback(sqlite3 *tac_db)
 {
        if (tac_db) {
                sqlite3_exec(tac_db, "ROLLBACK;", NULL, NULL, NULL);
                sqlite3_close(tac_db);
                tac_db = NULL;
        }
-       return true;
 }
 
 bool dbUpdate(sqlite3 *tac_db, std::string path, std::string query)
 {
-       sqlite3_stmt *stmt;
+       sqlite3_stmt *stmt = NULL;
        if (!dbOpen(tac_db, path)) {
                return false;
        }
        int ret = sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
        ret = sqlite3_prepare(tac_db, query.c_str(), QUERY_MAX_LEN , &stmt, NULL);
        if (ret != SQLITE_OK) {
-               _ERR("Sqlite error : [%s,%s]", query.c_str(), sqlite3_errmsg(tac_db));
-               goto ERROR;
+               _ERR("Sqlite error : [%s, %s]", query.c_str(), sqlite3_errmsg(tac_db));
+               dbClose(tac_db);
+               return false;
        }
        ret = sqlite3_step(stmt);
        if (ret != SQLITE_DONE && ret != SQLITE_ROW && ret != SQLITE_OK) {
                _ERR("Sqlite error [%d]", ret);
-               goto ERROR;
-       }
-       ret = sqlite3_finalize(stmt);
-       if (ret != SQLITE_OK) {
-               _ERR("Sqlite error [%d]", ret);
+               dbFinalize(stmt);
                dbClose(tac_db);
                return false;
        }
+       dbFinalize(stmt);
        return true;
-ERROR:
-       ret = sqlite3_finalize(stmt);
-       if (ret != SQLITE_OK) {
-               _ERR("Sqlite error [%d]", ret);
-       }
-       dbClose(tac_db);
-       return false;
 }
 
 bool dbInsert(sqlite3 *tac_db, std::string path, std::string query)
 {
-       sqlite3_stmt *stmt;
+       sqlite3_stmt *stmt = NULL;
        if (!dbOpen(tac_db, path)) {
                return false;
        }
@@ -116,33 +115,24 @@ bool dbInsert(sqlite3 *tac_db, std::string path, std::string query)
        ret = sqlite3_prepare(tac_db, query.c_str(), QUERY_MAX_LEN , &stmt, NULL);
        if (ret != SQLITE_OK) {
                _ERR("Sqlite error : [%s,%s]", query.c_str(), sqlite3_errmsg(tac_db));
-               goto ERROR;
+               dbClose(tac_db);
+               return false;
        }
        ret = sqlite3_step(stmt);
        if (ret != SQLITE_DONE && ret != SQLITE_ROW && ret != SQLITE_OK) {
                _ERR("Sqlite error [%d]", ret);
-               goto ERROR;
-       }
-       ret = sqlite3_finalize(stmt);
-       if (ret != SQLITE_OK) {
-               _ERR("Sqlite error [%d]", ret);
+               dbFinalize(stmt);
                dbClose(tac_db);
                return false;
        }
+       dbFinalize(stmt);
        return true;
-ERROR:
-       ret = sqlite3_finalize(stmt);
-       if (ret != SQLITE_OK) {
-               _ERR("Sqlite error [%d]", ret);
-       }
-       dbClose(tac_db);
-       return false;
 }
 
 std::vector<std::string> dbSelect(sqlite3 *tac_db, std::string path, std::string query)
 {
        std::vector<std::string> updateDB;
-       sqlite3_stmt* stmt;
+       sqlite3_stmt* stmt = NULL;
        const char* str = NULL;
        if (!dbOpen(tac_db, path)) {
                return updateDB;
@@ -150,38 +140,21 @@ std::vector<std::string> dbSelect(sqlite3 *tac_db, std::string path, std::string
        int ret = sqlite3_prepare_v2(tac_db, query.c_str(), strlen(query.c_str()), &stmt, NULL);
        if (ret != SQLITE_OK) {
                _ERR("Sqlite error : [%s,%s]", query.c_str(), sqlite3_errmsg(tac_db));
-               goto ERROR;
+               dbClose(tac_db);
+               return updateDB;
        }
        while (sqlite3_step(stmt) == SQLITE_ROW) {
-               //str = (const char *) sqlite3_column_text(stmt, 1);
-               //_DBG("pkgid : %s", (!str || !strlen(str)) ? NULL : strdup(str));
-               //str = (const char *) sqlite3_column_text(stmt, 3);
-               //_DBG("name : %s", (!str || !strlen(str)) ? NULL : strdup(str));
-               //str = (const char *) sqlite3_column_text(stmt, 4);
-               //_DBG("version : %s", (!str || !strlen(str)) ? NULL : strdup(str));
                str = (const char *) sqlite3_column_text(stmt, 2);
                _DBG("Nuget : %s", (!str || !strlen(str)) ? NULL : strdup(str));
                updateDB.push_back((!str || !strlen(str)) ? NULL : strdup(str));
        }
-       ret = sqlite3_finalize(stmt);
-       if (ret != SQLITE_OK) {
-               _ERR("Sqlite error [%d]", ret);
-               dbClose(tac_db);
-               return updateDB;
-       }
-       return updateDB;
-ERROR:
-       ret = sqlite3_finalize(stmt);
-       if (ret != SQLITE_OK) {
-               _ERR("Sqlite error [%d]", ret);
-       }
-       dbClose(tac_db);
+       dbFinalize(stmt);
        return updateDB;
 }
 
 bool dbDelete(sqlite3 *tac_db, std::string path, std::string query)
 {
-       sqlite3_stmt *stmt;
+       sqlite3_stmt *stmt = NULL;
        if (!dbOpen(tac_db, path)) {
                return false;
        }
@@ -189,25 +162,16 @@ bool dbDelete(sqlite3 *tac_db, std::string path, std::string query)
        ret = sqlite3_prepare(tac_db, query.c_str(), QUERY_MAX_LEN , &stmt, NULL);
        if (ret != SQLITE_OK) {
                _ERR("Sqlite error : [%s,%s]", query.c_str(), sqlite3_errmsg(tac_db));
-               goto ERROR;
+               dbClose(tac_db);
+               return false;
        }
        ret = sqlite3_step(stmt);
        if (ret != SQLITE_DONE && ret != SQLITE_ROW && ret != SQLITE_OK) {
                _ERR("Sqlite error [%d]", ret);
-               goto ERROR;
-       }
-       ret = sqlite3_finalize(stmt);
-       if (ret != SQLITE_OK) {
-               _ERR("Sqlite error [%d]", ret);
+               dbFinalize(stmt);
                dbClose(tac_db);
                return false;
        }
+       dbFinalize(stmt);
        return true;
-ERROR:
-       ret = sqlite3_finalize(stmt);
-       if (ret != SQLITE_OK) {
-               _ERR("Sqlite error [%d]", ret);
-       }
-       dbClose(tac_db);
-       return false;
 }
index 5e15b43..45a61f6 100644 (file)
@@ -20,6 +20,8 @@
 #include <unistd.h>
 #include <limits.h>
 #include <strings.h>
+#include <pkgmgr-info.h>
+#include <pkgmgr_installer_info.h>
 
 #include <cstdlib>
 #include <cstring>
@@ -55,6 +57,11 @@ bool isNativeImage(const std::string& fileName)
        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];
@@ -100,6 +107,40 @@ std::string absolutePath(const std::string& path)
        return absPath;
 }
 
+int getRootPath(std::string pkgId, std::string& rootPath)
+{
+       int ret = 0;
+       char *path = 0;
+       uid_t uid = 0;
+
+       if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
+               _ERR("Failed to get UID");
+               return -1;
+       }
+
+       pkgmgrinfo_pkginfo_h handle;
+       if (uid == 0) {
+               ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle);
+               if (ret != PMINFO_R_OK) {
+                       return -1;
+               }
+       } else {
+               ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
+               if (ret != PMINFO_R_OK) {
+                       return -1;
+               }
+       }
+
+       ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
+       if (ret != PMINFO_R_OK) {
+               pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+               return -1;
+       }
+       rootPath = path;
+       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+       return 0;
+}
+
 std::string baseName(const std::string& path)
 {
        auto pos = path.find_last_of(PATH_SEPARATOR);
@@ -169,7 +210,7 @@ void scanFilesInDir(const std::string& directory, FileReader reader, unsigned in
        struct dirent* entry;
        bool isDir;
 
-       if (strstr(directory.c_str(), "TAC.Release") != NULL)
+       if (strstr(directory.c_str(), ".TAC.Release") != NULL)
                return; // skip nitool --regen-all-app (--r2r)
 
        dir = opendir(directory.c_str());