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