8ae9e00aae9ddc15b73fa3847dd125ff1af27c3a
[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 "utils.h"
19 #include "db_manager.h"
20 #include "path_manager.h"
21 #include "plugin_manager.h"
22 #include "tac_common.h"
23
24 #include <cstring>
25 #include <fstream>
26 #include <sstream>
27 #include <vector>
28 #include <boost/filesystem.hpp>
29 #include <glib.h>
30 #include <json/json.h>
31 #include <pkgmgr-info.h>
32 #include <pkgmgr_installer_info.h>
33 #include <openssl/sha.h>
34
35 #ifdef  LOG_TAG
36 #undef  LOG_TAG
37 #endif
38 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
39
40 #define __XSTR(x) #x
41 #define __STR(x) __XSTR(x)
42 static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
43 #undef __STR
44 #undef __XSTR
45
46 typedef struct Metadata {
47         const char *key;
48         const char *value;
49 } Metadata;
50
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 = "";
56 std::string rootPath;
57 std::string execName;
58 std::string binPath;
59 static sqlite3 *tac_db = NULL;
60 bool tacPluginInstalled = false;
61 bool tacPluginFinished = false;
62
63 bool metadataCheck(GList *list)
64 {
65         GList *tag = NULL;
66         Metadata *mdInfo = NULL;
67         tag = g_list_first(list);
68         mdInfo = (Metadata*)tag->data;
69         if (strcmp(mdInfo->key, TAC_METADATA_KEY) == 0 && strcmp(mdInfo->value, METADATA_VALUE) == 0) {
70                 _DBG("Prefer nuget cache set TRUE");
71                 if (initializePluginManager("normal")) {
72                         _ERR("Fail to initialize PluginManager");
73                         return false;
74                 }
75                 if (initializePathManager(std::string(), std::string(), std::string())) {
76                         _ERR("Fail to initialize PathManger");
77                         return false;
78                 }
79                 return true;
80         }
81         return false;
82 }
83
84 bool appTypeCheck(std::string pkgId)
85 {
86         uid_t uid = 0;
87         if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
88                 _ERR("Failed to get UID");
89                 return false;
90         }
91
92         pkgmgrinfo_pkginfo_h handle;
93         int ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
94         if (ret != PMINFO_R_OK) {
95                 _ERR("Failed to get pkg info");
96                 return false;
97         }
98
99         bool isDotnetAppType = false;
100         auto dotnetAppCounter = [] (pkgmgrinfo_appinfo_h handle, void *userData) -> int {
101                 char* type = nullptr;
102                 bool* dotnet = static_cast<bool*>(userData);
103                 if (pkgmgrinfo_appinfo_get_apptype(handle, &type) != PMINFO_R_OK) {
104                         _ERR("Failed to get app type : %s", type);
105                         return -1;
106                 }
107                 if (strcmp(type, "dotnet") == 0) {
108                         *dotnet = true;
109                 }
110                 return 0;
111         };
112
113         if (pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP, dotnetAppCounter, &isDotnetAppType, uid) != PMINFO_R_OK) {
114                 _ERR("Failed to get list of app in pkg : %s", pkgId.c_str());
115                 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
116                 return false;
117         }
118
119         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
120         return isDotnetAppType;
121 }
122
123 void SHA256(std::string path, char outputBuffer[65])
124 {
125         FILE *file = fopen(path.c_str(), "rb");
126         if (!file) {
127                 return;
128         }
129
130         unsigned char hash[SHA256_DIGEST_LENGTH];
131         SHA256_CTX sha256;
132         SHA256_Init(&sha256);
133         int bytesRead = 0;
134         const int bufSize = 32768;
135         char *buffer = (char*)malloc(bufSize);
136         if (!buffer) {
137                 fclose(file);
138                 return;
139         }
140
141         while ((bytesRead = fread(buffer, 1, bufSize, file))) {
142                 SHA256_Update(&sha256, buffer, bytesRead);
143         }
144         SHA256_Final(hash, &sha256);
145         for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
146                 snprintf(outputBuffer + (i * 2), 3, "%02x", hash[i]);
147         }
148         outputBuffer[64] = 0;
149
150         fclose(file);
151         free(buffer);
152 }
153
154 void createSHA256Info(std::string sha256Info, std::string nugetPackage)
155 {
156         std::ofstream ofs(sha256Info, std::ios::app);
157         int assembly_count = 0;
158         for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
159                 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
160                 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
161                 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
162                 std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
163                 if (!strcmp(nuget_package.c_str(), nugetPackage.c_str())) {
164                         ofs << assembly << ":" << sha << std::endl;
165                         assembly_count++;
166                 }
167         }
168         ofs << assembly_count << std::endl;
169         ofs.close();
170 }
171
172 int compareSHA256Info(std::string sha256Info, std::string nugetPackage)
173 {
174         int compare_count = 0;
175         int assembly_count = 0;
176         std::string sha256_count = "0";
177         for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
178                 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
179                 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
180                 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
181                 std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
182                 if (!strcmp(nuget_package.c_str(), nugetPackage.c_str())) {
183                         assembly_count++;
184                         std::ifstream ifs(sha256Info);
185                         std::string get_str;
186                         if (ifs.is_open()) {
187                                 while (getline(ifs, get_str)) {
188                                         if (!strcmp(get_str.c_str(), (assembly + ":" + sha).c_str())) {
189                                                 compare_count++;
190                                         }
191                                         sha256_count = get_str;
192                                 }
193                                 ifs.close();
194                         }
195                 }
196         }
197         if (!strcmp(std::to_string(assembly_count).c_str(), std::to_string(compare_count).c_str()) &&
198                 !strcmp(std::to_string(assembly_count).c_str(), sha256_count.c_str())) {
199                 _INFO("Same nuget : %s", nugetPackage.c_str());
200                 return 1;
201         }
202         return 0;
203 }
204
205 int copyNCreateSymlink(std::string tacVersionDir, std::string nugetPackage, bool isCreateTacDir)
206 {
207         uid_t g_uid = 0;
208         gid_t g_gid = 0;
209         if (pkgmgr_installer_info_get_target_uid(&g_uid) < 0) {
210                 _ERR("Failed to get UID");
211                 return -1;
212         }
213
214         std::string tac_dir = concatPath(binPath, TAC_SYMLINK_SUB_DIR);
215         if (!createDir(tac_dir)) {
216                 _ERR("Cannot create directory: %s", tac_dir.c_str());
217                 return -1;
218         }
219
220         for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
221                 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
222                 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
223                 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
224                 if (!strcmp(nuget_package.c_str(), nugetPackage.c_str())) {
225                         if (bf::exists(concatPath(binPath, assembly))) {
226                                 if (isCreateTacDir) {
227                                         if (!copyFile(concatPath(binPath, assembly), concatPath(tacVersionDir, assembly))) {
228                                                 _ERR("Failed to copy of %s", assembly.c_str());
229                                                 return -1;
230                                         }
231                                 }
232                                 bf::create_symlink(concatPath(tacVersionDir, assembly), concatPath(tac_dir, assembly));
233                                 if (lchown(concatPath(tac_dir, assembly).c_str(), g_uid, g_gid)) {
234                                         _ERR("Failed to change owner of: %s", concatPath(tac_dir, assembly).c_str());
235                                         return -1;
236                                 }
237                                 if (!removeFile(concatPath(binPath, assembly))) {
238                                         _ERR("Failed to remove of %s", assembly.c_str());
239                                         return -1;
240                                 }
241                         }
242                 }
243         }
244         return 0;
245 }
246
247 void depsJsonCheck() {
248         for (auto& npAssembly : depsJsonParser(rootPath, execName, getTPA())) {
249                 std::string nuget_package = npAssembly.substr(0, npAssembly.rfind(':'));
250                 std::string assembly_name = npAssembly.substr(npAssembly.rfind(':') + 1);
251                 tacDB.push_back(nuget_package);
252                 char buffer[65] = {0};
253                 SHA256(concatPath(binPath, assembly_name), buffer);
254                 nugetPackagesAssembliesSha.push_back(nuget_package + ":" + assembly_name + ":" + buffer);
255                 _INFO("Assembly : [%s] / SHA256 : [%s]", assembly_name.c_str(), buffer);
256         }
257         std::sort(tacDB.begin(), tacDB.end());
258         tacDB.erase(unique(tacDB.begin(), tacDB.end()), tacDB.end());
259 }
260
261 extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *appId, GList *list)
262 {
263         _DBG("[===== PKGMGR_MDPARSER_PLUGIN_INSTALL =====]");
264         _INFO("PackageID : %s", pkgId);
265
266         // Can be multiple apps in one package
267         if (tacPluginInstalled) {
268                 _INFO("TAC plugin already installed");
269                 return 0;
270         }
271         tacPluginInstalled = true;
272
273         if (!appTypeCheck(std::string(pkgId))) {
274                 _INFO("App type is not dotnet");
275                 return 0;
276         }
277         if (getExecName(std::string(pkgId), execName) < 0) {
278                 return 0;
279         }
280         if (getRootPath(std::string(pkgId), rootPath) < 0) {
281                 return 0;
282         } else {
283                 binPath = concatPath(rootPath, "bin");
284         }
285         if (metadataCheck(list)) {
286                 depsJsonCheck();
287         }
288
289         status = "install";
290         tac_db = dbCreate(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE);
291         if (!tac_db) {
292                 _ERR("Sqlite create error");
293                 return 0;
294         }
295
296         if (tacDB.empty()) {
297                 _INFO("Not exist data for TAC in %s", pkgId);
298                 return 0;
299         }
300
301         for (auto& np : tacDB) {
302                 std::string tac_name = np.substr(0, np.find('/'));
303                 std::string tac_version = np.substr(np.rfind('/') + 1);
304                 _INFO("TAC name : %s", tac_name.c_str());
305                 _INFO("TAC version : %s", tac_version.c_str());
306
307                 std::string tac_version_dir = concatPath(__DOTNET_DIR, np);
308                 std::string sha256_info = concatPath(tac_version_dir, TAC_SHA_256_INFO);
309                 if (!bf::exists(tac_version_dir)) {
310                         _INFO("Create tac_version_dir [%s]", tac_version_dir.c_str());
311                         if (!createDir(tac_version_dir)) {
312                                 _ERR("Cannot create directory: %s", tac_version_dir.c_str());
313                                 return 0;
314                         }
315                         createDirectories.push_back(tac_version_dir);
316                         if (!bf::is_symlink(sha256_info)) {
317                                 createSHA256Info(sha256_info, np);
318                         } else {
319                                 _ERR("Failed to create sha256_info. Symbolic link is detected");
320                                 return -1;
321                         }
322
323                         if (copyNCreateSymlink(tac_version_dir, np, true) < 0) {
324                                 _ERR("Failed to create symlink");
325                                 return -1;
326                         }
327
328                         char *sql = sqlite3_mprintf(
329                                 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
330                                 "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
331                         dbInsert(tac_db, TAC_APP_LIST_DB, sql);
332                         sqlite3_free(sql);
333                 } else {
334                         _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str());
335                         if (!bf::is_symlink(sha256_info)) {
336                                 if (compareSHA256Info(sha256_info, np)) {
337                                         if (copyNCreateSymlink(tac_version_dir, np, false) < 0) {
338                                                 _ERR("Failed to create symlink");
339                                                 return -1;
340                                         }
341
342                                         char *sql = sqlite3_mprintf(
343                                                 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
344                                                 "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
345                                         dbInsert(tac_db, TAC_APP_LIST_DB, sql);
346                                         sqlite3_free(sql);
347                                 } else {
348                                         _INFO("Different nuget : %s", np.c_str());
349                                 }
350                         } else {
351                                 _ERR("Failed to create sha256_info. Symbolic link is detected");
352                                 return -1;
353                         }
354                 }
355                 if (!bf::exists(sha256_info)) {
356                         if(!removeAll(tac_version_dir)) {
357                                 _ERR("Failed to remove of %s", tac_version_dir.c_str());
358                         }
359                 }
360         }
361         return 0;
362 }
363
364 static int sqliteCb(void *count, int argc, char **argv, char **colName) {
365         int *c = (int*)count;
366         *c = atoi(argv[0]);
367         return 0;
368 }
369
370 int updateTacDB(sqlite3 *sqlite)
371 {
372         for (auto& unp : updateTac) {
373                 int count = -1;
374                 char *sql = sqlite3_mprintf("SELECT COUNT(NUGET) FROM TAC WHERE NUGET = %Q;", unp.c_str());
375                 int ret = sqlite3_exec(sqlite, sql, sqliteCb, &count, NULL);
376                 if (ret != SQLITE_OK) {
377                         _ERR("Sqlite select error");
378                         sqlite3_free(sql);
379                         return -1;
380                 }
381                 if (count == 0) {
382                         std::string tac_version_dir_prev = concatPath(__DOTNET_DIR, unp);
383                         std::string tac_version_dir_backup = tac_version_dir_prev + ".bck";
384                         if (!copyDir(tac_version_dir_prev, tac_version_dir_backup)) {
385                                 _ERR("Failed to copy of %s to %s", tac_version_dir_prev.c_str(), tac_version_dir_backup.c_str());
386                                 sqlite3_free(sql);
387                                 return -1;
388                         }
389                         if (!removeAll(tac_version_dir_prev)) {
390                                 _ERR("Failed to remove of %s", tac_version_dir_prev.c_str());
391                                 sqlite3_free(sql);
392                                 return -1;
393                         }
394                 }
395                 sqlite3_free(sql);
396         }
397         return 0;
398 }
399
400 extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *appId, GList *list)
401 {
402         _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UPGRADE =====]");
403         _INFO("PackageID : %s", pkgId);
404
405         // Can be multiple apps in one package
406         if (tacPluginInstalled) {
407                 _INFO("TAC plugin already upgraded");
408                 return 0;
409         }
410         tacPluginInstalled = true;
411
412         if (!appTypeCheck(std::string(pkgId))) {
413                 _INFO("App type is not dotnet");
414                 return 0;
415         }
416         if (getExecName(std::string(pkgId), execName) < 0) {
417                 return 0;
418         }
419         if (getRootPath(std::string(pkgId), rootPath) < 0) {
420                 return 0;
421         } else {
422                 binPath = concatPath(rootPath, "bin");
423         }
424         if (!strcmp("removed", status.c_str())) {
425                 _INFO("Skipped to parse of deps.json");
426         } else {
427                 if (metadataCheck(list)) {
428                         depsJsonCheck();
429                 }
430         }
431
432         status = "update";
433         tac_db = dbCreate(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE);
434         if (!tac_db) {
435                 _ERR("Sqlite open error");
436                 return 0;
437         }
438
439         char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
440         updateTac = dbSelect(tac_db, TAC_APP_LIST_DB, sql);
441         sqlite3_free(sql);
442
443         if (tacDB.empty()) {
444                 sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q;", pkgId);
445                 dbDelete(tac_db, TAC_APP_LIST_DB, sql);
446                 sqlite3_free(sql);
447                 if (updateTacDB(tac_db) < 0) {
448                         return -1;
449                 }
450                 _INFO("Not exist data for TAC in %s", pkgId);
451                 return 0;
452         } else {
453                 for (auto& np : tacDB) {
454                         std::string tac_name = np.substr(0, np.find('/'));
455                         std::string tac_version = np.substr(np.rfind('/') + 1);
456                         _INFO("TAC name : %s", tac_name.c_str());
457                         _INFO("TAC version : %s", tac_version.c_str());
458
459                         std::string tac_version_dir = concatPath(__DOTNET_DIR, np);
460                         std::string sha256_info = concatPath(tac_version_dir, TAC_SHA_256_INFO);
461                         if (!bf::exists(tac_version_dir)) {
462                                 _INFO("Create tac_version_dir [%s]", tac_version_dir.c_str());
463                                 if (!createDir(tac_version_dir)) {
464                                         _ERR("Cannot create directory: %s", tac_version_dir.c_str());
465                                         return 0;
466                                 }
467                                 createDirectories.push_back(tac_version_dir);
468                                 if (!bf::is_symlink(sha256_info)) {
469                                         createSHA256Info(sha256_info, np);
470                                 } else {
471                                         _ERR("Failed to create sha256_info. Symbolic link is detected");
472                                         return -1;
473                                 }
474
475                                 if (copyNCreateSymlink(tac_version_dir, np, true) < 0) {
476                                         _ERR("Failed to create symlink");
477                                         return -1;
478                                 }
479
480                                 int count = -1;
481                                 sql = sqlite3_mprintf(
482                                                 "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = %Q AND NAME = %Q;", pkgId, tac_name.c_str());
483                                 int ret = sqlite3_exec(tac_db, sql, sqliteCb, &count, NULL);
484                                 if (ret != SQLITE_OK) {
485                                         _ERR("Sqlite select error");
486                                         sqlite3_free(sql);
487                                         return -1;
488                                 }
489                                 sqlite3_free(sql);
490                                 if (count == 1) {
491                                         sql = sqlite3_mprintf(
492                                                 "UPDATE TAC SET NAME = %Q, VERSION = %Q, NUGET = %Q WHERE PKGID = %Q AND NAME = %Q;",
493                                                 tac_name.c_str(), tac_version.c_str(), np.c_str(), pkgId, tac_name.c_str());
494                                         dbUpdate(tac_db, TAC_APP_LIST_DB, sql);
495                                         sqlite3_free(sql);
496                                 } else if (count == 0) {
497                                         sql = sqlite3_mprintf(
498                                                 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
499                                                 "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
500                                         dbInsert(tac_db, TAC_APP_LIST_DB, sql);
501                                         sqlite3_free(sql);
502                                 }
503                         } else {
504                                 _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str());
505                                 if (!bf::is_symlink(sha256_info)) {
506                                         if (compareSHA256Info(sha256_info, np)) {
507                                                 if (copyNCreateSymlink(tac_version_dir, np, false) < 0) {
508                                                         _ERR("Failed to create symlink");
509                                                         return -1;
510                                                 }
511
512                                                 int count = -1;
513                                                 char *sql = sqlite3_mprintf(
514                                                         "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = %Q AND NAME = %Q;", pkgId, tac_name.c_str());
515                                                 int ret = sqlite3_exec(tac_db, sql, sqliteCb, &count, NULL);
516                                                 if (ret != SQLITE_OK) {
517                                                         _ERR("Sqlite select error");
518                                                         sqlite3_free(sql);
519                                                         return -1;
520                                                 }
521                                                 sqlite3_free(sql);
522                                                 if (count == 1) {
523                                                         sql = sqlite3_mprintf(
524                                                                 "UPDATE TAC SET NAME = %Q, VERSION = %Q, NUGET = %Q WHERE PKGID = %Q AND NAME = %Q;",
525                                                                 tac_name.c_str(), tac_version.c_str(), np.c_str(), pkgId, tac_name.c_str());
526                                                         dbUpdate(tac_db, TAC_APP_LIST_DB, sql);
527                                                         sqlite3_free(sql);
528                                                 } else if (count == 0) {
529                                                         sql = sqlite3_mprintf(
530                                                                 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
531                                                                 "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
532                                                         dbInsert(tac_db, TAC_APP_LIST_DB, sql);
533                                                         sqlite3_free(sql);
534                                                 }
535                                         } else {
536                                                 _INFO("Different nuget : %s", np.c_str());
537                                         }
538                                 } else {
539                                         _ERR("Failed to create sha256_info. Symbolic link is detected");
540                                         return -1;
541                                 }
542                         }
543                         if (!bf::exists(sha256_info)) {
544                                 if(!removeAll(tac_version_dir)) {
545                                         _ERR("Failed to remove of %s", tac_version_dir.c_str());
546                                 }
547                         }
548                 }
549                 for (auto& unp : updateTac) {
550                         bool isExits = false;
551                         for (auto& np : tacDB) {
552                                 if (!strcmp(unp.c_str(), np.c_str())) {
553                                         isExits = true;
554                                         break;
555                                 }
556                         }
557                         if (!isExits) {
558                                 char *sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q AND NUGET = %Q;", pkgId, unp.c_str());
559                                 dbDelete(tac_db, TAC_APP_LIST_DB, sql);
560                                 sqlite3_free(sql);
561                         }
562                 }
563                 if (updateTacDB(tac_db) < 0) {
564                         return -1;
565                 }
566         }
567         return 0;
568 }
569
570 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgId, const char *appId, GList *list)
571 {
572         _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UNINSTALL =====]");
573         _INFO("PackageID : %s", pkgId);
574
575         // Can be multiple apps in one package
576         if (tacPluginInstalled) {
577                 _INFO("TAC plugin already uninstalled");
578                 return 0;
579         }
580         tacPluginInstalled = true;
581
582         status = "uninstall";
583         tac_db = dbOpen(TAC_APP_LIST_DB);
584         if (!tac_db) {
585                 _ERR("Sqlite open error");
586                 return 0;
587         }
588
589         char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
590         updateTac = dbSelect(tac_db, TAC_APP_LIST_DB, sql);
591         sqlite3_free(sql);
592
593         sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q;", pkgId);
594         dbDelete(tac_db, TAC_APP_LIST_DB, sql);
595         sqlite3_free(sql);
596
597         if (updateTacDB(tac_db) < 0) {
598                 return -1;
599         }
600         return 0;
601 }
602
603 extern "C" int PKGMGR_MDPARSER_PLUGIN_REMOVED(const char *pkgId, const char *appId, GList *list)
604 {
605         _DBG("[===== PKGMGR_MDPARSER_PLUGIN_REMOVED =====]");
606         _INFO("PackageID : %s", pkgId);
607
608         status = "removed";
609
610         return PKGMGR_MDPARSER_PLUGIN_UPGRADE(pkgId, appId, list);
611 }
612
613 void cleanStep(std::string tac)
614 {
615         std::string current_tac = concatPath(__DOTNET_DIR, tac.substr(0, tac.find('/')));
616         try {
617                 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
618                         std::string bck_path = bck.path().string();
619                         if (bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") != NULL) {
620                                 if (!removeAll(bck_path)) {
621                                         _ERR("Failed to remove of %s", bck_path.c_str());
622                                 }
623                                 break;
624                         }
625                 }
626
627                 bool isExist = false;
628                 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
629                         std::string bck_path = bck.path().string();
630                         if (bf::exists(bck_path) && bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") == NULL) {
631                                 isExist = true;
632                                 break;
633                         }
634                 }
635                 if (!isExist) {
636                         if (!removeAll(current_tac)) {
637                                 _ERR("Failed to remove of %s", current_tac.c_str());
638                         }
639                 }
640         } catch (const bf::filesystem_error& error) {
641                 _ERR("Failed to recursive directory: %s", error.what());
642                 return;
643         }
644 }
645
646 void install_Clean()
647 {
648         return;
649 }
650
651 void unInstall_Clean()
652 {
653         for (auto& unp : updateTac) {
654                 cleanStep(unp);
655         }
656 }
657
658 void update_Clean()
659 {
660         if (!tacDB.empty()) {
661                 for (auto& np : tacDB) {
662                         cleanStep(np);
663                 }
664         }
665         unInstall_Clean();
666 }
667
668 extern "C" int PKGMGR_MDPARSER_PLUGIN_CLEAN(const char *pkgId, const char *appId, GList *list)
669 {
670         _DBG("[===== PKGMGR_MDPARSER_PLUGIN_CLEAN =====]");
671         _INFO("PackageID : %s", pkgId);
672
673         // Can be multiple apps in one package
674         if (tacPluginFinished) {
675                 _INFO("TAC plugin already finished(CLEAN)");
676                 return 0;
677         }
678         tacPluginFinished = true;
679
680         if (tac_db) {
681                 dbClose(tac_db);
682                 tac_db = NULL;
683         }
684         if (!strcmp("install", status.c_str())) {
685                 install_Clean();
686         } else if (!strcmp("update", status.c_str())) {
687                 update_Clean();
688         } else if (!strcmp("uninstall", status.c_str())) {
689                 unInstall_Clean();
690         }
691         return 0;
692 }
693
694 void undoStep(std::string tac)
695 {
696         std::string current_tac = concatPath(__DOTNET_DIR, tac.substr(0, tac.find('/')));
697         try {
698                 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
699                         std::string bck_path = bck.path().string();
700                         if (bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") != NULL) {
701                                 if (!moveFile(bck_path, bck_path.substr(0, bck_path.rfind(".bck")))) {
702                                         _ERR("Failed to move %s", bck_path.c_str());
703                                 }
704                                 break;
705                         }
706                 }
707         } catch (const bf::filesystem_error& error) {
708                 _ERR("Failed to recursive directory: %s", error.what());
709                 return;
710         }
711 }
712
713 void install_Undo()
714 {
715         for (auto& cd : createDirectories) {
716                 if (!removeAll(cd)) {
717                         _ERR("Failed to remove of %s", cd.c_str());
718                 }
719         }
720 }
721
722 void unInstall_Undo()
723 {
724         for (auto& unp : updateTac) {
725                 undoStep(unp);
726         }
727 }
728
729 void update_Undo()
730 {
731         install_Undo();
732         if (!tacDB.empty()) {
733                 for (auto& np : tacDB) {
734                         undoStep(np);
735                 }
736         }
737         unInstall_Undo();
738 }
739
740 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNDO(const char *pkgId, const char *appId, GList *list)
741 {
742         _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UNDO =====]");
743         _INFO("PackageID : %s", pkgId);
744
745         // Can be multiple apps in one package
746         if (tacPluginFinished) {
747                 _INFO("TAC plugin already finished(UNDO)");
748                 return 0;
749         }
750         tacPluginFinished = true;
751
752         if (tac_db) {
753                 dbRollback(tac_db);
754                 tac_db = NULL;
755         }
756         if (!strcmp("install", status.c_str())) {
757                 install_Undo();
758         } else if (!strcmp("update", status.c_str())) {
759                 update_Undo();
760         } else if (!strcmp("uninstall", status.c_str())) {
761                 unInstall_Undo();
762         }
763         return 0;
764 }