diff --git a/frameworks/native/backup_ext/BUILD.gn b/frameworks/native/backup_ext/BUILD.gn index bc45931ad13ff564a6223747596331c028375e77..4bca82b35789512373a2ac5b8c4db63937ea0266 100644 --- a/frameworks/native/backup_ext/BUILD.gn +++ b/frameworks/native/backup_ext/BUILD.gn @@ -51,6 +51,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 d8b515636312dcbf8a67f1074d07f5758803d12e..0fb2c7f37699f12be07f0230cde27c1fc4806b34 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -48,6 +48,7 @@ #include "filemgmt_libhilog.h" #include "hitrace_meter.h" #include "i_service.h" +#include "sandbox_helper.h" #include "service_proxy.h" #include "tar_file.h" #include "untar_file.h" @@ -763,8 +764,20 @@ 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 report item attribute + string reportPath = GetReportFileName(path + item.hashName); + UniqueFd fd(open(reportPath.data(), O_RDONLY)); + if (fd < 0) { + HILOGE("Failed to open report file = %{private}s, err = %{public}d", reportPath.c_str(), errno); + throw BError(BError::Codes::EXT_INVAL_ARG, string("open report file failed")); + } + BReportEntity rp(move(fd)); + rp.CheckAndUpdateIfReportLineEncoded(itemFileName); + + string fileName = path + itemHashName; + string filePath = appendTargetPath ? (path + itemFileName) : itemFileName; if (!RestoreBigFilePrecheck(fileName, path, item.hashName, filePath)) { continue; @@ -1276,12 +1289,18 @@ static void WriteFile(const string &filename, const map GetReportInfos(); + /** + * @brief Check if line is encode + * + */ + void CheckAndUpdateIfReportLineEncoded(std::string &path); + + /** + * @brief encode report item + */ + static std::string EncodeReportItem(const std::string &reportItem, bool enableEncode); + + /** + * @brief decode report item + */ + static std::string DecodeReportItem(const std::string &reportItem, bool enableEncode); + public: /** * @brief 构造方法 diff --git a/utils/src/b_json/b_report_entity.cpp b/utils/src/b_json/b_report_entity.cpp index bf2fe1be45a153a2469fd299f8ffd690aa62b838..bd14b1c4a68dcc62d628315b3c7ddd911fdb5247 100644 --- a/utils/src/b_json/b_report_entity.cpp +++ b/utils/src/b_json/b_report_entity.cpp @@ -23,6 +23,7 @@ #include "b_error/b_error.h" #include "filemgmt_libhilog.h" +#include "sandbox_helper.h" #include "unique_fd.h" namespace OHOS::FileManagement::Backup { @@ -40,6 +41,7 @@ const string INFO_MODE = "mode"; const string INFO_MTIME = "mtime"; const string INFO_PATH = "path"; const string INFO_SIZE = "size"; +const string INFO_ENCODE_FLAG = "encodeFlag"; } // namespace static vector SplitStringByChar(const string &str, const char &sep) @@ -61,29 +63,39 @@ static vector SplitStringByChar(const string &str, const char &sep) return splits; } +static void ParseSplitsItem(const vector &splits, const unordered_map &keys, + vector &residue, string &path) +{ + size_t splitsLen = splits.size(); + for (size_t i = 0; i < splitsLen; i++) { + if (i <= splitsLen - keys.size()) { + path += splits[i] + ";"; + } else { + residue.emplace_back(splits[i]); + } + } +} + static ErrCode ParseReportInfo(struct ReportFileInfo &fileStat, const vector &splits, const unordered_map &keys) { // 根据数据拼接结构体 - size_t splitsLen = splits.size(); // 处理path路径 string path; vector residue; try { - for (size_t i = 0; i < splitsLen; i++) { - if (i <= splitsLen - keys.size()) { - path += splits[i] + ";"; - } else { - residue.emplace_back(splits[i]); - } - } + // 识别path字段与其他字段 + ParseSplitsItem(splits, keys, residue, path); if (residue.size() != keys.size() - 1) { HILOGE("Error residue size"); return EPERM; } + if (keys.find(INFO_ENCODE_FLAG) != keys.end()) { + fileStat.encodeFlag = residue[keys.find(INFO_ENCODE_FLAG)->second] == "1" ? true : false; + } path = (path.length() > 0 && path[0] == '/') ? path.substr(1, path.length() - 1) : path; - fileStat.filePath = path.substr(0, path.length() - 1); + fileStat.filePath = BReportEntity::DecodeReportItem(path.substr(0, path.length() - 1), fileStat.encodeFlag); HILOGI("Briefings file %{public}s", fileStat.filePath.c_str()); if (keys.find(INFO_MODE) != keys.end()) { fileStat.mode = residue[keys.find(INFO_MODE)->second]; @@ -121,6 +133,10 @@ static void DealLine(unordered_map &keys, const string &line, unordered_map &infos) { + if (line.empty()) { + return; + } + string currentLine = line; if (currentLine[currentLine.length() - 1] == LINE_WRAP) { currentLine.pop_back(); @@ -173,4 +189,43 @@ unordered_map BReportEntity::GetReportInfos() return infos; } + +void BReportEntity::CheckAndUpdateIfReportLineEncoded(std::string &path) +{ + if (path.empty()) { + return; + } + + unordered_map infos = GetReportInfos(); + if (infos.size() == 1) { + auto info = infos.begin(); + if (info->second.encodeFlag) { + path = info->first; + } + } else { + HILOGE("Invalid item sizes in report, current is %{public}zu", infos.size()); + } +} + +string BReportEntity::EncodeReportItem(const string &reportItem, bool enableEncode) +{ + string encodeItem; + if (enableEncode) { + encodeItem = AppFileService::SandboxHelper::Encode(reportItem); + } else { + encodeItem = reportItem; + } + return encodeItem; +} + +string BReportEntity::DecodeReportItem(const string &reportItem, bool enableEncode) +{ + string decodeItem; + if (enableEncode) { + decodeItem = AppFileService::SandboxHelper::Decode(reportItem); + } else { + decodeItem = reportItem; + } + return decodeItem; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file