Add flags to the install plugin to avoid duplicate execution
[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 .deps.json file");
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         } else {
451                 for (auto& np : tacDB) {
452                         std::string tac_name = np.substr(0, np.find('/'));
453                         std::string tac_version = np.substr(np.rfind('/') + 1);
454                         _INFO("TAC name : %s", tac_name.c_str());
455                         _INFO("TAC version : %s", tac_version.c_str());
456
457                         std::string tac_version_dir = concatPath(__DOTNET_DIR, np);
458                         std::string sha256_info = concatPath(tac_version_dir, TAC_SHA_256_INFO);
459                         if (!bf::exists(tac_version_dir)) {
460                                 _INFO("Create tac_version_dir [%s]", tac_version_dir.c_str());
461                                 if (!createDir(tac_version_dir)) {
462                                         _ERR("Cannot create directory: %s", tac_version_dir.c_str());
463                                         return 0;
464                                 }
465                                 createDirectories.push_back(tac_version_dir);
466                                 if (!bf::is_symlink(sha256_info)) {
467                                         createSHA256Info(sha256_info, np);
468                                 } else {
469                                         _ERR("Failed to create sha256_info. Symbolic link is detected");
470                                         return -1;
471                                 }
472
473                                 if (copyNCreateSymlink(tac_version_dir, np, true) < 0) {
474                                         _ERR("Failed to create symlink");
475                                         return -1;
476                                 }
477
478                                 int count = -1;
479                                 sql = sqlite3_mprintf(
480                                                 "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = %Q AND NAME = %Q;", pkgId, tac_name.c_str());
481                                 int ret = sqlite3_exec(tac_db, sql, sqliteCb, &count, NULL);
482                                 if (ret != SQLITE_OK) {
483                                         _ERR("Sqlite select error");
484                                         sqlite3_free(sql);
485                                         return -1;
486                                 }
487                                 sqlite3_free(sql);
488                                 if (count == 1) {
489                                         sql = sqlite3_mprintf(
490                                                 "UPDATE TAC SET NAME = %Q, VERSION = %Q, NUGET = %Q WHERE PKGID = %Q AND NAME = %Q;",
491                                                 tac_name.c_str(), tac_version.c_str(), np.c_str(), pkgId, tac_name.c_str());
492                                         dbUpdate(tac_db, TAC_APP_LIST_DB, sql);
493                                         sqlite3_free(sql);
494                                 } else if (count == 0) {
495                                         sql = sqlite3_mprintf(
496                                                 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
497                                                 "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
498                                         dbInsert(tac_db, TAC_APP_LIST_DB, sql);
499                                         sqlite3_free(sql);
500                                 }
501                         } else {
502                                 _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str());
503                                 if (!bf::is_symlink(sha256_info)) {
504                                         if (compareSHA256Info(sha256_info, np)) {
505                                                 if (copyNCreateSymlink(tac_version_dir, np, false) < 0) {
506                                                         _ERR("Failed to create symlink");
507                                                         return -1;
508                                                 }
509
510                                                 int count = -1;
511                                                 char *sql = sqlite3_mprintf(
512                                                         "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = %Q AND NAME = %Q;", pkgId, tac_name.c_str());
513                                                 int ret = sqlite3_exec(tac_db, sql, sqliteCb, &count, NULL);
514                                                 if (ret != SQLITE_OK) {
515                                                         _ERR("Sqlite select error");
516                                                         sqlite3_free(sql);
517                                                         return -1;
518                                                 }
519                                                 sqlite3_free(sql);
520                                                 if (count == 1) {
521                                                         sql = sqlite3_mprintf(
522                                                                 "UPDATE TAC SET NAME = %Q, VERSION = %Q, NUGET = %Q WHERE PKGID = %Q AND NAME = %Q;",
523                                                                 tac_name.c_str(), tac_version.c_str(), np.c_str(), pkgId, tac_name.c_str());
524                                                         dbUpdate(tac_db, TAC_APP_LIST_DB, sql);
525                                                         sqlite3_free(sql);
526                                                 } else if (count == 0) {
527                                                         sql = sqlite3_mprintf(
528                                                                 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
529                                                                 "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
530                                                         dbInsert(tac_db, TAC_APP_LIST_DB, sql);
531                                                         sqlite3_free(sql);
532                                                 }
533                                         } else {
534                                                 _INFO("Different nuget : %s", np.c_str());
535                                         }
536                                 } else {
537                                         _ERR("Failed to create sha256_info. Symbolic link is detected");
538                                         return -1;
539                                 }
540                         }
541                         if (!bf::exists(sha256_info)) {
542                                 if(!removeAll(tac_version_dir)) {
543                                         _ERR("Failed to remove of %s", tac_version_dir.c_str());
544                                 }
545                         }
546                 }
547                 for (auto& unp : updateTac) {
548                         bool isExits = false;
549                         for (auto& np : tacDB) {
550                                 if (!strcmp(unp.c_str(), np.c_str())) {
551                                         isExits = true;
552                                         break;
553                                 }
554                         }
555                         if (!isExits) {
556                                 char *sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q AND NUGET = %Q;", pkgId, unp.c_str());
557                                 dbDelete(tac_db, TAC_APP_LIST_DB, sql);
558                                 sqlite3_free(sql);
559                         }
560                 }
561                 if (updateTacDB(tac_db) < 0) {
562                         return -1;
563                 }
564         }
565         return 0;
566 }
567
568 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgId, const char *appId, GList *list)
569 {
570         _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UNINSTALL =====]");
571         _INFO("PackageID : %s", pkgId);
572
573         // Can be multiple apps in one package
574         if (tacPluginInstalled) {
575                 _INFO("TAC plugin already uninstalled");
576                 return 0;
577         }
578         tacPluginInstalled = true;
579
580         status = "uninstall";
581         tac_db = dbOpen(TAC_APP_LIST_DB);
582         if (!tac_db) {
583                 _ERR("Sqlite open error");
584                 return 0;
585         }
586
587         char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
588         updateTac = dbSelect(tac_db, TAC_APP_LIST_DB, sql);
589         sqlite3_free(sql);
590
591         sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q;", pkgId);
592         dbDelete(tac_db, TAC_APP_LIST_DB, sql);
593         sqlite3_free(sql);
594
595         if (updateTacDB(tac_db) < 0) {
596                 return -1;
597         }
598         return 0;
599 }
600
601 extern "C" int PKGMGR_MDPARSER_PLUGIN_REMOVED(const char *pkgId, const char *appId, GList *list)
602 {
603         _DBG("[===== PKGMGR_MDPARSER_PLUGIN_REMOVED =====]");
604         _INFO("PackageID : %s", pkgId);
605
606         status = "removed";
607
608         return PKGMGR_MDPARSER_PLUGIN_UPGRADE(pkgId, appId, list);
609 }
610
611 void cleanStep(std::string tac)
612 {
613         std::string current_tac = concatPath(__DOTNET_DIR, tac.substr(0, tac.find('/')));
614         try {
615                 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
616                         std::string bck_path = bck.path().string();
617                         if (bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") != NULL) {
618                                 if (!removeAll(bck_path)) {
619                                         _ERR("Failed to remove of %s", bck_path.c_str());
620                                 }
621                                 break;
622                         }
623                 }
624
625                 bool isExist = false;
626                 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
627                         std::string bck_path = bck.path().string();
628                         if (bf::exists(bck_path) && bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") == NULL) {
629                                 isExist = true;
630                                 break;
631                         }
632                 }
633                 if (!isExist) {
634                         if (!removeAll(current_tac)) {
635                                 _ERR("Failed to remove of %s", current_tac.c_str());
636                         }
637                 }
638         } catch (const bf::filesystem_error& error) {
639                 _ERR("Failed to recursive directory: %s", error.what());
640                 return;
641         }
642 }
643
644 void install_Clean()
645 {
646         return;
647 }
648
649 void unInstall_Clean()
650 {
651         for (auto& unp : updateTac) {
652                 cleanStep(unp);
653         }
654 }
655
656 void update_Clean()
657 {
658         if (!tacDB.empty()) {
659                 for (auto& np : tacDB) {
660                         cleanStep(np);
661                 }
662         }
663         unInstall_Clean();
664 }
665
666 extern "C" int PKGMGR_MDPARSER_PLUGIN_CLEAN(const char *pkgId, const char *appId, GList *list)
667 {
668         _DBG("[===== PKGMGR_MDPARSER_PLUGIN_CLEAN =====]");
669         _INFO("PackageID : %s", pkgId);
670
671         // Can be multiple apps in one package
672         if (tacPluginFinished) {
673                 _INFO("TAC plugin already finished(CLEAN)");
674                 return 0;
675         }
676         tacPluginFinished = true;
677
678         if (tac_db) {
679                 dbClose(tac_db);
680                 tac_db = NULL;
681         }
682         if (!strcmp("install", status.c_str())) {
683                 install_Clean();
684         } else if (!strcmp("update", status.c_str())) {
685                 update_Clean();
686         } else if (!strcmp("uninstall", status.c_str())) {
687                 unInstall_Clean();
688         }
689         return 0;
690 }
691
692 void undoStep(std::string tac)
693 {
694         std::string current_tac = concatPath(__DOTNET_DIR, tac.substr(0, tac.find('/')));
695         try {
696                 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
697                         std::string bck_path = bck.path().string();
698                         if (bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") != NULL) {
699                                 if (!moveFile(bck_path, bck_path.substr(0, bck_path.rfind(".bck")))) {
700                                         _ERR("Failed to move %s", bck_path.c_str());
701                                 }
702                                 break;
703                         }
704                 }
705         } catch (const bf::filesystem_error& error) {
706                 _ERR("Failed to recursive directory: %s", error.what());
707                 return;
708         }
709 }
710
711 void install_Undo()
712 {
713         for (auto& cd : createDirectories) {
714                 if (!removeAll(cd)) {
715                         _ERR("Failed to remove of %s", cd.c_str());
716                 }
717         }
718 }
719
720 void unInstall_Undo()
721 {
722         for (auto& unp : updateTac) {
723                 undoStep(unp);
724         }
725 }
726
727 void update_Undo()
728 {
729         install_Undo();
730         if (!tacDB.empty()) {
731                 for (auto& np : tacDB) {
732                         undoStep(np);
733                 }
734         }
735         unInstall_Undo();
736 }
737
738 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNDO(const char *pkgId, const char *appId, GList *list)
739 {
740         _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UNDO =====]");
741         _INFO("PackageID : %s", pkgId);
742
743         // Can be multiple apps in one package
744         if (tacPluginFinished) {
745                 _INFO("TAC plugin already finished(UNDO)");
746                 return 0;
747         }
748         tacPluginFinished = true;
749
750         if (tac_db) {
751                 dbRollback(tac_db);
752                 tac_db = NULL;
753         }
754         if (!strcmp("install", status.c_str())) {
755                 install_Undo();
756         } else if (!strcmp("update", status.c_str())) {
757                 update_Undo();
758         } else if (!strcmp("uninstall", status.c_str())) {
759                 unInstall_Undo();
760         }
761         return 0;
762 }