Merge branch 'tizen' of https://github.sec.samsung.net/j-h-choi/dotnet-launcher into...
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / ni_common.cc
index 3811e97..d2b7ec3 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,53 +152,37 @@ 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 int 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());
-               return -1;
+               return NI_ERROR_NO_SUCH_FILE;
        }
 
        if (!isManagedAssembly(dllPath)) {
                fprintf(stderr, "Input file is not a dll file : %s\n", dllPath.c_str());
-               return -1;
+               return NI_ERROR_INVALID_PARAMETER;
        }
 
        if (niExist(dllPath)) {
                fprintf(stderr, "Already ni file is exist for %s\n", dllPath.c_str());
-               return -1;
+               return NI_ERROR_ALREADY_EXIST;
        }
 
        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 -1;
+               return NI_ERROR_UNKNOWN;
+       }
+
+       if (isAppNI) {
+               absNiPath = getAppNIPath(absNiPath);
        }
 
        pid_t pid = fork();
        if (pid == -1)
-               return -1;
+               return NI_ERROR_UNKNOWN;
 
        if (pid > 0) {
                int status;
@@ -165,10 +192,10 @@ static int crossgen(const std::string& dllPath, const std::string& appPath, bool
                        // niEixst() return false for System.Private.Corelib.dll
                        if (isFileExist(absNiPath)) {
                                updateNiFileInfo(absDllPath, absNiPath);
-                               return 0;
+                               return NI_ERROR_NONE;
                        } else {
                                fprintf(stderr, "Fail to create native image for %s\n", dllPath.c_str());
-                               return -1;
+                               return NI_ERROR_NO_SUCH_FILE;
                        }
                }
        } else {
@@ -205,9 +232,44 @@ static int crossgen(const std::string& dllPath, const std::string& appPath, bool
                exit(0);
        }
 
-       return 0;
+       return NI_ERROR_NONE;
 }
 
+static ni_error_e getRootPath(std::string pkgId, std::string& rootPath)
+{
+       int ret = 0;
+       char *path = 0;
+
+       uid_t uid = 0;
+
+       if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
+               _ERR("Failed to get UID");
+               return NI_ERROR_UNKNOWN;
+       }
+
+       pkgmgrinfo_pkginfo_h handle;
+       if (uid == 0) {
+               ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle);
+               if (ret != PMINFO_R_OK)
+                       return NI_ERROR_UNKNOWN;
+       } else {
+               ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
+               if (ret != PMINFO_R_OK)
+                       return NI_ERROR_UNKNOWN;
+       }
+
+       ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
+       if (ret != PMINFO_R_OK) {
+               pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+               return NI_ERROR_UNKNOWN;
+       }
+       rootPath = path;
+       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+
+       return NI_ERROR_NONE;
+}
+
+// callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
 {
        char *pkgId = NULL;
@@ -256,7 +318,7 @@ static void createCoreLibNI(bool enableR2R)
        }
 }
 
-int initNICommon(NiCommonOption* option)
+ni_error_e initNICommon(NiCommonOption* option)
 {
 #if defined(__arm__)
        // get interval value
@@ -269,19 +331,19 @@ int initNICommon(NiCommonOption* option)
 
        if (initializePluginManager("normal")) {
                fprintf(stderr, "Fail to initialize plugin manager\n");
-               return -1;
+               return NI_ERROR_UNKNOWN;
        }
        if (initializePathManager(option->runtimeDir, option->tizenFXDir, option->extraDirs)) {
                fprintf(stderr, "Fail to initialize path manager\n");
-               return -1;
+               return NI_ERROR_UNKNOWN;
        }
 
        __tpa = getTPA();
 
-       return 0;
+       return NI_ERROR_NONE;
 #else
        fprintf(stderr, "crossgen supports arm architecture only. skip ni file generation\n");
-       return -1;
+       return NI_ERROR_NOT_SUPPORTED;
 #endif
 }
 
