2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <pkgmgr-info.h>
18 #include <pkgmgr_installer_info.h>
23 #include "pkgmgr_parser_plugin_interface.h"
39 #include "ni_common.h"
40 #include "db_manager.h"
41 #include "tac_common.h"
42 #include "path_manager.h"
43 #include "plugin_manager.h"
48 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
51 #error "CROSSGEN_PATH is missed"
55 #define __STR(x) __XSTR(x)
56 static const char* __NATIVE_LIB_DIR = __STR(NATIVE_LIB_DIR);
57 static const char* __CROSSGEN_PATH = __STR(CROSSGEN_PATH);
58 static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
60 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
61 static const char* __SYSTEM_BASE_FILE = __STR(SYSTEM_BASE_FILE);
67 static int __interval = 0;
68 static std::string __tpa;
70 static void waitInterval()
72 // by the recommand, ignore small value for performance.
73 if (__interval > 10000) {
74 fprintf(stdout, "sleep %d usec\n", __interval);
79 static std::string getNIFilePath(const std::string& dllPath)
81 size_t index = dllPath.find_last_of(".");
82 if (index == std::string::npos) {
83 fprintf(stderr, "File doesnot contain extension. fail to get NI file name\n");
86 std::string fName = dllPath.substr(0, index);
87 std::string fExt = dllPath.substr(index, dllPath.length());
89 // crossgen generate file with lower case extension only
90 std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
91 std::string niPath = fName + ".ni" + fExt;
96 static std::string getAppNIPath(const std::string& niPath)
99 std::string niDirPath;
100 std::string prevPath;
102 size_t index = niPath.find_last_of("/");
103 if (index != std::string::npos) {
104 prevPath = niPath.substr(0, index);
105 fileName = niPath.substr(index + 1, niPath.length());
111 niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
113 if (!isFile(niDirPath)) {
114 if (mkdir(niDirPath.c_str(), 0755) == 0) {
115 copySmackAndOwnership(prevPath, niDirPath);
117 fprintf(stderr, "Fail to create app ni directory (%s)\n", niDirPath.c_str());
121 return concatPath(niDirPath, fileName);
124 static bool checkNIExistence(const std::string& path)
126 std::string f = getNIFilePath(path);
135 // native image of System.Private.CoreLib.dll should have to overwrite
136 // original file to support new coreclr
137 if (path.find("System.Private.CoreLib.dll") != std::string::npos) {
138 std::string coreLibBackup = path + ".Backup";
139 if (isFile(coreLibBackup)) {
147 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
148 static uintptr_t getFileSize(const std::string& path)
152 if (stat(path.c_str(), &sb) == 0) {
159 // Get next base address to be used for system ni image from file
160 // __SYSTEM_BASE_FILE should be checked for existance before calling this function
161 static uintptr_t getNextBaseAddrFromFile()
163 FILE *pFile = fopen(__SYSTEM_BASE_FILE, "r");
165 fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
172 while (fscanf(pFile, "%u %u", &addr, &size) != EOF) {
180 // Get next base address to be used for system ni image
181 static uintptr_t getNextBaseAddr()
183 uintptr_t baseAddr = 0;
185 if (!isFile(__SYSTEM_BASE_FILE)) {
186 // This is the starting address for all default base addresses
187 baseAddr = DEFAULT_BASE_ADDR_START;
189 baseAddr = getNextBaseAddrFromFile();
191 // Round to a multple of 64K (see ZapImage::CalculateZapBaseAddress in CoreCLR)
192 uintptr_t BASE_ADDRESS_ALIGNMENT = 0xffff;
193 baseAddr = (baseAddr + BASE_ADDRESS_ALIGNMENT) & ~BASE_ADDRESS_ALIGNMENT;
199 // Save base address of system ni image to file
200 static void updateBaseAddrFile(const std::string& absNIPath, uintptr_t baseAddr)
202 uintptr_t niSize = getFileSize(absNIPath);
204 fprintf(stderr, "File %s doesn't exist\n", absNIPath.c_str());
208 // Write new entry to the file
209 FILE *pFile = fopen(__SYSTEM_BASE_FILE, "a");
211 fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
215 fprintf(pFile, "%u %u\n", baseAddr, niSize);
219 // check if dll is listed in TPA
220 static bool isTPADll(const std::string& dllPath)
222 std::string absDllPath = getAbsolutePath(dllPath);
224 if (__tpa.find(absDllPath) != std::string::npos) {
232 // baseAddr should be checked in file before getting here
233 static ni_error_e crossgen(const std::string& dllPath, const std::string& appPath, DWORD flags)
235 if (!isFile(dllPath)) {
236 fprintf(stderr, "dll file is not exist : %s\n", dllPath.c_str());
237 return NI_ERROR_NO_SUCH_FILE;
240 if (!isManagedAssembly(dllPath)) {
241 //fprintf(stderr, "Input file is not a dll file : %s\n", dllPath.c_str());
242 return NI_ERROR_INVALID_PARAMETER;
245 if (checkNIExistence(dllPath)) {
246 fprintf(stderr, "Already ni file is exist for %s\n", dllPath.c_str());
247 return NI_ERROR_ALREADY_EXIST;
250 std::string absDllPath = getAbsolutePath(dllPath);
251 std::string absNIPath = getNIFilePath(dllPath);
254 if (absNIPath.empty()) {
255 fprintf(stderr, "Fail to get ni file name\n");
256 return NI_ERROR_UNKNOWN;
259 bool isAppNI = flags & NI_FLAGS_APPNI;
260 if (isAppNI && strstr(absNIPath.c_str(), __DOTNET_DIR) == NULL) {
261 absNIPath = getAppNIPath(absNIPath);
264 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
265 uintptr_t baseAddr = 0;
267 if (isTPADll(dllPath)) {
268 baseAddr = getNextBaseAddr();
274 return NI_ERROR_UNKNOWN;
278 waitpid(pid, &status, 0);
279 if (WIFEXITED(status)) {
280 // Do not use checkNIExistence() function to check whether ni file created or not.
281 // checkNIExistence() return false for System.Private.Corelib.dll
282 if (isFile(absNIPath)) {
283 copySmackAndOwnership(absDllPath, absNIPath);
284 std::string absPdbPath = replaceAll(absDllPath, ".dll", ".pdb");
285 std::string pdbFilePath = replaceAll(absNIPath, ".ni.dll", ".pdb");
286 if (isFile(absPdbPath) && (absPdbPath != pdbFilePath)) {
287 if (!copyFile(absPdbPath, pdbFilePath)) {
288 fprintf(stderr, "Failed to copy a .pdb file\n");
290 copySmackAndOwnership(absPdbPath, pdbFilePath);
293 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
295 updateBaseAddrFile(absNIPath, baseAddr);
298 return NI_ERROR_NONE;
300 fprintf(stderr, "Fail to create native image for %s\n", dllPath.c_str());
301 return NI_ERROR_NO_SUCH_FILE;
305 std::string jitPath = getRuntimeDir() + "/libclrjit.so";
306 std::vector<const char*> argv = {
309 "/JITPath", jitPath.c_str()
312 bool compat = flags & NI_FLAGS_COMPATIBILITY;
313 argv.push_back(compat ? "/Trusted_Platform_Assemblies" : "/r");
314 argv.push_back(__tpa.c_str());
316 bool enableR2R = flags & NI_FLAGS_ENABLER2R;
318 argv.push_back("/FragileNonVersionable");
321 if (flags & NI_FLAGS_VERBOSE) {
322 argv.push_back("/verbose");
325 if (flags & NI_FLAGS_INSTRUMENT) {
326 argv.push_back("/Tuning");
329 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
331 argv.push_back("/BaseAddress");
332 argv.push_back(std::to_string(baseAddr).c_str());
336 argv.push_back("/App_Paths");
337 std::string absAppPath;
338 if (!appPath.empty()) {
339 absAppPath = appPath;
341 absAppPath = getBaseName(absDllPath);
343 argv.push_back(absAppPath.c_str());
345 argv.push_back("/out");
346 argv.push_back(absNIPath.c_str());
348 argv.push_back(absDllPath.c_str());
349 argv.push_back(nullptr);
351 fprintf(stdout, "+ %s (%s)\n", absDllPath.c_str(), enableR2R ? "R2R" : "FNV");
353 execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
357 return NI_ERROR_NONE;
360 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
361 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
365 DWORD *pFlags = (DWORD*)userData;
367 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
368 if (ret != PMINFO_R_OK) {
369 fprintf(stderr, "Failed to get pkgid\n");
373 if (removeNIUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
374 fprintf(stderr, "Failed to remove previous dlls from [%s]\n", pkgId);
378 if (resetTACPackage(pkgId) != TAC_ERROR_NONE) {
379 fprintf(stderr, "Failed to remove symlink for given package [%s]\n", pkgId);
383 // Regenerate ni files with R2R mode forcibiliy. (there is no way to now which option is used)
384 if (createNIUnderPkgRoot(pkgId, *pFlags) != NI_ERROR_NONE) {
385 fprintf(stderr, "Failed to generate NI file [%s]\n", pkgId);
388 fprintf(stdout, "Complete make application to native image\n");
391 if (createTACPkgRoot(pkgId, *pFlags) != NI_ERROR_NONE) {
392 fprintf(stderr, "Failed to generate symbolic link file [%s]\n", pkgId);
395 fprintf(stdout, "Complete make symbolic link file to tac\n");
401 static bool isCoreLibPrepared(DWORD flags)
403 if (flags & NI_FLAGS_ENABLER2R) {
407 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
408 if (isFile(coreLibBackup)) {
411 fprintf(stderr, "The native image of System.Private.CoreLib does not exist\n"
412 "Run the command to create the native image\n"
413 "# dotnettool --ni-dll /usr/share/dotnet.tizen/netcoreapp/System.Private.CoreLib.dll\n\n");
418 static bool hasCoreLibNI()
422 std::string ildasm = concatPath(getRuntimeDir(), "ildasm");
423 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
424 std::string cmd = ildasm + " " + coreLib + " | grep '\\.corflags'";
425 fp = popen(cmd.c_str(), "r");
427 while (fgets(buff, sizeof(buff), fp) != NULL) {
428 buff[strlen(buff) - 1] = '\0';
430 std::string corflag = replaceAll(buff, ".corflags", "");
431 corflag.erase(std::remove(corflag.begin(), corflag.end(), ' '), corflag.end());
432 // CorFlags.ILLibrary=0x00000004 (.ni.dll)
433 if (!strcmp(corflag.substr(0, 10).c_str(), "0x00000004")) {
442 static ni_error_e createCoreLibNI(DWORD flags)
444 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
445 std::string niCoreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.ni.dll");
446 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
448 if (!isFile(coreLibBackup) && !hasCoreLibNI()) {
449 if (!crossgen(coreLib, std::string(), flags)) {
450 if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
451 fprintf(stderr, "Failed to rename System.Private.CoreLib.dll\n");
452 return NI_ERROR_CORE_NI_FILE;
454 if (rename(niCoreLib.c_str(), coreLib.c_str())) {
455 fprintf(stderr, "Failed to rename System.Private.CoreLib.ni.dll\n");
456 return NI_ERROR_CORE_NI_FILE;
459 fprintf(stderr, "Failed to create native image for %s\n", coreLib.c_str());
460 return NI_ERROR_CORE_NI_FILE;
463 return NI_ERROR_NONE;
466 ni_error_e initNICommon(NICommonOption* option)
468 #if defined(__arm__) || defined(__aarch64__)
469 // get interval value
470 const static std::string intervalFile = concatPath(__NATIVE_LIB_DIR, "crossgen_interval.txt");
471 std::ifstream inFile(intervalFile);
473 fprintf(stdout, "crossgen_interval.txt is found\n");
474 inFile >> __interval;
477 if (initializePluginManager("normal")) {
478 fprintf(stderr, "Fail to initialize PluginManager\n");
479 return NI_ERROR_UNKNOWN;
481 if (initializePathManager(option->runtimeDir, option->tizenFXDir, option->extraDirs)) {
482 fprintf(stderr, "Fail to initialize PathManager\n");
483 return NI_ERROR_UNKNOWN;
488 return NI_ERROR_NONE;
490 fprintf(stderr, "crossgen supports arm/arm64 architecture only. skip ni file generation\n");
491 return NI_ERROR_NOT_SUPPORTED;
495 void finalizeNICommon()
499 finalizePluginManager();
500 finalizePathManager();
505 ni_error_e createNIPlatform(DWORD flags)
507 if (createCoreLibNI(flags) != NI_ERROR_NONE) {
508 return NI_ERROR_CORE_NI_FILE;
511 const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
512 return createNIUnderDirs(platformDirs, 2, flags);
515 ni_error_e createNIDll(const std::string& dllPath, DWORD flags)
517 if (dllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
518 return createCoreLibNI(flags);
521 if (!isCoreLibPrepared(flags)) {
522 return NI_ERROR_CORE_NI_FILE;
525 return crossgen(dllPath, std::string(), flags);
528 static void createNIUnderTAC(const std::vector<std::string>& nugets, DWORD flags)
530 std::string appPaths;
531 for (auto& nuget : nugets) {
532 appPaths += concatPath(__DOTNET_DIR, nuget);
535 if (appPaths.back() == ':') {
539 auto convert = [&appPaths, flags](const std::string& path, const std::string& filename) {
540 if (strstr(path.c_str(), TAC_SHA_256_INFO) != NULL)
542 if (!crossgen(path, appPaths.c_str(), flags)) {
546 for (auto& nuget : nugets) {
547 scanFilesInDirectory(concatPath(__DOTNET_DIR, nuget), convert, -1);
551 ni_error_e createTACPkgRoot(const std::string& pkgId, DWORD flags)
553 if (!isCoreLibPrepared(flags)) {
554 return NI_ERROR_CORE_NI_FILE;
557 std::string rootPath = getRootPath(pkgId);
558 if (rootPath.empty()) {
559 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
560 return NI_ERROR_INVALID_PACKAGE;
563 std::string binDir = concatPath(rootPath, "bin");
564 std::string libDir = concatPath(rootPath, "lib");
565 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
566 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
567 std::string paths = binDir + ":" + libDir + ":" + tacDir;
568 if (bf::exists(tacDir)) {
570 for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
571 std::string symPath = symlinkAssembly.path().string();
572 if (bf::is_symlink(symPath)) {
573 if (!isNativeImage(symPath)) {
574 std::string originPath = bf::read_symlink(symPath).string();
575 std::string originNIPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
576 if (!bf::exists(originNIPath)) {
577 flags |= NI_FLAGS_APPNI;
578 if (crossgen(originPath, paths, flags) != NI_ERROR_NONE) {
579 fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str());
580 return NI_ERROR_UNKNOWN;
583 std::string symNIPath = symPath.substr(0, symPath.rfind(".dll")) + ".ni.dll";
584 if (!bf::exists(symNIPath)) {
585 bf::create_symlink(originNIPath, symNIPath);
586 fprintf(stdout, "%s symbolic link file generated successfully.\n", symNIPath.c_str());
587 copySmackAndOwnership(tacDir.c_str(), symNIPath.c_str(), true);
589 std::string niFileName = symNIPath.substr(symNIPath.rfind('/') + 1);
590 if (!removeFile(concatPath(binNIDir, niFileName))) {
591 fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, niFileName).c_str());
597 } catch (const bf::filesystem_error& error) {
598 fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
599 return NI_ERROR_UNKNOWN;
602 return NI_ERROR_NONE;
605 ni_error_e createNIUnderDirs(const std::string rootPaths[], int count, DWORD flags)
607 if (!isCoreLibPrepared(flags)) {
608 return NI_ERROR_CORE_NI_FILE;
611 std::string appPaths;
612 for (int i = 0; i < count; i++) {
613 appPaths += rootPaths[i];
617 if (appPaths.back() == ':')
620 std::vector<std::string> tpaAssemblies;
621 splitPath(__tpa, tpaAssemblies);
623 auto convert = [&appPaths, flags, tpaAssemblies](const std::string& path, const std::string& filename) {
624 bool isAppNI = flags & NI_FLAGS_APPNI;
626 for (auto& tpa : tpaAssemblies) {
627 if (!strcmp(replaceAll(tpa.substr(tpa.rfind('/') + 1), ".ni.dll", ".dll").c_str(), filename.c_str())) {
628 fprintf(stderr, "%s present in the TPA list skips generation of NI file.\n", filename.c_str());
633 if (!crossgen(path, appPaths.c_str(), flags)) {
638 for (int i = 0; i < count; i++) {
639 scanFilesInDirectory(rootPaths[i], convert, 0);
642 tpaAssemblies.clear();
643 return NI_ERROR_NONE;
646 ni_error_e createNIUnderPkgRoot(const std::string& pkgId, DWORD flags)
648 std::string rootPath = getRootPath(pkgId);
649 if (rootPath.empty()) {
650 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
651 return NI_ERROR_INVALID_PACKAGE;
654 std::string binDir = concatPath(rootPath, "bin");
655 std::string libDir = concatPath(rootPath, "lib");
656 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
657 std::string paths[] = {binDir, libDir, tacDir};
659 flags |= NI_FLAGS_APPNI;
660 return createNIUnderDirs(paths, 3, flags);
663 ni_error_e createNIDllUnderPkgRoot(const std::string& pkgId, const std::string& dllPath, DWORD flags)
665 if (!isCoreLibPrepared(flags)) {
666 return NI_ERROR_CORE_NI_FILE;
669 std::string rootPath = getRootPath(pkgId);
670 if (rootPath.empty()) {
671 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
672 return NI_ERROR_INVALID_PACKAGE;
675 std::string binDir = concatPath(rootPath, "bin");
676 std::string libDir = concatPath(rootPath, "lib");
677 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
678 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
679 std::string paths = binDir + ":" + libDir + ":" + tacDir;
681 if (bf::is_symlink(dllPath)) {
682 if (bf::exists(tacDir)) {
683 if (!isNativeImage(dllPath)) {
684 std::string originPath = bf::read_symlink(dllPath).string();
685 std::string originNIPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
686 if (!bf::exists(originNIPath)) {
687 flags |= NI_FLAGS_APPNI;
688 if (crossgen(originPath, paths, flags) != NI_ERROR_NONE) {
689 fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str());
690 return NI_ERROR_UNKNOWN;
693 std::string setNIPath = dllPath.substr(0, dllPath.rfind(".dll")) + ".ni.dll";
694 if (!bf::exists(setNIPath)) {
695 bf::create_symlink(originNIPath, setNIPath);
696 fprintf(stdout, "%s symbolic link file generated successfully.\n", setNIPath.c_str());
697 copySmackAndOwnership(tacDir.c_str(), setNIPath.c_str(), true);
699 std::string niFileName = setNIPath.substr(setNIPath.rfind('/') + 1);
700 if (!removeFile(concatPath(binNIDir, niFileName))) {
701 fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, niFileName).c_str());
705 return NI_ERROR_NONE;
707 std::string assembly = dllPath.substr(dllPath.rfind('/') + 1);
708 std::vector<std::string> tpaAssemblies;
709 splitPath(__tpa, tpaAssemblies);
710 for (auto& tpa : tpaAssemblies) {
711 if (!strcmp(replaceAll(tpa.substr(tpa.rfind('/') + 1), ".ni.dll", ".dll").c_str(), assembly.c_str())) {
712 fprintf(stderr, "%s present in the TPA list skips generation of NI file.\n", assembly.c_str());
713 return NI_ERROR_NONE;
716 tpaAssemblies.clear();
717 flags |= NI_FLAGS_APPNI;
718 return crossgen(dllPath, paths, flags);
722 void removeNIPlatform()
724 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
725 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
727 if (hasCoreLibNI()) {
728 if (!isFile(coreLibBackup)) {
732 if (remove(coreLib.c_str())) {
733 fprintf(stderr, "Failed to remove System.Private.CoreLib native image file\n");
735 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
736 fprintf(stderr, "Failed to rename System.Private.CoreLib.Backup to origin\n");
740 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
741 if (isFile(__SYSTEM_BASE_FILE)) {
742 if (remove(__SYSTEM_BASE_FILE)) {
743 fprintf(stderr, "Failed to remove %s\n", __SYSTEM_BASE_FILE);
748 const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
750 removeNIUnderDirs(platformDirs, 2);
753 void removeNIUnderDirs(const std::string rootPaths[], int count)
755 auto convert = [](const std::string& path, const std::string& filename) {
756 if (isNativeImage(path)) {
757 if (remove(path.c_str())) {
758 fprintf(stderr, "Failed to remove %s\n", path.c_str());
763 for (int i = 0; i < count; i++) {
764 scanFilesInDirectory(rootPaths[i], convert, -1);
768 ni_error_e removeNIUnderPkgRoot(const std::string& pkgId)
770 std::string rootPath = getRootPath(pkgId);
771 if (rootPath.empty()) {
772 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
773 return NI_ERROR_INVALID_PACKAGE;
776 std::string binDir = concatPath(rootPath, "bin");
777 std::string libDir = concatPath(rootPath, "lib");
778 std::string paths[] = {binDir, libDir};
780 removeNIUnderDirs(paths, 2);
782 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
783 if (isFile(binNIDir)) {
784 if (!removeAll(binNIDir.c_str())) {
785 fprintf(stderr, "Failed to remove app ni dir [%s]\n", binNIDir.c_str());
789 std::string libNIDir = concatPath(libDir, APP_NI_SUB_DIR);
790 if (isFile(libNIDir)) {
791 if (!removeAll(libNIDir.c_str())) {
792 fprintf(stderr, "Failed to remove app ni dir [%s]\n", libNIDir.c_str());
796 return NI_ERROR_NONE;
799 ni_error_e regenerateAppNI(DWORD flags)
801 if (!isCoreLibPrepared(flags)) {
802 return NI_ERROR_CORE_NI_FILE;
806 pkgmgrinfo_appinfo_metadata_filter_h handle;
808 ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
809 if (ret != PMINFO_R_OK)
810 return NI_ERROR_UNKNOWN;
812 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE);
813 if (ret != PMINFO_R_OK) {
814 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
815 return NI_ERROR_UNKNOWN;
818 ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, appAotCb, &flags);
819 if (ret != PMINFO_R_OK) {
820 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
821 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
822 return NI_ERROR_UNKNOWN;
825 fprintf(stdout, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
827 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
828 return NI_ERROR_NONE;
831 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
832 static int regenTacCb(pkgmgrinfo_appinfo_h handle, void *userData)
835 DWORD *pFlags = (DWORD*)userData;
837 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
838 if (ret != PMINFO_R_OK) {
839 fprintf(stderr, "Failed to get pkgid\n");
843 sqlite3 *tac_db = dbOpen(TAC_APP_LIST_DB);
845 fprintf(stderr, "Sqlite open error\n");
849 char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
850 std::vector<std::string> nugets = dbSelect(tac_db, TAC_APP_LIST_DB, sql);
858 createNIUnderTAC(nugets, *pFlags);
863 ni_error_e regenerateTACNI(DWORD flags)
865 if (!isCoreLibPrepared(flags)) {
866 return NI_ERROR_CORE_NI_FILE;
869 const std::string tacDir[] = {__DOTNET_DIR};
870 removeNIUnderDirs(tacDir, 1);
872 pkgmgrinfo_appinfo_metadata_filter_h handle;
873 int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
874 if (ret != PMINFO_R_OK) {
875 return NI_ERROR_UNKNOWN;
878 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
879 if (ret != PMINFO_R_OK) {
880 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
881 return NI_ERROR_UNKNOWN;
884 ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, regenTacCb, &flags);
885 if (ret != PMINFO_R_OK) {
886 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
887 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
888 return NI_ERROR_UNKNOWN;
890 fprintf(stdout, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
892 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
894 return NI_ERROR_NONE;