Use multi-user support pkgmgrinfo API
[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 (exist(tacDir) && exist(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 = pkgmgrMDFilterForeach(handle, tac_restoreDBCb, NULL);
171         if (ret != 0) {
172                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
173                 return TAC_ERROR_UNKNOWN;
174         }
175
176         pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
177
178         if (tac_db) {
179                 closeDB(tac_db);
180                 tac_db = NULL;
181         }
182
183         const uid_t g_uid = 301; // app_fw
184         const gid_t g_gid = 301; // app_fw
185
186         if (!copyFile(TAC_APP_LIST_RESTORE_DB, TAC_APP_LIST_DB)) {
187                 fprintf(stderr, "Failed to copy of %s\n", TAC_APP_LIST_DB);
188                 return TAC_ERROR_UNKNOWN;
189         }
190         if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
191                 fprintf(stderr, "Failed to remove of %s\n", TAC_APP_LIST_RESTORE_DB);
192                 return TAC_ERROR_UNKNOWN;
193         }
194         if (chown(TAC_APP_LIST_DB, g_uid, g_gid) == -1) {
195                 fprintf(stderr, "Failed to change owner and group name\n");
196         }
197
198         std::string dbJournal = TAC_APP_LIST_DB + std::string("-journal");
199         if (!copyFile(dbRestoreJournal, dbJournal)) {
200                 fprintf(stderr, "Failed to copy of %s\n", dbJournal.c_str());
201                 return TAC_ERROR_UNKNOWN;
202         }
203         if (!removeFile(dbRestoreJournal)) {
204                 fprintf(stderr, "Failed to remove of %s\n", dbRestoreJournal.c_str());
205                 return TAC_ERROR_UNKNOWN;
206         }
207         if (chown(dbJournal.c_str(), g_uid, g_gid) == -1) {
208                 fprintf(stderr, "Failed to change owner and group name\n");
209         }
210
211         cleanupDirectory();
212
213         return TAC_ERROR_NONE;
214 }
215
216 tac_error_e disableTACPackage(const std::string& pkgId)
217 {
218         std::string rootPath = getRootPath(pkgId);
219         if (rootPath.empty()) {
220                 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
221                 return TAC_ERROR_INVALID_PACKAGE;
222         }
223
224         std::string binDir = concatPath(rootPath, "bin");
225         std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
226         std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
227         if (exist(tacDir)) {
228                 try {
229                         for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
230                                 std::string symPath = symlinkAssembly.path().string();
231                                 std::string fileName = symlinkAssembly.path().filename().string();
232                                 if (bf::is_symlink(symPath)) {
233                                         std::string originPath = bf::read_symlink(symPath).string();
234                                         if (!isNativeImage(symPath)) {
235                                                 std::string dllPath = concatPath(binDir, fileName);
236                                                 if (!copyFile(originPath, dllPath)) {
237                                                         fprintf(stderr, "Failed to copy of %s\n", dllPath.c_str());
238                                                         return TAC_ERROR_UNKNOWN;
239                                                 }
240                                                 copySmackAndOwnership(binDir.c_str(), concatPath(binDir, fileName).c_str());
241                                         } else {
242                                                 std::string niPath = concatPath(binNIDir, fileName);
243                                                 if (!copyFile(originPath, niPath)) {
244                                                         fprintf(stderr, "Failed to copy of %s\n", niPath.c_str());
245                                                         return TAC_ERROR_UNKNOWN;
246                                                 }
247                                                 copySmackAndOwnership(binDir.c_str(), niPath.c_str());
248                                         }
249                                 }
250                         }
251                         if (!removeAll(tacDir)) {
252                                 fprintf(stderr, "Failed to remove of %s\n", tacDir.c_str());
253                                 return TAC_ERROR_UNKNOWN;
254                         }
255                 } catch (const bf::filesystem_error& error) {
256                         fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
257                         return TAC_ERROR_UNKNOWN;
258                 }
259         }
260         return TAC_ERROR_NONE;
261 }
262
263 tac_error_e enableTACPackage(const std::string& pkgId)
264 {
265         std::string rootPath = getRootPath(pkgId);
266         if (rootPath.empty()) {
267                 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
268                 return TAC_ERROR_INVALID_PACKAGE;
269         }
270
271         std::string execName = getExecName(pkgId);
272         if (execName.empty()) {
273                 fprintf(stderr, "Failed to get exec name from [%s]\n", pkgId.c_str());
274                 return TAC_ERROR_INVALID_PACKAGE;
275         }
276
277         std::string metaValue = getMetadataValue(pkgId, TAC_METADATA_KEY);
278         if (metaValue.empty()) {
279                 fprintf(stderr, "Failed to get metadata from [%s]\n", pkgId.c_str());
280                 return TAC_ERROR_INVALID_PACKAGE;
281         }
282
283         if (!strcmp(metaValue.c_str(), "true")) {
284                 std::string binDir = concatPath(rootPath, "bin");
285                 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
286                 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
287                 if (!exist(tacDir)) {
288                         if (!createDir(tacDir)) {
289                                 fprintf(stderr, "Cannot create directory: %s\n", tacDir.c_str());
290                                 return TAC_ERROR_UNKNOWN;
291                         }
292                         copySmackAndOwnership(binDir.c_str(), tacDir.c_str());
293
294                         std::vector<std::string> enableNuget;
295                         for (auto& npAssembly : depsJsonParser(rootPath, execName)) {
296                                 std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
297                                 std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
298                                 std::string nugetPath = concatPath(__DOTNET_DIR, nugetPackage);
299                                 if (exist(nugetPath)) {
300                                         std::string originPath = concatPath(nugetPath, assemblyName);
301                                         if (exist(originPath)) {
302                                                 enableNuget.push_back(originPath);
303                                         }
304                                 }
305                         }
306
307                         for (auto& originPath : enableNuget) {
308                                 if (exist(originPath)) {
309                                         std::string fileName = originPath.substr(originPath.rfind('/') + 1);
310                                         std::string NIFileName = fileName.substr(0, fileName.rfind(".dll")) + ".ni.dll";
311                                         if (exist(binNIDir)) {
312                                                 std::string originNIPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
313                                                 if (exist(originNIPath)) {
314                                                         bf::create_symlink(originNIPath, concatPath(tacDir, NIFileName));
315                                                         fprintf(stdout, "%s symbolic link file generated successfully.\n", concatPath(tacDir, NIFileName).c_str());
316                                                         copySmackAndOwnership(tacDir.c_str(), concatPath(tacDir, NIFileName).c_str(), true);
317
318                                                         if (!removeFile(concatPath(binNIDir, NIFileName))) {
319                                                                 fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, NIFileName).c_str());
320                                                                 return TAC_ERROR_UNKNOWN;
321                                                         }
322                                                 }
323                                         }
324                                         bf::create_symlink(originPath, concatPath(tacDir, fileName));
325                                         fprintf(stdout, "%s symbolic link file generated successfully.\n", concatPath(tacDir, fileName).c_str());
326                                         copySmackAndOwnership(tacDir.c_str(), concatPath(tacDir, fileName).c_str(), true);
327
328                                         if (!removeFile(concatPath(binDir, fileName))) {
329                                                 fprintf(stderr, "Failed to remove of %s\n", concatPath(binDir, fileName).c_str());
330                                                 return TAC_ERROR_UNKNOWN;
331                                         }
332                                 }
333                         }
334                         if (enableNuget.empty()) {
335                                 if (!removeAll(tacDir)) {
336                                         fprintf(stderr, "Failed to remove of %s\n", tacDir.c_str());
337                                 }
338                         }
339                         enableNuget.clear();
340                 }
341         } else {
342                 fprintf(stderr, "The metadata key is missing or the metadata value is false of [%s]\n", pkgId.c_str());
343         }
344         return TAC_ERROR_NONE;
345 }
346
347 //Parser the .deps.json file to get nuget information.
348 std::vector<std::string> depsJsonParser(const std::string& rootPath, const std::string& execName)
349 {
350         std::vector<std::string> parserData;
351         std::string depsJsonName = execName.substr(0, execName.rfind(".dll")) + ".deps.json";
352         std::string depsJsonPath = concatPath(rootPath, depsJsonName);
353         try {
354                 if (exist(depsJsonPath)) {
355                         std::ifstream ifs(depsJsonPath);
356                         Json::CharReaderBuilder reader;
357                         Json::Value root;
358                         std::string error;
359                         if (ifs.is_open()) {
360                                 if (!Json::parseFromStream(reader, ifs, &root, &error)) {
361                                         _ERR("Failed to parse of deps.json");
362                                         ifs.close();
363                                         return parserData;
364                                 }
365                                 const Json::Value runtimeTargetName = root["runtimeTarget"]["name"];
366                                 const Json::Value nugetPackages = root["targets"][runtimeTargetName.asString().c_str()];
367                                 std::vector<std::string> appDependencies;
368                                 for (auto& nuget : nugetPackages.getMemberNames()) {
369                                         if (strstr(nuget.c_str(), (execName.substr(0, execName.find(".Tizen."))).c_str()) != NULL ||
370                                                 strstr(nuget.c_str(), (execName.substr(0, execName.find(".dll"))).c_str()) != NULL) {
371                                                 const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
372                                                 if (assemblies != Json::nullValue) {
373                                                         const Json::Value dependencies = nugetPackages[nuget.c_str()]["dependencies"];
374                                                         for (auto& dependency : dependencies.getMemberNames()) {
375                                                                 appDependencies.push_back(dependency);
376                                                         }
377                                                 }
378                                         }
379                                 }
380                                 for (auto& nuget : nugetPackages.getMemberNames()) {
381                                         //Skip the nuget package related to Tizen
382                                         if (strstr(nuget.c_str(), TIZEN_DOTNET_NUGET) == NULL &&
383                                                 strstr(nuget.c_str(), TIZEN_DOTNET_SDK_NUGET) == NULL &&
384                                                 strstr(nuget.c_str(), (execName.substr(0, execName.find(".Tizen."))).c_str()) == NULL &&
385                                                 strstr(nuget.c_str(), (execName.substr(0, execName.find(".dll"))).c_str()) == NULL) {
386                                                 const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
387                                                 if (assemblies != Json::nullValue) {
388                                                         const Json::Value dependencies = nugetPackages[nuget.c_str()]["dependencies"];
389                                                         bool hasDependency = false;
390                                                         for (auto& dependency : dependencies.getMemberNames()) {
391                                                                 //Skip the nugget package that is dependent on another nuget package
392                                                                 if (strstr(dependency.c_str(), TIZEN_DOTNET_NUGET) == NULL &&
393                                                                         strstr(dependency.c_str(), NET_STANDARD_LIBRARY_NUGET) == NULL) {
394                                                                         hasDependency = true;
395                                                                         for (auto& ad : appDependencies) {
396                                                                                 if (!strcmp(ad.c_str(), dependency.c_str())) {
397                                                                                         hasDependency = true;
398                                                                                         break;
399                                                                                 } else {
400                                                                                         hasDependency = false;
401                                                                                 }
402                                                                         }
403                                                                         if (hasDependency) break;
404                                                                 }
405                                                         }
406                                                         if (!hasDependency) {
407                                                                 // handle assembly even though that is included in the TPA.
408                                                                 for (auto& assembly : assemblies.getMemberNames()) {
409                                                                         std::string assemblyName = assembly.substr(assembly.rfind('/') + 1);
410                                                                         parserData.push_back(nuget + ":" + assemblyName);
411                                                                         _INFO("Nuget : [%s] / Assembly : [%s]", nuget.c_str(), assemblyName.c_str());
412                                                                 }
413                                                         }
414                                                 }
415                                         }
416                                 }
417                                 appDependencies.clear();
418                                 ifs.close();
419                         }
420                 }
421         } catch (const Json::LogicError& error) {
422                 _ERR("Failed to parse Json: %s", error.what());
423         }
424         return parserData;
425 }
426
427 std::vector<std::string> getLibrariesInfo(const std::string& rootPath)
428 {
429         std::vector<std::string> LibrariesInfo;
430         std::string runtimesDir = concatPath(rootPath, "bin/runtimes");
431         if (!exist(runtimesDir))
432                 return LibrariesInfo;
433
434         auto convert = [&LibrariesInfo](const std::string& filepath, const std::string& filename) {
435                 if (filepath.rfind(".so") != std::string::npos) {
436                         std::string buffer = SHA256(filepath);
437                         LibrariesInfo.push_back(filepath + ":" + buffer);
438                         _INFO("Library : [%s] / SHA256 : [%s]", filename.c_str(), buffer.c_str());
439                 }
440         };
441         scanFilesInDirectory(runtimesDir, convert, -1);
442
443         return LibrariesInfo;
444 }
445
446 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
447 static int tlc_restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
448 {
449         char *pkgId = NULL;
450         char *root = NULL;
451         std::string rootPath;
452
453         int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
454         if (ret != PMINFO_R_OK) {
455                 fprintf(stderr, "Failed to get pkgid\n");
456                 return -1;
457         }
458
459         ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
460         if (ret != PMINFO_R_OK) {
461                 fprintf(stderr, "Failed to get root path\n");
462                 return -1;
463         }
464         rootPath = std::string(root);
465
466         for (auto& librarySha : getLibrariesInfo(rootPath)) {
467                 std::string library = librarySha.substr(0, librarySha.find(':'));
468                 if (exist(library)) {
469                         std::string fileSha = library.substr(library.rfind('/') + 1) + ".." + librarySha.substr(librarySha.find(':') + 1);
470                         char *sql = sqlite3_mprintf("INSERT INTO TLC (PKGID, LIBRARY) VALUES (%Q, %Q);", pkgId, fileSha.c_str());
471                         insertDB(tlc_db, sql);
472                         restore_library.push_back(fileSha);
473                         sqlite3_free(sql);
474                 }
475         }
476         return 0;
477 }
478
479 tac_error_e tlc_restoreDB()
480 {
481         if (!removeFile(TLC_APP_LIST_RESTORE_DB)) {
482                 fprintf(stderr, "Failed to remove of %s\n", TLC_APP_LIST_RESTORE_DB);
483                 return TAC_ERROR_UNKNOWN;
484         }
485
486         std::string dbRestoreJournal = TLC_APP_LIST_RESTORE_DB + std::string("-journal");
487         if (!removeFile(dbRestoreJournal)) {
488                 fprintf(stderr, "Failed to remove of %s\n", dbRestoreJournal.c_str());
489                 return TAC_ERROR_UNKNOWN;
490         }
491
492         tlc_db = createDB(TLC_APP_LIST_RESTORE_DB, CREATE_TLC_DB_TABLE);
493         if (!tlc_db) {
494                 fprintf(stderr, "Sqlite create error\n");
495                 return TAC_ERROR_UNKNOWN;
496         }
497         sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL);
498
499         pkgmgrinfo_appinfo_metadata_filter_h handle;
500         int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
501         if (ret != PMINFO_R_OK) {
502                 return TAC_ERROR_UNKNOWN;
503         }
504
505         ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
506         if (ret != PMINFO_R_OK) {
507                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
508                 return TAC_ERROR_UNKNOWN;
509         }
510
511         ret = pkgmgrMDFilterForeach(handle, tlc_restoreDBCb, NULL);
512         if (ret != 0) {
513                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
514                 return TAC_ERROR_UNKNOWN;
515         }
516
517         pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
518
519         if (tlc_db) {
520                 closeDB(tlc_db);
521                 tlc_db = NULL;
522         }
523
524         const uid_t g_uid = 301; // app_fw
525         const gid_t g_gid = 301; // app_fw
526
527         if (!copyFile(TLC_APP_LIST_RESTORE_DB, TLC_APP_LIST_DB)) {
528                 fprintf(stderr, "Failed to copy of %s\n", TLC_APP_LIST_DB);
529                 return TAC_ERROR_UNKNOWN;
530         }
531         if (!removeFile(TLC_APP_LIST_RESTORE_DB)) {
532                 fprintf(stderr, "Failed to remove of %s\n", TLC_APP_LIST_RESTORE_DB);
533                 return TAC_ERROR_UNKNOWN;
534         }
535         if (chown(TLC_APP_LIST_DB, g_uid, g_gid) == -1) {
536                 fprintf(stderr, "Failed to change owner and group name\n");
537         }
538
539         std::string dbJournal = TLC_APP_LIST_DB + std::string("-journal");
540         if (!copyFile(dbRestoreJournal, dbJournal)) {
541                 fprintf(stderr, "Failed to copy of %s\n", dbJournal.c_str());
542                 return TAC_ERROR_UNKNOWN;
543         }
544         if (!removeFile(dbRestoreJournal)) {
545                 fprintf(stderr, "Failed to remove of %s\n", dbRestoreJournal.c_str());
546                 return TAC_ERROR_UNKNOWN;
547         }
548         if (chown(dbJournal.c_str(), g_uid, g_gid) == -1) {
549                 fprintf(stderr, "Failed to change owner and group name\n");
550         }
551
552         auto convert = [](const std::string& path, const std::string& filename) {
553                 bool isExist = false;
554                 for (auto& library : restore_library) {
555                         if (!strcmp(filename.c_str(), library.c_str())) {
556                                 isExist = true;
557                                 break;
558                         }
559                 }
560                 if (!isExist) {
561                         if (!removeFile(path)) {
562                                 _ERR("Failed to remove of %s", path.c_str());
563                         }
564                 }
565         };
566
567         scanFilesInDirectory(concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR), convert, 0);
568
569         return TAC_ERROR_NONE;
570 }