Support UI Thread Separate (UTS) App Model (#464)
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / tac_common.cc
index ca666f0..874b4b8 100644 (file)
@@ -67,12 +67,12 @@ static void cleanupDirectory()
 
                for (auto& rm : removeNuget) {
                        if (!removeAll(rm)) {
-                               fprintf(stderr, "Failed to remove of %s\n", rm.c_str());
+                               _SERR("Failed to remove of %s", rm.c_str());
                        }
                }
                removeNuget.clear();
        } catch (const bf::filesystem_error& error) {
-               fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
+               _SERR("Failed to recursive directory: %s", error.what());
                return;
        }
 }
@@ -83,30 +83,28 @@ static int tac_restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
        char *pkgId = NULL;
        char *root = NULL;
        char *exec = NULL;
-       std::string rootPath;
-       std::string execName;
 
        int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
        if (ret != PMINFO_R_OK) {
-               fprintf(stderr, "Failed to get pkgid\n");
+               _SERR("Failed to get pkgid");
                return -1;
        }
 
-       enableTACPackage(std::string(pkgId));
-
        ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
        if (ret != PMINFO_R_OK) {
-               fprintf(stderr, "Failed to get root path\n");
+               _SERR("Failed to get root path");
                return -1;
        }
-       rootPath = std::string(root);
+       std::string rootPath = std::string(root);
 
        ret = pkgmgrinfo_appinfo_get_exec(handle, &exec);
        if (ret != PMINFO_R_OK) {
-               fprintf(stderr, "Failed to get exec name\n");
+               _SERR("Failed to get exec name");
                return -1;
        }
-       execName = std::string(exec).substr(std::string(exec).rfind('/') + 1);
+       std::string execName = std::string(exec).substr(std::string(exec).rfind('/') + 1);
+
+       enableTACPackage(std::string(pkgId));
 
        std::vector<std::string> parserData;
        std::string binDir = concatPath(rootPath, "bin");
@@ -134,25 +132,26 @@ static int tac_restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
                }
        }
        parserData.clear();
+
        return 0;
 }
 
 tac_error_e tac_restoreDB()
 {
        if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
-               fprintf(stderr, "Failed to remove of %s\n", TAC_APP_LIST_RESTORE_DB);
+               _SERR("Failed to remove of %s", TAC_APP_LIST_RESTORE_DB);
                return TAC_ERROR_UNKNOWN;
        }
 
        std::string dbRestoreJournal = TAC_APP_LIST_RESTORE_DB + std::string("-journal");
        if (!removeFile(dbRestoreJournal)) {
-               fprintf(stderr, "Failed to remove of %s\n", dbRestoreJournal.c_str());
+               _SERR("Failed to remove of %s", dbRestoreJournal.c_str());
                return TAC_ERROR_UNKNOWN;
        }
 
        tac_db = createDB(TAC_APP_LIST_RESTORE_DB, CREATE_TAC_DB_TABLE);
        if (!tac_db) {
-               fprintf(stderr, "Sqlite create error\n");
+               _SERR("Sqlite create error");
                return TAC_ERROR_UNKNOWN;
        }
        sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
@@ -163,7 +162,7 @@ tac_error_e tac_restoreDB()
                return TAC_ERROR_UNKNOWN;
        }
 
-       ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
+       ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE_TRUE);
        if (ret != PMINFO_R_OK) {
                pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
                return TAC_ERROR_UNKNOWN;
@@ -183,21 +182,21 @@ tac_error_e tac_restoreDB()
        }
 
        if (!copyFile(TAC_APP_LIST_RESTORE_DB, TAC_APP_LIST_DB)) {
-               fprintf(stderr, "Failed to copy of %s\n", TAC_APP_LIST_DB);
+               _SERR("Failed to copy of %s", TAC_APP_LIST_DB);
                return TAC_ERROR_UNKNOWN;
        }
        if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
