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