• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 HDC_TRANSFER_H
16 #define HDC_TRANSFER_H
17 #include "common.h"
18 
19 namespace Hdc {
20 class HdcTransferBase : public HdcTaskBase {
21 public:
22     enum CompressType { COMPRESS_NONE, COMPRESS_LZ4, COMPRESS_LZ77, COMPRESS_LZMA, COMPRESS_BROTLI };
23     // used for child class
24     struct TransferConfig {
25         uint64_t fileSize;
26         uint64_t atime;  // ns
27         uint64_t mtime;  // ns
28         string options;
29         string path;
30         string optionalName;
31         bool updateIfNew;
32         uint8_t compressType;
33         bool holdTimestamp;
34         string functionName;
35         string clientCwd;
36         string reserve1;
37         string reserve2;
38     };
39     // used for HdcTransferBase. just base class use, not public
40     struct TransferPayload {
41         uint64_t index;
42         uint8_t compressType;
43         uint32_t compressSize;
44         uint32_t uncompressSize;
45     };
46     HdcTransferBase(HTaskInfo hTaskInfo);
47     virtual ~HdcTransferBase();
StopTask()48     virtual void StopTask()
49     {
50     }
51     bool CommandDispatch(const uint16_t command, uint8_t *payload, const int payloadSize);
52 
53 protected:
54     // Static file context
55     struct CtxFile {  // The structure cannot be initialized by MEMSET, will rename to CtxTransfer
56         uint64_t fileSize;
57         uint64_t indexIO;  // Id or written IO bytes
58         uint64_t transferBegin;
59         string localName;
60         string localPath;
61         string remotePath;
62         bool master;  // Document transmission initiative
63         bool closeNotify;
64         bool ioFinish;
65         void *thisClass;
66         uint32_t lastErrno;
67 
68         uv_loop_t *loop;
69         uv_fs_t fsOpenReq;
70         uv_fs_t fsCloseReq;
71         uv_fs_cb cb;
72         vector<string> taskQueue;
73         TransferConfig transferConfig;  // Used for network IO configuration initialization
74     };
75     // Just app-mode use
76     enum AppModType {
77         APPMOD_NONE,
78         APPMOD_INSTALL,
79         APPMOD_UNINSTALL,
80         APPMOD_SIDELOAD,
81     };
82 
83     static void OnFileOpen(uv_fs_t *req);
84     static void OnFileClose(uv_fs_t *req);
85     int GetSubFiles(const char *path, string filter, vector<string> *out);
CheckMaster(CtxFile * context)86     virtual void CheckMaster(CtxFile *context)
87     {
88     }
WhenTransferFinish(CtxFile * context)89     virtual void WhenTransferFinish(CtxFile *context)
90     {
91     }
92     bool MatchPackageExtendName(string fileName, string extName);
93     bool ResetCtx(CtxFile *context, bool full = false);
94     bool SmartSlavePath(string &cwd, string &localPath, const char *optName);
95     void SetFileTime(CtxFile *context);
96     void ExtractRelativePath(string &cwd, string &path);
97 
98     CtxFile ctxNow;
99     uint16_t commandBegin;
100     uint16_t commandData;
101     const string CMD_OPTION_CLIENTCWD = "-cwd";
102 
103 private:
104     // dynamic IO context
105     struct CtxFileIO {
106         uv_fs_t fs;
107         uint8_t *bufIO;
108         CtxFile *context;
109     };
110     const uint8_t payloadPrefixReserve = 64;
111     static void OnFileIO(uv_fs_t *req);
112     int SimpleFileIO(CtxFile *context, uint64_t index, uint8_t *sendBuf, int bytes);
113     bool SendIOPayload(CtxFile *context, int index, uint8_t *data, int dataSize);
114     bool RecvIOPayload(CtxFile *context, uint8_t *data, int dataSize);
115     double maxTransferBufFactor = 0.8;  // Make the data sent by each IO in one hdc packet
116 };
117 }  // namespace Hdc
118 
119 #endif
120