@@ -302,13 +364,19 @@ void createNiPlatform(bool enableR2R)
        createNiUnderDirs(platformDirs, 2, enableR2R);
 }
 
-int createNiDll(const std::string& dllPath, bool enableR2R)
+ni_error_e createNiDll(const std::string& dllPath, bool enableR2R)
 {
        createCoreLibNI(enableR2R);
+       // System.Private.CoreLib.dll is generated in the createCoreLibNI function.
+       // Skip if input dll is System.Private.CoreLib.dll
+       if (dllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
+               return NI_ERROR_NONE;
+       }
+
        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);
 
@@ -321,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();
                }
        };
@@ -332,12 +400,12 @@ void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R)
        }
 }
 
-int createNiUnderPkgRoot(const std::string& pkgId, bool enableR2R)
+ni_error_e createNiUnderPkgRoot(const std::string& pkgId, bool enableR2R)
 {
        std::string pkgRoot;
-       if (getRootPath(pkgId, pkgRoot) < 0) {
+       if (getRootPath(pkgId, pkgRoot) != NI_ERROR_NONE) {
                fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
-               return -1;
+               return NI_ERROR_INVALID_PACKAGE;
        }
 
        std::string binDir = concatPath(pkgRoot, "bin");
@@ -345,24 +413,24 @@ int createNiUnderPkgRoot(const std::string& pkgId, bool enableR2R)
        std::string appTAC = concatPath(binDir, ".TAC.Release");
        std::string paths[] = {binDir, libDir, appTAC};
 
-       createNiUnderDirs(paths, 3, enableR2R);
+       createNiUnderDirs(paths, 3, enableR2R, true);
 
-       return 0;
+       return NI_ERROR_NONE;
 }
 
-int createNiDllUnderPkgRoot(const std::string& pkgId, const std::string& dllPath, bool enableR2R)
+ni_error_e createNiDllUnderPkgRoot(const std::string& pkgId, const std::string& dllPath, bool enableR2R)
 {
        std::string pkgRoot;
        if (getRootPath(pkgId, pkgRoot) < 0) {
                fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
-               return -1;
+               return NI_ERROR_INVALID_PACKAGE;
        }
 
        std::string binDir = concatPath(pkgRoot, "bin");
        std::string libDir = concatPath(pkgRoot, "lib");
        std::string paths = binDir + ":" + libDir;
 
-       return crossgen(dllPath, paths, enableR2R);
+       return crossgen(dllPath, paths, enableR2R, true);
 }
 
 void removeNiPlatform()
@@ -401,12 +469,12 @@ void removeNiUnderDirs(const std::string rootPaths[], int count)
                scanFilesInDir(rootPaths[i], convert, -1);
 }
 
-int removeNiUnderPkgRoot(const std::string& pkgId)
+ni_error_e removeNiUnderPkgRoot(const std::string& pkgId)
 {
        std::string pkgRoot;
        if (getRootPath(pkgId, pkgRoot) < 0) {
                fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
-               return -1;
+               return NI_ERROR_INVALID_PACKAGE;
        }
 
        std::string binDir = concatPath(pkgRoot, "bin");
@@ -415,33 +483,47 @@ int removeNiUnderPkgRoot(const std::string& pkgId)
 
        removeNiUnderDirs(paths, 2);
 
-       return 0;
+       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;
 }
 
-int regenerateAppNI(bool enableR2R)
+ni_error_e regenerateAppNI(bool enableR2R)
 {
        int ret = 0;
        pkgmgrinfo_appinfo_metadata_filter_h handle;
 
        ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
        if (ret != PMINFO_R_OK)
-               return -1;
+               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 -1;
+               return NI_ERROR_UNKNOWN;
        }
 
        ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, appAotCb, &enableR2R);
        if (ret != PMINFO_R_OK) {
                fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
                pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
-               return -1;
+               return NI_ERROR_UNKNOWN;
        }
 
        fprintf(stderr, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
 
        pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
-       return 0;
+       return NI_ERROR_NONE;
 }