add command to nitool to remove system and app ni
[platform/core/dotnet/launcher.git] / NativeLauncher / installer-plugin / common.cc
index 8ba2a09..7d826db 100644 (file)
@@ -35,6 +35,8 @@
 #include <unistd.h>
 #include <string.h>
 
+#include <fstream>
+
 #include "common.h"
 
 #ifdef  LOG_TAG
@@ -63,7 +65,7 @@ static const char* __JIT_PATH = __STR(RUNTIME_DIR)"/libclrjit.so";
 #undef __STR
 #undef __XSTR
 
-
+#if 0
 static std::string replace(std::string &str, const std::string& from, const std::string& to)
 {
        size_t startPos = 0;
@@ -73,6 +75,7 @@ static std::string replace(std::string &str, const std::string& from, const std:
        }
        return str;
 }
+#endif
 
 static void smack_(const char* dllPath, const char* label)
 {
@@ -131,8 +134,12 @@ static void crossgen(const char* dllPath, const char* appPath)
 
                // get reference API directory ([DEVICE_API_DIR]/ref)
                int len = strlen(__DEVICE_API_DIR);
-               char* refAPIDir = (char*)calloc(len + 4, 1);
-               snprintf(refAPIDir, len + 4, "%s%s", __DEVICE_API_DIR, "/ref");
+               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;
@@ -248,6 +255,40 @@ static void createCoreLibNI()
        }
 }
 
+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();
@@ -327,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());
@@ -345,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;