Code cleanup (#251)
[platform/core/dotnet/launcher.git] / NativeLauncher / util / utils.cc
index f399b2c..5b047ae 100644 (file)
@@ -60,23 +60,6 @@ bool isNativeImage(const std::string& fileName)
        return iCompare(fileName, fileName.size()-7, ".ni", 0, 3);
 }
 
-bool cmdOptionExists(char** begin, char** end, const std::string& option)
-{
-       return std::find(begin, end, option) != end;
-}
-
-std::string readSelfPath()
-{
-       char buff[PATH_MAX];
-       ssize_t len = ::readlink("/proc/self/exe", buff, sizeof(buff)-1);
-       if (len != -1) {
-               buff[len] = '\0';
-               return std::string(buff);
-       }
-
-       return "";
-}
-
 std::string concatPath(const std::string& path1, const std::string& path2)
 {
        std::string path(path1);
@@ -112,109 +95,151 @@ std::string getAbsolutePath(const std::string& path)
        return absPath;
 }
 
-int getRootPath(const std::string& pkgId, std::string& rootPath)
+std::string getRootPath(const std::string& pkgId)
 {
        int ret = 0;
        char *path = 0;
        uid_t uid = 0;
+       std::string rootPath;
 
        if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
                _ERR("Failed to get UID");
-               return -1;
+               return rootPath;
        }
 
        pkgmgrinfo_pkginfo_h handle;
        if (uid == 0) {
                ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle);
                if (ret != PMINFO_R_OK) {
-                       return -1;
+                       return rootPath;
                }
        } else {
                ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
                if (ret != PMINFO_R_OK) {
-                       return -1;
+                       return rootPath;
                }
        }
 
        ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
        if (ret != PMINFO_R_OK) {
                pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-               return -1;
+               return rootPath;
        }
        rootPath = path;
        pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-       return 0;
+
+       return rootPath;
 }
 
-int getExecName(const std::string& pkgId, std::string& execName)
+std::string getExecName(const std::string& pkgId)
 {
        char *exec = NULL;
        char *appId = 0;
+       std::string execName;
 
        pkgmgrinfo_pkginfo_h pkg_handle;
        int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &pkg_handle);
        if (ret != PMINFO_R_OK) {
-               return -1;
+               return execName;
        }
        ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId);
        if (ret != PMINFO_R_OK) {
                pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
-               return -1;
+               return execName;
        }
 
        pkgmgrinfo_appinfo_h app_handle;
        ret = pkgmgrinfo_appinfo_get_appinfo(appId, &app_handle);
        if (ret != PMINFO_R_OK) {
                pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
-               return -1;
+               return execName;
        }
        ret = pkgmgrinfo_appinfo_get_exec(app_handle, &exec);
        if (ret != PMINFO_R_OK) {
                pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
                pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
-               return -1;
+               return execName;
        }
        execName = std::string(exec).substr(std::string(exec).rfind('/') + 1);
 
        pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
        pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
-       return 0;
+
+       return execName;
 }
 
-int getMetadataValue(const std::string& pkgId, const std::string& metadataKey, std::string& metadataValue)
+std::string getAppType(const std::string& pkgId)
+{
+       char *appId = 0;
+       char *type = 0;
+       std::string appType;
+
+       pkgmgrinfo_pkginfo_h pkg_handle;
+       int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &pkg_handle);
+       if (ret != PMINFO_R_OK) {
+               return appType;
+       }
+       ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId);
+       if (ret != PMINFO_R_OK) {
+               pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
+               return appType;
+       }
+
+       pkgmgrinfo_appinfo_h app_handle;
+       ret = pkgmgrinfo_appinfo_get_appinfo(appId, &app_handle);
+       if (ret != PMINFO_R_OK) {
+               pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
+               return appType;
+       }
+       ret = pkgmgrinfo_appinfo_get_apptype(app_handle, &type);
+       if (ret != PMINFO_R_OK) {
+               pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
+               pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
+               return appType;
+       }
+       appType = type;
+
+       pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
+       pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
+
+       return appType;
+}
+
+std::string getMetadataValue(const std::string& pkgId, const std::string& key)
 {
        char *value = NULL;
        char *appId = 0;
+       std::string metadataValue;
 
        pkgmgrinfo_pkginfo_h pkg_handle;
        int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &pkg_handle);
        if (ret != PMINFO_R_OK) {
-               return -1;
+               return metadataValue;
        }
        ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId);
        if (ret != PMINFO_R_OK) {
                pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
-               return -1;
+               return metadataValue;
        }
 
        pkgmgrinfo_appinfo_h app_handle;
        ret = pkgmgrinfo_appinfo_get_appinfo(appId, &app_handle);
        if (ret != PMINFO_R_OK) {
                pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
-               return -1;
+               return metadataValue;
        }
-       ret = pkgmgrinfo_appinfo_get_metadata_value(app_handle, metadataKey.c_str(), &value);
+       ret = pkgmgrinfo_appinfo_get_metadata_value(app_handle, key.c_str(), &value);
        if (ret != PMINFO_R_OK) {
                pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
                pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
-               //Does not return error because the metadata key may not exist.
-               return 0;
+               return metadataValue;
        }
        metadataValue = std::string(value);
 
        pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
        pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
