diff --git a/frameworks/js/napi/src/send_file.cpp b/frameworks/js/napi/src/send_file.cpp index 0c5805d0c7d3f812ce3e1a8d353c9176f74dddef..45a855dc79e6c7c64759efe6a25dd5b66b96d92e 100644 --- a/frameworks/js/napi/src/send_file.cpp +++ b/frameworks/js/napi/src/send_file.cpp @@ -59,7 +59,7 @@ int32_t SendFile::RegisterCallback() sptr callback = (std::make_unique()).release(); LOGD("SendFile::RegisterCallback: cb[%{public}p]", callback->AsObject().GetRefPtr()); - if (IDistributedFileService::DFS_NO_ERROR != distributedFileService->RegisterNotifyCallback(callback)) { + if (distributedFileService->RegisterNotifyCallback(callback) != IDistributedFileService::DFS_NO_ERROR) { LOGD("SendFile::RegisterCallback: remote call failed.\n"); return NAPI_SENDFILE_IPC_ERROR; } @@ -68,7 +68,7 @@ int32_t SendFile::RegisterCallback() int32_t SendFile::JoinCidToAppId(const std::string &cid, const std::string &AppId) { - if (cid.empty() | AppId.empty()) { + if (cid.empty() || AppId.empty()) { LOGE("SendFile::JoinCidToAppId: input para error.\n"); return NAPI_SENDFILE_PARA_ERROR; } @@ -89,7 +89,7 @@ int32_t SendFile::JoinCidToAppId(const std::string &cid, const std::string &AppI int32_t SendFile::DisjoinCidToAppId(const std::string &cid, const std::string &AppId) { - if (cid.empty() | AppId.empty()) { + if (cid.empty() || AppId.empty()) { LOGE("SendFile::DisJoinCidToAppId: input para error.\n"); return NAPI_SENDFILE_PARA_ERROR; } @@ -110,7 +110,7 @@ int32_t SendFile::DisjoinCidToAppId(const std::string &cid, const std::string &A int32_t SendFile::EmitTransEvent(TransEvent &event, const std::string &cid, const std::string &AppId) { - if (cid.empty() | AppId.empty()) { + if (cid.empty() || AppId.empty()) { LOGE("SendFile::EmitTransEvent: input para error.\n"); return NAPI_SENDFILE_PARA_ERROR; } @@ -182,7 +182,7 @@ int32_t SendFile::WriteFile(int32_t fd, const std::string &fileName) int32_t SendFile::ExecSendFile(const std::string &deviceId, const std::vector& srcList, const std::vector& dstList, uint32_t num) { - if (deviceId.empty()) { + if (deviceId.empty() || srcList.empty()) { LOGE("SendFile::ExecSendFile: para error.\n"); return NAPI_SENDFILE_PARA_ERROR; } @@ -208,11 +208,16 @@ int32_t SendFile::ExecSendFile(const std::string &deviceId, const std::vectorOpenFile(fd, srcList.at(0), (S_IREAD | S_IWRITE) | S_IRGRP | S_IROTH); - if (IDistributedFileService::DFS_SENDFILE_SUCCESS != - distributedFileService->SendFile(deviceId, srcList, dstList, num)) { - LOGE("SendFile::ExecSendFile: error code %{public}d", result); - return NAPI_SENDFILE_SEND_ERROR; - } + if (result != IDistributedFileService::DFS_SUCCESS) { + LOGE("SendFile::ExecSendFile: error code %{public}d", result); + return NAPI_SENDFILE_SEND_ERROR; + } + + result = distributedFileService->SendFile(deviceId, srcList, dstList, num); + if (result != IDistributedFileService::DFS_SENDFILE_SUCCESS) { + LOGE("SendFile::ExecSendFile: error code %{public}d", result); + return NAPI_SENDFILE_SEND_ERROR; + } LOGD("SendFile::ExecSendFile: napi sendfile success."); return NAPI_SENDFILE_NO_ERROR; diff --git a/frameworks/js/napi/src/sendfile_napi.cpp b/frameworks/js/napi/src/sendfile_napi.cpp index ec4045358545907d09bbb264b5f6a71c78baed79..e73eb6d078632b9b73d56b6fc957aa62619bb27c 100644 --- a/frameworks/js/napi/src/sendfile_napi.cpp +++ b/frameworks/js/napi/src/sendfile_napi.cpp @@ -44,6 +44,9 @@ napi_value JsSendFile(napi_env env, napi_callback_info info) std::tie(succ, deviceId, std::ignore) = NVal(env, funcArg[DFS_ARG_POS::FIRST]).ToUTF8String(); napi_get_value_uint32(env, funcArg[DFS_ARG_POS::FOURTH], &fileCount); + if (fileCount != 1) { + return nullptr; + } napi_value result; napi_status status; @@ -89,7 +92,6 @@ napi_value JsSendFile(napi_env env, napi_callback_info info) return NVal::CreateUndefined(env).val_; } - napi_value JsOn(napi_env env, napi_callback_info cbinfo) { size_t requireArgc = 2; diff --git a/frameworks/native/src/dfs_filetransfer_callback.cpp b/frameworks/native/src/dfs_filetransfer_callback.cpp index 99e126e7f9ca65be468f998028830eb3a125c5cc..44be900a43f9d1cfecfa8b0bcc141d6ff532cac9 100644 --- a/frameworks/native/src/dfs_filetransfer_callback.cpp +++ b/frameworks/native/src/dfs_filetransfer_callback.cpp @@ -59,7 +59,7 @@ int32_t DfsFileTransferCallback::SendError(const std::string &cid) int32_t DfsFileTransferCallback::ReceiveFinished(const std::string &cid, const std::string &fileName, uint32_t num) { TransEvent event(TransEvent::TRANS_SUCCESS, fileName, num); - std::string eventName("ReceiveFinished"); + std::string eventName("receiveFinished"); event.SetName(eventName); SendFile::EmitTransEvent(event, cid); LOGI("DfsFileTransferCallback::ReceiveFinished: [%{public}s].", cid.c_str()); @@ -69,7 +69,7 @@ int32_t DfsFileTransferCallback::ReceiveFinished(const std::string &cid, const s int32_t DfsFileTransferCallback::ReceiveError(const std::string &cid) { TransEvent event(TransEvent::TRANS_FAILURE); - std::string eventName("ReceiveFinished"); + std::string eventName("receiveFinished"); event.SetName(eventName); SendFile::EmitTransEvent(event, cid); LOGI("DfsFileTransferCallback::ReceiveError: [%{public}s].", cid.c_str()); diff --git a/services/distributedfileservice/include/ipc/distributedfile_service.h b/services/distributedfileservice/include/ipc/distributedfile_service.h index 6e5d68f6dedffeb83da3444c7e565176d367f152..811c05a62333a11c79bee585b524260182f5085c 100644 --- a/services/distributedfileservice/include/ipc/distributedfile_service.h +++ b/services/distributedfileservice/include/ipc/distributedfile_service.h @@ -49,6 +49,10 @@ public: private: void PublishSA(); void StartManagers(); + int32_t CreateSourceResources(const std::vector &sourceFileList, + uint32_t fileCount, char **sFileList); + int32_t CreateDestResources(const std::vector &destinationFileList, + uint32_t fileCount, char **dFileList); }; } // namespace DistributedFile } // namespace Storage diff --git a/services/distributedfileservice/src/ipc/distributedfile_service.cpp b/services/distributedfileservice/src/ipc/distributedfile_service.cpp index 08cc999fc93dd42e44d5a6651e6e0a34b7c1f1f9..ed4346c52a8b14d00ef9eb4546a81c32fdcc790f 100644 --- a/services/distributedfileservice/src/ipc/distributedfile_service.cpp +++ b/services/distributedfileservice/src/ipc/distributedfile_service.cpp @@ -70,22 +70,21 @@ void DistributedFileService::StartManagers() DeviceManagerAgent::GetInstance(); } -int32_t DistributedFileService::SendFile(const std::string &cid, - const std::vector &sourceFileList, - const std::vector &destinationFileList, - const uint32_t fileCount) +int32_t DistributedFileService::CreateSourceResources(const std::vector &sourceFileList, + uint32_t fileCount, char **sFileList) { - if (fileCount <= 0) { - return DFS_PARAM_FILE_COUNT_ERROR; + if (fileCount != 1 || sourceFileList.size() != fileCount || sFileList == nullptr) { + return DFS_MEM_ERROR; } - - char **sFileList = new char* [fileCount]; for (int index = 0; index < sourceFileList.size(); ++index) { LOGI("DistributedFileService::SendFile Source File List %{public}d, %{public}s, %{public}d", index, sourceFileList.at(index).c_str(), sourceFileList.at(index).length()); if (index == 0) { std::string tmpString("/data/system_ce/tmp"); int32_t length = tmpString.length(); + if (length <= 0) { + return DFS_MEM_ERROR; + } sFileList[index] = new char[length + 1]; if (memset_s(sFileList[index], length + 1, '\0', length + 1) != EOK) { return DFS_MEM_ERROR; @@ -106,33 +105,63 @@ int32_t DistributedFileService::SendFile(const std::string &cid, sFileList[index][length] = '\0'; } } + return DFS_NO_ERROR; +} - char **dFileList = nullptr; - if (destinationFileList.empty()) { - dFileList = new char* [fileCount]; - for (int index = 0; index < destinationFileList.size(); ++index) { - int32_t length = destinationFileList.at(index).length(); - dFileList[index] = new char[length + 1]; - if (memset_s(dFileList[index], length + 1, '\0', length + 1) != EOK) { - return DFS_MEM_ERROR; - } - if (memcpy_s(dFileList[index], length + 1, destinationFileList.at(index).c_str(), length) != EOK) { - return DFS_MEM_ERROR; - } - dFileList[index][length] = '\0'; +int32_t DistributedFileService::CreateDestResources(const std::vector &destinationFileList, + uint32_t fileCount, char **dFileList) +{ + if (fileCount != 1 || destinationFileList.size() != fileCount || dFileList == nullptr) { + return DFS_MEM_ERROR; + } + + for (int index = 0; index < destinationFileList.size(); ++index) { + int32_t length = destinationFileList.at(index).length(); + dFileList[index] = new char[length + 1]; + if (memset_s(dFileList[index], length + 1, '\0', length + 1) != EOK) { + return DFS_MEM_ERROR; } + if (memcpy_s(dFileList[index], length + 1, destinationFileList.at(index).c_str(), length) != EOK) { + return DFS_MEM_ERROR; + } + dFileList[index][length] = '\0'; } + return DFS_NO_ERROR; +} - auto softBusAgent = SoftbusAgent::GetInstance(); - if (DFS_SUCCESS != softBusAgent->SendFile(cid, (const char **)sFileList, (const char **)dFileList, fileCount)) { - return DFS_SOFTBUS_SEND_ERROR; +int32_t DistributedFileService::SendFile(const std::string &cid, + const std::vector &sourceFileList, + const std::vector &destinationFileList, + const uint32_t fileCount) +{ + int32_t result = DFS_NO_ERROR; + if (cid.empty() || fileCount != 1 || sourceFileList.empty() || sourceFileList.size() != fileCount) { + LOGE("SendFile params failed"); + return DFS_PARAM_FILE_COUNT_ERROR; } - return DFS_SUCCESS; + + char **sFileList = new char* [fileCount]; + char **dFileList = nullptr; + do { + result = CreateSourceResources(sourceFileList, fileCount, sFileList); + + if (destinationFileList.empty()) { + dFileList = new char* [fileCount]; + result = CreateDestResources(destinationFileList, fileCount, dFileList); + } + } while (false); + + if (result != DFS_MEM_ERROR) { + auto softBusAgent = SoftbusAgent::GetInstance(); + result = softBusAgent->SendFile(cid, (const char **)sFileList, (const char **)dFileList, fileCount); + } + + return result; } int32_t DistributedFileService::OpenFile(int32_t fd, const std::string &fileName, int32_t mode) { - if (fd <= 0) { + if (fd <= 0 || fileName.empty()) { return DFS_FILE_OP_ERROR; } @@ -202,4 +231,4 @@ int32_t DistributedFileService::IsDeviceOnline(const std::string &cid) } } // namespace DistributedFile } // namespace Storage -} // namespace OHOS +} // namespace OHOS \ No newline at end of file