Merge branch 'tizen' of https://github.sec.samsung.net/j-h-choi/dotnet-launcher into...
[platform/core/dotnet/launcher.git] / NativeLauncher / installer-plugin / prefer_nuget_cache_plugin.cc
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "log.h"
18 #include "ni_common.h"
19 #include "utils.h"
20 #include "db_manager.h"
21
22 #ifdef  LOG_TAG
23 #undef  LOG_TAG
24 #endif
25 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
26
27 #include <cstring>
28 #include <fstream>
29 #include <sstream>
30 #include <vector>
31 #include <boost/filesystem.hpp>
32 #include <glib.h>
33 #include <json/json.h>
34 #include <pkgmgr-info.h>
35 #include <pkgmgr_installer_info.h>
36 #include <openssl/sha.h>
37
38 typedef struct Metadata {
39         const char *key;
40         const char *value;
41 } Metadata;
42
43 const std::string mOptUsrDotnet = "/opt/usr/dotnet";
44 const std::string mTizenNET = "Tizen.NET";
45 const std::string mTizenNETSdk = "Tizen.NET.Sdk";
46 const std::string mNETStandardLibrary = "NETStandard.Library";
47 const std::string mTacRelease = ".TAC.Release";
48 const std::string mDepsJson = ".deps.json";
49 const std::string mBackup = ".bck";
50 const std::string mdValue = "true";
51 const std::string mdKey = "http://tizen.org/metadata/prefer_nuget_cache";
52 const std::string tacAppListDB = mOptUsrDotnet + "/.TAC.App.list.db";
53
54 std::vector<std::string> nugetPackagesAssembliesShaR2R;
55 std::vector<std::string> tacDB;
56 std::vector<std::string> createDirectories;
57 std::vector<std::string> updateTac;
58 std::string status = "";
59 std::string rootPath;
60 std::string execName;
61 bf::path binPath;
62 bool isCreateDirectory = false;
63 static sqlite3 *tac_db = NULL;
64
65 int metadataCheck(GList *list)
66 {
67         GList *tag = NULL;
68         Metadata *mdInfo = NULL;
69         tag = g_list_first(list);
70         mdInfo = (Metadata*)tag->data;
71         if (mdInfo->key == mdKey && mdInfo->value == mdValue) {
72                 _DBG("Prefer nuget cache set TRUE");
73                 return 0;
74         } else {
75                 return 1;
76         }
77 }
78
79 int appTypeCheck(std::string pkgId)
80 {
81         uid_t uid = 0;
82         if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
83                 _ERR("Failed to get UID");
84                 return 0;
85         }
86
87         pkgmgrinfo_pkginfo_h handle;
88         int ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
89         if (ret != PMINFO_R_OK) {
90                 _ERR("Failed to get pkg info");
91                 return 0;
92         }
93
94         bool isDotnetAppType = false;
95         auto dotnetAppCounter = [] (pkgmgrinfo_appinfo_h handle, void *userData) -> int {
96                 char* type = nullptr;
97                 bool* dotnet = static_cast<bool*>(userData);
98                 if (pkgmgrinfo_appinfo_get_apptype(handle, &type) != PMINFO_R_OK) {
99                         _ERR("Failed to get app type : %s", type);
100                         return 0;
101                 }
102                 if (strcmp(type, "dotnet") == 0) {
103                         *dotnet = true;
104                 }
105                 return 0;
106         };
107
108         if (pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP, dotnetAppCounter, &isDotnetAppType, uid) != PMINFO_R_OK) {
109                 _ERR("Failed to get list of app in pkg : %s", pkgId.c_str());
110                 return -1;
111         }
112
113         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
114         return isDotnetAppType;
115 }
116
117 int getExecName(std::string pkgId)
118 {
119         uid_t uid = 0;
120         if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
121                 _ERR("Failed to get UID");
122                 return 0;
123         }
124
125         pkgmgrinfo_pkginfo_h handle;
126         int ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
127         if (ret != PMINFO_R_OK) {
128                 _ERR("Failed to get pkg info");
129                 return 0;
130         }
131
132         auto dotnetAppCounter = [] (pkgmgrinfo_appinfo_h handle, void *userData) -> int {
133                 char* exec = nullptr;
134                 if (pkgmgrinfo_appinfo_get_exec(handle, &exec) != PMINFO_R_OK) {
135                         _ERR("Failed to get exec : %s", exec);
136                         return 0;
137                 }
138                 execName = std::string(exec).substr(std::string(exec).rfind('/') + 1);
139                 return 0;
140         };
141
142         if (pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP, dotnetAppCounter, NULL, uid) != PMINFO_R_OK) {
143                 _ERR("Failed to get list of app in pkg : %s", pkgId.c_str());
144                 return -1;
145         }
146
147         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
148         return 0;
149 }
150
151 int SHA256(std::string path, char outputBuffer[65])
152 {
153         FILE *file = fopen(path.c_str(), "rb");
154         if (!file) {
155                 return -1;
156         }
157
158         unsigned char hash[SHA256_DIGEST_LENGTH];
159         SHA256_CTX sha256;
160         SHA256_Init(&sha256);
161         int bytesRead = 0;
162         const int bufSize = 32768;
163         char *buffer = (char*)malloc(bufSize);
164         if (!buffer) {
165                 fclose(file);
166                 return -1;
167         }
168
169         while ((bytesRead = fread(buffer, 1, bufSize, file))) {
170                 SHA256_Update(&sha256, buffer, bytesRead);
171         }
172         SHA256_Final(hash, &sha256);
173         for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
174                 sprintf(outputBuffer + (i * 2), "%02x", hash[i]);
175         }
176         outputBuffer[64] = 0;
177
178         fclose(file);
179         free(buffer);
180         return 0;
181 }
182
183 int depsJsonParser()
184 {
185         std::string deps_json_name = execName.substr(0, execName.rfind(".dll")) + mDepsJson;
186         if (bf::exists(rootPath + "/" + deps_json_name)) {
187                 std::string deps_json_path = rootPath + "/" + deps_json_name;
188                 std::ifstream ifs(deps_json_path);
189                 Json::CharReaderBuilder reader;
190                 Json::Value root;
191                 std::string error;
192                 if (ifs.is_open()) {
193                         if (!Json::parseFromStream(reader, ifs, &root, &error)) {
194                                 _INFO("Failed to parse of deps.json");
195                                 ifs.close();
196                                 return -1;
197                         }
198                         const Json::Value runtimeTargetName = root["runtimeTarget"]["name"];
199                         std::string runtimeTarget_name = runtimeTargetName.asString();
200                         const Json::Value nugetPackages = root["targets"][runtimeTarget_name.c_str()];
201                         for (auto& nuget : nugetPackages.getMemberNames()) {
202                                 if (strstr(nuget.c_str(), mTizenNET.c_str()) != NULL ||
203                                         strstr(nuget.c_str(), mTizenNETSdk.c_str()) != NULL ||
204                                         strstr(nuget.c_str(), (execName.substr(0, execName.find(".Tizen."))).c_str()) != NULL ||
205                                         strstr(nuget.c_str(), (execName.substr(0, execName.find(".dll"))).c_str()) != NULL) {
206                                         continue;
207                                 } else {
208                                         const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
209                                         if (assemblies != Json::nullValue) {
210                                                 const Json::Value dependencies = nugetPackages[nuget.c_str()]["dependencies"];
211                                                 std::string r2r = "";
212                                                 for (auto& dependency : dependencies.getMemberNames()) {
213                                                         if (strstr(dependency.c_str(), mTizenNET.c_str()) != NULL ||
214                                                                 strstr(dependency.c_str(), mNETStandardLibrary.c_str()) != NULL) {
215                                                                 continue;
216                                                         } else {
217                                                                 r2r = "--r2r";
218                                                         }
219                                                 }
220                                                 if (strcmp(r2r.c_str(), "--r2r")) {
221                                                         tacDB.push_back(nuget);
222                                                         _INFO("Nuget package : %s", nuget.c_str());
223                                                         for (auto& assembly : assemblies.getMemberNames()) {
224                                                                 std::string assembly_name = assembly.substr(assembly.rfind('/') + 1);
225                                                                 char buffer[65];
226                                                                 SHA256((binPath / assembly_name).string(), buffer);
227                                                                 nugetPackagesAssembliesShaR2R.push_back(nuget + "/" + assembly_name + "/" + buffer + "/" + r2r);
228                                                                 _INFO("Assembly / SHA256 : %s / %s", assembly_name.c_str(), buffer);
229                                                         }
230                                                 }
231                                         }
232                                 }
233                         }
234                         ifs.close();
235                 }
236         }
237         return 0;
238 }
239
240 int createSymlink(bf::path tac_version_dir, std::string np)
241 {
242         bs::error_code error;
243         uid_t uid = 0;
244         for (auto& npAssemblyShaR2R : nugetPackagesAssembliesShaR2R) {
245                 std::string nuget_package_assembly_sha = npAssemblyShaR2R.substr(0, npAssemblyShaR2R.rfind('/'));
246                 std::string sha = nuget_package_assembly_sha.substr(nuget_package_assembly_sha.rfind('/') + 1);
247                 std::string nuget_package_assembly = nuget_package_assembly_sha.substr(0, nuget_package_assembly_sha.rfind('/'));
248                 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind('/'));
249                 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind('/') + 1);
250                 std::string r2r = npAssemblyShaR2R.substr(npAssemblyShaR2R.rfind('/') + 1);
251                 std::string ni_assembly = assembly.substr(0, assembly.rfind(".dll")) + ".ni.dll";
252                 if (!strcmp(nuget_package.c_str(), np.c_str())) {
253                         if (bf::exists(binPath / assembly)) {
254                                 if (isCreateDirectory) {
255                                         std::string command = "nitool --dll " + binPath.string() + "/" + assembly + " " + r2r;
256                                         if (system(command.c_str()) != 0) {
257                                                 _ERR("Failed to create ni dll [%s]", command.c_str());
258                                                 return -1;
259                                         }
260                                         if (!copyFile(binPath / ni_assembly, tac_version_dir / ni_assembly)) {
261                                                 _ERR("Failed to move of %s", ni_assembly.c_str());
262                                                 return -1;
263                                         }
264                                         if (!copyFile(binPath / assembly, tac_version_dir / assembly)) {
265                                                 _ERR("Failed to move of %s", assembly.c_str());
266                                                 return -1;
267                                         }
268                                 }
269                                 if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
270                                         _ERR("Failed to get UID");
271                                         return -1;
272                                 }
273                                 bf::create_symlink(tac_version_dir / ni_assembly, binPath / mTacRelease / ni_assembly, error);
274                                 if (error) {
275                                         _ERR("Failed to create symlink");
276                                         return -1;
277                                 }
278                                 bf::create_symlink(tac_version_dir / assembly, binPath / mTacRelease / assembly, error);
279                                 if (error) {
280                                         _ERR("Failed to create symlink");
281                                         return -1;
282                                 }
283                                 if (lchown((binPath / mTacRelease / ni_assembly).c_str(), uid, 0)) {
284                                         _ERR("Failed to change owner of: %s", (binPath / mTacRelease / ni_assembly).c_str());
285                                         return -1;
286                                 }
287                                 if (lchown((binPath / mTacRelease / assembly).c_str(), uid, 0)) {
288                                         _ERR("Failed to change owner of: %s", (binPath / mTacRelease / assembly).c_str());
289                                         return -1;
290                                 }
291                         }
292                 }
293         }
294         return 0;
295 }
296
297 int removeOriginalAssembly() {
298         for (auto& npAssemblyShaR2R : nugetPackagesAssembliesShaR2R) {
299                 std::string nuget_package_assembly_sha = npAssemblyShaR2R.substr(0, npAssemblyShaR2R.rfind('/'));
300                 std::string sha = nuget_package_assembly_sha.substr(nuget_package_assembly_sha.rfind('/') + 1);
301                 std::string nuget_package_assembly = nuget_package_assembly_sha.substr(0, nuget_package_assembly_sha.rfind('/'));
302                 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind('/'));
303                 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind('/') + 1);
304                 std::string r2r = npAssemblyShaR2R.substr(npAssemblyShaR2R.rfind('/') + 1);
305                 std::string ni_assembly = assembly.substr(0, assembly.rfind(".dll")) + ".ni.dll";
306                 if (bf::exists(binPath / assembly)) {
307                         if (!removeFile(binPath / assembly)) {
308                                 _ERR("Failed to remove of %s", assembly.c_str());
309                                 return -1;
310                         }
311                 }
312                 if (bf::exists(binPath / ni_assembly)) {
313                         if (!removeFile(binPath / ni_assembly)) {
314                                 _ERR("Failed to remove of %s", ni_assembly.c_str());
315                                 return -1;
316                         }
317                 }
318         }
319         return 0;
320 }
321
322 extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *appId, GList *list)
323 {
324         _DBG("[===== PKGMGR_MDPARSER_PLUGIN_INSTALL =====]");
325         _INFO("PackageID : %s", pkgId);
326
327         if (!appTypeCheck(std::string(pkgId))) {
328                 _INFO("App type is not dotnet");
329                 return 0;
330         }
331         if (getExecName(std::string(pkgId)) < 0) {
332                 return 0;
333         }
334         if (getRootPath(std::string(pkgId), rootPath) < 0) {
335                 return 0;
336         } else {
337                 binPath = rootPath / bf::path("bin");
338         }
339         if (!metadataCheck(list)) {
340                 if (depsJsonParser()) {
341                         return 0;
342                 }
343         }
344
345         status = "install";
346         tac_db = dbCreate(tacAppListDB);
347         if (tac_db) {
348                 if (!dbOpen(tac_db, tacAppListDB)) {
349                         return 0;
350                 }
351         } else {
352                 _ERR("Sqlite create error");
353                 return 0;
354         }
355
356         if (tacDB.empty()) {
357                 _ERR("Not exist .deps.json file");
358                 return 0;
359         }
360         if (!bf::exists(binPath / mTacRelease)) {
361                 if (!createDir(binPath / mTacRelease)) {
362                         _INFO("Cannot create directory: %s", (binPath / mTacRelease).c_str());
363                         return 0;
364                 }
365         }
366
367         for (auto& np : tacDB) {
368                 std::string tac_name = np.substr(0, np.find('/'));
369                 std::string tac_version = np.substr(np.rfind('/') + 1);
370                 _INFO("TAC name : %s", tac_name.c_str());
371                 _INFO("TAC version : %s", tac_version.c_str());
372
373                 bf::path tac_version_dir = bf::path(mOptUsrDotnet) / np;
374                 isCreateDirectory = false;
375                 if (!bf::exists(tac_version_dir)) {
376                         _INFO("Create tac_version_dir [%s]", tac_version_dir.c_str());
377                         if (!createDir(tac_version_dir)) {
378                                 _ERR("Cannot create directory: %s", tac_version_dir.c_str());
379                                 return 0;
380                         }
381                         isCreateDirectory = true;
382                         createDirectories.push_back(tac_version_dir.string());
383                         std::string sha256_info = (tac_version_dir / bf::path("SHA256.info")).string();
384                         std::ofstream ofs(sha256_info, std::ios::app);
385                         int assembly_count = 0;
386                         for (auto& npAssemblyShaR2R : nugetPackagesAssembliesShaR2R) {
387                                 std::string nuget_package_assembly_sha = npAssemblyShaR2R.substr(0, npAssemblyShaR2R.rfind('/'));
388                                 std::string sha = nuget_package_assembly_sha.substr(nuget_package_assembly_sha.rfind('/') + 1);
389                                 std::string nuget_package_assembly = nuget_package_assembly_sha.substr(0, nuget_package_assembly_sha.rfind('/'));
390                                 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind('/'));
391                                 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind('/') + 1);
392                                 std::string r2r = npAssemblyShaR2R.substr(npAssemblyShaR2R.rfind('/') + 1);
393                                 if (!strcmp(nuget_package.c_str(), np.c_str())) {
394                                         ofs << assembly << ";" << sha << std::endl;
395                                         assembly_count++;
396                                 }
397                         }
398                         ofs << assembly_count << std::endl;
399                         ofs.close();
400
401                         if (createSymlink(tac_version_dir, np)) {
402                                 _ERR("Failed to create symlink");
403                                 return 0;
404                         }
405                         std::string sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
406                                         "VALUES ('" + std::string(pkgId) + "', '" + np + "', '" + tac_name + "', '" + tac_version + "');";
407                         dbInsert(tac_db, tacAppListDB, sql);
408                 } else {
409                         _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str());
410                         int compare_count = 0;
411                         int assembly_count = 0;
412                         std::string sha256_count = "0";
413                         for (auto& npAssemblyShaR2R : nugetPackagesAssembliesShaR2R) {
414                                 std::string nuget_package_assembly_sha = npAssemblyShaR2R.substr(0, npAssemblyShaR2R.rfind('/'));
415                                 std::string sha = nuget_package_assembly_sha.substr(nuget_package_assembly_sha.rfind('/') + 1);
416                                 std::string nuget_package_assembly = nuget_package_assembly_sha.substr(0, nuget_package_assembly_sha.rfind('/'));
417                                 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind('/'));
418                                 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind('/') + 1);
419                                 std::string r2r = npAssemblyShaR2R.substr(npAssemblyShaR2R.rfind('/') + 1);
420                                 if (!strcmp(nuget_package.c_str(), np.c_str())) {
421                                         assembly_count++;
422                                         std::string sha256_info = (tac_version_dir / bf::path("SHA256.info")).string();
423                                         std::ifstream ifs(sha256_info);
424                                         std::string get_str;
425                                         if (ifs.is_open()) {
426                                                 while (getline(ifs, get_str)) {
427                                                         if (!strcmp(get_str.c_str(), (assembly + ";" + sha).c_str())) {
428                                                                 compare_count++;
429                                                         }
430                                                         sha256_count = get_str;
431                                                 }
432                                                 ifs.close();
433                                         }
434                                 }
435                         }
436                         if (!strcmp(std::to_string(assembly_count).c_str(), std::to_string(compare_count).c_str()) &&
437                                 !strcmp(std::to_string(assembly_count).c_str(), sha256_count.c_str())) {
438                                 _INFO("Same : %s", tac_name.c_str());
439                                 if (createSymlink(tac_version_dir, np)) {
440                                         return 0;
441                                 }
442                                 std::string sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
443                                                 "VALUES ('" + std::string(pkgId) + "', '" + np + "', '" + tac_name + "', '" + tac_version + "');";
444                                 dbInsert(tac_db, tacAppListDB, sql);
445                         } else {
446                                 _INFO("Different : %s", tac_name.c_str());
447                         }
448                 }
449         }
450         if (removeOriginalAssembly()) {
451                 return 0;
452         }
453         return 0;
454 }
455
456 static int sqliteCb(void *count, int argc, char **argv, char **azColName) {
457         int *c = (int*)count;
458         *c = atoi(argv[0]);
459         return 0;
460 }
461
462 int updateTacDB(const char *pkgId)
463 {
464         for (auto& unp : updateTac) {
465                 char *error = 0;
466                 int ret;
467                 int count = 0;
468                 if (tac_db) {
469                         if (!dbOpen(tac_db, tacAppListDB)) {
470                                 return 0;
471                         }
472                 } else {
473                         _ERR("Sqlite create error");
474                         return 0;
475                 }
476                 std::string sql = "SELECT COUNT(NUGET) FROM TAC WHERE NUGET = '" + unp + "';";
477                 ret = sqlite3_exec(tac_db, sql.c_str(), sqliteCb, &count, &error);
478                 if (ret != SQLITE_OK) {
479                         _ERR("SQL error: %s", error);
480                         sqlite3_free(error);
481                 }
482                 if (count < 1) {
483                         bf::path tac_version_dir_prev = bf::path(mOptUsrDotnet) / unp;
484                         bf::path tac_version_dir_backup = bf::path(mOptUsrDotnet) / (unp + mBackup);
485                         if (!copyDir(tac_version_dir_prev, tac_version_dir_backup)) {
486                                 _ERR("Failed to copy of %s to %s", tac_version_dir_prev.c_str(), tac_version_dir_backup.c_str());
487                                 return -1;
488                         }
489                         if (!removeAll(tac_version_dir_prev)) {
490                                 _ERR("Failed to remove of %s", tac_version_dir_prev.c_str());
491                                 return -1;
492                         }
493                 }
494         }
495         return 0;
496 }
497
498 extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *appId, GList *list)
499 {
500         _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UPGRADE =====]");
501         _INFO("PackageID : %s", pkgId);
502
503         if (!appTypeCheck(std::string(pkgId))) {
504                 _INFO("App type is not dotnet");
505                 return 0;
506         }
507         if (getExecName(std::string(pkgId)) < 0) {
508                 return 0;
509         }
510         if (getRootPath(std::string(pkgId), rootPath) < 0) {
511                 return 0;
512         } else {
513                 binPath = rootPath / bf::path("bin");
514         }
515         if (!strcmp("removed", status.c_str())) {
516                 _INFO("Skipped to parse of deps.json");
517         } else {
518                 if (!metadataCheck(list)) {
519                         if (depsJsonParser()) {
520                                 return 0;
521                         }
522                 }
523         }
524
525         status = "update";
526         tac_db = dbCreate(tacAppListDB);
527         if (tac_db) {
528                 if (!dbOpen(tac_db, tacAppListDB)) {
529                         return 0;
530                 }
531         } else {
532                 _ERR("Sqlite create error");
533                 return 0;
534         }
535
536         std::string sql = "SELECT * FROM TAC WHERE PKGID = '" + std::string(pkgId) + "';";
537         updateTac = dbSelect(tac_db, tacAppListDB, sql);
538
539         if (tacDB.empty()) {
540                 sql = "DELETE FROM TAC WHERE PKGID = '" + std::string(pkgId) + "';";
541                 dbDelete(tac_db, tacAppListDB, sql);
542                 if (updateTacDB(pkgId)) {
543                         return 0;
544                 }
545         } else {
546                 if (!bf::exists(binPath / mTacRelease)) {
547                         if (!createDir(binPath / mTacRelease)) {
548                                 _INFO("Cannot create directory: %s", (binPath / mTacRelease).c_str());
549                                 return 0;
550                         }
551                 }
552
553                 for (auto& np : tacDB) {
554                         std::string tac_name = np.substr(0, np.find('/'));
555                         std::string tac_version = np.substr(np.rfind('/') + 1);
556                         _INFO("TAC name : %s", tac_name.c_str());
557                         _INFO("TAC version : %s", tac_version.c_str());
558
559                         bf::path tac_version_dir = bf::path(mOptUsrDotnet) / np;
560                         isCreateDirectory = false;
561                         if (!bf::exists(tac_version_dir)) {
562                                 _INFO("Create tac_version_dir [%s]", tac_version_dir.c_str());
563                                 if (!createDir(tac_version_dir)) {
564                                         _ERR("Cannot create directory: %s", tac_version_dir.c_str());
565                                         return 0;
566                                 }
567                                 isCreateDirectory = true;
568                                 createDirectories.push_back(tac_version_dir.string());
569                                 std::string sha256_info = (tac_version_dir / bf::path("SHA256.info")).string();
570                                 std::ofstream ofs2(sha256_info, std::ios::app);
571                                 int assembly_count = 0;
572                                 for (auto& npAssemblyShaR2R : nugetPackagesAssembliesShaR2R) {
573                                         std::string nuget_package_assembly_sha = npAssemblyShaR2R.substr(0, npAssemblyShaR2R.rfind('/'));
574                                         std::string sha = nuget_package_assembly_sha.substr(nuget_package_assembly_sha.rfind('/') + 1);
575                                         std::string nuget_package_assembly = nuget_package_assembly_sha.substr(0, nuget_package_assembly_sha.rfind('/'));
576                                         std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind('/'));
577                                         std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind('/') + 1);
578                                         std::string r2r = npAssemblyShaR2R.substr(npAssemblyShaR2R.rfind('/') + 1);
579                                         if (!strcmp(nuget_package.c_str(), np.c_str())) {
580                                                 ofs2 << assembly << ";" << sha << std::endl;
581                                                 assembly_count++;
582                                         }
583                                 }
584                                 ofs2 << assembly_count << std::endl;
585                                 ofs2.close();
586                                 if (createSymlink(tac_version_dir, np)) {
587                                         return 0;
588                                 }
589
590                                 char *error = 0;
591                                 int count = 0;
592                                 sql = "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = '" + std::string(pkgId) + "' AND NAME = '" + tac_name + "';";
593                                 int ret = sqlite3_exec(tac_db, sql.c_str(), sqliteCb, &count, &error);
594                                 if (ret != SQLITE_OK) {
595                                         _ERR("SQL error: %s", error);
596                                         sqlite3_free(error);
597                                 }
598
599                                 if (count == 1) {
600                                         sql = "UPDATE TAC SET NAME = '" + tac_name + "', VERSION = '" + tac_version + "', NUGET = '" + np + "' WHERE PKGID = '" + std::string(pkgId) + "' AND NAME = '" + tac_name + "';";
601                                         dbUpdate(tac_db, tacAppListDB, sql);
602                                 } else {
603                                         sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
604                                                 "VALUES ('" + std::string(pkgId) + "', '" + np + "', '" + tac_name + "', '" + tac_version + "');";
605                                         dbInsert(tac_db, tacAppListDB, sql);
606                                 }
607                         } else {
608                                 _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str());
609                                 int compare_count = 0;
610                                 int assembly_count = 0;
611                                 std::string sha256_count = "0";
612                                 for (auto& npAssemblyShaR2R : nugetPackagesAssembliesShaR2R) {
613                                         std::string nuget_package_assembly_sha = npAssemblyShaR2R.substr(0, npAssemblyShaR2R.rfind('/'));
614                                         std::string sha = nuget_package_assembly_sha.substr(nuget_package_assembly_sha.rfind('/') + 1);
615                                         std::string nuget_package_assembly = nuget_package_assembly_sha.substr(0, nuget_package_assembly_sha.rfind('/'));
616                                         std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind('/'));
617                                         std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind('/') + 1);
618                                         std::string r2r = npAssemblyShaR2R.substr(npAssemblyShaR2R.rfind('/') + 1);
619                                         if (!strcmp(nuget_package.c_str(), np.c_str())) {
620                                                 assembly_count++;
621                                                 std::string sha256_info = (tac_version_dir / bf::path("SHA256.info")).string();
622                                                 std::ifstream ifs2(sha256_info);
623                                                 std::string get_str;
624                                                 if (ifs2.is_open()) {
625                                                         while (getline(ifs2, get_str)) {
626                                                                 if (!strcmp(get_str.c_str(), (assembly + ";" + sha).c_str())) {
627                                                                         compare_count++;
628                                                                 }
629                                                                 sha256_count = get_str;
630                                                         }
631                                                         ifs2.close();
632                                                 }
633                                         }
634                                 }
635
636                                 if (!strcmp(std::to_string(assembly_count).c_str(), std::to_string(compare_count).c_str()) &&
637                                         !strcmp(std::to_string(assembly_count).c_str(), sha256_count.c_str())) {
638                                         _INFO("Same : %s", tac_name.c_str());
639                                         if (createSymlink(tac_version_dir, np)) {
640                                                 return 0;
641                                         }
642
643                                         char *error = 0;
644                                         int count = 0;
645                                         std::string sql = "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = '" + std::string(pkgId) + "' AND NAME = '" + tac_name + "';";
646                                         int ret = sqlite3_exec(tac_db, sql.c_str(), sqliteCb, &count, &error);
647                                         if (ret != SQLITE_OK) {
648                                                 _ERR("SQL error: %s", error);
649                                                 sqlite3_free(error);
650                                         }
651
652                                         if (count == 1) {
653                                                 sql = "UPDATE TAC SET NAME = '" + tac_name + "', VERSION = '" + tac_version + "', NUGET = '" + np + "' WHERE PKGID = '" + std::string(pkgId) + "' AND NAME = '" + tac_name + "';";
654                                                 dbUpdate(tac_db, tacAppListDB, sql);
655                                         } else {
656                                                 sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
657                                                         "VALUES ('" + std::string(pkgId) + "', '" + np + "', '" + tac_name + "', '" + tac_version + "');";
658                                                 dbInsert(tac_db, tacAppListDB, sql);
659                                         }
660                                 } else {
661                                         _INFO("Different : %s", tac_name.c_str());
662                                 }
663                         }
664                 }
665                 for (auto& unp : updateTac) {
666                         bool isExits = false;
667                         for (auto& np : tacDB) {
668                                 if (!strcmp(unp.c_str(), np.c_str())) {
669                                         isExits = true;
670                                         break;
671                                 }
672                         }
673
674                         if (!isExits) {
675                                 std::string sql = "DELETE FROM TAC WHERE PKGID = '" + std::string(pkgId) + "' AND NUGET = '" + unp + "';";
676                                 dbDelete(tac_db, tacAppListDB, sql);
677                         }
678                 }
679
680                 if (removeOriginalAssembly()) {
681                         return 0;
682                 }
683                 if (updateTacDB(pkgId)) {
684                         return 0;
685                 }
686         }
687         return 0;
688 }
689
690 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgId, const char *appId, GList *list)
691 {
692         _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UNINSTALL =====]");
693         _INFO("PackageID : %s", pkgId);
694
695         status = "uninstall";
696         tac_db = dbCreate(tacAppListDB);
697         if (tac_db) {
698                 if (!dbOpen(tac_db, tacAppListDB)) {
699                         return 0;
700                 }
701         } else {
702                 _ERR("Sqlite create error");
703                 return 0;
704         }
705
706         std::string sql = "SELECT * FROM TAC WHERE PKGID = '" + std::string(pkgId) + "';";
707         updateTac = dbSelect(tac_db, tacAppListDB, sql);
708
709         sql = "DELETE FROM TAC WHERE PKGID = '" + std::string(pkgId) + "';";
710         dbDelete(tac_db, tacAppListDB, sql);
711
712         if (updateTacDB(pkgId)) {
713                 return 0;
714         }
715         return 0;
716 }
717
718 extern "C" int PKGMGR_MDPARSER_PLUGIN_REMOVED(const char *pkgId, const char *appId, GList *list)
719 {
720         _DBG("[===== PKGMGR_MDPARSER_PLUGIN_REMOVED =====]");
721         status = "removed";
722
723         PKGMGR_MDPARSER_PLUGIN_UPGRADE(pkgId, appId, list);
724         return 0;
725 }
726
727 int install_Clean()
728 {
729         return 0;
730 }
731
732 int unInstall_Clean()
733 {
734         for (auto& unp : updateTac) {
735                 bf::path current_tac = bf::path(mOptUsrDotnet) / unp.substr(0, unp.find('/'));
736                 std::vector<std::string> exist_directory_name;
737                 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
738                         if (bf::exists(bck.path()) && bf::is_directory(bck.path()) && strstr(bck.path().c_str(), mBackup.c_str()) != NULL) {
739                                 if (!removeAll(bck.path().string())) {
740                                         _ERR("Failed to remove of %s", bck.path().c_str());
741                                         return 0;
742                                 }
743                                 break;
744                         }
745                 }
746                 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
747                         if (bf::exists(bck.path()) && bf::is_directory(bck.path()) && strstr(bck.path().c_str(), mBackup.c_str()) == NULL) {
748                                 exist_directory_name.push_back(bck.path().string());
749                                 break;
750                         }
751                 }
752                 if (exist_directory_name.empty()) {
753                         if (!removeAll(current_tac)) {
754                                 _ERR("Failed to remove of %s", current_tac.c_str());
755                                 return 0;
756                         }
757                 } else {
758                         exist_directory_name.clear();
759                 }
760         }
761         return 0;
762 }
763
764 int update_Clean()
765 {
766         if (!tacDB.empty()) {
767                 for (auto& np : tacDB) {
768                         bf::path current_tac = bf::path(mOptUsrDotnet) / np.substr(0, np.find('/'));
769                         std::vector<std::string> exist_directory_name;
770                         for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
771                                 if (bf::exists(bck.path()) && bf::is_directory(bck.path()) && strstr(bck.path().c_str(), mBackup.c_str()) != NULL) {
772                                         if (!removeAll(bck.path().string())) {
773                                                 _ERR("Failed to remove of %s", bck.path().c_str());
774                                                 return 0;
775                                         }
776                                         break;
777                                 }
778                         }
779                         for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
780                                 if (bf::exists(bck.path()) && bf::is_directory(bck.path()) && strstr(bck.path().c_str(), mBackup.c_str()) == NULL) {
781                                         exist_directory_name.push_back(bck.path().string());
782                                         break;
783                                 }
784                         }
785                         if (exist_directory_name.empty()) {
786                                 if (!removeAll(current_tac)) {
787                                         _ERR("Failed to remove of %s", current_tac.c_str());
788                                         return 0;
789                                 }
790                         } else {
791                                 exist_directory_name.clear();
792                         }
793                 }
794         }
795         unInstall_Clean();
796         return 0;
797 }
798
799 extern "C" int PKGMGR_MDPARSER_PLUGIN_CLEAN(const char *pkgId, const char *appId, GList *list)
800 {
801         _DBG("[===== PKGMGR_MDPARSER_PLUGIN_CLEAN =====]");
802
803         if (tac_db) {
804                 dbClose(tac_db);
805                 tac_db = NULL;
806         }
807         if (!strcmp("install", status.c_str())) {
808                 install_Clean();
809         } else if (!strcmp("update", status.c_str())) {
810                 update_Clean();
811         } else if (!strcmp("uninstall", status.c_str())) {
812                 unInstall_Clean();
813         }
814         return 0;
815 }
816
817 int install_Undo()
818 {
819         for (auto& cd : createDirectories) {
820                 if (!removeAll(cd)) {
821                         _ERR("Failed to remove of %s", cd.c_str());
822                         return 0;
823                 }
824         }
825         return 0;
826 }
827
828 int unInstall_Undo()
829 {
830         for (auto& unp : updateTac) {
831                 for (auto& bck : bf::recursive_directory_iterator(bf::path(mOptUsrDotnet) / unp.substr(0, unp.find('/')))) {
832                         if (bf::exists(bck.path()) && bf::is_directory(bck.path()) && strstr(bck.path().c_str(), mBackup.c_str()) != NULL) {
833                                 if (!moveFile(bck.path(), bck.path().string().substr(0, bck.path().string().rfind(mBackup.c_str())))) {
834                                         _ERR("Failed to move %s to %s",
835                                                 bck.path().c_str(), bck.path().string().substr(0, bck.path().string().rfind(mBackup.c_str())).c_str());
836                                         return 0;
837                                 }
838                                 break;
839                         }
840                 }
841         }
842         return 0;
843 }
844
845 int update_Undo()
846 {
847         for (auto& cd : createDirectories) {
848                 if (!removeAll(cd)) {
849                         _ERR("Failed to remove of %s", cd.c_str());
850                         return 0;
851                 }
852         }
853         if (!tacDB.empty()) {
854                 for (auto& np : tacDB) {
855                         for (auto& bck : bf::recursive_directory_iterator(bf::path(mOptUsrDotnet) / np.substr(0, np.find('/')))) {
856                                 if (bf::exists(bck.path()) && bf::is_directory(bck.path()) && strstr(bck.path().c_str(), mBackup.c_str()) != NULL) {
857                                         if (!moveFile(bck.path(), bck.path().string().substr(0, bck.path().string().rfind(mBackup.c_str())))) {
858                                                 _ERR("Failed to move %s to %s",
859                                                         bck.path().c_str(), bck.path().string().substr(0, bck.path().string().rfind(mBackup.c_str())).c_str());
860                                                 return 0;
861                                         }
862                                         break;
863                                 }
864                         }
865                 }
866         }
867         unInstall_Undo();
868         return 0;
869 }
870
871 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNDO(const char *pkgId, const char *appId, GList *list)
872 {
873         _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UNDO =====]");
874
875         if (tac_db) {
876                 dbRollback(tac_db);
877                 tac_db = NULL;
878         }
879         if (!strcmp("install", status.c_str())) {
880                 install_Undo();
881         } else if (!strcmp("update", status.c_str())) {
882                 update_Undo();
883         } else if (!strcmp("uninstall", status.c_str())) {
884                 unInstall_Undo();
885         }
886         return 0;
887 }