diff --git a/include/binary_file_packer.h b/include/binary_file_packer.h index 00c45ed893a94ac8bfed134e0a0a1038e72d37de..f1fb6a33a58910a4eb1f55528d428a8b767c8f67 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 9e291a01b09ed17899a1234900a7b10593223d6f..120b921f92310220f7229c44f91c9a0be4bbf862 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 7dc0bd685f9d47a8cda0b1df24a6af0262a1f4c6..1af9e25e8f3077ff277228aa14e0a9dc2099abfb 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 8934794fb4fdc42e206f5526918ee08b32b35c61..5a6d72651cd360f7d9580553d9f069dc94b3f996 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 e294db53882dd25b689e6586a02f7867dd85e751..b399196b52c287c6cc5c883a6978437dc607d92a 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;