diff --git a/include/binary_file_packer.h b/include/binary_file_packer.h index bfc64c4..b01f9ee 100644 --- a/include/binary_file_packer.h +++ b/include/binary_file_packer.h @@ -28,6 +28,7 @@ public: explicit BinaryFilePacker(const PackageParser &packageParser, const std::string &moduleName); ~BinaryFilePacker(); std::future CopyBinaryFileAsync(const std::vector &inputs); + void StopCopy(); private: uint32_t CopyBinaryFile(const std::vector &inputs); @@ -38,6 +39,7 @@ private: std::string moduleName_; ThreadPool threadPool_; std::vector> copyResults_; + std::atomic stopCopy_{false}; }; } // namespace Restool } // namespace Global diff --git a/include/resource_pack.h b/include/resource_pack.h index 46bd366..9f566fe 100644 --- a/include/resource_pack.h +++ b/include/resource_pack.h @@ -18,10 +18,11 @@ #include "cmd_parser.h" #include "config_parser.h" -#include "resource_util.h" -#include "resource_item.h" -#include "resource_data.h" #include "resource_append.h" +#include "resource_data.h" +#include "resource_item.h" +#include "resource_merge.h" +#include "resource_util.h" namespace OHOS { namespace Global { @@ -49,6 +50,7 @@ private: uint32_t PackPreview(); uint32_t PackAppend(); uint32_t PackCombine(); + uint32_t PackQualifierResources(const ResourceMerge &resourceMerge); uint32_t HandleFeature(); uint32_t FindResourceItems(const std::map> &resInfoLocal, std::vector &items, int64_t id) const; diff --git a/include/thread_pool.h b/include/thread_pool.h index 3c5c7dc..0efc3df 100644 --- a/include/thread_pool.h +++ b/include/thread_pool.h @@ -74,11 +74,6 @@ std::future::type> ThreadPool::Enqueue(F &&f auto task = std::make_shared(std::bind(std::forward(f), std::forward(args)...)); std::future res = task->get_future(); - if (!running_ || workerThreads_.empty()) { - // If the pool is not running or there are no worker threads, execute the task immediately - (*task)(); - return res; - } { std::unique_lock lock(queueMutex_); tasks_.emplace([task]() { (*task)(); }); diff --git a/src/binary_file_packer.cpp b/src/binary_file_packer.cpp index d25d76d..75cf11b 100644 --- a/src/binary_file_packer.cpp +++ b/src/binary_file_packer.cpp @@ -34,6 +34,11 @@ BinaryFilePacker::~BinaryFilePacker() threadPool_.Stop(); } +void BinaryFilePacker::StopCopy() +{ + stopCopy_.store(true); +} + std::future BinaryFilePacker::CopyBinaryFileAsync(const std::vector &inputs) { auto func = [this](const std::vector &inputs) { return this->CopyBinaryFile(inputs); }; @@ -56,6 +61,10 @@ uint32_t BinaryFilePacker::CopyBinaryFile(const vector &inputs) } } for (auto &res : copyResults_) { + if (stopCopy_.load()) { + cout << "Info: CopyBinaryFile: stop copy binary file." << endl; + return RESTOOL_ERROR; + } uint32_t ret = res.get(); if (ret != RESTOOL_SUCCESS) { return RESTOOL_ERROR; @@ -111,6 +120,10 @@ uint32_t BinaryFilePacker::CopyBinaryFileImpl(const string &src, const string &d cerr << "Warning: '" << entry->GetFilePath().GetPath() << "' is defined repeatedly." << endl; continue; } + if (stopCopy_.load()) { + cout << "Info: CopyBinaryFileImpl: stop copy binary file." << endl; + return RESTOOL_ERROR; + } string path = entry->GetFilePath().GetPath(); auto copyFunc = [this](const string path, string subPath) { return this->CopySingleFile(path, subPath); }; std::future res = threadPool_.Enqueue(copyFunc, path, subPath); diff --git a/src/resource_pack.cpp b/src/resource_pack.cpp index 1d76700..7a45297 100644 --- a/src/resource_pack.cpp +++ b/src/resource_pack.cpp @@ -295,7 +295,16 @@ uint32_t ResourcePack::PackNormal() BinaryFilePacker rawFilePacker(packageParser_, moduleName_); std::future copyFuture = rawFilePacker.CopyBinaryFileAsync(resourceMerge.GetInputs()); + uint32_t packQualifierRet = PackQualifierResources(resourceMerge); + if (packQualifierRet != RESTOOL_SUCCESS) { + rawFilePacker.StopCopy(); + } + uint32_t copyRet = copyFuture.get(); + return packQualifierRet == RESTOOL_SUCCESS && copyRet == RESTOOL_SUCCESS ? RESTOOL_SUCCESS : RESTOOL_ERROR; +} +uint32_t ResourcePack::PackQualifierResources(const ResourceMerge &resourceMerge) +{ if (ScanResources(resourceMerge.GetInputs(), packageParser_.GetOutput()) != RESTOOL_SUCCESS) { return RESTOOL_ERROR; } @@ -329,8 +338,7 @@ uint32_t ResourcePack::PackNormal() if (resourceTable.CreateResourceTable() != RESTOOL_SUCCESS) { return RESTOOL_ERROR; } - uint32_t copyRet = copyFuture.get(); - return copyRet == RESTOOL_SUCCESS ? RESTOOL_SUCCESS : RESTOOL_ERROR; + return RESTOOL_SUCCESS; } uint32_t ResourcePack::HandleFeature() diff --git a/src/thread_pool.cpp b/src/thread_pool.cpp index eef88db..9cd88b2 100644 --- a/src/thread_pool.cpp +++ b/src/thread_pool.cpp @@ -49,7 +49,11 @@ void ThreadPool::Stop() running_ = false; } condition_.notify_all(); - for (std::thread &worker : workerThreads_) { worker.join(); } + for (std::thread &worker : workerThreads_) { + if (worker.joinable()) { + worker.join(); + } + } cout << "Info: thread pool is stopped" << endl; }