Merge branch 'tizen' into use_app_ni_path
[platform/core/dotnet/launcher.git] / NativeLauncher / installer-plugin / ni_common.cc
index cf3e85e..29f0b82 100644 (file)
@@ -69,7 +69,27 @@ static void waitInterval()
        }
 }
 
-static std::string getNiFileName(const std::string& dllPath)
+static void updateNiFileInfo(const std::string& dllPath, const std::string& niPath)
+{
+       char* label = NULL;
+
+       // change smack label
+       if (smack_getlabel(dllPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
+               if (smack_setlabel(niPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
+                       fprintf(stderr, "Fail to set smack label\n");
+               }
+               free(label);
+       }
+
+       // change owner and groups for generated ni file.
+       struct stat info;
+       if (!stat(dllPath.c_str(), &info)) {
+               if (chown(niPath.c_str(), info.st_uid, info.st_gid) == -1)
+                       fprintf(stderr, "Failed to change owner and group name\n");
+       }
+}
+
+static std::string getNiFilePath(const std::string& dllPath)
 {
        size_t index = dllPath.find_last_of(".");
        if (index == std::string::npos) {
@@ -86,9 +106,32 @@ static std::string getNiFileName(const std::string& dllPath)
        return niPath;
 }
 
+static std::string getAppNIPath(const std::string& niPath)
+{
+       size_t index = niPath.find_last_of("/");
+       if (index == std::string::npos) {
+               fprintf(stderr, "dllPath doesnot contains path info\n");
+               return "";
+       }
+
+       std::string prevPath = niPath.substr(0, index);
+       std::string fileName = niPath.substr(index, niPath.length());
+       std::string niDirPath = prevPath + APP_NI_SUB_DIR;
+
+       if (!isFileExist(niDirPath)) {
+               if (mkdir(niDirPath.c_str(), 0755) == 0) {
+                       updateNiFileInfo(prevPath, niDirPath);
+               } else {
+                       fprintf(stderr, "Fail to create app ni directory (%s)\n", niDirPath.c_str());
+               }
+       }
+
+       return niDirPath + fileName;
+}
+
 static bool niExist(const std::string& path)
 {
-       std::string f = getNiFileName(path);
+       std::string f = getNiFilePath(path);
        if (f.empty()) {
                return false;
        }
@@ -109,27 +152,7 @@ static bool niExist(const std::string& path)
        return false;
 }
 
-static void updateNiFileInfo(const std::string& dllPath, const std::string& niPath)
-{
-       char* label = NULL;
-
-       // change smack label
-       if (smack_getlabel(dllPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
-               if (smack_setlabel(niPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
-                       fprintf(stderr, "Fail to set smack label\n");
-               }
-               free(label);
-       }
-
-       // change owner and groups for generated ni file.
-       struct stat info;
-       if (!stat(dllPath.c_str(), &info)) {
-               if (chown(niPath.c_str(), info.st_uid, info.st_gid) == -1)
-                       fprintf(stderr, "Failed to change owner and group name\n");
-       }
-}
-
-static ni_error_e crossgen(const std::string& dllPath, const std::string& appPath, bool enableR2R)
+static ni_error_e crossgen(const std::string& dllPath, const std::string& appPath, bool enableR2R, bool isAppNI = false)
 {
        if (!isFileExist(dllPath)) {
                fprintf(stderr, "dll file is not exist : %s\n", dllPath.c_str());
@@ -147,12 +170,16 @@ static ni_error_e crossgen(const std::string& dllPath, const std::string& appPat
        }
 
        std::string absDllPath = absolutePath(dllPath);
-       std::string absNiPath = getNiFileName(dllPath);
+       std::string absNiPath = getNiFilePath(dllPath);
        if (absNiPath.empty()) {
                fprintf(stderr, "Fail to get ni file name\n");
                return NI_ERROR_UNKNOWN;
        }
 
+       if (isAppNI) {
+               absNiPath = getAppNIPath(absNiPath);
+       }
+
        pid_t pid = fork();
        if (pid == -1)
                return NI_ERROR_UNKNOWN;
@@ -349,7 +376,7 @@ ni_error_e createNiDll(const std::string& dllPath, bool enableR2R)
        return crossgen(dllPath, std::string(), enableR2R);
 }
 
-void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R)
+void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R, bool isAppNI)
 {
        createCoreLibNI(enableR2R);
 
@@ -362,8 +389,8 @@ void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R)
        if (appPaths.back() == ':')
                appPaths.pop_back();
 
-       auto convert = [&appPaths, enableR2R](const std::string& path, const char* name) {
-               if (!crossgen(path, appPaths.c_str(), enableR2R)) {
+       auto convert = [&appPaths, enableR2R, isAppNI](const std::string& path, const char* name) {
+               if (!crossgen(path, appPaths.c_str(), enableR2R, isAppNI)) {
                        waitInterval();
                }
        };
@@ -385,7 +412,7 @@ ni_error_e createNiUnderPkgRoot(const std::string& pkgName, bool enableR2R)
        std::string libDir = concatPath(pkgRoot, "lib");
        std::string paths[] = {binDir, libDir};
 
-       createNiUnderDirs(paths, 2, enableR2R);
+       createNiUnderDirs(paths, 2, enableR2R, true);
 
        return NI_ERROR_NONE;
 }
@@ -402,7 +429,7 @@ ni_error_e createNiDllUnderPkgRoot(const std::string& pkgName, const std::string
        std::string libDir = concatPath(pkgRoot, "lib");
        std::string paths = binDir + ":" + libDir;
 
-       return crossgen(dllPath, paths, enableR2R);
+       return crossgen(dllPath, paths, enableR2R, true);
 }
 
 void removeNiPlatform()
@@ -456,6 +483,20 @@ ni_error_e removeNiUnderPkgRoot(const std::string& pkgName)
 
        removeNiUnderDirs(paths, 2);
 
+       std::string binNIDir = binDir + APP_NI_SUB_DIR;
+       if (isFileExist(binNIDir)) {
+               if (rmdir(binNIDir.c_str()) != 0) {
+                       fprintf(stderr, "Failed to remove app ni dir [%s]\n", binNIDir.c_str());
+               }
+       }
+
+       std::string libNIDir = libDir + APP_NI_SUB_DIR;
+       if (isFileExist(libNIDir)) {
+               if (rmdir(libNIDir.c_str()) != 0) {
+                       fprintf(stderr, "Failed to remove app ni dir [%s]\n", libNIDir.c_str());
+               }
+       }
+
        return NI_ERROR_NONE;
 }
 
@@ -468,7 +509,7 @@ ni_error_e regenerateAppNI(bool enableR2R)
        if (ret != PMINFO_R_OK)
                return NI_ERROR_UNKNOWN;
 
-       ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, "http://tizen.org/metadata/prefer_dotnet_aot", "true");
+       ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, AOT_METADATA_VALUE);
        if (ret != PMINFO_R_OK) {
                pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
                return NI_ERROR_UNKNOWN;