log manager code cleanup
[platform/core/dotnet/launcher.git] / NativeLauncher / util / utils.cc
index a33eec2..2c29518 100644 (file)
@@ -30,6 +30,7 @@
 #include <unordered_map>
 #include <vector>
 #include <iterator>
+#include <fstream>
 #include <sstream>
 #include <map>
 
@@ -101,9 +102,11 @@ void splitPath(const std::string& path, std::vector<std::string>& out)
 std::string absolutePath(const std::string& path)
 {
        std::string absPath;
-       char realPath[PATH_MAX];
-       if (realpath(path.c_str(), realPath) != nullptr && realPath[0] != '\0')
+       char *realPath = realpath(path.c_str(), NULL);
+       if (realPath) {
                absPath.assign(realPath);
+               free(realPath);
+       }
 
        return absPath;
 }
@@ -142,6 +145,77 @@ int getRootPath(std::string pkgId, std::string& rootPath)
        return 0;
 }
 
+int getExecName(std::string pkgId, std::string& execName)
+{
+       char *exec = NULL;
+       char *appId = 0;
+
+       pkgmgrinfo_pkginfo_h pkg_handle;
+       int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &pkg_handle);
+       if (ret != PMINFO_R_OK) {
+               return -1;
+       }
+       ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId);
+       if (ret != PMINFO_R_OK) {
+               pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
+               return -1;
+       }
+
+       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;
+       }
+       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;
+       }
+       execName = std::string(exec).substr(std::string(exec).rfind('/') + 1);
+
+       pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
+       pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
+       return 0;
+}
+
+int getMetadataValue(std::string pkgId, std::string metadataKey, std::string& metadataValue)
+{
+       char *value = NULL;
+       char *appId = 0;
+
+       pkgmgrinfo_pkginfo_h pkg_handle;
+       int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &pkg_handle);
+       if (ret != PMINFO_R_OK) {
+               return -1;
+       }
+       ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId);
+       if (ret != PMINFO_R_OK) {
+               pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
+               return -1;
+       }
+
+       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;
+       }
+       ret = pkgmgrinfo_appinfo_get_metadata_value(app_handle, metadataKey.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;
+       }
+       metadataValue = std::string(value);
+
+       pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
+       pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
+       return 0;
+}
+
 std::string baseName(const std::string& path)
 {
        auto pos = path.find_last_of(PATH_SEPARATOR);
@@ -152,12 +226,37 @@ std::string baseName(const std::string& path)
        return path;
 }
 
+std::string replaceAll(const std::string &str, const std::string &pattern, const std::string &replace)
+{
+       std::string result = str;
+       std::string::size_type pos = 0;
+       std::string::size_type offset = 0;
+
+       while ((pos = result.find(pattern, offset)) != std::string::npos) {
+               result.replace(result.begin() + pos, result.begin() + pos + pattern.size(), replace);
+               offset = pos + replace.size();
+       }
+
+       return result;
+}
+
 bool isFileExist(const std::string& path)
 {
        struct stat sb;
        return stat(path.c_str(), &sb) == 0;
 }
 
+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 stripNiDLL(const std::string& path)
 {
        std::string niPath(path);
@@ -212,7 +311,7 @@ void scanFilesInDir(const std::string& directory, FileReader reader, unsigned in
        bool isDir;
 
        if (strstr(directory.c_str(), TAC_SYMLINK_SUB_DIR) != NULL)
-               return; // skip nitool --regen-all-app (--r2r)
+               return;
 
        dir = opendir(directory.c_str());
 
@@ -253,23 +352,39 @@ void scanFilesInDir(const std::string& directory, FileReader reader, unsigned in
        closedir(dir);
 }
 
-void updateAssemblyInfo(const std::string& getPath, const std::string& setPath)
+void copySmackAndOwnership(const std::string& fromPath, const std::string& toPath, bool isSymlink)
 {
        char* label = NULL;
+       struct stat info;
 
-       // change smack label
-       if (smack_getlabel(getPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
-               if (smack_setlabel(setPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
-                       fprintf(stderr, "Fail to set smack label\n");
+       if (isSymlink) {
+               // change smack label for symbolic link.
+               if (smack_lgetlabel(fromPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
+                       if (smack_lsetlabel(toPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
+                               fprintf(stderr, "Fail to set smack label\n");
+                       }
+                       free(label);
                }
-               free(label);
-       }
 
-       // change owner and groups for generated ni file.
-       struct stat info;
-       if (!stat(getPath.c_str(), &info)) {
-               if (chown(setPath.c_str(), info.st_uid, info.st_gid) == -1)
-                       fprintf(stderr, "Failed to change owner and group name\n");
+               // change owner and groups for symbolic link.
+               if (!stat(fromPath.c_str(), &info)) {
+                       if (lchown(toPath.c_str(), info.st_uid, info.st_gid) == -1)
+                               fprintf(stderr, "Failed to change owner and group name\n");
+               }
+       } else {
+               // change smack label
+               if (smack_getlabel(fromPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
+                       if (smack_setlabel(toPath.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.
+               if (!stat(fromPath.c_str(), &info)) {
+                       if (chown(toPath.c_str(), info.st_uid, info.st_gid) == -1)
+                               fprintf(stderr, "Failed to change owner and group name\n");
+               }
        }
 }
 
@@ -423,6 +538,9 @@ bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags) {
 
 bool copyFile(const bf::path& path1, const bf::path& path2) {
        bs::error_code error;
+       if (!bf::exists(path1)) {
+               return false;
+       }
        bf::copy_file(path1, path2, bf::copy_option::overwrite_if_exists, error);
        if (error) {
                _ERR("copy file %s due to error [%s]", path1.c_str(), error.message().c_str());
@@ -432,7 +550,7 @@ bool copyFile(const bf::path& path1, const bf::path& path2) {
 }
 
 bool moveFile(const bf::path& path1, const bf::path& path2) {
-       if (bf::exists(path2)) {
+       if (!bf::exists(path1) || bf::exists(path2)) {
                return false;
        }
        bs::error_code error;
@@ -477,4 +595,4 @@ bool removeAll(const bf::path& path) {
                return false;
        }
        return true;
-}
\ No newline at end of file
+}