-               fprintf(stderr, "Failed to remove of %s\n", TAC_APP_LIST_RESTORE_DB);
+               _SERR("Failed to remove of %s", TAC_APP_LIST_RESTORE_DB);
                return TAC_ERROR_UNKNOWN;
        }
 
        std::string dbJournal = TAC_APP_LIST_DB + std::string("-journal");
        if (!copyFile(dbRestoreJournal, dbJournal)) {
-               fprintf(stderr, "Failed to copy of %s\n", dbJournal.c_str());
+               _SERR("Failed to copy of %s", dbJournal.c_str());
                return TAC_ERROR_UNKNOWN;
        }
        if (!removeFile(dbRestoreJournal)) {
-               fprintf(stderr, "Failed to remove of %s\n", dbRestoreJournal.c_str());
+               _SERR("Failed to remove of %s", dbRestoreJournal.c_str());
                return TAC_ERROR_UNKNOWN;
        }
 
@@ -210,7 +209,7 @@ tac_error_e disableTACPackage(const std::string& pkgId)
 {
        std::string rootPath = getRootPath(pkgId);
        if (rootPath.empty()) {
-               fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
+               _SERR("Failed to get root path from [%s]", pkgId.c_str());
                return TAC_ERROR_INVALID_PACKAGE;
        }
 
@@ -222,19 +221,19 @@ tac_error_e disableTACPackage(const std::string& pkgId)
                        for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
                                std::string symPath = symlinkAssembly.path().string();
                                std::string fileName = symlinkAssembly.path().filename().string();
-                               if (bf::is_symlink(symPath)) {
+                               if (isSymlinkFile(symPath)) {
                                        std::string originPath = bf::read_symlink(symPath).string();
                                        if (!isNativeImage(symPath)) {
                                                std::string dllPath = concatPath(binDir, fileName);
                                                if (!copyFile(originPath, dllPath)) {
-                                                       fprintf(stderr, "Failed to copy of %s\n", dllPath.c_str());
+                                                       _SERR("Failed to copy of %s", dllPath.c_str());
                                                        return TAC_ERROR_UNKNOWN;
                                                }
                                                copySmackAndOwnership(binDir.c_str(), concatPath(binDir, fileName).c_str());
                                        } else {
                                                std::string niPath = concatPath(binNIDir, fileName);
                                                if (!copyFile(originPath, niPath)) {
-                                                       fprintf(stderr, "Failed to copy of %s\n", niPath.c_str());
+                                                       _SERR("Failed to copy of %s", niPath.c_str());
                                                        return TAC_ERROR_UNKNOWN;
                                                }
                                                copySmackAndOwnership(binDir.c_str(), niPath.c_str());
@@ -242,11 +241,11 @@ tac_error_e disableTACPackage(const std::string& pkgId)
                                }
                        }
                        if (!removeAll(tacDir)) {
-                               fprintf(stderr, "Failed to remove of %s\n", tacDir.c_str());
+                               _SERR("Failed to remove of %s", tacDir.c_str());
                                return TAC_ERROR_UNKNOWN;
                        }
                } catch (const bf::filesystem_error& error) {
-                       fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
+                       _SERR("Failed to recursive directory: %s", error.what());
                        return TAC_ERROR_UNKNOWN;
                }
        }
