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