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* __TAC_DIR = __STR(TAC_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;
61 bool metadataCheck(GList *list)
64 Metadata *mdInfo = NULL;
65 tag = g_list_first(list);
66 mdInfo = (Metadata*)tag->data;
67 if (strcmp(mdInfo->key, TAC_METADATA_KEY) == 0 && strcmp(mdInfo->value, METADATA_VALUE) == 0) {
68 _DBG("Prefer nuget cache set TRUE");
69 if (initializePluginManager("normal")) {
70 _ERR("Fail to initialize PluginManager");
73 if (initializePathManager(std::string(), std::string(), std::string())) {
74 _ERR("Fail to initialize PathManger");
82 bool appTypeCheck(std::string pkgId)
85 if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
86 _ERR("Failed to get UID");
90 pkgmgrinfo_pkginfo_h handle;
91 int ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
92 if (ret != PMINFO_R_OK) {
93 _ERR("Failed to get pkg info");
97 bool isDotnetAppType = false;
98 auto dotnetAppCounter = [] (pkgmgrinfo_appinfo_h handle, void *userData) -> int {
100 bool* dotnet = static_cast<bool*>(userData);
101 if (pkgmgrinfo_appinfo_get_apptype(handle, &type) != PMINFO_R_OK) {
102 _ERR("Failed to get app type : %s", type);
105 if (strcmp(type, "dotnet") == 0) {
111 if (pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP, dotnetAppCounter, &isDotnetAppType, uid) != PMINFO_R_OK) {
112 _ERR("Failed to get list of app in pkg : %s", pkgId.c_str());
113 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
117 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
118 return isDotnetAppType;
121 void SHA256(std::string path, char outputBuffer[65])
123 FILE *file = fopen(path.c_str(), "rb");
128 unsigned char hash[SHA256_DIGEST_LENGTH];
130 SHA256_Init(&sha256);
132 const int bufSize = 32768;
133 char *buffer = (char*)malloc(bufSize);
139 while ((bytesRead = fread(buffer, 1, bufSize, file))) {
140 SHA256_Update(&sha256, buffer, bytesRead);
142 SHA256_Final(hash, &sha256);
143 for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
144 snprintf(outputBuffer + (i * 2), 3, "%02x", hash[i]);
146 outputBuffer[64] = 0;
152 void createSHA256Info(std::string sha256_info, std::string np)
154 std::ofstream ofs(sha256_info, std::ios::app);
155 int assembly_count = 0;
156 for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
157 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
158 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
159 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
160 std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
161 if (!strcmp(nuget_package.c_str(), np.c_str())) {
162 ofs << assembly << ":" << sha << std::endl;
166 ofs << assembly_count << std::endl;
170 int compareSHA256Info(std::string sha256_info, std::string np)
172 int compare_count = 0;
173 int assembly_count = 0;
174 std::string sha256_count = "0";
175 for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
176 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
177 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
178 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
179 std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
180 if (!strcmp(nuget_package.c_str(), np.c_str())) {
182 std::ifstream ifs(sha256_info);
185 while (getline(ifs, get_str)) {
186 if (!strcmp(get_str.c_str(), (assembly + ":" + sha).c_str())) {
189 sha256_count = get_str;
195 if (!strcmp(std::to_string(assembly_count).c_str(), std::to_string(compare_count).c_str()) &&
196 !strcmp(std::to_string(assembly_count).c_str(), sha256_count.c_str())) {
197 _INFO("Same nuget : %s", np.c_str());
203 int copyNCreateSymlink(std::string tac_version_dir, std::string np, bool is_create_tac_dir)
207 if (pkgmgr_installer_info_get_target_uid(&g_uid) < 0) {
208 _ERR("Failed to get UID");
212 std::string tac_dir = concatPath(binPath, TAC_SYMLINK_SUB_DIR);
213 if (!createDir(tac_dir)) {
214 _ERR("Cannot create directory: %s", tac_dir.c_str());
218 for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
219 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
220 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
221 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
222 if (!strcmp(nuget_package.c_str(), np.c_str())) {
223 if (bf::exists(concatPath(binPath, assembly))) {
224 if (is_create_tac_dir) {
225 if (!copyFile(concatPath(binPath, assembly), concatPath(tac_version_dir, assembly))) {
226 _ERR("Failed to copy of %s", assembly.c_str());
230 bf::create_symlink(concatPath(tac_version_dir, assembly), concatPath(tac_dir, assembly));
231 if (lchown(concatPath(tac_dir, assembly).c_str(), g_uid, g_gid)) {
232 _ERR("Failed to change owner of: %s", concatPath(tac_dir, assembly).c_str());
235 if (!removeFile(concatPath(binPath, assembly))) {
236 _ERR("Failed to remove of %s", assembly.c_str());
245 void depsJsonCheck() {
246 for (auto& npAssembly : depsJsonParser(rootPath, execName, getTPA())) {
247 std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
248 std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
249 tacDB.push_back(nugetPackage);
250 char buffer[65] = {0};
251 SHA256(concatPath(binPath, assemblyName), buffer);
252 nugetPackagesAssembliesSha.push_back(nugetPackage + ":" + assemblyName + ":" + buffer);
253 _INFO("Assembly : [%s] / SHA256 : [%s]", assemblyName.c_str(), buffer);
255 std::sort(tacDB.begin(), tacDB.end());
256 tacDB.erase(unique(tacDB.begin(), tacDB.end()), tacDB.end());
259 extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *appId, GList *list)
261 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_INSTALL =====]");
262 _INFO("PackageID : %s", pkgId);
264 if (!appTypeCheck(std::string(pkgId))) {
265 _INFO("App type is not dotnet");
268 if (getExecName(std::string(pkgId), execName) < 0) {
271 if (getRootPath(std::string(pkgId), rootPath) < 0) {
274 binPath = concatPath(rootPath, "bin");
276 if (metadataCheck(list)) {
281 tac_db = dbCreate(TAC_APP_LIST_DB);
283 _ERR("Sqlite create error");
288 _INFO("Not exist .deps.json file");
292 for (auto& np : tacDB) {
293 std::string tac_name = np.substr(0, np.find('/'));
294 std::string tac_version = np.substr(np.rfind('/') + 1);
295 _INFO("TAC name : %s", tac_name.c_str());
296 _INFO("TAC version : %s", tac_version.c_str());
298 std::string tac_version_dir = concatPath(__TAC_DIR, np);
299 std::string sha256_info = concatPath(tac_version_dir, TAC_SHA_256_INFO);
300 if (!bf::exists(tac_version_dir)) {
301 _INFO("Create tac_version_dir [%s]", tac_version_dir.c_str());
302 if (!createDir(tac_version_dir)) {
303 _ERR("Cannot create directory: %s", tac_version_dir.c_str());
306 createDirectories.push_back(tac_version_dir);
307 if (!bf::is_symlink(sha256_info)) {
308 createSHA256Info(sha256_info, np);
310 _ERR("Failed to create sha256_info. Symbolic link is detected");
314 if (copyNCreateSymlink(tac_version_dir, np, true) < 0) {
315 _ERR("Failed to create symlink");
319 char *sql = sqlite3_mprintf(
320 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
321 "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
322 dbInsert(tac_db, TAC_APP_LIST_DB, sql);
325 _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str());
326 if (!bf::is_symlink(sha256_info)) {
327 if (compareSHA256Info(sha256_info, np)) {
328 if (copyNCreateSymlink(tac_version_dir, np, false) < 0) {
329 _ERR("Failed to create symlink");
333 char *sql = sqlite3_mprintf(
334 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
335 "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
336 dbInsert(tac_db, TAC_APP_LIST_DB, sql);
339 _INFO("Different nuget : %s", np.c_str());
342 _ERR("Failed to create sha256_info. Symbolic link is detected");
346 if (!bf::exists(sha256_info)) {
347 if(!removeAll(tac_version_dir)) {
348 _ERR("Failed to remove of %s", tac_version_dir.c_str());
355 static int sqliteCb(void *count, int argc, char **argv, char **colName) {
356 int *c = (int*)count;
361 int updateTacDB(sqlite3 *sqlite)
363 for (auto& unp : updateTac) {
365 char *sql = sqlite3_mprintf("SELECT COUNT(NUGET) FROM TAC WHERE NUGET = %Q;", unp.c_str());
366 int ret = sqlite3_exec(sqlite, sql, sqliteCb, &count, NULL);
367 if (ret != SQLITE_OK) {
368 _ERR("Sqlite select error");
373 std::string tac_version_dir_prev = concatPath(__TAC_DIR, unp);
374 std::string tac_version_dir_backup = tac_version_dir_prev + ".bck";
375 if (!copyDir(tac_version_dir_prev, tac_version_dir_backup)) {
376 _ERR("Failed to copy of %s to %s", tac_version_dir_prev.c_str(), tac_version_dir_backup.c_str());
380 if (!removeAll(tac_version_dir_prev)) {
381 _ERR("Failed to remove of %s", tac_version_dir_prev.c_str());
391 extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *appId, GList *list)
393 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UPGRADE =====]");
394 _INFO("PackageID : %s", pkgId);
396 if (!appTypeCheck(std::string(pkgId))) {
397 _INFO("App type is not dotnet");
400 if (getExecName(std::string(pkgId), execName) < 0) {
403 if (getRootPath(std::string(pkgId), rootPath) < 0) {
406 binPath = concatPath(rootPath, "bin");
408 if (!strcmp("removed", status.c_str())) {
409 _INFO("Skipped to parse of deps.json");
411 if (metadataCheck(list)) {
417 tac_db = dbCreate(TAC_APP_LIST_DB);
419 _ERR("Sqlite open error");
423 char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
424 updateTac = dbSelect(tac_db, TAC_APP_LIST_DB, sql);
428 sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q;", pkgId);
429 dbDelete(tac_db, TAC_APP_LIST_DB, sql);
431 if (updateTacDB(tac_db) < 0) {
435 for (auto& np : tacDB) {
436 std::string tac_name = np.substr(0, np.find('/'));
437 std::string tac_version = np.substr(np.rfind('/') + 1);
438 _INFO("TAC name : %s", tac_name.c_str());
439 _INFO("TAC version : %s", tac_version.c_str());
441 std::string tac_version_dir = concatPath(__TAC_DIR, np);
442 std::string sha256_info = concatPath(tac_version_dir, TAC_SHA_256_INFO);
443 if (!bf::exists(tac_version_dir)) {
444 _INFO("Create tac_version_dir [%s]", tac_version_dir.c_str());
445 if (!createDir(tac_version_dir)) {
446 _ERR("Cannot create directory: %s", tac_version_dir.c_str());
449 createDirectories.push_back(tac_version_dir);
450 if (!bf::is_symlink(sha256_info)) {
451 createSHA256Info(sha256_info, np);
453 _ERR("Failed to create sha256_info. Symbolic link is detected");
457 if (copyNCreateSymlink(tac_version_dir, np, true) < 0) {
458 _ERR("Failed to create symlink");
463 sql = sqlite3_mprintf(
464 "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = %Q AND NAME = %Q;", pkgId, tac_name.c_str());
465 int ret = sqlite3_exec(tac_db, sql, sqliteCb, &count, NULL);
466 if (ret != SQLITE_OK) {
467 _ERR("Sqlite select error");
472 sql = sqlite3_mprintf(
473 "UPDATE TAC SET NAME = %Q, VERSION = %Q, NUGET = %Q WHERE PKGID = %Q AND NAME = %Q;",
474 tac_name.c_str(), tac_version.c_str(), np.c_str(), pkgId, tac_name.c_str());
475 dbUpdate(tac_db, TAC_APP_LIST_DB, sql);
477 } else if (count == 0) {
478 sql = sqlite3_mprintf(
479 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
480 "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
481 dbInsert(tac_db, TAC_APP_LIST_DB, sql);
485 _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str());
486 if (!bf::is_symlink(sha256_info)) {
487 if (compareSHA256Info(sha256_info, np)) {
488 if (copyNCreateSymlink(tac_version_dir, np, false) < 0) {
489 _ERR("Failed to create symlink");
494 char *sql = sqlite3_mprintf(
495 "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = %Q AND NAME = %Q;", pkgId, tac_name.c_str());
496 int ret = sqlite3_exec(tac_db, sql, sqliteCb, &count, NULL);
497 if (ret != SQLITE_OK) {
498 _ERR("Sqlite select error");
503 sql = sqlite3_mprintf(
504 "UPDATE TAC SET NAME = %Q, VERSION = %Q, NUGET = %Q WHERE PKGID = %Q AND NAME = %Q;",
505 tac_name.c_str(), tac_version.c_str(), np.c_str(), pkgId, tac_name.c_str());
506 dbUpdate(tac_db, TAC_APP_LIST_DB, sql);
508 } else if (count == 0) {
509 sql = sqlite3_mprintf(
510 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
511 "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
512 dbInsert(tac_db, TAC_APP_LIST_DB, sql);
516 _INFO("Different nuget : %s", np.c_str());
519 _ERR("Failed to create sha256_info. Symbolic link is detected");
523 if (!bf::exists(sha256_info)) {
524 if(!removeAll(tac_version_dir)) {
525 _ERR("Failed to remove of %s", tac_version_dir.c_str());
529 for (auto& unp : updateTac) {
530 bool isExits = false;
531 for (auto& np : tacDB) {
532 if (!strcmp(unp.c_str(), np.c_str())) {
538 char *sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q AND NUGET = %Q;", pkgId, unp.c_str());
539 dbDelete(tac_db, TAC_APP_LIST_DB, sql);
543 if (updateTacDB(tac_db) < 0) {
550 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgId, const char *appId, GList *list)
552 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UNINSTALL =====]");
553 _INFO("PackageID : %s", pkgId);
555 status = "uninstall";
556 tac_db = dbOpen(TAC_APP_LIST_DB);
558 _ERR("Sqlite open error");
562 char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
563 updateTac = dbSelect(tac_db, TAC_APP_LIST_DB, sql);
566 sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q;", pkgId);
567 dbDelete(tac_db, TAC_APP_LIST_DB, sql);
570 if (updateTacDB(tac_db) < 0) {
576 extern "C" int PKGMGR_MDPARSER_PLUGIN_REMOVED(const char *pkgId, const char *appId, GList *list)
578 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_REMOVED =====]");
579 _INFO("PackageID : %s", pkgId);
583 return PKGMGR_MDPARSER_PLUGIN_UPGRADE(pkgId, appId, list);
586 void cleanStep(std::string tac)
588 std::string current_tac = concatPath(__TAC_DIR, tac.substr(0, tac.find('/')));
590 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
591 std::string bck_path = bck.path().string();
592 if (bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") != NULL) {
593 if (!removeAll(bck_path)) {
594 _ERR("Failed to remove of %s", bck_path.c_str());
600 bool isExist = false;
601 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
602 std::string bck_path = bck.path().string();
603 if (bf::exists(bck_path) && bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") == NULL) {
609 if (!removeAll(current_tac)) {
610 _ERR("Failed to remove of %s", current_tac.c_str());
613 } catch (const bf::filesystem_error& error) {
614 _ERR("Failed to recursive directory: %s", error.what());
624 void unInstall_Clean()
626 for (auto& unp : updateTac) {
633 if (!tacDB.empty()) {
634 for (auto& np : tacDB) {
641 extern "C" int PKGMGR_MDPARSER_PLUGIN_CLEAN(const char *pkgId, const char *appId, GList *list)
643 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_CLEAN =====]");
644 _INFO("PackageID : %s", pkgId);
650 if (!strcmp("install", status.c_str())) {
652 } else if (!strcmp("update", status.c_str())) {
654 } else if (!strcmp("uninstall", status.c_str())) {
660 void undoStep(std::string tac)
662 std::string current_tac = concatPath(__TAC_DIR, tac.substr(0, tac.find('/')));
664 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
665 std::string bck_path = bck.path().string();
666 if (bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") != NULL) {
667 if (!moveFile(bck_path, bck_path.substr(0, bck_path.rfind(".bck")))) {
668 _ERR("Failed to move %s", bck_path.c_str());
673 } catch (const bf::filesystem_error& error) {
674 _ERR("Failed to recursive directory: %s", error.what());
681 for (auto& cd : createDirectories) {
682 if (!removeAll(cd)) {
683 _ERR("Failed to remove of %s", cd.c_str());
688 void unInstall_Undo()
690 for (auto& unp : updateTac) {
698 if (!tacDB.empty()) {
699 for (auto& np : tacDB) {
706 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNDO(const char *pkgId, const char *appId, GList *list)
708 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UNDO =====]");
709 _INFO("PackageID : %s", pkgId);
715 if (!strcmp("install", status.c_str())) {
717 } else if (!strcmp("update", status.c_str())) {
719 } else if (!strcmp("uninstall", status.c_str())) {