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