Fixed svace (376818, 386676, 386690)
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / tac_common.cc
index 1f9c76b..78c20cf 100644 (file)
@@ -42,29 +42,34 @@ std::vector<std::string> restoreNuget;
 void cleanupDirectory()
 {
        std::vector<std::string> removeNuget;
-       for (auto& nuget : bf::recursive_directory_iterator(__TAC_DIR)) {
-               bool isExist = false;
-               std::string nugetPath = nuget.path().string();
-               for (auto& restore : restoreNuget) {
-                       if (!bf::is_directory(nugetPath)) {
-                               isExist = true;
+       try {
+               for (auto& nuget : bf::recursive_directory_iterator(__TAC_DIR)) {
+                       bool isExist = false;
+                       std::string nugetPath = nuget.path().string();
+                       for (auto& restore : restoreNuget) {
+                               if (!bf::is_directory(nugetPath)) {
+                                       isExist = true;
+                               }
+                               if (strstr(nugetPath.c_str(), restore.c_str()) != NULL) {
+                                       isExist = true;
+                                       break;
+                               }
                        }
-                       if (strstr(nugetPath.c_str(), restore.c_str()) != NULL) {
-                               isExist = true;
-                               break;
+                       if (!isExist) {
+                               removeNuget.push_back(nugetPath);
                        }
                }
-               if (!isExist) {
-                       removeNuget.push_back(nugetPath);
-               }
-       }
 
-       for (auto& rm : removeNuget) {
-               if (!removeAll(rm)) {
-                       _ERR("Failed to remove of %s", rm.c_str());
+               for (auto& rm : removeNuget) {
+                       if (!removeAll(rm)) {
+                               _ERR("Failed to remove of %s", rm.c_str());
+                       }
                }
+               removeNuget.clear();
+       } catch (const bf::filesystem_error& error) {
+               _ERR("Failed to recursive directory: %s", error.what());
+               return;
        }
-       removeNuget.clear();
 }
 
 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
@@ -206,22 +211,27 @@ tac_error_e resetTACPackage(const std::string& pkgId)
        std::string binDir = concatPath(pkgRoot, "bin");
        std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
        if (bf::exists(tacDir)) {
-               for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
-                       std::string symPath = symlinkAssembly.path().string();
-                       if (bf::is_symlink(symPath)) {
-                               if (isNativeImage(symPath)) {
-                                       tacNativeImage.push_back(symPath);
+               try {
+                       for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
+                               std::string symPath = symlinkAssembly.path().string();
+                               if (bf::is_symlink(symPath)) {
+                                       if (isNativeImage(symPath)) {
+                                               tacNativeImage.push_back(symPath);
+                                       }
                                }
                        }
-               }
-               for (auto& path : tacNativeImage) {
-                       if (!removeFile(path)) {
-                               fprintf(stderr, "Failed to remove of %s", path.c_str());
-                               return TAC_ERROR_UNKNOWN;
+                       for (auto& path : tacNativeImage) {
+                               if (!removeFile(path)) {
+                                       fprintf(stderr, "Failed to remove of %s", path.c_str());
+                                       return TAC_ERROR_UNKNOWN;
+                               }
                        }
+                       tacNativeImage.clear();
+               } catch (const bf::filesystem_error& error) {
+                       _ERR("Failed to recursive directory: %s", error.what());
+                       return TAC_ERROR_UNKNOWN;
                }
        }
-       tacNativeImage.clear();
        return TAC_ERROR_NONE;
 }
 
@@ -236,32 +246,37 @@ tac_error_e createTACPackage(const std::string& pkgId)
        std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
        std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
        if (bf::exists(tacDir)) {
-               for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
-                       std::string symPath = symlinkAssembly.path().string();
-                       if (bf::is_symlink(symPath)) {
-                               if (!isNativeImage(symPath)) {
-                                       std::string originPath = bf::read_symlink(symPath).string();
-                                       std::string originNiPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
-                                       if (!bf::exists(originNiPath)) {
-                                               if(createNiDll(originPath, false) != NI_ERROR_NONE) {
-                                                       fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str());
-                                                       return TAC_ERROR_UNKNOWN;
+               try {
+                       for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
+                               std::string symPath = symlinkAssembly.path().string();
+                               if (bf::is_symlink(symPath)) {
+                                       if (!isNativeImage(symPath)) {
+                                               std::string originPath = bf::read_symlink(symPath).string();
+                                               std::string originNiPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
+                                               if (!bf::exists(originNiPath)) {
+                                                       if(createNiDll(originPath, false) != NI_ERROR_NONE) {
+                                                               fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str());
+                                                               return TAC_ERROR_UNKNOWN;
+                                                       }
                                                }
-                                       }
-                                       std::string symNIPath = symPath.substr(0, symPath.rfind(".dll")) + ".ni.dll";
-                                       if (!bf::exists(symNIPath)) {
-                                               bf::create_symlink(originNiPath, symNIPath);
-                                               fprintf(stderr, "%s symbolic link file generated successfully.\n", symNIPath.c_str());
-                                               updateAssemblyInfo(tacDir.c_str(), symNIPath.c_str(), true);
-
-                                               std::string NIFileName = symNIPath.substr(symNIPath.rfind('/') + 1);
-                                               if (!removeFile(concatPath(binNIDir, NIFileName))) {
-                                                       fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, NIFileName).c_str());
-                                                       return TAC_ERROR_UNKNOWN;
+                                               std::string symNIPath = symPath.substr(0, symPath.rfind(".dll")) + ".ni.dll";
+                                               if (!bf::exists(symNIPath)) {
+                                                       bf::create_symlink(originNiPath, symNIPath);
+                                                       fprintf(stderr, "%s symbolic link file generated successfully.\n", symNIPath.c_str());
+                                                       updateAssemblyInfo(tacDir.c_str(), symNIPath.c_str(), true);
+
+                                                       std::string NIFileName = symNIPath.substr(symNIPath.rfind('/') + 1);
+                                                       if (!removeFile(concatPath(binNIDir, NIFileName))) {
+                                                               fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, NIFileName).c_str());
+                                                               return TAC_ERROR_UNKNOWN;
+                                                       }
                                                }
                                        }
                                }
                        }
+               } catch (const bf::filesystem_error& error) {
+                       _ERR("Failed to recursive directory: %s", error.what());
+                       return TAC_ERROR_UNKNOWN;
                }
        }
        return TAC_ERROR_NONE;
@@ -297,31 +312,38 @@ tac_error_e disableTACPackage(const std::string& pkgId)
        std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
        std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
        if (bf::exists(tacDir)) {
-               for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
-                       std::string symPath = symlinkAssembly.path().string();
-                       std::string fileName = symlinkAssembly.path().filename().string();
-                       if (bf::is_symlink(symPath)) {
-                               std::string originPath = bf::read_symlink(symPath).string();
-                               if (!isNativeImage(symPath)) {
-                                       if (!copyFile(originPath, concatPath(binDir, fileName))) {
-                                               fprintf(stderr, "Failed to copy of %s\n", concatPath(binDir, fileName).c_str());
-                                               return TAC_ERROR_UNKNOWN;
-                                       }
-                                       updateAssemblyInfo(binDir.c_str(), concatPath(binDir, fileName).c_str());
-                               } else {
-                                       if (!copyFile(originPath, concatPath(binNIDir, fileName))) {
-                                               fprintf(stderr, "Failed to copy of %s\n", concatPath(binNIDir, fileName).c_str());
-                                               return TAC_ERROR_UNKNOWN;
+               try {
+                       for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
+                               std::string symPath = symlinkAssembly.path().string();
+                               std::string fileName = symlinkAssembly.path().filename().string();
+                               if (bf::is_symlink(symPath)) {
+                                       std::string originPath = bf::read_symlink(symPath).string();
+                                       if (!isNativeImage(symPath)) {
+                                               std::string dllPath = concatPath(binDir, fileName);
+                                               if (!copyFile(originPath, dllPath)) {
+                                                       fprintf(stderr, "Failed to copy of %s\n", dllPath.c_str());
+                                                       return TAC_ERROR_UNKNOWN;
+                                               }
+                                               updateAssemblyInfo(binDir.c_str(), concatPath(binDir, fileName).c_str());
+                                       } else {
+                                               std::string niPath = concatPath(binNIDir, fileName);
+                                               if (!copyFile(originPath, niPath)) {
+                                                       fprintf(stderr, "Failed to copy of %s\n", niPath.c_str());
+                                                       return TAC_ERROR_UNKNOWN;
+                                               }
+                                               updateAssemblyInfo(binDir.c_str(), niPath.c_str());
                                        }
-                                       updateAssemblyInfo(binDir.c_str(), concatPath(binNIDir, fileName).c_str());
                                }
                        }
+                       if (!removeAll(tacDir)) {
+                               fprintf(stderr, "Failed to remove of %s\n", tacDir.c_str());
+                               return TAC_ERROR_UNKNOWN;
+                       }
+               } catch (const bf::filesystem_error& error) {
+                       _ERR("Failed to recursive directory: %s", error.what());
+                       return TAC_ERROR_UNKNOWN;
                }
        }
-       if (!removeAll(tacDir)) {
-               fprintf(stderr, "Failed to remove of %s\n", tacDir.c_str());
-               return TAC_ERROR_UNKNOWN;
-       }
        return TAC_ERROR_NONE;
 }