1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef USCRIPT_TRANSFERLIST_H 16 #define USCRIPT_TRANSFERLIST_H 17 18 #include <functional> 19 #include <string> 20 #include <unordered_map> 21 #include <vector> 22 #include "applypatch/command.h" 23 #include "command.h" 24 #include "script_instruction.h" 25 #include "script_manager.h" 26 27 28 namespace Updater { 29 static std::unordered_map<std::string, BlockSet> blocksetMap; 30 31 struct WriterThreadInfo { 32 pthread_mutex_t mutex; 33 pthread_cond_t cond; 34 BlockSet bs; 35 std::unique_ptr<BlockWriter> writer; 36 bool readyToWrite; 37 Uscript::UScriptEnv *env; 38 Hpackage::PkgManager::FileInfoPtr fileInfo; 39 std::string newPatch; 40 }; 41 42 struct TransferParams { 43 size_t version; 44 size_t blockCount; 45 size_t maxEntries; 46 size_t maxBlocks; 47 size_t written; 48 pthread_t thread; 49 Uscript::UScriptEnv *env; 50 std::unique_ptr<WriterThreadInfo> writerThreadInfo; 51 int storeCreated; 52 std::string storeBase; 53 std::string retryFile; 54 uint8_t *patchDataBuffer; 55 size_t patchDataSize; 56 }; 57 using GlobalParams = TransferParams; 58 class TransferManager; 59 using TransferManagerPtr = TransferManager *; 60 class TransferManager { 61 public: TransferManager()62 TransferManager() {} 63 static TransferManagerPtr GetTransferManagerInstance(); 64 static void ReleaseTransferManagerInstance(TransferManagerPtr transferManager); 65 virtual ~TransferManager(); 66 67 void Init(); 68 bool CommandsParser(int fd, const std::vector<std::string> &context); 69 GetGlobalParams()70 GlobalParams* GetGlobalParams() 71 { 72 return globalParams.get(); 73 } 74 std::string ReloadForRetry() const; 75 bool CheckResult(const CommandResult result, const std::string &cmd, const CommandType &type); 76 bool NeedSetProgress(const CommandType &type); 77 78 private: 79 bool RegisterForRetry(const std::string &cmd); 80 std::unique_ptr<GlobalParams> globalParams; 81 }; 82 } // namespace Updater 83 #endif 84