Added logic to recover when db file is corrupted.
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / ni_common.cc
1 /*
2  * Copyright (c) 2016 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 <pkgmgr-info.h>
18 #include <pkgmgr_installer_info.h>
19 #include <aul.h>
20
21 #include "log.h"
22 #include "utils.h"
23 #include "pkgmgr_parser_plugin_interface.h"
24
25 #include <wait.h>
26 #include <dirent.h>
27 #include <sys/stat.h>
28
29 #include <algorithm>
30 #include <string>
31 #include <fstream>
32
33 #include <pwd.h>
34 #include <grp.h>
35 #include <unistd.h>
36 #include <string.h>
37
38 #include "ni_common.h"
39 #include "tac_common.h"
40 #include "path_manager.h"
41 #include "plugin_manager.h"
42
43 #ifdef  LOG_TAG
44 #undef  LOG_TAG
45 #endif
46 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
47
48 #ifndef CROSSGEN_PATH
49 #error "CROSSGEN_PATH is missed"
50 #endif
51
52 #define __XSTR(x) #x
53 #define __STR(x) __XSTR(x)
54 static const char* __CROSSGEN_PATH = __STR(CROSSGEN_PATH);
55 static const char* __TAC_DIR = __STR(TAC_DIR);
56
57 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
58 static const char* __SYSTEM_BASE_FILE = __STR(SYSTEM_BASE_FILE);
59 #endif
60
61 #undef __STR
62 #undef __XSTR
63
64 static int __interval = 0;
65 static std::string __tpa;
66
67 static void waitInterval()
68 {
69         // by the recommand, ignore small value for performance.
70         if (__interval > 10000) {
71                 fprintf(stderr, "sleep %d usec\n", __interval);
72                 usleep(__interval);
73         }
74 }
75
76 static std::string getNiFilePath(const std::string& dllPath)
77 {
78         size_t index = dllPath.find_last_of(".");
79         if (index == std::string::npos) {
80                 fprintf(stderr, "File doesnot contain extension. fail to get NI file name\n");
81                 return "";
82         }
83         std::string fName = dllPath.substr(0, index);
84         std::string fExt = dllPath.substr(index, dllPath.length());
85
86         // crossgen generate file with lower case extension only
87         std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
88         std::string niPath = fName + ".ni" + fExt;
89
90         return niPath;
91 }
92
93 static std::string getAppNIPath(const std::string& niPath)
94 {
95         std::string fileName;
96         std::string niDirPath;
97         std::string prevPath;
98
99         size_t index = niPath.find_last_of("/");
100         if (index != std::string::npos) {
101                 prevPath = niPath.substr(0, index);
102                 fileName = niPath.substr(index + 1, niPath.length());
103         } else {
104                 prevPath = ".";
105                 fileName = niPath;
106         }
107
108         niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
109
110         if (!isFileExist(niDirPath)) {
111                 if (mkdir(niDirPath.c_str(), 0755) == 0) {
112                         updateAssemblyInfo(prevPath, niDirPath);
113                 } else {
114                         fprintf(stderr, "Fail to create app ni directory (%s)\n", niDirPath.c_str());
115                 }
116         }
117
118         return concatPath(niDirPath, fileName);
119 }
120
121 static bool niExist(const std::string& path)
122 {
123         std::string f = getNiFilePath(path);
124         if (f.empty()) {
125                 return false;
126         }
127
128         if (isFileExist(f)) {
129                 return true;
130         }
131
132         // native image of System.Private.CoreLib.dll should have to overwrite
133         // original file to support new coreclr
134         if (path.find("System.Private.CoreLib.dll") != std::string::npos) {
135                 std::string coreLibBackup = path + ".Backup";
136                 if (isFileExist(coreLibBackup)) {
137                         return true;
138                 }
139         }
140
141         return false;
142 }
143
144 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
145 // Get next base address to be used for system ni image from file
146 // __SYSTEM_BASE_FILE should be checked for existance before calling this function
147 static uintptr_t getNextBaseAddrFromFile()
148 {
149         FILE *pFile = fopen(__SYSTEM_BASE_FILE, "r");
150         if (pFile == NULL) {
151                 fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
152                 return 0;
153         }
154
155         uintptr_t addr = 0;
156         uintptr_t size = 0;
157
158         while (fscanf(pFile, "%u %u", &addr, &size) != EOF) {
159         }
160
161         fclose(pFile);
162
163         return addr + size;
164 }
165
166 // Get next base address to be used for system ni image
167 static uintptr_t getNextBaseAddr()
168 {
169         uintptr_t baseAddr = 0;
170
171         if (!isFileExist(__SYSTEM_BASE_FILE)) {
172                 // This is the starting address for all default base addresses
173                 baseAddr = DEFAULT_BASE_ADDR_START;
174         } else {
175                 baseAddr = getNextBaseAddrFromFile();
176
177                 // Round to a multple of 64K (see ZapImage::CalculateZapBaseAddress in CoreCLR)
178                 uintptr_t BASE_ADDRESS_ALIGNMENT = 0xffff;
179                 baseAddr = (baseAddr + BASE_ADDRESS_ALIGNMENT) & ~BASE_ADDRESS_ALIGNMENT;
180         }
181
182         return baseAddr;
183 }
184
185 // Save base address of system ni image to file
186 static void updateBaseAddrFile(const std::string &absNiPath, uintptr_t baseAddr)
187 {
188         uintptr_t niSize = getFileSize(absNiPath);
189         if (niSize == 0) {
190                 fprintf(stderr, "File %s doesn't exist\n", absNiPath.c_str());
191                 return;
192         }
193
194         // Write new entry to the file
195         FILE *pFile = fopen(__SYSTEM_BASE_FILE, "a");
196         if (pFile == NULL) {
197                 fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
198                 return;
199         }
200
201         fprintf(pFile, "%u %u\n", baseAddr, niSize);
202         fclose(pFile);
203 }
204
205 // check if dll is listed in TPA
206 static bool isTPADll(const std::string &dllPath)
207 {
208         std::string absDllPath = absolutePath(dllPath);
209
210         if (__tpa.find(absDllPath) != std::string::npos) {
211                 return true;
212         }
213
214         return false;
215 }
216 #endif
217
218 // baseAddr should be checked in file before getting here
219 static ni_error_e crossgen(const std::string& dllPath, const std::string& appPath, DWORD flags)
220 {
221         if (!isFileExist(dllPath)) {
222                 fprintf(stderr, "dll file is not exist : %s\n", dllPath.c_str());
223                 return NI_ERROR_NO_SUCH_FILE;
224         }
225
226         if (!isManagedAssembly(dllPath)) {
227                 fprintf(stderr, "Input file is not a dll file : %s\n", dllPath.c_str());
228                 return NI_ERROR_INVALID_PARAMETER;
229         }
230
231         if (niExist(dllPath)) {
232                 fprintf(stderr, "Already ni file is exist for %s\n", dllPath.c_str());
233                 return NI_ERROR_ALREADY_EXIST;
234         }
235
236         std::string absDllPath = absolutePath(dllPath);
237         std::string absNiPath = getNiFilePath(dllPath);
238         if (absNiPath.empty()) {
239                 fprintf(stderr, "Fail to get ni file name\n");
240                 return NI_ERROR_UNKNOWN;
241         }
242
243         bool isAppNI = flags & NI_FLAGS_APPNI;
244         if (isAppNI && strstr(absNiPath.c_str(), __TAC_DIR) == NULL) {
245                 absNiPath = getAppNIPath(absNiPath);
246         }
247
248 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
249         uintptr_t baseAddr = 0;
250
251         if (isTPADll(dllPath)) {
252                 baseAddr = getNextBaseAddr();
253         }
254 #endif
255
256         pid_t pid = fork();
257         if (pid == -1)
258                 return NI_ERROR_UNKNOWN;
259
260         if (pid > 0) {
261                 int status;
262                 waitpid(pid, &status, 0);
263                 if (WIFEXITED(status)) {
264                         // Do not use niExist() function to check whether ni file created or not.
265                         // niEixst() return false for System.Private.Corelib.dll
266                         if (isFileExist(absNiPath)) {
267                                 updateAssemblyInfo(absDllPath, absNiPath);
268 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
269                                 if (baseAddr != 0) {
270                                         updateBaseAddrFile(absNiPath, baseAddr);
271                                 }
272 #endif
273                                 return NI_ERROR_NONE;
274                         } else {
275                                 fprintf(stderr, "Fail to create native image for %s\n", dllPath.c_str());
276                                 return NI_ERROR_NO_SUCH_FILE;
277                         }
278                 }
279         } else {
280                 std::string jitPath = getRuntimeDir() + "/libclrjit.so";
281                 std::vector<const char*> argv = {
282                         __CROSSGEN_PATH,
283                         "/nologo",
284                         "/JITPath", jitPath.c_str()
285                 };
286
287                 bool compat = flags & NI_FLAGS_COMPATIBILITY;
288                 argv.push_back(compat ? "/Trusted_Platform_Assemblies" : "/r");
289                 argv.push_back(__tpa.c_str());
290
291                 bool enableR2R = flags & NI_FLAGS_ENABLER2R;
292                 if (!enableR2R) {
293                         argv.push_back("/FragileNonVersionable");
294                 }
295
296                 if (flags & NI_FLAGS_VERBOSE) {
297                         argv.push_back("/verbose");
298                 }
299
300                 if (flags & NI_FLAGS_INSTRUMENT) {
301                         argv.push_back("/Tuning");
302                 }
303
304 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
305                 if (baseAddr != 0) {
306                         argv.push_back("/BaseAddress");
307                         argv.push_back(std::to_string(baseAddr).c_str());
308                 }
309 #endif
310
311                 argv.push_back("/App_Paths");
312                 std::string absAppPath;
313                 if (!appPath.empty()) {
314                         absAppPath = appPath;
315                 } else {
316                         absAppPath = baseName(absDllPath);
317                 }
318                 argv.push_back(absAppPath.c_str());
319
320                 argv.push_back("/out");
321                 argv.push_back(absNiPath.c_str());
322
323                 argv.push_back(absDllPath.c_str());
324                 argv.push_back(nullptr);
325
326                 fprintf(stderr, "+ %s (%s)\n", absDllPath.c_str(), enableR2R ? "R2R" : "FNV");
327
328                 execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
329                 exit(0);
330         }
331
332         return NI_ERROR_NONE;
333 }
334
335 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
336 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
337 {
338         char *pkgId = NULL;
339         int ret = 0;
340         DWORD *pFlags = (DWORD*)userData;
341
342         ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
343         if (ret != PMINFO_R_OK) {
344                 fprintf(stderr, "Failed to get pkgid\n");
345                 return -1;
346         }
347
348         if (removeNiUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
349                 fprintf(stderr, "Failed to remove previous dlls from [%s]\n", pkgId);
350                 return -1;
351         }
352
353         if (resetTACPackage(pkgId) != TAC_ERROR_NONE) {
354                 fprintf(stderr, "Failed to remove symlink for given package [%s]\n", pkgId);
355                 return -1;
356         }
357
358         // Regenerate ni files with R2R mode forcibiliy. (there is no way to now which option is used)
359         if (createNiUnderPkgRoot(pkgId, *pFlags) != NI_ERROR_NONE) {
360                 fprintf(stderr, "Failed to generate NI file [%s]\n", pkgId);
361                 return -1;
362         } else {
363                 fprintf(stderr, "Complete make application to native image\n");
364         }
365
366         if (createTACPkgRoot(pkgId, *pFlags) != NI_ERROR_NONE) {
367                 fprintf(stderr, "Failed to generate symbolic link file [%s]\n", pkgId);
368                 return -1;
369         }else {
370                 fprintf(stderr, "Complete make symbolic link file to tac\n");
371         }
372
373         return 0;
374 }
375
376 static void createCoreLibNI(DWORD flags)
377 {
378         std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
379         std::string niCoreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.ni.dll");
380         std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
381
382         if (!isFileExist(coreLibBackup)) {
383
384                 if (!crossgen(coreLib, std::string(), flags)) {
385                         if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
386                                 fprintf(stderr, "Failed to rename System.Private.CoreLib.dll\n");
387                         }
388                         if (rename(niCoreLib.c_str(), coreLib.c_str())) {
389                                 fprintf(stderr, "Failed to rename System.Private.CoreLib.ni.dll\n");
390                         }
391                 } else {
392                         fprintf(stderr, "Failed to create native image for %s\n", coreLib.c_str());
393                 }
394         }
395 }
396
397 ni_error_e initNICommon(NiCommonOption* option)
398 {
399 #if defined(__arm__)
400         // get interval value
401         const char* intervalFile = "/usr/share/dotnet.tizen/lib/crossgen_interval.txt";
402         std::ifstream inFile(intervalFile);
403         if (inFile) {
404                 fprintf(stderr, "crossgen_interval.txt is found\n");
405                 inFile >> __interval;
406         }
407
408         if (initializePluginManager("normal")) {
409                 fprintf(stderr, "Fail to initialize PluginManager\n");
410                 return NI_ERROR_UNKNOWN;
411         }
412         if (initializePathManager(option->runtimeDir, option->tizenFXDir, option->extraDirs)) {
413                 fprintf(stderr, "Fail to initialize PathManager\n");
414                 return NI_ERROR_UNKNOWN;
415         }
416
417         __tpa = getTPA();
418
419         return NI_ERROR_NONE;
420 #else
421         fprintf(stderr, "crossgen supports arm architecture only. skip ni file generation\n");
422         return NI_ERROR_NOT_SUPPORTED;
423 #endif
424 }
425
426 void finalizeNICommon()
427 {
428         __interval = 0;
429
430         finalizePluginManager();
431         finalizePathManager();
432
433         __tpa.clear();
434 }
435
436
437 void createNiPlatform(DWORD flags)
438 {
439         const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
440         createNiUnderDirs(platformDirs, 2, flags);
441 }
442
443 ni_error_e createNiDll(const std::string& dllPath, DWORD flags)
444 {
445         createCoreLibNI(flags);
446         // System.Private.CoreLib.dll is generated in the createCoreLibNI function.
447         // Skip if input dll is System.Private.CoreLib.dll
448         if (dllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
449                 return NI_ERROR_NONE;
450         }
451
452         ni_error_e status = crossgen(dllPath, std::string(), flags);
453
454         return status;
455 }
456
457 void createNiUnderTAC(const std::string rootPaths[], int count, DWORD flags)
458 {
459         std::string appPaths;
460         try {
461                 for (auto& nuget : bf::recursive_directory_iterator(__TAC_DIR)) {
462                         std::string nugetPath = nuget.path().string();
463                         if (bf::is_directory(nugetPath)) {
464                                 appPaths += nugetPath;
465                                 appPaths += ':';
466                         }
467                 }
468                 if (appPaths.back() == ':') {
469                         appPaths.pop_back();
470                 }
471
472                 auto convert = [&appPaths, flags](const std::string& path, const char* name) {
473                         if (strstr(path.c_str(), TAC_APP_LIST_DB) != NULL ||
474                                 strstr(path.c_str(), TAC_APP_LIST_RESTORE_DB) != NULL ||
475                                 strstr(path.c_str(), TAC_SHA_256_INFO) != NULL)
476                                 return;
477                         if (!crossgen(path, appPaths.c_str(), flags)) {
478                                 waitInterval();
479                         }
480                 };
481
482                 for (int i = 0; i < count; i++) {
483                         scanFilesInDir(rootPaths[i], convert, -1);
484                 }
485         } catch (const bf::filesystem_error& error) {
486                 fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
487                 return;
488         }
489 }
490
491 ni_error_e createTACPkgRoot(const std::string& pkgId, DWORD flags)
492 {
493         std::string pkgRoot;
494         if (getRootPath(pkgId, pkgRoot) < 0) {
495                 return NI_ERROR_INVALID_PACKAGE;
496         }
497
498         std::string binDir = concatPath(pkgRoot, "bin");
499         std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
500         std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
501         if (bf::exists(tacDir)) {
502                 try {
503                         for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
504                                 std::string symPath = symlinkAssembly.path().string();
505                                 if (bf::is_symlink(symPath)) {
506                                         if (!isNativeImage(symPath)) {
507                                                 std::string originPath = bf::read_symlink(symPath).string();
508                                                 std::string originNiPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
509                                                 if (!bf::exists(originNiPath)) {
510                                                         if(createNiDll(originPath, flags) != NI_ERROR_NONE) {
511                                                                 fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str());
512                                                                 return NI_ERROR_UNKNOWN;
513                                                         }
514                                                 }
515                                                 std::string symNIPath = symPath.substr(0, symPath.rfind(".dll")) + ".ni.dll";
516                                                 if (!bf::exists(symNIPath)) {
517                                                         bf::create_symlink(originNiPath, symNIPath);
518                                                         fprintf(stderr, "%s symbolic link file generated successfully.\n", symNIPath.c_str());
519                                                         updateAssemblyInfo(tacDir.c_str(), symNIPath.c_str(), true);
520
521                                                         std::string NIFileName = symNIPath.substr(symNIPath.rfind('/') + 1);
522                                                         if (!removeFile(concatPath(binNIDir, NIFileName))) {
523                                                                 fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, NIFileName).c_str());
524                                                                 return NI_ERROR_UNKNOWN;
525                                                         }
526                                                 }
527                                         }
528                                 }
529                         }
530                 } catch (const bf::filesystem_error& error) {
531                         fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
532                         return NI_ERROR_UNKNOWN;
533                 }
534         }
535         return NI_ERROR_NONE;
536 }
537
538 void createNiUnderDirs(const std::string rootPaths[], int count, DWORD flags)
539 {
540         createCoreLibNI(flags);
541
542         std::string appPaths;
543         for (int i = 0; i < count; i++) {
544                 appPaths += rootPaths[i];
545                 appPaths += ':';
546         }
547
548         if (appPaths.back() == ':')
549                 appPaths.pop_back();
550
551         std::vector<std::string> tpaAssemblies;
552         splitPath(__tpa, tpaAssemblies);
553
554         auto convert = [&appPaths, flags, tpaAssemblies](const std::string& path, const char* name) {
555                 bool isAppNI = flags & NI_FLAGS_APPNI;
556                 if (isAppNI) {
557                         std::string assembly = path.substr(path.rfind('/') + 1);
558                         bool isExist = false;
559                         for (auto& tpa : tpaAssemblies) {
560                                 if (!strcmp(replaceAll(tpa, ".ni.dll", ".dll").c_str(), assembly.c_str())) {
561                                         isExist = true;
562                                         break;
563                                 }
564                         }
565                         if (isExist) {
566                                 fprintf(stderr, "%s present in the TPA list skips generation of NI file.\n", path.c_str());
567                                 return;
568                         }
569                 }
570                 if (!crossgen(path, appPaths.c_str(), flags)) {
571                         waitInterval();
572                 }
573         };
574
575         for (int i = 0; i < count; i++) {
576                 scanFilesInDir(rootPaths[i], convert, 1);
577         }
578
579         tpaAssemblies.clear();
580 }
581
582 ni_error_e createNiUnderPkgRoot(const std::string& pkgId, DWORD flags)
583 {
584         std::string pkgRoot;
585         if (getRootPath(pkgId, pkgRoot) < 0) {
586                 return NI_ERROR_INVALID_PACKAGE;
587         }
588
589         std::string binDir = concatPath(pkgRoot, "bin");
590         std::string libDir = concatPath(pkgRoot, "lib");
591         std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
592         std::string paths[] = {binDir, libDir, tacDir};
593
594         flags |= NI_FLAGS_APPNI;
595         createNiUnderDirs(paths, 3, flags);
596
597         return NI_ERROR_NONE;
598 }
599
600 ni_error_e createNiDllUnderPkgRoot(const std::string& pkgId, const std::string& dllPath, DWORD flags)
601 {
602         std::string pkgRoot;
603         if (getRootPath(pkgId, pkgRoot) < 0) {
604                 return NI_ERROR_INVALID_PACKAGE;
605         }
606
607         std::string binDir = concatPath(pkgRoot, "bin");
608         std::string libDir = concatPath(pkgRoot, "lib");
609         std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
610         std::string paths = binDir + ":" + libDir + ":" + tacDir;
611
612         if (bf::is_symlink(dllPath)) {
613                 if (bf::exists(tacDir)) {
614                         if (!isNativeImage(dllPath)) {
615                                 std::string originPath = bf::read_symlink(dllPath).string();
616                                 std::string originNiPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
617                                 if (!bf::exists(originNiPath)) {
618                                         if(createNiDll(originPath, flags) != NI_ERROR_NONE) {
619                                                 fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str());
620                                                 return NI_ERROR_UNKNOWN;
621                                         }
622                                 }
623                                 std::string setNiPath = dllPath.substr(0, dllPath.rfind(".dll")) + ".ni.dll";
624                                 if (!bf::exists(setNiPath)) {
625                                         bf::create_symlink(originNiPath, setNiPath);
626                                         fprintf(stderr, "%s symbolic link file generated successfully.\n", setNiPath.c_str());
627                                         updateAssemblyInfo(tacDir.c_str(), setNiPath.c_str(), true);
628                                 }
629                         }
630                 }
631                 return NI_ERROR_NONE;
632         } else {
633                 std::string assembly = dllPath.substr(dllPath.rfind('/') + 1);
634                 std::vector<std::string> tpaAssemblies;
635                 splitPath(__tpa, tpaAssemblies);
636                 bool isExist = false;
637                 for (auto& tpa : tpaAssemblies) {
638                         if (!strcmp(replaceAll(tpa, ".ni.dll", ".dll").c_str(), assembly.c_str())) {
639                                 isExist = true;
640                                 break;
641                         }
642                 }
643                 tpaAssemblies.clear();
644                 if (isExist) {
645                         fprintf(stderr, "%s present in the TPA list skips generation of NI file.\n", dllPath.c_str());
646                         return NI_ERROR_NONE;
647                 } else {
648                         flags |= NI_FLAGS_APPNI;
649                         return crossgen(dllPath, paths, flags);
650                 }
651         }
652 }
653
654 void removeNiPlatform()
655 {
656         std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
657         std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
658
659         if (!isFileExist(coreLibBackup)) {
660                 return;
661         }
662
663 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
664         if (isFileExist(__SYSTEM_BASE_FILE)) {
665                 if (remove(__SYSTEM_BASE_FILE)) {
666                         fprintf(stderr, "Failed to remove %s\n", __SYSTEM_BASE_FILE);
667                 }
668         }
669 #endif
670
671         if (remove(coreLib.c_str())) {
672                 fprintf(stderr, "Failed to remove System.Private.CoreLib native image file\n");
673         }
674
675         if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
676                 fprintf(stderr, "Failed to rename System.Private.CoreLib.Backup to origin\n");
677         }
678
679         const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
680
681         removeNiUnderDirs(platformDirs, 2);
682 }
683
684 void removeNiUnderDirs(const std::string rootPaths[], int count)
685 {
686         auto convert = [](const std::string& path, std::string name) {
687                 if (isNativeImage(path)) {
688                         if (remove(path.c_str())) {
689                                 fprintf(stderr, "Failed to remove %s\n", path.c_str());
690                         }
691                 }
692         };
693
694         for (int i = 0; i < count; i++)
695                 scanFilesInDir(rootPaths[i], convert, -1);
696 }
697
698 ni_error_e removeNiUnderPkgRoot(const std::string& pkgId)
699 {
700         std::string pkgRoot;
701         if (getRootPath(pkgId, pkgRoot) < 0) {
702                 return NI_ERROR_INVALID_PACKAGE;
703         }
704
705         std::string binDir = concatPath(pkgRoot, "bin");
706         std::string libDir = concatPath(pkgRoot, "lib");
707         std::string paths[] = {binDir, libDir};
708
709         removeNiUnderDirs(paths, 2);
710
711         std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
712         if (isFileExist(binNIDir)) {
713                 if (rmdir(binNIDir.c_str()) != 0) {
714                         fprintf(stderr, "Failed to remove app ni dir [%s]\n", binNIDir.c_str());
715                 }
716         }
717
718         std::string libNIDir = concatPath(libDir, APP_NI_SUB_DIR);
719         if (isFileExist(libNIDir)) {
720                 if (rmdir(libNIDir.c_str()) != 0) {
721                         fprintf(stderr, "Failed to remove app ni dir [%s]\n", libNIDir.c_str());
722                 }
723         }
724
725         return NI_ERROR_NONE;
726 }
727
728 ni_error_e regenerateAppNI(DWORD flags)
729 {
730         int ret = 0;
731         pkgmgrinfo_appinfo_metadata_filter_h handle;
732
733         ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
734         if (ret != PMINFO_R_OK)
735                 return NI_ERROR_UNKNOWN;
736
737         ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE);
738         if (ret != PMINFO_R_OK) {
739                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
740                 return NI_ERROR_UNKNOWN;
741         }
742
743         ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, appAotCb, &flags);
744         if (ret != PMINFO_R_OK) {
745                 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
746                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
747                 return NI_ERROR_UNKNOWN;
748         }
749
750         fprintf(stderr, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
751
752         pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
753         return NI_ERROR_NONE;
754 }
755
756 ni_error_e regenerateTACNI(DWORD flags)
757 {
758         const std::string tacDir[] = {__TAC_DIR};
759         removeNiUnderDirs(tacDir, 1);
760         createNiUnderTAC(tacDir, 1, flags);
761         return NI_ERROR_NONE;
762 }