diff --git a/src/common/transfer.cpp b/src/common/transfer.cpp index d9bb1b1f8c7d548e27e4f9471162e5764b6bdbc9..568af4909e966777495218cf04e78446cdc654a2 100644 --- a/src/common/transfer.cpp +++ b/src/common/transfer.cpp @@ -68,42 +68,73 @@ bool HdcTransferBase::ResetCtx(CtxFile *context, bool full) return true; } -int HdcTransferBase::SimpleFileIO(CtxFile *context, uint64_t index, uint8_t *sendBuf, int bytes) +uint8_t *HdcTransferBase::MallocBuf(int bytes) { - StartTraceScope("HdcTransferBase::SimpleFileIO"); - // The first 8 bytes file offset #ifndef CONFIG_USE_JEMALLOC_DFX_INIF uint8_t *buf = cirbuf.Malloc(); #else uint8_t *buf = new uint8_t[bytes + payloadPrefixReserve](); #endif + return buf; +} + +void HdcTransferBase::FreeBuf(uint8_t *buf) +{ +#ifndef CONFIG_USE_JEMALLOC_DFX_INIF + cirbuf.Free(buf); +#else + delete[] buf; +#endif +} + +bool HdcTransferBase::MemcpyBufIO(uint8_t *bufIO, size_t bufMaxSize, uint8_t *sendBuf, int bytes) +{ +#ifndef CONFIG_USE_JEMALLOC_DFX_INIF + if (bytes > 0 && memcpy_s(bufIO, bufMaxSize, sendBuf, bytes) != EOK) { +#else + if (bytes > 0 && memcpy_s(bufIO, bytes, sendBuf, bytes) != EOK) { +#endif + WRITE_LOG(LOG_WARN, "memcpy error cid:%u sid:%u", taskInfo->channelId, taskInfo->sessionId); + return false; + } + return true; +} + +bool HdcTransferBase::CheckBytes(bool isStable, int bytes, size_t &bufMaxSize) +{ + // The first 8 bytes file offset + bufMaxSize = isStable ? + static_cast(Base::GetUsbffsBulkSizeStable() - payloadPrefixReserve) : + static_cast(Base::GetUsbffsBulkSize() - payloadPrefixReserve); + if (bytes < 0 || static_cast(bytes) > bufMaxSize) { + WRITE_LOG(LOG_DEBUG, "check failed cid:%u sid:%u", taskInfo->channelId, taskInfo->sessionId); + return false; + } + return true; +} + +int HdcTransferBase::SimpleFileIO(CtxFile *context, uint64_t index, uint8_t *sendBuf, int bytes) +{ + StartTraceScope("HdcTransferBase::SimpleFileIO"); + uint8_t *buf = MallocBuf(bytes); if (buf == nullptr) { - WRITE_LOG(LOG_FATAL, "SimpleFileIO buf nullptr cid:%u sid:%u", taskInfo->channelId, taskInfo->sessionId); + WRITE_LOG(LOG_FATAL, "buf nullptr cid:%u sid:%u", taskInfo->channelId, taskInfo->sessionId); return -1; } CtxFileIO *ioContext = new(std::nothrow) CtxFileIO(); if (ioContext == nullptr) { -#ifndef CONFIG_USE_JEMALLOC_DFX_INIF - cirbuf.Free(buf); -#else - delete[] buf; -#endif - WRITE_LOG(LOG_FATAL, "SimpleFileIO ioContext nullptr cid:%u sid:%u", taskInfo->channelId, taskInfo->sessionId); + FreeBuf(buf); + WRITE_LOG(LOG_FATAL, "ioContext nullptr cid:%u sid:%u", taskInfo->channelId, taskInfo->sessionId); return -1; } bool ret = false; while (true) { - size_t bufMaxSize = context->isStableBufSize ? - static_cast(Base::GetUsbffsBulkSizeStable() - payloadPrefixReserve) : - static_cast(Base::GetUsbffsBulkSize() - payloadPrefixReserve); - if (bytes < 0 || static_cast(bytes) > bufMaxSize) { - WRITE_LOG(LOG_DEBUG, "SimpleFileIO param check failed cid:%u sid:%u", taskInfo->channelId, - taskInfo->sessionId); + size_t bufMaxSize = 0; + if (!CheckBytes(context->isStableBufSize, bytes, bufMaxSize)) { break; } if (context->ioFinish) { - WRITE_LOG(LOG_DEBUG, "SimpleFileIO to closed IOStream cid:%u sid:%u", taskInfo->channelId, - taskInfo->sessionId); + WRITE_LOG(LOG_DEBUG, "closed IOStream cid:%u sid:%u", taskInfo->channelId, taskInfo->sessionId); break; } uv_fs_t *req = &ioContext->fs; @@ -118,9 +149,7 @@ int HdcTransferBase::SimpleFileIO(CtxFile *context, uint64_t index, uint8_t *sen } else { // The US_FS_WRITE here must be brought into the actual file offset, which cannot be incorporated with local // accumulated index because UV_FS_WRITE will be executed multiple times and then trigger a callback. - if (bytes > 0 && memcpy_s(ioContext->bufIO, bufMaxSize, sendBuf, bytes) != EOK) { - WRITE_LOG(LOG_WARN, "SimpleFileIO memcpy error cid:%u sid:%u", taskInfo->channelId, - taskInfo->sessionId); + if (!MemcpyBufIO(ioContext->bufIO, bufMaxSize, sendBuf, bytes)) { break; } uv_buf_t iov = uv_buf_init(reinterpret_cast(ioContext->bufIO), bytes); @@ -136,11 +165,7 @@ int HdcTransferBase::SimpleFileIO(CtxFile *context, uint64_t index, uint8_t *sen WRITE_LOG(LOG_WARN, "SimpleFileIO ret=false, delete context, cid:%u sid:%u", taskInfo->channelId, taskInfo->sessionId); } -#ifndef CONFIG_USE_JEMALLOC_DFX_INIF - cirbuf.Free(buf); -#else - delete[] buf; -#endif + FreeBuf(buf); return -1; } return bytes; @@ -583,7 +608,6 @@ int HdcTransferBase::GetSubFiles(const char *path, string filter, vector return retNum; } - int HdcTransferBase::GetSubFilesRecursively(string path, string currentDirname, vector *out) { int retNum = 0; @@ -641,7 +665,6 @@ int HdcTransferBase::GetSubFilesRecursively(string path, string currentDirname, return retNum; } - bool HdcTransferBase::CheckLocalPath(string &localPath, string &optName, string &errStr) { // If optName show this is directory mode, check localPath and try create each layer diff --git a/src/common/transfer.h b/src/common/transfer.h index 7c54d871577048b73c9394e99903c54ae0d4b91b..95b087e17e3e0e1ec8a5805bf165981fc31fae89 100644 --- a/src/common/transfer.h +++ b/src/common/transfer.h @@ -186,6 +186,10 @@ private: static bool ProcressFileIOWrite(uv_fs_t *req, CtxFile *context, HdcTransferBase *thisClass); static void ProcressFileIOFinish(uv_fs_t *req, CtxFile *context, HdcTransferBase *thisClass); static bool ProcressFileIOIsSuccess(uv_fs_t *req, CtxFile *context, uint64_t bytes); + uint8_t *MallocBuf(int bytes); + void FreeBuf(uint8_t *buf); + bool CheckBytes(bool isStable, int bytes, size_t &bufMaxSize); + bool MemcpyBufIO(uint8_t *bufIO, size_t bufMaxSize, uint8_t *sendBuf, int bytes); int SimpleFileIO(CtxFile *context, uint64_t index, uint8_t *sendBuf, int bytes); bool SendIOPayload(CtxFile *context, uint64_t index, uint8_t *data, int dataSize); bool RecvIOPayload(CtxFile *context, uint8_t *data, int dataSize);