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_const.h" 23 #include "applypatch/command.h" 24 #include "script_instruction.h" 25 #include "script_manager.h" 26 27 28 namespace Updater { 29 30 struct WriterThreadInfo { 31 pthread_mutex_t mutex; 32 pthread_cond_t cond; 33 BlockSet bs; 34 std::unique_ptr<BlockWriter> writer; 35 bool readyToWrite; 36 Uscript::UScriptEnv *env; 37 Hpackage::PkgManager::FileInfoPtr fileInfo; 38 std::string newPatch; 39 }; 40 41 struct TransferParams { 42 size_t version; 43 size_t blockCount; 44 size_t maxEntries; 45 size_t maxBlocks; 46 size_t written; 47 pthread_t thread; 48 Uscript::UScriptEnv *env; 49 std::unique_ptr<WriterThreadInfo> writerThreadInfo; 50 int storeCreated; 51 std::string storeBase; 52 std::string freeStash; 53 std::string retryFile; 54 std::string devPath; 55 std::string patchDatFile; 56 uint8_t *dataBuffer; 57 size_t dataBufferSize; 58 bool canWrite; 59 bool isUpdaterMode; 60 }; 61 62 class TransferManager; 63 using TransferManagerPtr = TransferManager *; 64 class TransferManager { 65 public: 66 TransferManager(); ~TransferManager()67 virtual ~TransferManager() {}; 68 69 bool CommandsParser(int fd, const std::vector<std::string> &context); 70 GetTransferParams()71 TransferParams* GetTransferParams() 72 { 73 return transferParams_.get(); 74 } 75 std::string ReloadForRetry() const; 76 bool CheckResult(const CommandResult result, const std::string &cmd, const CommandType &type); 77 78 private: 79 void UpdateProgress(size_t &initBlock, size_t totalSize); 80 bool RegisterForRetry(const std::string &cmd); 81 bool CommandsExecute(int fd, Command &cmd); 82 bool CommandParserPreCheck(const std::vector<std::string> &context); 83 std::vector<std::string>::const_iterator InitCommandParser(std::vector<std::string>::const_iterator ct, 84 std::string &retryCmd); 85 std::unique_ptr<TransferParams> transferParams_; 86 }; 87 } // namespace Updater 88 #endif 89