From 23c891d9f19cd800c2ab4b18be3f6fcb4c5dca17 Mon Sep 17 00:00:00 2001 From: "j-h.choi" Date: Thu, 10 Jun 2021 15:10:02 +0900 Subject: [PATCH] Added missing exception handling for boost_filesystem Change-Id: Ief5956ffa9683c5f4047f3abfcb0ba44513c401b --- NativeLauncher/inc/utils.h | 7 +++++ .../installer-plugin/prefer_nuget_cache_plugin.cc | 32 +++++++++++++++----- NativeLauncher/tool/tac_common.cc | 15 ++++++++-- NativeLauncher/tool/tpatool.cc | 2 +- NativeLauncher/util/utils.cc | 35 ++++++++++++++-------- 5 files changed, 68 insertions(+), 23 deletions(-) diff --git a/NativeLauncher/inc/utils.h b/NativeLauncher/inc/utils.h index 98e1da0..c7910e2 100644 --- a/NativeLauncher/inc/utils.h +++ b/NativeLauncher/inc/utils.h @@ -136,6 +136,13 @@ void splitPath(const std::string& path, std::vector& out); bool isFile(const std::string& path); /** + * @brief check symlink file + * @param[in] source path + * @return bool + */ +bool isSymlinkFile(const std::string& path); + +/** * @brief check directory exists * @param[in] source path * @return bool diff --git a/NativeLauncher/installer-plugin/prefer_nuget_cache_plugin.cc b/NativeLauncher/installer-plugin/prefer_nuget_cache_plugin.cc index a8011a3..0e785ce 100644 --- a/NativeLauncher/installer-plugin/prefer_nuget_cache_plugin.cc +++ b/NativeLauncher/installer-plugin/prefer_nuget_cache_plugin.cc @@ -119,7 +119,13 @@ static bool copyAssemblyCreateSymlink(std::string binPath, std::string tacDir, s break; } } - bf::create_symlink(concatPath(tac_version_dir, assembly), concatPath(tacDir, assembly)); + bs::error_code error; + bf::create_symlink(concatPath(tac_version_dir, assembly), concatPath(tacDir, assembly), error); + if (error) { + _ERR("Failed to create symlink %s file", concatPath(tacDir, assembly).c_str()); + nuget_restoration = true; + break; + } copySmackAndOwnership(tacDir, concatPath(tacDir, assembly), true); if (!removeFile(concatPath(binPath, assembly))) { @@ -173,7 +179,17 @@ static void copyLibraryCreateSymlink(const char* pkgId, std::vector } continue; } - bf::create_symlink(concatPath(tlcDir, fileSha), library); + bs::error_code error; + bf::create_symlink(concatPath(tlcDir, fileSha), library, error); + if (error) { + _ERR("Failed to create symlink %s file", library.c_str()); + copyFile(concatPath(tlcDir, fileSha), library); + copySmackAndOwnership(getBaseName(library), library); + if (fileCopied) { + removeFile(concatPath(tlcDir, fileSha)); + } + continue; + } copySmackAndOwnership(getBaseName(library), library, true); char *sql = sqlite3_mprintf("INSERT INTO TLC (PKGID, LIBRARY) VALUES (%Q, %Q);", pkgId, fileSha.c_str()); @@ -269,6 +285,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *app _INFO("TAC name : %s", tac_name.c_str()); _INFO("TAC version : %s", tac_version.c_str()); + bs::error_code error; std::string tac_version_dir = concatPath(__DOTNET_DIR, np); std::string sha256_info = concatPath(tac_version_dir, TAC_SHA_256_INFO); if (!exist(tac_version_dir)) { @@ -279,7 +296,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *app return -1; } createDirectories.push_back(tac_version_dir); - if (!bf::is_symlink(sha256_info)) { + if (!isSymlinkFile(sha256_info)) { createSHA256Info(sha256_info, np); } else { _ERR("Failed to create sha256_info. Symbolic link is detected"); @@ -310,7 +327,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *app sqlite3_free(sql); } else { _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str()); - if (!bf::is_symlink(sha256_info)) { + if (!isSymlinkFile(sha256_info)) { if (compareSHA256Info(sha256_info, np)) { if (copyAssemblyCreateSymlink(binPath, tac_dir, np, false)) { _ERR("Failed to create symlink"); @@ -516,6 +533,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *app _INFO("TAC name : %s", tac_name.c_str()); _INFO("TAC version : %s", tac_version.c_str()); + bs::error_code error; std::string tac_version_dir = concatPath(__DOTNET_DIR, np); std::string sha256_info = concatPath(tac_version_dir, TAC_SHA_256_INFO); if (!exist(tac_version_dir)) { @@ -526,7 +544,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *app return -1; } createDirectories.push_back(tac_version_dir); - if (!bf::is_symlink(sha256_info)) { + if (!isSymlinkFile(sha256_info)) { createSHA256Info(sha256_info, np); } else { _ERR("Failed to create sha256_info. Symbolic link is detected"); @@ -581,7 +599,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *app } } else { _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str()); - if (!bf::is_symlink(sha256_info)) { + if (!isSymlinkFile(sha256_info)) { if (compareSHA256Info(sha256_info, np)) { if (copyAssemblyCreateSymlink(binPath, tac_dir, np, false)) { _ERR("Failed to create symlink"); @@ -994,7 +1012,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_CLEAN(const char *pkgId, const char *appId try { for (auto& path : bf::recursive_directory_iterator(runtimesDir)) { std::string symPath = path.path().string(); - if (isDirectory(symPath) || !bf::is_symlink(symPath)) + if (isDirectory(symPath) || !isSymlinkFile(symPath)) continue; std::string targetDir = symPath.substr(symPath.rfind("/runtimes/") + 10); if (!std::regex_match(targetDir.substr(0, targetDir.find('/')), pattern)) diff --git a/NativeLauncher/tool/tac_common.cc b/NativeLauncher/tool/tac_common.cc index 1d69acf..cea457c 100644 --- a/NativeLauncher/tool/tac_common.cc +++ b/NativeLauncher/tool/tac_common.cc @@ -222,7 +222,7 @@ tac_error_e disableTACPackage(const std::string& pkgId) 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)) { + if (isSymlinkFile(symPath)) { std::string originPath = bf::read_symlink(symPath).string(); if (!isNativeImage(symPath)) { std::string dllPath = concatPath(binDir, fileName); @@ -297,6 +297,7 @@ tac_error_e enableTACPackage(const std::string& pkgId) } } + bs::error_code error; for (auto& originPath : enableNuget) { if (exist(originPath)) { std::string fileName = originPath.substr(originPath.rfind('/') + 1); @@ -304,7 +305,11 @@ tac_error_e enableTACPackage(const std::string& pkgId) if (exist(binNIDir)) { std::string originNIPath = changeExtension(originPath, "dll", "ni.dll"); if (exist(originNIPath)) { - bf::create_symlink(originNIPath, concatPath(tacDir, NIFileName)); + bf::create_symlink(originNIPath, concatPath(tacDir, NIFileName), error); + if (error) { + _SERR("Failed to create symlink %s file", concatPath(tacDir, NIFileName).c_str()); + return TAC_ERROR_UNKNOWN; + } _SOUT("%s symbolic link file generated successfully.", concatPath(tacDir, NIFileName).c_str()); copySmackAndOwnership(tacDir.c_str(), concatPath(tacDir, NIFileName).c_str(), true); @@ -314,7 +319,11 @@ tac_error_e enableTACPackage(const std::string& pkgId) } } } - bf::create_symlink(originPath, concatPath(tacDir, fileName)); + bf::create_symlink(originPath, concatPath(tacDir, fileName), error); + if (error) { + _SERR("Failed to create symlink %s file", concatPath(tacDir, fileName).c_str()); + return TAC_ERROR_UNKNOWN; + } _SOUT("%s symbolic link file generated successfully.", concatPath(tacDir, fileName).c_str()); copySmackAndOwnership(tacDir.c_str(), concatPath(tacDir, fileName).c_str(), true); diff --git a/NativeLauncher/tool/tpatool.cc b/NativeLauncher/tool/tpatool.cc index ffc971b..8256890 100644 --- a/NativeLauncher/tool/tpatool.cc +++ b/NativeLauncher/tool/tpatool.cc @@ -35,7 +35,7 @@ int main(int argc, char* argv[]) std::string tpaList; addAssembliesFromDirectories(__pm->getPlatformAssembliesPaths(), tpaList); - if (!bf::is_symlink(PLATFORM_TPA_CACHE)) { + if (!isSymlinkFile(PLATFORM_TPA_CACHE)) { std::ofstream out(PLATFORM_TPA_CACHE); out << tpaList; out.close(); diff --git a/NativeLauncher/util/utils.cc b/NativeLauncher/util/utils.cc index 1f784ce..fe50c38 100644 --- a/NativeLauncher/util/utils.cc +++ b/NativeLauncher/util/utils.cc @@ -301,17 +301,22 @@ bool isFile(const std::string& path) return lstat(path.c_str(), &sb) == 0; } -bool isDirectory(const std::string& path) +bool isSymlinkFile(const std::string& path) { struct stat sb; - if (stat(path.c_str(), &sb) != 0) { + if (lstat(path.c_str(), &sb) != 0) { return false; } - if (sb.st_mode & S_IFDIR) { - return true; - } else { + return (sb.st_mode & S_IFMT) == S_IFLNK; +} + +bool isDirectory(const std::string& path) +{ + struct stat sb; + if (stat(path.c_str(), &sb) != 0) { return false; } + return (sb.st_mode & S_IFMT) == S_IFDIR; } std::string getAssemblyNameFromPath(const std::string& path) @@ -487,7 +492,12 @@ static bool copyOwnershipAndPermissions(const bf::path& path, const bf::path& pa _ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), path2.c_str()); return false; } - bf::perms permissions = bf::status(path).permissions(); + bs::error_code error; + bf::perms permissions = bf::status(path, error).permissions(); + if (error) { + _ERR("Failed to copy ownership and permissions : %s", error.message().c_str()); + return false; + } struct stat stats; if (stat(path.c_str(), &stats) != 0) { return false; @@ -557,11 +567,11 @@ bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags) } // Iterate through the source directory - for (bf::directory_iterator file(path1); file != bf::directory_iterator(); ++file) { - try { + try { + for (bf::directory_iterator file(path1); file != bf::directory_iterator(); ++file) { bf::path current(file->path()); bf::path target = path2 / current.filename(); - if (bf::is_symlink(symlink_status(current))) { + if (bf::is_symlink(bf::symlink_status(current))) { if ((flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE)) && exist(target)) { continue; } @@ -599,11 +609,12 @@ bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags) bf::rename(destination, target); } } - } catch (const bf::filesystem_error& error) { - _ERR("Failed to copy directory: %s", error.what()); - return false; } + } catch (const bf::filesystem_error& error) { + _ERR("Failed to copy directory: %s", error.what()); + return false; } + return true; } -- 2.7.4