• 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 uId;
42         uint64_t gId;
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     union FeatureFlagsUnion {
54         struct {
55             uint8_t hugeBuf : 1; // bit 1: enable huge buffer 512K
56             uint8_t compressLz4 : 1; // bit 2: enable compress default is lz4
57             uint8_t reserveBits1 : 6; // bit 3-8: reserved
58             uint8_t reserveBits2 : 8; // bit 9-16: reserved
59             uint16_t reserveBits3 : 16; // bit 17-32: reserved
60             uint32_t reserveBits4 : 32; // bit 33-64: reserved
61         } bits;
62         uint8_t raw[FEATURE_FLAG_MAX_SIZE];
63     };
64     // FEATURE_FLAG_MAX_SIZE * 8 bits = 8 * 8 bits = 64 bits
65     // is used for HdcTransferBase. just base class use, not public
66     HdcTransferBase(HTaskInfo hTaskInfo);
67     virtual ~HdcTransferBase();
StopTask()68     virtual void StopTask()
69     {
70     }
71     bool CommandDispatch(const uint16_t command, uint8_t *payload, const int payloadSize);
72     const string cmdOptionTstmp = "-a";
73     const string cmdOptionSync = "-sync";
74     const string cmdOptionZip = "-z";
75     const string cmdOptionModeSync = "-m";
76     const string cmdBundleName = "-b";
77 
78 protected:
79     // Static file context
80     struct CtxFile {  // The structure cannot be initialized by MEMSET, will rename to CtxTransfer
81         uint64_t fileSize;
82         uint64_t dirSize;
83         uint64_t indexIO; // Id or written IO bytes
84         uint32_t fileCnt; // add for directory mode
85         bool isDir;       // add for directory mode
86         bool targetDirNotExist;
87         uint64_t transferBegin;
88         uint64_t transferDirBegin;
89         string localName;
90         string localPath;
91         string remotePath;
92         string localDirName;
93         bool fileModeSync;
94         bool master;  // Document transmission initiative
95         bool closeNotify;
96         bool ioFinish;
97         bool closeReqSubmitted;
98         bool isStableBufSize; // USB IO buffer size set stable value, false: 512K, true: 61K
99         bool isFdOpen;
100         void *thisClass;
101         uint32_t lastErrno;
102         uv_loop_t *loop;
103         uv_fs_t fsOpenReq;
104         uv_fs_t fsCloseReq;
105         uv_fs_cb cb;
106         vector<string> taskQueue;  // save file list if directory send mode
107         TransferConfig transferConfig;  // Used for network IO configuration initialization
108         FileMode fileMode;
109         vector<FileMode> dirMode; // save dir mode on master
110         map<string, FileMode> dirModeMap; // save dir mode on slave
111         bool sandboxMode;
112         string bundleName;
113         string inputLocalPath;
114         bool isOtherSideSandboxSupported;
115     };
116     // Just app-mode use
117     enum AppModType {
118         APPMOD_NONE,
119         APPMOD_INSTALL,
120         APPMOD_UNINSTALL,
121         APPMOD_SIDELOAD,
122     };
123 
124     static void OnFileOpen(uv_fs_t *req);
125     static void OnFileOpenFailed(CtxFile *context);
126     static void OnFileClose(uv_fs_t *req);
127     bool CheckSandboxOptionCompatibility(const string &option, CtxFile *context);
128     int GetSubFiles(const char *path, string filter, vector<string> *out);
129     int GetSubFilesRecursively(string path, string currentDirname, vector<string> *out);
CheckMaster(CtxFile * context)130     virtual void CheckMaster(CtxFile *context)
131     {
132     }
WhenTransferFinish(CtxFile * context)133     virtual void WhenTransferFinish(CtxFile *context)
134     {
135     }
136     bool MatchPackageExtendName(string fileName, string extName);
137     bool ResetCtx(CtxFile *context, bool full = false);
138     bool SmartSlavePath(string &cwd, string &localPath, const char *optName);
139     bool CheckLocalPath(string &localPath, string &optName, string &errStr);
140     bool CheckFilename(string &localPath, string &optName, string &errStr);
141     void SetFileTime(CtxFile *context);
142     void ExtractRelativePath(string &cwd, string &path);
143     bool AddFeatures(FeatureFlagsUnion &feature);
144     bool CheckFeatures(CtxFile *context, uint8_t *payload, const int payloadSize);
145     void RemoveSandboxRootPath(std::string &srcStr, const std::string &bundleName);
146     bool IsValidBundlePath(const string &bundleName);
147 
148     CtxFile ctxNow;
149     uint16_t commandBegin;
150     uint16_t commandData;
151     bool isStableBuf;
152     const string CMD_OPTION_CLIENTCWD = "-cwd";
153     const string SANDBOX_ROOT_DIR = "/mnt/debug/100/debug_hap/";
154 #ifndef CONFIG_USE_JEMALLOC_DFX_INIF
155     CircleBuffer cirbuf;
156 #endif
157 
158 private:
159     // dynamic IO context
160     struct CtxFileIO {
161         uv_fs_t fs;
162         uint8_t *bufIO;
163         CtxFile *context;
164     };
165     static const uint8_t payloadPrefixReserve = 64;
166     static void OnFileIO(uv_fs_t *req);
167     int SimpleFileIO(CtxFile *context, uint64_t index, uint8_t *sendBuf, int bytes);
168     bool SendIOPayload(CtxFile *context, uint64_t index, uint8_t *data, int dataSize);
169     bool RecvIOPayload(CtxFile *context, uint8_t *data, int dataSize);
170     double maxTransferBufFactor = 0.8;  // Make the data sent by each IO in one hdc packet
171 };
172 }  // namespace Hdc
173 
174 #endif
175