-       return 0;
+
+       return metadataValue;
 }
 
 std::string getBaseName(const std::string& path)
@@ -252,24 +277,14 @@ bool isDirectory(const std::string& path)
        struct stat sb;
        if (stat(path.c_str(), &sb) != 0) {
                return false;
-       } else if (sb.st_mode & S_IFDIR) {
+       }
+       if (sb.st_mode & S_IFDIR) {
                return true;
        } else {
                return false;
        }
 }
 
-uintptr_t getFileSize(const std::string& path)
-{
-       struct stat sb;
-
-       if (stat(path.c_str(), &sb) == 0) {
-               return sb.st_size;
-       }
-
-       return 0;
-}
-
 std::string getAssemblyNameFromPath(const std::string& path)
 {
        std::string ret(getFileName(path));
@@ -284,7 +299,8 @@ std::string getAssemblyNameFromPath(const std::string& path)
        return ret;
 }
 
-void addAssembliesFromDirectories(const std::vector<std::string>& directories, std::string& list) {
+void addAssembliesFromDirectories(const std::vector<std::string>& directories, std::string& list)
+{
        std::vector<std::string> assems;
        std::unordered_map<std::string, std::string> assemPaths;
 
@@ -401,7 +417,8 @@ void copySmackAndOwnership(const std::string& fromPath, const std::string& toPat
        }
 }
 
-static bool setOwnership(const bf::path& path, uid_t uid, gid_t gid) {
+static bool setOwnership(const bf::path& path, uid_t uid, gid_t gid)
+{
        int fd = open(path.c_str(), O_RDONLY);
        if (fd < 0) {
                _ERR("Can't open directory: %s", path.c_str());
@@ -416,7 +433,8 @@ static bool setOwnership(const bf::path& path, uid_t uid, gid_t gid) {
        return true;
 }
 
-static bool setDirPermissions(const bf::path& path, bf::perms permissions) {
+static bool setDirPermissions(const bf::path& path, bf::perms permissions)
+{
        bs::error_code error;
        bf::permissions(path, permissions, error);
        if (error) {
@@ -426,7 +444,8 @@ static bool setDirPermissions(const bf::path& path, bf::perms permissions) {
        return true;
 }
 
-static bool setDirOwnershipAndPermissions(const bf::path& path, bf::perms permissions, uid_t uid, gid_t gid) {
+static bool setDirOwnershipAndPermissions(const bf::path& path, bf::perms permissions, uid_t uid, gid_t gid)
+{
        if (!setOwnership(path, uid, gid)) {
                _ERR("Failed to change owner: %s, (uid: %d, gid: %d)", path.c_str(), uid, gid);
                return false;
@@ -438,7 +457,8 @@ static bool setDirOwnershipAndPermissions(const bf::path& path, bf::perms permis
        return true;
 }
 
-static bool copyOwnershipAndPermissions(const bf::path& path, const bf::path& path2) {
+static bool copyOwnershipAndPermissions(const bf::path& path, const bf::path& path2)
+{
        if (!bf::exists(path)) {
                _ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), path2.c_str());
                return false;
@@ -455,7 +475,8 @@ static bool copyOwnershipAndPermissions(const bf::path& path, const bf::path& pa
        return true;
 }
 
-bool createDir(const bf::path& path) {
+bool createDir(const bf::path& path)
+{
        if (bf::exists(path)) {
                return true;
        }
@@ -468,7 +489,8 @@ bool createDir(const bf::path& path) {
        return true;
 }
 
-bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags) {
+bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags)
+{
        try {
                // Check whether the function call is valid
                if (!bf::exists(path1) || !bf::is_directory(path1)) {
@@ -549,7 +571,8 @@ bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags) {
        return true;
 }
 
-bool copyFile(const bf::path& path1, const bf::path& path2) {
+bool copyFile(const bf::path& path1, const bf::path& path2)
+{
        bs::error_code error;
        if (!bf::exists(path1)) {
                return false;
@@ -562,7 +585,8 @@ bool copyFile(const bf::path& path1, const bf::path& path2) {
        return true;
 }
 
-bool moveFile(const bf::path& path1, const bf::path& path2) {
+bool moveFile(const bf::path& path1, const bf::path& path2)
+{
        if (!bf::exists(path1) || bf::exists(path2)) {
                return false;
        }
@@ -584,7 +608,8 @@ bool moveFile(const bf::path& path1, const bf::path& path2) {
        return true;
 }
 
-bool removeFile(const bf::path& path) {
+bool removeFile(const bf::path& path)
+{
        if (!bf::exists(path)) {
                return true;
        }
@@ -597,7 +622,8 @@ bool removeFile(const bf::path& path) {
        return true;
 }
 
-bool removeAll(const bf::path& path) {
+bool removeAll(const bf::path& path)
+{
        if (!exists(path)) {
                return true;
        }