From fa4ae645b69c3cbdbd86b93b4bba3e0851f606a3 Mon Sep 17 00:00:00 2001 From: hhchinasoft Date: Wed, 16 Feb 2022 17:09:12 +0800 Subject: [PATCH] fixed bc6f5b0 from https://gitee.com/hhchinasoft/filemanagement_dfs_service/pulls/41 bugfix sendfile napi callback Signed-off-by: hhchinasoft --- frameworks/js/napi/include/sendfile.h | 2 +- frameworks/js/napi/src/sendfile.cpp | 19 ++++++++++++------- frameworks/js/napi/src/sendfile_napi.cpp | 3 ++- .../src/ipc/distributedfile_service_stub.cpp | 5 +++-- .../src/network/softbus_agent.cpp | 2 +- 5 files changed, 19 insertions(+), 12 deletions(-) diff --git a/frameworks/js/napi/include/sendfile.h b/frameworks/js/napi/include/sendfile.h index a22276433..7330b214b 100644 --- a/frameworks/js/napi/include/sendfile.h +++ b/frameworks/js/napi/include/sendfile.h @@ -31,7 +31,7 @@ int32_t NapiSendError(const std::string &cid); int32_t NapiReceiveFinished(const std::string &cid, const std::string &fileName, uint32_t num); int32_t NapiReceiveError(const std::string &cid); int32_t NapiWriteFile(int32_t fd, const std::string &fileName); - +void SetEventAgentMap(const std::unordered_map &map); napi_value RegisterSendFileNotifyCallback(); static std::unordered_map g_mapUidToEventAgent; diff --git a/frameworks/js/napi/src/sendfile.cpp b/frameworks/js/napi/src/sendfile.cpp index 0d1a421f0..19674170f 100644 --- a/frameworks/js/napi/src/sendfile.cpp +++ b/frameworks/js/napi/src/sendfile.cpp @@ -37,7 +37,7 @@ namespace DistributedFile { namespace { const std::string DfsAppUid { "SendFileTestUid" }; constexpr int32_t FILE_BLOCK_SIZE = 1024; -const std::string APP_PATH { "/data/accounts/account_0/appdata/com.example.filetestkits/cache/" }; +const std::string APP_PATH { "/data/accounts/account_0/appdata/" }; } napi_value RegisterSendFileNotifyCallback() @@ -72,7 +72,7 @@ int32_t NapiDeviceOnline(const std::string &cid) for (std::unordered_map::iterator iter = g_mapUidToEventAgent.begin(); iter != g_mapUidToEventAgent.end(); ++iter) { EventAgent* agent = iter->second; - if (agent != nullptr && agent->FindDevice(DfsAppUid)) { + if (agent != nullptr && agent->FindDevice(cid)) { LOGI("NapiDeviceOnline, event agent for device[%{public}s] was found.", cid.c_str()); agent->InsertDevice(cid); break; @@ -86,7 +86,7 @@ int32_t NapiDeviceOffline(const std::string &cid) for (std::unordered_map::iterator iter = g_mapUidToEventAgent.begin(); iter != g_mapUidToEventAgent.end(); ++iter) { EventAgent* agent = iter->second; - if (agent != nullptr && agent->FindDevice(DfsAppUid)) { + if (agent != nullptr && agent->FindDevice(cid)) { LOGI("NapiDeviceOffline, event agent for device[%{public}s] was found.", cid.c_str()); agent->RemoveDevice(cid); break; @@ -101,7 +101,7 @@ int32_t NapiSendFinished(const std::string &cid, const std::string &fileName) std::unordered_map::iterator iter; for (iter = g_mapUidToEventAgent.begin(); iter != g_mapUidToEventAgent.end(); ++iter) { agent = iter->second; - if (agent->FindDevice(cid)) { + if (agent != nullptr && agent->FindDevice(cid)) { LOGI("DEBUG_SENDFILE:OnSendFinished, event agent for device[%{public}s] was found.", cid.c_str()); break; } else { @@ -143,7 +143,7 @@ int32_t NapiSendError(const std::string &cid) std::unordered_map::iterator iter; for (iter = g_mapUidToEventAgent.begin(); iter != g_mapUidToEventAgent.end(); ++iter) { agent = iter->second; - if (agent->FindDevice(cid)) { + if (agent != nullptr && agent->FindDevice(cid)) { LOGI("DEBUG_SENDFILE:OnSendError, event agent for device[%{public}s] was found.", cid.c_str()); break; } else { @@ -185,7 +185,7 @@ int32_t NapiReceiveFinished(const std::string &cid, const std::string &fileName, std::unordered_map::iterator iter; for (auto iter = g_mapUidToEventAgent.begin(); iter != g_mapUidToEventAgent.end(); ++iter) { agent = iter->second; - if (agent->FindDevice(cid)) { + if (agent != nullptr && agent->FindDevice(cid)) { LOGI("DEBUG_SENDFILE:OnReceiveFinished, event agent for device[%{public}s] was found.", cid.c_str()); break; } else { @@ -227,7 +227,7 @@ int32_t NapiReceiveError(const std::string &cid) std::unordered_map::iterator iter; for (auto iter = g_mapUidToEventAgent.begin(); iter != g_mapUidToEventAgent.end(); ++iter) { agent = iter->second; - if (agent->FindDevice(cid)) { + if (agent != nullptr && agent->FindDevice(cid)) { LOGI("OnReceiveError : event agent for device[%{public}s] was found.", cid.c_str()); break; } else { @@ -306,6 +306,11 @@ int32_t NapiWriteFile(int32_t fd, const std::string &fileName) return 0; } +void SetEventAgentMap(const std::unordered_map &map) +{ + g_mapUidToEventAgent = map; +} + int32_t ExecSendFile(const std::string &deviceId, const std::vector& srcList, const std::vector& dstList, uint32_t num) { diff --git a/frameworks/js/napi/src/sendfile_napi.cpp b/frameworks/js/napi/src/sendfile_napi.cpp index ec0e9abba..1d2f9b802 100644 --- a/frameworks/js/napi/src/sendfile_napi.cpp +++ b/frameworks/js/napi/src/sendfile_napi.cpp @@ -177,7 +177,7 @@ napi_value JS_Constructor(napi_env env, napi_callback_info cbinfo) char bundleName[SENDFILE_NAPI_BUF_LENGTH] = { 0 }; size_t typeLen = 0; napi_get_value_string_utf8(env, argv[0], bundleName, sizeof(bundleName), &typeLen); - LOGI("DEBUG_SENDFILE:JS_Constructor. %{public}s", bundleName); + LOGI("DEBUG_SENDFILE:JS_Constructor. [%{public}s]", bundleName); EventAgent* agent = new EventAgent(env, thisVar); { @@ -193,6 +193,7 @@ napi_value JS_Constructor(napi_env env, napi_callback_info cbinfo) return nullptr; } else { LOGI("map size %{public}d", g_mapUidToEventAgent.size()); + SetEventAgentMap(g_mapUidToEventAgent); } } } diff --git a/services/distributedfileservice/src/ipc/distributedfile_service_stub.cpp b/services/distributedfileservice/src/ipc/distributedfile_service_stub.cpp index 8f0195bd7..636c5fa79 100644 --- a/services/distributedfileservice/src/ipc/distributedfile_service_stub.cpp +++ b/services/distributedfileservice/src/ipc/distributedfile_service_stub.cpp @@ -19,6 +19,7 @@ #include #include +#include "filetransfer_callback_proxy.h" #include "ipc_skeleton.h" #include "utils_log.h" @@ -117,13 +118,13 @@ int32_t DistributedFileServiceStub::OpenFileStub(MessageParcel &data, MessagePar int32_t DistributedFileServiceStub::CmdRegisterNotifyCallback(MessageParcel &data, MessageParcel &reply) { - OHOS::sptr remote = data.ReadRemoteObject(); + sptr remote = data.ReadRemoteObject(); if (remote == nullptr) { LOGE("Callback ptr is nullptr."); return 0; } - OHOS::sptr callback = iface_cast(remote); + sptr callback = iface_cast(remote); int32_t result = RegisterNotifyCallback(callback); reply.WriteInt32(result); return result; diff --git a/services/distributedfileservice/src/network/softbus_agent.cpp b/services/distributedfileservice/src/network/softbus_agent.cpp index af0734b2a..4a9f7a00f 100644 --- a/services/distributedfileservice/src/network/softbus_agent.cpp +++ b/services/distributedfileservice/src/network/softbus_agent.cpp @@ -194,7 +194,7 @@ void SoftbusAgent::OnReceiveFileFinished(const int sessionId, const std::string } } - std::string desFileName = std::string("/data/system_ce/") + files; + std::string desFileName = DEFAULT_ROOT_PATH + files; int32_t fd = open(desFileName.c_str(), O_RDONLY); if (fd <= 0) { LOGE("NapiWriteFile open recive distributedfile %{public}d, %{public}s, %{public}d", -- Gitee