diff --git a/interfaces/common/include/sandbox_helper.h b/interfaces/common/include/sandbox_helper.h index 73745dc84abe47e9f0344f9ca56acb7d7b4516c2..eedf84f83a6139f738fe31a0ee939ddb1745d433 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 4f1b0ae1ae0f72f6ed1a61535dc82e21882c7063..14f58c9b07605a4ff673a7c038977f9f86e7460b 100644 --- a/interfaces/common/src/sandbox_helper.cpp +++ b/interfaces/common/src/sandbox_helper.cpp @@ -55,6 +55,7 @@ struct MediaUriInfo { }; std::unordered_map SandboxHelper::sandboxPathMap_; +std::mutex SandboxHelper::mapMutex_; string SandboxHelper::Encode(const string &uri) { @@ -118,27 +119,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; + } + + if (sandboxPathMap_.size() == 0) { + return false; } - return; + + return true; } static int32_t GetPathSuffix(const std::string &path, string &pathSuffix) @@ -247,13 +258,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; @@ -269,6 +280,7 @@ int32_t SandboxHelper::GetPhysicalPath(const std::string &fileUri, const std::st } if (lowerPathHead == "") { + LOGE("lowerPathHead is invalid"); return -EINVAL; } else { physicalPath = GetLowerPath(lowerPathHead, lowerPathTail, userId, bundleName);