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