From f64d29199c7e56f64eb67bbbd70fcf400413d7ca Mon Sep 17 00:00:00 2001 From: "@ran-zhao-yu" Date: Wed, 10 Jul 2024 16:10:54 +0800 Subject: [PATCH] =?UTF-8?q?:=E6=94=AF=E6=8C=81=E5=A2=9E=E9=87=8F=E5=8C=85t?= =?UTF-8?q?est?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: @ran-zhao-yu --- frameworks/resmgr/include/hap_manager.h | 4 ++ frameworks/resmgr/include/hap_parser.h | 13 +++-- frameworks/resmgr/include/hap_resource.h | 42 +++++++++++++- .../resmgr/include/resource_manager_impl.h | 2 + frameworks/resmgr/src/hap_manager.cpp | 58 +++++++++++++++++-- .../resmgr/src/resource_manager_impl.cpp | 5 ++ frameworks/resmgr/src/utils/hap_parser.cpp | 58 ++++++++++++++----- .../inner_api/include/resource_manager.h | 3 + 8 files changed, 158 insertions(+), 27 deletions(-) diff --git a/frameworks/resmgr/include/hap_manager.h b/frameworks/resmgr/include/hap_manager.h index dc83493..9aaa56f 100644 --- a/frameworks/resmgr/include/hap_manager.h +++ b/frameworks/resmgr/include/hap_manager.h @@ -78,6 +78,8 @@ public: */ bool AddResource(const char *path, const uint32_t &selectedTypes); + bool AddPatchResource(const char *path, const char *patchPath); + /** * Add resource path to overlay paths * @param path the resource path @@ -395,6 +397,8 @@ private: bool AddResourcePath(const char *path, const uint32_t &selectedTypes = SELECT_ALL); + bool AddPatchResourcePath(const char *path, const char *patchPath); + // when resConfig_ updated we must call ReloadAll() RState ReloadAll(); diff --git a/frameworks/resmgr/include/hap_parser.h b/frameworks/resmgr/include/hap_parser.h index 3f56eec..5473d12 100644 --- a/frameworks/resmgr/include/hap_parser.h +++ b/frameworks/resmgr/include/hap_parser.h @@ -20,6 +20,7 @@ #include #include #include +#include #include "res_desc.h" #include "res_config_impl.h" #include "resource_manager.h" @@ -90,23 +91,25 @@ public: /** * Get the raw file data from hap * @param hapPath the hap path + * @param patchPath the hqf path * @param rawFileName the rawFile path * @param len the rawFile path * @param outValue the rawFile path * @return the rawFile path */ - static RState ReadRawFileFromHap(const std::string &hapPath, const std::string &rawFileName, - size_t &len, std::unique_ptr &outValue); + static RState ReadRawFileFromHap(const std::string &hapPath, const std::string &patchPath, + const std::string &rawFileName, size_t &len, std::unique_ptr &outValue); /** * Get the raw file descriptor * @param hapPath the hap path + * @param patchPath the hqf path * @param rawFileName the rawFile path * @param descriptor the rawFile path * @return the rawFile path */ - static RState ReadRawFileDescriptor(const char *hapPath, const std::string &rawFileName, - ResourceManager::RawFileDescriptor &descriptor); + static RState ReadRawFileDescriptor(const char *hapPath, const char *patchPath, + const std::string &rawFileName, ResourceManager::RawFileDescriptor &descriptor); /** * Get the raw file list @@ -116,7 +119,7 @@ public: * @return SUCCESS if resource exist, else not found */ static RState GetRawFileList(const std::string &hapPath, const std::string &rawDirPath, - std::vector& fileList); + std::set& fileList); /** * Get the raw file list in UnCompressed diff --git a/frameworks/resmgr/include/hap_resource.h b/frameworks/resmgr/include/hap_resource.h index 6ba3959..4a61286 100644 --- a/frameworks/resmgr/include/hap_resource.h +++ b/frameworks/resmgr/include/hap_resource.h @@ -133,6 +133,40 @@ public: return isOverlay_; } + /** + * Get the hqf resource path. + */ + inline std::string GetPatchPath() const + { + return patchPath_; + } + + /** + * Get the hqf flag of HapResource. + * + * @return true if isPatch_ is true, false otherwise + */ + inline bool IsPatch() const + { + return isPatch_; + } + + /** + * Set the hqf resource path. + */ + inline void SetPatchPath(const std::string& patchPath) + { + patchPath_ = patchPath; + } + + /** + * Set the hqf flag of HapResource. + */ + inline void IsPatch(bool isPatch) + { + isPatch_ = isPatch; + } + /** * Get the resource information */ @@ -340,8 +374,14 @@ private: // judge the hap resource is overlay or not. bool isOverlay_; - //judge the theme SystemRes is enabled or not. + // judge the theme SystemRes is enabled or not. bool isThemeSystemResEnable_; + + // hqf resource path. + std::string patchPath_; + + // judge the hqf is enabled or not. + bool isPatch_ = false; }; } // namespace Resource } // namespace Global diff --git a/frameworks/resmgr/include/resource_manager_impl.h b/frameworks/resmgr/include/resource_manager_impl.h index 0c7dbaf..62a272d 100644 --- a/frameworks/resmgr/include/resource_manager_impl.h +++ b/frameworks/resmgr/include/resource_manager_impl.h @@ -62,6 +62,8 @@ public: */ virtual bool AddResource(const char *path, const uint32_t &selectedTypes = SELECT_ALL); + virtual bool AddPatchResource(const char *path, const char *patchPath); + /** * Add resource path to overlay paths * @param path the resource path diff --git a/frameworks/resmgr/src/hap_manager.cpp b/frameworks/resmgr/src/hap_manager.cpp index 8b44b97..d02695f 100644 --- a/frameworks/resmgr/src/hap_manager.cpp +++ b/frameworks/resmgr/src/hap_manager.cpp @@ -372,6 +372,12 @@ bool HapManager::AddResource(const char *path, const uint32_t &selectedTypes) return this->AddResourcePath(path, selectedTypes); } +bool HapManager::AddPatchResource(const char *path, const char *patchPath) +{ + AutoMutex mutex(this->lock_); + return this->AddPatchResourcePath(path, patchPath); +} + bool HapManager::AddResource(const std::string &path, const std::vector &overlayPaths) { AutoMutex mutex(this->lock_); @@ -497,6 +503,25 @@ bool HapManager::AddResourcePath(const char *path, const uint32_t &selectedTypes return true; } +bool HapManager::AddPatchResourcePath(const char *path, const char *patchPath) +{ + std::string sPath(path); + auto it = loadedHapPaths_.find(sPath); + if (it == loadedHapPaths_.end()) { + RESMGR_HILOGW(RESMGR_TAG, "AddPatchResourcePath hapPath not load, hapPath = %{public}s", sPath.c_str()); + return false; + } + std::string sPatchPath(patchPath); + for (auto iter = hapResources_.begin(); iter != hapResources_.end(); iter++) { + if ((*iter)->GetIndexPath() == sPath) { + (*iter)->SetPatchPath(sPatchPath); + (*iter)->IsPatch(true); + return true; + } + } + return false; +} + RState HapManager::ReloadAll() { if (hapResources_.size() == 0) { @@ -748,9 +773,13 @@ RState HapManager::FindRawFileFromHap(const std::string &rawFileName, size_t &le if ((*iter)->IsSystemResource() || (*iter)->IsOverlayResource()) { continue; } - const std::string tempPath = (*iter)->GetIndexPath(); + std::string tempPath = (*iter)->GetIndexPath(); + std::string tempPatchPath = ""; + if ((*iter)->IsPatch()) { + tempPatchPath = (*iter)->GetPatchPath(); + } if (Utils::ContainsTail(tempPath, Utils::tailSet)) { // if file path is compressed - RState state = HapParser::ReadRawFileFromHap(tempPath, rawFileName, len, outValue); + RState state = HapParser::ReadRawFileFromHap(tempPath, tempPatchPath, rawFileName, len, outValue); if (state != SUCCESS) { continue; } @@ -792,9 +821,13 @@ RState HapManager::GetRawFd(const std::string &rawFileName, ResourceManager::Raw if ((*iter)->IsSystemResource() || (*iter)->IsOverlayResource()) { continue; } - const std::string tempPath = (*iter)->GetIndexPath(); + std::string tempPath = (*iter)->GetIndexPath(); + std::string tempPatchPath = ""; + if ((*iter)->IsPatch()) { + tempPatchPath = (*iter)->GetPatchPath(); + } if (Utils::ContainsTail(tempPath, Utils::tailSet)) { // if file path is compressed - state = HapParser::ReadRawFileDescriptor(tempPath.c_str(), rawFileName, descriptor); + state = HapParser::ReadRawFileDescriptor(tempPath.c_str(), tempPatchPath.c_str(), rawFileName, descriptor); } else { // if file path is uncompressed state = HapManager::FindRawFileDescriptor(rawFileName, descriptor); } @@ -810,7 +843,22 @@ RState HapManager::GetRawFileList(const std::string &rawDirPath, std::vectorIsSystemResource() || (*iter)->IsOverlayResource()) { + continue; + } + if ((*iter)->GetIndexPath() == hapOrIndexPath && (*iter)->IsPatch()) { + temPatchPath = (*iter)->GetPatchPath(); + } + } + std::set fileSet; + RState hapState = HapParser::GetRawFileList(hapOrIndexPath, rawDirPath, fileSet); + RState hqfState = HapParser::GetRawFileList(temPatchPath, rawDirPath, fileSet); + for (auto it = fileSet.begin(); it != fileSet.end(); it++) { + fileList.emplace_back(*it); + } + return (hapState != SUCCESS && hqfState != SUCCESS) ? ERROR_CODE_RES_PATH_INVALID : SUCCESS; } if (HapManager::GetValidIndexPath(hapOrIndexPath) == OK) { return HapParser::GetRawFileListUnCompressed(hapOrIndexPath, rawDirPath, fileList); diff --git a/frameworks/resmgr/src/resource_manager_impl.cpp b/frameworks/resmgr/src/resource_manager_impl.cpp index a3762b7..5d3802d 100644 --- a/frameworks/resmgr/src/resource_manager_impl.cpp +++ b/frameworks/resmgr/src/resource_manager_impl.cpp @@ -1091,6 +1091,11 @@ bool ResourceManagerImpl::AddResource(const char *path, const uint32_t &selected return this->hapManager_->AddResource(path, selectedTypes); } +bool ResourceManagerImpl::AddPatchResource(const char *path, const char *patchPath) +{ + return this->hapManager_->AddPatchResource(path, patchPath); +} + bool ResourceManagerImpl::AddResource(const std::string &path, const std::vector &overlayPaths) { return this->hapManager_->AddResource(path, overlayPaths); diff --git a/frameworks/resmgr/src/utils/hap_parser.cpp b/frameworks/resmgr/src/utils/hap_parser.cpp index d61ce09..1cb57c5 100644 --- a/frameworks/resmgr/src/utils/hap_parser.cpp +++ b/frameworks/resmgr/src/utils/hap_parser.cpp @@ -276,40 +276,56 @@ std::string HapParser::GetRawFilePath(std::shared_ptr &e } #endif -RState HapParser::ReadRawFileFromHap(const std::string &hapPath, const std::string &rawFileName, size_t &len, - std::unique_ptr &outValue) +RState HapParser::ReadRawFileFromHap(const std::string &hapPath, const std::string &patchPath, + const std::string &rawFileName, size_t &len, std::unique_ptr &outValue) { #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__) HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__); bool isNewExtractor = false; - auto extractor = AbilityBase::ExtractorUtil::GetExtractor(hapPath, isNewExtractor); + std::string tempPath = patchPath.empty() ? hapPath : patchPath; + auto extractor = AbilityBase::ExtractorUtil::GetExtractor(tempPath, isNewExtractor); if (extractor == nullptr) { - RESMGR_HILOGE(RESMGR_TAG, "failed to get extractor hapPath, %{public}s", hapPath.c_str()); + RESMGR_HILOGE(RESMGR_TAG, "failed to get extractor hapPath, %{public}s", tempPath.c_str()); return NOT_FOUND; } std::string rawfilePath = HapParser::GetRawFilePath(extractor, rawFileName); - if (!extractor->HasEntry(rawfilePath)) { + if (!extractor->HasEntry(rawfilePath) && patchPath.empty()) { RESMGR_HILOGD(RESMGR_TAG, - "the rawfile file %{public}s is not exist in %{public}s", rawfilePath.c_str(), hapPath.c_str()); + "the rawfile file %{public}s is not exist in %{public}s", rawfilePath.c_str(), tempPath.c_str()); return ERROR_CODE_RES_PATH_INVALID; } + if (!extractor->HasEntry(rawfilePath) && !patchPath.empty()) { + extractor = AbilityBase::ExtractorUtil::GetExtractor(hapPath, isNewExtractor); + if (extractor == nullptr) { + RESMGR_HILOGE(RESMGR_TAG, "failed to get extractor hapPath, %{public}s", tempPath.c_str()); + return NOT_FOUND; + } + rawfilePath = HapParser::GetRawFilePath(extractor, rawFileName); + if (!extractor->HasEntry(rawfilePath)) { + RESMGR_HILOGD(RESMGR_TAG, + "the rawfile file %{public}s is not exist in %{public}s", rawfilePath.c_str(), tempPath.c_str()); + return ERROR_CODE_RES_PATH_INVALID; + } + } bool ret = extractor->ExtractToBufByName(rawfilePath, outValue, len); if (!ret) { RESMGR_HILOGE(RESMGR_TAG, "failed to get rawfile data rawfilePath, %{public}s, hapPath, %{public}s", - rawfilePath.c_str(), hapPath.c_str()); + rawfilePath.c_str(), tempPath.c_str()); return NOT_FOUND; } #endif return SUCCESS; } -RState HapParser::ReadRawFileDescriptor(const char *hapPath, const std::string &rawFileName, +RState HapParser::ReadRawFileDescriptor(const char *hapPath, const char *patchPath, const std::string &rawFileName, ResourceManager::RawFileDescriptor &descriptor) { #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__) HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__); char outPath[PATH_MAX + 1] = {0}; - Utils::CanonicalizePath(hapPath, outPath, PATH_MAX); + std::string sPatchPath(patchPath); + const char *tempPath = sPatchPath.empty() ? hapPath : patchPath; + Utils::CanonicalizePath(tempPath, outPath, PATH_MAX); bool isNewExtractor = false; auto extractor = AbilityBase::ExtractorUtil::GetExtractor(outPath, isNewExtractor); if (extractor == nullptr) { @@ -317,11 +333,25 @@ RState HapParser::ReadRawFileDescriptor(const char *hapPath, const std::string & return NOT_FOUND; } std::string rawfilePath = HapParser::GetRawFilePath(extractor, rawFileName); - if (!extractor->HasEntry(rawfilePath)) { + if (!extractor->HasEntry(rawfilePath) && sPatchPath.empty()) { RESMGR_HILOGD(RESMGR_TAG, - "the rawfile file %{public}s is not exist in %{public}s", rawfilePath.c_str(), hapPath); + "the rawfile file %{public}s is not exist in %{public}s", rawfilePath.c_str(), outPath); return ERROR_CODE_RES_PATH_INVALID; } + if (!extractor->HasEntry(rawfilePath) && !sPatchPath.empty()) { + Utils::CanonicalizePath(hapPath, outPath, PATH_MAX); + extractor = AbilityBase::ExtractorUtil::GetExtractor(outPath, isNewExtractor); + if (extractor == nullptr) { + RESMGR_HILOGE(RESMGR_TAG, "failed to get extractor hapPath, %{public}s", outPath); + return NOT_FOUND; + } + rawfilePath = HapParser::GetRawFilePath(extractor, rawFileName); + if (!extractor->HasEntry(rawfilePath)) { + RESMGR_HILOGD(RESMGR_TAG, + "the rawfile file %{public}s is not exist in %{public}s", rawfilePath.c_str(), outPath); + return ERROR_CODE_RES_PATH_INVALID; + } + } AbilityBase::FileInfo fileInfo; bool ret = extractor->GetFileInfo(rawfilePath, fileInfo); if (!ret) { @@ -341,7 +371,7 @@ RState HapParser::ReadRawFileDescriptor(const char *hapPath, const std::string & } RState HapParser::GetRawFileList(const std::string &hapPath, const std::string &rawDirPath, - std::vector& fileList) + std::set& fileSet) { #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__) HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__); @@ -352,7 +382,6 @@ RState HapParser::GetRawFileList(const std::string &hapPath, const std::string & "failed to get extractor from ability in GetRawFileList hapPath, %{public}s", hapPath.c_str()); return NOT_FOUND; } - std::set fileSet; std::string rawfilePath = HapParser::GetRawFilePath(extractor, rawDirPath); if (!extractor->IsDirExist(rawfilePath)) { RESMGR_HILOGD(RESMGR_TAG, @@ -364,9 +393,6 @@ RState HapParser::GetRawFileList(const std::string &hapPath, const std::string & RESMGR_HILOGE(RESMGR_TAG, "failed to get fileSet from ability rawfilePath, %{public}s", rawfilePath.c_str()); return ERROR_CODE_RES_PATH_INVALID; } - for (auto it = fileSet.begin(); it != fileSet.end(); it++) { - fileList.emplace_back(*it); - } #endif return SUCCESS; } diff --git a/interfaces/inner_api/include/resource_manager.h b/interfaces/inner_api/include/resource_manager.h index c82be73..3efae48 100644 --- a/interfaces/inner_api/include/resource_manager.h +++ b/interfaces/inner_api/include/resource_manager.h @@ -236,6 +236,8 @@ public: virtual RState GetFormatPluralStringByName(std::string &outValue, const char *name, int quantity, std::vector> &jsParams) = 0; + + virtual bool AddPatchResource(const char *path, const char *patchPath) = 0; }; EXPORT_FUNC ResourceManager *CreateResourceManager(); @@ -270,6 +272,7 @@ EXPORT_FUNC ResourceManager *GetSystemResourceManagerNoSandBox(); EXPORT_FUNC std::shared_ptr CreateResourceManager(const std::string &bundleName, const std::string &moduleName, const std::string &hapPath, const std::vector &overlayPath, ResConfig &resConfig, int32_t appType = 0, int32_t userId = 0); + } // namespace Resource } // namespace Global } // namespace OHOS