Fixed svace (376818, 386676, 386690)
[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
23 #include <cstring>
24 #include <fstream>
25 #include <sstream>
26 #include <vector>
27 #include <boost/filesystem.hpp>
28 #include <glib.h>
29 #include <json/json.h>
30 #include <pkgmgr-info.h>
31 #include <pkgmgr_installer_info.h>
32 #include <openssl/sha.h>
33
34 #ifdef  LOG_TAG
35 #undef  LOG_TAG
36 #endif
37 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
38
39 #define __XSTR(x) #x
40 #define __STR(x) __XSTR(x)
41 static const char* __TAC_DIR = __STR(TAC_DIR);
42 #undef __STR
43 #undef __XSTR
44
45 typedef struct Metadata {
46         const char *key;
47         const char *value;
48 } Metadata;
49
50 std::vector<std::string> nugetPackagesAssembliesSha;
51 std::vector<std::string> tacDB;
52 std::vector<std::string> createDirectories;
53 std::vector<std::string> updateTac;
54 std::string status = "";
55 std::string rootPath;
56 std::string execName;
57 std::string binPath;
58 bool isCreateDirectory = false;
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 createSymlink(std::string tac_version_dir, std::string np)
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 (isCreateDirectory) {
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                 if (!dbOpen(tac_db, TAC_APP_LIST_DB)) {
232                         _ERR("Sqlite open error");
233                         return 0;
234                 }
235         } else {
236                 _ERR("Sqlite create error");
237                 return 0;
238         }
239
240         if (tacDB.empty()) {
241                 _INFO("Not exist .deps.json file");
242                 return 0;
243         }
244
245         for (auto& np : tacDB) {
246                 std::string tac_name = np.substr(0, np.find('/'));
247                 std::string tac_version = np.substr(np.rfind('/') + 1);
248                 _INFO("TAC name : %s", tac_name.c_str());
249                 _INFO("TAC version : %s", tac_version.c_str());
250
251                 std::string tac_version_dir = concatPath(__TAC_DIR, np);
252                 std::string sha256_info = concatPath(tac_version_dir, TAC_SHA_256_INFO);
253                 isCreateDirectory = false;
254                 if (!bf::exists(tac_version_dir)) {
255                         _INFO("Create tac_version_dir [%s]", tac_version_dir.c_str());
256                         if (!createDir(tac_version_dir)) {
257                                 _ERR("Cannot create directory: %s", tac_version_dir.c_str());
258                                 return 0;
259                         }
260                         isCreateDirectory = true;
261                         createDirectories.push_back(tac_version_dir);
262                         std::ofstream ofs(sha256_info, std::ios::app);
263                         int assembly_count = 0;
264                         for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
265                                 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
266                                 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
267                                 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
268                                 std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
269                                 if (!strcmp(nuget_package.c_str(), np.c_str())) {
270                                         ofs << assembly << ":" << sha << std::endl;
271                                         assembly_count++;
272                                 }
273                         }
274                         ofs << assembly_count << std::endl;
275                         ofs.close();
276
277                         if (createSymlink(tac_version_dir, np) < 0) {
278                                 _ERR("Failed to create symlink");
279                                 return -1;
280                         }
281                         std::string sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
282                                         "VALUES ('" + std::string(pkgId) + "', '" + np + "', '" + tac_name + "', '" + tac_version + "');";
283                         dbInsert(tac_db, TAC_APP_LIST_DB, sql);
284                 } else {
285                         _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str());
286                         int compare_count = 0;
287                         int assembly_count = 0;
288                         std::string sha256_count = "0";
289                         for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
290                                 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
291                                 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
292                                 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
293                                 std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
294                                 if (!strcmp(nuget_package.c_str(), np.c_str())) {
295                                         assembly_count++;
296                                         std::ifstream ifs(sha256_info);
297                                         std::string get_str;
298                                         if (ifs.is_open()) {
299                                                 while (getline(ifs, get_str)) {
300                                                         if (!strcmp(get_str.c_str(), (assembly + ":" + sha).c_str())) {
301                                                                 compare_count++;
302                                                         }
303                                                         sha256_count = get_str;
304                                                 }
305                                                 ifs.close();
306                                         }
307                                 }
308                         }
309                         if (!strcmp(std::to_string(assembly_count).c_str(), std::to_string(compare_count).c_str()) &&
310                                 !strcmp(std::to_string(assembly_count).c_str(), sha256_count.c_str())) {
311                                 _INFO("Same nuget : %s", np.c_str());
312                                 if (createSymlink(tac_version_dir, np) < 0) {
313                                         _ERR("Failed to create symlink");
314                                         return -1;
315                                 }
316                                 std::string sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
317                                                 "VALUES ('" + std::string(pkgId) + "', '" + np + "', '" + tac_name + "', '" + tac_version + "');";
318                                 dbInsert(tac_db, TAC_APP_LIST_DB, sql);
319                         } else {
320                                 _INFO("Different nuget : %s", np.c_str());
321                         }
322                 }
323                 if (!bf::exists(sha256_info)) {
324                         if(!removeAll(tac_version_dir)) {
325                                 _ERR("Failed to remove of %s", tac_version_dir.c_str());
326                         }
327                 }
328         }
329         return 0;
330 }
331
332 static int sqliteCb(void *count, int argc, char **argv, char **colName) {
333         int *c = (int*)count;
334         *c = atoi(argv[0]);
335         return 0;
336 }
337
338 int updateTacDB(const char *pkgId)
339 {
340         for (auto& unp : updateTac) {
341                 int count = -1;
342                 if (tac_db) {
343                         if (!dbOpen(tac_db, TAC_APP_LIST_DB)) {
344                                 _ERR("Sqlite open error");
345                                 return -1;
346                         }
347                 } else {
348                         _ERR("Sqlite create error");
349                         return -1;
350                 }
351                 std::string sql = "SELECT COUNT(NUGET) FROM TAC WHERE NUGET = '" + unp + "';";
352                 int ret = sqlite3_exec(tac_db, sql.c_str(), sqliteCb, &count, NULL);
353                 if (ret != SQLITE_OK) {
354                         _ERR("Sqlite select error");
355                         return -1;
356                 }
357                 if (count == 0) {
358                         std::string tac_version_dir_prev = concatPath(__TAC_DIR, unp);
359                         std::string tac_version_dir_backup = tac_version_dir_prev + ".bck";
360                         if (!copyDir(tac_version_dir_prev, tac_version_dir_backup)) {
361                                 _ERR("Failed to copy of %s to %s", tac_version_dir_prev.c_str(), tac_version_dir_backup.c_str());
362                                 return -1;
363                         }
364                         if (!removeAll(tac_version_dir_prev)) {
365                                 _ERR("Failed to remove of %s", tac_version_dir_prev.c_str());
366                                 return -1;
367                         }
368                 }
369         }
370         return 0;
371 }
372
373 extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *appId, GList *list)
374 {
375         _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UPGRADE =====]");
376         _INFO("PackageID : %s", pkgId);
377
378         if (!appTypeCheck(std::string(pkgId))) {
379                 _INFO("App type is not dotnet");
380                 return 0;
381         }
382         if (getExecName(std::string(pkgId), execName) < 0) {
383                 return 0;
384         }
385         if (getRootPath(std::string(pkgId), rootPath) < 0) {
386                 return 0;
387         } else {
388                 binPath = concatPath(rootPath, "bin");
389         }
390         if (!strcmp("removed", status.c_str())) {
391                 _INFO("Skipped to parse of deps.json");
392         } else {
393                 if (metadataCheck(list)) {
394                         depsJsonCheck();
395                 }
396         }
397
398         status = "update";
399         tac_db = dbCreate(TAC_APP_LIST_DB);
400         if (tac_db) {
401                 if (!dbOpen(tac_db, TAC_APP_LIST_DB)) {
402                         _ERR("Sqlite open error");
403                         return 0;
404                 }
405         } else {
406                 _ERR("Sqlite create error");
407                 return 0;
408         }
409
410         std::string sql = "SELECT * FROM TAC WHERE PKGID = '" + std::string(pkgId) + "';";
411         updateTac = dbSelect(tac_db, TAC_APP_LIST_DB, sql);
412
413         if (tacDB.empty()) {
414                 sql = "DELETE FROM TAC WHERE PKGID = '" + std::string(pkgId) + "';";
415                 dbDelete(tac_db, TAC_APP_LIST_DB, sql);
416                 if (updateTacDB(pkgId) < 0) {
417                         return -1;
418                 }
419         } else {
420                 for (auto& np : tacDB) {
421                         std::string tac_name = np.substr(0, np.find('/'));
422                         std::string tac_version = np.substr(np.rfind('/') + 1);
423                         _INFO("TAC name : %s", tac_name.c_str());
424                         _INFO("TAC version : %s", tac_version.c_str());
425
426                         std::string tac_version_dir = concatPath(__TAC_DIR, np);
427                         std::string sha256_info = concatPath(tac_version_dir, TAC_SHA_256_INFO);
428                         isCreateDirectory = false;
429                         if (!bf::exists(tac_version_dir)) {
430                                 _INFO("Create tac_version_dir [%s]", tac_version_dir.c_str());
431                                 if (!createDir(tac_version_dir)) {
432                                         _ERR("Cannot create directory: %s", tac_version_dir.c_str());
433                                         return 0;
434                                 }
435                                 isCreateDirectory = true;
436                                 createDirectories.push_back(tac_version_dir);
437                                 std::ofstream ofs(sha256_info, std::ios::app);
438                                 int assembly_count = 0;
439                                 for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
440                                         std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
441                                         std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
442                                         std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
443                                         std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
444                                         if (!strcmp(nuget_package.c_str(), np.c_str())) {
445                                                 ofs << assembly << ":" << sha << std::endl;
446                                                 assembly_count++;
447                                         }
448                                 }
449                                 ofs << assembly_count << std::endl;
450                                 ofs.close();
451                                 if (createSymlink(tac_version_dir, np) < 0) {
452                                         _ERR("Failed to create symlink");
453                                         return -1;
454                                 }
455
456                                 int count = -1;
457                                 sql = "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = '" + std::string(pkgId) + "' AND NAME = '" + tac_name + "';";
458                                 int ret = sqlite3_exec(tac_db, sql.c_str(), sqliteCb, &count, NULL);
459                                 if (ret != SQLITE_OK) {
460                                         _ERR("Sqlite select error");
461                                         return -1;
462                                 }
463                                 if (count == 1) {
464                                         sql = "UPDATE TAC SET NAME = '" + tac_name + "', VERSION = '" + tac_version + "', NUGET = '" + np + "' WHERE PKGID = '" + std::string(pkgId) + "' AND NAME = '" + tac_name + "';";
465                                         dbUpdate(tac_db, TAC_APP_LIST_DB, sql);
466                                 } else if (count == 0) {
467                                         sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
468                                                 "VALUES ('" + std::string(pkgId) + "', '" + np + "', '" + tac_name + "', '" + tac_version + "');";
469                                         dbInsert(tac_db, TAC_APP_LIST_DB, sql);
470                                 }
471                         } else {
472                                 _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str());
473                                 int compare_count = 0;
474                                 int assembly_count = 0;
475                                 std::string sha256_count = "0";
476                                 for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
477                                         std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
478                                         std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
479                                         std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
480                                         std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
481                                         if (!strcmp(nuget_package.c_str(), np.c_str())) {
482                                                 assembly_count++;
483                                                 std::ifstream ifs(sha256_info);
484                                                 std::string get_str;
485                                                 if (ifs.is_open()) {
486                                                         while (getline(ifs, get_str)) {
487                                                                 if (!strcmp(get_str.c_str(), (assembly + ":" + sha).c_str())) {
488                                                                         compare_count++;
489                                                                 }
490                                                                 sha256_count = get_str;
491                                                         }
492                                                         ifs.close();
493                                                 }
494                                         }
495                                 }
496
497                                 if (!strcmp(std::to_string(assembly_count).c_str(), std::to_string(compare_count).c_str()) &&
498                                         !strcmp(std::to_string(assembly_count).c_str(), sha256_count.c_str())) {
499                                         _INFO("Same nuget : %s", np.c_str());
500                                         if (createSymlink(tac_version_dir, np) < 0) {
501                                                 _ERR("Failed to create symlink");
502                                                 return -1;
503                                         }
504
505                                         int count = -1;
506                                         std::string sql = "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = '" + std::string(pkgId) + "' AND NAME = '" + tac_name + "';";
507                                         int ret = sqlite3_exec(tac_db, sql.c_str(), sqliteCb, &count, NULL);
508                                         if (ret != SQLITE_OK) {
509                                                 _ERR("Sqlite select error");
510                                                 return -1;
511                                         }
512                                         if (count == 1) {
513                                                 sql = "UPDATE TAC SET NAME = '" + tac_name + "', VERSION = '" + tac_version + "', NUGET = '" + np + "' WHERE PKGID = '" + std::string(pkgId) + "' AND NAME = '" + tac_name + "';";
514                                                 dbUpdate(tac_db, TAC_APP_LIST_DB, sql);
515                                         } else if (count == 0) {
516                                                 sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
517                                                         "VALUES ('" + std::string(pkgId) + "', '" + np + "', '" + tac_name + "', '" + tac_version + "');";
518                                                 dbInsert(tac_db, TAC_APP_LIST_DB, sql);
519                                         }
520                                 } else {
521                                         _INFO("Different nuget : %s", np.c_str());
522                                 }
523                         }
524                         if (!bf::exists(sha256_info)) {
525                                 if(!removeAll(tac_version_dir)) {
526                                         _ERR("Failed to remove of %s", tac_version_dir.c_str());
527                                 }
528                         }
529                 }
530                 for (auto& unp : updateTac) {
531                         bool isExits = false;
532                         for (auto& np : tacDB) {
533                                 if (!strcmp(unp.c_str(), np.c_str())) {
534                                         isExits = true;
535                                         break;
536                                 }
537                         }
538                         if (!isExits) {
539                                 std::string sql = "DELETE FROM TAC WHERE PKGID = '" + std::string(pkgId) + "' AND NUGET = '" + unp + "';";
540                                 dbDelete(tac_db, TAC_APP_LIST_DB, sql);
541                         }
542                 }
543                 if (updateTacDB(pkgId) < 0) {
544                         return -1;
545                 }
546         }
547         return 0;
548 }
549
550 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgId, const char *appId, GList *list)
551 {
552         _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UNINSTALL =====]");
553         _INFO("PackageID : %s", pkgId);
554
555         status = "uninstall";
556         tac_db = dbCreate(TAC_APP_LIST_DB);
557         if (tac_db) {
558                 if (!dbOpen(tac_db, TAC_APP_LIST_DB)) {
559                         _ERR("Sqlite open error");
560                         return 0;
561                 }
562         } else {
563                 _ERR("Sqlite create error");
564                 return 0;
565         }
566
567         std::string sql = "SELECT * FROM TAC WHERE PKGID = '" + std::string(pkgId) + "';";
568         updateTac = dbSelect(tac_db, TAC_APP_LIST_DB, sql);
569
570         sql = "DELETE FROM TAC WHERE PKGID = '" + std::string(pkgId) + "';";
571         dbDelete(tac_db, TAC_APP_LIST_DB, sql);
572
573         if (updateTacDB(pkgId) < 0) {
574                 return -1;
575         }
576         return 0;
577 }
578
579 extern "C" int PKGMGR_MDPARSER_PLUGIN_REMOVED(const char *pkgId, const char *appId, GList *list)
580 {
581         _DBG("[===== PKGMGR_MDPARSER_PLUGIN_REMOVED =====]");
582         _INFO("PackageID : %s", pkgId);
583
584         status = "removed";
585
586         return PKGMGR_MDPARSER_PLUGIN_UPGRADE(pkgId, appId, list);
587 }
588
589 void cleanStep(std::string tac)
590 {
591         std::string current_tac = concatPath(__TAC_DIR, tac.substr(0, tac.find('/')));
592         try {
593                 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
594                         std::string bck_path = bck.path().string();
595                         if (bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") != NULL) {
596                                 if (!removeAll(bck_path)) {
597                                         _ERR("Failed to remove of %s", bck_path.c_str());
598                                 }
599                                 break;
600                         }
601                 }
602
603                 bool isExist = false;
604                 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
605                         std::string bck_path = bck.path().string();
606                         if (bf::exists(bck_path) && bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") == NULL) {
607                                 isExist = true;
608                                 break;
609                         }
610                 }
611                 if (!isExist) {
612                         if (!removeAll(current_tac)) {
613                                 _ERR("Failed to remove of %s", current_tac.c_str());
614                         }
615                 }
616         } catch (const bf::filesystem_error& error) {
617                 _ERR("Failed to recursive directory: %s", error.what());
618                 return;
619         }
620 }
621
622 void install_Clean()
623 {
624         return;
625 }
626
627 void unInstall_Clean()
628 {
629         for (auto& unp : updateTac) {
630                 cleanStep(unp);
631         }
632 }
633
634 void update_Clean()
635 {
636         if (!tacDB.empty()) {
637                 for (auto& np : tacDB) {
638                         cleanStep(np);
639                 }
640         }
641         unInstall_Clean();
642 }
643
644 extern "C" int PKGMGR_MDPARSER_PLUGIN_CLEAN(const char *pkgId, const char *appId, GList *list)
645 {
646         _DBG("[===== PKGMGR_MDPARSER_PLUGIN_CLEAN =====]");
647         _INFO("PackageID : %s", pkgId);
648
649         if (tac_db) {
650                 dbClose(tac_db);
651                 tac_db = NULL;
652         }
653         if (!strcmp("install", status.c_str())) {
654                 install_Clean();
655         } else if (!strcmp("update", status.c_str())) {
656                 update_Clean();
657         } else if (!strcmp("uninstall", status.c_str())) {
658                 unInstall_Clean();
659         }
660         return 0;
661 }
662
663 void undoStep(std::string tac)
664 {
665         std::string current_tac = concatPath(__TAC_DIR, tac.substr(0, tac.find('/')));
666         try {
667                 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
668                         std::string bck_path = bck.path().string();
669                         if (bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") != NULL) {
670                                 if (!moveFile(bck_path, bck_path.substr(0, bck_path.rfind(".bck")))) {
671                                         _ERR("Failed to move %s", bck_path.c_str());
672                                 }
673                                 break;
674                         }
675                 }
676         } catch (const bf::filesystem_error& error) {
677                 _ERR("Failed to recursive directory: %s", error.what());
678                 return;
679         }
680 }
681
682 void install_Undo()
683 {
684         for (auto& cd : createDirectories) {
685                 if (!removeAll(cd)) {
686                         _ERR("Failed to remove of %s", cd.c_str());
687                 }
688         }
689 }
690
691 void unInstall_Undo()
692 {
693         for (auto& unp : updateTac) {
694                 undoStep(unp);
695         }
696 }
697
698 void update_Undo()
699 {
700         install_Undo();
701         if (!tacDB.empty()) {
702                 for (auto& np : tacDB) {
703                         undoStep(np);
704                 }
705         }
706         unInstall_Undo();
707 }
708
709 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNDO(const char *pkgId, const char *appId, GList *list)
710 {
711         _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UNDO =====]");
712         _INFO("PackageID : %s", pkgId);
713
714         if (tac_db) {
715                 dbRollback(tac_db);
716                 tac_db = NULL;
717         }
718         if (!strcmp("install", status.c_str())) {
719                 install_Undo();
720         } else if (!strcmp("update", status.c_str())) {
721                 update_Undo();
722         } else if (!strcmp("uninstall", status.c_str())) {
723                 unInstall_Undo();
724         }
725         return 0;
726 }