diff --git a/frameworks/native/backup_ext/BUILD.gn b/frameworks/native/backup_ext/BUILD.gn index eb044ee941024402744b702b845a89ec4965dbec..ee46ddb114a86b628efb0df907affa5e2ce6cb88 100644 --- a/frameworks/native/backup_ext/BUILD.gn +++ b/frameworks/native/backup_ext/BUILD.gn @@ -50,6 +50,7 @@ ohos_shared_library("backup_extension_ability_native") { deps = [ "${path_backup}/interfaces/inner_api/native/backup_kit_inner:backup_kit_inner", + "${path_backup}/interfaces/innerkits/native:sandbox_helper_native", "${path_backup}/utils:backup_utils", "${path_jsoncpp}:jsoncpp", ] diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index f3cc2c8bdee3f7a5a8b8f738b10788cdb61be5d5..e49da6aabe7795cca8c9b59be0c25e700aa19387 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -50,6 +50,7 @@ #include "b_tarball/b_tarball_factory.h" #include "filemgmt_libhilog.h" #include "hitrace_meter.h" +#include "sandbox_helper.h" #include "service_proxy.h" #include "tar_file.h" #include "untar_file.h" @@ -799,10 +800,22 @@ static void RestoreBigFiles(bool appendTargetPath) continue; } - string fileName = path + item.hashName; - string filePath = appendTargetPath ? (path + item.fileName) : item.fileName; + string itemHashName = item.hashName; + string itemFileName = item.fileName; + // check if item.hasName and fileName need decode by rreport version + string reportPath = GetReportFileName(path + item.hashName); + UniqueFd fd(open(reportPath.data(), O_RDONLY)); + if (fd >= 0) { + BReportEntity rp(move(fd)); + bool enableEncode = rp.CheckIfEncodeEnable(); + BReportEntity::DecodeReportItem(item.hashName, itemHashName, enableEncode); + BReportEntity::DecodeReportItem(item.fileName, itemFileName, enableEncode); + } + + string fileName = path + itemHashName; + string filePath = appendTargetPath ? (path + itemFileName) : itemFileName; - if (!RestoreBigFilePrecheck(fileName, path, item.hashName, filePath)) { + if (!RestoreBigFilePrecheck(fileName, path, itemHashName, filePath)) { continue; } @@ -1336,11 +1349,12 @@ static void WriteFile(const string &filename, const map GetReportInfos(); + /** + * @brief Get Report version + * + * @return double + */ + double GetReportVersion(); + + /** + * @brief Check if encode enabled + * + * @return bool + */ + bool CheckIfEncodeEnable(); + + /** + * @brief encode report item + */ + static void EncodeReportItem(const std::string &reportItem, std::string& encodeItem, bool enableEncode); + + /** + * @brief decode report item + */ + static void DecodeReportItem(const std::string &reportItem, std::string& decodeItem, bool enableEncode); + public: /** * @brief 构造方法 diff --git a/utils/include/b_resources/b_constants.h b/utils/include/b_resources/b_constants.h index 131bf801644dcb1bc3e22df0ff0f927f13070ee2..50fc03e3f1586ea70220e97cb9cc09cc4785b0c4 100644 --- a/utils/include/b_resources/b_constants.h +++ b/utils/include/b_resources/b_constants.h @@ -68,6 +68,8 @@ constexpr int MAX_PARCELABLE_VECTOR_NUM = 10000; constexpr int DEFAULT_VFS_CACHE_PRESSURE = 100; // 默认内存回收参数 constexpr int BACKUP_VFS_CACHE_PRESSURE = 10000; // 备份过程修改参数 +constexpr double REPORT_VERSION_WITH_ENCODE = 1.1; // 包含编解码的简报版本 + // backup.para内配置项的名称,该配置项值为true时可在不更新hap包的情况下,可以读取包管理元数据配置文件的内容 static inline std::string BACKUP_DEBUG_OVERRIDE_EXTENSION_CONFIG_KEY = "backup.debug.overrideExtensionConfig"; diff --git a/utils/src/b_json/b_report_entity.cpp b/utils/src/b_json/b_report_entity.cpp index 02fbb3c68cec1b3f66862ae3f9a008af725de97d..ce8463f4ec652b1abc836e554fea4a88bdb0637b 100644 --- a/utils/src/b_json/b_report_entity.cpp +++ b/utils/src/b_json/b_report_entity.cpp @@ -22,7 +22,9 @@ #include #include "b_error/b_error.h" +#include "b_resources/b_constants.h" #include "filemgmt_libhilog.h" +#include "sandbox_helper.h" #include "unique_fd.h" namespace OHOS::FileManagement::Backup { @@ -31,6 +33,7 @@ namespace { const char ATTR_SEP = ';'; const char LINE_SEP = '\n'; const char LINE_WRAP = '\r'; +const char VERSION_SEP = '='; const int64_t HASH_BUFFER_SIZE = 4096; // 每次读取的siz const int INFO_ALIGN_NUM = 2; const string INFO_DIR = "dir"; @@ -60,7 +63,8 @@ static vector SplitStringByChar(const string &str, const char &sep) static ErrCode ParseReportInfo(struct ReportFileInfo &fileStat, const vector &splits, - const unordered_map &keys) + const unordered_map &keys, + bool enableEncode) { // 根据数据拼接结构体 int splitsLen = (int)splits.size(); @@ -80,7 +84,8 @@ static ErrCode ParseReportInfo(struct ReportFileInfo &fileStat, return EPERM; } path = (path.length() > 0 && path[0] == '/') ? path.substr(1, path.length() - 1) : path; - fileStat.filePath = path.substr(0, path.length() - 1); + // decode path if needed + BReportEntity::DecodeReportItem(path.substr(0, path.length() - 1), fileStat.filePath, enableEncode); if (keys.find(INFO_MODE) != keys.end()) { fileStat.mode = residue[keys.find(INFO_MODE)->second]; } @@ -115,7 +120,8 @@ static ErrCode ParseReportInfo(struct ReportFileInfo &fileStat, static void DealLine(unordered_map &keys, int &num, const string &line, - unordered_map &infos) + unordered_map &infos, + bool enableEncode) { string currentLine = line; if (currentLine[currentLine.length() - 1] == LINE_WRAP) { @@ -132,7 +138,7 @@ static void DealLine(unordered_map &keys, num++; } else { struct ReportFileInfo fileState; - auto code = ParseReportInfo(fileState, splits, keys); + auto code = ParseReportInfo(fileState, splits, keys, enableEncode); if (code != ERR_OK) { HILOGE("ParseReportInfo err:%{public}d, %{public}s", code, currentLine.c_str()); } else { @@ -150,11 +156,12 @@ unordered_map BReportEntity::GetReportInfos() string currentLine; unordered_map keys; + bool enableEncode = CheckIfEncodeEnable(); int num = 0; while ((bytesRead = read(srcFile_, buffer, sizeof(buffer))) > 0) { for (ssize_t i = 0; i < bytesRead; i++) { if (buffer[i] == LINE_SEP) { - DealLine(keys, num, currentLine, infos); + DealLine(keys, num, currentLine, infos, enableEncode); currentLine.clear(); } else { currentLine += buffer[i]; @@ -164,9 +171,74 @@ unordered_map BReportEntity::GetReportInfos() // 处理文件中的最后一行 if (!currentLine.empty()) { - DealLine(keys, num, currentLine, infos); + DealLine(keys, num, currentLine, infos, enableEncode); } return infos; } + +double BReportEntity::GetReportVersion() +{ + char buffer[HASH_BUFFER_SIZE]; + ssize_t bytesRead; + string line; + string versionStr; + + while ((bytesRead = read(srcFile_, buffer, sizeof(buffer) - 1)) > 0) { + buffer[bytesRead] = '\0'; + line += buffer; + size_t versionPartPos = line.find(VERSION_SEP); + if (versionPartPos != std::string::npos) { + // get content like version=XXX + versionStr = line.substr(0, versionPartPos); + break; + } + } + HILOGE("!!!!!!!!!!!!!versionStr = %{public}s", versionStr.c_str()); + if (versionStr.empty()) { + HILOGE("Invalid report file"); + return 0; + } + + // parse the version part to double + double reportVersion = 0; + try { + reportVersion = std::stod(versionStr); + } catch (...) { + HILOGE("Invalid version code"); + return 0; + } + return reportVersion; +} + +bool BReportEntity::CheckIfEncodeEnable() +{ + double reportVersion = GetReportVersion(); + if (reportVersion == 0) { + HILOGE("Report file with invalid report version"); + return false; + } + if (reportVersion < BConstants::REPORT_VERSION_WITH_ENCODE) { + return false; + } + return true; +} + +void BReportEntity::EncodeReportItem(const string &reportItem, string& encodeItem, bool enableEncode) +{ + if (enableEncode) { + encodeItem = AppFileService::SandboxHelper::Encode(reportItem); + } else { + encodeItem = reportItem; + } +} + +void BReportEntity::DecodeReportItem(const string &reportItem, string& decodeItem, bool enableEncode) +{ + if (enableEncode) { + decodeItem = AppFileService::SandboxHelper::Decode(reportItem); + } else { + decodeItem = reportItem; + } +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file