mirror of
https://github.com/openharmony/ability_ability_runtime.git
synced 2026-08-24 22:21:36 -04:00
Fix review comments.
Signed-off-by: dy_study <dingyao5@huawei.com> Change-Id: I8852176d392e54993817aab446bc8c648f9df16f
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
@@ -30,6 +30,23 @@ inline bool StringStartWith(const std::string& str, const char* startStr, size_t
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::string GetLoadPath(const std::string& hapPath)
|
||||
{
|
||||
std::regex hapPattern(std::string(Constants::ABS_CODE_PATH) + std::string(Constants::FILE_SEPARATOR));
|
||||
std::string loadPath = std::regex_replace(hapPath, hapPattern, "");
|
||||
loadPath = std::string(Constants::LOCAL_CODE_PATH) + std::string(Constants::FILE_SEPARATOR) +
|
||||
loadPath.substr(loadPath.find(std::string(Constants::FILE_SEPARATOR)) + 1);
|
||||
return loadPath;
|
||||
}
|
||||
|
||||
std::string GetRelativePath(const std::string& srcPath)
|
||||
{
|
||||
std::regex srcPattern(std::string(Constants::LOCAL_CODE_PATH) + std::string(Constants::FILE_SEPARATOR));
|
||||
std::string relativePath = std::regex_replace(srcPath, srcPattern, "");
|
||||
relativePath = relativePath.substr(relativePath.find(std::string(Constants::FILE_SEPARATOR)) + 1);
|
||||
return relativePath;
|
||||
}
|
||||
|
||||
std::shared_ptr<RuntimeExtractor> InitRuntimeExtractor(const std::string& hapPath)
|
||||
{
|
||||
if (hapPath.empty()) {
|
||||
@@ -39,10 +56,7 @@ std::shared_ptr<RuntimeExtractor> InitRuntimeExtractor(const std::string& hapPat
|
||||
|
||||
std::string loadPath;
|
||||
if (!StringStartWith(hapPath, Constants::SYSTEM_APP_PATH, sizeof(Constants::SYSTEM_APP_PATH) - 1)) {
|
||||
std::regex hapPattern(std::string(Constants::ABS_CODE_PATH) + std::string(Constants::FILE_SEPARATOR));
|
||||
loadPath = std::regex_replace(hapPath, hapPattern, "");
|
||||
loadPath = std::string(Constants::LOCAL_CODE_PATH) + std::string(Constants::FILE_SEPARATOR) +
|
||||
loadPath.substr(loadPath.find(std::string(Constants::FILE_SEPARATOR)) + 1);
|
||||
loadPath = GetLoadPath(hapPath);
|
||||
} else {
|
||||
loadPath = hapPath;
|
||||
}
|
||||
@@ -56,16 +70,14 @@ std::shared_ptr<RuntimeExtractor> InitRuntimeExtractor(const std::string& hapPat
|
||||
}
|
||||
|
||||
bool GetFileBuffer(
|
||||
const std::shared_ptr<RuntimeExtractor>& runtimeExtractor, const std::string& srcPath, std::ostringstream &dest)
|
||||
const std::shared_ptr<RuntimeExtractor>& runtimeExtractor, const std::string& srcPath, std::ostringstream& dest)
|
||||
{
|
||||
if (runtimeExtractor == nullptr || srcPath.empty()) {
|
||||
HILOG_ERROR("GetFileBuffer::runtimeExtractor or srcPath is nullptr");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::regex srcPattern(std::string(Constants::LOCAL_CODE_PATH) + std::string(Constants::FILE_SEPARATOR));
|
||||
std::string relativePath = std::regex_replace(srcPath, srcPattern, "");
|
||||
relativePath = relativePath.substr(relativePath.find(std::string(Constants::FILE_SEPARATOR)) + 1);
|
||||
std::string relativePath = GetRelativePath(srcPath);
|
||||
if (!runtimeExtractor->ExtractByName(relativePath, dest)) {
|
||||
HILOG_ERROR("GetFileBuffer::Extract file failed");
|
||||
return false;
|
||||
@@ -73,61 +85,5 @@ bool GetFileBuffer(
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GetFileBufferFromHap(const std::string& hapPath, const std::string& srcPath, std::ostringstream &dest)
|
||||
{
|
||||
if (hapPath.empty() || srcPath.empty()) {
|
||||
HILOG_ERROR("GetFileBufferFromHap::hapPath or srcPath is nullptr");
|
||||
return false;
|
||||
}
|
||||
|
||||
return GetFileBuffer(InitRuntimeExtractor(hapPath), srcPath, dest);
|
||||
}
|
||||
|
||||
bool GetFileListFromHap(const std::string& hapPath, const std::string& srcPath, std::vector<std::string>& assetList)
|
||||
{
|
||||
if (hapPath.empty() || srcPath.empty()) {
|
||||
HILOG_ERROR("GetFileListFromHap::hapPath or srcPath is nullptr");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string loadPath;
|
||||
if (!StringStartWith(hapPath, Constants::SYSTEM_APP_PATH, sizeof(Constants::SYSTEM_APP_PATH) - 1)) {
|
||||
std::regex hapPattern(std::string(Constants::ABS_CODE_PATH) + std::string(Constants::FILE_SEPARATOR));
|
||||
loadPath = std::regex_replace(hapPath, hapPattern, "");
|
||||
loadPath = std::string(Constants::LOCAL_CODE_PATH) + std::string(Constants::FILE_SEPARATOR) +
|
||||
loadPath.substr(loadPath.find(std::string(Constants::FILE_SEPARATOR)) + 1);
|
||||
} else {
|
||||
loadPath = hapPath;
|
||||
}
|
||||
RuntimeExtractor runtimeExtractor(loadPath);
|
||||
if (!runtimeExtractor.Init()) {
|
||||
HILOG_ERROR("GetFileListFromHap::Runtime extractor init failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::regex srcPattern(std::string(Constants::LOCAL_CODE_PATH) + std::string(Constants::FILE_SEPARATOR));
|
||||
std::string relativePath = std::regex_replace(srcPath, srcPattern, "");
|
||||
relativePath = relativePath.substr(relativePath.find(std::string(Constants::FILE_SEPARATOR)) + 1);
|
||||
|
||||
std::vector<std::string> fileList;
|
||||
if (!runtimeExtractor.GetZipFileNames(fileList)) {
|
||||
HILOG_ERROR("GetFileListFromHap::Get file list failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::regex replacePattern(relativePath);
|
||||
for (auto value : fileList) {
|
||||
if (StringStartWith(value, relativePath.c_str(), sizeof(relativePath.c_str()) - 1)) {
|
||||
std::string realpath = std::regex_replace(value, replacePattern, "");
|
||||
if (realpath.find(Constants::FILE_SEPARATOR) != std::string::npos) {
|
||||
continue;
|
||||
}
|
||||
assetList.emplace_back(value);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
} // namespace AbilityRuntime
|
||||
} // namespace OHOS
|
||||
|
||||
Reference in New Issue
Block a user