From 023862b61e5cf16e58df628f415f7d85549a0d38 Mon Sep 17 00:00:00 2001 From: z30054037 Date: Mon, 13 Nov 2023 11:00:43 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8c++=E6=9D=A5=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2rust=E7=9A=84=E7=9B=B8=E7=AD=89=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E8=BD=AC=E6=88=90=E7=BB=9D=E5=AF=B9=E8=B7=AF=E5=BE=84=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: z30054037 Change-Id: I68276a776963398b6e372f0ad14e8a77763e0274 --- utils/src/b_error/b_excep_utils.cpp | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/utils/src/b_error/b_excep_utils.cpp b/utils/src/b_error/b_excep_utils.cpp index 032f51b34..9af76541f 100644 --- a/utils/src/b_error/b_excep_utils.cpp +++ b/utils/src/b_error/b_excep_utils.cpp @@ -16,6 +16,13 @@ #include "b_error/b_excep_utils.h" #include +#include +#ifdef _WIN32 +#include +#else +#include +#include +#endif #include "b_resources/b_constants.h" #include "cxx.h" @@ -27,11 +34,10 @@ using namespace std; void BExcepUltils::VerifyPath(const string_view &path, bool isExtension) { try { - auto ret = canonicalize(path.data()); - string absPath = ret.c_str(); + string absPath = BExcepUltils::Canonicalize(path); if (isExtension && absPath.find(string(BConstants::PATH_BUNDLE_BACKUP_HOME) - .append(BConstants::SA_BUNDLE_BACKUP_RESTORE)) == std::string::npos) { + .append(BConstants::SA_BUNDLE_BACKUP_RESTORE)) == string::npos) { throw BError(BError::Codes::EXT_INVAL_ARG, "Invalid path, not in backup restore path"); } } catch (const rust::Error &e) { @@ -42,8 +48,21 @@ void BExcepUltils::VerifyPath(const string_view &path, bool isExtension) string BExcepUltils::Canonicalize(const string_view &path) { try { - auto ret = canonicalize(path.data()); - return ret.c_str(); + char full_path[PATH_MAX] = {0}; + //1.转换绝对路径到dir +#ifdef _WIN32 + _fullpath(full_path, path.data(), PATH_MAX); +#else + realpath(path.data(), full_path); +#endif + //2.替换绝对路径中的'\'为'/' + //因为上述方法转换出来的绝对路径之间会以'\'分隔,例如“C:\user\desktop”,字符串处理遇到'\'一般会报错,下面一行代码将'\'全部替换为'/' + // for (int i = 0; full_path[i] != 0 && i < MAX_PATH_LEN; i++) { + // if (full_path[i] == 92) { + // full_path[i] = '/'; + // } + // } + return string(full_path); } catch (const rust::Error &e) { throw BError(BError::Codes::EXT_INVAL_ARG, "Invalid path"); } -- Gitee