add command to nitool to remove system and app ni
[platform/core/dotnet/launcher.git] / NativeLauncher / installer-plugin / common.cc
index e53cff3..7d826db 100644 (file)
@@ -33,6 +33,9 @@
 #include <grp.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <string.h>
+
+#include <fstream>
 
 #include "common.h"
 
@@ -62,10 +65,8 @@ static const char* __JIT_PATH = __STR(RUNTIME_DIR)"/libclrjit.so";
 #undef __STR
 #undef __XSTR
 
-static void crossgen(const char* dllPath, const char* appPath);
-static void smack_(const char* dllPath, const char* label);
-
-std::string replace(std::string &str, const std::string& from, const std::string& to)
+#if 0
+static std::string replace(std::string &str, const std::string& from, const std::string& to)
 {
        size_t startPos = 0;
        while ((startPos = str.find(from, startPos)) != std::string::npos) {
@@ -74,45 +75,7 @@ std::string replace(std::string &str, const std::string& from, const std::string
        }
        return str;
 }
-
-void createNiPlatform()
-{
-       std::string coreLib = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.dll");
-       std::string niCoreLib = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.ni.dll");
-
-       if (fileNotExist(niCoreLib)) {
-               crossgen(coreLib.c_str(), nullptr);
-               smack_(niCoreLib.c_str(), "_");
-       }
-
-       const char* platformDirs[] = {__RUNTIME_DIR, __DEVICE_API_DIR, "/usr/bin"};
-       const char* ignores[] = {coreLib.c_str()};
-
-       createNiUnderDirs(platformDirs, 3, ignores, 1, [](const char* ni) {
-               smack_(ni, "_");
-       });
-}
-
-void createNiSelect(const char* dllPath)
-{
-       std::string coreLib = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.dll");
-       std::string niCoreLib = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.ni.dll");
-
-       if (fileNotExist(niCoreLib)) {
-               crossgen(coreLib.c_str(), nullptr);
-               smack_(niCoreLib.c_str(), "_");
-       }
-
-       if (!fileNotExist(dllPath)) {
-               std::string strPath = dllPath;
-               std::string niPath = replace(strPath, std::string(".dll"), std::string(".ni.dll"));
-               if (fileNotExist(niPath))
-                       crossgen(dllPath, nullptr);
-               else
-                       printf("Already [%s] file is exist\n", niPath.c_str());
-               smack_(niPath.c_str(), "_");
-       }
-}
+#endif
 
 static void smack_(const char* dllPath, const char* label)
 {
@@ -169,13 +132,23 @@ static void crossgen(const char* dllPath, const char* appPath)
                tpaDir.push_back(__RUNTIME_DIR);
                tpaDir.push_back(__DEVICE_API_DIR);
 
+               // get reference API directory ([DEVICE_API_DIR]/ref)
+               int len = strlen(__DEVICE_API_DIR);
+               char* refAPIDir = (char*)calloc(len + 5, 1);
+               if (!refAPIDir) {
+                       printf("fail to allocate memory for reference API directory\n");
+                       return;
+               }
+               snprintf(refAPIDir, len + 5, "%s%s", __DEVICE_API_DIR, "/ref");
+               tpaDir.push_back(refAPIDir);
+
                std::string tpa;
                assembliesInDirectory(tpaDir, tpa);
 
                std::vector<const char*> argv = {
                        __CROSSGEN_PATH,
                        "/Trusted_Platform_Assemblies", tpa.c_str(),
-                       "/__JIT_PATH", __JIT_PATH,
+                       "/JITPath", __JIT_PATH,
                        "/FragileNonVersionable"
                };
                if (appPath != nullptr) {
@@ -230,6 +203,17 @@ static int getRootPath(const char *pkgId, std::string& rootPath)
 
 static bool niExist(const std::string& path, std::string& ni)
 {
+       // native image of System.Private.CoreLib.dll should have to overwrite
+       // original file to support new coreclr
+       if (path.find("System.Private.CoreLib.dll") != std::string::npos) {
+               std::string coreLibBackup = path + ".Backup";
+               if (!fileNotExist(coreLibBackup)) {
+                       ni = path;
+                       return true;
+               }
+               return false;
+       }
+
        static const char* possibleExts[] = {
                ".ni.dll", ".NI.dll", ".NI.DLL", ".ni.DLL",
                ".ni.exe", ".NI.exe", ".NI.EXE", ".ni.EXE"
@@ -249,6 +233,96 @@ static bool niExist(const std::string& path, std::string& ni)
        return false;
 }
 
+static void createCoreLibNI()
+{
+       std::string coreLib = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.dll");
+       std::string niCoreLib = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.ni.dll");
+       std::string coreLibBackup = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.dll.Backup");
+
+       if (!niExist(coreLib, niCoreLib)) {
+               crossgen(coreLib.c_str(), nullptr);
+               if (!fileNotExist(niCoreLib)) {
+                       // change owner and groups for generated ni file.
+                       struct stat info;
+                       if (!stat(coreLib.c_str(), &info)) {
+                               if (chown(niCoreLib.c_str(), info.st_uid, info.st_gid) == -1)
+                                       _ERR("Failed to change owner and group name");
+                       }
+                       smack_(niCoreLib.c_str(), "_");
+                       rename(coreLib.c_str(), coreLibBackup.c_str());
+                       rename(niCoreLib.c_str(), coreLib.c_str());
+               }
+       }
+}
+
+void removeNiUnderDirs(const char* rootPaths[], int count)
+{
+       auto convert = [](const char* path, const char* name) {
+               std::string ni;
+               if (isNativeImage(path)) {
+                       remove(path);
+               }
+       };
+
+       for (int i = 0; i < count; i++)
+               scanFilesInDir(rootPaths[i], convert, -1);
+}
+
+void removeNiPlatform()
+{
+       std::string coreLib = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.dll");
+       std::string niCoreLib = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.ni.dll");
+       std::string coreLibBackup = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.dll.Backup");
+
+       if (fileNotExist(coreLibBackup)) {
+               return;
+       }
+
+       if (remove(coreLib.c_str())) {
+               _ERR("Failed to remove System.Private.CoreLib native image file");
+       }
+
+       rename(coreLibBackup.c_str(), coreLib.c_str());
+
+       const char* platformDirs[] = {__RUNTIME_DIR, __DEVICE_API_DIR, "/usr/bin"};
+
+       removeNiUnderDirs(platformDirs, 3);
+}
+
+void createNiPlatform()
+{
+       createCoreLibNI();
+
+       const char* platformDirs[] = {__RUNTIME_DIR, __DEVICE_API_DIR, "/usr/bin"};
+
+       createNiUnderDirs(platformDirs, 3, [](const char* ni) {
+               smack_(ni, "_");
+       });
+}
+
+void createNiSelect(const char* dllPath)
+{
+       createCoreLibNI();
+
+       std::string niPath;
+       if (!fileNotExist(dllPath)) {
+               if (!niExist(dllPath, niPath)) {
+                       crossgen(dllPath, nullptr);
+                       if (niExist(dllPath, niPath)) {
+                               // change owner and groups for generated ni file.
+                               struct stat info;
+                               if (!stat(dllPath, &info)) {
+                                       if (chown(niPath.c_str(), info.st_uid, info.st_gid) == -1)
+                                               _ERR("Failed to change owner and group name");
+                               }
+                               smack_(niPath.c_str(), "_");
+                       }
+               }
+               else
+                       printf("Already [%s] file is exist\n", niPath.c_str());
+       }
+}
+
 void createNiUnderDirs(const char* rootPaths[], int count, const char* ignores[], int igcount, afterCreate cb)
 {
        std::string appPaths;
@@ -294,12 +368,45 @@ void createNiUnderDirs(const char* rootPaths[], int count)
        createNiUnderDirs(rootPaths, count, nullptr);
 }
 
+int removeNiUnderPkgRoot(const char* pkgName)
+{
+       std::string pkgRoot;
+       if (getRootPath(pkgName, pkgRoot) < 0)
+               return 1;
+
+       std::string binDir = concatPath(pkgRoot, "bin");
+       std::string libDir = concatPath(pkgRoot, "lib");
+       _INFO("bindir : %s", binDir.c_str());
+       _INFO("libdir : %s", libDir.c_str());
+
+       const char* paths[] = {
+               binDir.c_str(),
+               libDir.c_str()
+       };
+
+       removeNiUnderDirs(paths, 2);
+
+       return 0;
+}
+
+
 int createNiUnderPkgRoot(const char* pkgName)
 {
        std::string pkgRoot;
        if (getRootPath(pkgName, pkgRoot) < 0)
                return 1;
 
+       // get interval value
+       const char* intervalFile = "/usr/share/dotnet.tizen/lib/crossgen_interval.txt";
+       int interval = 0;
+       std::ifstream inFile(intervalFile);
+       if (inFile) {
+               _INFO("crossgen_interval.txt is found");
+               inFile >> interval;
+       } else {
+               _INFO("fail to read crossgen_interval.txt file");
+       }
+
        std::string binDir = concatPath(pkgRoot, "bin");
        std::string libDir = concatPath(pkgRoot, "lib");
        _INFO("bindir : %s", binDir.c_str());
@@ -312,8 +419,12 @@ int createNiUnderPkgRoot(const char* pkgName)
 
        // change smack label for generated ni file.
        std::string label = "User::Pkg::" + std::string(pkgName) + "::RO";
-       createNiUnderDirs(paths, 2, [label](const char* ni) {
+       createNiUnderDirs(paths, 2, [label, interval](const char* ni) {
                        smack_(ni, label.c_str());
+                       if (interval != 0) {
+                               _INFO("sleep %d usec", interval);
+                               usleep(interval);
+                       }
        });
 
        return 0;