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 freeStash; 54 std::string retryFile; 55 uint8_t *patchDataBuffer; 56 size_t patchDataSize; 57 }; 58 using GlobalParams = TransferParams; 59 class TransferManager; 60 using TransferManagerPtr = TransferManager *; 61 class TransferManager { 62 public: TransferManager()63 TransferManager() {} 64 static TransferManagerPtr GetTransferManagerInstance(); 65 static void ReleaseTransferManagerInstance(TransferManagerPtr transferManager); 66 virtual ~TransferManager(); 67 68 void Init(); 69 bool CommandsParser(int fd, const std::vector<std::string> &context); 70 GetGlobalParams()71 GlobalParams* GetGlobalParams() 72 { 73 return globalParams.get(); 74 } 75 std::string ReloadForRetry() const; 76 bool CheckResult(const CommandResult result, const std::string &cmd, const CommandType &type); 77 bool NeedSetProgress(const CommandType &type); 78 79 private: 80 bool RegisterForRetry(const std::string &cmd); 81 bool CommandsExecute(int fd, Command &cmd); 82 std::unique_ptr<GlobalParams> globalParams; 83 }; 84 } // namespace Updater 85 #endif 86