@@ -257,29 +256,35 @@ tac_error_e enableTACPackage(const std::string& pkgId)
 {
        std::string rootPath = getRootPath(pkgId);
        if (rootPath.empty()) {
-               fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
+               _SERR("Failed to get root path from [%s]", pkgId.c_str());
                return TAC_ERROR_INVALID_PACKAGE;
        }
 
+       std::string binDir = concatPath(rootPath, "bin");
+       if (exist(concatPath(binDir, PRE_COMPILED_PACKAGE_FILE))) {
+               _INFO("The %s is a Pre-Compiled package. So, skip the TAC", pkgId.c_str());
+               return TAC_ERROR_NONE;
+       }
+
        std::string execName = getExecName(pkgId);
        if (execName.empty()) {
-               fprintf(stderr, "Failed to get exec name from [%s]\n", pkgId.c_str());
+               _SERR("Failed to get exec name from [%s]", pkgId.c_str());
                return TAC_ERROR_INVALID_PACKAGE;
        }
 
        std::string metaValue = getMetadataValue(pkgId, TAC_METADATA_KEY);
        if (metaValue.empty()) {
-               fprintf(stderr, "Failed to get metadata from [%s]\n", pkgId.c_str());
+               _SERR("Failed to get metadata from [%s]", pkgId.c_str());
                return TAC_ERROR_INVALID_PACKAGE;
        }
 
-       if (!strcmp(metaValue.c_str(), "true")) {
+       if (!strcmp(metaValue.c_str(), METADATA_VALUE_TRUE)) {
                std::string binDir = concatPath(rootPath, "bin");
                std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
                std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
                if (!exist(tacDir)) {
                        if (!createDir(tacDir)) {
-                               fprintf(stderr, "Cannot create directory: %s\n", tacDir.c_str());
+                               _SERR("Cannot create directory: %s", tacDir.c_str());
                                return TAC_ERROR_UNKNOWN;
                        }
                        copySmackAndOwnership(binDir.c_str(), tacDir.c_str());
@@ -297,6 +302,7 @@ tac_error_e enableTACPackage(const std::string& pkgId)
                                }
                        }
 
+                       bs::error_code error;
                        for (auto& originPath : enableNuget) {
                                if (exist(originPath)) {
                                        std::string fileName = originPath.substr(originPath.rfind('/') + 1);
@@ -304,35 +310,43 @@ tac_error_e enableTACPackage(const std::string& pkgId)
                                        if (exist(binNIDir)) {
                                                std::string originNIPath = changeExtension(originPath, "dll", "ni.dll");
                                                if (exist(originNIPath)) {
-                                                       bf::create_symlink(originNIPath, concatPath(tacDir, NIFileName));
-                                                       fprintf(stdout, "%s symbolic link file generated successfully.\n", concatPath(tacDir, NIFileName).c_str());
+                                                       bf::create_symlink(originNIPath, concatPath(tacDir, NIFileName), error);
+                                                       if (error) {
+                                                               _SERR("Failed to create symlink %s file", concatPath(tacDir, NIFileName).c_str());
+                                                               return TAC_ERROR_UNKNOWN;
+                                                       }
+                                                       _SOUT("%s symbolic link file generated successfully.", concatPath(tacDir, NIFileName).c_str());
                                                        copySmackAndOwnership(tacDir.c_str(), concatPath(tacDir, NIFileName).c_str(), true);
 
                                                        if (!removeFile(concatPath(binNIDir, NIFileName))) {
-                                                               fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, NIFileName).c_str());
+                                                               _SERR("Failed to remove of %s", concatPath(binNIDir, NIFileName).c_str());
                                                                return TAC_ERROR_UNKNOWN;
                                                        }
                                                }
                                        }
-                                       bf::create_symlink(originPath, concatPath(tacDir, fileName));
-                                       fprintf(stdout, "%s symbolic link file generated successfully.\n", concatPath(tacDir, fileName).c_str());
+                                       bf::create_symlink(originPath, concatPath(tacDir, fileName), error);
+                                       if (error) {
+                                               _SERR("Failed to create symlink %s file", concatPath(tacDir, fileName).c_str());
+                                               return TAC_ERROR_UNKNOWN;
+                                       }
+                                       _SOUT("%s symbolic link file generated successfully.", concatPath(tacDir, fileName).c_str());
                                        copySmackAndOwnership(tacDir.c_str(), concatPath(tacDir, fileName).c_str(), true);
 
                                        if (!removeFile(concatPath(binDir, fileName))) {
-                                               fprintf(stderr, "Failed to remove of %s\n", concatPath(binDir, fileName).c_str());
+                                               _SERR("Failed to remove of %s", concatPath(binDir, fileName).c_str());
                                                return TAC_ERROR_UNKNOWN;
                                        }
                                }
                        }
                        if (enableNuget.empty()) {
                                if (!removeAll(tacDir)) {
-                                       fprintf(stderr, "Failed to remove of %s\n", tacDir.c_str());
+                                       _SERR("Failed to remove of %s", tacDir.c_str());
                                }
                        }
                        enableNuget.clear();
                }
        } else {
-               fprintf(stderr, "The metadata key is missing or the metadata value is false of [%s]\n", pkgId.c_str());
+               _SERR("The metadata key is missing or the metadata value is false of [%s]", pkgId.c_str());
        }
        return TAC_ERROR_NONE;
 }
