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 int copyNCreateSymlink(std::string tac_version_dir, std::string np, bool is_create_tac_dir)
155 if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
156 _ERR("Failed to get UID");
160 std::string tac_dir = concatPath(binPath, TAC_SYMLINK_SUB_DIR);
161 if (!createDir(tac_dir)) {
162 _ERR("Cannot create directory: %s", tac_dir.c_str());
166 for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
167 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
168 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
169 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
170 if (!strcmp(nuget_package.c_str(), np.c_str())) {
171 if (bf::exists(concatPath(binPath, assembly))) {
172 if (is_create_tac_dir) {
173 if (!copyFile(concatPath(binPath, assembly), concatPath(tac_version_dir, assembly))) {
174 _ERR("Failed to copy of %s", assembly.c_str());
178 bf::create_symlink(concatPath(tac_version_dir, assembly), concatPath(tac_dir, assembly));
179 if (lchown(concatPath(tac_dir, assembly).c_str(), uid, 0)) {
180 _ERR("Failed to change owner of: %s", concatPath(tac_dir, assembly).c_str());
183 if (!removeFile(concatPath(binPath, assembly))) {
184 _ERR("Failed to remove of %s", assembly.c_str());
193 void depsJsonCheck() {
194 for (auto& npAssembly : depsJsonParser(rootPath, execName, getTPA())) {
195 std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
196 std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
197 tacDB.push_back(nugetPackage);
198 char buffer[65] = {0};
199 SHA256(concatPath(binPath, assemblyName), buffer);
200 nugetPackagesAssembliesSha.push_back(nugetPackage + ":" + assemblyName + ":" + buffer);
201 _INFO("Assembly : [%s] / SHA256 : [%s]", assemblyName.c_str(), buffer);
203 std::sort(tacDB.begin(), tacDB.end());
204 tacDB.erase(unique(tacDB.begin(), tacDB.end()), tacDB.end());
207 extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *appId, GList *list)
209 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_INSTALL =====]");
210 _INFO("PackageID : %s", pkgId);
212 if (!appTypeCheck(std::string(pkgId))) {
213 _INFO("App type is not dotnet");
216 if (getExecName(std::string(pkgId), execName) < 0) {
219 if (getRootPath(std::string(pkgId), rootPath) < 0) {
222 binPath = concatPath(rootPath, "bin");
224 if (metadataCheck(list)) {
229 tac_db = dbCreate(TAC_APP_LIST_DB);
231 _ERR("Sqlite create error");
236 _INFO("Not exist .deps.json file");
240 for (auto& np : tacDB) {
241 std::string tac_name = np.substr(0, np.find('/'));
242 std::string tac_version = np.substr(np.rfind('/') + 1);
243 _INFO("TAC name : %s", tac_name.c_str());
244 _INFO("TAC version : %s", tac_version.c_str());
246 std::string tac_version_dir = concatPath(__TAC_DIR, np);
247 std::string sha256_info = concatPath(tac_version_dir, TAC_SHA_256_INFO);
248 if (!bf::exists(tac_version_dir)) {
249 _INFO("Create tac_version_dir [%s]", tac_version_dir.c_str());
250 if (!createDir(tac_version_dir)) {
251 _ERR("Cannot create directory: %s", tac_version_dir.c_str());
254 createDirectories.push_back(tac_version_dir);
255 std::ofstream ofs(sha256_info, std::ios::app);
256 int assembly_count = 0;
257 for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
258 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
259 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
260 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
261 std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
262 if (!strcmp(nuget_package.c_str(), np.c_str())) {
263 ofs << assembly << ":" << sha << std::endl;
267 ofs << assembly_count << std::endl;
270 if (copyNCreateSymlink(tac_version_dir, np, true) < 0) {
271 _ERR("Failed to create symlink");
274 std::string sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
275 "VALUES ('" + std::string(pkgId) + "', '" + np + "', '" + tac_name + "', '" + tac_version + "');";
276 dbInsert(tac_db, TAC_APP_LIST_DB, sql);
278 _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str());
279 int compare_count = 0;
280 int assembly_count = 0;
281 std::string sha256_count = "0";
282 for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
283 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
284 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
285 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
286 std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
287 if (!strcmp(nuget_package.c_str(), np.c_str())) {
289 std::ifstream ifs(sha256_info);
292 while (getline(ifs, get_str)) {
293 if (!strcmp(get_str.c_str(), (assembly + ":" + sha).c_str())) {
296 sha256_count = get_str;
302 if (!strcmp(std::to_string(assembly_count).c_str(), std::to_string(compare_count).c_str()) &&
303 !strcmp(std::to_string(assembly_count).c_str(), sha256_count.c_str())) {
304 _INFO("Same nuget : %s", np.c_str());
305 if (copyNCreateSymlink(tac_version_dir, np, false) < 0) {
306 _ERR("Failed to create symlink");
309 std::string sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
310 "VALUES ('" + std::string(pkgId) + "', '" + np + "', '" + tac_name + "', '" + tac_version + "');";
311 dbInsert(tac_db, TAC_APP_LIST_DB, sql);
313 _INFO("Different nuget : %s", np.c_str());
316 if (!bf::exists(sha256_info)) {
317 if(!removeAll(tac_version_dir)) {
318 _ERR("Failed to remove of %s", tac_version_dir.c_str());
325 static int sqliteCb(void *count, int argc, char **argv, char **colName) {
326 int *c = (int*)count;
331 int updateTacDB(const char *pkgId)
333 for (auto& unp : updateTac) {
335 std::string sql = "SELECT COUNT(NUGET) FROM TAC WHERE NUGET = '" + unp + "';";
336 int ret = sqlite3_exec(tac_db, sql.c_str(), sqliteCb, &count, NULL);
337 if (ret != SQLITE_OK) {
338 _ERR("Sqlite select error");
342 std::string tac_version_dir_prev = concatPath(__TAC_DIR, unp);
343 std::string tac_version_dir_backup = tac_version_dir_prev + ".bck";
344 if (!copyDir(tac_version_dir_prev, tac_version_dir_backup)) {
345 _ERR("Failed to copy of %s to %s", tac_version_dir_prev.c_str(), tac_version_dir_backup.c_str());
348 if (!removeAll(tac_version_dir_prev)) {
349 _ERR("Failed to remove of %s", tac_version_dir_prev.c_str());
357 extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *appId, GList *list)
359 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UPGRADE =====]");
360 _INFO("PackageID : %s", pkgId);
362 if (!appTypeCheck(std::string(pkgId))) {
363 _INFO("App type is not dotnet");
366 if (getExecName(std::string(pkgId), execName) < 0) {
369 if (getRootPath(std::string(pkgId), rootPath) < 0) {
372 binPath = concatPath(rootPath, "bin");
374 if (!strcmp("removed", status.c_str())) {
375 _INFO("Skipped to parse of deps.json");
377 if (metadataCheck(list)) {
383 tac_db = dbCreate(TAC_APP_LIST_DB);
385 _ERR("Sqlite open error");
390 std::string sql = "DELETE FROM TAC WHERE PKGID = '" + std::string(pkgId) + "';";
391 dbDelete(tac_db, TAC_APP_LIST_DB, sql);
392 if (updateTacDB(pkgId) < 0) {
396 std::string sql = "SELECT * FROM TAC WHERE PKGID = '" + std::string(pkgId) + "';";
397 updateTac = dbSelect(tac_db, TAC_APP_LIST_DB, sql);
399 for (auto& np : tacDB) {
400 std::string tac_name = np.substr(0, np.find('/'));
401 std::string tac_version = np.substr(np.rfind('/') + 1);
402 _INFO("TAC name : %s", tac_name.c_str());
403 _INFO("TAC version : %s", tac_version.c_str());
405 std::string tac_version_dir = concatPath(__TAC_DIR, np);
406 std::string sha256_info = concatPath(tac_version_dir, TAC_SHA_256_INFO);
407 if (!bf::exists(tac_version_dir)) {
408 _INFO("Create tac_version_dir [%s]", tac_version_dir.c_str());
409 if (!createDir(tac_version_dir)) {
410 _ERR("Cannot create directory: %s", tac_version_dir.c_str());
413 createDirectories.push_back(tac_version_dir);
414 std::ofstream ofs(sha256_info, std::ios::app);
415 int assembly_count = 0;
416 for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
417 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
418 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
419 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
420 std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
421 if (!strcmp(nuget_package.c_str(), np.c_str())) {
422 ofs << assembly << ":" << sha << std::endl;
426 ofs << assembly_count << std::endl;
428 if (copyNCreateSymlink(tac_version_dir, np, true) < 0) {
429 _ERR("Failed to create symlink");
434 sql = "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = '" + std::string(pkgId) + "' AND NAME = '" + tac_name + "';";
435 int ret = sqlite3_exec(tac_db, sql.c_str(), sqliteCb, &count, NULL);
436 if (ret != SQLITE_OK) {
437 _ERR("Sqlite select error");
441 sql = "UPDATE TAC SET NAME = '" + tac_name + "', VERSION = '" + tac_version + "', NUGET = '" + np + "' WHERE PKGID = '" + std::string(pkgId) + "' AND NAME = '" + tac_name + "';";
442 dbUpdate(tac_db, TAC_APP_LIST_DB, sql);
443 } else if (count == 0) {
444 sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
445 "VALUES ('" + std::string(pkgId) + "', '" + np + "', '" + tac_name + "', '" + tac_version + "');";
446 dbInsert(tac_db, TAC_APP_LIST_DB, sql);
449 _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str());
450 int compare_count = 0;
451 int assembly_count = 0;
452 std::string sha256_count = "0";
453 for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
454 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
455 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
456 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
457 std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
458 if (!strcmp(nuget_package.c_str(), np.c_str())) {
460 std::ifstream ifs(sha256_info);
463 while (getline(ifs, get_str)) {
464 if (!strcmp(get_str.c_str(), (assembly + ":" + sha).c_str())) {
467 sha256_count = get_str;
474 if (!strcmp(std::to_string(assembly_count).c_str(), std::to_string(compare_count).c_str()) &&
475 !strcmp(std::to_string(assembly_count).c_str(), sha256_count.c_str())) {
476 _INFO("Same nuget : %s", np.c_str());
477 if (copyNCreateSymlink(tac_version_dir, np, false) < 0) {
478 _ERR("Failed to create symlink");
483 std::string sql = "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = '" + std::string(pkgId) + "' AND NAME = '" + tac_name + "';";
484 int ret = sqlite3_exec(tac_db, sql.c_str(), sqliteCb, &count, NULL);
485 if (ret != SQLITE_OK) {
486 _ERR("Sqlite select error");
490 sql = "UPDATE TAC SET NAME = '" + tac_name + "', VERSION = '" + tac_version + "', NUGET = '" + np + "' WHERE PKGID = '" + std::string(pkgId) + "' AND NAME = '" + tac_name + "';";
491 dbUpdate(tac_db, TAC_APP_LIST_DB, sql);
492 } else if (count == 0) {
493 sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
494 "VALUES ('" + std::string(pkgId) + "', '" + np + "', '" + tac_name + "', '" + tac_version + "');";
495 dbInsert(tac_db, TAC_APP_LIST_DB, sql);
498 _INFO("Different nuget : %s", np.c_str());
501 if (!bf::exists(sha256_info)) {
502 if(!removeAll(tac_version_dir)) {
503 _ERR("Failed to remove of %s", tac_version_dir.c_str());
507 for (auto& unp : updateTac) {
508 bool isExits = false;
509 for (auto& np : tacDB) {
510 if (!strcmp(unp.c_str(), np.c_str())) {
516 std::string sql = "DELETE FROM TAC WHERE PKGID = '" + std::string(pkgId) + "' AND NUGET = '" + unp + "';";
517 dbDelete(tac_db, TAC_APP_LIST_DB, sql);
520 if (updateTacDB(pkgId) < 0) {
527 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgId, const char *appId, GList *list)
529 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UNINSTALL =====]");
530 _INFO("PackageID : %s", pkgId);
532 status = "uninstall";
533 tac_db = dbOpen(TAC_APP_LIST_DB);
535 _ERR("Sqlite open error");
539 std::string sql = "SELECT * FROM TAC WHERE PKGID = '" + std::string(pkgId) + "';";
540 updateTac = dbSelect(tac_db, TAC_APP_LIST_DB, sql);
542 sql = "DELETE FROM TAC WHERE PKGID = '" + std::string(pkgId) + "';";
543 dbDelete(tac_db, TAC_APP_LIST_DB, sql);
545 if (updateTacDB(pkgId) < 0) {
551 extern "C" int PKGMGR_MDPARSER_PLUGIN_REMOVED(const char *pkgId, const char *appId, GList *list)
553 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_REMOVED =====]");
554 _INFO("PackageID : %s", pkgId);
558 return PKGMGR_MDPARSER_PLUGIN_UPGRADE(pkgId, appId, list);
561 void cleanStep(std::string tac)
563 std::string current_tac = concatPath(__TAC_DIR, tac.substr(0, tac.find('/')));
565 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
566 std::string bck_path = bck.path().string();
567 if (bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") != NULL) {
568 if (!removeAll(bck_path)) {
569 _ERR("Failed to remove of %s", bck_path.c_str());
575 bool isExist = false;
576 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
577 std::string bck_path = bck.path().string();
578 if (bf::exists(bck_path) && bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") == NULL) {
584 if (!removeAll(current_tac)) {
585 _ERR("Failed to remove of %s", current_tac.c_str());
588 } catch (const bf::filesystem_error& error) {
589 _ERR("Failed to recursive directory: %s", error.what());
599 void unInstall_Clean()
601 for (auto& unp : updateTac) {
608 if (!tacDB.empty()) {
609 for (auto& np : tacDB) {
616 extern "C" int PKGMGR_MDPARSER_PLUGIN_CLEAN(const char *pkgId, const char *appId, GList *list)
618 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_CLEAN =====]");
619 _INFO("PackageID : %s", pkgId);
625 if (!strcmp("install", status.c_str())) {
627 } else if (!strcmp("update", status.c_str())) {
629 } else if (!strcmp("uninstall", status.c_str())) {
635 void undoStep(std::string tac)
637 std::string current_tac = concatPath(__TAC_DIR, tac.substr(0, tac.find('/')));
639 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
640 std::string bck_path = bck.path().string();
641 if (bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") != NULL) {
642 if (!moveFile(bck_path, bck_path.substr(0, bck_path.rfind(".bck")))) {
643 _ERR("Failed to move %s", bck_path.c_str());
648 } catch (const bf::filesystem_error& error) {
649 _ERR("Failed to recursive directory: %s", error.what());
656 for (auto& cd : createDirectories) {
657 if (!removeAll(cd)) {
658 _ERR("Failed to remove of %s", cd.c_str());
663 void unInstall_Undo()
665 for (auto& unp : updateTac) {
673 if (!tacDB.empty()) {
674 for (auto& np : tacDB) {
681 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNDO(const char *pkgId, const char *appId, GList *list)
683 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UNDO =====]");
684 _INFO("PackageID : %s", pkgId);
690 if (!strcmp("install", status.c_str())) {
692 } else if (!strcmp("update", status.c_str())) {
694 } else if (!strcmp("uninstall", status.c_str())) {