From f7a9d3817649f26f68ff1aab21ba3ac7a57e8513 Mon Sep 17 00:00:00 2001 From: zhangkaixiang Date: Tue, 25 Jul 2023 20:39:24 +0800 Subject: [PATCH] decode uri when opening a file Signed-off-by: zhangkaixiang Change-Id: I985cf56886277610591316a35f060167ea9281e4 --- interfaces/kits/js/src/mod_fs/common_func.cpp | 42 ++++++++++++++----- interfaces/kits/js/src/mod_fs/common_func.h | 1 + .../kits/js/src/mod_fs/properties/open.cpp | 10 +++-- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/common_func.cpp b/interfaces/kits/js/src/mod_fs/common_func.cpp index e810fd1a4..fda3274ce 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.cpp +++ b/interfaces/kits/js/src/mod_fs/common_func.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include #include @@ -168,16 +169,16 @@ void CommonFunc::fs_req_cleanup(uv_fs_t* req) string CommonFunc::GetModeFromFlags(unsigned int flags) { - const string RDONLY = "r"; - const string WRONLY = "w"; - const string APPEND = "a"; - const string TRUNC = "t"; - string mode = RDONLY; - mode += (((flags & O_RDWR) == O_RDWR) ? WRONLY : ""); - mode = (((flags & O_WRONLY) == O_WRONLY) ? WRONLY : mode); - if (mode != RDONLY) { - mode += ((flags & O_TRUNC) ? TRUNC : ""); - mode += ((flags & O_APPEND) ? APPEND : ""); + const string readMode = "r"; + const string writeMode = "w"; + const string appendMode = "a"; + const string truncMode = "t"; + string mode = readMode; + mode += (((flags & O_RDWR) == O_RDWR) ? writeMode : ""); + mode = (((flags & O_WRONLY) == O_WRONLY) ? writeMode : mode); + if (mode != readMode) { + mode += ((flags & O_TRUNC) ? truncMode : ""); + mode += ((flags & O_APPEND) ? appendMode : ""); } return mode; } @@ -192,6 +193,27 @@ bool CommonFunc::CheckPublicDirPath(const std::string &sandboxPath) return false; } +string CommonFunc::Decode(const std::string &uri) +{ + std::ostringstream outPutStream; + const int32_t encodeLen = 2; + size_t index = 0; + while (index < uri.length()) { + if (uri[index] == '%') { + int hex = 0; + std::istringstream inputStream(uri.substr(index + 1, encodeLen)); + inputStream >> std::hex >> hex; + outPutStream << static_cast(hex); + index += encodeLen + 1; + } else { + outPutStream << uri[index]; + index++; + } + } + + return outPutStream.str(); +} + tuple, unique_ptr> CommonFunc::GetCopyPathArg(napi_env env, napi_value srcPath, napi_value dstPath) diff --git a/interfaces/kits/js/src/mod_fs/common_func.h b/interfaces/kits/js/src/mod_fs/common_func.h index fc97865fc..238c5aa6c 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.h +++ b/interfaces/kits/js/src/mod_fs/common_func.h @@ -72,6 +72,7 @@ struct CommonFunc { static void fs_req_cleanup(uv_fs_t* req); static std::string GetModeFromFlags(unsigned int flags); static bool CheckPublicDirPath(const std::string &path); + static std::string Decode(const std::string &uri); }; } // namespace ModuleFileIO } // namespace FileManagement diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index 7360218b4..a5f982e74 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -163,14 +163,16 @@ static string GetBundleNameSelf() static string GetPathFromFileUri(string path, string bundleName, unsigned int mode) { - if (CommonFunc::CheckPublicDirPath(path) || bundleName != GetBundleNameSelf()) { + string decodePath = CommonFunc::Decode(path); + HILOGD("open path: %{private}s decodePath: %{private}s", path.c_str(), decodePath.c_str()); + if (CommonFunc::CheckPublicDirPath(decodePath) || bundleName != GetBundleNameSelf()) { if ((mode & O_WRONLY) == O_WRONLY || (mode & O_RDWR) == O_RDWR) { - path = PATH_SHARE + MODE_RW + bundleName + path; + decodePath = PATH_SHARE + MODE_RW + bundleName + decodePath; } else { - path = PATH_SHARE + MODE_R + bundleName + path; + decodePath = PATH_SHARE + MODE_R + bundleName + decodePath; } } - return path; + return decodePath; } #endif -- Gitee