From 4d676190ecd0a69af7553bfeff78d4b8efd81fca Mon Sep 17 00:00:00 2001 From: lvyuanyuan Date: Tue, 12 Sep 2023 14:15:41 +0800 Subject: [PATCH] =?UTF-8?q?uri=E6=89=B9=E9=87=8F=E6=8E=88=E6=9D=83crash?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lvyuanyuan Change-Id: Ifd6c89130211ea41a5cab08f8d7c505249aa5ba3 --- interfaces/common/include/sandbox_helper.h | 4 +++ interfaces/common/src/sandbox_helper.cpp | 32 +++++++++++++++------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/interfaces/common/include/sandbox_helper.h b/interfaces/common/include/sandbox_helper.h index 73745dc84..eedf84f83 100644 --- a/interfaces/common/include/sandbox_helper.h +++ b/interfaces/common/include/sandbox_helper.h @@ -16,13 +16,17 @@ #ifndef FILEMANAGEMENT_APP_FILE_SERVICE_INTERFACES_INNERKITS_NATIVE_COMMON_INCLUDE_SANDBOX_HELPER_H #define FILEMANAGEMENT_APP_FILE_SERVICE_INTERFACES_INNERKITS_NATIVE_COMMON_INCLUDE_SANDBOX_HELPER_H +#include #include #include namespace OHOS { namespace AppFileService { class SandboxHelper { +private: + static std::mutex mapMutex_; static std::unordered_map sandboxPathMap_; + static bool GetSandboxPathMap(); public: static std::string Encode(const std::string &uri); static std::string Decode(const std::string &uri); diff --git a/interfaces/common/src/sandbox_helper.cpp b/interfaces/common/src/sandbox_helper.cpp index d46a71231..83c18d615 100644 --- a/interfaces/common/src/sandbox_helper.cpp +++ b/interfaces/common/src/sandbox_helper.cpp @@ -57,6 +57,7 @@ struct MediaUriInfo { }; std::unordered_map SandboxHelper::sandboxPathMap_; +std::mutex SandboxHelper::mapMutex_; string SandboxHelper::Encode(const string &uri) { @@ -126,27 +127,37 @@ static string GetLowerPath(string &lowerPathHead, const string &lowerPathTail, return lowerPathHead + lowerPathTail; } -static void GetSandboxPathMap(unordered_map &sandboxPathMap) +bool SandboxHelper::GetSandboxPathMap() { + lock_guard lock(mapMutex_); + if (sandboxPathMap_.size() > 0) { + return true; + } + nlohmann::json jsonObj; int ret = JsonUtils::GetJsonObjFromPath(jsonObj, SANDBOX_JSON_FILE_PATH); if (ret != 0) { LOGE("Get json object failed from %{public}s with %{public}d", SANDBOX_JSON_FILE_PATH.c_str(), ret); - return; + return false; } if (jsonObj.find(MOUNT_PATH_MAP_KEY) == jsonObj.end()) { LOGE("Json object find mount path map failed"); - return; + return false; } nlohmann::json mountPathMap = jsonObj[MOUNT_PATH_MAP_KEY]; for (size_t i = 0; i < mountPathMap.size(); i++) { string srcPath = mountPathMap[i][PHYSICAL_PATH_KEY]; string sandboxPath = mountPathMap[i][SANDBOX_PATH_KEY]; - sandboxPathMap[sandboxPath] = srcPath; + sandboxPathMap_[sandboxPath] = srcPath; } - return; + + if (sandboxPathMap_.size() == 0) { + return false; + } + + return true; } static int32_t GetPathSuffix(const std::string &path, string &pathSuffix) @@ -270,13 +281,13 @@ int32_t SandboxHelper::GetPhysicalPath(const std::string &fileUri, const std::st return -EINVAL; } - string lowerPathTail = ""; - string lowerPathHead = ""; - - if (sandboxPathMap_.size() == 0) { - GetSandboxPathMap(sandboxPathMap_); + if (!GetSandboxPathMap()) { + LOGE("GetSandboxPathMap failed"); + return -EINVAL; } + string lowerPathTail = ""; + string lowerPathHead = ""; string::size_type curPrefixMatchLen = 0; for (auto it = sandboxPathMap_.begin(); it != sandboxPathMap_.end(); it++) { string sandboxPathPrefix = it->first; @@ -292,6 +303,7 @@ int32_t SandboxHelper::GetPhysicalPath(const std::string &fileUri, const std::st } if (lowerPathHead == "") { + LOGE("lowerPathHead is invalid"); return -EINVAL; } -- Gitee