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 // Get next base address to be used for system ni image from file
149 // __SYSTEM_BASE_FILE should be checked for existance before calling this function
150 static uintptr_t getNextBaseAddrFromFile()
152 FILE *pFile = fopen(__SYSTEM_BASE_FILE, "r");
154 fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
161 while (fscanf(pFile, "%u %u", &addr, &size) != EOF) {
169 // Get next base address to be used for system ni image
170 static uintptr_t getNextBaseAddr()
172 uintptr_t baseAddr = 0;
174 if (!isFile(__SYSTEM_BASE_FILE)) {
175 // This is the starting address for all default base addresses
176 baseAddr = DEFAULT_BASE_ADDR_START;
178 baseAddr = getNextBaseAddrFromFile();
180 // Round to a multple of 64K (see ZapImage::CalculateZapBaseAddress in CoreCLR)
181 uintptr_t BASE_ADDRESS_ALIGNMENT = 0xffff;
182 baseAddr = (baseAddr + BASE_ADDRESS_ALIGNMENT) & ~BASE_ADDRESS_ALIGNMENT;
188 // Save base address of system ni image to file
189 static void updateBaseAddrFile(const std::string &absNIPath, uintptr_t baseAddr)
191 uintptr_t niSize = getFileSize(absNIPath);
193 fprintf(stderr, "File %s doesn't exist\n", absNIPath.c_str());
197 // Write new entry to the file
198 FILE *pFile = fopen(__SYSTEM_BASE_FILE, "a");
200 fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
204 fprintf(pFile, "%u %u\n", baseAddr, niSize);
208 // check if dll is listed in TPA
209 static bool isTPADll(const std::string &dllPath)
211 std::string absDllPath = getAbsolutePath(dllPath);
213 if (__tpa.find(absDllPath) != std::string::npos) {
221 // baseAddr should be checked in file before getting here
222 static ni_error_e crossgen(const std::string& dllPath, const std::string& appPath, DWORD flags)
224 if (!isFile(dllPath)) {
225 fprintf(stderr, "dll file is not exist : %s\n", dllPath.c_str());
226 return NI_ERROR_NO_SUCH_FILE;
229 if (!isManagedAssembly(dllPath)) {
230 //fprintf(stderr, "Input file is not a dll file : %s\n", dllPath.c_str());
231 return NI_ERROR_INVALID_PARAMETER;
234 if (checkNIExistence(dllPath)) {
235 fprintf(stderr, "Already ni file is exist for %s\n", dllPath.c_str());
236 return NI_ERROR_ALREADY_EXIST;
239 std::string absDllPath = getAbsolutePath(dllPath);
240 std::string absNIPath = getNIFilePath(dllPath);
243 if (absNIPath.empty()) {
244 fprintf(stderr, "Fail to get ni file name\n");
245 return NI_ERROR_UNKNOWN;
248 bool isAppNI = flags & NI_FLAGS_APPNI;
249 if (isAppNI && strstr(absNIPath.c_str(), __DOTNET_DIR) == NULL) {
250 absNIPath = getAppNIPath(absNIPath);
253 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
254 uintptr_t baseAddr = 0;
256 if (isTPADll(dllPath)) {
257 baseAddr = getNextBaseAddr();
263 return NI_ERROR_UNKNOWN;
267 waitpid(pid, &status, 0);
268 if (WIFEXITED(status)) {
269 // Do not use checkNIExistence() function to check whether ni file created or not.
270 // checkNIExistence() return false for System.Private.Corelib.dll
271 if (isFile(absNIPath)) {
272 copySmackAndOwnership(absDllPath, absNIPath);
273 std::string absPdbPath = replaceAll(absDllPath, ".dll", ".pdb");
274 std::string pdbFilePath = replaceAll(absNIPath, ".ni.dll", ".pdb");
275 if (isFile(absPdbPath) && (absPdbPath != pdbFilePath)) {
276 if (!copyFile(absPdbPath, pdbFilePath)) {
277 fprintf(stderr, "Failed to copy a .pdb file\n");
279 copySmackAndOwnership(absPdbPath, pdbFilePath);
282 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
284 updateBaseAddrFile(absNIPath, baseAddr);
287 return NI_ERROR_NONE;
289 fprintf(stderr, "Fail to create native image for %s\n", dllPath.c_str());
290 return NI_ERROR_NO_SUCH_FILE;
294 std::string jitPath = getRuntimeDir() + "/libclrjit.so";
295 std::vector<const char*> argv = {
298 "/JITPath", jitPath.c_str()
301 bool compat = flags & NI_FLAGS_COMPATIBILITY;
302 argv.push_back(compat ? "/Trusted_Platform_Assemblies" : "/r");
303 argv.push_back(__tpa.c_str());
305 bool enableR2R = flags & NI_FLAGS_ENABLER2R;
307 argv.push_back("/FragileNonVersionable");
310 if (flags & NI_FLAGS_VERBOSE) {
311 argv.push_back("/verbose");
314 if (flags & NI_FLAGS_INSTRUMENT) {
315 argv.push_back("/Tuning");
318 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
320 argv.push_back("/BaseAddress");
321 argv.push_back(std::to_string(baseAddr).c_str());
325 argv.push_back("/App_Paths");
326 std::string absAppPath;
327 if (!appPath.empty()) {
328 absAppPath = appPath;
330 absAppPath = getBaseName(absDllPath);
332 argv.push_back(absAppPath.c_str());
334 argv.push_back("/out");
335 argv.push_back(absNIPath.c_str());
337 argv.push_back(absDllPath.c_str());
338 argv.push_back(nullptr);
340 fprintf(stdout, "+ %s (%s)\n", absDllPath.c_str(), enableR2R ? "R2R" : "FNV");
342 execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
346 return NI_ERROR_NONE;
349 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
350 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
354 DWORD *pFlags = (DWORD*)userData;
356 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
357 if (ret != PMINFO_R_OK) {
358 fprintf(stderr, "Failed to get pkgid\n");
362 if (removeNIUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
363 fprintf(stderr, "Failed to remove previous dlls from [%s]\n", pkgId);
367 if (resetTACPackage(pkgId) != TAC_ERROR_NONE) {
368 fprintf(stderr, "Failed to remove symlink for given package [%s]\n", pkgId);
372 // Regenerate ni files with R2R mode forcibiliy. (there is no way to now which option is used)
373 if (createNIUnderPkgRoot(pkgId, *pFlags) != NI_ERROR_NONE) {
374 fprintf(stderr, "Failed to generate NI file [%s]\n", pkgId);
377 fprintf(stdout, "Complete make application to native image\n");
380 if (createTACPkgRoot(pkgId, *pFlags) != NI_ERROR_NONE) {
381 fprintf(stderr, "Failed to generate symbolic link file [%s]\n", pkgId);
384 fprintf(stdout, "Complete make symbolic link file to tac\n");
390 static bool isCoreLibPrepared(DWORD flags)
392 if (flags & NI_FLAGS_ENABLER2R) {
396 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
397 if (isFile(coreLibBackup)) {
400 fprintf(stderr, "The native image of System.Private.CoreLib does not exist\n"
401 "Run the command to create the native image\n"
402 "# dotnettool --ni-dll /usr/share/dotnet.tizen/netcoreapp/System.Private.CoreLib.dll\n\n");
407 static bool hasCoreLibNI()
411 std::string ildasm = concatPath(getRuntimeDir(), "ildasm");
412 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
413 std::string cmd = ildasm + " " + coreLib + " | grep '\\.corflags'";
414 fp = popen(cmd.c_str(), "r");
416 while (fgets(buff, sizeof(buff), fp) != NULL) {
417 buff[strlen(buff) - 1] = '\0';
419 std::string corflag = replaceAll(buff, ".corflags", "");
420 corflag.erase(std::remove(corflag.begin(), corflag.end(), ' '), corflag.end());
421 // CorFlags.ILLibrary=0x00000004 (.ni.dll)
422 if (!strcmp(corflag.substr(0, 10).c_str(), "0x00000004")) {
431 static ni_error_e createCoreLibNI(DWORD flags)
433 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
434 std::string niCoreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.ni.dll");
435 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
437 if (!isFile(coreLibBackup) && !hasCoreLibNI()) {
438 if (!crossgen(coreLib, std::string(), flags)) {
439 if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
440 fprintf(stderr, "Failed to rename System.Private.CoreLib.dll\n");
441 return NI_ERROR_CORE_NI_FILE;
443 if (rename(niCoreLib.c_str(), coreLib.c_str())) {
444 fprintf(stderr, "Failed to rename System.Private.CoreLib.ni.dll\n");
445 return NI_ERROR_CORE_NI_FILE;
448 fprintf(stderr, "Failed to create native image for %s\n", coreLib.c_str());
449 return NI_ERROR_CORE_NI_FILE;
452 return NI_ERROR_NONE;
455 ni_error_e initNICommon(NICommonOption* option)
458 // get interval value
459 const static std::string intervalFile = concatPath(__NATIVE_LIB_DIR, "crossgen_interval.txt");
460 std::ifstream inFile(intervalFile);
462 fprintf(stdout, "crossgen_interval.txt is found\n");
463 inFile >> __interval;
466 if (initializePluginManager("normal")) {
467 fprintf(stderr, "Fail to initialize PluginManager\n");
468 return NI_ERROR_UNKNOWN;
470 if (initializePathManager(option->runtimeDir, option->tizenFXDir, option->extraDirs)) {
471 fprintf(stderr, "Fail to initialize PathManager\n");
472 return NI_ERROR_UNKNOWN;
477 return NI_ERROR_NONE;
479 fprintf(stderr, "crossgen supports arm architecture only. skip ni file generation\n");
480 return NI_ERROR_NOT_SUPPORTED;
484 void finalizeNICommon()
488 finalizePluginManager();
489 finalizePathManager();
495 ni_error_e createNIPlatform(DWORD flags)
497 if (createCoreLibNI(flags) != NI_ERROR_NONE) {
498 return NI_ERROR_CORE_NI_FILE;
501 const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
502 return createNIUnderDirs(platformDirs, 2, flags);
505 ni_error_e createNIDll(const std::string& dllPath, DWORD flags)
507 if (dllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
508 return createCoreLibNI(flags);
511 if (!isCoreLibPrepared(flags)) {
512 return NI_ERROR_CORE_NI_FILE;
515 return crossgen(dllPath, std::string(), flags);
518 void createNIUnderTAC(std::vector<std::string> nugets, DWORD flags)
520 std::string appPaths;
521 for (auto& nuget : nugets) {
522 appPaths += concatPath(__DOTNET_DIR, nuget);
525 if (appPaths.back() == ':') {
529 auto convert = [&appPaths, flags](const std::string& path, const std::string& filename) {
530 if (strstr(path.c_str(), TAC_SHA_256_INFO) != NULL)
532 if (!crossgen(path, appPaths.c_str(), flags)) {
536 for (auto& nuget : nugets) {
537 scanFilesInDirectory(concatPath(__DOTNET_DIR, nuget), convert, -1);
541 ni_error_e createTACPkgRoot(const std::string& pkgId, DWORD flags)
543 if (!isCoreLibPrepared(flags)) {
544 return NI_ERROR_CORE_NI_FILE;
548 if (getRootPath(pkgId, pkgRoot) < 0) {
549 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
550 return NI_ERROR_INVALID_PACKAGE;
553 std::string binDir = concatPath(pkgRoot, "bin");
554 std::string libDir = concatPath(pkgRoot, "lib");
555 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
556 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
557 std::string paths = binDir + ":" + libDir + ":" + tacDir;
558 if (bf::exists(tacDir)) {
560 for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
561 std::string symPath = symlinkAssembly.path().string();
562 if (bf::is_symlink(symPath)) {
563 if (!isNativeImage(symPath)) {
564 std::string originPath = bf::read_symlink(symPath).string();
565 std::string originNIPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
566 if (!bf::exists(originNIPath)) {
567 flags |= NI_FLAGS_APPNI;
568 if(crossgen(originPath, paths, flags) != NI_ERROR_NONE) {
569 fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str());
570 return NI_ERROR_UNKNOWN;
573 std::string symNIPath = symPath.substr(0, symPath.rfind(".dll")) + ".ni.dll";
574 if (!bf::exists(symNIPath)) {
575 bf::create_symlink(originNIPath, symNIPath);
576 fprintf(stdout, "%s symbolic link file generated successfully.\n", symNIPath.c_str());
577 copySmackAndOwnership(tacDir.c_str(), symNIPath.c_str(), true);
579 std::string niFileName = symNIPath.substr(symNIPath.rfind('/') + 1);
580 if (!removeFile(concatPath(binNIDir, niFileName))) {
581 fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, niFileName).c_str());
587 } catch (const bf::filesystem_error& error) {
588 fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
589 return NI_ERROR_UNKNOWN;
592 return NI_ERROR_NONE;
595 ni_error_e createNIUnderDirs(const std::string rootPaths[], int count, DWORD flags)
597 if (!isCoreLibPrepared(flags)) {
598 return NI_ERROR_CORE_NI_FILE;
601 std::string appPaths;
602 for (int i = 0; i < count; i++) {
603 appPaths += rootPaths[i];
607 if (appPaths.back() == ':')
610 std::vector<std::string> tpaAssemblies;
611 splitPath(__tpa, tpaAssemblies);
613 auto convert = [&appPaths, flags, tpaAssemblies](const std::string& path, const std::string& filename) {
614 bool isAppNI = flags & NI_FLAGS_APPNI;
616 for (auto& tpa : tpaAssemblies) {
617 if (!strcmp(replaceAll(tpa.substr(tpa.rfind('/') + 1), ".ni.dll", ".dll").c_str(), filename.c_str())) {
618 fprintf(stderr, "%s present in the TPA list skips generation of NI file.\n", filename.c_str());
623 if (!crossgen(path, appPaths.c_str(), flags)) {
628 for (int i = 0; i < count; i++) {
629 scanFilesInDirectory(rootPaths[i], convert, 0);
632 tpaAssemblies.clear();
633 return NI_ERROR_NONE;
636 ni_error_e createNIUnderPkgRoot(const std::string& pkgId, DWORD flags)
639 if (getRootPath(pkgId, pkgRoot) < 0) {
640 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
641 return NI_ERROR_INVALID_PACKAGE;
644 std::string binDir = concatPath(pkgRoot, "bin");
645 std::string libDir = concatPath(pkgRoot, "lib");
646 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
647 std::string paths[] = {binDir, libDir, tacDir};
649 flags |= NI_FLAGS_APPNI;
650 return createNIUnderDirs(paths, 3, flags);
653 ni_error_e createNIDllUnderPkgRoot(const std::string& pkgId, const std::string& dllPath, DWORD flags)
655 if (!isCoreLibPrepared(flags)) {
656 return NI_ERROR_CORE_NI_FILE;
660 if (getRootPath(pkgId, pkgRoot) < 0) {
661 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
662 return NI_ERROR_INVALID_PACKAGE;
665 std::string binDir = concatPath(pkgRoot, "bin");
666 std::string libDir = concatPath(pkgRoot, "lib");
667 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
668 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
669 std::string paths = binDir + ":" + libDir + ":" + tacDir;
671 if (bf::is_symlink(dllPath)) {
672 if (bf::exists(tacDir)) {
673 if (!isNativeImage(dllPath)) {
674 std::string originPath = bf::read_symlink(dllPath).string();
675 std::string originNIPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
676 if (!bf::exists(originNIPath)) {
677 flags |= NI_FLAGS_APPNI;
678 if(crossgen(originPath, paths, flags) != NI_ERROR_NONE) {
679 fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str());
680 return NI_ERROR_UNKNOWN;
683 std::string setNIPath = dllPath.substr(0, dllPath.rfind(".dll")) + ".ni.dll";
684 if (!bf::exists(setNIPath)) {
685 bf::create_symlink(originNIPath, setNIPath);
686 fprintf(stdout, "%s symbolic link file generated successfully.\n", setNIPath.c_str());
687 copySmackAndOwnership(tacDir.c_str(), setNIPath.c_str(), true);
689 std::string niFileName = setNIPath.substr(setNIPath.rfind('/') + 1);
690 if (!removeFile(concatPath(binNIDir, niFileName))) {
691 fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, niFileName).c_str());
695 return NI_ERROR_NONE;
697 std::string assembly = dllPath.substr(dllPath.rfind('/') + 1);
698 std::vector<std::string> tpaAssemblies;
699 splitPath(__tpa, tpaAssemblies);
700 for (auto& tpa : tpaAssemblies) {
701 if (!strcmp(replaceAll(tpa.substr(tpa.rfind('/') + 1), ".ni.dll", ".dll").c_str(), assembly.c_str())) {
702 fprintf(stderr, "%s present in the TPA list skips generation of NI file.\n", assembly.c_str());
703 return NI_ERROR_NONE;
706 tpaAssemblies.clear();
707 flags |= NI_FLAGS_APPNI;
708 return crossgen(dllPath, paths, flags);
712 void removeNIPlatform()
714 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
715 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
717 if (hasCoreLibNI()) {
718 if (!isFile(coreLibBackup)) {
722 if (remove(coreLib.c_str())) {
723 fprintf(stderr, "Failed to remove System.Private.CoreLib native image file\n");
725 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
726 fprintf(stderr, "Failed to rename System.Private.CoreLib.Backup to origin\n");
730 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
731 if (isFile(__SYSTEM_BASE_FILE)) {
732 if (remove(__SYSTEM_BASE_FILE)) {
733 fprintf(stderr, "Failed to remove %s\n", __SYSTEM_BASE_FILE);
738 const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
740 removeNIUnderDirs(platformDirs, 2);
743 void removeNIUnderDirs(const std::string rootPaths[], int count)
745 auto convert = [](const std::string& path, const std::string& filename) {
746 if (isNativeImage(path)) {
747 if (remove(path.c_str())) {
748 fprintf(stderr, "Failed to remove %s\n", path.c_str());
753 for (int i = 0; i < count; i++) {
754 scanFilesInDirectory(rootPaths[i], convert, -1);
758 ni_error_e removeNIUnderPkgRoot(const std::string& pkgId)
761 if (getRootPath(pkgId, pkgRoot) < 0) {
762 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
763 return NI_ERROR_INVALID_PACKAGE;
766 std::string binDir = concatPath(pkgRoot, "bin");
767 std::string libDir = concatPath(pkgRoot, "lib");
768 std::string paths[] = {binDir, libDir};
770 removeNIUnderDirs(paths, 2);
772 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
773 if (isFile(binNIDir)) {
774 if (!removeAll(binNIDir.c_str())) {
775 fprintf(stderr, "Failed to remove app ni dir [%s]\n", binNIDir.c_str());
779 std::string libNIDir = concatPath(libDir, APP_NI_SUB_DIR);
780 if (isFile(libNIDir)) {
781 if (!removeAll(libNIDir.c_str())) {
782 fprintf(stderr, "Failed to remove app ni dir [%s]\n", libNIDir.c_str());
786 return NI_ERROR_NONE;
789 ni_error_e regenerateAppNI(DWORD flags)
791 if (!isCoreLibPrepared(flags)) {
792 return NI_ERROR_CORE_NI_FILE;
796 pkgmgrinfo_appinfo_metadata_filter_h handle;
798 ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
799 if (ret != PMINFO_R_OK)
800 return NI_ERROR_UNKNOWN;
802 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE);
803 if (ret != PMINFO_R_OK) {
804 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
805 return NI_ERROR_UNKNOWN;
808 ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, appAotCb, &flags);
809 if (ret != PMINFO_R_OK) {
810 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
811 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
812 return NI_ERROR_UNKNOWN;
815 fprintf(stdout, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
817 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
818 return NI_ERROR_NONE;
821 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
822 static int regenTacCb(pkgmgrinfo_appinfo_h handle, void *userData)
825 DWORD *pFlags = (DWORD*)userData;
827 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
828 if (ret != PMINFO_R_OK) {
829 fprintf(stderr, "Failed to get pkgid\n");
833 sqlite3 *tac_db = dbOpen(TAC_APP_LIST_DB);
835 fprintf(stderr, "Sqlite open error\n");
839 char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
840 std::vector<std::string> nugets = dbSelect(tac_db, TAC_APP_LIST_DB, sql);
848 createNIUnderTAC(nugets, *pFlags);
853 ni_error_e regenerateTACNI(DWORD flags)
855 if (!isCoreLibPrepared(flags)) {
856 return NI_ERROR_CORE_NI_FILE;
859 const std::string tacDir[] = {__DOTNET_DIR};
860 removeNIUnderDirs(tacDir, 1);
862 pkgmgrinfo_appinfo_metadata_filter_h handle;
863 int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
864 if (ret != PMINFO_R_OK) {
865 return NI_ERROR_UNKNOWN;
868 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
869 if (ret != PMINFO_R_OK) {
870 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
871 return NI_ERROR_UNKNOWN;
874 ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, regenTacCb, &flags);
875 if (ret != PMINFO_R_OK) {
876 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
877 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
878 return NI_ERROR_UNKNOWN;
880 fprintf(stdout, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
882 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
884 return NI_ERROR_NONE;