+ return true;
+}
+
+static std::string getNIFilePath(const std::string& absDllPath, NIOption* opt)
+{
+ std::string dllPath = absDllPath;
+ std::string fileName = getFileName(absDllPath);
+ if (opt->flags & NI_FLAGS_APPNI) {
+ std::string niDirPath;
+ std::string niTmpDirPath;
+ std::string prevPath;
+
+ prevPath = getBaseName(absDllPath);
+ niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
+ niTmpDirPath = concatPath(prevPath, APP_NI_SUB_TMP_DIR);
+
+ if (opt->flags & NI_FLAGS_APP_UNDER_RO_AREA) {
+ niTmpDirPath = replaceAll(niTmpDirPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
+ niDirPath = replaceAll(niDirPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
+ _INFO("App is installed in RO area. Change NI path to RW area(%s).", niTmpDirPath.c_str());
+ }
+
+ if (!isDirectory(niDirPath)) {
+ if (!createDirsAndCopyOwnerShip(niTmpDirPath, prevPath)) {
+ niTmpDirPath = prevPath;
+ _SERR("fail to create dir (%s)", niTmpDirPath.c_str());
+ }
+ dllPath = concatPath(niTmpDirPath, fileName);
+ } else {
+ dllPath = concatPath(niDirPath, fileName);
+ }
+ } else if (opt->flags & NI_FLAGS_RESOURCE_NI) {
+ std::string rpkDir = concatPath(__pm->getAppRootPath(), APP_NI_SUB_DIR);
+ dllPath = createDir(rpkDir) ? concatPath(rpkDir, fileName) : concatPath(__pm->getAppRootPath(), fileName);
+ }
+
+ size_t index = dllPath.find_last_of(".");
+ if (index == std::string::npos) {
+ _SERR("File doesnot contain extension. fail to get NI file name");
+ return "";
+ }
+ std::string fName = dllPath.substr(0, index);
+ std::string fExt = dllPath.substr(index, dllPath.length());
+
+ // crossgen generate file with lower case extension only
+ std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
+ std::string niPath = fName + ".ni" + fExt;
+
+ return niPath;
+}
+
+static bool checkNIExistence(const std::string& absDllPath, NIOption* opt)
+{
+ std::string absNIPath = getNIFilePath(absDllPath, opt);
+ if (absNIPath.empty()) {
+ return false;
+ }
+
+ if (isFile(absNIPath)) {
+ return true;
+ }
+
+ // native image of System.Private.CoreLib.dll should have to overwrite
+ // original file to support new coreclr
+ if (absDllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
+ return isR2RImage(absDllPath);
+ }
+
+ return false;
+}
+
+static bool checkDllExistInDir(const std::string& path)
+{
+ bool ret = false;
+ auto func = [&ret](const std::string& f_path, const std::string& f_name) {
+ if (isManagedAssembly(f_name) || isR2RImage(f_name)) {
+ ret = true;
+ }
+ };
+
+ scanFilesInDirectory(path, func, 0);
+
+ return ret;
+}
+
+/*
+ * Get the list of managed files in the specific directory (of Application)
+ * Absolute paths of managed files are stored at the result list.
+ * If native image already exist in the (same / .native_image) directory, managed file is ignored.
+ */
+static ni_error_e getTargetDllList(const std::string& path, std::vector<std::string>& fileList, NIOption *opt)
+{
+ if (!isDirectory(path)) {