From f5ec2bba518783dfd0969dd96307a03cc9128ab3 Mon Sep 17 00:00:00 2001 From: liduo Date: Sat, 30 Aug 2025 17:37:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Drestool=20crash=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liduo --- include/binary_file_packer.h | 11 +++++++---- src/binary_file_packer.cpp | 26 +++++++++++++++++++------- src/overlap_binary_file_packer.cpp | 4 ++-- src/resource_overlap.cpp | 8 ++++---- src/resource_pack.cpp | 6 +++--- 5 files changed, 35 insertions(+), 20 deletions(-) diff --git a/include/binary_file_packer.h b/include/binary_file_packer.h index 00c45ed..f1fb6a3 100644 --- a/include/binary_file_packer.h +++ b/include/binary_file_packer.h @@ -27,8 +27,9 @@ class BinaryFilePacker { public: explicit BinaryFilePacker(const PackageParser &packageParser, const std::string &moduleName); virtual ~BinaryFilePacker(); - std::future CopyBinaryFileAsync(const std::vector &inputs); - void StopCopy(); + void CopyBinaryFileAsync(const std::vector &inputs); + void Terminate(); + uint32_t GetResult(); protected: virtual uint32_t CopyBinaryFile(const std::vector &inputs); @@ -37,14 +38,16 @@ protected: virtual bool IsDuplicated(const std::unique_ptr &entry, std::string subPath); PackageParser packageParser_; std::string moduleName_; - std::vector> copyResults_; - std::atomic stopCopy_{false}; std::mutex mutex_; private: uint32_t CopyBinaryFile(const std::string &filePath, const std::string &fileType); uint32_t CopyBinaryFileImpl(const std::string &src, const std::string &dst); uint32_t CopySingleFile(const std::string &path, std::string &subPath); + std::future copyFuture_; + std::vector> copyResults_; + std::atomic terminate_{false}; + uint32_t result_ = RESTOOL_SUCCESS; }; } // namespace Restool } // namespace Global diff --git a/src/binary_file_packer.cpp b/src/binary_file_packer.cpp index 9e291a0..120b921 100644 --- a/src/binary_file_packer.cpp +++ b/src/binary_file_packer.cpp @@ -32,16 +32,24 @@ BinaryFilePacker::~BinaryFilePacker() { } -void BinaryFilePacker::StopCopy() +void BinaryFilePacker::Terminate() { - stopCopy_.store(true); + terminate_.store(true); + GetResult(); } -std::future BinaryFilePacker::CopyBinaryFileAsync(const std::vector &inputs) +uint32_t BinaryFilePacker::GetResult() +{ + if (copyFuture_.valid()) { + result_ = copyFuture_.get(); + } + return result_; +} + +void BinaryFilePacker::CopyBinaryFileAsync(const std::vector &inputs) { auto func = [this](const vector &inputs) { return this->CopyBinaryFile(inputs); }; - std::future res = ThreadPool::GetInstance().Enqueue(func, inputs); - return res; + copyFuture_ = ThreadPool::GetInstance().Enqueue(func, inputs); } uint32_t BinaryFilePacker::CopyBinaryFile(const vector &inputs) @@ -118,7 +126,7 @@ uint32_t BinaryFilePacker::CopyBinaryFileImpl(const string &src, const string &d continue; } - if (stopCopy_.load()) { + if (terminate_.load()) { cout << "Info: CopyBinaryFileImpl: stop copy binary file." << endl; return RESTOOL_ERROR; } @@ -145,6 +153,10 @@ bool BinaryFilePacker::IsDuplicated(const unique_ptr &entry, string s uint32_t BinaryFilePacker::CopySingleFile(const std::string &path, std::string &subPath) { + if (terminate_.load()) { + cout << "Info: CopySingleFile: stop copy binary file." << endl; + return RESTOOL_ERROR; + } if (moduleName_ == "har" || CompressionParser::GetCompressionParser()->GetDefaultCompress()) { if (!ResourceUtil::CopyFileInner(path, subPath)) { return RESTOOL_ERROR; @@ -160,7 +172,7 @@ uint32_t BinaryFilePacker::CopySingleFile(const std::string &path, std::string & uint32_t BinaryFilePacker::CheckCopyResults() { for (auto &res : copyResults_) { - if (stopCopy_.load()) { + if (terminate_.load()) { cout << "Info: CopyBinaryFile: stop copy binary file." << endl; return RESTOOL_ERROR; } diff --git a/src/overlap_binary_file_packer.cpp b/src/overlap_binary_file_packer.cpp index 7dc0bd6..1af9e25 100644 --- a/src/overlap_binary_file_packer.cpp +++ b/src/overlap_binary_file_packer.cpp @@ -35,8 +35,8 @@ uint32_t OverlapBinaryFilePacker::CopyBinaryFile(const vector &inputs) vector resource(inputs.begin() + 1, inputs.end()); BinaryFilePacker rawFilePacker(packageParser_, moduleName_); - std::future copyFuture = rawFilePacker.CopyBinaryFileAsync(resource); - if (copyFuture.get() != RESTOOL_SUCCESS) { + rawFilePacker.CopyBinaryFileAsync(resource); + if (rawFilePacker.GetResult() != RESTOOL_SUCCESS) { return RESTOOL_ERROR; } return RESTOOL_SUCCESS; diff --git a/src/resource_overlap.cpp b/src/resource_overlap.cpp index 8934794..5a6d726 100644 --- a/src/resource_overlap.cpp +++ b/src/resource_overlap.cpp @@ -43,19 +43,19 @@ uint32_t ResourceOverlap::Pack() } OverlapBinaryFilePacker rawFilePacker(packageParser_, moduleName_); - future copyFuture = rawFilePacker.CopyBinaryFileAsync(resourceMerge.GetInputs()); + rawFilePacker.CopyBinaryFileAsync(resourceMerge.GetInputs()); if (LoadHapResources() != RESTOOL_SUCCESS) { - rawFilePacker.StopCopy(); + rawFilePacker.Terminate(); return RESTOOL_ERROR; } if (PackResources(resourceMerge) != RESTOOL_SUCCESS) { - rawFilePacker.StopCopy(); + rawFilePacker.Terminate(); return RESTOOL_ERROR; } - if (copyFuture.get() != RESTOOL_SUCCESS) { + if (rawFilePacker.GetResult() != RESTOOL_SUCCESS) { return RESTOOL_ERROR; } return RESTOOL_SUCCESS; diff --git a/src/resource_pack.cpp b/src/resource_pack.cpp index e294db5..b399196 100644 --- a/src/resource_pack.cpp +++ b/src/resource_pack.cpp @@ -316,14 +316,14 @@ uint32_t ResourcePack::Pack() } BinaryFilePacker rawFilePacker(packageParser_, moduleName_); - std::future copyFuture = rawFilePacker.CopyBinaryFileAsync(resourceMerge.GetInputs()); + rawFilePacker.CopyBinaryFileAsync(resourceMerge.GetInputs()); if (PackResources(resourceMerge) != RESTOOL_SUCCESS) { - rawFilePacker.StopCopy(); + rawFilePacker.Terminate(); return RESTOOL_ERROR; } - if (copyFuture.get() != RESTOOL_SUCCESS) { + if (rawFilePacker.GetResult() != RESTOOL_SUCCESS) { return RESTOOL_ERROR; } return RESTOOL_SUCCESS; -- Gitee