From b9df57a7c989950fdbf612a771fdff486757df39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B8=8A?= Date: Mon, 28 Jul 2025 16:06:33 +0800 Subject: [PATCH 1/9] use brotli compress for backup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘渊 --- bundle.json | 2 + frameworks/native/backup_ext/BUILD.gn | 2 + .../native/backup_ext/include/tar_file.h | 17 ++ .../native/backup_ext/src/ext_extension.cpp | 3 +- frameworks/native/backup_ext/src/tar_file.cpp | 181 +++++++++++++++++- test/fuzztest/backupext_fuzzer/BUILD.gn | 2 + tests/unittests/backup_ext/BUILD.gn | 10 + 7 files changed, 212 insertions(+), 5 deletions(-) diff --git a/bundle.json b/bundle.json index 48ba56c11..39041f40f 100644 --- a/bundle.json +++ b/bundle.json @@ -23,6 +23,7 @@ "ability_base", "ability_runtime", "access_token", + "brotli", "bundle_framework", "common_event_service", "cJSON", @@ -39,6 +40,7 @@ "init", "json", "jsoncpp", + "lz4", "napi", "runtime_core", "openssl", diff --git a/frameworks/native/backup_ext/BUILD.gn b/frameworks/native/backup_ext/BUILD.gn index 85d6d2369..6287cc935 100644 --- a/frameworks/native/backup_ext/BUILD.gn +++ b/frameworks/native/backup_ext/BUILD.gn @@ -67,6 +67,7 @@ ohos_shared_library("backup_extension_ability_native") { "ability_runtime:napi_common", "ability_runtime:runtime", "access_token:libaccesstoken_sdk", + "brotli:brotli_shared", "bundle_framework:appexecfwk_core", "c_utils:utils", "hilog:libhilog", @@ -74,6 +75,7 @@ ohos_shared_library("backup_extension_ability_native") { "ipc:ipc_core", "ipc:ipc_napi", "jsoncpp:jsoncpp", + "lz4:liblz4_shared", "napi:ace_napi", "runtime_core:ani", "samgr:samgr_proxy", diff --git a/frameworks/native/backup_ext/include/tar_file.h b/frameworks/native/backup_ext/include/tar_file.h index 0a875704c..c914e3f15 100644 --- a/frameworks/native/backup_ext/include/tar_file.h +++ b/frameworks/native/backup_ext/include/tar_file.h @@ -59,6 +59,8 @@ const char GNUTYPE_LONGNAME = 'L'; const char EXTENSION_HEADER = 'x'; const uint32_t OTHER_HEADER = 78; const int ERR_NO_PERMISSION = 13; +constexpr int SIZE_T_BYTE_LEN = 8; +constexpr bool USE_COMPRESS = false; } // namespace // 512 bytes @@ -81,6 +83,15 @@ using TarHeader = struct { char prefix[PREFIX_LEN]; char pad[PADDING_LEN]; }; + +#define SAFE_DELETES(address) \ +{ \ + if ((address) != nullptr) { \ + delete[] (address); \ + (address) = nullptr; \ + } \ +} \ + using TarMap = std::map>; class TarFile { public: @@ -100,6 +111,12 @@ public: void SetPacketMode(bool isReset); uint64_t GetTarFileSize() { return static_cast(currentTarFileSize_); } + + bool Compress(const uint8_t* inputBuffer, size_t inputSize, uint8_t* outputBuffer, size_t* outputSize); + bool Decompress(const uint8_t* inputBuffer, size_t inputSize, uint8_t* outputBuffer, size_t* outputSize); + void CompressFile(const std::string &srcFile, const std::string &compFile); + void DecompressFile(const std::string &compFile, const std::string &srcFile); + std::string DecompressTar(const std::string &tarPath); private: TarFile() {} ~TarFile() = default; diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index ee46dd698..fe59eda66 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -714,7 +714,7 @@ static ErrCode TarFileReady(const TarMap &tarFileInfo, sptr proxy) if (SUCCEEDED(ret)) { HILOGI("TarFileReady: AppFileReady success for %{public}s", tarName.c_str()); // 删除文件 - RemoveFile(tarPath); + RemoveFile(tarPath); // TODO } else { HILOGE("TarFileReady AppFileReady fail to be invoked for %{public}s: ret = %{public}d", tarName.c_str(), ret); } @@ -1006,6 +1006,7 @@ int BackupExtExtension::DoIncrementalRestore() HILOGE("Check incre tarfile path : %{public}s err, path is forbidden", GetAnonyPath(tarName).c_str()); return BError(BError::Codes::EXT_FORBID_BACKUP_RESTORE).GetCode(); } + tarName = TarFile::GetInstance().DecompressTar(tarName); unordered_map result; GetTarIncludes(tarName, result); if ((!extension_->SpecialVersionForCloneAndCloud()) && (!extension_->UseFullBackupOnly())) { diff --git a/frameworks/native/backup_ext/src/tar_file.cpp b/frameworks/native/backup_ext/src/tar_file.cpp index bf77dc212..de79d17c0 100644 --- a/frameworks/native/backup_ext/src/tar_file.cpp +++ b/frameworks/native/backup_ext/src/tar_file.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 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 @@ -15,13 +15,20 @@ #include "tar_file.h" +#include +#include #include #include +#include #include #include #include #include #include +#include "lz4.h" +#include "lz4hc.h" +#include "brotli/encode.h" +#include "brotli/decode.h" #include "b_anony/b_anony.h" #include "b_error/b_error.h" @@ -44,6 +51,10 @@ const uint32_t WAIT_INDEX = 100000; const uint32_t WAIT_TIME = 5; const string VERSION = "1.0"; const string LONG_LINK_SYMBOL = "longLinkSymbol"; +const string COMPRESS_FILE_SUFFIX = "__A"; +const string TAR_EXTENSION = ".tar"; +constexpr int BROTLI_QUALITY = 3; +constexpr int MEGA_BYTE = 1024 * 1024; } // namespace TarFile &TarFile::GetInstance() @@ -449,13 +460,23 @@ bool TarFile::FillSplitTailBlocks() if (isReset_) { tarMap_.clear(); } - - tarMap_.emplace(tarFileName_, make_tuple(currentTarName_, staTar, false)); + if (USE_COMPRESS) { + std::filesystem::path fullTarPath = currentTarName_; // 全量路径 + std::filesystem::path parentPath = fullTarPath.parent_path(); + std::filesystem::path fileNameWithoutExtension = fullTarPath.stem(); // 文件名前缀 + std::string tarNewName = fileNameWithoutExtension.string() + COMPRESS_FILE_SUFFIX + TAR_EXTENSION; + string newTarPath = parentPath.string() + "/" + tarNewName; + HILOGI("tarFileName:%{public}s, currentTarName:%{public}s, newTarPath:%{public}s", tarFileName_.c_str(), + currentTarName_.c_str(), newTarPath.c_str()); + tarMap_.emplace(tarNewName, make_tuple(newTarPath, staTar, false)); + CompressFile(fullTarPath, newTarPath); + } else { + tarMap_.emplace(tarFileName_, make_tuple(currentTarName_, staTar, false)); + } fclose(currentTarFile_); currentTarFile_ = nullptr; tarFileCount_++; - return true; } @@ -648,4 +669,156 @@ void TarFile::SetPacketMode(bool isReset) { isReset_ = isReset; } + +bool TarFile::Compress(const uint8_t* inputBuffer, size_t inputSize, uint8_t* outputBuffer, size_t* outputSize) +{ + int ret = BrotliEncoderCompress(BROTLI_QUALITY, BROTLI_MAX_WINDOW_BITS, BROTLI_DEFAULT_MODE, + inputSize, inputBuffer, outputSize, outputBuffer); + if (ret != BROTLI_TRUE) { + HILOGE("compress fail, error: %{public}d", ret); + return false; + } + return true; +} + +bool TarFile::Decompress(const uint8_t* inputBuffer, size_t inputSize, uint8_t* outputBuffer, size_t* outputSize) +{ + int ret = BrotliDecoderDecompress(inputSize, inputBuffer, outputSize, outputBuffer); + if (ret != BROTLI_TRUE) { + HILOGE("decompress fail, error: %{public}d", ret); + return false; + } + return true; +} + +void TarFile::CompressFile(const std::string &srcFile, const std::string &compFile) +{ + HILOGI("testComp begin, strFile: %{public}s, compFile: %{public}s", srcFile.c_str(), compFile.c_str()); + FILE *fin = fopen(srcFile.c_str(), "rb"); + FILE *fout = fopen(compFile.c_str(), "wb"); + if (fin == nullptr || fout == nullptr) { + HILOGE("open file fail!"); + return; + } + char *ori = new (std::nothrow) char[BLOCK_SIZE]; + size_t oriSize = BLOCK_SIZE; + size_t maxSize = LZ4_compressBound(BLOCK_SIZE); + size_t compressSize = maxSize; + uint8_t* compressBuffer = new (std::nothrow) uint8_t[maxSize]; + if (ori == nullptr || compressBuffer == nullptr) { + HILOGE("new buffer fail!"); + SAFE_DELETES(ori); + SAFE_DELETES(compressBuffer); + fclose(fin); + fclose(fout); + return; + } + size_t inTotal = 0; + size_t outTotal = 0; + auto compSpan = std::chrono::duration(std::chrono::seconds(0)); + while ((oriSize = fread(ori, 1, BLOCK_SIZE, fin)) > 0) { + compressSize = maxSize; + auto startTime = std::chrono::high_resolution_clock::now(); + if (!Compress((const uint8_t*)(ori), oriSize, compressBuffer, &compressSize)) { + break; + } + compSpan += (std::chrono::high_resolution_clock::now() - startTime); + if (compressSize < 0 || compressSize >= oriSize) { // 压缩后大小大于原始大小则直接存储原始内容 + compressSize = oriSize; + fwrite(&oriSize, SIZE_T_BYTE_LEN, 1, fout); + fwrite(&oriSize, SIZE_T_BYTE_LEN, 1, fout); + fwrite(ori, 1, oriSize, fout); + } else { + fwrite(&compressSize, SIZE_T_BYTE_LEN, 1, fout); + fwrite(&oriSize, SIZE_T_BYTE_LEN, 1, fout); + fwrite(compressBuffer, 1, compressSize, fout); + } + inTotal += oriSize; + outTotal += compressSize; + } + HILOGI("srcSize:%{public}lu, destSize:%{public}lu, rate:%{public}f, time:%{public}f ms, speed:%{public}f MB/s", + inTotal, outTotal, (outTotal == 0) ? 0 : (inTotal * 1.0f / outTotal), compSpan.count(), + (compSpan.count() == 0) ? 0 : inTotal * 1000.0f / MEGA_BYTE / compSpan.count()); + SAFE_DELETES(ori); + SAFE_DELETES(compressBuffer); + fclose(fin); + fclose(fout); + HILOGI("testComp end, strFile: %{public}s, compFile: %{public}s", srcFile.c_str(), compFile.c_str()); +} + +void TarFile::DecompressFile(const std::string &compFile, const std::string &srcFile) +{ + HILOGI("testDecomp begin, compFile: %{public}s, srcFile: %{public}s", compFile.c_str(), srcFile.c_str()); + FILE *fin = fopen(compFile.c_str(), "rb"); + FILE *fout = fopen(srcFile.c_str(), "wb"); + if (fin == nullptr || fout == nullptr) { + HILOGE("open file fail!"); + return; + } + uint8_t* decompressBuffer= new (std::nothrow) uint8_t[BLOCK_SIZE]; + size_t size; + size_t compressSize; + size_t decompressSize; + char *compressBuffer = new (std::nothrow) char[BLOCK_SIZE]; + if (decompressBuffer == nullptr || compressBuffer == nullptr) { + HILOGE("new buffer fail!"); + SAFE_DELETES(decompressBuffer); + SAFE_DELETES(compressBuffer); + fclose(fin); + fclose(fout); + return; + } + size_t inTotal = 0; + size_t outTotal = 0; + auto decompressSpan = std::chrono::duration(std::chrono::seconds(0)); + while ((size = fread(&compressSize, SIZE_T_BYTE_LEN, 1, fin)) > 0) { + fread(&decompressSize, SIZE_T_BYTE_LEN, 1, fin); + if (compressSize > BLOCK_SIZE) { + HILOGE("compress size is too big."); + compressSize = BLOCK_SIZE; + } + fread(compressBuffer, 1, compressSize, fin); + if (compressSize == decompressSize) { + fwrite(compressBuffer, 1, compressSize, fout); + } else { + auto startTime = std::chrono::high_resolution_clock::now(); + if (!Decompress((const uint8_t*)compressBuffer, compressSize, decompressBuffer, &decompressSize)) { + break; + } + decompressSpan += (std::chrono::high_resolution_clock::now() - startTime); + fwrite(decompressBuffer, 1, decompressSize, fout); + } + inTotal += compressSize; + outTotal += decompressSize; + } + HILOGI("srcSize:%{public}lu, destSize:%{public}lu, time:%{public}f ms, speed:%{public}f MB/s", + inTotal, outTotal, decompressSpan.count(), + (decompressSpan.count() == 0) ? 0 : outTotal * 1000.0f / MEGA_BYTE / decompressSpan.count()); + SAFE_DELETES(compressBuffer); + SAFE_DELETES(decompressBuffer); + fclose(fin); + fclose(fout); + HILOGI("testDecomp end, compFile: %{public}s, srcFile: %{public}s", compFile.c_str(), srcFile.c_str()); +} + +std::string TarFile::DecompressTar(const std::string &tarPath) +{ + if (!USE_COMPRESS) { + return tarPath; + } + std::filesystem::path filePath = tarPath; + std::filesystem::path parentPath = filePath.parent_path(); + std::filesystem::path fileNameWithoutExtension = filePath.stem(); + size_t pos = fileNameWithoutExtension.string().rfind(COMPRESS_FILE_SUFFIX); + if (pos == std::string::npos) { + return tarPath; + } + std::string oriTarName = fileNameWithoutExtension.string().substr(0, pos); + std::string decompressFileName = parentPath.string() + std::filesystem::path::preferred_separator + + oriTarName + TAR_EXTENSION; + HILOGI("restore decompress, originFileName:%{public}s, decompressFileName:%{public}s", tarPath.c_str(), + decompressFileName.c_str()); + DecompressFile(tarPath, decompressFileName); + return decompressFileName; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/test/fuzztest/backupext_fuzzer/BUILD.gn b/test/fuzztest/backupext_fuzzer/BUILD.gn index 94ac5b15e..eecaf07c0 100644 --- a/test/fuzztest/backupext_fuzzer/BUILD.gn +++ b/test/fuzztest/backupext_fuzzer/BUILD.gn @@ -65,6 +65,7 @@ ohos_fuzztest("BackupExtFuzzTest") { "ability_runtime:appkit_native", "ability_runtime:runtime", "access_token:libaccesstoken_sdk", + "brotli:brotli_shared", "bundle_framework:appexecfwk_core", "c_utils:utils", "hilog:libhilog", @@ -72,6 +73,7 @@ ohos_fuzztest("BackupExtFuzzTest") { "ipc:ipc_core", "ipc:ipc_napi", "jsoncpp:jsoncpp", + "lz4:liblz4_shared", "napi:ace_napi", "runtime_core:ani", ] diff --git a/tests/unittests/backup_ext/BUILD.gn b/tests/unittests/backup_ext/BUILD.gn index 1f2fccc23..644b390a6 100644 --- a/tests/unittests/backup_ext/BUILD.gn +++ b/tests/unittests/backup_ext/BUILD.gn @@ -174,6 +174,7 @@ ohos_unittest("tar_file_test") { "ability_runtime:appkit_native", "ability_runtime:runtime", "access_token:libaccesstoken_sdk", + "brotli:brotli_shared", "bundle_framework:appexecfwk_core", "c_utils:utils", "googletest:gmock_main", @@ -182,6 +183,7 @@ ohos_unittest("tar_file_test") { "ipc:ipc_core", "ipc:ipc_napi", "jsoncpp:jsoncpp", + "lz4:liblz4_shared", "napi:ace_napi", "runtime_core:ani", "samgr:samgr_proxy", @@ -255,6 +257,7 @@ ohos_unittest("untar_file_sup_test") { "ability_runtime:appkit_native", "ability_runtime:runtime", "access_token:libaccesstoken_sdk", + "brotli:brotli_shared", "bundle_framework:appexecfwk_core", "c_utils:utils", "googletest:gmock_main", @@ -263,6 +266,7 @@ ohos_unittest("untar_file_sup_test") { "ipc:ipc_core", "ipc:ipc_napi", "jsoncpp:jsoncpp", + "lz4:liblz4_shared", "napi:ace_napi", "runtime_core:ani", "samgr:samgr_proxy", @@ -330,6 +334,7 @@ ohos_unittest("untar_file_test") { "ability_runtime:appkit_native", "ability_runtime:runtime", "access_token:libaccesstoken_sdk", + "brotli:brotli_shared", "bundle_framework:appexecfwk_core", "c_utils:utils", "googletest:gmock_main", @@ -338,6 +343,7 @@ ohos_unittest("untar_file_test") { "ipc:ipc_core", "ipc:ipc_napi", "jsoncpp:jsoncpp", + "lz4:liblz4_shared", "napi:ace_napi", "runtime_core:ani", "samgr:samgr_proxy", @@ -444,9 +450,11 @@ ohos_unittest("tar_file_sub_test") { external_deps = [ "c_utils:utils", + "brotli:brotli_shared", "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", + "lz4:liblz4_shared", ] defines = [ "private=public" ] @@ -479,9 +487,11 @@ ohos_unittest("installd_un_tar_file_test") { external_deps = [ "c_utils:utils", + "brotli:brotli_shared", "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", + "lz4:liblz4_shared", ] defines = [ "private=public" ] -- Gitee From b3261622665570f41dab3ad5564dafb20d318cfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B8=8A?= Date: Mon, 28 Jul 2025 19:55:57 +0800 Subject: [PATCH 2/9] fix code sytle problem MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘渊 --- bundle.json | 1 - frameworks/native/backup_ext/BUILD.gn | 1 - .../native/backup_ext/include/tar_file.h | 31 ++- .../native/backup_ext/src/ext_extension.cpp | 2 +- frameworks/native/backup_ext/src/tar_file.cpp | 201 +++++++++++------- test/fuzztest/backupext_fuzzer/BUILD.gn | 1 - tests/unittests/backup_ext/BUILD.gn | 5 - 7 files changed, 141 insertions(+), 101 deletions(-) diff --git a/bundle.json b/bundle.json index 39041f40f..2fb93834c 100644 --- a/bundle.json +++ b/bundle.json @@ -40,7 +40,6 @@ "init", "json", "jsoncpp", - "lz4", "napi", "runtime_core", "openssl", diff --git a/frameworks/native/backup_ext/BUILD.gn b/frameworks/native/backup_ext/BUILD.gn index 6287cc935..d7113dd41 100644 --- a/frameworks/native/backup_ext/BUILD.gn +++ b/frameworks/native/backup_ext/BUILD.gn @@ -75,7 +75,6 @@ ohos_shared_library("backup_extension_ability_native") { "ipc:ipc_core", "ipc:ipc_napi", "jsoncpp:jsoncpp", - "lz4:liblz4_shared", "napi:ace_napi", "runtime_core:ani", "samgr:samgr_proxy", diff --git a/frameworks/native/backup_ext/include/tar_file.h b/frameworks/native/backup_ext/include/tar_file.h index c914e3f15..d17c3b21c 100644 --- a/frameworks/native/backup_ext/include/tar_file.h +++ b/frameworks/native/backup_ext/include/tar_file.h @@ -60,6 +60,7 @@ const char EXTENSION_HEADER = 'x'; const uint32_t OTHER_HEADER = 78; const int ERR_NO_PERMISSION = 13; constexpr int SIZE_T_BYTE_LEN = 8; +constexpr size_t MAX_BUFFER_SIZE = 4096; constexpr bool USE_COMPRESS = false; } // namespace @@ -84,15 +85,25 @@ using TarHeader = struct { char pad[PADDING_LEN]; }; -#define SAFE_DELETES(address) \ -{ \ - if ((address) != nullptr) { \ - delete[] (address); \ - (address) = nullptr; \ - } \ -} \ - using TarMap = std::map>; + +class UniqueFile { +public: + UniqueFile(FILE* file); + UniqueFile(const char* filePath, const char* mode); + ~UniqueFile(); + FILE* file_ = nullptr; +}; + +template +class Buffer { +public: + Buffer(size_t size); + ~Buffer(); + T* data_ = nullptr; + size_t size_ = 0; +}; + class TarFile { public: static TarFile &GetInstance(); @@ -114,8 +125,8 @@ public: bool Compress(const uint8_t* inputBuffer, size_t inputSize, uint8_t* outputBuffer, size_t* outputSize); bool Decompress(const uint8_t* inputBuffer, size_t inputSize, uint8_t* outputBuffer, size_t* outputSize); - void CompressFile(const std::string &srcFile, const std::string &compFile); - void DecompressFile(const std::string &compFile, const std::string &srcFile); + bool CompressFile(const std::string &srcFile, const std::string &compFile); + bool DecompressFile(const std::string &compFile, const std::string &srcFile); std::string DecompressTar(const std::string &tarPath); private: TarFile() {} diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index fe59eda66..3f1964279 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -714,7 +714,7 @@ static ErrCode TarFileReady(const TarMap &tarFileInfo, sptr proxy) if (SUCCEEDED(ret)) { HILOGI("TarFileReady: AppFileReady success for %{public}s", tarName.c_str()); // 删除文件 - RemoveFile(tarPath); // TODO + RemoveFile(tarPath); } else { HILOGE("TarFileReady AppFileReady fail to be invoked for %{public}s: ret = %{public}d", tarName.c_str(), ret); } diff --git a/frameworks/native/backup_ext/src/tar_file.cpp b/frameworks/native/backup_ext/src/tar_file.cpp index de79d17c0..172b146f6 100644 --- a/frameworks/native/backup_ext/src/tar_file.cpp +++ b/frameworks/native/backup_ext/src/tar_file.cpp @@ -25,8 +25,6 @@ #include #include #include -#include "lz4.h" -#include "lz4hc.h" #include "brotli/encode.h" #include "brotli/decode.h" @@ -57,6 +55,54 @@ constexpr int BROTLI_QUALITY = 3; constexpr int MEGA_BYTE = 1024 * 1024; } // namespace +UniqueFile::UniqueFile(FILE* file) +{ + if (file == nullptr) { + HILOGE("file is null"); + } + file_ = file; +} + +UniqueFile::UniqueFile(const char* filePath, const char* mode) +{ + file_ = fopen(filePath, mode); + if (file_ == nullptr) { + HILOGE("open file fail err: %{public}s", strerror(errno)); + } +} + +UniqueFile::~UniqueFile() +{ + if (file_ != nullptr) { + if (fclose(file_) == EOF) { + HILOGE("close file fail err: %{public}s", strerror(errno)); + } + } + file_ = nullptr; +} + +template +Buffer:Buffer(size_t size) +{ + if (size <= 0 || size > MAX_BUFFER_SIZE) { + return; + } + data_ = new (std::nothrow) T[size]; + if (data_ == nullptr) { + HILOGE("new fail, size=%{public}zu", size); + return; + } + size_ = size; +} + +template +Buffer::~Buffer() +{ + if (data_ != nullptr) { + delete[] data_; + } +} + TarFile &TarFile::GetInstance() { static TarFile instance; @@ -691,114 +737,105 @@ bool TarFile::Decompress(const uint8_t* inputBuffer, size_t inputSize, uint8_t* return true; } -void TarFile::CompressFile(const std::string &srcFile, const std::string &compFile) +bool TarFile::CompressFile(const std::string &srcFile, const std::string &compFile) { - HILOGI("testComp begin, strFile: %{public}s, compFile: %{public}s", srcFile.c_str(), compFile.c_str()); - FILE *fin = fopen(srcFile.c_str(), "rb"); - FILE *fout = fopen(compFile.c_str(), "wb"); - if (fin == nullptr || fout == nullptr) { + HILOGI("BEGIN strF: %{public}s, compF: %{public}s", GetAnonyPath(srcFile).c_str(), GetAnonyPath(compFile).c_str()); + UniqueFile fin(srcFile.c_str(), "rb"); + UniqueFile fout(compFile.c_str(), "wb"); + if (fin.file_ == nullptr || fout.file_ == nullptr) { HILOGE("open file fail!"); - return; + return false; } - char *ori = new (std::nothrow) char[BLOCK_SIZE]; - size_t oriSize = BLOCK_SIZE; - size_t maxSize = LZ4_compressBound(BLOCK_SIZE); - size_t compressSize = maxSize; - uint8_t* compressBuffer = new (std::nothrow) uint8_t[maxSize]; - if (ori == nullptr || compressBuffer == nullptr) { - HILOGE("new buffer fail!"); - SAFE_DELETES(ori); - SAFE_DELETES(compressBuffer); - fclose(fin); - fclose(fout); - return; + Buffer ori(BLOCK_SIZE); + if (ori.data_ == nullptr) { + HILOGE("new ori buffer fail!"); + return false; + } + size_t maxSize = BrotliEncoderMaxCompressedSize(BLOCK_SIZE); + Buffer compressBuffer(maxSize); + if (compressBuffer.data_ == nullptr) { + HILOGE("new compress buffer fail!"); + return false; } size_t inTotal = 0; size_t outTotal = 0; auto compSpan = std::chrono::duration(std::chrono::seconds(0)); - while ((oriSize = fread(ori, 1, BLOCK_SIZE, fin)) > 0) { - compressSize = maxSize; + while ((ori.size_ = fread(ori.data_, 1, BLOCK_SIZE, fin.file_)) > 0) { + compressBuffer.size_ = maxSize; auto startTime = std::chrono::high_resolution_clock::now(); - if (!Compress((const uint8_t*)(ori), oriSize, compressBuffer, &compressSize)) { - break; + if (!Compress((const uint8_t*)(ori.data_), ori.size_, compressBuffer.data_, &compressBuffer.size_)) { + return false; } compSpan += (std::chrono::high_resolution_clock::now() - startTime); - if (compressSize < 0 || compressSize >= oriSize) { // 压缩后大小大于原始大小则直接存储原始内容 - compressSize = oriSize; - fwrite(&oriSize, SIZE_T_BYTE_LEN, 1, fout); - fwrite(&oriSize, SIZE_T_BYTE_LEN, 1, fout); - fwrite(ori, 1, oriSize, fout); + if (compressBuffer.size_ < 0 || compressBuffer.size_ >= ori.size_) { // 压缩后大小大于原始大小则直接存储原始内容 + compressBuffer.size_ = ori.size_; + fwrite(&ori.size_, SIZE_T_BYTE_LEN, 1, fout.file_); + fwrite(&ori.size_, SIZE_T_BYTE_LEN, 1, fout.file_); + fwrite(ori.data_, 1, ori.size_, fout.file_); } else { - fwrite(&compressSize, SIZE_T_BYTE_LEN, 1, fout); - fwrite(&oriSize, SIZE_T_BYTE_LEN, 1, fout); - fwrite(compressBuffer, 1, compressSize, fout); + fwrite(&compressBuffer.size_, SIZE_T_BYTE_LEN, 1, fout.file_); + fwrite(&ori.size_, SIZE_T_BYTE_LEN, 1, fout.file_); + fwrite(compressBuffer.data_, 1, compressBuffer.size_, fout.file_); } - inTotal += oriSize; - outTotal += compressSize; + inTotal += ori.size_; + outTotal += compressBuffer.size_; } - HILOGI("srcSize:%{public}lu, destSize:%{public}lu, rate:%{public}f, time:%{public}f ms, speed:%{public}f MB/s", + HILOGI("END srcSize:%{public}zu, destSize:%{public}zu, rate:%{public}f, time:%{public}f ms, speed:%{public}f MB/s", inTotal, outTotal, (outTotal == 0) ? 0 : (inTotal * 1.0f / outTotal), compSpan.count(), (compSpan.count() == 0) ? 0 : inTotal * 1000.0f / MEGA_BYTE / compSpan.count()); - SAFE_DELETES(ori); - SAFE_DELETES(compressBuffer); - fclose(fin); - fclose(fout); - HILOGI("testComp end, strFile: %{public}s, compFile: %{public}s", srcFile.c_str(), compFile.c_str()); + return true; } -void TarFile::DecompressFile(const std::string &compFile, const std::string &srcFile) +bool TarFile::DecompressFile(const std::string &compFile, const std::string &srcFile) { - HILOGI("testDecomp begin, compFile: %{public}s, srcFile: %{public}s", compFile.c_str(), srcFile.c_str()); - FILE *fin = fopen(compFile.c_str(), "rb"); - FILE *fout = fopen(srcFile.c_str(), "wb"); - if (fin == nullptr || fout == nullptr) { + HILOGI("BEGIN, compF:%{public}s, srcF:%{public}s", GetAnonyPath(compFile).c_str(), GetAnonyPath(srcFile).c_str()); + UniqueFile fin(compFile.c_str(), "rb"); + UniqueFile fout(srcFile.c_str(), "wb"); + if (fin.file_ == nullptr || fout.file_ == nullptr) { HILOGE("open file fail!"); - return; + return false; } - uint8_t* decompressBuffer= new (std::nothrow) uint8_t[BLOCK_SIZE]; - size_t size; - size_t compressSize; - size_t decompressSize; - char *compressBuffer = new (std::nothrow) char[BLOCK_SIZE]; - if (decompressBuffer == nullptr || compressBuffer == nullptr) { + Buffer decompressBuffer(BLOCK_SIZE); + Buffer compressBuffer(BLOCK_SIZE); + if (decompressBuffer.data_ == nullptr || compressBuffer.data_ == nullptr) { HILOGE("new buffer fail!"); - SAFE_DELETES(decompressBuffer); - SAFE_DELETES(compressBuffer); - fclose(fin); - fclose(fout); - return; + return false; } size_t inTotal = 0; size_t outTotal = 0; - auto decompressSpan = std::chrono::duration(std::chrono::seconds(0)); - while ((size = fread(&compressSize, SIZE_T_BYTE_LEN, 1, fin)) > 0) { - fread(&decompressSize, SIZE_T_BYTE_LEN, 1, fin); - if (compressSize > BLOCK_SIZE) { + size_t size; + auto decompSpan = std::chrono::duration(std::chrono::seconds(0)); + while ((size = fread(&compressBuffer.size_, SIZE_T_BYTE_LEN, 1, fin.file_)) > 0) { + if (size != 1) { + HILOGE("read comp size fail"); + return false; + } + if (compressBuffer.size_ > BLOCK_SIZE) { HILOGE("compress size is too big."); - compressSize = BLOCK_SIZE; + return false; + } + if (fread(&decompressBuffer.size_, SIZE_T_BYTE_LEN, 1, fin.file_) != 1 || + fread(compressBuffer.data_, 1, compressBuffer.size_, fin.file_) != compressBuffer.size_) { + HILOGE("read comp buffer fail"); + return false; } - fread(compressBuffer, 1, compressSize, fin); - if (compressSize == decompressSize) { - fwrite(compressBuffer, 1, compressSize, fout); + if (compressBuffer.size_ == decompressBuffer.size_) { + fwrite(compressBuffer.data_, 1, compressBuffer.size_, fout.file_); } else { auto startTime = std::chrono::high_resolution_clock::now(); - if (!Decompress((const uint8_t*)compressBuffer, compressSize, decompressBuffer, &decompressSize)) { - break; + if (!Decompress((const uint8_t*)compressBuffer.data_, compressBuffer.size_, decompressBuffer.data_, + &decompressBuffer.size_)) { + return false; } - decompressSpan += (std::chrono::high_resolution_clock::now() - startTime); - fwrite(decompressBuffer, 1, decompressSize, fout); + decompSpan += (std::chrono::high_resolution_clock::now() - startTime); + fwrite(decompressBuffer.data_, 1, decompressBuffer.size_, fout.file_); } - inTotal += compressSize; - outTotal += decompressSize; + inTotal += compressBuffer.size_; + outTotal += decompressBuffer.size_; } - HILOGI("srcSize:%{public}lu, destSize:%{public}lu, time:%{public}f ms, speed:%{public}f MB/s", - inTotal, outTotal, decompressSpan.count(), - (decompressSpan.count() == 0) ? 0 : outTotal * 1000.0f / MEGA_BYTE / decompressSpan.count()); - SAFE_DELETES(compressBuffer); - SAFE_DELETES(decompressBuffer); - fclose(fin); - fclose(fout); - HILOGI("testDecomp end, compFile: %{public}s, srcFile: %{public}s", compFile.c_str(), srcFile.c_str()); + HILOGI("srcSize:%{public}zu, destSize:%{public}zu, time:%{public}f ms, speed:%{public}f MB/s", inTotal, outTotal, + decompSpan.count(), (decompSpan.count() == 0) ? 0 : outTotal * 1000.0f / MEGA_BYTE / decompSpan.count()); + return true; } std::string TarFile::DecompressTar(const std::string &tarPath) @@ -816,9 +853,9 @@ std::string TarFile::DecompressTar(const std::string &tarPath) std::string oriTarName = fileNameWithoutExtension.string().substr(0, pos); std::string decompressFileName = parentPath.string() + std::filesystem::path::preferred_separator + oriTarName + TAR_EXTENSION; - HILOGI("restore decompress, originFileName:%{public}s, decompressFileName:%{public}s", tarPath.c_str(), - decompressFileName.c_str()); - DecompressFile(tarPath, decompressFileName); + if (!DecompressFile(tarPath, decompressFileName)) { + return tarPath; + } return decompressFileName; } } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/test/fuzztest/backupext_fuzzer/BUILD.gn b/test/fuzztest/backupext_fuzzer/BUILD.gn index eecaf07c0..3d4b0de11 100644 --- a/test/fuzztest/backupext_fuzzer/BUILD.gn +++ b/test/fuzztest/backupext_fuzzer/BUILD.gn @@ -73,7 +73,6 @@ ohos_fuzztest("BackupExtFuzzTest") { "ipc:ipc_core", "ipc:ipc_napi", "jsoncpp:jsoncpp", - "lz4:liblz4_shared", "napi:ace_napi", "runtime_core:ani", ] diff --git a/tests/unittests/backup_ext/BUILD.gn b/tests/unittests/backup_ext/BUILD.gn index 644b390a6..b8f7048e2 100644 --- a/tests/unittests/backup_ext/BUILD.gn +++ b/tests/unittests/backup_ext/BUILD.gn @@ -183,7 +183,6 @@ ohos_unittest("tar_file_test") { "ipc:ipc_core", "ipc:ipc_napi", "jsoncpp:jsoncpp", - "lz4:liblz4_shared", "napi:ace_napi", "runtime_core:ani", "samgr:samgr_proxy", @@ -266,7 +265,6 @@ ohos_unittest("untar_file_sup_test") { "ipc:ipc_core", "ipc:ipc_napi", "jsoncpp:jsoncpp", - "lz4:liblz4_shared", "napi:ace_napi", "runtime_core:ani", "samgr:samgr_proxy", @@ -343,7 +341,6 @@ ohos_unittest("untar_file_test") { "ipc:ipc_core", "ipc:ipc_napi", "jsoncpp:jsoncpp", - "lz4:liblz4_shared", "napi:ace_napi", "runtime_core:ani", "samgr:samgr_proxy", @@ -454,7 +451,6 @@ ohos_unittest("tar_file_sub_test") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", - "lz4:liblz4_shared", ] defines = [ "private=public" ] @@ -491,7 +487,6 @@ ohos_unittest("installd_un_tar_file_test") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", - "lz4:liblz4_shared", ] defines = [ "private=public" ] -- Gitee From 356e1c36de5194e06ad81e5f72ce3c630d196eb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B8=8A?= Date: Wed, 30 Jul 2025 12:15:13 +0800 Subject: [PATCH 3/9] fix part dep problem MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘渊 --- backup.gni | 13 +++++ frameworks/native/backup_ext/BUILD.gn | 11 +++- .../native/backup_ext/include/tar_file.h | 6 ++- frameworks/native/backup_ext/src/tar_file.cpp | 35 +++++++------ test/fuzztest/backupext_fuzzer/BUILD.gn | 10 +++- tests/unittests/backup_ext/BUILD.gn | 50 +++++++++++++++---- 6 files changed, 97 insertions(+), 28 deletions(-) diff --git a/backup.gni b/backup.gni index 972c4a00b..d6149f38f 100644 --- a/backup.gni +++ b/backup.gni @@ -41,3 +41,16 @@ backup_mock_proxy_src = [ "$path_backup_mock/utils_mock/src/utils_mock_global_variable.cpp", "$path_backup/frameworks/native/backup_kit_inner/src/b_file_info.cpp", ] + +declare_args() { + brotli_enabled = true + if (defined(global_parts_info) && + (!defined(global_parts_info.thirdparty_brotli) || !global_parts_info.thirdparty_brotli)) { + brotli_enabled = false + } + if (current_cpu == "arm" || current_cpu == "x86") { + system_bit = 32 + } else { + system_bit = 64 + } +} \ No newline at end of file diff --git a/frameworks/native/backup_ext/BUILD.gn b/frameworks/native/backup_ext/BUILD.gn index d7113dd41..4c50e6a29 100644 --- a/frameworks/native/backup_ext/BUILD.gn +++ b/frameworks/native/backup_ext/BUILD.gn @@ -67,7 +67,6 @@ ohos_shared_library("backup_extension_ability_native") { "ability_runtime:napi_common", "ability_runtime:runtime", "access_token:libaccesstoken_sdk", - "brotli:brotli_shared", "bundle_framework:appexecfwk_core", "c_utils:utils", "hilog:libhilog", @@ -80,6 +79,16 @@ ohos_shared_library("backup_extension_ability_native") { "samgr:samgr_proxy", ] + if (brotli_enabled) { + external_deps += ["brotli:brotli_shared"] + defines += ["BROTLI_ENABLED"] + } + if (system_bit == 32) { + defines += ["SYSTEM_BIT_32"]; + } else { + defines += ["SYSTEM_BIT_64"]; + } + cflags_cc = [ "-fdata-sections", "-ffunction-sections", diff --git a/frameworks/native/backup_ext/include/tar_file.h b/frameworks/native/backup_ext/include/tar_file.h index d17c3b21c..668e20d3f 100644 --- a/frameworks/native/backup_ext/include/tar_file.h +++ b/frameworks/native/backup_ext/include/tar_file.h @@ -59,9 +59,12 @@ const char GNUTYPE_LONGNAME = 'L'; const char EXTENSION_HEADER = 'x'; const uint32_t OTHER_HEADER = 78; const int ERR_NO_PERMISSION = 13; +#ifdef SYSTEM_BIT_32 +constexpr int SIZE_T_BYTE_LEN = 4; +#else constexpr int SIZE_T_BYTE_LEN = 8; +#endif constexpr size_t MAX_BUFFER_SIZE = 4096; -constexpr bool USE_COMPRESS = false; } // namespace // 512 bytes @@ -89,7 +92,6 @@ using TarMap = std::map> class UniqueFile { public: - UniqueFile(FILE* file); UniqueFile(const char* filePath, const char* mode); ~UniqueFile(); FILE* file_ = nullptr; diff --git a/frameworks/native/backup_ext/src/tar_file.cpp b/frameworks/native/backup_ext/src/tar_file.cpp index 172b146f6..4d6e5e911 100644 --- a/frameworks/native/backup_ext/src/tar_file.cpp +++ b/frameworks/native/backup_ext/src/tar_file.cpp @@ -25,9 +25,6 @@ #include #include #include -#include "brotli/encode.h" -#include "brotli/decode.h" - #include "b_anony/b_anony.h" #include "b_error/b_error.h" #include "b_hiaudit/hi_audit.h" @@ -36,6 +33,11 @@ #include "filemgmt_libhilog.h" #include "securec.h" +#ifdef BROTLI_ENABLED +#include "brotli/encode.h" +#include "brotli/decode.h" +#endif + namespace OHOS::FileManagement::Backup { using namespace std; namespace { @@ -51,18 +53,15 @@ const string VERSION = "1.0"; const string LONG_LINK_SYMBOL = "longLinkSymbol"; const string COMPRESS_FILE_SUFFIX = "__A"; const string TAR_EXTENSION = ".tar"; -constexpr int BROTLI_QUALITY = 3; constexpr int MEGA_BYTE = 1024 * 1024; +#ifdef BROTLI_ENABLED +constexpr int BROTLI_QUALITY = 3; +constexpr bool USE_COMPRESS = false; // 默认关闭 +#else +constexpr bool USE_COMPRESS = false; +#endif } // namespace -UniqueFile::UniqueFile(FILE* file) -{ - if (file == nullptr) { - HILOGE("file is null"); - } - file_ = file; -} - UniqueFile::UniqueFile(const char* filePath, const char* mode) { file_ = fopen(filePath, mode); @@ -506,7 +505,7 @@ bool TarFile::FillSplitTailBlocks() if (isReset_) { tarMap_.clear(); } - if (USE_COMPRESS) { + if (defined(BROTLI_ENABLED) && USE_COMPRESS) { std::filesystem::path fullTarPath = currentTarName_; // 全量路径 std::filesystem::path parentPath = fullTarPath.parent_path(); std::filesystem::path fileNameWithoutExtension = fullTarPath.stem(); // 文件名前缀 @@ -718,27 +717,32 @@ void TarFile::SetPacketMode(bool isReset) bool TarFile::Compress(const uint8_t* inputBuffer, size_t inputSize, uint8_t* outputBuffer, size_t* outputSize) { +#ifdef BROTLI_ENABLED int ret = BrotliEncoderCompress(BROTLI_QUALITY, BROTLI_MAX_WINDOW_BITS, BROTLI_DEFAULT_MODE, inputSize, inputBuffer, outputSize, outputBuffer); if (ret != BROTLI_TRUE) { HILOGE("compress fail, error: %{public}d", ret); return false; } +#endif return true; } bool TarFile::Decompress(const uint8_t* inputBuffer, size_t inputSize, uint8_t* outputBuffer, size_t* outputSize) { +#ifdef BROTLI_ENABLED int ret = BrotliDecoderDecompress(inputSize, inputBuffer, outputSize, outputBuffer); if (ret != BROTLI_TRUE) { HILOGE("decompress fail, error: %{public}d", ret); return false; } +#endif return true; } bool TarFile::CompressFile(const std::string &srcFile, const std::string &compFile) { +#ifdef BROTLI_ENABLED HILOGI("BEGIN strF: %{public}s, compF: %{public}s", GetAnonyPath(srcFile).c_str(), GetAnonyPath(compFile).c_str()); UniqueFile fin(srcFile.c_str(), "rb"); UniqueFile fout(compFile.c_str(), "wb"); @@ -767,7 +771,7 @@ bool TarFile::CompressFile(const std::string &srcFile, const std::string &compFi return false; } compSpan += (std::chrono::high_resolution_clock::now() - startTime); - if (compressBuffer.size_ < 0 || compressBuffer.size_ >= ori.size_) { // 压缩后大小大于原始大小则直接存储原始内容 + if (compressBuffer.size_ >= ori.size_) { // 压缩后大小大于原始大小则直接存储原始内容 compressBuffer.size_ = ori.size_; fwrite(&ori.size_, SIZE_T_BYTE_LEN, 1, fout.file_); fwrite(&ori.size_, SIZE_T_BYTE_LEN, 1, fout.file_); @@ -783,6 +787,7 @@ bool TarFile::CompressFile(const std::string &srcFile, const std::string &compFi HILOGI("END srcSize:%{public}zu, destSize:%{public}zu, rate:%{public}f, time:%{public}f ms, speed:%{public}f MB/s", inTotal, outTotal, (outTotal == 0) ? 0 : (inTotal * 1.0f / outTotal), compSpan.count(), (compSpan.count() == 0) ? 0 : inTotal * 1000.0f / MEGA_BYTE / compSpan.count()); +#endif return true; } @@ -840,7 +845,7 @@ bool TarFile::DecompressFile(const std::string &compFile, const std::string &src std::string TarFile::DecompressTar(const std::string &tarPath) { - if (!USE_COMPRESS) { + if (!defined(BROTLI_ENABLED) || !USE_COMPRESS) { return tarPath; } std::filesystem::path filePath = tarPath; diff --git a/test/fuzztest/backupext_fuzzer/BUILD.gn b/test/fuzztest/backupext_fuzzer/BUILD.gn index 3d4b0de11..13147f656 100644 --- a/test/fuzztest/backupext_fuzzer/BUILD.gn +++ b/test/fuzztest/backupext_fuzzer/BUILD.gn @@ -65,7 +65,6 @@ ohos_fuzztest("BackupExtFuzzTest") { "ability_runtime:appkit_native", "ability_runtime:runtime", "access_token:libaccesstoken_sdk", - "brotli:brotli_shared", "bundle_framework:appexecfwk_core", "c_utils:utils", "hilog:libhilog", @@ -83,6 +82,15 @@ ohos_fuzztest("BackupExtFuzzTest") { "private = public", ] + if (brotli_enabled) { + external_deps += ["brotli:brotli_shared"] + } + if (system_bit == 32) { + defines += ["SYSTEM_BIT_32"]; + } else { + defines += ["SYSTEM_BIT_64"]; + } + use_exceptions = true } ############################################################################### diff --git a/tests/unittests/backup_ext/BUILD.gn b/tests/unittests/backup_ext/BUILD.gn index b8f7048e2..1bae87bc2 100644 --- a/tests/unittests/backup_ext/BUILD.gn +++ b/tests/unittests/backup_ext/BUILD.gn @@ -174,7 +174,6 @@ ohos_unittest("tar_file_test") { "ability_runtime:appkit_native", "ability_runtime:runtime", "access_token:libaccesstoken_sdk", - "brotli:brotli_shared", "bundle_framework:appexecfwk_core", "c_utils:utils", "googletest:gmock_main", @@ -190,6 +189,15 @@ ohos_unittest("tar_file_test") { defines = [ "private=public" ] + if (brotli_enabled) { + external_deps += ["brotli:brotli_shared"] + } + if (system_bit == 32) { + defines += ["SYSTEM_BIT_32"]; + } else { + defines += ["SYSTEM_BIT_64"]; + } + use_exceptions = true } @@ -256,7 +264,6 @@ ohos_unittest("untar_file_sup_test") { "ability_runtime:appkit_native", "ability_runtime:runtime", "access_token:libaccesstoken_sdk", - "brotli:brotli_shared", "bundle_framework:appexecfwk_core", "c_utils:utils", "googletest:gmock_main", @@ -271,7 +278,14 @@ ohos_unittest("untar_file_sup_test") { ] defines = [ "private=public" ] - + if (brotli_enabled) { + external_deps += ["brotli:brotli_shared"] + } + if (system_bit == 32) { + defines += ["SYSTEM_BIT_32"]; + } else { + defines += ["SYSTEM_BIT_64"]; + } use_exceptions = true } @@ -332,7 +346,6 @@ ohos_unittest("untar_file_test") { "ability_runtime:appkit_native", "ability_runtime:runtime", "access_token:libaccesstoken_sdk", - "brotli:brotli_shared", "bundle_framework:appexecfwk_core", "c_utils:utils", "googletest:gmock_main", @@ -347,7 +360,14 @@ ohos_unittest("untar_file_test") { ] defines = [ "private=public" ] - + if (brotli_enabled) { + external_deps += ["brotli:brotli_shared"] + } + if (system_bit == 32) { + defines += ["SYSTEM_BIT_32"]; + } else { + defines += ["SYSTEM_BIT_64"]; + } use_exceptions = true } @@ -447,14 +467,20 @@ ohos_unittest("tar_file_sub_test") { external_deps = [ "c_utils:utils", - "brotli:brotli_shared", "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", ] defines = [ "private=public" ] - + if (brotli_enabled) { + external_deps += ["brotli:brotli_shared"] + } + if (system_bit == 32) { + defines += ["SYSTEM_BIT_32"]; + } else { + defines += ["SYSTEM_BIT_64"]; + } use_exceptions = true } @@ -483,14 +509,20 @@ ohos_unittest("installd_un_tar_file_test") { external_deps = [ "c_utils:utils", - "brotli:brotli_shared", "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", ] defines = [ "private=public" ] - + if (brotli_enabled) { + external_deps += ["brotli:brotli_shared"] + } + if (system_bit == 32) { + defines += ["SYSTEM_BIT_32"]; + } else { + defines += ["SYSTEM_BIT_64"]; + } use_exceptions = true } -- Gitee From 38a71c4d2ac3381b7200bdf6daedd0d835188244 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B8=8A?= Date: Thu, 31 Jul 2025 11:46:13 +0800 Subject: [PATCH 4/9] fix compile problem MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘渊 --- frameworks/native/backup_ext/BUILD.gn | 4 ++-- .../native/backup_ext/include/tar_file.h | 4 ++++ test/fuzztest/backupext_fuzzer/BUILD.gn | 4 ++-- tests/unittests/backup_ext/BUILD.gn | 20 +++++++++---------- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/frameworks/native/backup_ext/BUILD.gn b/frameworks/native/backup_ext/BUILD.gn index 4c50e6a29..27a36aae5 100644 --- a/frameworks/native/backup_ext/BUILD.gn +++ b/frameworks/native/backup_ext/BUILD.gn @@ -84,9 +84,9 @@ ohos_shared_library("backup_extension_ability_native") { defines += ["BROTLI_ENABLED"] } if (system_bit == 32) { - defines += ["SYSTEM_BIT_32"]; + defines += ["SYSTEM_BIT_32"] } else { - defines += ["SYSTEM_BIT_64"]; + defines += ["SYSTEM_BIT_64"] } cflags_cc = [ diff --git a/frameworks/native/backup_ext/include/tar_file.h b/frameworks/native/backup_ext/include/tar_file.h index 668e20d3f..d581b7967 100644 --- a/frameworks/native/backup_ext/include/tar_file.h +++ b/frameworks/native/backup_ext/include/tar_file.h @@ -93,6 +93,8 @@ using TarMap = std::map> class UniqueFile { public: UniqueFile(const char* filePath, const char* mode); + UniqueFile(const UniqueFile&) = delete; + UniqueFile& operator=(const UniqueFile&) = delete; ~UniqueFile(); FILE* file_ = nullptr; }; @@ -101,6 +103,8 @@ template class Buffer { public: Buffer(size_t size); + Buffer(const Buffer&) = delete; + Buffer& operator=(const Buffer&) = delete; ~Buffer(); T* data_ = nullptr; size_t size_ = 0; diff --git a/test/fuzztest/backupext_fuzzer/BUILD.gn b/test/fuzztest/backupext_fuzzer/BUILD.gn index 13147f656..213accec6 100644 --- a/test/fuzztest/backupext_fuzzer/BUILD.gn +++ b/test/fuzztest/backupext_fuzzer/BUILD.gn @@ -86,9 +86,9 @@ ohos_fuzztest("BackupExtFuzzTest") { external_deps += ["brotli:brotli_shared"] } if (system_bit == 32) { - defines += ["SYSTEM_BIT_32"]; + defines += ["SYSTEM_BIT_32"] } else { - defines += ["SYSTEM_BIT_64"]; + defines += ["SYSTEM_BIT_64"] } use_exceptions = true diff --git a/tests/unittests/backup_ext/BUILD.gn b/tests/unittests/backup_ext/BUILD.gn index 1bae87bc2..83268e88e 100644 --- a/tests/unittests/backup_ext/BUILD.gn +++ b/tests/unittests/backup_ext/BUILD.gn @@ -193,9 +193,9 @@ ohos_unittest("tar_file_test") { external_deps += ["brotli:brotli_shared"] } if (system_bit == 32) { - defines += ["SYSTEM_BIT_32"]; + defines += ["SYSTEM_BIT_32"] } else { - defines += ["SYSTEM_BIT_64"]; + defines += ["SYSTEM_BIT_64"] } use_exceptions = true @@ -282,9 +282,9 @@ ohos_unittest("untar_file_sup_test") { external_deps += ["brotli:brotli_shared"] } if (system_bit == 32) { - defines += ["SYSTEM_BIT_32"]; + defines += ["SYSTEM_BIT_32"] } else { - defines += ["SYSTEM_BIT_64"]; + defines += ["SYSTEM_BIT_64"] } use_exceptions = true } @@ -364,9 +364,9 @@ ohos_unittest("untar_file_test") { external_deps += ["brotli:brotli_shared"] } if (system_bit == 32) { - defines += ["SYSTEM_BIT_32"]; + defines += ["SYSTEM_BIT_32"] } else { - defines += ["SYSTEM_BIT_64"]; + defines += ["SYSTEM_BIT_64"] } use_exceptions = true } @@ -477,9 +477,9 @@ ohos_unittest("tar_file_sub_test") { external_deps += ["brotli:brotli_shared"] } if (system_bit == 32) { - defines += ["SYSTEM_BIT_32"]; + defines += ["SYSTEM_BIT_32"] } else { - defines += ["SYSTEM_BIT_64"]; + defines += ["SYSTEM_BIT_64"] } use_exceptions = true } @@ -519,9 +519,9 @@ ohos_unittest("installd_un_tar_file_test") { external_deps += ["brotli:brotli_shared"] } if (system_bit == 32) { - defines += ["SYSTEM_BIT_32"]; + defines += ["SYSTEM_BIT_32"] } else { - defines += ["SYSTEM_BIT_64"]; + defines += ["SYSTEM_BIT_64"] } use_exceptions = true } -- Gitee From 2403da28642efc7f03af2c70796f1bc399732074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B8=8A?= Date: Thu, 31 Jul 2025 15:32:23 +0800 Subject: [PATCH 5/9] fix code sytle problem MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘渊 --- frameworks/native/backup_ext/src/tar_file.cpp | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/frameworks/native/backup_ext/src/tar_file.cpp b/frameworks/native/backup_ext/src/tar_file.cpp index 4d6e5e911..f3b734d3f 100644 --- a/frameworks/native/backup_ext/src/tar_file.cpp +++ b/frameworks/native/backup_ext/src/tar_file.cpp @@ -771,15 +771,20 @@ bool TarFile::CompressFile(const std::string &srcFile, const std::string &compFi return false; } compSpan += (std::chrono::high_resolution_clock::now() - startTime); + size_t written = 0; if (compressBuffer.size_ >= ori.size_) { // 压缩后大小大于原始大小则直接存储原始内容 compressBuffer.size_ = ori.size_; - fwrite(&ori.size_, SIZE_T_BYTE_LEN, 1, fout.file_); - fwrite(&ori.size_, SIZE_T_BYTE_LEN, 1, fout.file_); - fwrite(ori.data_, 1, ori.size_, fout.file_); + written += fwrite(&ori.size_, SIZE_T_BYTE_LEN, 1, fout.file_); + written += fwrite(&ori.size_, SIZE_T_BYTE_LEN, 1, fout.file_); + written += fwrite(ori.data_, 1, ori.size_, fout.file_); } else { - fwrite(&compressBuffer.size_, SIZE_T_BYTE_LEN, 1, fout.file_); - fwrite(&ori.size_, SIZE_T_BYTE_LEN, 1, fout.file_); - fwrite(compressBuffer.data_, 1, compressBuffer.size_, fout.file_); + written += fwrite(&compressBuffer.size_, SIZE_T_BYTE_LEN, 1, fout.file_); + written += fwrite(&ori.size_, SIZE_T_BYTE_LEN, 1, fout.file_); + written += fwrite(compressBuffer.data_, 1, compressBuffer.size_, fout.file_); + } + if (written != compressBuffer.size_ + 2) { + HILOGI("write data fail error: %{public}s", strerror(errno)); + return false; } inTotal += ori.size_; outTotal += compressBuffer.size_; @@ -809,7 +814,9 @@ bool TarFile::DecompressFile(const std::string &compFile, const std::string &src size_t inTotal = 0; size_t outTotal = 0; size_t size; +#ifdef COMPRESS_DEBUG auto decompSpan = std::chrono::duration(std::chrono::seconds(0)); +#endif while ((size = fread(&compressBuffer.size_, SIZE_T_BYTE_LEN, 1, fin.file_)) > 0) { if (size != 1) { HILOGE("read comp size fail"); @@ -824,22 +831,33 @@ bool TarFile::DecompressFile(const std::string &compFile, const std::string &src HILOGE("read comp buffer fail"); return false; } + size_t written = 0; if (compressBuffer.size_ == decompressBuffer.size_) { - fwrite(compressBuffer.data_, 1, compressBuffer.size_, fout.file_); + written += fwrite(compressBuffer.data_, 1, compressBuffer.size_, fout.file_); } else { +#ifdef COMPRESS_DEBUG auto startTime = std::chrono::high_resolution_clock::now(); +#endif if (!Decompress((const uint8_t*)compressBuffer.data_, compressBuffer.size_, decompressBuffer.data_, &decompressBuffer.size_)) { return false; } +#ifdef COMPRESS_DEBUG decompSpan += (std::chrono::high_resolution_clock::now() - startTime); - fwrite(decompressBuffer.data_, 1, decompressBuffer.size_, fout.file_); +#endif + written += fwrite(decompressBuffer.data_, 1, decompressBuffer.size_, fout.file_); + } + if (written != decompressBuffer.size_) { + HILOGI("write data fail error: %{public}s", strerror(errno)); + return false; } inTotal += compressBuffer.size_; outTotal += decompressBuffer.size_; } +#ifdef COMPRESS_DEBUG HILOGI("srcSize:%{public}zu, destSize:%{public}zu, time:%{public}f ms, speed:%{public}f MB/s", inTotal, outTotal, decompSpan.count(), (decompSpan.count() == 0) ? 0 : outTotal * 1000.0f / MEGA_BYTE / decompSpan.count()); +#endif return true; } -- Gitee From 34b6c64258290c04c5732e32f61a3c0ee7dc8133 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B8=8A?= Date: Thu, 31 Jul 2025 16:39:34 +0800 Subject: [PATCH 6/9] fix code style problem MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘渊 --- .../native/backup_ext/include/tar_file.h | 3 + frameworks/native/backup_ext/src/tar_file.cpp | 88 +++++++++++-------- test/fuzztest/backupext_fuzzer/BUILD.gn | 3 - tests/unittests/backup_ext/BUILD.gn | 16 ---- 4 files changed, 53 insertions(+), 57 deletions(-) diff --git a/frameworks/native/backup_ext/include/tar_file.h b/frameworks/native/backup_ext/include/tar_file.h index d581b7967..bed12aacd 100644 --- a/frameworks/native/backup_ext/include/tar_file.h +++ b/frameworks/native/backup_ext/include/tar_file.h @@ -135,6 +135,9 @@ public: bool DecompressFile(const std::string &compFile, const std::string &srcFile); std::string DecompressTar(const std::string &tarPath); private: + bool WriteCompressData(Buffer& compressBuffer, const Buffer& ori, UniqueFile& fout); + bool WriteDecompressData(const Buffer& compressBuffer, Buffer& decompressBuffer, + UniqueFile& fout, std::chrono::duration& decompSpan); TarFile() {} ~TarFile() = default; TarFile(const TarFile &instance) = delete; diff --git a/frameworks/native/backup_ext/src/tar_file.cpp b/frameworks/native/backup_ext/src/tar_file.cpp index f3b734d3f..a19407d0d 100644 --- a/frameworks/native/backup_ext/src/tar_file.cpp +++ b/frameworks/native/backup_ext/src/tar_file.cpp @@ -53,8 +53,8 @@ const string VERSION = "1.0"; const string LONG_LINK_SYMBOL = "longLinkSymbol"; const string COMPRESS_FILE_SUFFIX = "__A"; const string TAR_EXTENSION = ".tar"; -constexpr int MEGA_BYTE = 1024 * 1024; #ifdef BROTLI_ENABLED +constexpr int MEGA_BYTE = 1024 * 1024; constexpr int BROTLI_QUALITY = 3; constexpr bool USE_COMPRESS = false; // 默认关闭 #else @@ -81,7 +81,7 @@ UniqueFile::~UniqueFile() } template -Buffer:Buffer(size_t size) +Buffer::Buffer(size_t size) { if (size <= 0 || size > MAX_BUFFER_SIZE) { return; @@ -505,7 +505,7 @@ bool TarFile::FillSplitTailBlocks() if (isReset_) { tarMap_.clear(); } - if (defined(BROTLI_ENABLED) && USE_COMPRESS) { + if (USE_COMPRESS) { std::filesystem::path fullTarPath = currentTarName_; // 全量路径 std::filesystem::path parentPath = fullTarPath.parent_path(); std::filesystem::path fileNameWithoutExtension = fullTarPath.stem(); // 文件名前缀 @@ -740,6 +740,27 @@ bool TarFile::Decompress(const uint8_t* inputBuffer, size_t inputSize, uint8_t* return true; } +bool TarFile::WriteCompressData(Buffer& compressBuffer, const Buffer& ori, UniqueFile& fout) +{ + size_t written = 0; + size_t sizeCount = 1; + if (compressBuffer.size_ >= ori.size_) { // 压缩后大小大于原始大小则直接存储原始内容 + compressBuffer.size_ = ori.size_; + written += fwrite(&ori.size_, SIZE_T_BYTE_LEN, sizeCount, fout.file_); + written += fwrite(&ori.size_, SIZE_T_BYTE_LEN, sizeCount, fout.file_); + written += fwrite(ori.data_, 1, ori.size_, fout.file_); + } else { + written += fwrite(&compressBuffer.size_, SIZE_T_BYTE_LEN, sizeCount, fout.file_); + written += fwrite(&ori.size_, SIZE_T_BYTE_LEN, sizeCount, fout.file_); + written += fwrite(compressBuffer.data_, 1, compressBuffer.size_, fout.file_); + } + if (written != compressBuffer.size_ + sizeCount + sizeCount) { + HILOGI("write data fail error: %{public}s", strerror(errno)); + return false; + } + return true; +} + bool TarFile::CompressFile(const std::string &srcFile, const std::string &compFile) { #ifdef BROTLI_ENABLED @@ -771,19 +792,7 @@ bool TarFile::CompressFile(const std::string &srcFile, const std::string &compFi return false; } compSpan += (std::chrono::high_resolution_clock::now() - startTime); - size_t written = 0; - if (compressBuffer.size_ >= ori.size_) { // 压缩后大小大于原始大小则直接存储原始内容 - compressBuffer.size_ = ori.size_; - written += fwrite(&ori.size_, SIZE_T_BYTE_LEN, 1, fout.file_); - written += fwrite(&ori.size_, SIZE_T_BYTE_LEN, 1, fout.file_); - written += fwrite(ori.data_, 1, ori.size_, fout.file_); - } else { - written += fwrite(&compressBuffer.size_, SIZE_T_BYTE_LEN, 1, fout.file_); - written += fwrite(&ori.size_, SIZE_T_BYTE_LEN, 1, fout.file_); - written += fwrite(compressBuffer.data_, 1, compressBuffer.size_, fout.file_); - } - if (written != compressBuffer.size_ + 2) { - HILOGI("write data fail error: %{public}s", strerror(errno)); + if (!WriteCompressData(compressBuffer, ori, fout)) { return false; } inTotal += ori.size_; @@ -796,8 +805,31 @@ bool TarFile::CompressFile(const std::string &srcFile, const std::string &compFi return true; } +bool TarFile::WriteDecompressData(const Buffer& compressBuffer, Buffer& decompressBuffer, + UniqueFile& fout, std::chrono::duration& decompSpan) +{ + size_t written = 0; + if (compressBuffer.size_ == decompressBuffer.size_) { + written += fwrite(compressBuffer.data_, 1, compressBuffer.size_, fout.file_); + } else { + auto startTime = std::chrono::high_resolution_clock::now(); + if (!Decompress((const uint8_t*)compressBuffer.data_, compressBuffer.size_, decompressBuffer.data_, + &decompressBuffer.size_)) { + return false; + } + decompSpan += (std::chrono::high_resolution_clock::now() - startTime); + written += fwrite(decompressBuffer.data_, 1, decompressBuffer.size_, fout.file_); + } + if (written != decompressBuffer.size_) { + HILOGI("write data fail error: %{public}s", strerror(errno)); + return false; + } + return true; +} + bool TarFile::DecompressFile(const std::string &compFile, const std::string &srcFile) { +#ifdef BROTLI_ENABLED HILOGI("BEGIN, compF:%{public}s, srcF:%{public}s", GetAnonyPath(compFile).c_str(), GetAnonyPath(srcFile).c_str()); UniqueFile fin(compFile.c_str(), "rb"); UniqueFile fout(srcFile.c_str(), "wb"); @@ -814,9 +846,7 @@ bool TarFile::DecompressFile(const std::string &compFile, const std::string &src size_t inTotal = 0; size_t outTotal = 0; size_t size; -#ifdef COMPRESS_DEBUG auto decompSpan = std::chrono::duration(std::chrono::seconds(0)); -#endif while ((size = fread(&compressBuffer.size_, SIZE_T_BYTE_LEN, 1, fin.file_)) > 0) { if (size != 1) { HILOGE("read comp size fail"); @@ -831,30 +861,12 @@ bool TarFile::DecompressFile(const std::string &compFile, const std::string &src HILOGE("read comp buffer fail"); return false; } - size_t written = 0; - if (compressBuffer.size_ == decompressBuffer.size_) { - written += fwrite(compressBuffer.data_, 1, compressBuffer.size_, fout.file_); - } else { -#ifdef COMPRESS_DEBUG - auto startTime = std::chrono::high_resolution_clock::now(); -#endif - if (!Decompress((const uint8_t*)compressBuffer.data_, compressBuffer.size_, decompressBuffer.data_, - &decompressBuffer.size_)) { - return false; - } -#ifdef COMPRESS_DEBUG - decompSpan += (std::chrono::high_resolution_clock::now() - startTime); -#endif - written += fwrite(decompressBuffer.data_, 1, decompressBuffer.size_, fout.file_); - } - if (written != decompressBuffer.size_) { - HILOGI("write data fail error: %{public}s", strerror(errno)); + if (!WriteDecompressData(compressBuffer, decompressBuffer, fout, decompSpan)) { return false; } inTotal += compressBuffer.size_; outTotal += decompressBuffer.size_; } -#ifdef COMPRESS_DEBUG HILOGI("srcSize:%{public}zu, destSize:%{public}zu, time:%{public}f ms, speed:%{public}f MB/s", inTotal, outTotal, decompSpan.count(), (decompSpan.count() == 0) ? 0 : outTotal * 1000.0f / MEGA_BYTE / decompSpan.count()); #endif @@ -863,7 +875,7 @@ bool TarFile::DecompressFile(const std::string &compFile, const std::string &src std::string TarFile::DecompressTar(const std::string &tarPath) { - if (!defined(BROTLI_ENABLED) || !USE_COMPRESS) { + if (!USE_COMPRESS) { return tarPath; } std::filesystem::path filePath = tarPath; diff --git a/test/fuzztest/backupext_fuzzer/BUILD.gn b/test/fuzztest/backupext_fuzzer/BUILD.gn index 213accec6..5e569b6b3 100644 --- a/test/fuzztest/backupext_fuzzer/BUILD.gn +++ b/test/fuzztest/backupext_fuzzer/BUILD.gn @@ -82,9 +82,6 @@ ohos_fuzztest("BackupExtFuzzTest") { "private = public", ] - if (brotli_enabled) { - external_deps += ["brotli:brotli_shared"] - } if (system_bit == 32) { defines += ["SYSTEM_BIT_32"] } else { diff --git a/tests/unittests/backup_ext/BUILD.gn b/tests/unittests/backup_ext/BUILD.gn index 83268e88e..b350117a4 100644 --- a/tests/unittests/backup_ext/BUILD.gn +++ b/tests/unittests/backup_ext/BUILD.gn @@ -188,10 +188,6 @@ ohos_unittest("tar_file_test") { ] defines = [ "private=public" ] - - if (brotli_enabled) { - external_deps += ["brotli:brotli_shared"] - } if (system_bit == 32) { defines += ["SYSTEM_BIT_32"] } else { @@ -278,9 +274,6 @@ ohos_unittest("untar_file_sup_test") { ] defines = [ "private=public" ] - if (brotli_enabled) { - external_deps += ["brotli:brotli_shared"] - } if (system_bit == 32) { defines += ["SYSTEM_BIT_32"] } else { @@ -360,9 +353,6 @@ ohos_unittest("untar_file_test") { ] defines = [ "private=public" ] - if (brotli_enabled) { - external_deps += ["brotli:brotli_shared"] - } if (system_bit == 32) { defines += ["SYSTEM_BIT_32"] } else { @@ -473,9 +463,6 @@ ohos_unittest("tar_file_sub_test") { ] defines = [ "private=public" ] - if (brotli_enabled) { - external_deps += ["brotli:brotli_shared"] - } if (system_bit == 32) { defines += ["SYSTEM_BIT_32"] } else { @@ -515,9 +502,6 @@ ohos_unittest("installd_un_tar_file_test") { ] defines = [ "private=public" ] - if (brotli_enabled) { - external_deps += ["brotli:brotli_shared"] - } if (system_bit == 32) { defines += ["SYSTEM_BIT_32"] } else { -- Gitee From d77a95e2606da44e0df430e8aa1f91d876354bf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B8=8A?= Date: Thu, 31 Jul 2025 20:14:58 +0800 Subject: [PATCH 7/9] add preview opinion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘渊 --- frameworks/native/backup_ext/src/tar_file.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/frameworks/native/backup_ext/src/tar_file.cpp b/frameworks/native/backup_ext/src/tar_file.cpp index a19407d0d..1e0fbe5d2 100644 --- a/frameworks/native/backup_ext/src/tar_file.cpp +++ b/frameworks/native/backup_ext/src/tar_file.cpp @@ -513,8 +513,11 @@ bool TarFile::FillSplitTailBlocks() string newTarPath = parentPath.string() + "/" + tarNewName; HILOGI("tarFileName:%{public}s, currentTarName:%{public}s, newTarPath:%{public}s", tarFileName_.c_str(), currentTarName_.c_str(), newTarPath.c_str()); - tarMap_.emplace(tarNewName, make_tuple(newTarPath, staTar, false)); - CompressFile(fullTarPath, newTarPath); + if (!CompressFile(fullTarPath, newTarPath)) { + tarMap_.emplace(tarFileName_, make_tuple(currentTarName_, staTar, false)); + } else { + tarMap_.emplace(tarNewName, make_tuple(newTarPath, staTar, false)); + } } else { tarMap_.emplace(tarFileName_, make_tuple(currentTarName_, staTar, false)); } -- Gitee From 572adbad4fcedc1a28e4f60f0b6b5dbe86b04dbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B8=8A?= Date: Thu, 31 Jul 2025 22:17:27 +0800 Subject: [PATCH 8/9] fix review opinion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘渊 --- frameworks/native/backup_ext/src/tar_file.cpp | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/frameworks/native/backup_ext/src/tar_file.cpp b/frameworks/native/backup_ext/src/tar_file.cpp index 1e0fbe5d2..df4e26a79 100644 --- a/frameworks/native/backup_ext/src/tar_file.cpp +++ b/frameworks/native/backup_ext/src/tar_file.cpp @@ -745,20 +745,24 @@ bool TarFile::Decompress(const uint8_t* inputBuffer, size_t inputSize, uint8_t* bool TarFile::WriteCompressData(Buffer& compressBuffer, const Buffer& ori, UniqueFile& fout) { - size_t written = 0; size_t sizeCount = 1; + char* writeData = (char *)compressBuffer.data_; + size_t writeDataSize = compressBuffer.size_; if (compressBuffer.size_ >= ori.size_) { // 压缩后大小大于原始大小则直接存储原始内容 compressBuffer.size_ = ori.size_; - written += fwrite(&ori.size_, SIZE_T_BYTE_LEN, sizeCount, fout.file_); - written += fwrite(&ori.size_, SIZE_T_BYTE_LEN, sizeCount, fout.file_); - written += fwrite(ori.data_, 1, ori.size_, fout.file_); - } else { - written += fwrite(&compressBuffer.size_, SIZE_T_BYTE_LEN, sizeCount, fout.file_); - written += fwrite(&ori.size_, SIZE_T_BYTE_LEN, sizeCount, fout.file_); - written += fwrite(compressBuffer.data_, 1, compressBuffer.size_, fout.file_); + writeData = ori.data_; + writeDataSize = ori.size_; } - if (written != compressBuffer.size_ + sizeCount + sizeCount) { - HILOGI("write data fail error: %{public}s", strerror(errno)); + if (fwrite(&compressBuffer.size_, SIZE_T_BYTE_LEN, sizeCount, fout.file_) != sizeCount) { + HILOGI("write compress size fail error: %{public}s", strerror(errno)); + return false; + } + if (fwrite(&ori.size_, SIZE_T_BYTE_LEN, sizeCount, fout.file_) != sizeCount) { + HILOGI("write ori size fail error: %{public}s", strerror(errno)); + return false; + } + if (fwrite(writeData, 1, writeDataSize, fout.file_) != writeDataSize) { + HILOGI("write compress data fail error: %{public}s", strerror(errno)); return false; } return true; -- Gitee From 488efd2bd27a05df81466a6943ee0397979227d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B8=8A?= Date: Thu, 21 Aug 2025 09:59:08 +0800 Subject: [PATCH 9/9] fix review opition MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘渊 --- frameworks/native/backup_ext/src/tar_file.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/frameworks/native/backup_ext/src/tar_file.cpp b/frameworks/native/backup_ext/src/tar_file.cpp index df4e26a79..5b5d718b0 100644 --- a/frameworks/native/backup_ext/src/tar_file.cpp +++ b/frameworks/native/backup_ext/src/tar_file.cpp @@ -511,8 +511,9 @@ bool TarFile::FillSplitTailBlocks() std::filesystem::path fileNameWithoutExtension = fullTarPath.stem(); // 文件名前缀 std::string tarNewName = fileNameWithoutExtension.string() + COMPRESS_FILE_SUFFIX + TAR_EXTENSION; string newTarPath = parentPath.string() + "/" + tarNewName; - HILOGI("tarFileName:%{public}s, currentTarName:%{public}s, newTarPath:%{public}s", tarFileName_.c_str(), - currentTarName_.c_str(), newTarPath.c_str()); + HILOGI("tarFileName:%{public}s, currentTarName:%{public}s, newTarPath:%{public}s", + GetAnonyPath(tarFileName_).c_str(), GetAnonyPath(currentTarName_).c_str(), + GetAnonyPath(newTarPath).c_str()); if (!CompressFile(fullTarPath, newTarPath)) { tarMap_.emplace(tarFileName_, make_tuple(currentTarName_, staTar, false)); } else { @@ -754,7 +755,7 @@ bool TarFile::WriteCompressData(Buffer& compressBuffer, const Buffer