Delete unused libraries when installing the application (#269)
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / tac_common.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 <fstream>
18 #include <json/json.h>
19 #include <pkgmgr-info.h>
20 #include <pkgmgr_installer_info.h>
21
22 #include "log.h"
23 #include "utils.h"
24 #include "tac_common.h"
25 #include "db_manager.h"
26
27 #ifdef  LOG_TAG
28 #undef  LOG_TAG
29 #endif
30 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
31
32 #define __XSTR(x) #x
33 #define __STR(x) __XSTR(x)
34 static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
35 #undef __STR
36 #undef __XSTR
37
38 static sqlite3 *tac_db = NULL;
39 static sqlite3 *tlc_db = NULL;
40 std::vector<std::string> restore_nuget;
41 std::vector<std::string> restore_library;
42
43 static void cleanupDirectory()
44 {
45         std::vector<std::string> removeNuget;
46         try {
47                 for (auto& nuget : bf::recursive_directory_iterator(__DOTNET_DIR)) {
48                         bool isExist = false;
49                         std::string nugetPath = nuget.path().string();
50                         for (auto& restore : restore_nuget) {
51                                 if (!bf::is_directory(nugetPath) || nugetPath.find(TLC_LIBRARIES_DIR) != std::string::npos) {
52                                         isExist = true;
53                                         break;
54                                 }
55                                 if (!strcmp(nugetPath.c_str(), restore.c_str()) ||
56                                         !strcmp(nugetPath.c_str(), restore.substr(0, restore.rfind('/')).c_str())) {
57                                         isExist = true;
58                                         break;
59                                 }
60                         }
61                         if (!isExist) {
62                                 removeNuget.push_back(nugetPath);
63                         }
64                 }
65
66                 for (auto& rm : removeNuget) {
67                         if (!removeAll(rm)) {
68                                 fprintf(stderr, "Failed to remove of %s\n", rm.c_str());
69                         }
70                 }
71                 removeNuget.clear();
72         } catch (const bf::filesystem_error& error) {
73                 fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
74                 return;
75         }
76 }
77
78 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
79 static int tac_restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
80 {
81         char *pkgId = NULL;
82         char *root = NULL;
83         char *exec = NULL;
84         std::string rootPath;
85         std::string execName;
86
87         int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
88         if (ret != PMINFO_R_OK) {
89                 fprintf(stderr, "Failed to get pkgid\n");
90                 return -1;
91         }
92
93         enableTACPackage(std::string(pkgId));
94
95         ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
96         if (ret != PMINFO_R_OK) {
97                 fprintf(stderr, "Failed to get root path\n");
98                 return -1;
99         }
100         rootPath = std::string(root);
101
102         ret = pkgmgrinfo_appinfo_get_exec(handle, &exec);
103         if (ret != PMINFO_R_OK) {
104                 fprintf(stderr, "Failed to get exec name\n");
105                 return -1;
106         }
107         execName = std::string(exec).substr(std::string(exec).rfind('/') + 1);
108
109         std::vector<std::string> parserData;
110         std::string binDir = concatPath(rootPath, "bin");
111         std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
112         for (auto& npAssembly : depsJsonParser(rootPath, execName)) {
113                 std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
114                 std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
115                 if (bf::exists(tacDir) && bf::exists(concatPath(tacDir, assemblyName))) {
116                         parserData.push_back(nugetPackage);
117                 }
118         }
119         std::sort(parserData.begin(), parserData.end());
120         parserData.erase(unique(parserData.begin(), parserData.end()), parserData.end());
121
122         for (auto& nuget : parserData) {
123                 if (tac_db) {
124                         std::string name = nuget.substr(0, nuget.find('/'));
125                         std::string version = nuget.substr(nuget.rfind('/') + 1);
126                         char *sql = sqlite3_mprintf(
127                                 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
128                                 "VALUES (%Q, %Q, %Q, %Q);",     pkgId, nuget.c_str(), name.c_str(), version.c_str());
129                         insertDB(tac_db, sql);
130                         restore_nuget.push_back(concatPath(__DOTNET_DIR, nuget));
131                         sqlite3_free(sql);
132                 }
133         }
134         parserData.clear();
135         return 0;
136 }
137
138 tac_error_e tac_restoreDB()
139 {
140         if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
141                 fprintf(stderr, "Failed to remove of %s\n", TAC_APP_LIST_RESTORE_DB);
142                 return TAC_ERROR_UNKNOWN;
143         }
144
145         std::string dbRestoreJournal = TAC_APP_LIST_RESTORE_DB + std::string("-journal");
146         if (!removeFile(dbRestoreJournal)) {
147                 fprintf(stderr, "Failed to remove of %s\n", dbRestoreJournal.c_str());
148                 return TAC_ERROR_UNKNOWN;
149         }
150
151         tac_db = createDB(TAC_APP_LIST_RESTORE_DB, CREATE_TAC_DB_TABLE);
152         if (!tac_db) {
153                 fprintf(stderr, "Sqlite create error\n");
154                 return TAC_ERROR_UNKNOWN;
155         }
156         sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
157
158         pkgmgrinfo_appinfo_metadata_filter_h handle;
159         int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
160         if (ret != PMINFO_R_OK) {
161                 return TAC_ERROR_UNKNOWN;
162         }
163
164         ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
165         if (ret != PMINFO_R_OK) {
166                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
167                 return TAC_ERROR_UNKNOWN;
168         }
169
170         ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, tac_restoreDBCb, NULL);
171         if (ret != PMINFO_R_OK) {
172                 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
173                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
174                 return TAC_ERROR_UNKNOWN;
175         }
176         fprintf(stdout, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
177
178         pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
179
180         if (tac_db) {
181                 closeDB(tac_db);
182                 tac_db = NULL;
183         }
184
185         const uid_t g_uid = 301; // app_fw
186         const gid_t g_gid = 301; // app_fw
187
188         if (!copyFile(TAC_APP_LIST_RESTORE_DB, TAC_APP_LIST_DB)) {
189                 fprintf(stderr, "Failed to copy of %s\n", TAC_APP_LIST_DB);
190                 return TAC_ERROR_UNKNOWN;
191         }
192         if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
193                 fprintf(stderr, "Failed to remove of %s\n", TAC_APP_LIST_RESTORE_DB);
194                 return TAC_ERROR_UNKNOWN;
195         }
196         if (chown(TAC_APP_LIST_DB, g_uid, g_gid) == -1) {
197                 fprintf(stderr, "Failed to change owner and group name\n");
198         }
199
200         std::string dbJournal = TAC_APP_LIST_DB + std::string("-journal");
201         if (!copyFile(dbRestoreJournal, dbJournal)) {
202                 fprintf(stderr, "Failed to copy of %s\n", dbJournal.c_str());
203                 return TAC_ERROR_UNKNOWN;
204         }
205         if (!removeFile(dbRestoreJournal)) {
206                 fprintf(stderr, "Failed to remove of %s\n", dbRestoreJournal.c_str());
207                 return TAC_ERROR_UNKNOWN;
208         }
209         if (chown(dbJournal.c_str(), g_uid, g_gid) == -1) {
210                 fprintf(stderr, "Failed to change owner and group name\n");
211         }
212
213         cleanupDirectory();
214
215         return TAC_ERROR_NONE;
216 }
217
218 tac_error_e disableTACPackage(const std::string& pkgId)
219 {
220         std::string rootPath = getRootPath(pkgId);
221         if (rootPath.empty()) {
222                 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
223                 return TAC_ERROR_INVALID_PACKAGE;
224         }
225
226         std::string binDir = concatPath(rootPath, "bin");
227         std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
228         std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
229         if (bf::exists(tacDir)) {
230                 try {
231                         for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
232                                 std::string symPath = symlinkAssembly.path().string();
233                                 std::string fileName = symlinkAssembly.path().filename().string();
234                                 if (bf::is_symlink(symPath)) {
235                                         std::string originPath = bf::read_symlink(symPath).string();
236                                         if (!isNativeImage(symPath)) {
237                                                 std::string dllPath = concatPath(binDir, fileName);
238                                                 if (!copyFile(originPath, dllPath)) {
239                                                         fprintf(stderr, "Failed to copy of %s\n", dllPath.c_str());
240                                                         return TAC_ERROR_UNKNOWN;
241                                                 }
242                                                 copySmackAndOwnership(binDir.c_str(), concatPath(binDir, fileName).c_str());
243                                         } else {
244                                                 std::string niPath = concatPath(binNIDir, fileName);
245                                                 if (!copyFile(originPath, niPath)) {
246                                                         fprintf(stderr, "Failed to copy of %s\n", niPath.c_str());
247                                                         return TAC_ERROR_UNKNOWN;
248                                                 }
249                                                 copySmackAndOwnership(binDir.c_str(), niPath.c_str());
250                                         }
251                                 }
252                         }
253                         if (!removeAll(tacDir)) {
254                                 fprintf(stderr, "Failed to remove of %s\n", tacDir.c_str());
255                                 return TAC_ERROR_UNKNOWN;
256                         }
257                 } catch (const bf::filesystem_error& error) {
258                         fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
259                         return TAC_ERROR_UNKNOWN;
260                 }
261         }
262         return TAC_ERROR_NONE;
263 }
264
265 tac_error_e enableTACPackage(const std::string& pkgId)
266 {
267         std::string rootPath = getRootPath(pkgId);
268         if (rootPath.empty()) {
269                 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
270                 return TAC_ERROR_INVALID_PACKAGE;
271         }
272
273         std::string execName = getExecName(pkgId);
274         if (execName.empty()) {
275                 fprintf(stderr, "Failed to get exec name from [%s]\n", pkgId.c_str());
276                 return TAC_ERROR_INVALID_PACKAGE;
277         }
278
279         std::string metaValue = getMetadataValue(pkgId, TAC_METADATA_KEY);
280         if (metaValue.empty()) {
281                 fprintf(stderr, "Failed to get metadata from [%s]\n", pkgId.c_str());
282                 return TAC_ERROR_INVALID_PACKAGE;
283         }
284
285         if (!strcmp(metaValue.c_str(), "true")) {
286                 std::string binDir = concatPath(rootPath, "bin");
287                 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
288                 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
289                 if (!bf::exists(tacDir)) {
290                         if (!createDir(tacDir)) {
291                                 fprintf(stderr, "Cannot create directory: %s\n", tacDir.c_str());
292                                 return TAC_ERROR_UNKNOWN;
293                         }
294                         copySmackAndOwnership(binDir.c_str(), tacDir.c_str());
295
296                         std::vector<std::string> enableNuget;
297                         for (auto& npAssembly : depsJsonParser(rootPath, execName)) {
298                                 std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
299                                 std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
300                                 std::string nugetPath = concatPath(__DOTNET_DIR, nugetPackage);
301                                 if (bf::exists(nugetPath)) {
302                                         std::string originPath = concatPath(nugetPath, assemblyName);
303                                         if (bf::exists(originPath)) {
304                                                 enableNuget.push_back(originPath);
305                                         }
306                                 }
307                         }
308
309                         for (auto& originPath : enableNuget) {
310                                 if (bf::exists(originPath)) {
311                                         std::string fileName = originPath.substr(originPath.rfind('/') + 1);
312                                         std::string NIFileName = fileName.substr(0, fileName.rfind(".dll")) + ".ni.dll";
313                                         if (bf::exists(binNIDir)) {
314                                                 std::string originNIPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
315                                                 if (bf::exists(originNIPath)) {
316                                                         bf::create_symlink(originNIPath, concatPath(tacDir, NIFileName));
317                                                         fprintf(stdout, "%s symbolic link file generated successfully.\n", concatPath(tacDir, NIFileName).c_str());
318                                                         copySmackAndOwnership(tacDir.c_str(), concatPath(tacDir, NIFileName).c_str(), true);
319
320                                                         if (!removeFile(concatPath(binNIDir, NIFileName))) {
321                                                                 fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, NIFileName).c_str());
322                                                                 return TAC_ERROR_UNKNOWN;
323                                                         }
324                                                 }
325                                         }
326                                         bf::create_symlink(originPath, concatPath(tacDir, fileName));
327                                         fprintf(stdout, "%s symbolic link file generated successfully.\n", concatPath(tacDir, fileName).c_str());
328                                         copySmackAndOwnership(tacDir.c_str(), concatPath(tacDir, fileName).c_str(), true);
329
330                                         if (!removeFile(concatPath(binDir, fileName))) {
331                                                 fprintf(stderr, "Failed to remove of %s\n", concatPath(binDir, fileName).c_str());
332                                                 return TAC_ERROR_UNKNOWN;
333                                         }
334                                 }
335                         }
336                         if (enableNuget.empty()) {
337                                 if (!removeAll(tacDir)) {
338                                         fprintf(stderr, "Failed to remove of %s\n", tacDir.c_str());
339                                 }
340                         }
341                         enableNuget.clear();
342                 }
343         } else {
344                 fprintf(stderr, "The metadata key is missing or the metadata value is false of [%s]\n", pkgId.c_str());
345         }
346         return TAC_ERROR_NONE;
347 }
348
349 //Parser the .deps.json file to get nuget information.
350 std::vector<std::string> depsJsonParser(const std::string& rootPath, const std::string& execName)
351 {
352         std::vector<std::string> parserData;
353         std::string depsJsonName = execName.substr(0, execName.rfind(".dll")) + ".deps.json";
354         std::string depsJsonPath = concatPath(rootPath, depsJsonName);
355         try {
356                 if (bf::exists(depsJsonPath)) {
357                         std::ifstream ifs(depsJsonPath);
358                         Json::CharReaderBuilder reader;
359                         Json::Value root;
360                         std::string error;
361                         if (ifs.is_open()) {
362                                 if (!Json::parseFromStream(reader, ifs, &root, &error)) {
363                                         _ERR("Failed to parse of deps.json");
364                                         ifs.close();
365                                         return parserData;
366                                 }
367                                 const Json::Value runtimeTargetName = root["runtimeTarget"]["name"];
368                                 const Json::Value nugetPackages = root["targets"][runtimeTargetName.asString().c_str()];
369                                 std::vector<std::string> appDependencies;
370                                 for (auto& nuget : nugetPackages.getMemberNames()) {
371                                         if (strstr(nuget.c_str(), (execName.substr(0, execName.find(".Tizen."))).c_str()) != NULL ||
372                                                 strstr(nuget.c_str(), (execName.substr(0, execName.find(".dll"))).c_str()) != NULL) {
373                                                 const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
374                                                 if (assemblies != Json::nullValue) {
375                                                         const Json::Value dependencies = nugetPackages[nuget.c_str()]["dependencies"];
376                                                         for (auto& dependency : dependencies.getMemberNames()) {
377                                                                 appDependencies.push_back(dependency);
378                                                         }
379                                                 }
380                                         }
381                                 }
382                                 for (auto& nuget : nugetPackages.getMemberNames()) {
383                                         //Skip the nuget package related to Tizen
384                                         if (strstr(nuget.c_str(), TIZEN_DOTNET_NUGET) == NULL &&
385                                                 strstr(nuget.c_str(), TIZEN_DOTNET_SDK_NUGET) == NULL &&
386                                                 strstr(nuget.c_str(), (execName.substr(0, execName.find(".Tizen."))).c_str()) == NULL &&
387                                                 strstr(nuget.c_str(), (execName.substr(0, execName.find(".dll"))).c_str()) == NULL) {
388                                                 const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
389                                                 if (assemblies != Json::nullValue) {
390                                                         const Json::Value dependencies = nugetPackages[nuget.c_str()]["dependencies"];
391                                                         bool hasDependency = false;
392                                                         for (auto& dependency : dependencies.getMemberNames()) {
393                                                                 //Skip the nugget package that is dependent on another nuget package
394                                                                 if (strstr(dependency.c_str(), TIZEN_DOTNET_NUGET) == NULL &&
395                                                                         strstr(dependency.c_str(), NET_STANDARD_LIBRARY_NUGET) == NULL) {
396                                                                         hasDependency = true;
397                                                                         for (auto& ad : appDependencies) {
398                                                                                 if (!strcmp(ad.c_str(), dependency.c_str())) {
399                                                                                         hasDependency = true;
400                                                                                         break;
401                                                                                 } else {
402                                                                                         hasDependency = false;
403                                                                                 }
404                                                                         }
405                                                                         if (hasDependency) break;
406                                                                 }
407                                                         }
408                                                         if (!hasDependency) {
409                                                                 // handle assembly even though that is included in the TPA.
410                                                                 for (auto& assembly : assemblies.getMemberNames()) {
411                                                                         std::string assemblyName = assembly.substr(assembly.rfind('/') + 1);
412                                                                         parserData.push_back(nuget + ":" + assemblyName);
413                                                                         _INFO("Nuget : [%s] / Assembly : [%s]", nuget.c_str(), assemblyName.c_str());
414                                                                 }
415                                                         }
416                                                 }
417                                         }
418                                 }
419                                 appDependencies.clear();
420                                 ifs.close();
421                         }
422                 }
423         } catch (const Json::LogicError& error) {
424                 _ERR("Failed to parse Json: %s", error.what());
425         }
426         return parserData;
427 }
428
429 std::vector<std::string> getLibrariesInfo(const std::string& rootPath)
430 {
431         std::vector<std::string> LibrariesInfo;
432         std::string runtimesDir = concatPath(rootPath, "bin/runtimes");
433         if (!bf::exists(runtimesDir))
434                 return LibrariesInfo;
435
436         auto convert = [&LibrariesInfo](const std::string& filepath, const std::string& filename) {
437                 if (filepath.rfind(".so") != std::string::npos) {
438                         std::string buffer = SHA256(filepath);
439                         LibrariesInfo.push_back(filepath + ":" + buffer);
440                         _INFO("Library : [%s] / SHA256 : [%s]", filename.c_str(), buffer.c_str());
441                 }
442         };
443         scanFilesInDirectory(runtimesDir, convert, -1);
444
445         return LibrariesInfo;
446 }
447
448 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
449 static int tlc_restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
450 {
451         char *pkgId = NULL;
452         char *root = NULL;
453         std::string rootPath;
454
455         int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
456         if (ret != PMINFO_R_OK) {
457                 fprintf(stderr, "Failed to get pkgid\n");
458                 return -1;
459         }
460
461         ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
462         if (ret != PMINFO_R_OK) {
463                 fprintf(stderr, "Failed to get root path\n");
464                 return -1;
465         }
466         rootPath = std::string(root);
467
468         for (auto& librarySha : getLibrariesInfo(rootPath)) {
469                 std::string library = librarySha.substr(0, librarySha.find(':'));
470                 if (bf::exists(library)) {
471                         std::string fileSha = library.substr(library.rfind('/') + 1) + ".." + librarySha.substr(librarySha.find(':') + 1);
472                         char *sql = sqlite3_mprintf("INSERT INTO TLC (PKGID, LIBRARY) VALUES (%Q, %Q);", pkgId, fileSha.c_str());
473                         insertDB(tlc_db, sql);
474                         restore_library.push_back(fileSha);
475                         sqlite3_free(sql);
476                 }
477         }
478         return 0;
479 }
480
481 tac_error_e tlc_restoreDB()
482 {
483         if (!removeFile(TLC_APP_LIST_RESTORE_DB)) {
484                 fprintf(stderr, "Failed to remove of %s\n", TLC_APP_LIST_RESTORE_DB);
485                 return TAC_ERROR_UNKNOWN;
486         }
487
488         std::string dbRestoreJournal = TLC_APP_LIST_RESTORE_DB + std::string("-journal");
489         if (!removeFile(dbRestoreJournal)) {
490                 fprintf(stderr, "Failed to remove of %s\n", dbRestoreJournal.c_str());
491                 return TAC_ERROR_UNKNOWN;
492         }
493
494         tlc_db = createDB(TLC_APP_LIST_RESTORE_DB, CREATE_TLC_DB_TABLE);
495         if (!tlc_db) {
496                 fprintf(stderr, "Sqlite create error\n");
497                 return TAC_ERROR_UNKNOWN;
498         }
499         sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL);
500
501         pkgmgrinfo_appinfo_metadata_filter_h handle;
502         int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
503         if (ret != PMINFO_R_OK) {
504                 return TAC_ERROR_UNKNOWN;
505         }
506
507         ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
508         if (ret != PMINFO_R_OK) {
509                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
510                 return TAC_ERROR_UNKNOWN;
511         }
512
513         ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, tlc_restoreDBCb, NULL);
514         if (ret != PMINFO_R_OK) {
515                 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
516                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
517                 return TAC_ERROR_UNKNOWN;
518         }
519         fprintf(stdout, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
520
521         pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
522
523         if (tlc_db) {
524                 closeDB(tlc_db);
525                 tlc_db = NULL;
526         }
527
528         const uid_t g_uid = 301; // app_fw
529         const gid_t g_gid = 301; // app_fw
530
531         if (!copyFile(TLC_APP_LIST_RESTORE_DB, TLC_APP_LIST_DB)) {
532                 fprintf(stderr, "Failed to copy of %s\n", TLC_APP_LIST_DB);
533                 return TAC_ERROR_UNKNOWN;
534         }
535         if (!removeFile(TLC_APP_LIST_RESTORE_DB)) {
536                 fprintf(stderr, "Failed to remove of %s\n", TLC_APP_LIST_RESTORE_DB);
537                 return TAC_ERROR_UNKNOWN;
538         }
539         if (chown(TLC_APP_LIST_DB, g_uid, g_gid) == -1) {
540                 fprintf(stderr, "Failed to change owner and group name\n");
541         }
542
543         std::string dbJournal = TLC_APP_LIST_DB + std::string("-journal");
544         if (!copyFile(dbRestoreJournal, dbJournal)) {
545                 fprintf(stderr, "Failed to copy of %s\n", dbJournal.c_str());
546                 return TAC_ERROR_UNKNOWN;
547         }
548         if (!removeFile(dbRestoreJournal)) {
549                 fprintf(stderr, "Failed to remove of %s\n", dbRestoreJournal.c_str());
550                 return TAC_ERROR_UNKNOWN;
551         }
552         if (chown(dbJournal.c_str(), g_uid, g_gid) == -1) {
553                 fprintf(stderr, "Failed to change owner and group name\n");
554         }
555
556         auto convert = [](const std::string& path, const std::string& filename) {
557                 bool isExist = false;
558                 for (auto& library : restore_library) {
559                         if (!strcmp(filename.c_str(), library.c_str())) {
560                                 isExist = true;
561                                 break;
562                         }
563                 }
564                 if (!isExist) {
565                         if (!removeFile(path)) {
566                                 _ERR("Failed to remove of %s", path.c_str());
567                         }
568                 }
569         };
570
571         scanFilesInDirectory(concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR), convert, 0);
572
573         return TAC_ERROR_NONE;
574 }