@@ -411,7 +425,7 @@ std::vector<std::string> depsJsonParser(const std::string& rootPath, const std::
                                ifs.close();
                        }
                }
-       } catch (const Json::LogicError& error) {
+       } catch (const Json::Exception& error) {
                _ERR("Failed to parse Json: %s", error.what());
        }
        return parserData;
@@ -445,13 +459,13 @@ static int tlc_restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
 
        int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
        if (ret != PMINFO_R_OK) {
-               fprintf(stderr, "Failed to get pkgid\n");
+               _SERR("Failed to get pkgid");
                return -1;
        }
 
        ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
        if (ret != PMINFO_R_OK) {
-               fprintf(stderr, "Failed to get root path\n");
+               _SERR("Failed to get root path");
                return -1;
        }
        rootPath = std::string(root);
@@ -472,19 +486,19 @@ static int tlc_restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
 tac_error_e tlc_restoreDB()
 {
        if (!removeFile(TLC_APP_LIST_RESTORE_DB)) {
-               fprintf(stderr, "Failed to remove of %s\n", TLC_APP_LIST_RESTORE_DB);
+               _SERR("Failed to remove of %s", TLC_APP_LIST_RESTORE_DB);
                return TAC_ERROR_UNKNOWN;
        }
 
        std::string dbRestoreJournal = TLC_APP_LIST_RESTORE_DB + std::string("-journal");
        if (!removeFile(dbRestoreJournal)) {
-               fprintf(stderr, "Failed to remove of %s\n", dbRestoreJournal.c_str());
+               _SERR("Failed to remove of %s", dbRestoreJournal.c_str());
                return TAC_ERROR_UNKNOWN;
        }
 
        tlc_db = createDB(TLC_APP_LIST_RESTORE_DB, CREATE_TLC_DB_TABLE);
        if (!tlc_db) {
-               fprintf(stderr, "Sqlite create error\n");
+               _SERR("Sqlite create error");
                return TAC_ERROR_UNKNOWN;
        }
        sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL);
@@ -495,7 +509,7 @@ tac_error_e tlc_restoreDB()
                return TAC_ERROR_UNKNOWN;
        }
 
-       ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
+       ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE_TRUE);
        if (ret != PMINFO_R_OK) {
                pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
                return TAC_ERROR_UNKNOWN;
@@ -515,21 +529,21 @@ tac_error_e tlc_restoreDB()
        }
 
        if (!copyFile(TLC_APP_LIST_RESTORE_DB, TLC_APP_LIST_DB)) {
-               fprintf(stderr, "Failed to copy of %s\n", TLC_APP_LIST_DB);
+               _SERR("Failed to copy of %s", TLC_APP_LIST_DB);
                return TAC_ERROR_UNKNOWN;
        }
        if (!removeFile(TLC_APP_LIST_RESTORE_DB)) {
-               fprintf(stderr, "Failed to remove of %s\n", TLC_APP_LIST_RESTORE_DB);
+               _SERR("Failed to remove of %s", TLC_APP_LIST_RESTORE_DB);
                return TAC_ERROR_UNKNOWN;
        }
 
        std::string dbJournal = TLC_APP_LIST_DB + std::string("-journal");
        if (!copyFile(dbRestoreJournal, dbJournal)) {
-               fprintf(stderr, "Failed to copy of %s\n", dbJournal.c_str());
+               _SERR("Failed to copy of %s", dbJournal.c_str());
                return TAC_ERROR_UNKNOWN;
        }
        if (!removeFile(dbRestoreJournal)) {
-               fprintf(stderr, "Failed to remove of %s\n", dbRestoreJournal.c_str());
+               _SERR("Failed to remove of %s", dbRestoreJournal.c_str());
                return TAC_ERROR_UNKNOWN;
        }