diff --git a/utils/src/b_error/b_excep_utils.cpp b/utils/src/b_error/b_excep_utils.cpp index 032f51b34e171014a63c918a4c6d139f017019b5..9af76541fac1d2f126d82a3a6c17647da6ae9361 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"); }