Wrap bf::exists() to check for exception. (#270) accepted/tizen/unified/20200820.034645 submit/tizen/20200818.081345
author조웅석/Common Platform Lab(SR)/Principal Engineer/삼성전자 <ws77.cho@samsung.com>
Tue, 18 Aug 2020 08:02:14 +0000 (17:02 +0900)
committer이형주/Common Platform Lab(SR)/Staff Engineer/삼성전자 <leee.lee@samsung.com>
Tue, 18 Aug 2020 08:02:14 +0000 (17:02 +0900)
bf::exists() can throw exception. To avoid crash by exception, exist() which wrap bf::exists() is added.

NativeLauncher/inc/utils.h
NativeLauncher/installer-plugin/prefer_nuget_cache_plugin.cc
NativeLauncher/tool/tac_common.cc
NativeLauncher/util/utils.cc

index 2633024..77daa24 100644 (file)
@@ -169,6 +169,13 @@ void scanFilesInDirectory(const std::string& directory, FileReader reader, unsig
 void copySmackAndOwnership(const std::string& fromPath, const std::string& toPath, bool isSymlink = false);
 
 /**
+ * @brief check whether the path exists or not.
+ * @param[in] source path
+ * @return return true when the path exist.
+ */
+bool exist(const bf::path& path);
+
+/**
  * @brief create the new directory.
  * @param[in] source path
  * @return return true when the directory was created.
index b1f18f3..dbdae36 100644 (file)
@@ -111,7 +111,7 @@ static bool copyAssemblyCreateSymlink(std::string binPath, std::string tacDir, s
                std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
                std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
                if (!strcmp(nuget_package.c_str(), nugetPackage.c_str())) {
-                       if (bf::exists(concatPath(binPath, assembly))) {
+                       if (exist(concatPath(binPath, assembly))) {
                                if (isCreateTacDir) {
                                        if (!copyFile(concatPath(binPath, assembly), concatPath(tac_version_dir, assembly))) {
                                                _ERR("Failed to copy of %s", assembly.c_str());
@@ -157,7 +157,7 @@ static void copyLibraryCreateSymlink(const char* pkgId, std::vector<std::string>
                std::string filename = library.substr(library.rfind('/') + 1);
                std::string fileSha = filename + ".." + librarySha.substr(librarySha.find(':') + 1);
                bool fileCopied = false;
-               if (!bf::exists(concatPath(tlcDir, fileSha))) {
+               if (!exist(concatPath(tlcDir, fileSha))) {
                        if (!copyFile(library, concatPath(tlcDir, fileSha))) {
                                _ERR("Failed to copy of %s", filename.c_str());
                                continue;
@@ -268,7 +268,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *app
 
                std::string tac_version_dir = concatPath(__DOTNET_DIR, np);
                std::string sha256_info = concatPath(tac_version_dir, TAC_SHA_256_INFO);
-               if (!bf::exists(tac_version_dir)) {
+               if (!exist(tac_version_dir)) {
                        _INFO("Create tac_version_dir [%s]", tac_version_dir.c_str());
                        if (!createDir(tac_version_dir)) {
                                _ERR("Cannot create directory: %s", tac_version_dir.c_str());
@@ -514,7 +514,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *app
 
                        std::string tac_version_dir = concatPath(__DOTNET_DIR, np);
                        std::string sha256_info = concatPath(tac_version_dir, TAC_SHA_256_INFO);
-                       if (!bf::exists(tac_version_dir)) {
+                       if (!exist(tac_version_dir)) {
                                _INFO("Create tac_version_dir [%s]", tac_version_dir.c_str());
                                if (!createDir(tac_version_dir)) {
                                        _ERR("Cannot create directory: %s", tac_version_dir.c_str());
index 4cc26fd..d56e519 100644 (file)
@@ -112,7 +112,7 @@ static int tac_restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
        for (auto& npAssembly : depsJsonParser(rootPath, execName)) {
                std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
                std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
-               if (bf::exists(tacDir) && bf::exists(concatPath(tacDir, assemblyName))) {
+               if (exist(tacDir) && exist(concatPath(tacDir, assemblyName))) {
                        parserData.push_back(nugetPackage);
                }
        }
@@ -226,7 +226,7 @@ tac_error_e disableTACPackage(const std::string& pkgId)
        std::string binDir = concatPath(rootPath, "bin");
        std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
        std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
-       if (bf::exists(tacDir)) {
+       if (exist(tacDir)) {
                try {
                        for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
                                std::string symPath = symlinkAssembly.path().string();
@@ -286,7 +286,7 @@ tac_error_e enableTACPackage(const std::string& pkgId)
                std::string binDir = concatPath(rootPath, "bin");
                std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
                std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
-               if (!bf::exists(tacDir)) {
+               if (!exist(tacDir)) {
                        if (!createDir(tacDir)) {
                                fprintf(stderr, "Cannot create directory: %s\n", tacDir.c_str());
                                return TAC_ERROR_UNKNOWN;
@@ -298,21 +298,21 @@ tac_error_e enableTACPackage(const std::string& pkgId)
                                std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
                                std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
                                std::string nugetPath = concatPath(__DOTNET_DIR, nugetPackage);
-                               if (bf::exists(nugetPath)) {
+                               if (exist(nugetPath)) {
                                        std::string originPath = concatPath(nugetPath, assemblyName);
-                                       if (bf::exists(originPath)) {
+                                       if (exist(originPath)) {
                                                enableNuget.push_back(originPath);
                                        }
                                }
                        }
 
                        for (auto& originPath : enableNuget) {
-                               if (bf::exists(originPath)) {
+                               if (exist(originPath)) {
                                        std::string fileName = originPath.substr(originPath.rfind('/') + 1);
                                        std::string NIFileName = fileName.substr(0, fileName.rfind(".dll")) + ".ni.dll";
-                                       if (bf::exists(binNIDir)) {
+                                       if (exist(binNIDir)) {
                                                std::string originNIPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
-                                               if (bf::exists(originNIPath)) {
+                                               if (exist(originNIPath)) {
                                                        bf::create_symlink(originNIPath, concatPath(tacDir, NIFileName));
                                                        fprintf(stdout, "%s symbolic link file generated successfully.\n", concatPath(tacDir, NIFileName).c_str());
                                                        copySmackAndOwnership(tacDir.c_str(), concatPath(tacDir, NIFileName).c_str(), true);
@@ -353,7 +353,7 @@ std::vector<std::string> depsJsonParser(const std::string& rootPath, const std::
        std::string depsJsonName = execName.substr(0, execName.rfind(".dll")) + ".deps.json";
        std::string depsJsonPath = concatPath(rootPath, depsJsonName);
        try {
-               if (bf::exists(depsJsonPath)) {
+               if (exist(depsJsonPath)) {
                        std::ifstream ifs(depsJsonPath);
                        Json::CharReaderBuilder reader;
                        Json::Value root;
@@ -430,7 +430,7 @@ std::vector<std::string> getLibrariesInfo(const std::string& rootPath)
 {
        std::vector<std::string> LibrariesInfo;
        std::string runtimesDir = concatPath(rootPath, "bin/runtimes");
-       if (!bf::exists(runtimesDir))
+       if (!exist(runtimesDir))
                return LibrariesInfo;
 
        auto convert = [&LibrariesInfo](const std::string& filepath, const std::string& filename) {
@@ -467,7 +467,7 @@ static int tlc_restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
 
        for (auto& librarySha : getLibrariesInfo(rootPath)) {
                std::string library = librarySha.substr(0, librarySha.find(':'));
-               if (bf::exists(library)) {
+               if (exist(library)) {
                        std::string fileSha = library.substr(library.rfind('/') + 1) + ".." + librarySha.substr(librarySha.find(':') + 1);
                        char *sql = sqlite3_mprintf("INSERT INTO TLC (PKGID, LIBRARY) VALUES (%Q, %Q);", pkgId, fileSha.c_str());
                        insertDB(tlc_db, sql);
index 2864a6f..d6ba2de 100644 (file)
@@ -456,7 +456,7 @@ static bool setDirOwnershipAndPermissions(const bf::path& path, bf::perms permis
 
 static bool copyOwnershipAndPermissions(const bf::path& path, const bf::path& path2)
 {
-       if (!bf::exists(path)) {
+       if (!exist(path)) {
                _ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), path2.c_str());
                return false;
        }
@@ -472,9 +472,20 @@ static bool copyOwnershipAndPermissions(const bf::path& path, const bf::path& pa
        return true;
 }
 
+bool exist(const bf::path& path)
+{
+       bs::error_code error;
+       bf::exists(path, error);
+       if (error) {
+               _ERR("Failed to check %s exists : %s", path.c_str(), error.message().c_str());
+               return false;
+       }
+       return true;
+}
+
 bool createDir(const bf::path& path)
 {
-       if (bf::exists(path)) {
+       if (exist(path)) {
                return true;
        }
        bs::error_code error;
@@ -490,11 +501,11 @@ 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)) {
+               if (!exist(path1) || !bf::is_directory(path1)) {
                        _ERR("Source directory %s does not exist or is not a directory", path1.c_str());
                        return false;
                }
-               if (!bf::exists(path2)) {
+               if (!exist(path2)) {
                        // Create the destination directory
                        if (!createDir(path2)) {
                                _ERR("Unable to create destination directory %s", path2.c_str());
@@ -523,7 +534,7 @@ bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags)
                        bf::path current(file->path());
                        bf::path target = path2 / current.filename();
                        if (bf::is_symlink(symlink_status(current))) {
-                               if ((flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE)) && bf::exists(target)) {
+                               if ((flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE)) && exist(target)) {
                                        continue;
                                }
                                bs::error_code error;
@@ -538,7 +549,7 @@ bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags)
                                        return false;
                                }
                        } else {
-                               if ((flags & FS_MERGE_SKIP) && bf::exists(target)) {
+                               if ((flags & FS_MERGE_SKIP) && exist(target)) {
                                        continue;
                                }
                                bf::path destination = target;
@@ -571,7 +582,7 @@ 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)) {
+       if (!exist(path1)) {
                return false;
        }
        bf::copy_file(path1, path2, bf::copy_option::overwrite_if_exists, error);
@@ -584,7 +595,7 @@ bool copyFile(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)) {
+       if (!exist(path1) || exist(path2)) {
                return false;
        }
        bs::error_code error;
@@ -607,7 +618,7 @@ bool moveFile(const bf::path& path1, const bf::path& path2)
 
 bool removeFile(const bf::path& path)
 {
-       if (!bf::exists(path)) {
+       if (!exist(path)) {
                return true;
        }
        bs::error_code error;
@@ -621,7 +632,7 @@ bool removeFile(const bf::path& path)
 
 bool removeAll(const bf::path& path)
 {
-       if (!exists(path)) {
+       if (!exist(path)) {
                return true;
        }
        bs::error_code error;