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 void *thisClass; 80 uint32_t lastErrno; 81 uv_loop_t *loop; 82 uv_fs_t fsOpenReq; 83 uv_fs_t fsCloseReq; 84 uv_fs_cb cb; 85 vector<string> taskQueue; // save file list if directory send mode 86 TransferConfig transferConfig; // Used for network IO configuration initialization 87 FileMode fileMode; 88 vector<FileMode> dirMode; // save dir mode on master 89 map<string, FileMode> dirModeMap; // save dir mode on slave 90 }; 91 // Just app-mode use 92 enum AppModType { 93 APPMOD_NONE, 94 APPMOD_INSTALL, 95 APPMOD_UNINSTALL, 96 APPMOD_SIDELOAD, 97 }; 98 99 static void OnFileOpen(uv_fs_t *req); 100 static void OnFileClose(uv_fs_t *req); 101 int GetSubFiles(const char *path, string filter, vector<string> *out); 102 int GetSubFilesRecursively(string path, string currentDirname, vector<string> *out); CheckMaster(CtxFile * context)103 virtual void CheckMaster(CtxFile *context) 104 { 105 } WhenTransferFinish(CtxFile * context)106 virtual void WhenTransferFinish(CtxFile *context) 107 { 108 } 109 bool MatchPackageExtendName(string fileName, string extName); 110 bool ResetCtx(CtxFile *context, bool full = false); 111 bool SmartSlavePath(string &cwd, string &localPath, const char *optName); 112 bool CheckLocalPath(string &localPath, string &optName, string &errStr); 113 bool CheckFilename(string &localPath, string &optName, string &errStr); 114 void SetFileTime(CtxFile *context); 115 void ExtractRelativePath(string &cwd, string &path); 116 117 CtxFile ctxNow; 118 uint16_t commandBegin; 119 uint16_t commandData; 120 const string CMD_OPTION_CLIENTCWD = "-cwd"; 121 122 private: 123 // dynamic IO context 124 struct CtxFileIO { 125 uv_fs_t fs; 126 uint8_t *bufIO; 127 CtxFile *context; 128 }; 129 static const uint8_t payloadPrefixReserve = 64; 130 static void OnFileIO(uv_fs_t *req); 131 int SimpleFileIO(CtxFile *context, uint64_t index, uint8_t *sendBuf, int bytes); 132 bool SendIOPayload(CtxFile *context, uint64_t index, uint8_t *data, int dataSize); 133 bool RecvIOPayload(CtxFile *context, uint8_t *data, int dataSize); 134 double maxTransferBufFactor = 0.8; // Make the data sent by each IO in one hdc packet 135 }; 136 } // namespace Hdc 137 138 #endif 139