2 * Copyright (c) 2019 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.
19 #include "db_manager.h"
20 #include "path_manager.h"
21 #include "plugin_manager.h"
22 #include "tac_common.h"
28 #include <boost/filesystem.hpp>
30 #include <json/json.h>
31 #include <pkgmgr-info.h>
32 #include <pkgmgr_installer_info.h>
33 #include <openssl/sha.h>
38 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
41 #define __STR(x) __XSTR(x)
42 static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
46 typedef struct Metadata {
51 std::vector<std::string> nugetPackagesAssembliesSha;
52 std::vector<std::string> tacDB;
53 std::vector<std::string> createDirectories;
54 std::vector<std::string> updateTac;
55 std::string status = "";
59 static sqlite3 *tac_db = NULL;
60 bool tacPluginInstalled = false;
61 bool tacPluginFinished = false;
63 bool metadataCheck(GList *list)
66 Metadata *mdInfo = NULL;
67 tag = g_list_first(list);
68 mdInfo = (Metadata*)tag->data;
69 if (strcmp(mdInfo->key, TAC_METADATA_KEY) == 0 && strcmp(mdInfo->value, METADATA_VALUE) == 0) {
70 _DBG("Prefer nuget cache set TRUE");
71 if (initializePluginManager("normal")) {
72 _ERR("Fail to initialize PluginManager");
75 if (initializePathManager(std::string(), std::string(), std::string())) {
76 _ERR("Fail to initialize PathManger");
84 bool appTypeCheck(std::string pkgId)
87 if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
88 _ERR("Failed to get UID");
92 pkgmgrinfo_pkginfo_h handle;
93 int ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
94 if (ret != PMINFO_R_OK) {
95 _ERR("Failed to get pkg info");
99 bool isDotnetAppType = false;
100 auto dotnetAppCounter = [] (pkgmgrinfo_appinfo_h handle, void *userData) -> int {
101 char* type = nullptr;
102 bool* dotnet = static_cast<bool*>(userData);
103 if (pkgmgrinfo_appinfo_get_apptype(handle, &type) != PMINFO_R_OK) {
104 _ERR("Failed to get app type : %s", type);
107 if (strcmp(type, "dotnet") == 0) {
113 if (pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP, dotnetAppCounter, &isDotnetAppType, uid) != PMINFO_R_OK) {
114 _ERR("Failed to get list of app in pkg : %s", pkgId.c_str());
115 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
119 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
120 return isDotnetAppType;
123 void SHA256(std::string path, char outputBuffer[65])
125 FILE *file = fopen(path.c_str(), "rb");
130 unsigned char hash[SHA256_DIGEST_LENGTH];
132 SHA256_Init(&sha256);
134 const int bufSize = 32768;
135 char *buffer = (char*)malloc(bufSize);
141 while ((bytesRead = fread(buffer, 1, bufSize, file))) {
142 SHA256_Update(&sha256, buffer, bytesRead);
144 SHA256_Final(hash, &sha256);
145 for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
146 snprintf(outputBuffer + (i * 2), 3, "%02x", hash[i]);
148 outputBuffer[64] = 0;
154 void createSHA256Info(std::string sha256Info, std::string nugetPackage)
156 std::ofstream ofs(sha256Info, std::ios::app);
157 int assembly_count = 0;
158 for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
159 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
160 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
161 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
162 std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
163 if (!strcmp(nuget_package.c_str(), nugetPackage.c_str())) {
164 ofs << assembly << ":" << sha << std::endl;
168 ofs << assembly_count << std::endl;
172 int compareSHA256Info(std::string sha256Info, std::string nugetPackage)
174 int compare_count = 0;
175 int assembly_count = 0;
176 std::string sha256_count = "0";
177 for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
178 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
179 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
180 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
181 std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
182 if (!strcmp(nuget_package.c_str(), nugetPackage.c_str())) {
184 std::ifstream ifs(sha256Info);
187 while (getline(ifs, get_str)) {
188 if (!strcmp(get_str.c_str(), (assembly + ":" + sha).c_str())) {
191 sha256_count = get_str;
197 if (!strcmp(std::to_string(assembly_count).c_str(), std::to_string(compare_count).c_str()) &&
198 !strcmp(std::to_string(assembly_count).c_str(), sha256_count.c_str())) {
199 _INFO("Same nuget : %s", nugetPackage.c_str());
205 int copyNCreateSymlink(std::string tacVersionDir, std::string nugetPackage, bool isCreateTacDir)
209 if (pkgmgr_installer_info_get_target_uid(&g_uid) < 0) {
210 _ERR("Failed to get UID");
214 std::string tac_dir = concatPath(binPath, TAC_SYMLINK_SUB_DIR);
215 if (!createDir(tac_dir)) {
216 _ERR("Cannot create directory: %s", tac_dir.c_str());
220 for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
221 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
222 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
223 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
224 if (!strcmp(nuget_package.c_str(), nugetPackage.c_str())) {
225 if (bf::exists(concatPath(binPath, assembly))) {
226 if (isCreateTacDir) {
227 if (!copyFile(concatPath(binPath, assembly), concatPath(tacVersionDir, assembly))) {
228 _ERR("Failed to copy of %s", assembly.c_str());
232 bf::create_symlink(concatPath(tacVersionDir, assembly), concatPath(tac_dir, assembly));
233 if (lchown(concatPath(tac_dir, assembly).c_str(), g_uid, g_gid)) {
234 _ERR("Failed to change owner of: %s", concatPath(tac_dir, assembly).c_str());
237 if (!removeFile(concatPath(binPath, assembly))) {
238 _ERR("Failed to remove of %s", assembly.c_str());
247 void depsJsonCheck() {
248 for (auto& npAssembly : depsJsonParser(rootPath, execName, getTPA())) {
249 std::string nuget_package = npAssembly.substr(0, npAssembly.rfind(':'));
250 std::string assembly_name = npAssembly.substr(npAssembly.rfind(':') + 1);
251 tacDB.push_back(nuget_package);
252 char buffer[65] = {0};
253 SHA256(concatPath(binPath, assembly_name), buffer);
254 nugetPackagesAssembliesSha.push_back(nuget_package + ":" + assembly_name + ":" + buffer);
255 _INFO("Assembly : [%s] / SHA256 : [%s]", assembly_name.c_str(), buffer);
257 std::sort(tacDB.begin(), tacDB.end());
258 tacDB.erase(unique(tacDB.begin(), tacDB.end()), tacDB.end());
261 extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *appId, GList *list)
263 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_INSTALL =====]");
264 _INFO("PackageID : %s", pkgId);
266 // Can be multiple apps in one package
267 if (tacPluginInstalled) {
268 _INFO("TAC plugin already installed");
271 tacPluginInstalled = true;
273 if (!appTypeCheck(std::string(pkgId))) {
274 _INFO("App type is not dotnet");
277 if (getExecName(std::string(pkgId), execName) < 0) {
280 if (getRootPath(std::string(pkgId), rootPath) < 0) {
283 binPath = concatPath(rootPath, "bin");
285 if (metadataCheck(list)) {
290 tac_db = dbCreate(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE);
292 _ERR("Sqlite create error");
297 _INFO("Not exist .deps.json file");
301 for (auto& np : tacDB) {
302 std::string tac_name = np.substr(0, np.find('/'));
303 std::string tac_version = np.substr(np.rfind('/') + 1);
304 _INFO("TAC name : %s", tac_name.c_str());
305 _INFO("TAC version : %s", tac_version.c_str());
307 std::string tac_version_dir = concatPath(__DOTNET_DIR, np);
308 std::string sha256_info = concatPath(tac_version_dir, TAC_SHA_256_INFO);
309 if (!bf::exists(tac_version_dir)) {
310 _INFO("Create tac_version_dir [%s]", tac_version_dir.c_str());
311 if (!createDir(tac_version_dir)) {
312 _ERR("Cannot create directory: %s", tac_version_dir.c_str());
315 createDirectories.push_back(tac_version_dir);
316 if (!bf::is_symlink(sha256_info)) {
317 createSHA256Info(sha256_info, np);
319 _ERR("Failed to create sha256_info. Symbolic link is detected");
323 if (copyNCreateSymlink(tac_version_dir, np, true) < 0) {
324 _ERR("Failed to create symlink");
328 char *sql = sqlite3_mprintf(
329 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
330 "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
331 dbInsert(tac_db, TAC_APP_LIST_DB, sql);
334 _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str());
335 if (!bf::is_symlink(sha256_info)) {
336 if (compareSHA256Info(sha256_info, np)) {
337 if (copyNCreateSymlink(tac_version_dir, np, false) < 0) {
338 _ERR("Failed to create symlink");
342 char *sql = sqlite3_mprintf(
343 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
344 "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
345 dbInsert(tac_db, TAC_APP_LIST_DB, sql);
348 _INFO("Different nuget : %s", np.c_str());
351 _ERR("Failed to create sha256_info. Symbolic link is detected");
355 if (!bf::exists(sha256_info)) {
356 if(!removeAll(tac_version_dir)) {
357 _ERR("Failed to remove of %s", tac_version_dir.c_str());
364 static int sqliteCb(void *count, int argc, char **argv, char **colName) {
365 int *c = (int*)count;
370 int updateTacDB(sqlite3 *sqlite)
372 for (auto& unp : updateTac) {
374 char *sql = sqlite3_mprintf("SELECT COUNT(NUGET) FROM TAC WHERE NUGET = %Q;", unp.c_str());
375 int ret = sqlite3_exec(sqlite, sql, sqliteCb, &count, NULL);
376 if (ret != SQLITE_OK) {
377 _ERR("Sqlite select error");
382 std::string tac_version_dir_prev = concatPath(__DOTNET_DIR, unp);
383 std::string tac_version_dir_backup = tac_version_dir_prev + ".bck";
384 if (!copyDir(tac_version_dir_prev, tac_version_dir_backup)) {
385 _ERR("Failed to copy of %s to %s", tac_version_dir_prev.c_str(), tac_version_dir_backup.c_str());
389 if (!removeAll(tac_version_dir_prev)) {
390 _ERR("Failed to remove of %s", tac_version_dir_prev.c_str());
400 extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *appId, GList *list)
402 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UPGRADE =====]");
403 _INFO("PackageID : %s", pkgId);
405 // Can be multiple apps in one package
406 if (tacPluginInstalled) {
407 _INFO("TAC plugin already upgraded");
410 tacPluginInstalled = true;
412 if (!appTypeCheck(std::string(pkgId))) {
413 _INFO("App type is not dotnet");
416 if (getExecName(std::string(pkgId), execName) < 0) {
419 if (getRootPath(std::string(pkgId), rootPath) < 0) {
422 binPath = concatPath(rootPath, "bin");
424 if (!strcmp("removed", status.c_str())) {
425 _INFO("Skipped to parse of deps.json");
427 if (metadataCheck(list)) {
433 tac_db = dbCreate(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE);
435 _ERR("Sqlite open error");
439 char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
440 updateTac = dbSelect(tac_db, TAC_APP_LIST_DB, sql);
444 sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q;", pkgId);
445 dbDelete(tac_db, TAC_APP_LIST_DB, sql);
447 if (updateTacDB(tac_db) < 0) {
451 for (auto& np : tacDB) {
452 std::string tac_name = np.substr(0, np.find('/'));
453 std::string tac_version = np.substr(np.rfind('/') + 1);
454 _INFO("TAC name : %s", tac_name.c_str());
455 _INFO("TAC version : %s", tac_version.c_str());
457 std::string tac_version_dir = concatPath(__DOTNET_DIR, np);
458 std::string sha256_info = concatPath(tac_version_dir, TAC_SHA_256_INFO);
459 if (!bf::exists(tac_version_dir)) {
460 _INFO("Create tac_version_dir [%s]", tac_version_dir.c_str());
461 if (!createDir(tac_version_dir)) {
462 _ERR("Cannot create directory: %s", tac_version_dir.c_str());
465 createDirectories.push_back(tac_version_dir);
466 if (!bf::is_symlink(sha256_info)) {
467 createSHA256Info(sha256_info, np);
469 _ERR("Failed to create sha256_info. Symbolic link is detected");
473 if (copyNCreateSymlink(tac_version_dir, np, true) < 0) {
474 _ERR("Failed to create symlink");
479 sql = sqlite3_mprintf(
480 "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = %Q AND NAME = %Q;", pkgId, tac_name.c_str());
481 int ret = sqlite3_exec(tac_db, sql, sqliteCb, &count, NULL);
482 if (ret != SQLITE_OK) {
483 _ERR("Sqlite select error");
489 sql = sqlite3_mprintf(
490 "UPDATE TAC SET NAME = %Q, VERSION = %Q, NUGET = %Q WHERE PKGID = %Q AND NAME = %Q;",
491 tac_name.c_str(), tac_version.c_str(), np.c_str(), pkgId, tac_name.c_str());
492 dbUpdate(tac_db, TAC_APP_LIST_DB, sql);
494 } else if (count == 0) {
495 sql = sqlite3_mprintf(
496 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
497 "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
498 dbInsert(tac_db, TAC_APP_LIST_DB, sql);
502 _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str());
503 if (!bf::is_symlink(sha256_info)) {
504 if (compareSHA256Info(sha256_info, np)) {
505 if (copyNCreateSymlink(tac_version_dir, np, false) < 0) {
506 _ERR("Failed to create symlink");
511 char *sql = sqlite3_mprintf(
512 "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = %Q AND NAME = %Q;", pkgId, tac_name.c_str());
513 int ret = sqlite3_exec(tac_db, sql, sqliteCb, &count, NULL);
514 if (ret != SQLITE_OK) {
515 _ERR("Sqlite select error");
521 sql = sqlite3_mprintf(
522 "UPDATE TAC SET NAME = %Q, VERSION = %Q, NUGET = %Q WHERE PKGID = %Q AND NAME = %Q;",
523 tac_name.c_str(), tac_version.c_str(), np.c_str(), pkgId, tac_name.c_str());
524 dbUpdate(tac_db, TAC_APP_LIST_DB, sql);
526 } else if (count == 0) {
527 sql = sqlite3_mprintf(
528 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
529 "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
530 dbInsert(tac_db, TAC_APP_LIST_DB, sql);
534 _INFO("Different nuget : %s", np.c_str());
537 _ERR("Failed to create sha256_info. Symbolic link is detected");
541 if (!bf::exists(sha256_info)) {
542 if(!removeAll(tac_version_dir)) {
543 _ERR("Failed to remove of %s", tac_version_dir.c_str());
547 for (auto& unp : updateTac) {
548 bool isExits = false;
549 for (auto& np : tacDB) {
550 if (!strcmp(unp.c_str(), np.c_str())) {
556 char *sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q AND NUGET = %Q;", pkgId, unp.c_str());
557 dbDelete(tac_db, TAC_APP_LIST_DB, sql);
561 if (updateTacDB(tac_db) < 0) {
568 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgId, const char *appId, GList *list)
570 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UNINSTALL =====]");
571 _INFO("PackageID : %s", pkgId);
573 // Can be multiple apps in one package
574 if (tacPluginInstalled) {
575 _INFO("TAC plugin already uninstalled");
578 tacPluginInstalled = true;
580 status = "uninstall";
581 tac_db = dbOpen(TAC_APP_LIST_DB);
583 _ERR("Sqlite open error");
587 char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
588 updateTac = dbSelect(tac_db, TAC_APP_LIST_DB, sql);
591 sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q;", pkgId);
592 dbDelete(tac_db, TAC_APP_LIST_DB, sql);
595 if (updateTacDB(tac_db) < 0) {
601 extern "C" int PKGMGR_MDPARSER_PLUGIN_REMOVED(const char *pkgId, const char *appId, GList *list)
603 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_REMOVED =====]");
604 _INFO("PackageID : %s", pkgId);
608 return PKGMGR_MDPARSER_PLUGIN_UPGRADE(pkgId, appId, list);
611 void cleanStep(std::string tac)
613 std::string current_tac = concatPath(__DOTNET_DIR, tac.substr(0, tac.find('/')));
615 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
616 std::string bck_path = bck.path().string();
617 if (bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") != NULL) {
618 if (!removeAll(bck_path)) {
619 _ERR("Failed to remove of %s", bck_path.c_str());
625 bool isExist = false;
626 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
627 std::string bck_path = bck.path().string();
628 if (bf::exists(bck_path) && bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") == NULL) {
634 if (!removeAll(current_tac)) {
635 _ERR("Failed to remove of %s", current_tac.c_str());
638 } catch (const bf::filesystem_error& error) {
639 _ERR("Failed to recursive directory: %s", error.what());
649 void unInstall_Clean()
651 for (auto& unp : updateTac) {
658 if (!tacDB.empty()) {
659 for (auto& np : tacDB) {
666 extern "C" int PKGMGR_MDPARSER_PLUGIN_CLEAN(const char *pkgId, const char *appId, GList *list)
668 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_CLEAN =====]");
669 _INFO("PackageID : %s", pkgId);
671 // Can be multiple apps in one package
672 if (tacPluginFinished) {
673 _INFO("TAC plugin already finished(CLEAN)");
676 tacPluginFinished = true;
682 if (!strcmp("install", status.c_str())) {
684 } else if (!strcmp("update", status.c_str())) {
686 } else if (!strcmp("uninstall", status.c_str())) {
692 void undoStep(std::string tac)
694 std::string current_tac = concatPath(__DOTNET_DIR, tac.substr(0, tac.find('/')));
696 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
697 std::string bck_path = bck.path().string();
698 if (bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") != NULL) {
699 if (!moveFile(bck_path, bck_path.substr(0, bck_path.rfind(".bck")))) {
700 _ERR("Failed to move %s", bck_path.c_str());
705 } catch (const bf::filesystem_error& error) {
706 _ERR("Failed to recursive directory: %s", error.what());
713 for (auto& cd : createDirectories) {
714 if (!removeAll(cd)) {
715 _ERR("Failed to remove of %s", cd.c_str());
720 void unInstall_Undo()
722 for (auto& unp : updateTac) {
730 if (!tacDB.empty()) {
731 for (auto& np : tacDB) {
738 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNDO(const char *pkgId, const char *appId, GList *list)
740 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UNDO =====]");
741 _INFO("PackageID : %s", pkgId);
743 // Can be multiple apps in one package
744 if (tacPluginFinished) {
745 _INFO("TAC plugin already finished(UNDO)");
748 tacPluginFinished = true;
754 if (!strcmp("install", status.c_str())) {
756 } else if (!strcmp("update", status.c_str())) {
758 } else if (!strcmp("uninstall", status.c_str())) {