diff --git a/frameworks/native/backup_ext/src/untar_file.cpp b/frameworks/native/backup_ext/src/untar_file.cpp index f2814d36bb16d5046e0784a70474ab257eecc2cc..4050e8973f5e3e552bf7696cc9f2a896590356da 100644 --- a/frameworks/native/backup_ext/src/untar_file.cpp +++ b/frameworks/native/backup_ext/src/untar_file.cpp @@ -155,7 +155,11 @@ void UntarFile::HandleTarBuffer(const string &buff, const string &name, FileStat realName = info.longName; info.longName.clear(); } - info.fullPath = GenRealPath(rootPath_, realName); + if (realName[0] == '/') { + info.fullPath = realName.substr(0, realName.length() - 1); + return; + } + info.fullPath = realName; } int UntarFile::ParseTarFile(const string &rootPath) @@ -236,8 +240,9 @@ int UntarFile::ParseIncrementalTarFile(const string &rootPath) HILOGE("Parsing tar file completed, read data count is less then block size."); return 0; } + TarHeader *header = reinterpret_cast(buff); // two empty continuous block indicate end of file - if (IsEmptyBlock(buff)) { + if (IsEmptyBlock(buff) && header->typeFlag != GNUTYPE_LONGNAME) { char tailBuff[BLOCK_SIZE] = {0}; size_t tailRead = fread(tailBuff, 1, BLOCK_SIZE, tarFilePtr_); if (tailRead == BLOCK_SIZE && IsEmptyBlock(tailBuff)) { @@ -246,7 +251,6 @@ int UntarFile::ParseIncrementalTarFile(const string &rootPath) } } // check header - TarHeader *header = reinterpret_cast(buff); if (!IsValidTarBlock(*header)) { // when split unpack, ftell size is over than file really size [0,READ_BUFF_SIZE] if (ftello(tarFilePtr_) > (tarFileSize_ + READ_BUFF_SIZE) || !IsEmptyBlock(buff)) { @@ -274,6 +278,7 @@ void UntarFile::ParseFileByTypeFlag(char typeFlag, bool &isSkip, FileStatInfo &i switch (typeFlag) { case REGTYPE: case AREGTYPE: + info.fullPath = GenRealPath(rootPath_, info.fullPath); ParseRegularFile(info, typeFlag, isSkip); break; case SYMTYPE: @@ -313,6 +318,7 @@ int UntarFile::ParseIncrementalFileByTypeFlag(char typeFlag, bool &isSkip, FileS } break; } + info.fullPath = GenRealPath(rootPath_, info.fullPath); ParseRegularFile(info, typeFlag, isSkip); break; case SYMTYPE: @@ -468,7 +474,8 @@ FILE *UntarFile::CreateFile(string &filePath, mode_t mode, char fileType) return f; } - HILOGE("Failed to open file %{public}s, err = %{public}d", filePath.c_str(), errno); + uint32_t len = filePath.length(); + HILOGE("Failed to open file %{public}d, %{public}s, err = %{public}d", len, filePath.c_str(), errno); size_t pos = filePath.rfind('/'); if (pos == string::npos) { return nullptr; diff --git a/tests/unittests/backup_utils/b_json/b_report_entity_test.cpp b/tests/unittests/backup_utils/b_json/b_report_entity_test.cpp index 40a952afcec1a82908ab6b05639cb3fdab334386..8b702286a1687ecd718815f79d9055bf8f7761d1 100644 --- a/tests/unittests/backup_utils/b_json/b_report_entity_test.cpp +++ b/tests/unittests/backup_utils/b_json/b_report_entity_test.cpp @@ -81,7 +81,7 @@ HWTEST_F(BReportEntityTest, b_report_entity_GetReportInfos_0100, testing::ext::T unordered_map cloudFiles = cloudRp.GetReportInfos(); bool flag = false; - + fileName = fileName.substr(1, fileName.length() - 1); EXPECT_EQ(cloudFiles.size(), 1); for (auto &item : cloudFiles) { if (item.first == fileName) { diff --git a/utils/src/b_json/b_report_entity.cpp b/utils/src/b_json/b_report_entity.cpp index 6290f046c199170435028c0a32c6e43bc67a98b3..d2553512a59894d4dc042cace3030d95ca0e6b2c 100644 --- a/utils/src/b_json/b_report_entity.cpp +++ b/utils/src/b_json/b_report_entity.cpp @@ -63,14 +63,13 @@ static ErrCode ParseReportInfo(struct ReportFileInfo &fileStat, const unordered_map &keys) { // 根据数据拼接结构体 - int len = keys.size(); int splitsLen = (int)splits.size(); // 处理path路径 string path; vector residue; try { for (int i = 0; i < splitsLen; i++) { - if (i <= splitsLen - len) { + if (i <= splitsLen - keys.size()) { path += splits[i] + ";"; } else { residue.emplace_back(splits[i]); @@ -80,6 +79,7 @@ static ErrCode ParseReportInfo(struct ReportFileInfo &fileStat, HILOGE("Error residue size"); return EPERM; } + path = path[0] == '/' ? path.substr(1, path.length() - 1) : path; fileStat.filePath = path.substr(0, path.length() - 1); if (keys.find(INFO_MODE) != keys.end()) { fileStat.mode = residue[keys.find(INFO_MODE)->second];