• 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     struct FileMode {
40         uint64_t perm;
41         uint64_t u_id;
42         uint64_t g_id;
43         string context;
44         string fullName;
45     };
46     // used for HdcTransferBase. just base class use, not public
47     struct TransferPayload {
48         uint64_t index;
49         uint8_t compressType;
50         uint32_t compressSize;
51         uint32_t uncompressSize;
52     };
53     HdcTransferBase(HTaskInfo hTaskInfo);
54     virtual ~HdcTransferBase();
StopTask()55     virtual void StopTask()
56     {
57     }
58     bool CommandDispatch(const uint16_t command, uint8_t *payload, const int payloadSize);
59 
60 protected:
61     // Static file context
62     struct CtxFile {  // The structure cannot be initialized by MEMSET, will rename to CtxTransfer
63         uint64_t fileSize;
64         uint64_t dirSize;
65         uint64_t indexIO; // Id or written IO bytes
66         uint32_t fileCnt; // add for directory mode
67         bool isDir;       // add for directory mode
68         bool targetDirNotExist;
69         uint64_t transferBegin;
70         uint64_t transferDirBegin;
71         string localName;
72         string localPath;
73         string remotePath;
74         string localDirName;
75         bool fileModeSync;
76         bool master;  // Document transmission initiative
77         bool closeNotify;
78         bool ioFinish;
79         bool closeReqSubmitted;
80         void *thisClass;
81         uint32_t lastErrno;
82         uv_loop_t *loop;
83         uv_fs_t fsOpenReq;
84         uv_fs_t fsCloseReq;
85         uv_fs_cb cb;
86         vector<string> taskQueue;  // save file list if directory send mode
87         TransferConfig transferConfig;  // Used for network IO configuration initialization
88         FileMode fileMode;
89         vector<FileMode> dirMode; // save dir mode on master
90         map<string, FileMode> dirModeMap; // save dir mode on slave
91     };
92     // Just app-mode use
93     enum AppModType {
94         APPMOD_NONE,
95         APPMOD_INSTALL,
96         APPMOD_UNINSTALL,
97         APPMOD_SIDELOAD,
98     };
99 
100     static void OnFileOpen(uv_fs_t *req);
101     static void OnFileClose(uv_fs_t *req);
102     int GetSubFiles(const char *path, string filter, vector<string> *out);
103     int GetSubFilesRecursively(string path, string currentDirname, vector<string> *out);
CheckMaster(CtxFile * context)104     virtual void CheckMaster(CtxFile *context)
105     {
106     }
WhenTransferFinish(CtxFile * context)107     virtual void WhenTransferFinish(CtxFile *context)
108     {
109     }
110     bool MatchPackageExtendName(string fileName, string extName);
111     bool ResetCtx(CtxFile *context, bool full = false);
112     bool SmartSlavePath(string &cwd, string &localPath, const char *optName);
113     bool CheckLocalPath(string &localPath, string &optName, string &errStr);
114     bool CheckFilename(string &localPath, string &optName, string &errStr);
115     void SetFileTime(CtxFile *context);
116     void ExtractRelativePath(string &cwd, string &path);
117 
118     CtxFile ctxNow;
119     uint16_t commandBegin;
120     uint16_t commandData;
121     const string CMD_OPTION_CLIENTCWD = "-cwd";
122     CircleBuffer cirbuf;
123 
124 private:
125     // dynamic IO context
126     struct CtxFileIO {
127         uv_fs_t fs;
128         uint8_t *bufIO;
129         CtxFile *context;
130     };
131     static const uint8_t payloadPrefixReserve = 64;
132     static void OnFileIO(uv_fs_t *req);
133     int SimpleFileIO(CtxFile *context, uint64_t index, uint8_t *sendBuf, int bytes);
134     bool SendIOPayload(CtxFile *context, uint64_t index, uint8_t *data, int dataSize);
135     bool RecvIOPayload(CtxFile *context, uint8_t *data, int dataSize);
136     double maxTransferBufFactor = 0.8;  // Make the data sent by each IO in one hdc packet
137 };
138 }  // namespace Hdc
139 
140 #endif
141