• 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_t fsSyncReq;
106         uv_fs_cb cb;
107         vector<string> taskQueue;  // save file list if directory send mode
108         TransferConfig transferConfig;  // Used for network IO configuration initialization
109         FileMode fileMode;
110         vector<FileMode> dirMode; // save dir mode on master
111         map<string, FileMode> dirModeMap; // save dir mode on slave
112         ssize_t openFd;
113         bool sandboxMode;
114         string bundleName;
115         string inputLocalPath;
116         bool isOtherSideSandboxSupported;
117     };
118     // Just app-mode use
119     enum AppModType {
120         APPMOD_NONE,
121         APPMOD_INSTALL,
122         APPMOD_UNINSTALL,
123         APPMOD_SIDELOAD,
124     };
125 
126     static void OnFileOpen(uv_fs_t *req);
127     static void OnFileOpenFailed(CtxFile *context);
128     static void OnFileClose(CtxFile *context);
129     bool CheckSandboxOptionCompatibility(const string &option, CtxFile *context);
130     int GetSubFiles(const char *path, string filter, vector<string> *out);
131     int GetSubFilesRecursively(string path, string currentDirname, vector<string> *out);
CheckMaster(CtxFile * context)132     virtual void CheckMaster(CtxFile *context)
133     {
134     }
WhenTransferFinish(CtxFile * context)135     virtual void WhenTransferFinish(CtxFile *context)
136     {
137     }
138     bool MatchPackageExtendName(string fileName, string extName);
139     bool ResetCtx(CtxFile *context, bool full = false);
140     bool SmartSlavePath(string &cwd, string &localPath, const char *optName);
141     bool CheckLocalPath(string &localPath, string &optName, string &errStr);
142     bool CheckFilename(string &localPath, string &optName, string &errStr);
143     void SetFileTime(CtxFile *context);
144     void ExtractRelativePath(string &cwd, string &path);
145     bool AddFeatures(FeatureFlagsUnion &feature);
146     bool CheckFeatures(CtxFile *context, uint8_t *payload, const int payloadSize);
CloseCtxFd(CtxFile * context)147     static void CloseCtxFd(CtxFile *context)
148     {
149         if (context == nullptr || !context->isFdOpen) {
150             return;
151         }
152         WRITE_LOG(LOG_DEBUG, "CloseCtxFd, localPath:%s result:%d, closeReqSubmitted:%d",
153             Hdc::MaskString(context->localPath).c_str(), context->openFd, context->closeReqSubmitted);
154         uv_fs_t fs;
155         uv_fs_close(nullptr, &fs, context->openFd, nullptr);
156         uv_fs_req_cleanup(&fs);
157         // solve the fd leak caused by early exit due to illegal operation on a directory.
158         context->isFdOpen = false;
159     }
160     void RemoveSandboxRootPath(std::string &srcStr, const std::string &bundleName);
161     bool IsValidBundlePath(const string &bundleName);
162 
163     CtxFile ctxNow;
164     uint16_t commandBegin;
165     uint16_t commandData;
166     bool isStableBuf;
167     const string CMD_OPTION_CLIENTCWD = "-cwd";
168     const string SANDBOX_ROOT_DIR = "/mnt/debug/100/debug_hap/";
169 #ifndef CONFIG_USE_JEMALLOC_DFX_INIF
170     CircleBuffer cirbuf;
171 #endif
172 
173 private:
174     // dynamic IO context
175     struct CtxFileIO {
176         uv_fs_t fs;
177         uint8_t *bufIO;
178         CtxFile *context;
179         uint64_t bytes;
180     };
181     static const uint8_t payloadPrefixReserve = 64;
182     static void OnFileIO(uv_fs_t *req);
183     static bool IODelayed(uv_fs_t *req);
184     static bool ProcressFileIO(uv_fs_t *req, CtxFile *context, HdcTransferBase *thisClass, uint64_t bytes);
185     static bool ProcressFileIORead(uv_fs_t *req, CtxFile *context, HdcTransferBase *thisClass);
186     static bool ProcressFileIOWrite(uv_fs_t *req, CtxFile *context, HdcTransferBase *thisClass);
187     static void ProcressFileIOFinish(uv_fs_t *req, CtxFile *context, HdcTransferBase *thisClass);
188     static bool ProcressFileIOIsSuccess(uv_fs_t *req, CtxFile *context, uint64_t bytes);
189     int SimpleFileIO(CtxFile *context, uint64_t index, uint8_t *sendBuf, int bytes);
190     bool SendIOPayload(CtxFile *context, uint64_t index, uint8_t *data, int dataSize);
191     bool RecvIOPayload(CtxFile *context, uint8_t *data, int dataSize);
192     bool InitTransferPayload(TransferPayload &payloadHead, uint64_t index, uint8_t compressType,
193         uint32_t dataSize);
194     double maxTransferBufFactor = 0.8;  // Make the data sent by each IO in one hdc packet
195 };
196 }  // namespace Hdc
197 
198 #endif
199