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"
27 #include <boost/filesystem.hpp>
29 #include <json/json.h>
30 #include <pkgmgr-info.h>
31 #include <pkgmgr_installer_info.h>
32 #include <openssl/sha.h>
37 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
40 #define __STR(x) __XSTR(x)
41 static const char* __TAC_DIR = __STR(TAC_DIR);
45 typedef struct Metadata {
50 std::vector<std::string> nugetPackagesAssembliesSha;
51 std::vector<std::string> tacDB;
52 std::vector<std::string> createDirectories;
53 std::vector<std::string> updateTac;
54 std::string status = "";
58 bool isCreateDirectory = false;
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 createSymlink(std::string tac_version_dir, std::string np)
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 (isCreateDirectory) {
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 if (!dbOpen(tac_db, TAC_APP_LIST_DB)) {
232 _ERR("Sqlite open error");
236 _ERR("Sqlite create error");
241 _INFO("Not exist .deps.json file");
245 for (auto& np : tacDB) {
246 std::string tac_name = np.substr(0, np.find('/'));
247 std::string tac_version = np.substr(np.rfind('/') + 1);
248 _INFO("TAC name : %s", tac_name.c_str());
249 _INFO("TAC version : %s", tac_version.c_str());
251 std::string tac_version_dir = concatPath(__TAC_DIR, np);
252 std::string sha256_info = concatPath(tac_version_dir, TAC_SHA_256_INFO);
253 isCreateDirectory = false;
254 if (!bf::exists(tac_version_dir)) {
255 _INFO("Create tac_version_dir [%s]", tac_version_dir.c_str());
256 if (!createDir(tac_version_dir)) {
257 _ERR("Cannot create directory: %s", tac_version_dir.c_str());
260 isCreateDirectory = true;
261 createDirectories.push_back(tac_version_dir);
262 std::ofstream ofs(sha256_info, std::ios::app);
263 int assembly_count = 0;
264 for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
265 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
266 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
267 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
268 std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
269 if (!strcmp(nuget_package.c_str(), np.c_str())) {
270 ofs << assembly << ":" << sha << std::endl;
274 ofs << assembly_count << std::endl;
277 if (createSymlink(tac_version_dir, np) < 0) {
278 _ERR("Failed to create symlink");
281 std::string sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
282 "VALUES ('" + std::string(pkgId) + "', '" + np + "', '" + tac_name + "', '" + tac_version + "');";
283 dbInsert(tac_db, TAC_APP_LIST_DB, sql);
285 _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str());
286 int compare_count = 0;
287 int assembly_count = 0;
288 std::string sha256_count = "0";
289 for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
290 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
291 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
292 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
293 std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
294 if (!strcmp(nuget_package.c_str(), np.c_str())) {
296 std::ifstream ifs(sha256_info);
299 while (getline(ifs, get_str)) {
300 if (!strcmp(get_str.c_str(), (assembly + ":" + sha).c_str())) {
303 sha256_count = get_str;
309 if (!strcmp(std::to_string(assembly_count).c_str(), std::to_string(compare_count).c_str()) &&
310 !strcmp(std::to_string(assembly_count).c_str(), sha256_count.c_str())) {
311 _INFO("Same nuget : %s", np.c_str());
312 if (createSymlink(tac_version_dir, np) < 0) {
313 _ERR("Failed to create symlink");
316 std::string sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
317 "VALUES ('" + std::string(pkgId) + "', '" + np + "', '" + tac_name + "', '" + tac_version + "');";
318 dbInsert(tac_db, TAC_APP_LIST_DB, sql);
320 _INFO("Different nuget : %s", np.c_str());
323 if (!bf::exists(sha256_info)) {
324 if(!removeAll(tac_version_dir)) {
325 _ERR("Failed to remove of %s", tac_version_dir.c_str());
332 static int sqliteCb(void *count, int argc, char **argv, char **colName) {
333 int *c = (int*)count;
338 int updateTacDB(const char *pkgId)
340 for (auto& unp : updateTac) {
343 if (!dbOpen(tac_db, TAC_APP_LIST_DB)) {
344 _ERR("Sqlite open error");
348 _ERR("Sqlite create error");
351 std::string sql = "SELECT COUNT(NUGET) FROM TAC WHERE NUGET = '" + unp + "';";
352 int ret = sqlite3_exec(tac_db, sql.c_str(), sqliteCb, &count, NULL);
353 if (ret != SQLITE_OK) {
354 _ERR("Sqlite select error");
358 std::string tac_version_dir_prev = concatPath(__TAC_DIR, unp);
359 std::string tac_version_dir_backup = tac_version_dir_prev + ".bck";
360 if (!copyDir(tac_version_dir_prev, tac_version_dir_backup)) {
361 _ERR("Failed to copy of %s to %s", tac_version_dir_prev.c_str(), tac_version_dir_backup.c_str());
364 if (!removeAll(tac_version_dir_prev)) {
365 _ERR("Failed to remove of %s", tac_version_dir_prev.c_str());
373 extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *appId, GList *list)
375 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UPGRADE =====]");
376 _INFO("PackageID : %s", pkgId);
378 if (!appTypeCheck(std::string(pkgId))) {
379 _INFO("App type is not dotnet");
382 if (getExecName(std::string(pkgId), execName) < 0) {
385 if (getRootPath(std::string(pkgId), rootPath) < 0) {
388 binPath = concatPath(rootPath, "bin");
390 if (!strcmp("removed", status.c_str())) {
391 _INFO("Skipped to parse of deps.json");
393 if (metadataCheck(list)) {
399 tac_db = dbCreate(TAC_APP_LIST_DB);
401 if (!dbOpen(tac_db, TAC_APP_LIST_DB)) {
402 _ERR("Sqlite open error");
406 _ERR("Sqlite create error");
410 std::string sql = "SELECT * FROM TAC WHERE PKGID = '" + std::string(pkgId) + "';";
411 updateTac = dbSelect(tac_db, TAC_APP_LIST_DB, sql);
414 sql = "DELETE FROM TAC WHERE PKGID = '" + std::string(pkgId) + "';";
415 dbDelete(tac_db, TAC_APP_LIST_DB, sql);
416 if (updateTacDB(pkgId) < 0) {
420 for (auto& np : tacDB) {
421 std::string tac_name = np.substr(0, np.find('/'));
422 std::string tac_version = np.substr(np.rfind('/') + 1);
423 _INFO("TAC name : %s", tac_name.c_str());
424 _INFO("TAC version : %s", tac_version.c_str());
426 std::string tac_version_dir = concatPath(__TAC_DIR, np);
427 std::string sha256_info = concatPath(tac_version_dir, TAC_SHA_256_INFO);
428 isCreateDirectory = false;
429 if (!bf::exists(tac_version_dir)) {
430 _INFO("Create tac_version_dir [%s]", tac_version_dir.c_str());
431 if (!createDir(tac_version_dir)) {
432 _ERR("Cannot create directory: %s", tac_version_dir.c_str());
435 isCreateDirectory = true;
436 createDirectories.push_back(tac_version_dir);
437 std::ofstream ofs(sha256_info, std::ios::app);
438 int assembly_count = 0;
439 for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
440 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
441 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
442 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
443 std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
444 if (!strcmp(nuget_package.c_str(), np.c_str())) {
445 ofs << assembly << ":" << sha << std::endl;
449 ofs << assembly_count << std::endl;
451 if (createSymlink(tac_version_dir, np) < 0) {
452 _ERR("Failed to create symlink");
457 sql = "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = '" + std::string(pkgId) + "' AND NAME = '" + tac_name + "';";
458 int ret = sqlite3_exec(tac_db, sql.c_str(), sqliteCb, &count, NULL);
459 if (ret != SQLITE_OK) {
460 _ERR("Sqlite select error");
464 sql = "UPDATE TAC SET NAME = '" + tac_name + "', VERSION = '" + tac_version + "', NUGET = '" + np + "' WHERE PKGID = '" + std::string(pkgId) + "' AND NAME = '" + tac_name + "';";
465 dbUpdate(tac_db, TAC_APP_LIST_DB, sql);
466 } else if (count == 0) {
467 sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
468 "VALUES ('" + std::string(pkgId) + "', '" + np + "', '" + tac_name + "', '" + tac_version + "');";
469 dbInsert(tac_db, TAC_APP_LIST_DB, sql);
472 _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str());
473 int compare_count = 0;
474 int assembly_count = 0;
475 std::string sha256_count = "0";
476 for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
477 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
478 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
479 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
480 std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
481 if (!strcmp(nuget_package.c_str(), np.c_str())) {
483 std::ifstream ifs(sha256_info);
486 while (getline(ifs, get_str)) {
487 if (!strcmp(get_str.c_str(), (assembly + ":" + sha).c_str())) {
490 sha256_count = get_str;
497 if (!strcmp(std::to_string(assembly_count).c_str(), std::to_string(compare_count).c_str()) &&
498 !strcmp(std::to_string(assembly_count).c_str(), sha256_count.c_str())) {
499 _INFO("Same nuget : %s", np.c_str());
500 if (createSymlink(tac_version_dir, np) < 0) {
501 _ERR("Failed to create symlink");
506 std::string sql = "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = '" + std::string(pkgId) + "' AND NAME = '" + tac_name + "';";
507 int ret = sqlite3_exec(tac_db, sql.c_str(), sqliteCb, &count, NULL);
508 if (ret != SQLITE_OK) {
509 _ERR("Sqlite select error");
513 sql = "UPDATE TAC SET NAME = '" + tac_name + "', VERSION = '" + tac_version + "', NUGET = '" + np + "' WHERE PKGID = '" + std::string(pkgId) + "' AND NAME = '" + tac_name + "';";
514 dbUpdate(tac_db, TAC_APP_LIST_DB, sql);
515 } else if (count == 0) {
516 sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
517 "VALUES ('" + std::string(pkgId) + "', '" + np + "', '" + tac_name + "', '" + tac_version + "');";
518 dbInsert(tac_db, TAC_APP_LIST_DB, sql);
521 _INFO("Different nuget : %s", np.c_str());
524 if (!bf::exists(sha256_info)) {
525 if(!removeAll(tac_version_dir)) {
526 _ERR("Failed to remove of %s", tac_version_dir.c_str());
530 for (auto& unp : updateTac) {
531 bool isExits = false;
532 for (auto& np : tacDB) {
533 if (!strcmp(unp.c_str(), np.c_str())) {
539 std::string sql = "DELETE FROM TAC WHERE PKGID = '" + std::string(pkgId) + "' AND NUGET = '" + unp + "';";
540 dbDelete(tac_db, TAC_APP_LIST_DB, sql);
543 if (updateTacDB(pkgId) < 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 = dbCreate(TAC_APP_LIST_DB);
558 if (!dbOpen(tac_db, TAC_APP_LIST_DB)) {
559 _ERR("Sqlite open error");
563 _ERR("Sqlite create error");
567 std::string sql = "SELECT * FROM TAC WHERE PKGID = '" + std::string(pkgId) + "';";
568 updateTac = dbSelect(tac_db, TAC_APP_LIST_DB, sql);
570 sql = "DELETE FROM TAC WHERE PKGID = '" + std::string(pkgId) + "';";
571 dbDelete(tac_db, TAC_APP_LIST_DB, sql);
573 if (updateTacDB(pkgId) < 0) {
579 extern "C" int PKGMGR_MDPARSER_PLUGIN_REMOVED(const char *pkgId, const char *appId, GList *list)
581 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_REMOVED =====]");
582 _INFO("PackageID : %s", pkgId);
586 return PKGMGR_MDPARSER_PLUGIN_UPGRADE(pkgId, appId, list);
589 void cleanStep(std::string tac)
591 std::string current_tac = concatPath(__TAC_DIR, tac.substr(0, tac.find('/')));
593 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
594 std::string bck_path = bck.path().string();
595 if (bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") != NULL) {
596 if (!removeAll(bck_path)) {
597 _ERR("Failed to remove of %s", bck_path.c_str());
603 bool isExist = false;
604 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
605 std::string bck_path = bck.path().string();
606 if (bf::exists(bck_path) && bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") == NULL) {
612 if (!removeAll(current_tac)) {
613 _ERR("Failed to remove of %s", current_tac.c_str());
616 } catch (const bf::filesystem_error& error) {
617 _ERR("Failed to recursive directory: %s", error.what());
627 void unInstall_Clean()
629 for (auto& unp : updateTac) {
636 if (!tacDB.empty()) {
637 for (auto& np : tacDB) {
644 extern "C" int PKGMGR_MDPARSER_PLUGIN_CLEAN(const char *pkgId, const char *appId, GList *list)
646 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_CLEAN =====]");
647 _INFO("PackageID : %s", pkgId);
653 if (!strcmp("install", status.c_str())) {
655 } else if (!strcmp("update", status.c_str())) {
657 } else if (!strcmp("uninstall", status.c_str())) {
663 void undoStep(std::string tac)
665 std::string current_tac = concatPath(__TAC_DIR, tac.substr(0, tac.find('/')));
667 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
668 std::string bck_path = bck.path().string();
669 if (bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") != NULL) {
670 if (!moveFile(bck_path, bck_path.substr(0, bck_path.rfind(".bck")))) {
671 _ERR("Failed to move %s", bck_path.c_str());
676 } catch (const bf::filesystem_error& error) {
677 _ERR("Failed to recursive directory: %s", error.what());
684 for (auto& cd : createDirectories) {
685 if (!removeAll(cd)) {
686 _ERR("Failed to remove of %s", cd.c_str());
691 void unInstall_Undo()
693 for (auto& unp : updateTac) {
701 if (!tacDB.empty()) {
702 for (auto& np : tacDB) {
709 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNDO(const char *pkgId, const char *appId, GList *list)
711 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UNDO =====]");
712 _INFO("PackageID : %s", pkgId);
718 if (!strcmp("install", status.c_str())) {
720 } else if (!strcmp("update", status.c_str())) {
722 } else if (!strcmp("uninstall", status.c_str())) {