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 73 protected: 74 // Static file context 75 struct CtxFile { // The structure cannot be initialized by MEMSET, will rename to CtxTransfer 76 uint64_t fileSize; 77 uint64_t dirSize; 78 uint64_t indexIO; // Id or written IO bytes 79 uint32_t fileCnt; // add for directory mode 80 bool isDir; // add for directory mode 81 bool targetDirNotExist; 82 uint64_t transferBegin; 83 uint64_t transferDirBegin; 84 string localName; 85 string localPath; 86 string remotePath; 87 string localDirName; 88 bool fileModeSync; 89 bool master; // Document transmission initiative 90 bool closeNotify; 91 bool ioFinish; 92 bool closeReqSubmitted; 93 bool isStableBufSize; // USB IO buffer size set stable value, false: 512K, true: 61K 94 bool isFdOpen; 95 void *thisClass; 96 uint32_t lastErrno; 97 uv_loop_t *loop; 98 uv_fs_t fsOpenReq; 99 uv_fs_t fsCloseReq; 100 uv_fs_cb cb; 101 vector<string> taskQueue; // save file list if directory send mode 102 TransferConfig transferConfig; // Used for network IO configuration initialization 103 FileMode fileMode; 104 vector<FileMode> dirMode; // save dir mode on master 105 map<string, FileMode> dirModeMap; // save dir mode on slave 106 }; 107 // Just app-mode use 108 enum AppModType { 109 APPMOD_NONE, 110 APPMOD_INSTALL, 111 APPMOD_UNINSTALL, 112 APPMOD_SIDELOAD, 113 }; 114 115 static void OnFileOpen(uv_fs_t *req); 116 static void OnFileClose(uv_fs_t *req); 117 int GetSubFiles(const char *path, string filter, vector<string> *out); 118 int GetSubFilesRecursively(string path, string currentDirname, vector<string> *out); CheckMaster(CtxFile * context)119 virtual void CheckMaster(CtxFile *context) 120 { 121 } WhenTransferFinish(CtxFile * context)122 virtual void WhenTransferFinish(CtxFile *context) 123 { 124 } 125 bool MatchPackageExtendName(string fileName, string extName); 126 bool ResetCtx(CtxFile *context, bool full = false); 127 bool SmartSlavePath(string &cwd, string &localPath, const char *optName); 128 bool CheckLocalPath(string &localPath, string &optName, string &errStr); 129 bool CheckFilename(string &localPath, string &optName, string &errStr); 130 void SetFileTime(CtxFile *context); 131 void ExtractRelativePath(string &cwd, string &path); 132 bool AddFeatures(FeatureFlagsUnion &feature); 133 bool CheckFeatures(CtxFile *context, uint8_t *payload, const int payloadSize); 134 135 CtxFile ctxNow; 136 uint16_t commandBegin; 137 uint16_t commandData; 138 bool isStableBuf; 139 const string CMD_OPTION_CLIENTCWD = "-cwd"; 140 #ifndef CONFIG_USE_JEMALLOC_DFX_INIF 141 CircleBuffer cirbuf; 142 #endif 143 144 private: 145 // dynamic IO context 146 struct CtxFileIO { 147 uv_fs_t fs; 148 uint8_t *bufIO; 149 CtxFile *context; 150 }; 151 static const uint8_t payloadPrefixReserve = 64; 152 static void OnFileIO(uv_fs_t *req); 153 int SimpleFileIO(CtxFile *context, uint64_t index, uint8_t *sendBuf, int bytes); 154 bool SendIOPayload(CtxFile *context, uint64_t index, uint8_t *data, int dataSize); 155 bool RecvIOPayload(CtxFile *context, uint8_t *data, int dataSize); 156 double maxTransferBufFactor = 0.8; // Make the data sent by each IO in one hdc packet 157 }; 158 } // namespace Hdc 159 160 #endif 161