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 std::vector<std::string> nugetPackagesAssembliesSha;
47 std::vector<std::string> tacDB;
48 std::vector<std::string> createDirectories;
49 std::vector<std::string> updateTac;
50 std::string status = "";
51 static sqlite3 *tac_db = NULL;
52 bool tacPluginInstalled = false;
53 bool tacPluginFinished = false;
55 static void SHA256(std::string path, char outputBuffer[65])
57 FILE *file = fopen(path.c_str(), "rb");
62 unsigned char hash[SHA256_DIGEST_LENGTH];
66 const int bufSize = 32768;
67 char *buffer = (char*)malloc(bufSize);
73 while ((bytesRead = fread(buffer, 1, bufSize, file))) {
74 SHA256_Update(&sha256, buffer, bytesRead);
76 SHA256_Final(hash, &sha256);
77 for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
78 snprintf(outputBuffer + (i * 2), 3, "%02x", hash[i]);
86 static void createSHA256Info(std::string sha256Info, std::string nugetPackage)
88 std::ofstream ofs(sha256Info, std::ios::app);
89 int assembly_count = 0;
90 for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
91 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
92 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
93 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
94 std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
95 if (!strcmp(nuget_package.c_str(), nugetPackage.c_str())) {
96 ofs << assembly << ":" << sha << std::endl;
100 ofs << assembly_count << std::endl;
104 static int compareSHA256Info(std::string sha256Info, std::string nugetPackage)
106 int compare_count = 0;
107 int assembly_count = 0;
108 std::string sha256_count = "0";
109 for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
110 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
111 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
112 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
113 std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
114 if (!strcmp(nuget_package.c_str(), nugetPackage.c_str())) {
116 std::ifstream ifs(sha256Info);
119 while (getline(ifs, get_str)) {
120 if (!strcmp(get_str.c_str(), (assembly + ":" + sha).c_str())) {
123 sha256_count = get_str;
129 if (!strcmp(std::to_string(assembly_count).c_str(), std::to_string(compare_count).c_str()) &&
130 !strcmp(std::to_string(assembly_count).c_str(), sha256_count.c_str())) {
131 _INFO("Same nuget : %s", nugetPackage.c_str());
137 static int copyNCreateSymlink(std::string binPath, std::string tacVersionDir, std::string nugetPackage, bool isCreateTacDir)
141 if (pkgmgr_installer_info_get_target_uid(&g_uid) < 0) {
142 _ERR("Failed to get UID");
146 std::string tac_dir = concatPath(binPath, TAC_SYMLINK_SUB_DIR);
147 if (!createDir(tac_dir)) {
148 _ERR("Cannot create directory: %s", tac_dir.c_str());
152 for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
153 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
154 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
155 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
156 if (!strcmp(nuget_package.c_str(), nugetPackage.c_str())) {
157 if (bf::exists(concatPath(binPath, assembly))) {
158 if (isCreateTacDir) {
159 if (!copyFile(concatPath(binPath, assembly), concatPath(tacVersionDir, assembly))) {
160 _ERR("Failed to copy of %s", assembly.c_str());
164 bf::create_symlink(concatPath(tacVersionDir, assembly), concatPath(tac_dir, assembly));
165 if (lchown(concatPath(tac_dir, assembly).c_str(), g_uid, g_gid)) {
166 _ERR("Failed to change owner of: %s", concatPath(tac_dir, assembly).c_str());
169 if (!removeFile(concatPath(binPath, assembly))) {
170 _ERR("Failed to remove of %s", assembly.c_str());
179 static void depsJsonCheck(std::string rootPath, std::string binPath, std::string execName)
181 for (auto& npAssembly : depsJsonParser(rootPath, execName, getTPA())) {
182 std::string nuget_package = npAssembly.substr(0, npAssembly.rfind(':'));
183 std::string assembly_name = npAssembly.substr(npAssembly.rfind(':') + 1);
184 tacDB.push_back(nuget_package);
185 char buffer[65] = {0};
186 SHA256(concatPath(binPath, assembly_name), buffer);
187 nugetPackagesAssembliesSha.push_back(nuget_package + ":" + assembly_name + ":" + buffer);
188 _INFO("Assembly : [%s] / SHA256 : [%s]", assembly_name.c_str(), buffer);
190 std::sort(tacDB.begin(), tacDB.end());
191 tacDB.erase(unique(tacDB.begin(), tacDB.end()), tacDB.end());
194 extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *appId, GList *list)
196 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_INSTALL =====]");
197 _INFO("PackageID : %s", pkgId);
199 // Can be multiple apps in one package
200 if (tacPluginInstalled) {
201 _INFO("TAC plugin already installed");
204 tacPluginInstalled = true;
206 if (initializePathManager(std::string(), std::string(), std::string())) {
207 _ERR("Fail to initialize PathManger");
211 std::string appType = getAppType(std::string(pkgId));
212 if (strstr(appType.c_str(), "dotnet") == NULL) {
213 _ERR("App type is not dotnet");
216 std::string execName = getExecName(std::string(pkgId));
217 std::string rootPath = getRootPath(std::string(pkgId));
218 if (execName.empty() || rootPath.empty()) {
221 std::string binPath = concatPath(rootPath, "bin");
222 std::string metaValue = getMetadataValue(std::string(pkgId), TAC_METADATA_KEY);
223 if (metaValue.empty()) {
226 if (metaValue == METADATA_VALUE) {
227 depsJsonCheck(rootPath, binPath, execName);
231 tac_db = dbCreate(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE);
233 _ERR("Sqlite create error");
238 _INFO("Not exist data for TAC in %s", pkgId);
242 for (auto& np : tacDB) {
243 std::string tac_name = np.substr(0, np.find('/'));
244 std::string tac_version = np.substr(np.rfind('/') + 1);
245 _INFO("TAC name : %s", tac_name.c_str());
246 _INFO("TAC version : %s", tac_version.c_str());
248 std::string tac_version_dir = concatPath(__DOTNET_DIR, np);
249 std::string sha256_info = concatPath(tac_version_dir, TAC_SHA_256_INFO);
250 if (!bf::exists(tac_version_dir)) {
251 _INFO("Create tac_version_dir [%s]", tac_version_dir.c_str());
252 if (!createDir(tac_version_dir)) {
253 _ERR("Cannot create directory: %s", tac_version_dir.c_str());
256 createDirectories.push_back(tac_version_dir);
257 if (!bf::is_symlink(sha256_info)) {
258 createSHA256Info(sha256_info, np);
260 _ERR("Failed to create sha256_info. Symbolic link is detected");
264 if (copyNCreateSymlink(binPath, tac_version_dir, np, true) < 0) {
265 _ERR("Failed to create symlink");
269 char *sql = sqlite3_mprintf(
270 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
271 "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
272 dbInsert(tac_db, TAC_APP_LIST_DB, sql);
275 _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str());
276 if (!bf::is_symlink(sha256_info)) {
277 if (compareSHA256Info(sha256_info, np)) {
278 if (copyNCreateSymlink(binPath, tac_version_dir, np, false) < 0) {
279 _ERR("Failed to create symlink");
283 char *sql = sqlite3_mprintf(
284 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
285 "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
286 dbInsert(tac_db, TAC_APP_LIST_DB, sql);
289 _INFO("Different nuget : %s", np.c_str());
292 _ERR("Failed to create sha256_info. Symbolic link is detected");
296 if (!bf::exists(sha256_info)) {
297 if (!removeAll(tac_version_dir)) {
298 _ERR("Failed to remove of %s", tac_version_dir.c_str());
305 static int sqliteCb(void *count, int argc, char **argv, char **colName)
307 int *c = (int*)count;
312 static int updateTacDB(sqlite3 *sqlite)
314 for (auto& unp : updateTac) {
316 char *sql = sqlite3_mprintf("SELECT COUNT(NUGET) FROM TAC WHERE NUGET = %Q;", unp.c_str());
317 int ret = sqlite3_exec(sqlite, sql, sqliteCb, &count, NULL);
318 if (ret != SQLITE_OK) {
319 _ERR("Sqlite select error");
324 std::string tac_version_dir_prev = concatPath(__DOTNET_DIR, unp);
325 std::string tac_version_dir_backup = tac_version_dir_prev + ".bck";
326 if (!copyDir(tac_version_dir_prev, tac_version_dir_backup)) {
327 _ERR("Failed to copy of %s to %s", tac_version_dir_prev.c_str(), tac_version_dir_backup.c_str());
331 if (!removeAll(tac_version_dir_prev)) {
332 _ERR("Failed to remove of %s", tac_version_dir_prev.c_str());
342 extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *appId, GList *list)
344 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UPGRADE =====]");
345 _INFO("PackageID : %s", pkgId);
347 // Can be multiple apps in one package
348 if (tacPluginInstalled) {
349 _INFO("TAC plugin already upgraded");
352 tacPluginInstalled = true;
354 if (initializePathManager(std::string(), std::string(), std::string())) {
355 _ERR("Fail to initialize PathManger");
359 std::string appType = getAppType(std::string(pkgId));
360 if (strstr(appType.c_str(), "dotnet") == NULL) {
361 _ERR("App type is not dotnet");
364 std::string execName = getExecName(std::string(pkgId));
365 std::string rootPath = getRootPath(std::string(pkgId));
366 if (execName.empty() || rootPath.empty()) {
369 std::string binPath = concatPath(rootPath, "bin");
371 if (!strcmp("removed", status.c_str())) {
372 _INFO("Skipped to parse of deps.json");
374 std::string metaValue = getMetadataValue(std::string(pkgId), TAC_METADATA_KEY);
375 if (metaValue.empty()) {
378 if (metaValue == METADATA_VALUE) {
379 depsJsonCheck(rootPath, binPath, execName);
384 tac_db = dbCreate(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE);
386 _ERR("Sqlite open error");
390 char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
391 updateTac = dbSelect(tac_db, TAC_APP_LIST_DB, sql);
395 sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q;", pkgId);
396 dbDelete(tac_db, TAC_APP_LIST_DB, sql);
398 if (updateTacDB(tac_db) < 0) {
401 _INFO("Not exist data for TAC in %s", pkgId);
404 for (auto& np : tacDB) {
405 std::string tac_name = np.substr(0, np.find('/'));
406 std::string tac_version = np.substr(np.rfind('/') + 1);
407 _INFO("TAC name : %s", tac_name.c_str());
408 _INFO("TAC version : %s", tac_version.c_str());
410 std::string tac_version_dir = concatPath(__DOTNET_DIR, np);
411 std::string sha256_info = concatPath(tac_version_dir, TAC_SHA_256_INFO);
412 if (!bf::exists(tac_version_dir)) {
413 _INFO("Create tac_version_dir [%s]", tac_version_dir.c_str());
414 if (!createDir(tac_version_dir)) {
415 _ERR("Cannot create directory: %s", tac_version_dir.c_str());
418 createDirectories.push_back(tac_version_dir);
419 if (!bf::is_symlink(sha256_info)) {
420 createSHA256Info(sha256_info, np);
422 _ERR("Failed to create sha256_info. Symbolic link is detected");
426 if (copyNCreateSymlink(binPath, tac_version_dir, np, true) < 0) {
427 _ERR("Failed to create symlink");
432 sql = sqlite3_mprintf(
433 "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = %Q AND NAME = %Q;", pkgId, tac_name.c_str());
434 int ret = sqlite3_exec(tac_db, sql, sqliteCb, &count, NULL);
435 if (ret != SQLITE_OK) {
436 _ERR("Sqlite select error");
442 sql = sqlite3_mprintf(
443 "UPDATE TAC SET NAME = %Q, VERSION = %Q, NUGET = %Q WHERE PKGID = %Q AND NAME = %Q;",
444 tac_name.c_str(), tac_version.c_str(), np.c_str(), pkgId, tac_name.c_str());
445 dbUpdate(tac_db, TAC_APP_LIST_DB, sql);
447 } else if (count == 0) {
448 sql = sqlite3_mprintf(
449 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
450 "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
451 dbInsert(tac_db, TAC_APP_LIST_DB, sql);
455 _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str());
456 if (!bf::is_symlink(sha256_info)) {
457 if (compareSHA256Info(sha256_info, np)) {
458 if (copyNCreateSymlink(binPath, tac_version_dir, np, false) < 0) {
459 _ERR("Failed to create symlink");
464 char *sql = sqlite3_mprintf(
465 "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = %Q AND NAME = %Q;", pkgId, tac_name.c_str());
466 int ret = sqlite3_exec(tac_db, sql, sqliteCb, &count, NULL);
467 if (ret != SQLITE_OK) {
468 _ERR("Sqlite select error");
474 sql = sqlite3_mprintf(
475 "UPDATE TAC SET NAME = %Q, VERSION = %Q, NUGET = %Q WHERE PKGID = %Q AND NAME = %Q;",
476 tac_name.c_str(), tac_version.c_str(), np.c_str(), pkgId, tac_name.c_str());
477 dbUpdate(tac_db, TAC_APP_LIST_DB, sql);
479 } else if (count == 0) {
480 sql = sqlite3_mprintf(
481 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
482 "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
483 dbInsert(tac_db, TAC_APP_LIST_DB, sql);
487 _INFO("Different nuget : %s", np.c_str());
490 _ERR("Failed to create sha256_info. Symbolic link is detected");
494 if (!bf::exists(sha256_info)) {
495 if (!removeAll(tac_version_dir)) {
496 _ERR("Failed to remove of %s", tac_version_dir.c_str());
500 for (auto& unp : updateTac) {
501 bool isExits = false;
502 for (auto& np : tacDB) {
503 if (!strcmp(unp.c_str(), np.c_str())) {
509 char *sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q AND NUGET = %Q;", pkgId, unp.c_str());
510 dbDelete(tac_db, TAC_APP_LIST_DB, sql);
514 if (updateTacDB(tac_db) < 0) {
521 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgId, const char *appId, GList *list)
523 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UNINSTALL =====]");
524 _INFO("PackageID : %s", pkgId);
526 // Can be multiple apps in one package
527 if (tacPluginInstalled) {
528 _INFO("TAC plugin already uninstalled");
531 tacPluginInstalled = true;
533 status = "uninstall";
534 tac_db = dbOpen(TAC_APP_LIST_DB);
536 _ERR("Sqlite open error");
540 char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
541 updateTac = dbSelect(tac_db, TAC_APP_LIST_DB, sql);
544 sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q;", pkgId);
545 dbDelete(tac_db, TAC_APP_LIST_DB, sql);
548 if (updateTacDB(tac_db) < 0) {
554 extern "C" int PKGMGR_MDPARSER_PLUGIN_REMOVED(const char *pkgId, const char *appId, GList *list)
556 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_REMOVED =====]");
557 _INFO("PackageID : %s", pkgId);
561 return PKGMGR_MDPARSER_PLUGIN_UPGRADE(pkgId, appId, list);
564 void cleanStep(std::string tac)
566 std::string current_tac = concatPath(__DOTNET_DIR, tac.substr(0, tac.find('/')));
568 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
569 std::string bck_path = bck.path().string();
570 if (bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") != NULL) {
571 if (!removeAll(bck_path)) {
572 _ERR("Failed to remove of %s", bck_path.c_str());
578 bool isExist = false;
579 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
580 std::string bck_path = bck.path().string();
581 if (bf::exists(bck_path) && bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") == NULL) {
587 if (!removeAll(current_tac)) {
588 _ERR("Failed to remove of %s", current_tac.c_str());
591 } catch (const bf::filesystem_error& error) {
592 _ERR("Failed to recursive directory: %s", error.what());
602 void unInstall_Clean()
604 for (auto& unp : updateTac) {
611 if (!tacDB.empty()) {
612 for (auto& np : tacDB) {
619 extern "C" int PKGMGR_MDPARSER_PLUGIN_CLEAN(const char *pkgId, const char *appId, GList *list)
621 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_CLEAN =====]");
622 _INFO("PackageID : %s", pkgId);
624 // Can be multiple apps in one package
625 if (tacPluginFinished) {
626 _INFO("TAC plugin already finished(CLEAN)");
629 tacPluginFinished = true;
635 if (!strcmp("install", status.c_str())) {
637 } else if (!strcmp("update", status.c_str())) {
639 } else if (!strcmp("uninstall", status.c_str())) {
645 void undoStep(std::string tac)
647 std::string current_tac = concatPath(__DOTNET_DIR, tac.substr(0, tac.find('/')));
649 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
650 std::string bck_path = bck.path().string();
651 if (bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") != NULL) {
652 if (!moveFile(bck_path, bck_path.substr(0, bck_path.rfind(".bck")))) {
653 _ERR("Failed to move %s", bck_path.c_str());
658 } catch (const bf::filesystem_error& error) {
659 _ERR("Failed to recursive directory: %s", error.what());
666 for (auto& cd : createDirectories) {
667 if (!removeAll(cd)) {
668 _ERR("Failed to remove of %s", cd.c_str());
673 void unInstall_Undo()
675 for (auto& unp : updateTac) {
683 if (!tacDB.empty()) {
684 for (auto& np : tacDB) {
691 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNDO(const char *pkgId, const char *appId, GList *list)
693 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UNDO =====]");
694 _INFO("PackageID : %s", pkgId);
696 // Can be multiple apps in one package
697 if (tacPluginFinished) {
698 _INFO("TAC plugin already finished(UNDO)");
701 tacPluginFinished = true;
707 if (!strcmp("install", status.c_str())) {
709 } else if (!strcmp("update", status.c_str())) {
711 } else if (!strcmp("uninstall", status.c_str())) {