From 68faaebbfdb11f210ef35f6125fd5b3ade5d9e3c Mon Sep 17 00:00:00 2001 From: hhchinasoft Date: Sat, 12 Feb 2022 12:43:48 +0800 Subject: [PATCH 01/11] commit sendfile interface code Signed-off-by: hhchinasoft --- BUILD.gn | 6 + bundle.json | 6 +- frameworks/js/napi/BUILD.gn | 72 ++++ frameworks/js/napi/include/event_agent.h | 57 +++ frameworks/js/napi/include/sendfile.h | 65 +++ frameworks/js/napi/include/sendfile_napi.h | 48 +++ frameworks/js/napi/src/event_agent.cpp | 163 +++++++ frameworks/js/napi/src/sendfile.cpp | 405 ++++++++++++++++++ frameworks/js/napi/src/sendfile_napi.cpp | 242 +++++++++++ .../dfs_filetransfer_callback.h} | 82 ++-- .../include/filetransfer_callback_proxy.h | 43 ++ .../include/filetransfer_callback_stub.h | 51 +++ .../native/{ => include}/service_proxy.h | 11 +- .../native/src/dfs_filetransfer_callback.cpp | 72 ++++ .../src/filetransfer_callback_proxy.cpp | 204 +++++++++ .../native/src/filetransfer_callback_stub.cpp | 158 +++++++ frameworks/native/src/service_proxy.cpp | 168 ++++++++ .../innerkits/kits/js/@ohos.sendfile.d.ts | 60 +++ interfaces/innerkits/native/BUILD.gn | 12 +- .../native/i_distributedfile_service.h | 44 +- .../native/i_filetransfer_callback.h | 61 +++ .../include/device/device_manager_agent.h | 2 +- .../include/mountpoint/mount_manager.h | 2 +- services/distributedfileservice/BUILD.gn | 19 +- .../include/device/device_manager_agent.h | 4 +- .../include/ipc/distributedfile_service.h | 14 +- .../ipc/distributedfile_service_stub.h | 4 +- .../include/network/softbus_agent.h | 7 +- .../src/ipc/distributedfile_service.cpp | 147 ++++++- .../src/ipc/distributedfile_service_stub.cpp | 103 ++++- .../src/network/softbus_agent.cpp | 138 ++++-- .../src/network/softbus_dispatcher.cpp | 1 - utils/system/include/utils_directory.h | 3 + utils/system/include/utils_singleton.h | 2 +- utils/system/src/utils_directory.cpp | 11 + 35 files changed, 2331 insertions(+), 156 deletions(-) create mode 100644 frameworks/js/napi/BUILD.gn create mode 100644 frameworks/js/napi/include/event_agent.h create mode 100644 frameworks/js/napi/include/sendfile.h create mode 100644 frameworks/js/napi/include/sendfile_napi.h create mode 100644 frameworks/js/napi/src/event_agent.cpp create mode 100644 frameworks/js/napi/src/sendfile.cpp create mode 100644 frameworks/js/napi/src/sendfile_napi.cpp rename frameworks/native/{service_proxy.cpp => include/dfs_filetransfer_callback.h} (47%) create mode 100644 frameworks/native/include/filetransfer_callback_proxy.h create mode 100644 frameworks/native/include/filetransfer_callback_stub.h rename frameworks/native/{ => include}/service_proxy.h (75%) create mode 100644 frameworks/native/src/dfs_filetransfer_callback.cpp create mode 100644 frameworks/native/src/filetransfer_callback_proxy.cpp create mode 100644 frameworks/native/src/filetransfer_callback_stub.cpp create mode 100644 frameworks/native/src/service_proxy.cpp create mode 100644 interfaces/innerkits/kits/js/@ohos.sendfile.d.ts create mode 100644 interfaces/innerkits/native/i_filetransfer_callback.h diff --git a/BUILD.gn b/BUILD.gn index 180b46ec8..26d862a8b 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -28,6 +28,12 @@ group("services_target") { ] } +group("napi_target") { + deps = [ + "${distributedfile_path}/frameworks/js/napi:build_kits_js", + ] +} + group("dfs_test_moudule") { testonly = true deps = [ diff --git a/bundle.json b/bundle.json index fb9e31e5b..5b5762202 100644 --- a/bundle.json +++ b/bundle.json @@ -36,14 +36,16 @@ }, "build": { "sub_component": [ - "//foundation/filemanagement/dfs_service:services_target" + "//foundation/filemanagement/dfs_service:services_target", + "//foundation/filemanagement/dfs_service:napi_target" ], "inner_kits": [ { "name": "//foundation/filemanagement/dfs_service:libdistributedfile_innerkits", "header": { "header_files": [ - "i_distributedfile_service.h" + "i_distributedfile_service.h", + "i_filetransfer_callback.h" ], "header_base": "//foundation/filemanagement/dfs_service/interfaces/innerkits/native" } diff --git a/frameworks/js/napi/BUILD.gn b/frameworks/js/napi/BUILD.gn new file mode 100644 index 000000000..96abcc806 --- /dev/null +++ b/frameworks/js/napi/BUILD.gn @@ -0,0 +1,72 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +ohos_shared_library("sendfile") { + include_dirs = [ + "//third_party/libuv/include", + "//third_party/node/src", + "//foundation/ace/napi/interfaces/kits", + "//foundation/filemanagement/dfs_service/interfaces/innerkits/native", + "//foundation/filemanagement/dfs_service/frameworks/native/include", + "//foundation/filemanagement/dfs_service/frameworks/js/napi/include", + "//foundation/filemanagement/dfs_service/utils/log/include", + "//foundation/filemanagement/dfs_service/services/distributedfileservice/include/ipc", + "//foundation/filemanagement/dfs_service/services/distributedfileservice/include", + "//foundation/communication/ipc/interfaces/innerkits/libdbinder/include", + "//foundation/distributedschedule/samgr/services/samgr/native/include", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", + "//foundation/distributeddatamgr/distributedfile/utils/filemgmt_libn/include", + ] + + sources = [ + "//foundation/filemanagement/dfs_service/frameworks/js/napi/src/sendfile_napi.cpp", + "//foundation/filemanagement/dfs_service/frameworks/js/napi/src/sendfile.cpp", + "//foundation/filemanagement/dfs_service/frameworks/js/napi/src/event_agent.cpp", + "//foundation/filemanagement/dfs_service/frameworks/native/src/dfs_filetransfer_callback.cpp", + "//foundation/filemanagement/dfs_service/frameworks/native/src/filetransfer_callback_proxy.cpp", + "//foundation/filemanagement/dfs_service/frameworks/native/src/filetransfer_callback_stub.cpp", + ] + + deps = [ + "//foundation/filemanagement/dfs_service/utils:libdistributedfileutils", + "//foundation/filemanagement/dfs_service/interfaces/innerkits/native:libdistributedfile_innerkits", + "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", + "//foundation/ace/napi:ace_napi", + "//utils/native/base:utilsecurec", + "//foundation/distributeddatamgr/distributedfile/utils/filemgmt_libn:filemgmt_libn", + ] + + external_deps = [ + "ipc:ipc_core", + "samgr_standard:samgr_common", + "samgr_standard:samgr_proxy", + "hiviewdfx_hilog_native:libhilog", + ] + + cflags_cc = [ + "-Wno-unused-function" + ] + + relative_install_dir = "module" + subsystem_name = "filemanagement" + part_name = "dfs_service" +} + +group("build_kits_js") { + deps = [ + ":sendfile", + ] +} \ No newline at end of file diff --git a/frameworks/js/napi/include/event_agent.h b/frameworks/js/napi/include/event_agent.h new file mode 100644 index 000000000..efb7604d1 --- /dev/null +++ b/frameworks/js/napi/include/event_agent.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NATIVE_MODULE_SEND_FILE_EVENT_AGENT_H +#define NATIVE_MODULE_SEND_FILE_EVENT_AGENT_H + +#include "napi/native_api.h" +#include +#include + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +struct EventListener; +class Event { +public: + virtual napi_value ToJsObject(napi_env env) = 0; +}; + +class EventAgent { +public: + EventAgent(napi_env env, napi_value thisVar); + virtual ~EventAgent(); + + void On(const char* type, napi_value handler); + void Off(const char* type, napi_value handler); + void Off(const char* type); + void Emit(const char* type, Event* event); + void InsertDevice(const std::string&); + void RemoveDevice(const std::string&); + bool FindDevice(const std::string&); + void ClearDevice(); + +protected: + std::mutex deviceListMut_; + std::set deviceList_; + napi_env env_; + napi_ref thisVarRef_; + EventListener* first_; + EventListener* last_; +}; +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS +#endif /* NATIVE_MODULE_SEND_FILE_EVENT_AGENT_H */ \ No newline at end of file diff --git a/frameworks/js/napi/include/sendfile.h b/frameworks/js/napi/include/sendfile.h new file mode 100644 index 000000000..1b7b07196 --- /dev/null +++ b/frameworks/js/napi/include/sendfile.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SENDFILE_H +#define SENDFILE_H + +#include "event_agent.h" + +#include +#include +#include + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +class TransEvent : public Event { +public: + enum { + TRANS_SUCCESS = 0, + TRANS_FAILURE = 1 + }; +public: + TransEvent(int32_t err, std::string fname, int32_t num) : errorCode_(err), fileName_(fname), fileCount_(num) {} + TransEvent(int32_t err, std::string fname) : errorCode_(err), fileName_(fname) {} + TransEvent(int32_t err) : errorCode_(err) {} + virtual ~TransEvent() {} + napi_value ToJsObject(napi_env env); + +private: + int32_t errorCode_ = TRANS_SUCCESS; + std::string fileName_ = ""; + int32_t fileCount_ = 0; +}; + +int32_t NapiDeviceOnline(const std::string &cid); +int32_t NapiDeviceOffline(const std::string &cid); +int32_t NapiSendFinished(const std::string &cid, const std::string &fileName); +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); + +napi_value RegisterSendFileNotifyCallback(); + +static std::mutex g_uidMutex; +static std::unordered_map g_mapUidToEventAgent; + +int32_t ExecSendFile(const std::string &deviceId, const std::vector& srcList, + const std::vector& dstList, uint32_t num); +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS +#endif // SENDFILE_H \ No newline at end of file diff --git a/frameworks/js/napi/include/sendfile_napi.h b/frameworks/js/napi/include/sendfile_napi.h new file mode 100644 index 000000000..775ffa22d --- /dev/null +++ b/frameworks/js/napi/include/sendfile_napi.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SENDFILE_NAPI_H +#define SENDFILE_NAPI_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { + enum DFS_ARG_CNT { + ZERO = 0, + ONE = 1, + TWO = 2, + THREE = 3, + FOUR = 4, + FIVE = 5 + }; + enum DFS_ARG_POS { + FIRST = 0, + SECOND = 1, + THIRD = 2, + FOURTH = 3, + FIFTH = 4, + SIXTH = 5 + }; + napi_value JS_Constructor(napi_env env, napi_callback_info cbinfo); + napi_value SendFile(napi_env env, napi_callback_info info); + napi_value JS_On(napi_env env, napi_callback_info cbinfo); + napi_value JS_Off(napi_env env, napi_callback_info cbinfo); +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS +#endif // SENDFILE_NAPI_H \ No newline at end of file diff --git a/frameworks/js/napi/src/event_agent.cpp b/frameworks/js/napi/src/event_agent.cpp new file mode 100644 index 000000000..495918c56 --- /dev/null +++ b/frameworks/js/napi/src/event_agent.cpp @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "event_agent.h" + +#include "securec.h" +#include "utils_log.h" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +namespace { + constexpr int32_t LISTENER_TYPTE_MAX_LENGTH = 64; +} +struct EventListener { + char type[LISTENER_TYPTE_MAX_LENGTH] = { 0 }; + bool isOnce = false; + napi_ref handlerRef = nullptr; + EventListener* back = nullptr; + EventListener* next = nullptr; +}; + +EventAgent::EventAgent(napi_env env, napi_value thisVar) + : env_(env), thisVarRef_(nullptr), first_(nullptr), last_(nullptr) +{ + napi_create_reference(env, thisVar, 1, &thisVarRef_); +} + +EventAgent::~EventAgent() +{ + EventListener* temp = nullptr; + for (EventListener* i = first_; i != nullptr; i = temp) { + temp = i->next; + if (i == first_) { + first_ = first_->next; + } else if (i == last_) { + last_ = last_->back; + } else { + i->next->back = i->back; + i->back->next = i->next; + } + napi_delete_reference(env_, i->handlerRef); + delete i; + } + napi_delete_reference(env_, thisVarRef_); +} + +void EventAgent::On(const char* type, napi_value handler) +{ + auto tmp = new EventListener(); + + if (strncpy_s(tmp->type, LISTENER_TYPTE_MAX_LENGTH, type, strlen(type)) == -1) { + delete tmp; + tmp = nullptr; + return; + } + + if (first_ == nullptr) { + first_ = last_ = tmp; + } else { + last_->next = tmp; + last_->next->back = last_; + last_ = last_->next; + } + last_->isOnce = false; + napi_create_reference(env_, handler, 1, &last_->handlerRef); +} + +void EventAgent::Off(const char* type) +{ + EventListener* temp = nullptr; + for (EventListener* eventListener = first_; eventListener != nullptr; eventListener = temp) { + temp = eventListener->next; + if (strcmp(eventListener->type, type) == 0) { + if (eventListener == first_) { + first_ = first_->next; + } else if (eventListener == last_) { + last_ = last_->back; + } else { + eventListener->next->back = eventListener->back; + eventListener->back->next = eventListener->next; + } + napi_delete_reference(env_, eventListener->handlerRef); + delete eventListener; + eventListener = nullptr; + } + } +} + +void EventAgent::Emit(const char* type, Event* event) +{ + napi_handle_scope scope = nullptr; + napi_open_handle_scope(env_, &scope); + + napi_value thisVar = nullptr; + napi_get_reference_value(env_, thisVarRef_, &thisVar); + for (EventListener* eventListener = first_; eventListener != nullptr; eventListener = eventListener->next) { + if (strcmp(eventListener->type, type) == 0) { + napi_value jsEvent = event ? event->ToJsObject(env_) : nullptr; + napi_value handler = nullptr; + napi_value callbackResult = nullptr; + napi_value result = nullptr; + napi_get_undefined(env_, &result); + napi_value callbackValues[2] = {0}; + callbackValues[0] = result; + callbackValues[1] = jsEvent; + napi_get_reference_value(env_, eventListener->handlerRef, &handler); + napi_call_function(env_, thisVar, handler, std::size(callbackValues), callbackValues, &callbackResult); + if (eventListener->isOnce) { + Off(type); + } + } + } + + napi_close_handle_scope(env_, scope); +} + +void EventAgent::InsertDevice(const std::string& deviceId) +{ + if (deviceList_.find(deviceId) == deviceList_.end()) { + std::unique_lock lock(deviceListMut_); + deviceList_.insert(deviceId); + LOGI("xhl insert device[%{public}s] into the devicelist", deviceId.c_str()); + } else { + LOGI("xhl device[%{public}s] has already in the devicelist", deviceId.c_str()); + } +} + +void EventAgent::RemoveDevice(const std::string& deviceId) +{ + if (deviceList_.find(deviceId) != deviceList_.end()) { + std::unique_lock lock(deviceListMut_); + deviceList_.erase(deviceId); + LOGI("xhl remove device[%{public}s] from the devicelist", deviceId.c_str()); + } else { + LOGI("xhl no device[%{public}s] in the devicelist", deviceId.c_str()); + } +} + +bool EventAgent::FindDevice(const std::string& deviceId) +{ + return (deviceList_.find(deviceId) != deviceList_.end()); +} + +void EventAgent::ClearDevice() +{ + deviceList_.clear(); +} +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/src/sendfile.cpp b/frameworks/js/napi/src/sendfile.cpp new file mode 100644 index 000000000..cc165e312 --- /dev/null +++ b/frameworks/js/napi/src/sendfile.cpp @@ -0,0 +1,405 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "sendfile.h" + +#include +#include +#include + +#include "system_ability_definition.h" +#include "i_distributedfile_service.h" +#include "service_proxy.h" +#include "utils_log.h" +#include "event_agent.h" +#include "filetransfer_callback_proxy.h" +#include "dfs_filetransfer_callback.h" +#include "utils_directory.h" + +#include +#include + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +const std::string DfsAppUid{"SendFileTestUid"}; +constexpr int32_t FILE_BLOCK_SIZE = 1024; + +napi_value TransEvent::ToJsObject(napi_env env) +{ + napi_value te; + napi_status status = napi_create_object(env, &te); + if (napi_ok == status) { + napi_value err; + status = napi_create_int32(env, errorCode_, &err); + if (napi_ok == status) { + status = napi_set_named_property(env, te, "errorCode", err); + if (napi_ok != status) { + LOGE("ToJsObject: package error code failed.\n"); + return nullptr; + } + } + else { + LOGE("ToJsObject: create error code failed.\n"); + return nullptr; + } + + napi_value fname; + status = napi_create_string_utf8(env, fileName_.c_str(), fileName_.size()+1, &fname); + if (napi_ok == status) { + status = napi_set_named_property(env, te, "fileName", fname); + if (napi_ok != status) { + LOGE("ToJsObject: package filename failed.\n"); + return nullptr; + } + } + else { + LOGE("ToJsObject: create filename failed.\n"); + return nullptr; + } + + napi_value fcount; + status = napi_create_int32(env, fileCount_, &fcount); + if (napi_ok == status) { + status = napi_set_named_property(env, te, "fileCount", fcount); + if (napi_ok != status) { + LOGE("ToJsObject: package file counts failed.\n"); + return nullptr; + } + } + else { + LOGE("ToJsObject: create file counts failed.\n"); + return nullptr; + } + } + else { + LOGE("ToJsObject: create object failed.\n"); + return nullptr; + } + + LOGI("ToJsObject: create parameter object success.\n"); + return te; +} + + +napi_value RegisterSendFileNotifyCallback() +{ + sptr systemAbilityMgr = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityMgr == nullptr) { + LOGE("SendFile napi Regist callback Get ISystemAbilityManager failed ... \n"); + return nullptr; + } + + sptr remote = systemAbilityMgr->GetSystemAbility(STORAGE_DISTRIBUTED_FILE_SERVICE_SA_ID); + if (remote == nullptr) { + LOGE("SendFile napi Regist callback Get SaId = %{public}d fail ... \n", STORAGE_DISTRIBUTED_FILE_SERVICE_SA_ID); + return nullptr; + } + + sptr distributedFileService = iface_cast(remote); + if (distributedFileService == nullptr) { + LOGE("SendFile napi Regist callback == nullptr\n"); + return nullptr; + } + + sptr callback = (std::make_unique()).release(); + LOGI("INotifyCallback == callback %{public}p", callback->AsObject().GetRefPtr()); + distributedFileService->RegisterNotifyCallback(callback); + return nullptr; +} + +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)) { + LOGI("NapiDeviceOnline, event agent for device[%{public}s] was found.", cid.c_str()); + agent->InsertDevice(cid); + break; + } + } + return 0; +} + +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)) { + LOGI("NapiDeviceOffline, event agent for device[%{public}s] was found.", cid.c_str()); + agent->RemoveDevice(cid); + break; + } + } + return 0; +} + +int32_t NapiSendFinished(const std::string &cid, const std::string &fileName) +{ + EventAgent* agent = nullptr; + std::unordered_map::iterator iter; + for (iter = g_mapUidToEventAgent.begin(); iter != g_mapUidToEventAgent.end(); ++iter) { + agent = iter->second; + if (agent->FindDevice(cid)) { + LOGI("DEBUG_SENDFILE:OnSendFinished, event agent for device[%{public}s] was found.", cid.c_str()); + break; + } + else { + sptr systemAbilityMgr = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityMgr == nullptr) { + return -1; + } + sptr remote = systemAbilityMgr->GetSystemAbility(STORAGE_DISTRIBUTED_FILE_SERVICE_SA_ID); + if (remote == nullptr) { + return -1; + } + sptr distributedFileService = iface_cast(remote); + if (distributedFileService == nullptr) { + return -1; + } + if (distributedFileService->IsDeviceOnline(cid) == 1) { + agent->InsertDevice(cid); + } + LOGI("DEBUG_SENDFILE:OnSendFinished, ------ no event agent for device[%{public}s].", cid.c_str()); + } + } + + iter = g_mapUidToEventAgent.find(DfsAppUid); + if (g_mapUidToEventAgent.end() != iter) { + TransEvent event(TransEvent::TRANS_SUCCESS, fileName); + agent->Emit("sendFinished", reinterpret_cast(&event)); + LOGI("DEBUG_SENDFILE:OnSendFinished, [%{public}s] event was done.", cid.c_str()); + return 0; + } + else { + LOGI("DEBUG_SENDFILE:OnSendFinished, [%{public}s] no event processor.", cid.c_str()); + return -1; + } +} + +int32_t NapiSendError(const std::string &cid) +{ + EventAgent* agent = nullptr; + std::unordered_map::iterator iter; + for (iter = g_mapUidToEventAgent.begin(); iter != g_mapUidToEventAgent.end(); ++iter) { + agent = iter->second; + if (agent->FindDevice(cid)) { + LOGI("DEBUG_SENDFILE:OnSendError, event agent for device[%{public}s] was found.", cid.c_str()); + break; + } + else { + sptr systemAbilityMgr = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityMgr == nullptr) { + return -1; + } + sptr remote = systemAbilityMgr->GetSystemAbility(STORAGE_DISTRIBUTED_FILE_SERVICE_SA_ID); + if (remote == nullptr) { + return -1; + } + sptr distributedFileService = iface_cast(remote); + if (distributedFileService == nullptr) { + return -1; + } + if (distributedFileService->IsDeviceOnline(cid) == 1) { + agent->InsertDevice(cid); + } + LOGI("DEBUG_SENDFILE:OnSendError, no event agent for device[%{public}s].", cid.c_str()); + } + } + + iter = g_mapUidToEventAgent.find(DfsAppUid); + if (g_mapUidToEventAgent.end() != iter) { + TransEvent event(TransEvent::TRANS_FAILURE); + agent->Emit("sendFinished", reinterpret_cast(&event)); + LOGI("DEBUG_SENDFILE:OnSendError, [%{public}s] event was done.", cid.c_str()); + return 0; + } + else { + LOGI("DEBUG_SENDFILE:OnSendError, [%{public}s] no event processor.", cid.c_str()); + return -1; + } +} + +int32_t NapiReceiveFinished(const std::string &cid, const std::string &fileName, uint32_t num) +{ + EventAgent* agent = nullptr; + std::unordered_map::iterator iter; + for (auto iter = g_mapUidToEventAgent.begin(); iter != g_mapUidToEventAgent.end(); ++iter) { + agent = iter->second; + if (agent->FindDevice(cid)) { + LOGI("DEBUG_SENDFILE:OnReceiveFinished, event agent for device[%{public}s] was found.", cid.c_str()); + break; + } + else { + sptr systemAbilityMgr = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityMgr == nullptr) { + return -1; + } + sptr remote = systemAbilityMgr->GetSystemAbility(STORAGE_DISTRIBUTED_FILE_SERVICE_SA_ID); + if (remote == nullptr) { + return -1; + } + sptr distributedFileService = iface_cast(remote); + if (distributedFileService == nullptr) { + return -1; + } + if (distributedFileService->IsDeviceOnline(cid) == 1) { + agent->InsertDevice(cid); + } + LOGI("DEBUG_SENDFILE:OnReceiveFinished, no event agent for device[%{public}s].", cid.c_str()); + } + } + + iter = g_mapUidToEventAgent.find(DfsAppUid); + if (g_mapUidToEventAgent.end() != iter) { + TransEvent event(TransEvent::TRANS_SUCCESS, fileName, num); + agent->Emit("receiveFinished", reinterpret_cast(&event)); + LOGI("DEBUG_SENDFILE:OnReceiveFinished, [%{public}s] event was done.", cid.c_str()); + return 0; + } + else { + LOGI("DEBUG_SENDFILE:OnReceiveFinished, [%{public}s] no event processor.", cid.c_str()); + return -1; + } +} + +int32_t NapiReceiveError(const std::string &cid) +{ + EventAgent* agent = nullptr; + std::unordered_map::iterator iter; + for (auto iter = g_mapUidToEventAgent.begin(); iter != g_mapUidToEventAgent.end(); ++iter) { + agent = iter->second; + if (agent->FindDevice(cid)) { + LOGI("OnReceiveError : event agent for device[%{public}s] was found.", cid.c_str()); + break; + } + else { + sptr systemAbilityMgr = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityMgr == nullptr) { + return -1; + } + sptr remote = systemAbilityMgr->GetSystemAbility(STORAGE_DISTRIBUTED_FILE_SERVICE_SA_ID); + if (remote == nullptr) { + return -1; + } + sptr distributedFileService = iface_cast(remote); + if (distributedFileService == nullptr) { + return -1; + } + if (distributedFileService->IsDeviceOnline(cid) == 1) { + agent->InsertDevice(cid); + } + LOGI("OnReceiveError : no event agent for device[%{public}s].", cid.c_str()); + } + } + + iter = g_mapUidToEventAgent.find(DfsAppUid); + if (g_mapUidToEventAgent.end() != iter) { + TransEvent event(TransEvent::TRANS_FAILURE); + agent->Emit("receiveFinished", reinterpret_cast(&event)); + LOGI("OnReceiveError : [%{public}s] event was done.", cid.c_str()); + return 0; + } + else { + LOGI("OnReceiveError : [%{public}s] no event processor.", cid.c_str()); + return -1; + } +} + +int32_t NapiWriteFile(int32_t fd, const std::string &fileName) +{ + LOGI("NapiWriteFile start LOGI %{public}s", fileName.c_str()); + LOGI("NapiWriteFile start LOGI %{public}d", fd); + if (fd <= 0) { + return -1; + } + std::string filePath = std::string("/data/accounts/account_0/appdata/com.example.filetestkits/cache/") + fileName; + int32_t flags = O_WRONLY; + LOGI("flags %{public}d", flags); + if (!Utils::IsFileExist(filePath)) { + LOGI("NapiWriteFile file not exist"); + flags |= O_CREAT; + } + LOGI("flags %{public}d", flags); + int32_t writeFd = open(filePath.c_str(), flags, 00644); + // int32_t writeFd = open("/data/init_agent/test.txt", O_RDWR | O_CREAT, 00664); + if (writeFd <= 0) { + LOGE("NapiWriteFile open file failed %{public}d, %{public}s, %{public}d", writeFd, strerror(errno), errno); + return -1; + } + auto buffer = std::make_unique(FILE_BLOCK_SIZE); + ssize_t actLen = 0; + do { + actLen = read(fd, buffer.get(), FILE_BLOCK_SIZE); + if (actLen > 0) { + write(writeFd, buffer.get(), actLen); + } else if (actLen == 0) { + break; + } else { + if (errno == EINTR) { + actLen = FILE_BLOCK_SIZE; + } else { + return -1; + } + } + } while (actLen > 0); + + close(writeFd); + + return 0; +} + +int32_t ExecSendFile(const std::string &deviceId, const std::vector& srcList, + const std::vector& dstList, uint32_t num) +{ + if (deviceId.empty()) { + LOGE("DeviceId can't be emptey.\n"); + return -2; + } + sptr systemAbilityMgr = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityMgr == nullptr) { + LOGE("BundleService Get ISystemAbilityManager failed ... \n"); + return -1; + } + sptr remote = systemAbilityMgr->CheckSystemAbility(STORAGE_DISTRIBUTED_FILE_SERVICE_SA_ID); + if (remote == nullptr) { + LOGE("DistributedFileService Get STORAGE_DISTRIBUTED_FILE_SERVICE_SA_ID = %{public}d fail ... \n", + STORAGE_DISTRIBUTED_FILE_SERVICE_SA_ID); + return -1; + } + + sptr distributedFileService = iface_cast(remote); + if (distributedFileService == nullptr) { + LOGE("DistributedFileService == nullptr\n"); + return -1; + } + + int32_t fd = open(srcList.at(0).c_str(), O_RDONLY); + int32_t result = distributedFileService->OpenFile(fd, srcList.at(0), (S_IREAD & S_IWRITE) | S_IRGRP | S_IROTH); + result = distributedFileService->SendFile(deviceId, srcList, dstList, num); + LOGI(" ------------ DistributedFileService SendFile result %{public}d", result); + return result; +} +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/src/sendfile_napi.cpp b/frameworks/js/napi/src/sendfile_napi.cpp new file mode 100644 index 000000000..47de94c54 --- /dev/null +++ b/frameworks/js/napi/src/sendfile_napi.cpp @@ -0,0 +1,242 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "sendfile_napi.h" + +#include "filemgmt_libn.h" +#include "sendfile.h" +#include "utils_log.h" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +using namespace FileManagement::LibN; +namespace { + constexpr int32_t MAX_SEND_FILE_HAP_NUMBER = 50; + constexpr int32_t SENDFILE_NAPI_BUF_LENGTH = 256; + const std::string DfsAppUid{"SendFileTestUid"}; +} + +napi_value SendFile(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(DFS_ARG_CNT::FOUR, DFS_ARG_CNT::FIVE)) { + NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + bool succ = false; + auto resultCode = std::make_shared(); + + std::unique_ptr deviceId; + std::vector sourPath; + std::vector destPath; + + uint32_t fileCount = 0; + + 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); + + napi_value result; + napi_status status; + for (uint32_t i = 0; i < fileCount; ++i) { + status = napi_get_element(env, funcArg[DFS_ARG_POS::SECOND], i, &result); + if (napi_ok != status) { + return nullptr; + } + std::unique_ptr path; + std::tie(succ, path, std::ignore) = NVal(env, result).ToUTF8String(); + sourPath.push_back(path.get()); + + status = napi_get_element(env, funcArg[DFS_ARG_POS::THIRD], i, &result); + if (napi_ok != status) { + return nullptr; + } + std::tie(succ, path, std::ignore) = NVal(env, result).ToUTF8String(); + destPath.push_back(path.get()); + } + + std::string deviceIdString(deviceId.get()); + auto cbExec = [deviceIdString, sourPath, destPath, fileCount, resultCode]() -> NError { + *resultCode = ExecSendFile(deviceIdString.c_str(), sourPath, destPath, fileCount); + LOGI("cbExec resultCode : %{public}d", *resultCode); + return NError(*resultCode); + }; + + auto cbComplete = [resultCode](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + LOGI("cbComplete resultCode : %{public}d", *resultCode); + return { NVal::CreateInt64(env, *resultCode) }; + }; + + LOGI("JS SendFile created thread params count %{public}d", funcArg.GetArgc()); + std::string procedureName = "SendFile"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == DFS_ARG_CNT::FOUR) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } else if (funcArg.GetArgc() == DFS_ARG_CNT::FIVE) { + NVal cb(env, funcArg[DFS_ARG_POS::FIFTH]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + } + + return NVal::CreateUndefined(env).val_; +} + + +napi_value JS_On(napi_env env, napi_callback_info cbinfo) +{ + size_t requireArgc = 2; + size_t argc = 2; + napi_value argv[2] = { 0 }; + napi_value thisVar = 0; + void* data = nullptr; + napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data); + + EventAgent* agent = nullptr; + napi_unwrap(env, thisVar, (void**)&agent); + + NAPI_ASSERT(env, argc >= requireArgc, "requires 2 parameter"); + + napi_valuetype eventValueType = napi_undefined; + napi_typeof(env, argv[0], &eventValueType); + NAPI_ASSERT(env, eventValueType == napi_string, "type mismatch for parameter 1"); + + napi_valuetype eventHandleType = napi_undefined; + napi_typeof(env, argv[1], &eventHandleType); + NAPI_ASSERT(env, eventHandleType == napi_function, "type mismatch for parameter 2"); + + char type[64] = { 0 }; + size_t typeLen = 0; + + napi_get_value_string_utf8(env, argv[0], type, sizeof(type), &typeLen); + + agent->On((const char*)type, argv[1]); + + napi_value result = nullptr; + napi_get_undefined(env, &result); + return result; +} + +napi_value JS_Off(napi_env env, napi_callback_info cbinfo) +{ + size_t requireArgc = 1; + size_t argc = 1; + napi_value argv[1] = { 0 }; + napi_value thisVar = 0; + void* data = nullptr; + napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data); + + EventAgent* agent = nullptr; + napi_unwrap(env, thisVar, (void**)&agent); + + NAPI_ASSERT(env, argc == requireArgc, "requires 1 parameter"); + + napi_valuetype eventValueType = napi_undefined; + napi_typeof(env, argv[0], &eventValueType); + NAPI_ASSERT(env, eventValueType == napi_string, "type mismatch for parameter 1"); + + char* type = nullptr; + size_t typeLen = 0; + napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typeLen); + + NAPI_ASSERT(env, typeLen > 0, "typeLen == 0"); + type = new char[typeLen + 1]; + + napi_get_value_string_utf8(env, argv[0], type, typeLen + 1, &typeLen); + + agent->Off(type); + + delete[] type; + napi_value result = nullptr; + napi_get_undefined(env, &result); + return result; +} + +napi_value JS_Constructor(napi_env env, napi_callback_info cbinfo) +{ + size_t argc = 2; + napi_value argv[2] = { nullptr }; + napi_value thisVar = nullptr; + napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, nullptr); + + 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); + + EventAgent* agent = new EventAgent(env, thisVar); + { + std::unique_lock lock(g_uidMutex); + if (g_mapUidToEventAgent.end() != g_mapUidToEventAgent.find(DfsAppUid)) { + delete g_mapUidToEventAgent[DfsAppUid]; + g_mapUidToEventAgent.erase(DfsAppUid); + } + if (g_mapUidToEventAgent.size() <= MAX_SEND_FILE_HAP_NUMBER) { + auto [ignored, inserted] = g_mapUidToEventAgent.insert(make_pair(DfsAppUid, agent)); + if (!inserted) { + LOGE("map env to event agent error."); + return nullptr; + } else { + LOGI("map size %{public}d", g_mapUidToEventAgent.size()); + } + } + } + + napi_wrap(env, thisVar, agent, + [](napi_env env, void* data, void* hint) { + auto iter = g_mapUidToEventAgent.find(DfsAppUid); + if (g_mapUidToEventAgent.end() != iter) { + auto agent = (EventAgent*)data; + if (agent != nullptr) { + std::unique_lock lock(g_uidMutex); + agent->ClearDevice(); + g_mapUidToEventAgent.erase(DfsAppUid); + delete agent; + } + } + }, + nullptr, nullptr); + + return thisVar; +} + +/*********************************************** + * Module export and register + ***********************************************/ +napi_value SendFileExport(napi_env env, napi_value exports) +{ + const char className[] = "SendFile"; + static napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("sendFile", SendFile), + DECLARE_NAPI_FUNCTION("on", JS_On), + DECLARE_NAPI_FUNCTION("off", JS_Off), + }; + napi_value sendFileClass = nullptr; + + napi_define_class(env, className, sizeof(className), JS_Constructor, nullptr, + sizeof(desc) / sizeof(desc[0]), desc, &sendFileClass); + + napi_set_named_property(env, exports, "SendFile", sendFileClass); + + RegisterSendFileNotifyCallback(); + return exports; +} + +NAPI_MODULE(sendfile, SendFileExport) +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/service_proxy.cpp b/frameworks/native/include/dfs_filetransfer_callback.h similarity index 47% rename from frameworks/native/service_proxy.cpp rename to frameworks/native/include/dfs_filetransfer_callback.h index 8d9ae8239..f2a84517b 100644 --- a/frameworks/native/service_proxy.cpp +++ b/frameworks/native/include/dfs_filetransfer_callback.h @@ -1,43 +1,39 @@ -/* - * Copyright (C) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "service_proxy.h" -#include "utils_log.h" - -namespace OHOS { -namespace Storage { -namespace DistributedFile { - -int32_t ServiceProxy::SendFile(const std::string &cid, - const std::vector &sourceFileList, - const std::vector &destinationFileList, - const uint32_t fileCount) -{ - return 0; -} - -int32_t ServiceProxy::sendTest() -{ - LOGE("sendTest enter"); - MessageParcel data; - MessageParcel reply; - MessageOption option; - int ret = Remote()->SendRequest(TEST_CODE, data, reply, option); - LOGE("sendTest sendrequest done %{public}d", ret); - return ret; -} -} // namespace DistributedFile -} // namespace Storage -} // namespace OHOS \ No newline at end of file +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DFS_FILETRANSFER_CALLBACK_H +#define DFS_FILETRANSFER_CALLBACK_H + +#include "filetransfer_callback_stub.h" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +class DfsFileTransferCallback : public FileTransferCallbackStub { +public: + DfsFileTransferCallback(); + virtual ~DfsFileTransferCallback() override; + int32_t DeviceOnline(const std::string &cid) override; + int32_t DeviceOffline(const std::string &cid) override; + int32_t SendFinished(const std::string &cid, std::string fileName) override; + int32_t SendError(const std::string &cid) override; + int32_t ReceiveFinished(const std::string &cid, const std::string &fileName, uint32_t num) override; + int32_t ReceiveError(const std::string &cid) override; + int32_t WriteFile(int32_t fd, const std::string &fileName) override; +}; +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS +#endif // DFS_FILETRANSFER_CALLBACK_H \ No newline at end of file diff --git a/frameworks/native/include/filetransfer_callback_proxy.h b/frameworks/native/include/filetransfer_callback_proxy.h new file mode 100644 index 000000000..cc2423ee6 --- /dev/null +++ b/frameworks/native/include/filetransfer_callback_proxy.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef FILETRANSFER_CALLBACK_PROXY_H +#define FILETRANSFER_CALLBACK_PROXY_H + +#include "iremote_proxy.h" +#include "i_filetransfer_callback.h" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +class FileTransferCallbackProxy : public IRemoteProxy { +public: + explicit FileTransferCallbackProxy(const sptr &impl); + virtual ~FileTransferCallbackProxy(); + +public: + int32_t DeviceOnline(const std::string &cid) override; + int32_t DeviceOffline(const std::string &cid) override; + int32_t SendFinished(const std::string &cid, std::string fileName) override; + int32_t SendError(const std::string &cid) override; + int32_t ReceiveFinished(const std::string &cid, const std::string &fileName, uint32_t num) override; + int32_t ReceiveError(const std::string &cid) override; + int32_t WriteFile(int32_t fd, const std::string &fileName) override; +private: + static inline BrokerDelegator delegator_; +}; +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS +#endif // FILETRANSFER_CALLBACK_PROXY_H \ No newline at end of file diff --git a/frameworks/native/include/filetransfer_callback_stub.h b/frameworks/native/include/filetransfer_callback_stub.h new file mode 100644 index 000000000..a5dcce439 --- /dev/null +++ b/frameworks/native/include/filetransfer_callback_stub.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef FILETRANSFER_CALLBACK_STUB_H +#define FILETRANSFER_CALLBACK_STUB_H + +#include + +#include "iremote_stub.h" +#include "i_filetransfer_callback.h" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +class FileTransferCallbackStub : public IRemoteStub { +public: + FileTransferCallbackStub(); + virtual ~FileTransferCallbackStub(); + + int32_t OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + +private: + using NotifyCallbackFunc = int32_t (FileTransferCallbackStub::*)(MessageParcel &, MessageParcel &); + +private: + int32_t CmdOnDeviceOnline(MessageParcel &data, MessageParcel &reply); + int32_t CmdOnDeviceOffline(MessageParcel &data, MessageParcel &reply); + int32_t CmdOnSendFinished(MessageParcel &data, MessageParcel &reply); + int32_t CmdOnSendError(MessageParcel &data, MessageParcel &reply); + int32_t CmdOnReceiveFinished(MessageParcel &data, MessageParcel &reply); + int32_t CmdOnReceiveError(MessageParcel &data, MessageParcel &reply); + int32_t CmdWriteFile(MessageParcel &data, MessageParcel &reply); +private: + std::map memberFuncMap_; +}; +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS +#endif // FILETRANSFER_CALLBACK_STUB_H \ No newline at end of file diff --git a/frameworks/native/service_proxy.h b/frameworks/native/include/service_proxy.h similarity index 75% rename from frameworks/native/service_proxy.h rename to frameworks/native/include/service_proxy.h index d5a2d52c4..e1b2561d4 100644 --- a/frameworks/native/service_proxy.h +++ b/frameworks/native/include/service_proxy.h @@ -16,7 +16,7 @@ #ifndef DISTRIBUTEDFILE_SERVICE_PROXY_H #define DISTRIBUTEDFILE_SERVICE_PROXY_H -#include +#include "iremote_proxy.h" #include "i_distributedfile_service.h" #include "message_parcel.h" @@ -30,15 +30,18 @@ public: * * @param impl */ - explicit ServiceProxy(const sptr &impl) : IRemoteProxy(impl) {} + explicit ServiceProxy(const sptr &impl); - virtual ~ServiceProxy() = default; + virtual ~ServiceProxy(); int32_t SendFile(const std::string &cid, const std::vector &sourceFileList, const std::vector &destinationFileList, const uint32_t fileCount) override; - int32_t sendTest() override; + int32_t OpenFile(int32_t fd, const std::string &fileName, int32_t mode) override; + int32_t RegisterNotifyCallback(sptr &callback) override; + int32_t UnRegisterNotifyCallback() override; + int32_t IsDeviceOnline(const std::string &cid) override; private: static inline BrokerDelegator delegator_; }; diff --git a/frameworks/native/src/dfs_filetransfer_callback.cpp b/frameworks/native/src/dfs_filetransfer_callback.cpp new file mode 100644 index 000000000..19dc846e4 --- /dev/null +++ b/frameworks/native/src/dfs_filetransfer_callback.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dfs_filetransfer_callback.h" + +#include +#include "sendfile.h" +#include "utils_log.h" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +DfsFileTransferCallback::DfsFileTransferCallback() {} + +DfsFileTransferCallback::~DfsFileTransferCallback() {} + +int32_t DfsFileTransferCallback::DeviceOnline(const std::string &cid) +{ + NapiDeviceOnline(cid); + return 0; +} + +int32_t DfsFileTransferCallback::DeviceOffline(const std::string &cid) +{ + NapiDeviceOffline(cid); + return 0; +} + +int32_t DfsFileTransferCallback::SendFinished(const std::string &cid, std::string fileName) +{ + NapiSendFinished(cid, fileName); + return 0; +} + +int32_t DfsFileTransferCallback::SendError(const std::string &cid) +{ + NapiSendError(cid); + return 0; +} + +int32_t DfsFileTransferCallback::ReceiveFinished(const std::string &cid, const std::string &fileName, uint32_t num) +{ + NapiReceiveFinished(cid, fileName, num); + return 0; +} + +int32_t DfsFileTransferCallback::ReceiveError(const std::string &cid) +{ + NapiReceiveError(cid); + return 0; +} + +int32_t DfsFileTransferCallback::WriteFile(int32_t fd, const std::string &fileName) +{ + NapiWriteFile(fd, fileName); + return 0; +} +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/src/filetransfer_callback_proxy.cpp b/frameworks/native/src/filetransfer_callback_proxy.cpp new file mode 100644 index 000000000..39cf790bf --- /dev/null +++ b/frameworks/native/src/filetransfer_callback_proxy.cpp @@ -0,0 +1,204 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "filetransfer_callback_proxy.h" +#include "utils_log.h" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +FileTransferCallbackProxy::FileTransferCallbackProxy(const sptr &impl) + : IRemoteProxy(impl) {} + +FileTransferCallbackProxy::~FileTransferCallbackProxy() {} + +int32_t FileTransferCallbackProxy::DeviceOnline(const std::string &cid) +{ + + MessageParcel data; + if (!data.WriteInterfaceToken(FileTransferCallbackProxy::GetDescriptor())) { + LOGE("xhl [---FileTransferCallbackProxy---] WriteInterfaceToken failed"); + return DFS_CALLBACK_WRITE_DESCRIPTOR_TOKEN_FAIL; + } + + sptr remote = Remote(); + if (remote == nullptr) { + LOGE("xhl [---FileTransferCallbackProxy---] Remote is null"); + return ERR_NULL_OBJECT; + } + + data.WriteString(cid); + MessageParcel reply; + MessageOption option; + int32_t ret = remote->SendRequest(ON_DEVICE_ONLINE, data, reply, option); + if (ret != ERR_NONE) { + LOGE("[---FileTransferCallbackProxy---] Proxy SendRequest failed, ret code:[%{public}d]", ret); + } + return ret; +} + +int32_t FileTransferCallbackProxy::DeviceOffline(const std::string &cid) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(FileTransferCallbackProxy::GetDescriptor())) { + LOGE("xhl [---FileTransferCallbackProxy---] WriteInterfaceToken failed"); + return 0; + } + + sptr remote = Remote(); + if (remote == nullptr) { + LOGE("xhl [---FileTransferCallbackProxy---] Remote is null"); + return ERR_NULL_OBJECT; + } + + data.WriteString(cid); + MessageParcel reply; + MessageOption option; + int32_t ret = remote->SendRequest(ON_DEVICE_OFFLINE, data, reply, option); + if (ret != ERR_NONE) { + LOGE("[---FileTransferCallbackProxy---] Proxy SendRequest failed, ret code:[%{public}d]", ret); + } + return ret; +} + +int32_t FileTransferCallbackProxy::SendFinished(const std::string &cid, std::string fileName) +{ + LOGI("[---FileTransferCallbackProxy---] Proxy SendFinished"); + MessageParcel data; + if (!data.WriteInterfaceToken(FileTransferCallbackProxy::GetDescriptor())) { + LOGE("[---FileTransferCallbackProxy---] WriteInterfaceToken failed"); + return 0; + } + + sptr remote = Remote(); + if (remote == nullptr) { + LOGE("[---FileTransferCallbackProxy---] Remote is null"); + return ERR_NULL_OBJECT; + } + + data.WriteString(cid); + data.WriteString(fileName); + MessageParcel reply; + MessageOption option; + int32_t ret = remote->SendRequest(ON_SEND_FINISHED, data, reply, option); + if (ret != ERR_NONE) { + LOGE("[---FileTransferCallbackProxy---] Proxy SendRequest failed, ret code:[%{public}d]", ret); + } + return ret; +} + +int32_t FileTransferCallbackProxy::SendError(const std::string &cid) +{ + LOGI("[---FileTransferCallbackProxy---] Proxy SendError"); + MessageParcel data; + if (!data.WriteInterfaceToken(FileTransferCallbackProxy::GetDescriptor())) { + LOGE("[---FileTransferCallbackProxy---] WriteInterfaceToken failed"); + return 0; + } + + sptr remote = Remote(); + if (remote == nullptr) { + LOGE("[---FileTransferCallbackProxy---] Remote is null"); + return ERR_NULL_OBJECT; + } + + data.WriteString(cid); + MessageParcel reply; + MessageOption option; + int32_t ret = remote->SendRequest(ON_SEND_ERROR, data, reply, option); + if (ret != ERR_NONE) { + LOGE("[---FileTransferCallbackProxy---] Proxy SendRequest failed, ret code:[%{public}d]", ret); + } + return ret; +} + +int32_t FileTransferCallbackProxy::ReceiveFinished(const std::string &cid, const std::string &fileName, uint32_t num) +{ + LOGI("[---FileTransferCallbackProxy---] Proxy ReceiveFinished, %{public}s", cid.c_str()); + MessageParcel data; + if (!data.WriteInterfaceToken(FileTransferCallbackProxy::GetDescriptor())) { + LOGE("[---FileTransferCallbackProxy---] WriteInterfaceToken failed"); + return 0; + } + + sptr remote = Remote(); + if (remote == nullptr) { + LOGE("[---FileTransferCallbackProxy---] Remote is null"); + return ERR_NULL_OBJECT; + } + + data.WriteString(cid); + data.WriteString(fileName); + data.WriteUint32(num); + MessageParcel reply; + MessageOption option; + int32_t ret = remote->SendRequest(ON_RECEIVE_FINISHED, data, reply, option); + if (ret != ERR_NONE) { + LOGE("[---FileTransferCallbackProxy---] Proxy SendRequest failed, ret code:[%{public}d]", ret); + } + return ret; +} + +int32_t FileTransferCallbackProxy::ReceiveError(const std::string &cid) +{ + LOGI("[---FileTransferCallbackProxy---] Proxy ReceiveError"); + MessageParcel data; + if (!data.WriteInterfaceToken(FileTransferCallbackProxy::GetDescriptor())) { + LOGE("WriteInterfaceToken failed"); + return 0; + } + + sptr remote = Remote(); + if (remote == nullptr) { + LOGE("[---FileTransferCallbackProxy---] Remote is null"); + return ERR_NULL_OBJECT; + } + + data.WriteString(cid); + MessageParcel reply; + MessageOption option; + int32_t ret = remote->SendRequest(ON_RECEIVE_ERROR, data, reply, option); + if (ret != ERR_NONE) { + LOGE("[---FileTransferCallbackProxy---] Proxy SendRequest failed, ret code:[%{public}d]", ret); + } + return ret; +} + +int32_t FileTransferCallbackProxy::WriteFile(int32_t fd, const std::string &fileName) +{ + LOGI("xhl WriteFile enter"); + LOGI("xhl WriteFile fd %{public}d, %{public}s", fd, fileName.c_str()); + MessageParcel data; + if (!data.WriteInterfaceToken(FileTransferCallbackProxy::GetDescriptor())) { + return DFS_CALLBACK_WRITE_DESCRIPTOR_TOKEN_FAIL; + } + + data.WriteFileDescriptor(fd); + data.WriteString(fileName); + + if (Remote() == nullptr) { + LOGE("xhl WriteFile remote object address is null"); + return DFS_CALLBACK_REMOTE_ADDRESS_IS_NULL; + } + + MessageParcel reply; + MessageOption option; + Remote()->SendRequest(ON_WRITE_FILE, data, reply, option); + LOGI("xhl WriteFile end"); + return reply.ReadInt32(); +} +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/src/filetransfer_callback_stub.cpp b/frameworks/native/src/filetransfer_callback_stub.cpp new file mode 100644 index 000000000..91eaef2f8 --- /dev/null +++ b/frameworks/native/src/filetransfer_callback_stub.cpp @@ -0,0 +1,158 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "filetransfer_callback_stub.h" + +#include +#include "utils_log.h" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +FileTransferCallbackStub::FileTransferCallbackStub() +{ + memberFuncMap_[ON_SEND_FINISHED] = &FileTransferCallbackStub::CmdOnSendFinished; + memberFuncMap_[ON_SEND_ERROR] = &FileTransferCallbackStub::CmdOnSendError; + memberFuncMap_[ON_RECEIVE_FINISHED] = &FileTransferCallbackStub::CmdOnReceiveFinished; + memberFuncMap_[ON_RECEIVE_ERROR] = &FileTransferCallbackStub::CmdOnReceiveError; + memberFuncMap_[ON_DEVICE_ONLINE] = &FileTransferCallbackStub::CmdOnDeviceOnline; + memberFuncMap_[ON_DEVICE_OFFLINE] = &FileTransferCallbackStub::CmdOnDeviceOffline; + memberFuncMap_[ON_WRITE_FILE] = &FileTransferCallbackStub::CmdWriteFile; +} + +FileTransferCallbackStub::~FileTransferCallbackStub() {} + +int32_t FileTransferCallbackStub::OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + std::u16string myDescripter = FileTransferCallbackStub::GetDescriptor(); + std::u16string remoteDescripter = data.ReadInterfaceToken(); + if (myDescripter != remoteDescripter) { + LOGE("Descriptor checked failed"); + return DFS_CALLBACK_DESCRIPTOR_IS_EMPTY; + } + + auto itFunc = memberFuncMap_.find(code); + if (itFunc != memberFuncMap_.end()) { + auto requestFunc = itFunc->second; + if (requestFunc != nullptr) { + return (this->*requestFunc)(data, reply); + } + } + + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); +} + +int32_t FileTransferCallbackStub::CmdOnDeviceOnline(MessageParcel &data, MessageParcel &reply) +{ + std::string cid = data.ReadString(); + int32_t result = DeviceOnline(cid); + if (!reply.WriteInt32(result)) { + LOGE("Write parcel failed"); + return result; + } + + return ERR_NONE; +} + +int32_t FileTransferCallbackStub::CmdOnDeviceOffline(MessageParcel &data, MessageParcel &reply) +{ + std::string cid = data.ReadString(); + int32_t result = DeviceOffline(cid); + if (!reply.WriteInt32(result)) { + LOGE("Write parcel failed"); + return result; + } + + return ERR_NONE; +} + +int32_t FileTransferCallbackStub::CmdOnSendFinished(MessageParcel &data, MessageParcel &reply) +{ + std::string cid = data.ReadString(); + std::string fileName = data.ReadString(); + int32_t result = SendFinished(cid, fileName); + if (!reply.WriteInt32(result)) { + LOGE("Write parcel failed"); + return result; + } + + return ERR_NONE; +} + +int32_t FileTransferCallbackStub::CmdOnSendError(MessageParcel &data, MessageParcel &reply) +{ + std::string cid = data.ReadString(); + int32_t result = SendError(cid); + if (!reply.WriteInt32(result)) { + LOGE("Write parcel failed"); + return result; + } + + return ERR_NONE; +} + +int32_t FileTransferCallbackStub::CmdOnReceiveFinished(MessageParcel &data, MessageParcel &reply) +{ + std::string cid = data.ReadString(); + std::string fileName = data.ReadString(); + uint32_t num = data.ReadUint32(); + int32_t result = ReceiveFinished(cid, fileName, num); + if (!reply.WriteInt32(result)) { + LOGE("Write parcel failed"); + return result; + } + + return ERR_NONE; +} + +int32_t FileTransferCallbackStub::CmdOnReceiveError(MessageParcel &data, MessageParcel &reply) +{ + std::string cid = data.ReadString(); + int32_t result = ReceiveError(cid); + if (!reply.WriteInt32(result)) { + LOGE("Write parcel failed"); + return result; + } + + return ERR_NONE; +} + +int32_t FileTransferCallbackStub::CmdWriteFile(MessageParcel &data, MessageParcel &reply) +{ + int32_t fileData = data.ReadFileDescriptor(); + int32_t fd = fcntl(fileData, F_GETFD, 0); + if (fd < 0) { + reply.WriteInt32(DFS_CALLBACK_ERR_SET_FD_FAIL); + LOGE("FileTransferCallbackStub : Failed to get app device id, error: invalid device id"); + return DFS_CALLBACK_ERR_SET_FD_FAIL; + } + + std::string fileName = data.ReadString(); + if (fileName.empty()) { + LOGE("FileTransferCallbackStub : Failed to get app device id, error: invalid device id"); + return DFS_CALLBACK_DESCRIPTOR_IS_EMPTY; + } + + int32_t result = WriteFile(fileData, fileName); + if (!reply.WriteInt32(result)) { + LOGE("fail to write parcel"); + return DFS_CALLBACK_WRITE_REPLY_FAIL; + } + + return ERR_NONE; +} +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/src/service_proxy.cpp b/frameworks/native/src/service_proxy.cpp new file mode 100644 index 000000000..746a12a08 --- /dev/null +++ b/frameworks/native/src/service_proxy.cpp @@ -0,0 +1,168 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "service_proxy.h" +#include "utils_log.h" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +ServiceProxy::ServiceProxy(const sptr &impl) : IRemoteProxy(impl) {} + +ServiceProxy::~ServiceProxy() {} + +int32_t ServiceProxy::SendFile(const std::string &cid, + const std::vector &sourceFileList, + const std::vector &destinationFileList, + const uint32_t fileCount) +{ + LOGI("xhl sendFile enter"); + MessageParcel data; + if (!data.WriteInterfaceToken(ServiceProxy::GetDescriptor())) { + return DFS_WRITE_DESCRIPTOR_TOKEN_FAIL; + } + data.WriteString(cid); + int32_t sourceListNumber = sourceFileList.size(); + data.WriteInt32(sourceListNumber); + for (int32_t index = 0; index < sourceListNumber; ++index) { + data.WriteString(sourceFileList.at(index)); + } + int32_t destinationListNumber = destinationFileList.size(); + data.WriteInt32(destinationListNumber); + for (int32_t index = 0; index < destinationListNumber; ++index) { + data.WriteString(destinationFileList.at(index)); + } + data.WriteUint32(fileCount); + + if (Remote() == nullptr) { + LOGE("xhl unregister sendfile remote object address is null"); + return DFS_REMOTE_ADDRESS_IS_NULL; + } + + MessageParcel reply; + MessageOption option; + int32_t result = Remote()->SendRequest(SEND_FILE_DISTRIBUTED, data, reply, option); + if (result != DFS_SENDFILE_SUCCESS) { + LOGE("xhl sendfile error code : %{public}d", result); + return result; + } + LOGE("xhl sendfile sendRequest done %{public}d", result); + + return reply.ReadInt32(); +} + +int32_t ServiceProxy::OpenFile(int32_t fd, const std::string &fileName, int32_t mode) +{ + LOGI("xhl OpenFile enter"); + LOGI("xhl OpenFile fd %{public}d", fd); + MessageParcel data; + if (!data.WriteInterfaceToken(ServiceProxy::GetDescriptor())) { + return DFS_WRITE_DESCRIPTOR_TOKEN_FAIL; + } + + data.WriteFileDescriptor(fd); + data.WriteString(fileName); + data.WriteInt32(mode); + + if (Remote() == nullptr) { + LOGE("xhl openfile remote object address is null"); + return DFS_REMOTE_ADDRESS_IS_NULL; + } + + MessageParcel reply; + MessageOption option; + Remote()->SendRequest(OPEN_FILE_FD, data, reply, option); + LOGI("xhl RegisterNotifyCallback end"); + return reply.ReadInt32(); +} + +int32_t ServiceProxy::RegisterNotifyCallback(sptr &callback) +{ + LOGI("xhl OpenFile enter"); + MessageParcel data; + if (!data.WriteInterfaceToken(ServiceProxy::GetDescriptor())) { + return DFS_WRITE_DESCRIPTOR_TOKEN_FAIL; + } + + if (callback == nullptr) { + LOGE("xhl The parameter of callback is nullptr"); + return DFS_CALLBACK_PARAM_ERROR; + } + + if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) { + LOGE("xhl write remote object failed"); + return DFS_WRITE_REMOTE_OBJECT_FAIL; + } + + if (Remote() == nullptr) { + LOGE("xhl register filetransfer callback remote object address is null"); + return DFS_REMOTE_ADDRESS_IS_NULL; + } + + MessageParcel reply; + MessageOption option; + Remote()->SendRequest(REGISTER_NOTIFY_CALLBACK, data, reply, option); + LOGI("xhl RegisterNotifyCallback end"); + return reply.ReadInt32(); +} + +int32_t ServiceProxy::UnRegisterNotifyCallback() +{ + LOGI("xhl UnRegisterNotifyCallback enter"); + MessageParcel data; + if (!data.WriteInterfaceToken(ServiceProxy::GetDescriptor())) { + return DFS_WRITE_DESCRIPTOR_TOKEN_FAIL; + } + + if (Remote() == nullptr) { + LOGE("xhl unregister filetransfer callback remote object address is null"); + return DFS_REMOTE_ADDRESS_IS_NULL; + } + + MessageParcel reply; + MessageOption option; + Remote()->SendRequest(UN_REGISTER_NOTIFY_CALLBACK, data, reply, option); + LOGI("xhl UnRegisterNotifyCallback end"); + return reply.ReadInt32(); +} + +int32_t ServiceProxy::IsDeviceOnline(const std::string &cid) +{ + LOGE("xhl IsDeviceOnline enter"); + MessageParcel data; + if (!data.WriteInterfaceToken(ServiceProxy::GetDescriptor())) { + return DFS_WRITE_DESCRIPTOR_TOKEN_FAIL; + } + data.WriteString(cid); + + if (Remote() == nullptr) { + LOGE("Remote object address is null"); + return DFS_REMOTE_ADDRESS_IS_NULL; + } + + MessageParcel reply; + MessageOption option; + int32_t result = Remote()->SendRequest(IS_DEVICE_ONLINE, data, reply, option); + if (result != DFS_NO_ERROR) { + LOGE("Function RemoveBundleDistributedDirs! errCode:%{public}d", result); + return DFS_NO_DEVICE_ONLINE; + } + LOGE("xhl IsDeviceOnline done %{public}d", result); + + return reply.ReadInt32(); +} +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/innerkits/kits/js/@ohos.sendfile.d.ts b/interfaces/innerkits/kits/js/@ohos.sendfile.d.ts new file mode 100644 index 000000000..ef88807e3 --- /dev/null +++ b/interfaces/innerkits/kits/js/@ohos.sendfile.d.ts @@ -0,0 +1,60 @@ +/* +* Copyright (C) 2021 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import {AsyncCallback, Callback} from "./basic"; +/** + * Provides Environment APIs. + * + * @since 8 + * @sysCap SystemCapability.FileManagement.File.DistributeFile + */ +declare namespace SendFile { + /** + * send file. + * + * @since 8 + */ + function sendFile(deviceId: string, sourPath: Array, destPath: Array, fileCount: number, callback: AsyncCallback); + function sendFile(deviceId: string, sourPath: Array, destPath: Array, fileCount: number): Promise; + + export interface TransResult { + /** + * send file error code. + */ + errCode: number; + /** + * receive file full path. + */ + fileName: Array; + /** + * receive file count. + */ + fileCount: number; + } + /** + * event listen on. + * + * @since 8 + */ + function on(type: 'sendFinished' | 'receiveFinished', callback: AsyncCallback): void; + /** + * event listen off. + * + * @since 8 + */ + function off(type: 'sendFinished' | 'receiveFinished'): void; +} + +export default SendFile; \ No newline at end of file diff --git a/interfaces/innerkits/native/BUILD.gn b/interfaces/innerkits/native/BUILD.gn index 06431810e..a08cf85a6 100644 --- a/interfaces/innerkits/native/BUILD.gn +++ b/interfaces/innerkits/native/BUILD.gn @@ -14,9 +14,15 @@ import("//build/ohos.gni") import("//foundation/filemanagement/dfs_service/distributedfile.gni") config("private_config") { - include_dirs = [ "../../../frameworks/native" ] + include_dirs = [ + "../../../frameworks/native/include" + ] - configs = [ "//build/config/compiler:exceptions" ] + # configs = [ "//build/config/compiler:exceptions" ] + defines = [ + "LOG_TAG=\"distributedfile\"", + "LOG_DOMAIN=0xD001600", + ] } config("public_config") { @@ -24,7 +30,7 @@ config("public_config") { } ohos_shared_library("libdistributedfile_innerkits") { - sources = [ "../../../frameworks/native/service_proxy.cpp" ] + sources = [ "${distributedfile_path}/frameworks/native/src/service_proxy.cpp" ] configs = [ ":private_config" ] diff --git a/interfaces/innerkits/native/i_distributedfile_service.h b/interfaces/innerkits/native/i_distributedfile_service.h index 725725020..c77f0667a 100644 --- a/interfaces/innerkits/native/i_distributedfile_service.h +++ b/interfaces/innerkits/native/i_distributedfile_service.h @@ -17,6 +17,7 @@ #define I_DISTRIBUTEDFILE_SERVICE_H #include "iremote_broker.h" +#include "i_filetransfer_callback.h" namespace OHOS { namespace Storage { @@ -25,33 +26,40 @@ class IDistributedFileService : public IRemoteBroker { public: DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedFile.IDistributedFileService"); // define the message code - enum DistributedFileSurfaceCode { INTERFACE1 = 0, SEND_FILE_DISTRIBUTED, TEST_CODE }; + enum DistributedFileSurfaceCode { + INTERFACE1 = 0, + SEND_FILE_DISTRIBUTED, + OPEN_FILE_FD, + REGISTER_NOTIFY_CALLBACK, + UN_REGISTER_NOTIFY_CALLBACK, + IS_DEVICE_ONLINE + }; // define the error code enum { DISTRIBUTEDFILE_SUCCESS = 0, + DFS_SUCCESS = 0, + DFS_NO_ERROR = 0, + DFS_SENDFILE_SUCCESS = 0, GET_DISTRIBUTEDFILE_DISTRIBUTED_DIR_FAIL = 1, - DISTRIBUTEDFILE_WRITE_DESCRIPTOR_TOKEN_FAIL, - ERR_FLATTEN_OBJECT, - DISTRIBUTEDFILE_NO_ERROR, - DISTRIBUTEDFILE_CONNECT_SYSTEM_ABILITY_STUB_FAIL, - REMOVE_DISTRIBUTEDFILE_DISTRIBUTEDDIRS_FAIL, - DISTRIBUTEDFILE_BAD_TYPE, - DISTRIBUTEDFILE_FAIL, - DISTRIBUTEDFILE_REMOTE_ADDRESS_IS_NULL, - DISTRIBUTEDFILE_WRITE_REPLY_FAIL, - DISTRIBUTEDFILE_DIR_NAME_IS_EMPTY, - DISTRIBUTEDFILE_NAME_NOT_FOUND, - DISTRIBUTEDFILE_PERMISSION_DENIED, - ROOT_UID, - SYSTEM_SERVICE_UID, - SEND_FILE_FAIL, - SEND_FILE_DISTRIBUTED_DESCRIPTION_FAIL + DFS_REMOTE_ADDRESS_IS_NULL, + DFS_DESCRIPTOR_IS_EMPTY, + DFS_SESSION_ID_IS_EMPTY, + DFS_WRITE_REPLY_FAIL, + DFS_WRITE_REMOTE_OBJECT_FAIL, + DFS_WRITE_DESCRIPTOR_TOKEN_FAIL, + DFS_CALLBACK_PARAM_ERROR, + DFS_NO_SUCH_FILE, + DFS_NO_DEVICE_ONLINE, + DFS_SET_FD_FAIL }; virtual int32_t SendFile(const std::string &cid, const std::vector &sourceFileList, const std::vector &destinationFileList, const uint32_t fileCount) = 0; - virtual int32_t sendTest() = 0; + virtual int32_t OpenFile(int32_t fd, const std::string &fileName, int32_t mode) = 0; + virtual int32_t RegisterNotifyCallback(sptr &callback) = 0; + virtual int32_t UnRegisterNotifyCallback() = 0; + virtual int32_t IsDeviceOnline(const std::string &cid) = 0; }; } // namespace DistributedFile } // namespace Storage diff --git a/interfaces/innerkits/native/i_filetransfer_callback.h b/interfaces/innerkits/native/i_filetransfer_callback.h new file mode 100644 index 000000000..4f71696ad --- /dev/null +++ b/interfaces/innerkits/native/i_filetransfer_callback.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef I_FILETRANSFER_CALLBACK_H +#define I_FILETRANSFER_CALLBACK_H + +#include + +#include "iremote_broker.h" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +class IFileTransferCallback : public IRemoteBroker { +public: + virtual ~IFileTransferCallback() = default; +public: + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Storage.DistributedFile.IFileTransferCallback"); + enum { + ON_SEND_FINISHED = 0, + ON_SEND_ERROR, + ON_RECEIVE_FINISHED, + ON_RECEIVE_ERROR, + ON_DEVICE_ONLINE, + ON_DEVICE_OFFLINE, + ON_WRITE_FILE + }; + // define the error code + enum { + DFS_CALLBACK_SUCCESS = 0, + DFS_CALLBACK_REMOTE_ADDRESS_IS_NULL, + DFS_CALLBACK_DESCRIPTOR_IS_EMPTY, + DFS_CALLBACK_WRITE_REPLY_FAIL, + DFS_CALLBACK_WRITE_REMOTE_OBJECT_FAIL, + DFS_CALLBACK_WRITE_DESCRIPTOR_TOKEN_FAIL, + DFS_CALLBACK_ERR_SET_FD_FAIL + }; +public: + virtual int32_t SendFinished(const std::string &cid, std::string fileName) = 0; + virtual int32_t SendError(const std::string &cid) = 0; + virtual int32_t ReceiveFinished(const std::string &cid, const std::string &fileName, uint32_t num) = 0; + virtual int32_t ReceiveError(const std::string &cid) = 0; + virtual int32_t DeviceOnline(const std::string &cid) = 0; + virtual int32_t DeviceOffline(const std::string &cid) = 0; + virtual int32_t WriteFile(int32_t fd, const std::string &fileName) = 0; +}; +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS +#endif // I_FILETRANSFER_CALLBACK_H \ No newline at end of file diff --git a/services/distributedfiledaemon/include/device/device_manager_agent.h b/services/distributedfiledaemon/include/device/device_manager_agent.h index 27617ac03..2e276027d 100644 --- a/services/distributedfiledaemon/include/device/device_manager_agent.h +++ b/services/distributedfiledaemon/include/device/device_manager_agent.h @@ -54,7 +54,7 @@ class DeviceManagerAgent final : public DistributedHardware::DmInitCallback, public Startable, public Actor, public Utils::Singleton { - DECLARE_SINGLETON(DeviceManagerAgent); + DECLARE_UTILS_SINGLETON(DeviceManagerAgent); public: void Start() override; diff --git a/services/distributedfiledaemon/include/mountpoint/mount_manager.h b/services/distributedfiledaemon/include/mountpoint/mount_manager.h index 7b04f10c3..2d3b2939f 100644 --- a/services/distributedfiledaemon/include/mountpoint/mount_manager.h +++ b/services/distributedfiledaemon/include/mountpoint/mount_manager.h @@ -31,7 +31,7 @@ public: void Mount(std::unique_ptr mp); void Umount(std::weak_ptr wmp); void Umount(const std::string &groupId); - DECLARE_SINGLETON(MountManager); + DECLARE_UTILS_SINGLETON(MountManager); private: void StartInstance() override {} diff --git a/services/distributedfileservice/BUILD.gn b/services/distributedfileservice/BUILD.gn index a98b9383f..ecc81370a 100644 --- a/services/distributedfileservice/BUILD.gn +++ b/services/distributedfileservice/BUILD.gn @@ -15,27 +15,40 @@ import("//foundation/filemanagement/dfs_service/distributedfile.gni") ohos_shared_library("libdistributedfileservice") { include_dirs = [ - "include/ipc", "include/device", + "include/ipc", "include/network", + "${distributedfile_path}/frameworks/native", + "//foundation/ace/napi/interfaces/kits", + "//third_party/node/src", + "//third_party/libuv/include", "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", + "//foundation/filemanagement/dfs_service/frameworks/native/include", + "//foundation/filemanagement/dfs_service/frameworks/native/native_module_sendfile", + "//foundation/filemanagement/dfs_service/frameworks/js/napi/common", ] sources = [ - "src/device/device_manager_agent.cpp", "src/ipc/distributedfile_service.cpp", "src/ipc/distributedfile_service_stub.cpp", + "src/device/device_manager_agent.cpp", "src/network/softbus_agent.cpp", "src/network/softbus_dispatcher.cpp", + "${distributedfile_path}/frameworks/native/src/service_proxy.cpp", + "${distributedfile_path}/frameworks/native/src/filetransfer_callback_proxy.cpp", ] configs = [ "${utils_path}:compiler_configs" ] external_deps = [ - "dsoftbus_standard:softbus_client", + "ability_runtime:app_manager", + "ability_runtime:want", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", "ipc:ipc_core", "safwk:system_ability_fwk", "samgr_standard:samgr_proxy", + "dsoftbus_standard:softbus_client", ] deps = [ diff --git a/services/distributedfileservice/include/device/device_manager_agent.h b/services/distributedfileservice/include/device/device_manager_agent.h index b2a11da1a..df2cd1fbf 100644 --- a/services/distributedfileservice/include/device/device_manager_agent.h +++ b/services/distributedfileservice/include/device/device_manager_agent.h @@ -33,7 +33,7 @@ class DeviceManagerAgent : public DistributedHardware::DmInitCallback, public DistributedHardware::DeviceStateCallback, public std::enable_shared_from_this, public Utils::Singleton { - DECLARE_SINGLETON(DeviceManagerAgent); + DECLARE_UTILS_SINGLETON(DeviceManagerAgent); public: void OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo) override; @@ -57,6 +57,8 @@ private: std::mutex devsRecordMutex_; std::set alreadyOnlineDev_; + + std::string pkgName_{"ohos.storage.distributedfile.service"}; }; } // namespace DistributedFile } // namespace Storage diff --git a/services/distributedfileservice/include/ipc/distributedfile_service.h b/services/distributedfileservice/include/ipc/distributedfile_service.h index 8bca0a920..70c2dcbd9 100644 --- a/services/distributedfileservice/include/ipc/distributedfile_service.h +++ b/services/distributedfileservice/include/ipc/distributedfile_service.h @@ -18,19 +18,21 @@ #include "distributedfile_service_stub.h" #include "iremote_stub.h" +#include "singleton.h" #include "system_ability.h" +#include + namespace OHOS { namespace Storage { namespace DistributedFile { class DistributedFileService : public SystemAbility, public DistributedFileServiceStub, public std::enable_shared_from_this { + DECLARE_DELAYED_SINGLETON(DistributedFileService) DECLARE_SYSTEM_ABILITY(DistributedFileService) public: - DistributedFileService(int32_t saID, bool runOnCreate) : SystemAbility(saID, runOnCreate) {}; - ~DistributedFileService() {}; - + explicit DistributedFileService(int32_t saId, bool runOnCreate = true) : SystemAbility(saId, runOnCreate) {} void OnDump() override; void OnStart() override; void OnStop() override; @@ -39,7 +41,10 @@ public: const std::vector &sourceFileList, const std::vector &destinationFileList, const uint32_t fileCount) override; - int32_t sendTest() override; + int32_t OpenFile(int32_t fd, const std::string &fileName, int32_t mode) override; + int32_t RegisterNotifyCallback(sptr &callback) override; + int32_t UnRegisterNotifyCallback() override; + int32_t IsDeviceOnline(const std::string &cid) override; static inline const std::string pkgName_ { "ohos.storage.distributedfile.service" }; private: @@ -49,5 +54,4 @@ private: } // namespace DistributedFile } // namespace Storage } // namespace OHOS - #endif // DISTRIBUTEDFILE_SERVICE_H diff --git a/services/distributedfileservice/include/ipc/distributedfile_service_stub.h b/services/distributedfileservice/include/ipc/distributedfile_service_stub.h index a4c18bb51..136ba3235 100644 --- a/services/distributedfileservice/include/ipc/distributedfile_service_stub.h +++ b/services/distributedfileservice/include/ipc/distributedfile_service_stub.h @@ -37,7 +37,9 @@ private: MessageParcel &reply); std::map memberFuncMap_; int32_t SendFileStub(MessageParcel &data, MessageParcel &reply); - int test(MessageParcel &data, MessageParcel &reply); + int32_t OpenFileStub(MessageParcel &data, MessageParcel &reply); + int32_t CmdRegisterNotifyCallback(MessageParcel &data, MessageParcel &reply); + int32_t CmdIsDeviceOnline(MessageParcel &data, MessageParcel &reply); }; } // namespace DistributedFile } // namespace Storage diff --git a/services/distributedfileservice/include/network/softbus_agent.h b/services/distributedfileservice/include/network/softbus_agent.h index ec93ec966..eac1dbb77 100644 --- a/services/distributedfileservice/include/network/softbus_agent.h +++ b/services/distributedfileservice/include/network/softbus_agent.h @@ -16,6 +16,8 @@ #ifndef DFS_SOFTBUS_AGENT_H #define DFS_SOFTBUS_AGENT_H +#include "utils_singleton.h" +#include "i_filetransfer_callback.h" #include #include #include @@ -25,7 +27,7 @@ namespace OHOS { namespace Storage { namespace DistributedFile { class SoftbusAgent : public std::enable_shared_from_this, public Utils::Singleton { - DECLARE_SINGLETON(SoftbusAgent); + DECLARE_UTILS_SINGLETON(SoftbusAgent); public: void RegisterSessionListener(); @@ -40,6 +42,8 @@ public: int OnSendFileFinished(const int sessionId, const std::string firstFile); int OnFileTransError(const int sessionId); void OnReceiveFileFinished(const int sessionId, const std::string files, int fileCnt); + void SetTransCallback(sptr &callback); + void RemoveTransCallbak(); protected: void StartInstance() override; @@ -55,6 +59,7 @@ private: std::mutex getSessionCVMut_; std::condition_variable getSessionCV_; + sptr notifyCallback_ = nullptr; }; } // namespace DistributedFile } // namespace Storage diff --git a/services/distributedfileservice/src/ipc/distributedfile_service.cpp b/services/distributedfileservice/src/ipc/distributedfile_service.cpp index 8bb1fb7d4..a00a1f0a1 100644 --- a/services/distributedfileservice/src/ipc/distributedfile_service.cpp +++ b/services/distributedfileservice/src/ipc/distributedfile_service.cpp @@ -14,32 +14,61 @@ */ #include "distributedfile_service.h" +#include +#include +#include +#include #include -#include "device_manager_agent.h" -#include "utils_exception.h" + +#include "bundle_mgr_interface.h" +#include "bundle_mgr_proxy.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "message_parcel.h" +#include "parcel.h" +#include "utils_directory.h" #include "utils_log.h" + #include "softbus_agent.h" +#include "device_manager_agent.h" namespace OHOS { namespace Storage { namespace DistributedFile { -using namespace std; +namespace { + constexpr int32_t FILE_BLOCK_SIZE = 1024; + constexpr char TEMP_FILE_NAME[] = "/data/system_ce/tmp"; +} REGISTER_SYSTEM_ABILITY_BY_ID(DistributedFileService, STORAGE_DISTRIBUTED_FILE_SERVICE_SA_ID, false); +DistributedFileService::DistributedFileService() : SystemAbility(STORAGE_DISTRIBUTED_FILE_SERVICE_SA_ID, false) {} + +DistributedFileService::~DistributedFileService() {} + void DistributedFileService::OnDump() { LOGI("OnDump"); } +void DistributedFileService::OnStart() +{ + PublishSA(); + StartManagers(); +} + +void DistributedFileService::OnStop() +{ + LOGI("OnStop"); +} + void DistributedFileService::PublishSA() { - LOGI("Begin to init, pull the service SA"); - bool ret = SystemAbility::Publish(this); + bool ret = SystemAbility::Publish(DelayedSingleton::GetInstance().get()); if (!ret) { - throw runtime_error("publishing DistributedFileService failed"); + LOGE("Leave, publishing DistributedFileService failed!"); + return; } - LOGI("Init finished successfully"); } void DistributedFileService::StartManagers() @@ -47,33 +76,107 @@ void DistributedFileService::StartManagers() DeviceManagerAgent::GetInstance(); } -void DistributedFileService::OnStart() +int32_t DistributedFileService::SendFile(const std::string &cid, + const std::vector &sourceFileList, + const std::vector &destinationFileList, + const uint32_t fileCount) { - LOGI("DistributedFileService::OnStart"); - try { - PublishSA(); - StartManagers(); - } catch (const Exception &e) { - LOGE("%{public}s", e.what()); + 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(); + sFileList[index] = new char[length + 1]; + memset_s(sFileList[index], length + 1, '\0', length + 1); + memcpy_s(sFileList[index], length + 1, tmpString.c_str(), length); + sFileList[index][length] = '\0'; + } else { + int32_t length = sourceFileList.at(index).length(); + sFileList[index] = new char[length + 1]; + memset_s(sFileList[index], length + 1, '\0', length + 1); + memcpy_s(sFileList[index], length + 1, sourceFileList.at(index).c_str(), length); + sFileList[index][length] = '\0'; + } } + + char **dFileList = new char*[fileCount]; + for (int index = 0; index < destinationFileList.size(); ++index) { + LOGI("DistributedFileService::SendFile Destination File List %{public}d, %{public}s", + index, destinationFileList.at(index).c_str()); + int32_t length = destinationFileList.at(index).length(); + dFileList[index] = new char[length + 1]; + memset_s(dFileList[index], length + 1, '\0', length + 1); + memcpy_s(dFileList[index], length + 1, destinationFileList.at(index).c_str(), length); + dFileList[index][length] = '\0'; + } + + auto softBusAgent = SoftbusAgent::GetInstance(); + int32_t result = softBusAgent->SendFile(cid, (const char **)sFileList, (const char **)dFileList, fileCount); + return result; } -void DistributedFileService::OnStop() +int32_t DistributedFileService::OpenFile(int32_t fd, const std::string &fileName, int32_t mode) { - LOGI("DistributedFileService::OnStop"); + if (fd <= 0) { + return -1; + } + + const char *tmpFile = TEMP_FILE_NAME; + struct stat fileStat; + bool fileExist = (stat(tmpFile, &fileStat) == 0); + if (fileExist) { + int32_t result = remove(tmpFile); + if (result != 0) { + LOGE("DFS SA remove temp file result %{public}d, %{public}s, %{public}d", result, strerror(errno), errno); + return -1; + } + } + + int32_t writeFd = open(tmpFile, O_WRONLY | O_CREAT, (S_IREAD & S_IWRITE) | S_IRGRP | S_IROTH); + if (writeFd <= 0) { + LOGE("DFS SA open temp file failed %{public}d, %{public}s, %{public}d", writeFd, strerror(errno), errno); + return -1; + } + auto buffer = std::make_unique(FILE_BLOCK_SIZE); + ssize_t actLen = 0; + do { + actLen = read(fd, buffer.get(), FILE_BLOCK_SIZE); + if (actLen > 0) { + write(writeFd, buffer.get(), actLen); + } else if (actLen == 0) { + break; + } else { + if (errno == EINTR) { + actLen = FILE_BLOCK_SIZE; + } else { + return -1; + } + } + } while (actLen > 0); + close(writeFd); + return 0; } -int32_t DistributedFileService::sendTest() +int32_t DistributedFileService::RegisterNotifyCallback(sptr &callback) { - LOGI("DistributedFileService::sendTest"); + SoftbusAgent::GetInstance()->SetTransCallback(callback); return 0; } -int32_t DistributedFileService::SendFile(const std::string &cid, - const std::vector &sourceFileList, - const std::vector &destinationFileList, - const uint32_t fileCount) +int32_t DistributedFileService::UnRegisterNotifyCallback() { + SoftbusAgent::GetInstance()->RemoveTransCallbak(); + return 0; +} + +int32_t DistributedFileService::IsDeviceOnline(const std::string &cid) +{ + std::set onlineDevice = DeviceManagerAgent::GetInstance()->getOnlineDevs(); + if (onlineDevice.find(cid) != onlineDevice.end()) { + return 1; + } return 0; } } // namespace DistributedFile diff --git a/services/distributedfileservice/src/ipc/distributedfile_service_stub.cpp b/services/distributedfileservice/src/ipc/distributedfile_service_stub.cpp index 793918816..8f0195bd7 100644 --- a/services/distributedfileservice/src/ipc/distributedfile_service_stub.cpp +++ b/services/distributedfileservice/src/ipc/distributedfile_service_stub.cpp @@ -15,9 +15,11 @@ #include "distributedfile_service_stub.h" -#include +#include +#include #include +#include "ipc_skeleton.h" #include "utils_log.h" namespace OHOS { @@ -26,7 +28,9 @@ namespace DistributedFile { DistributedFileServiceStub::DistributedFileServiceStub() { memberFuncMap_[SEND_FILE_DISTRIBUTED] = &DistributedFileServiceStub::SendFileStub; - memberFuncMap_[TEST_CODE] = &DistributedFileServiceStub::test; + memberFuncMap_[OPEN_FILE_FD] = &DistributedFileServiceStub::OpenFileStub; + memberFuncMap_[REGISTER_NOTIFY_CALLBACK] = &DistributedFileServiceStub::CmdRegisterNotifyCallback; + memberFuncMap_[IS_DEVICE_ONLINE] = &DistributedFileServiceStub::CmdIsDeviceOnline; } DistributedFileServiceStub::~DistributedFileServiceStub() @@ -39,7 +43,13 @@ int DistributedFileServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &reply, MessageOption &option) { - LOGD("DistributedFileServiceStub : OnRemoteRequest enter, code %{public}d ", code); + std::u16string myDescriptor = DistributedFileServiceStub::GetDescriptor(); + std::u16string remoteDescriptor = data.ReadInterfaceToken(); + if (myDescriptor != remoteDescriptor) { + LOGE("DistributedFileServiceStub descriptor information"); + return DFS_DESCRIPTOR_IS_EMPTY; + } + auto itFunc = memberFuncMap_.find(code); if (itFunc != memberFuncMap_.end()) { auto memberFunc = itFunc->second; @@ -47,20 +57,93 @@ int DistributedFileServiceStub::OnRemoteRequest(uint32_t code, return (this->*memberFunc)(data, reply); } } - return IPCObjectStub::OnRemoteRequest(code, data, reply, option); } -int DistributedFileServiceStub::test(MessageParcel &data, MessageParcel &reply) +int32_t DistributedFileServiceStub::SendFileStub(MessageParcel &data, MessageParcel &reply) { - LOGD(" DistributedFileServiceStub : sendTest enter"); - sendTest(); - return 0; + std::string cid = data.ReadString(); + if (cid.empty()) { + LOGE("SendFileStub : Failed to get app device id, error: invalid device id"); + return DFS_SESSION_ID_IS_EMPTY; + } + + int32_t sourceListNumber = data.ReadInt32(); + std::vector srcList; + for (int32_t index = 0; index < sourceListNumber; ++index) { + srcList.push_back(data.ReadString()); + } + std::vector dstList; + int32_t destinationListNumber = data.ReadInt32(); + for (int32_t index = 0; index < destinationListNumber; ++index) { + dstList.push_back(data.ReadString()); + } + uint32_t fileCount = data.ReadUint32(); + + int32_t result = SendFile(cid, srcList, dstList, fileCount); + if (!reply.WriteInt32(result)) { + LOGE("sendfilestub fail to write parcel"); + return DFS_WRITE_REPLY_FAIL; + } + + return result; } -int32_t DistributedFileServiceStub::SendFileStub(MessageParcel &data, MessageParcel &reply) +int32_t DistributedFileServiceStub::OpenFileStub(MessageParcel &data, MessageParcel &reply) +{ + int32_t fileData = data.ReadFileDescriptor(); + int32_t fd = fcntl(fileData, F_GETFD, 0); + if (fd < 0) { + reply.WriteInt32(DFS_SET_FD_FAIL); + LOGE("DistributedFileServiceStub : Failed to get app device id, error: invalid device id"); + return DFS_SET_FD_FAIL; + } + + std::string fileName = data.ReadString(); + if (fileName.empty()) { + LOGE("DistributedFileServiceStub : Failed to get app device id, error: invalid device id"); + return DFS_NO_SUCH_FILE; + } + + int32_t modeData = data.ReadInt32(); + int32_t result = OpenFile(fileData, fileName, modeData); + if (!reply.WriteInt32(result)) { + LOGE("fail to write parcel"); + return DFS_WRITE_REPLY_FAIL; + } + + return result; +} + +int32_t DistributedFileServiceStub::CmdRegisterNotifyCallback(MessageParcel &data, MessageParcel &reply) { - return 0; + OHOS::sptr remote = data.ReadRemoteObject(); + if (remote == nullptr) { + LOGE("Callback ptr is nullptr."); + return 0; + } + + OHOS::sptr callback = iface_cast(remote); + int32_t result = RegisterNotifyCallback(callback); + reply.WriteInt32(result); + return result; +} + +int32_t DistributedFileServiceStub::CmdIsDeviceOnline(MessageParcel &data, MessageParcel &reply) +{ + std::string cid = data.ReadString(); + if (cid.empty()) { + LOGE("DistributedFileServiceStub : Failed to get app device id, error: invalid device id"); + return DFS_SESSION_ID_IS_EMPTY; + } + + int32_t result = IsDeviceOnline(cid); + if (!reply.WriteInt32(result)) { + LOGE("fail to write parcel"); + return DFS_WRITE_REPLY_FAIL; + } + + return result; } } // namespace DistributedFile } // namespace Storage diff --git a/services/distributedfileservice/src/network/softbus_agent.cpp b/services/distributedfileservice/src/network/softbus_agent.cpp index da2acb13d..af0734b2a 100644 --- a/services/distributedfileservice/src/network/softbus_agent.cpp +++ b/services/distributedfileservice/src/network/softbus_agent.cpp @@ -18,21 +18,27 @@ #include #include #include + #include "device_manager_agent.h" #include "distributedfile_service.h" +#include "i_distributedfile_service.h" +#include "iservice_registry.h" #include "session.h" #include "softbus_dispatcher.h" +#include "system_ability_definition.h" #include "utils_exception.h" +#include "utils_directory.h" #include "utils_log.h" namespace OHOS { namespace Storage { namespace DistributedFile { -using namespace std; -constexpr int32_t SOFTBUS_OK = 0; -constexpr int32_t DEVICE_ID_SIZE_MAX = 65; -constexpr int32_t IS_CLIENT = 0; -const std::string DEFAULT_ROOT_PATH = "/data/service/el2/100/hmdfs/non_account/data/"; +namespace { + constexpr int32_t SOFTBUS_OK = 0; + constexpr int32_t DEVICE_ID_SIZE_MAX = 65; + constexpr int32_t IS_CLIENT = 0; + const std::string DEFAULT_ROOT_PATH = "/data/service/el2/100/hmdfs/non_account/data/"; +} SoftbusAgent::SoftbusAgent() {} @@ -40,6 +46,7 @@ SoftbusAgent::~SoftbusAgent() { StopInstance(); } + void SoftbusAgent::StartInstance() { RegisterSessionListener(); @@ -48,9 +55,12 @@ void SoftbusAgent::StartInstance() void SoftbusAgent::StopInstance() { - for (auto iter = cidToSessionID_.begin(); iter != cidToSessionID_.end();) { - CloseSession(iter->first); - iter = cidToSessionID_.erase(iter); + { + std::unique_lock lock(sessionMapMux_); + for (auto iter = cidToSessionID_.begin(); iter != cidToSessionID_.end();) { + CloseSession(iter->first); + iter = cidToSessionID_.erase(iter); + } } getSessionCV_.notify_all(); UnRegisterSessionListener(); @@ -58,16 +68,12 @@ void SoftbusAgent::StopInstance() void SoftbusAgent::OnDeviceOnline(const std::string &cid) { - // todo:测试接口SendFile - const char *sFileList[1] = {"/data/user/0/xhl_sendfile_test/1.txt"}; - - static int sendfile_cnt = 0; - int ret = SendFile(cid, sFileList, nullptr, 1); - if (ret != 0) { - LOGE("sendfile failed, ret %{public}d", ret); + if (notifyCallback_ == nullptr) { + LOGE("OnDeviceOnline Nofify pointer is empty"); return; } - LOGE("sendfile... ret %{public}d, sendfile_cnt %{public}d", ret, sendfile_cnt); + + notifyCallback_->DeviceOnline(cid); } int SoftbusAgent::SendFile(const std::string &cid, const char *sFileList[], const char *dFileList[], uint32_t fileCnt) @@ -102,7 +108,7 @@ int SoftbusAgent::SendFile(const std::string &cid, const char *sFileList[], cons } int ret = ::SendFile(sessionId, sFileList, dFileList, fileCnt); - LOGE("sendfile is processing, sessionId:%{publid}d, ret %{public}d", sessionId, ret); + LOGD("sendfile is processing, sessionId:%{public}d, ret %{public}d", sessionId, ret); return ret; } @@ -122,7 +128,6 @@ void SoftbusAgent::OpenSession(const std::string &cid) void SoftbusAgent::OnSessionOpened(const int sessionId, const int result) { - LOGD("get session res:%{public}d, sessionId:%{public}d", result, sessionId); if (result != 0) { LOGE("open failed, result:%{public}d, sessionId:%{public}d", result, sessionId); return; @@ -135,7 +140,7 @@ void SoftbusAgent::OnSessionOpened(const int sessionId, const int result) // client session priority use, so insert list head { - std::unique_lock lock(sessionMapMux_); + std::unique_lock lock(sessionMapMux_); if (::GetSessionSide(sessionId) == IS_CLIENT) { cidToSessionID_[cid].push_front(sessionId); } else { @@ -143,26 +148,60 @@ void SoftbusAgent::OnSessionOpened(const int sessionId, const int result) } } - // cv 唤醒等待的sendfile流程 getSessionCV_.notify_one(); LOGD("get session SUCCESS, sessionId:%{public}d", sessionId); } int SoftbusAgent::OnSendFileFinished(const int sessionId, const std::string firstFile) { - LOGD("send file finish, sessionId:%{public}d, firstFile %{public}s", sessionId, firstFile.c_str()); + if (notifyCallback_ == nullptr) { + LOGE("OnSendFileFinished Nofify pointer is empty"); + return -1; + } + std::string cid = GetPeerDevId(sessionId); + notifyCallback_->SendFinished(cid, firstFile); return 0; } int SoftbusAgent::OnFileTransError(const int sessionId) { - LOGD("file trans error, sessionId:%{public}d", sessionId); + if (notifyCallback_ == nullptr) { + LOGE("OnFileTransError Nofify pointer is empty"); + return -1; + } + std::string cid = GetPeerDevId(sessionId); + if (::GetSessionSide(sessionId) == IS_CLIENT) { + notifyCallback_->SendError(cid); + } else { + notifyCallback_->ReceiveError(cid); + } return 0; } void SoftbusAgent::OnReceiveFileFinished(const int sessionId, const std::string files, int fileCnt) { - LOGD("recv file finish, sessionId:%{public}d, files %{public}s, cnt %{public}d", sessionId, files.c_str(), fileCnt); + if (notifyCallback_ == nullptr) { + LOGE("OnReceiveFileFinished Nofify pointer is empty"); + return; + } + std::string cid = GetPeerDevId(sessionId); + if (cid.empty()) { + auto alreadyOnliceDev = DeviceManagerAgent::GetInstance()->getOnlineDevs(); + LOGI("IsDeviceOnline size, %{public}d", alreadyOnliceDev.size()); + for (std::string item : alreadyOnliceDev) { + LOGI("IsDeviceOnline item, %{public}s", item.c_str()); + cid = item; + } + } + + std::string desFileName = std::string("/data/system_ce/") + files; + int32_t fd = open(desFileName.c_str(), O_RDONLY); + if (fd <= 0) { + LOGE("NapiWriteFile open recive distributedfile %{public}d, %{public}s, %{public}d", + fd, strerror(errno), errno); + } + notifyCallback_->WriteFile(fd, files); + notifyCallback_->ReceiveFinished(cid, files, fileCnt); } void SoftbusAgent::OnSessionClosed(int sessionId) @@ -172,24 +211,26 @@ void SoftbusAgent::OnSessionClosed(int sessionId) LOGE("get peer device id failed"); return; } - std::unique_lock lock(sessionMapMux_); - if (cidToSessionID_.find(cid) != cidToSessionID_.end()) { - cidToSessionID_[cid].remove(sessionId); + { + std::unique_lock lock(sessionMapMux_); + if (cidToSessionID_.find(cid) != cidToSessionID_.end()) { + cidToSessionID_[cid].remove(sessionId); + } } - return; } std::string SoftbusAgent::GetPeerDevId(const int sessionId) { std::string cid; - char peerDevId[DEVICE_ID_SIZE_MAX] = ""; + char peerDevId[DEVICE_ID_SIZE_MAX] = { '\0' }; int ret = ::GetPeerDeviceId(sessionId, peerDevId, sizeof(peerDevId)); if (ret != SOFTBUS_OK) { LOGE("get my peer device id failed, errno:%{public}d, sessionId:%{public}d", ret, sessionId); cid = ""; } else { - cid = string(peerDevId); + cid = std::string(peerDevId); } + return cid; } @@ -211,8 +252,18 @@ void SoftbusAgent::OnDeviceOffline(const std::string &cid) if (CloseSession(cid) == -1) { return; } - std::unique_lock lock(sessionMapMux_); - cidToSessionID_.erase(cid); + + { + std::unique_lock lock(sessionMapMux_); + cidToSessionID_.erase(cid); + } + + if (notifyCallback_ == nullptr) { + LOGE("OnDeviceOffline Nofify pointer is empty"); + return; + } + + notifyCallback_->DeviceOffline(cid); } void SoftbusAgent::RegisterSessionListener() @@ -223,10 +274,10 @@ void SoftbusAgent::RegisterSessionListener() }; int ret = ::CreateSessionServer(DistributedFileService::pkgName_.c_str(), sessionName_.c_str(), &sessionListener); if (ret != 0) { - stringstream ss; + std::stringstream ss; ss << "Failed to CreateSessionServer, errno:" << ret; LOGE("%{public}s, sessionName:%{public}s", ss.str().c_str(), sessionName_.c_str()); - return throw runtime_error(ss.str()); + return throw std::runtime_error(ss.str()); } LOGD("Succeed to CreateSessionServer, pkgName %{public}s, sbusName:%{public}s", DistributedFileService::pkgName_.c_str(), sessionName_.c_str()); @@ -241,10 +292,10 @@ void SoftbusAgent::RegisterFileListener() std::string pkgName = DistributedFileService::pkgName_; int ret = ::SetFileSendListener(pkgName.c_str(), sessionName_.c_str(), &fileSendListener); if (ret != 0) { - stringstream ss; + std::stringstream ss; ss << "Failed to SetFileSendListener, errno:" << ret; LOGE("%{public}s, sessionName:%{public}s", ss.str().c_str(), sessionName_.c_str()); - throw runtime_error(ss.str()); + throw std::runtime_error(ss.str()); } LOGD("Succeed to SetFileSendListener, pkgName %{public}s, sbusName:%{public}s", pkgName.c_str(), sessionName_.c_str()); @@ -256,10 +307,10 @@ void SoftbusAgent::RegisterFileListener() ret = ::SetFileReceiveListener(pkgName.c_str(), sessionName_.c_str(), &fileRecvListener, DEFAULT_ROOT_PATH.c_str()); if (ret != 0) { - stringstream ss; + std::stringstream ss; ss << "Failed to SetFileReceiveListener, errno:" << ret; LOGE("%{public}s, sessionName:%{public}s", ss.str().c_str(), sessionName_.c_str()); - throw runtime_error(ss.str()); + throw std::runtime_error(ss.str()); } LOGD("Succeed to SetFileReceiveListener, pkgName %{public}s, sbusName:%{public}s", pkgName.c_str(), sessionName_.c_str()); @@ -268,13 +319,22 @@ void SoftbusAgent::UnRegisterSessionListener() { int ret = ::RemoveSessionServer(DistributedFileService::pkgName_.c_str(), sessionName_.c_str()); if (ret != 0) { - stringstream ss; + std::stringstream ss; ss << "Failed to RemoveSessionServer, errno:" << ret; LOGE("%{public}s", ss.str().c_str()); - throw runtime_error(ss.str()); + throw std::runtime_error(ss.str()); } LOGD("RemoveSessionServer success!"); } +void SoftbusAgent::SetTransCallback(sptr &callback) +{ + notifyCallback_ = callback; +} + +void SoftbusAgent::RemoveTransCallbak() +{ + notifyCallback_ = nullptr; +} } // namespace DistributedFile } // namespace Storage } // namespace OHOS \ No newline at end of file diff --git a/services/distributedfileservice/src/network/softbus_dispatcher.cpp b/services/distributedfileservice/src/network/softbus_dispatcher.cpp index 542f089c0..e69e31aca 100644 --- a/services/distributedfileservice/src/network/softbus_dispatcher.cpp +++ b/services/distributedfileservice/src/network/softbus_dispatcher.cpp @@ -20,7 +20,6 @@ namespace OHOS { namespace Storage { namespace DistributedFile { - int SoftbusDispatcher::OnSessionOpened(int sessionId, int result) { LOGD("get session res:%{public}d, sessionId:%{public}d", result, sessionId); diff --git a/utils/system/include/utils_directory.h b/utils/system/include/utils_directory.h index 48d89abcf..8d8a57563 100644 --- a/utils/system/include/utils_directory.h +++ b/utils/system/include/utils_directory.h @@ -38,6 +38,9 @@ void ForceCreateDirectory(const std::string &path, mode_t mode); void ForceCreateDirectory(const std::string &path, mode_t mode, uid_t uid, gid_t gid); void ForceRemoveDirectory(const std::string &path); + +bool IsFileExist(const std::string &fileName); +int32_t RemoveFile(const std::string &fileName); } // namespace Utils } // namespace DistributedFile } // namespace Storage diff --git a/utils/system/include/utils_singleton.h b/utils/system/include/utils_singleton.h index ffa1b58b5..f4c0a2eb4 100644 --- a/utils/system/include/utils_singleton.h +++ b/utils/system/include/utils_singleton.h @@ -25,7 +25,7 @@ namespace OHOS { namespace Storage { namespace DistributedFile { namespace Utils { -#define DECLARE_SINGLETON(MyClass) \ +#define DECLARE_UTILS_SINGLETON(MyClass) \ public: \ ~MyClass(); \ MyClass(const MyClass&) = delete; \ diff --git a/utils/system/src/utils_directory.cpp b/utils/system/src/utils_directory.cpp index 7c62a2a40..deee461d0 100644 --- a/utils/system/src/utils_directory.cpp +++ b/utils/system/src/utils_directory.cpp @@ -78,6 +78,17 @@ void ForceRemoveDirectory(const string &path) throw system_error(errno, system_category()); } } + +bool IsFileExist(const std::string &fileName) +{ + struct stat fileStat; + return (stat(fileName.c_str(), &fileStat) == 0); +} + +int32_t RemoveFile(const std::string &fileName) +{ + return remove(fileName.c_str()); +} } // namespace Utils } // namespace DistributedFile } // namespace Storage -- Gitee From ef5a03ad7b543810d5b758d08d90a79fb9c773e3 Mon Sep 17 00:00:00 2001 From: hhchinasoft Date: Sat, 12 Feb 2022 13:13:44 +0800 Subject: [PATCH 02/11] sendfile code check clean Signed-off-by: hhchinasoft --- frameworks/js/napi/include/event_agent.h | 3 +- frameworks/js/napi/include/sendfile.h | 2 - frameworks/js/napi/src/sendfile.cpp | 40 ++++++------------- frameworks/js/napi/src/sendfile_napi.cpp | 5 ++- .../native/src/dfs_filetransfer_callback.cpp | 1 - .../src/filetransfer_callback_proxy.cpp | 1 - .../include/network/softbus_agent.h | 4 +- .../src/ipc/distributedfile_service.cpp | 12 +++--- 8 files changed, 26 insertions(+), 42 deletions(-) diff --git a/frameworks/js/napi/include/event_agent.h b/frameworks/js/napi/include/event_agent.h index efb7604d1..0e1d317c3 100644 --- a/frameworks/js/napi/include/event_agent.h +++ b/frameworks/js/napi/include/event_agent.h @@ -16,10 +16,11 @@ #ifndef NATIVE_MODULE_SEND_FILE_EVENT_AGENT_H #define NATIVE_MODULE_SEND_FILE_EVENT_AGENT_H -#include "napi/native_api.h" #include #include +#include "napi/native_api.h" + namespace OHOS { namespace Storage { namespace DistributedFile { diff --git a/frameworks/js/napi/include/sendfile.h b/frameworks/js/napi/include/sendfile.h index 1b7b07196..01107c86b 100644 --- a/frameworks/js/napi/include/sendfile.h +++ b/frameworks/js/napi/include/sendfile.h @@ -18,7 +18,6 @@ #include "event_agent.h" -#include #include #include @@ -54,7 +53,6 @@ int32_t NapiWriteFile(int32_t fd, const std::string &fileName); napi_value RegisterSendFileNotifyCallback(); -static std::mutex g_uidMutex; static std::unordered_map g_mapUidToEventAgent; int32_t ExecSendFile(const std::string &deviceId, const std::vector& srcList, diff --git a/frameworks/js/napi/src/sendfile.cpp b/frameworks/js/napi/src/sendfile.cpp index cc165e312..fc5b54408 100644 --- a/frameworks/js/napi/src/sendfile.cpp +++ b/frameworks/js/napi/src/sendfile.cpp @@ -34,7 +34,7 @@ namespace OHOS { namespace Storage { namespace DistributedFile { -const std::string DfsAppUid{"SendFileTestUid"}; +const std::string DfsAppUid{ "SendFileTestUid" }; constexpr int32_t FILE_BLOCK_SIZE = 1024; napi_value TransEvent::ToJsObject(napi_env env) @@ -50,8 +50,7 @@ napi_value TransEvent::ToJsObject(napi_env env) LOGE("ToJsObject: package error code failed.\n"); return nullptr; } - } - else { + } else { LOGE("ToJsObject: create error code failed.\n"); return nullptr; } @@ -64,8 +63,7 @@ napi_value TransEvent::ToJsObject(napi_env env) LOGE("ToJsObject: package filename failed.\n"); return nullptr; } - } - else { + } else { LOGE("ToJsObject: create filename failed.\n"); return nullptr; } @@ -78,22 +76,18 @@ napi_value TransEvent::ToJsObject(napi_env env) LOGE("ToJsObject: package file counts failed.\n"); return nullptr; } - } - else { + } else { LOGE("ToJsObject: create file counts failed.\n"); return nullptr; } - } - else { + } else { LOGE("ToJsObject: create object failed.\n"); return nullptr; } - LOGI("ToJsObject: create parameter object success.\n"); return te; } - napi_value RegisterSendFileNotifyCallback() { sptr systemAbilityMgr = @@ -158,8 +152,7 @@ int32_t NapiSendFinished(const std::string &cid, const std::string &fileName) if (agent->FindDevice(cid)) { LOGI("DEBUG_SENDFILE:OnSendFinished, event agent for device[%{public}s] was found.", cid.c_str()); break; - } - else { + } else { sptr systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (systemAbilityMgr == nullptr) { @@ -186,8 +179,7 @@ int32_t NapiSendFinished(const std::string &cid, const std::string &fileName) agent->Emit("sendFinished", reinterpret_cast(&event)); LOGI("DEBUG_SENDFILE:OnSendFinished, [%{public}s] event was done.", cid.c_str()); return 0; - } - else { + } else { LOGI("DEBUG_SENDFILE:OnSendFinished, [%{public}s] no event processor.", cid.c_str()); return -1; } @@ -202,8 +194,7 @@ int32_t NapiSendError(const std::string &cid) if (agent->FindDevice(cid)) { LOGI("DEBUG_SENDFILE:OnSendError, event agent for device[%{public}s] was found.", cid.c_str()); break; - } - else { + } else { sptr systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (systemAbilityMgr == nullptr) { @@ -230,8 +221,7 @@ int32_t NapiSendError(const std::string &cid) agent->Emit("sendFinished", reinterpret_cast(&event)); LOGI("DEBUG_SENDFILE:OnSendError, [%{public}s] event was done.", cid.c_str()); return 0; - } - else { + } else { LOGI("DEBUG_SENDFILE:OnSendError, [%{public}s] no event processor.", cid.c_str()); return -1; } @@ -246,8 +236,7 @@ int32_t NapiReceiveFinished(const std::string &cid, const std::string &fileName, if (agent->FindDevice(cid)) { LOGI("DEBUG_SENDFILE:OnReceiveFinished, event agent for device[%{public}s] was found.", cid.c_str()); break; - } - else { + } else { sptr systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (systemAbilityMgr == nullptr) { @@ -274,8 +263,7 @@ int32_t NapiReceiveFinished(const std::string &cid, const std::string &fileName, agent->Emit("receiveFinished", reinterpret_cast(&event)); LOGI("DEBUG_SENDFILE:OnReceiveFinished, [%{public}s] event was done.", cid.c_str()); return 0; - } - else { + } else { LOGI("DEBUG_SENDFILE:OnReceiveFinished, [%{public}s] no event processor.", cid.c_str()); return -1; } @@ -290,8 +278,7 @@ int32_t NapiReceiveError(const std::string &cid) if (agent->FindDevice(cid)) { LOGI("OnReceiveError : event agent for device[%{public}s] was found.", cid.c_str()); break; - } - else { + } else { sptr systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (systemAbilityMgr == nullptr) { @@ -318,8 +305,7 @@ int32_t NapiReceiveError(const std::string &cid) agent->Emit("receiveFinished", reinterpret_cast(&event)); LOGI("OnReceiveError : [%{public}s] event was done.", cid.c_str()); return 0; - } - else { + } else { LOGI("OnReceiveError : [%{public}s] no event processor.", cid.c_str()); return -1; } diff --git a/frameworks/js/napi/src/sendfile_napi.cpp b/frameworks/js/napi/src/sendfile_napi.cpp index 47de94c54..5c71cf1e4 100644 --- a/frameworks/js/napi/src/sendfile_napi.cpp +++ b/frameworks/js/napi/src/sendfile_napi.cpp @@ -15,6 +15,8 @@ #include "sendfile_napi.h" +#include + #include "filemgmt_libn.h" #include "sendfile.h" #include "utils_log.h" @@ -26,7 +28,8 @@ using namespace FileManagement::LibN; namespace { constexpr int32_t MAX_SEND_FILE_HAP_NUMBER = 50; constexpr int32_t SENDFILE_NAPI_BUF_LENGTH = 256; - const std::string DfsAppUid{"SendFileTestUid"}; + const std::string DfsAppUid{ "SendFileTestUid" }; + static std::mutex g_uidMutex; } napi_value SendFile(napi_env env, napi_callback_info info) diff --git a/frameworks/native/src/dfs_filetransfer_callback.cpp b/frameworks/native/src/dfs_filetransfer_callback.cpp index 19dc846e4..ef8aa22a9 100644 --- a/frameworks/native/src/dfs_filetransfer_callback.cpp +++ b/frameworks/native/src/dfs_filetransfer_callback.cpp @@ -15,7 +15,6 @@ #include "dfs_filetransfer_callback.h" -#include #include "sendfile.h" #include "utils_log.h" diff --git a/frameworks/native/src/filetransfer_callback_proxy.cpp b/frameworks/native/src/filetransfer_callback_proxy.cpp index 39cf790bf..420a3fe0a 100644 --- a/frameworks/native/src/filetransfer_callback_proxy.cpp +++ b/frameworks/native/src/filetransfer_callback_proxy.cpp @@ -26,7 +26,6 @@ FileTransferCallbackProxy::~FileTransferCallbackProxy() {} int32_t FileTransferCallbackProxy::DeviceOnline(const std::string &cid) { - MessageParcel data; if (!data.WriteInterfaceToken(FileTransferCallbackProxy::GetDescriptor())) { LOGE("xhl [---FileTransferCallbackProxy---] WriteInterfaceToken failed"); diff --git a/services/distributedfileservice/include/network/softbus_agent.h b/services/distributedfileservice/include/network/softbus_agent.h index eac1dbb77..75a08c2f2 100644 --- a/services/distributedfileservice/include/network/softbus_agent.h +++ b/services/distributedfileservice/include/network/softbus_agent.h @@ -16,11 +16,11 @@ #ifndef DFS_SOFTBUS_AGENT_H #define DFS_SOFTBUS_AGENT_H -#include "utils_singleton.h" -#include "i_filetransfer_callback.h" #include #include #include + +#include "i_filetransfer_callback.h" #include "utils_singleton.h" namespace OHOS { diff --git a/services/distributedfileservice/src/ipc/distributedfile_service.cpp b/services/distributedfileservice/src/ipc/distributedfile_service.cpp index a00a1f0a1..5067b6b0d 100644 --- a/services/distributedfileservice/src/ipc/distributedfile_service.cpp +++ b/services/distributedfileservice/src/ipc/distributedfile_service.cpp @@ -17,21 +17,19 @@ #include #include #include -#include -#include #include "bundle_mgr_interface.h" #include "bundle_mgr_proxy.h" +#include "device_manager_agent.h" #include "ipc_skeleton.h" #include "iservice_registry.h" #include "message_parcel.h" #include "parcel.h" +#include "softbus_agent.h" +#include "system_ability_definition.h" #include "utils_directory.h" #include "utils_log.h" -#include "softbus_agent.h" -#include "device_manager_agent.h" - namespace OHOS { namespace Storage { namespace DistributedFile { @@ -81,7 +79,7 @@ int32_t DistributedFileService::SendFile(const std::string &cid, const std::vector &destinationFileList, const uint32_t fileCount) { - char **sFileList = new char*[fileCount]; + 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()); @@ -101,7 +99,7 @@ int32_t DistributedFileService::SendFile(const std::string &cid, } } - char **dFileList = new char*[fileCount]; + char **dFileList = new char* [fileCount]; for (int index = 0; index < destinationFileList.size(); ++index) { LOGI("DistributedFileService::SendFile Destination File List %{public}d, %{public}s", index, destinationFileList.at(index).c_str()); -- Gitee From 99bc0a8632f0ee2de670645bb8c0ce6bb863f5e2 Mon Sep 17 00:00:00 2001 From: hhchinasoft Date: Sat, 12 Feb 2022 13:21:36 +0800 Subject: [PATCH 03/11] sendfile code check clean Signed-off-by: hhchinasoft --- BUILD.gn | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index 26d862a8b..f630eb5aa 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -29,9 +29,7 @@ group("services_target") { } group("napi_target") { - deps = [ - "${distributedfile_path}/frameworks/js/napi:build_kits_js", - ] + deps = [ "${distributedfile_path}/frameworks/js/napi:build_kits_js" ] } group("dfs_test_moudule") { -- Gitee From 8668c5bc5547b889a808c472c4ee2fa2006af6ef Mon Sep 17 00:00:00 2001 From: hhchinasoft Date: Sat, 12 Feb 2022 14:06:20 +0800 Subject: [PATCH 04/11] sendfile code check clean Signed-off-by: hhchinasoft --- frameworks/js/napi/BUILD.gn | 21 +++--- frameworks/js/napi/include/event_agent.h | 13 +++- frameworks/js/napi/include/sendfile.h | 18 ----- frameworks/js/napi/include/trans_event.h | 45 +++++++++++ frameworks/js/napi/src/event_agent.cpp | 11 --- frameworks/js/napi/src/sendfile.cpp | 69 +++-------------- frameworks/js/napi/src/sendfile_napi.cpp | 6 +- frameworks/js/napi/src/trans_event.cpp | 75 +++++++++++++++++++ .../include/device/device_manager_agent.h | 2 +- .../src/ipc/distributedfile_service.cpp | 21 +++++- 10 files changed, 171 insertions(+), 110 deletions(-) create mode 100644 frameworks/js/napi/include/trans_event.h create mode 100644 frameworks/js/napi/src/trans_event.cpp diff --git a/frameworks/js/napi/BUILD.gn b/frameworks/js/napi/BUILD.gn index 96abcc806..60de4eb45 100644 --- a/frameworks/js/napi/BUILD.gn +++ b/frameworks/js/napi/BUILD.gn @@ -32,33 +32,32 @@ ohos_shared_library("sendfile") { ] sources = [ - "//foundation/filemanagement/dfs_service/frameworks/js/napi/src/sendfile_napi.cpp", - "//foundation/filemanagement/dfs_service/frameworks/js/napi/src/sendfile.cpp", "//foundation/filemanagement/dfs_service/frameworks/js/napi/src/event_agent.cpp", + "//foundation/filemanagement/dfs_service/frameworks/js/napi/src/sendfile.cpp", + "//foundation/filemanagement/dfs_service/frameworks/js/napi/src/sendfile_napi.cpp", + "//foundation/filemanagement/dfs_service/frameworks/js/napi/src/trans_event.cpp", "//foundation/filemanagement/dfs_service/frameworks/native/src/dfs_filetransfer_callback.cpp", "//foundation/filemanagement/dfs_service/frameworks/native/src/filetransfer_callback_proxy.cpp", "//foundation/filemanagement/dfs_service/frameworks/native/src/filetransfer_callback_stub.cpp", ] deps = [ - "//foundation/filemanagement/dfs_service/utils:libdistributedfileutils", - "//foundation/filemanagement/dfs_service/interfaces/innerkits/native:libdistributedfile_innerkits", "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", "//foundation/ace/napi:ace_napi", - "//utils/native/base:utilsecurec", "//foundation/distributeddatamgr/distributedfile/utils/filemgmt_libn:filemgmt_libn", + "//foundation/filemanagement/dfs_service/interfaces/innerkits/native:libdistributedfile_innerkits", + "//foundation/filemanagement/dfs_service/utils:libdistributedfileutils", + "//utils/native/base:utilsecurec", ] external_deps = [ + "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "samgr_standard:samgr_common", "samgr_standard:samgr_proxy", - "hiviewdfx_hilog_native:libhilog", ] - cflags_cc = [ - "-Wno-unused-function" - ] + cflags_cc = [ "-Wno-unused-function" ] relative_install_dir = "module" subsystem_name = "filemanagement" @@ -66,7 +65,5 @@ ohos_shared_library("sendfile") { } group("build_kits_js") { - deps = [ - ":sendfile", - ] + deps = [ ":sendfile" ] } \ No newline at end of file diff --git a/frameworks/js/napi/include/event_agent.h b/frameworks/js/napi/include/event_agent.h index 0e1d317c3..5ecb636cf 100644 --- a/frameworks/js/napi/include/event_agent.h +++ b/frameworks/js/napi/include/event_agent.h @@ -24,7 +24,18 @@ namespace OHOS { namespace Storage { namespace DistributedFile { -struct EventListener; +namespace { + constexpr int32_t LISTENER_TYPTE_MAX_LENGTH = 64; +} + +struct EventListener { + char type[LISTENER_TYPTE_MAX_LENGTH] = { 0 }; + bool isOnce = false; + napi_ref handlerRef = nullptr; + EventListener* back = nullptr; + EventListener* next = nullptr; +}; + class Event { public: virtual napi_value ToJsObject(napi_env env) = 0; diff --git a/frameworks/js/napi/include/sendfile.h b/frameworks/js/napi/include/sendfile.h index 01107c86b..c6accb39b 100644 --- a/frameworks/js/napi/include/sendfile.h +++ b/frameworks/js/napi/include/sendfile.h @@ -24,24 +24,6 @@ namespace OHOS { namespace Storage { namespace DistributedFile { -class TransEvent : public Event { -public: - enum { - TRANS_SUCCESS = 0, - TRANS_FAILURE = 1 - }; -public: - TransEvent(int32_t err, std::string fname, int32_t num) : errorCode_(err), fileName_(fname), fileCount_(num) {} - TransEvent(int32_t err, std::string fname) : errorCode_(err), fileName_(fname) {} - TransEvent(int32_t err) : errorCode_(err) {} - virtual ~TransEvent() {} - napi_value ToJsObject(napi_env env); - -private: - int32_t errorCode_ = TRANS_SUCCESS; - std::string fileName_ = ""; - int32_t fileCount_ = 0; -}; int32_t NapiDeviceOnline(const std::string &cid); int32_t NapiDeviceOffline(const std::string &cid); diff --git a/frameworks/js/napi/include/trans_event.h b/frameworks/js/napi/include/trans_event.h new file mode 100644 index 000000000..f44c2837f --- /dev/null +++ b/frameworks/js/napi/include/trans_event.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TRANS_EVENT_H +#define TRANS_EVENT_H + +#include "event_agent.h" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +class TransEvent : public Event { +public: + enum { + TRANS_SUCCESS = 0, + TRANS_FAILURE = 1 + }; +public: + TransEvent(int32_t err, std::string fname, int32_t num) : errorCode_(err), fileName_(fname), fileCount_(num) {} + TransEvent(int32_t err, std::string fname) : errorCode_(err), fileName_(fname) {} + TransEvent(int32_t err) : errorCode_(err) {} + virtual ~TransEvent() {} + napi_value ToJsObject(napi_env env); + +private: + int32_t errorCode_ = TRANS_SUCCESS; + std::string fileName_ = ""; + int32_t fileCount_ = 0; +}; +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS +#endif // TRANS_EVENT_H \ No newline at end of file diff --git a/frameworks/js/napi/src/event_agent.cpp b/frameworks/js/napi/src/event_agent.cpp index 495918c56..68a3892b7 100644 --- a/frameworks/js/napi/src/event_agent.cpp +++ b/frameworks/js/napi/src/event_agent.cpp @@ -21,17 +21,6 @@ namespace OHOS { namespace Storage { namespace DistributedFile { -namespace { - constexpr int32_t LISTENER_TYPTE_MAX_LENGTH = 64; -} -struct EventListener { - char type[LISTENER_TYPTE_MAX_LENGTH] = { 0 }; - bool isOnce = false; - napi_ref handlerRef = nullptr; - EventListener* back = nullptr; - EventListener* next = nullptr; -}; - EventAgent::EventAgent(napi_env env, napi_value thisVar) : env_(env), thisVarRef_(nullptr), first_(nullptr), last_(nullptr) { diff --git a/frameworks/js/napi/src/sendfile.cpp b/frameworks/js/napi/src/sendfile.cpp index fc5b54408..76a0f807d 100644 --- a/frameworks/js/napi/src/sendfile.cpp +++ b/frameworks/js/napi/src/sendfile.cpp @@ -19,75 +19,24 @@ #include #include -#include "system_ability_definition.h" -#include "i_distributedfile_service.h" -#include "service_proxy.h" -#include "utils_log.h" +#include "dfs_filetransfer_callback.h" #include "event_agent.h" #include "filetransfer_callback_proxy.h" -#include "dfs_filetransfer_callback.h" +#include "i_distributedfile_service.h" +#include "iservice_registry.h" +#include "service_proxy.h" +#include "system_ability_definition.h" +#include "system_ability_manager.h" +#include "trans_event.h" #include "utils_directory.h" - -#include -#include +#include "utils_log.h" namespace OHOS { namespace Storage { namespace DistributedFile { -const std::string DfsAppUid{ "SendFileTestUid" }; +const std::string DfsAppUid { "SendFileTestUid" }; constexpr int32_t FILE_BLOCK_SIZE = 1024; -napi_value TransEvent::ToJsObject(napi_env env) -{ - napi_value te; - napi_status status = napi_create_object(env, &te); - if (napi_ok == status) { - napi_value err; - status = napi_create_int32(env, errorCode_, &err); - if (napi_ok == status) { - status = napi_set_named_property(env, te, "errorCode", err); - if (napi_ok != status) { - LOGE("ToJsObject: package error code failed.\n"); - return nullptr; - } - } else { - LOGE("ToJsObject: create error code failed.\n"); - return nullptr; - } - - napi_value fname; - status = napi_create_string_utf8(env, fileName_.c_str(), fileName_.size()+1, &fname); - if (napi_ok == status) { - status = napi_set_named_property(env, te, "fileName", fname); - if (napi_ok != status) { - LOGE("ToJsObject: package filename failed.\n"); - return nullptr; - } - } else { - LOGE("ToJsObject: create filename failed.\n"); - return nullptr; - } - - napi_value fcount; - status = napi_create_int32(env, fileCount_, &fcount); - if (napi_ok == status) { - status = napi_set_named_property(env, te, "fileCount", fcount); - if (napi_ok != status) { - LOGE("ToJsObject: package file counts failed.\n"); - return nullptr; - } - } else { - LOGE("ToJsObject: create file counts failed.\n"); - return nullptr; - } - } else { - LOGE("ToJsObject: create object failed.\n"); - return nullptr; - } - - return te; -} - napi_value RegisterSendFileNotifyCallback() { sptr systemAbilityMgr = diff --git a/frameworks/js/napi/src/sendfile_napi.cpp b/frameworks/js/napi/src/sendfile_napi.cpp index 5c71cf1e4..ec0e9abba 100644 --- a/frameworks/js/napi/src/sendfile_napi.cpp +++ b/frameworks/js/napi/src/sendfile_napi.cpp @@ -19,6 +19,7 @@ #include "filemgmt_libn.h" #include "sendfile.h" +#include "trans_event.h" #include "utils_log.h" namespace OHOS { @@ -28,7 +29,7 @@ using namespace FileManagement::LibN; namespace { constexpr int32_t MAX_SEND_FILE_HAP_NUMBER = 50; constexpr int32_t SENDFILE_NAPI_BUF_LENGTH = 256; - const std::string DfsAppUid{ "SendFileTestUid" }; + const std::string DfsAppUid { "SendFileTestUid" }; static std::mutex g_uidMutex; } @@ -74,7 +75,6 @@ napi_value SendFile(napi_env env, napi_callback_info info) std::string deviceIdString(deviceId.get()); auto cbExec = [deviceIdString, sourPath, destPath, fileCount, resultCode]() -> NError { *resultCode = ExecSendFile(deviceIdString.c_str(), sourPath, destPath, fileCount); - LOGI("cbExec resultCode : %{public}d", *resultCode); return NError(*resultCode); }; @@ -82,11 +82,9 @@ napi_value SendFile(napi_env env, napi_callback_info info) if (err) { return { env, err.GetNapiErr(env) }; } - LOGI("cbComplete resultCode : %{public}d", *resultCode); return { NVal::CreateInt64(env, *resultCode) }; }; - LOGI("JS SendFile created thread params count %{public}d", funcArg.GetArgc()); std::string procedureName = "SendFile"; NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == DFS_ARG_CNT::FOUR) { diff --git a/frameworks/js/napi/src/trans_event.cpp b/frameworks/js/napi/src/trans_event.cpp new file mode 100644 index 000000000..8d452edf1 --- /dev/null +++ b/frameworks/js/napi/src/trans_event.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "trans_event.h" + +#include "utils_log.h" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +napi_value TransEvent::ToJsObject(napi_env env) +{ + napi_value te; + napi_status status = napi_create_object(env, &te); + if (napi_ok == status) { + napi_value err; + status = napi_create_int32(env, errorCode_, &err); + if (napi_ok == status) { + status = napi_set_named_property(env, te, "errorCode", err); + if (napi_ok != status) { + LOGE("ToJsObject: package error code failed.\n"); + return nullptr; + } + } else { + LOGE("ToJsObject: create error code failed.\n"); + return nullptr; + } + + napi_value fname; + status = napi_create_string_utf8(env, fileName_.c_str(), fileName_.size()+1, &fname); + if (napi_ok == status) { + status = napi_set_named_property(env, te, "fileName", fname); + if (napi_ok != status) { + LOGE("ToJsObject: package filename failed.\n"); + return nullptr; + } + } else { + LOGE("ToJsObject: create filename failed.\n"); + return nullptr; + } + + napi_value fcount; + status = napi_create_int32(env, fileCount_, &fcount); + if (napi_ok == status) { + status = napi_set_named_property(env, te, "fileCount", fcount); + if (napi_ok != status) { + LOGE("ToJsObject: package file counts failed.\n"); + return nullptr; + } + } else { + LOGE("ToJsObject: create file counts failed.\n"); + return nullptr; + } + } else { + LOGE("ToJsObject: create object failed.\n"); + return nullptr; + } + + return te; +} +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfileservice/include/device/device_manager_agent.h b/services/distributedfileservice/include/device/device_manager_agent.h index df2cd1fbf..bcdfd5126 100644 --- a/services/distributedfileservice/include/device/device_manager_agent.h +++ b/services/distributedfileservice/include/device/device_manager_agent.h @@ -58,7 +58,7 @@ private: std::mutex devsRecordMutex_; std::set alreadyOnlineDev_; - std::string pkgName_{"ohos.storage.distributedfile.service"}; + std::string pkgName_ { "ohos.storage.distributedfile.service" }; }; } // namespace DistributedFile } // namespace Storage diff --git a/services/distributedfileservice/src/ipc/distributedfile_service.cpp b/services/distributedfileservice/src/ipc/distributedfile_service.cpp index 5067b6b0d..1220629c7 100644 --- a/services/distributedfileservice/src/ipc/distributedfile_service.cpp +++ b/services/distributedfileservice/src/ipc/distributedfile_service.cpp @@ -79,6 +79,9 @@ int32_t DistributedFileService::SendFile(const std::string &cid, const std::vector &destinationFileList, const uint32_t fileCount) { + if (fileCount < 0) { + return -1; + } 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", @@ -88,13 +91,21 @@ int32_t DistributedFileService::SendFile(const std::string &cid, int32_t length = tmpString.length(); sFileList[index] = new char[length + 1]; memset_s(sFileList[index], length + 1, '\0', length + 1); - memcpy_s(sFileList[index], length + 1, tmpString.c_str(), length); + int ret = memcpy_s(sFileList[index], length + 1, tmpString.c_str(), length); + if (ret < 0) { + LOGE("memory copy failed"); + return -1; + } sFileList[index][length] = '\0'; } else { int32_t length = sourceFileList.at(index).length(); sFileList[index] = new char[length + 1]; memset_s(sFileList[index], length + 1, '\0', length + 1); - memcpy_s(sFileList[index], length + 1, sourceFileList.at(index).c_str(), length); + int ret = memcpy_s(sFileList[index], length + 1, sourceFileList.at(index).c_str(), length); + if (ret < 0) { + LOGE("memory copy failed"); + return -1; + } sFileList[index][length] = '\0'; } } @@ -106,7 +117,11 @@ int32_t DistributedFileService::SendFile(const std::string &cid, int32_t length = destinationFileList.at(index).length(); dFileList[index] = new char[length + 1]; memset_s(dFileList[index], length + 1, '\0', length + 1); - memcpy_s(dFileList[index], length + 1, destinationFileList.at(index).c_str(), length); + int ret = memcpy_s(dFileList[index], length + 1, destinationFileList.at(index).c_str(), length); + if (ret < 0) { + LOGE("memory copy failed"); + return -1; + } dFileList[index][length] = '\0'; } -- Gitee From 47b310e59f13d32eed648b0a7ffa1a256fe7e1a1 Mon Sep 17 00:00:00 2001 From: hhchinasoft Date: Sat, 12 Feb 2022 14:41:44 +0800 Subject: [PATCH 05/11] sendfile code check clean Signed-off-by: hhchinasoft --- frameworks/js/napi/include/sendfile.h | 1 - frameworks/js/napi/src/sendfile.cpp | 2 +- services/distributedfileservice/BUILD.gn | 2 +- .../src/ipc/distributedfile_service.cpp | 6 +++--- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/frameworks/js/napi/include/sendfile.h b/frameworks/js/napi/include/sendfile.h index c6accb39b..a22276433 100644 --- a/frameworks/js/napi/include/sendfile.h +++ b/frameworks/js/napi/include/sendfile.h @@ -24,7 +24,6 @@ namespace OHOS { namespace Storage { namespace DistributedFile { - int32_t NapiDeviceOnline(const std::string &cid); int32_t NapiDeviceOffline(const std::string &cid); int32_t NapiSendFinished(const std::string &cid, const std::string &fileName); diff --git a/frameworks/js/napi/src/sendfile.cpp b/frameworks/js/napi/src/sendfile.cpp index 76a0f807d..88dcc5692 100644 --- a/frameworks/js/napi/src/sendfile.cpp +++ b/frameworks/js/napi/src/sendfile.cpp @@ -308,7 +308,7 @@ int32_t ExecSendFile(const std::string &deviceId, const std::vector { if (deviceId.empty()) { LOGE("DeviceId can't be emptey.\n"); - return -2; + return -1; } sptr systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); diff --git a/services/distributedfileservice/BUILD.gn b/services/distributedfileservice/BUILD.gn index ecc81370a..64e3e4bcf 100644 --- a/services/distributedfileservice/BUILD.gn +++ b/services/distributedfileservice/BUILD.gn @@ -41,8 +41,8 @@ ohos_shared_library("libdistributedfileservice") { configs = [ "${utils_path}:compiler_configs" ] external_deps = [ + "ability_base:want", "ability_runtime:app_manager", - "ability_runtime:want", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "ipc:ipc_core", diff --git a/services/distributedfileservice/src/ipc/distributedfile_service.cpp b/services/distributedfileservice/src/ipc/distributedfile_service.cpp index 1220629c7..5c63e4c13 100644 --- a/services/distributedfileservice/src/ipc/distributedfile_service.cpp +++ b/services/distributedfileservice/src/ipc/distributedfile_service.cpp @@ -92,7 +92,7 @@ int32_t DistributedFileService::SendFile(const std::string &cid, sFileList[index] = new char[length + 1]; memset_s(sFileList[index], length + 1, '\0', length + 1); int ret = memcpy_s(sFileList[index], length + 1, tmpString.c_str(), length); - if (ret < 0) { + if (ret != EOK) { LOGE("memory copy failed"); return -1; } @@ -102,7 +102,7 @@ int32_t DistributedFileService::SendFile(const std::string &cid, sFileList[index] = new char[length + 1]; memset_s(sFileList[index], length + 1, '\0', length + 1); int ret = memcpy_s(sFileList[index], length + 1, sourceFileList.at(index).c_str(), length); - if (ret < 0) { + if (ret != EOK) { LOGE("memory copy failed"); return -1; } @@ -118,7 +118,7 @@ int32_t DistributedFileService::SendFile(const std::string &cid, dFileList[index] = new char[length + 1]; memset_s(dFileList[index], length + 1, '\0', length + 1); int ret = memcpy_s(dFileList[index], length + 1, destinationFileList.at(index).c_str(), length); - if (ret < 0) { + if (ret != EOK) { LOGE("memory copy failed"); return -1; } -- Gitee From 14fe6e6fbfcf2cd53fbede020b9cba958da293d7 Mon Sep 17 00:00:00 2001 From: hhchinasoft Date: Sat, 12 Feb 2022 14:53:28 +0800 Subject: [PATCH 06/11] sendfile code check clean Signed-off-by: hhchinasoft --- frameworks/js/napi/src/event_agent.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/frameworks/js/napi/src/event_agent.cpp b/frameworks/js/napi/src/event_agent.cpp index 68a3892b7..bb3d62827 100644 --- a/frameworks/js/napi/src/event_agent.cpp +++ b/frameworks/js/napi/src/event_agent.cpp @@ -121,9 +121,6 @@ void EventAgent::InsertDevice(const std::string& deviceId) if (deviceList_.find(deviceId) == deviceList_.end()) { std::unique_lock lock(deviceListMut_); deviceList_.insert(deviceId); - LOGI("xhl insert device[%{public}s] into the devicelist", deviceId.c_str()); - } else { - LOGI("xhl device[%{public}s] has already in the devicelist", deviceId.c_str()); } } @@ -132,9 +129,6 @@ void EventAgent::RemoveDevice(const std::string& deviceId) if (deviceList_.find(deviceId) != deviceList_.end()) { std::unique_lock lock(deviceListMut_); deviceList_.erase(deviceId); - LOGI("xhl remove device[%{public}s] from the devicelist", deviceId.c_str()); - } else { - LOGI("xhl no device[%{public}s] in the devicelist", deviceId.c_str()); } } -- Gitee From 3d02e4e80a254f887a177b23e3999c103c8543c1 Mon Sep 17 00:00:00 2001 From: hhchinasoft Date: Sat, 12 Feb 2022 15:11:44 +0800 Subject: [PATCH 07/11] sendfile code check clean Signed-off-by: hhchinasoft --- .../src/filetransfer_callback_proxy.cpp | 14 ++++------ frameworks/native/src/service_proxy.cpp | 28 +++++++------------ .../src/ipc/distributedfile_service.cpp | 23 +++++++-------- 3 files changed, 28 insertions(+), 37 deletions(-) diff --git a/frameworks/native/src/filetransfer_callback_proxy.cpp b/frameworks/native/src/filetransfer_callback_proxy.cpp index 420a3fe0a..0991c77df 100644 --- a/frameworks/native/src/filetransfer_callback_proxy.cpp +++ b/frameworks/native/src/filetransfer_callback_proxy.cpp @@ -28,13 +28,13 @@ int32_t FileTransferCallbackProxy::DeviceOnline(const std::string &cid) { MessageParcel data; if (!data.WriteInterfaceToken(FileTransferCallbackProxy::GetDescriptor())) { - LOGE("xhl [---FileTransferCallbackProxy---] WriteInterfaceToken failed"); + LOGE("[---FileTransferCallbackProxy---] WriteInterfaceToken failed"); return DFS_CALLBACK_WRITE_DESCRIPTOR_TOKEN_FAIL; } sptr remote = Remote(); if (remote == nullptr) { - LOGE("xhl [---FileTransferCallbackProxy---] Remote is null"); + LOGE("[---FileTransferCallbackProxy---] Remote is null"); return ERR_NULL_OBJECT; } @@ -52,13 +52,13 @@ int32_t FileTransferCallbackProxy::DeviceOffline(const std::string &cid) { MessageParcel data; if (!data.WriteInterfaceToken(FileTransferCallbackProxy::GetDescriptor())) { - LOGE("xhl [---FileTransferCallbackProxy---] WriteInterfaceToken failed"); + LOGE("[---FileTransferCallbackProxy---] WriteInterfaceToken failed"); return 0; } sptr remote = Remote(); if (remote == nullptr) { - LOGE("xhl [---FileTransferCallbackProxy---] Remote is null"); + LOGE("[---FileTransferCallbackProxy---] Remote is null"); return ERR_NULL_OBJECT; } @@ -177,8 +177,6 @@ int32_t FileTransferCallbackProxy::ReceiveError(const std::string &cid) int32_t FileTransferCallbackProxy::WriteFile(int32_t fd, const std::string &fileName) { - LOGI("xhl WriteFile enter"); - LOGI("xhl WriteFile fd %{public}d, %{public}s", fd, fileName.c_str()); MessageParcel data; if (!data.WriteInterfaceToken(FileTransferCallbackProxy::GetDescriptor())) { return DFS_CALLBACK_WRITE_DESCRIPTOR_TOKEN_FAIL; @@ -188,14 +186,14 @@ int32_t FileTransferCallbackProxy::WriteFile(int32_t fd, const std::string &file data.WriteString(fileName); if (Remote() == nullptr) { - LOGE("xhl WriteFile remote object address is null"); + LOGE("WriteFile remote object address is null"); return DFS_CALLBACK_REMOTE_ADDRESS_IS_NULL; } MessageParcel reply; MessageOption option; Remote()->SendRequest(ON_WRITE_FILE, data, reply, option); - LOGI("xhl WriteFile end"); + return reply.ReadInt32(); } } // namespace DistributedFile diff --git a/frameworks/native/src/service_proxy.cpp b/frameworks/native/src/service_proxy.cpp index 746a12a08..533218aba 100644 --- a/frameworks/native/src/service_proxy.cpp +++ b/frameworks/native/src/service_proxy.cpp @@ -28,7 +28,6 @@ int32_t ServiceProxy::SendFile(const std::string &cid, const std::vector &destinationFileList, const uint32_t fileCount) { - LOGI("xhl sendFile enter"); MessageParcel data; if (!data.WriteInterfaceToken(ServiceProxy::GetDescriptor())) { return DFS_WRITE_DESCRIPTOR_TOKEN_FAIL; @@ -47,7 +46,7 @@ int32_t ServiceProxy::SendFile(const std::string &cid, data.WriteUint32(fileCount); if (Remote() == nullptr) { - LOGE("xhl unregister sendfile remote object address is null"); + LOGE("unregister sendfile remote object address is null"); return DFS_REMOTE_ADDRESS_IS_NULL; } @@ -55,18 +54,15 @@ int32_t ServiceProxy::SendFile(const std::string &cid, MessageOption option; int32_t result = Remote()->SendRequest(SEND_FILE_DISTRIBUTED, data, reply, option); if (result != DFS_SENDFILE_SUCCESS) { - LOGE("xhl sendfile error code : %{public}d", result); + LOGE("sendfile error code : %{public}d", result); return result; } - LOGE("xhl sendfile sendRequest done %{public}d", result); return reply.ReadInt32(); } int32_t ServiceProxy::OpenFile(int32_t fd, const std::string &fileName, int32_t mode) { - LOGI("xhl OpenFile enter"); - LOGI("xhl OpenFile fd %{public}d", fd); MessageParcel data; if (!data.WriteInterfaceToken(ServiceProxy::GetDescriptor())) { return DFS_WRITE_DESCRIPTOR_TOKEN_FAIL; @@ -77,70 +73,67 @@ int32_t ServiceProxy::OpenFile(int32_t fd, const std::string &fileName, int32_t data.WriteInt32(mode); if (Remote() == nullptr) { - LOGE("xhl openfile remote object address is null"); + LOGE("openfile remote object address is null"); return DFS_REMOTE_ADDRESS_IS_NULL; } MessageParcel reply; MessageOption option; Remote()->SendRequest(OPEN_FILE_FD, data, reply, option); - LOGI("xhl RegisterNotifyCallback end"); + return reply.ReadInt32(); } int32_t ServiceProxy::RegisterNotifyCallback(sptr &callback) { - LOGI("xhl OpenFile enter"); MessageParcel data; if (!data.WriteInterfaceToken(ServiceProxy::GetDescriptor())) { return DFS_WRITE_DESCRIPTOR_TOKEN_FAIL; } if (callback == nullptr) { - LOGE("xhl The parameter of callback is nullptr"); + LOGE("The parameter of callback is nullptr"); return DFS_CALLBACK_PARAM_ERROR; } if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) { - LOGE("xhl write remote object failed"); + LOGE("write remote object failed"); return DFS_WRITE_REMOTE_OBJECT_FAIL; } if (Remote() == nullptr) { - LOGE("xhl register filetransfer callback remote object address is null"); + LOGE("register filetransfer callback remote object address is null"); return DFS_REMOTE_ADDRESS_IS_NULL; } MessageParcel reply; MessageOption option; Remote()->SendRequest(REGISTER_NOTIFY_CALLBACK, data, reply, option); - LOGI("xhl RegisterNotifyCallback end"); + return reply.ReadInt32(); } int32_t ServiceProxy::UnRegisterNotifyCallback() { - LOGI("xhl UnRegisterNotifyCallback enter"); MessageParcel data; if (!data.WriteInterfaceToken(ServiceProxy::GetDescriptor())) { return DFS_WRITE_DESCRIPTOR_TOKEN_FAIL; } if (Remote() == nullptr) { - LOGE("xhl unregister filetransfer callback remote object address is null"); + LOGE("unregister filetransfer callback remote object address is null"); return DFS_REMOTE_ADDRESS_IS_NULL; } MessageParcel reply; MessageOption option; Remote()->SendRequest(UN_REGISTER_NOTIFY_CALLBACK, data, reply, option); - LOGI("xhl UnRegisterNotifyCallback end"); + return reply.ReadInt32(); } int32_t ServiceProxy::IsDeviceOnline(const std::string &cid) { - LOGE("xhl IsDeviceOnline enter"); MessageParcel data; if (!data.WriteInterfaceToken(ServiceProxy::GetDescriptor())) { return DFS_WRITE_DESCRIPTOR_TOKEN_FAIL; @@ -159,7 +152,6 @@ int32_t ServiceProxy::IsDeviceOnline(const std::string &cid) LOGE("Function RemoveBundleDistributedDirs! errCode:%{public}d", result); return DFS_NO_DEVICE_ONLINE; } - LOGE("xhl IsDeviceOnline done %{public}d", result); return reply.ReadInt32(); } diff --git a/services/distributedfileservice/src/ipc/distributedfile_service.cpp b/services/distributedfileservice/src/ipc/distributedfile_service.cpp index 5c63e4c13..c5828aaad 100644 --- a/services/distributedfileservice/src/ipc/distributedfile_service.cpp +++ b/services/distributedfileservice/src/ipc/distributedfile_service.cpp @@ -90,20 +90,20 @@ int32_t DistributedFileService::SendFile(const std::string &cid, std::string tmpString("/data/system_ce/tmp"); int32_t length = tmpString.length(); sFileList[index] = new char[length + 1]; - memset_s(sFileList[index], length + 1, '\0', length + 1); - int ret = memcpy_s(sFileList[index], length + 1, tmpString.c_str(), length); - if (ret != EOK) { - LOGE("memory copy failed"); + if (memset_s(sFileList[index], length + 1, '\0', length + 1) != EOK) { + return -1; + } + if (memcpy_s(sFileList[index], length + 1, tmpString.c_str(), length) != EOK) { return -1; } sFileList[index][length] = '\0'; } else { int32_t length = sourceFileList.at(index).length(); sFileList[index] = new char[length + 1]; - memset_s(sFileList[index], length + 1, '\0', length + 1); - int ret = memcpy_s(sFileList[index], length + 1, sourceFileList.at(index).c_str(), length); - if (ret != EOK) { - LOGE("memory copy failed"); + if (memset_s(sFileList[index], length + 1, '\0', length + 1) != EOK) { + return -1; + } + if (memcpy_s(sFileList[index], length + 1, sourceFileList.at(index).c_str(), length) != EOK) { return -1; } sFileList[index][length] = '\0'; @@ -116,9 +116,10 @@ int32_t DistributedFileService::SendFile(const std::string &cid, index, destinationFileList.at(index).c_str()); int32_t length = destinationFileList.at(index).length(); dFileList[index] = new char[length + 1]; - memset_s(dFileList[index], length + 1, '\0', length + 1); - int ret = memcpy_s(dFileList[index], length + 1, destinationFileList.at(index).c_str(), length); - if (ret != EOK) { + if (memset_s(dFileList[index], length + 1, '\0', length + 1) != EOK) { + return -1; + } + if (memcpy_s(dFileList[index], length + 1, destinationFileList.at(index).c_str(), length) != EOK) { LOGE("memory copy failed"); return -1; } -- Gitee From 33f38804d375379ba22b3f7490774711499e87c2 Mon Sep 17 00:00:00 2001 From: hhchinasoft Date: Sat, 12 Feb 2022 16:44:05 +0800 Subject: [PATCH 08/11] sendfile code check clean Signed-off-by: hhchinasoft --- frameworks/js/napi/BUILD.gn | 1 - frameworks/native/include/service_proxy.h | 1 - .../include/device/device_manager_agent.h | 2 +- .../include/mountpoint/mount_manager.h | 2 +- services/distributedfileservice/BUILD.gn | 1 - .../include/device/device_manager_agent.h | 4 +--- .../include/ipc/distributedfile_service.h | 3 +-- .../distributedfileservice/include/network/softbus_agent.h | 2 +- .../src/ipc/distributedfile_service.cpp | 6 +----- utils/system/include/utils_singleton.h | 2 +- 10 files changed, 7 insertions(+), 17 deletions(-) diff --git a/frameworks/js/napi/BUILD.gn b/frameworks/js/napi/BUILD.gn index 60de4eb45..70f477af9 100644 --- a/frameworks/js/napi/BUILD.gn +++ b/frameworks/js/napi/BUILD.gn @@ -23,7 +23,6 @@ ohos_shared_library("sendfile") { "//foundation/filemanagement/dfs_service/frameworks/js/napi/include", "//foundation/filemanagement/dfs_service/utils/log/include", "//foundation/filemanagement/dfs_service/services/distributedfileservice/include/ipc", - "//foundation/filemanagement/dfs_service/services/distributedfileservice/include", "//foundation/communication/ipc/interfaces/innerkits/libdbinder/include", "//foundation/distributedschedule/samgr/services/samgr/native/include", "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include", diff --git a/frameworks/native/include/service_proxy.h b/frameworks/native/include/service_proxy.h index e1b2561d4..8e238288b 100644 --- a/frameworks/native/include/service_proxy.h +++ b/frameworks/native/include/service_proxy.h @@ -31,7 +31,6 @@ public: * @param impl */ explicit ServiceProxy(const sptr &impl); - virtual ~ServiceProxy(); int32_t SendFile(const std::string &cid, diff --git a/services/distributedfiledaemon/include/device/device_manager_agent.h b/services/distributedfiledaemon/include/device/device_manager_agent.h index 2e276027d..27617ac03 100644 --- a/services/distributedfiledaemon/include/device/device_manager_agent.h +++ b/services/distributedfiledaemon/include/device/device_manager_agent.h @@ -54,7 +54,7 @@ class DeviceManagerAgent final : public DistributedHardware::DmInitCallback, public Startable, public Actor, public Utils::Singleton { - DECLARE_UTILS_SINGLETON(DeviceManagerAgent); + DECLARE_SINGLETON(DeviceManagerAgent); public: void Start() override; diff --git a/services/distributedfiledaemon/include/mountpoint/mount_manager.h b/services/distributedfiledaemon/include/mountpoint/mount_manager.h index 2d3b2939f..7b04f10c3 100644 --- a/services/distributedfiledaemon/include/mountpoint/mount_manager.h +++ b/services/distributedfiledaemon/include/mountpoint/mount_manager.h @@ -31,7 +31,7 @@ public: void Mount(std::unique_ptr mp); void Umount(std::weak_ptr wmp); void Umount(const std::string &groupId); - DECLARE_UTILS_SINGLETON(MountManager); + DECLARE_SINGLETON(MountManager); private: void StartInstance() override {} diff --git a/services/distributedfileservice/BUILD.gn b/services/distributedfileservice/BUILD.gn index 64e3e4bcf..e3edeedce 100644 --- a/services/distributedfileservice/BUILD.gn +++ b/services/distributedfileservice/BUILD.gn @@ -42,7 +42,6 @@ ohos_shared_library("libdistributedfileservice") { external_deps = [ "ability_base:want", - "ability_runtime:app_manager", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "ipc:ipc_core", diff --git a/services/distributedfileservice/include/device/device_manager_agent.h b/services/distributedfileservice/include/device/device_manager_agent.h index bcdfd5126..b2a11da1a 100644 --- a/services/distributedfileservice/include/device/device_manager_agent.h +++ b/services/distributedfileservice/include/device/device_manager_agent.h @@ -33,7 +33,7 @@ class DeviceManagerAgent : public DistributedHardware::DmInitCallback, public DistributedHardware::DeviceStateCallback, public std::enable_shared_from_this, public Utils::Singleton { - DECLARE_UTILS_SINGLETON(DeviceManagerAgent); + DECLARE_SINGLETON(DeviceManagerAgent); public: void OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo) override; @@ -57,8 +57,6 @@ private: std::mutex devsRecordMutex_; std::set alreadyOnlineDev_; - - std::string pkgName_ { "ohos.storage.distributedfile.service" }; }; } // namespace DistributedFile } // namespace Storage diff --git a/services/distributedfileservice/include/ipc/distributedfile_service.h b/services/distributedfileservice/include/ipc/distributedfile_service.h index 70c2dcbd9..6e5d68f6d 100644 --- a/services/distributedfileservice/include/ipc/distributedfile_service.h +++ b/services/distributedfileservice/include/ipc/distributedfile_service.h @@ -18,7 +18,6 @@ #include "distributedfile_service_stub.h" #include "iremote_stub.h" -#include "singleton.h" #include "system_ability.h" #include @@ -29,10 +28,10 @@ namespace DistributedFile { class DistributedFileService : public SystemAbility, public DistributedFileServiceStub, public std::enable_shared_from_this { - DECLARE_DELAYED_SINGLETON(DistributedFileService) DECLARE_SYSTEM_ABILITY(DistributedFileService) public: explicit DistributedFileService(int32_t saId, bool runOnCreate = true) : SystemAbility(saId, runOnCreate) {} + ~DistributedFileService() {} void OnDump() override; void OnStart() override; void OnStop() override; diff --git a/services/distributedfileservice/include/network/softbus_agent.h b/services/distributedfileservice/include/network/softbus_agent.h index 75a08c2f2..13a722371 100644 --- a/services/distributedfileservice/include/network/softbus_agent.h +++ b/services/distributedfileservice/include/network/softbus_agent.h @@ -27,7 +27,7 @@ namespace OHOS { namespace Storage { namespace DistributedFile { class SoftbusAgent : public std::enable_shared_from_this, public Utils::Singleton { - DECLARE_UTILS_SINGLETON(SoftbusAgent); + DECLARE_SINGLETON(SoftbusAgent); public: void RegisterSessionListener(); diff --git a/services/distributedfileservice/src/ipc/distributedfile_service.cpp b/services/distributedfileservice/src/ipc/distributedfile_service.cpp index c5828aaad..cff8adb95 100644 --- a/services/distributedfileservice/src/ipc/distributedfile_service.cpp +++ b/services/distributedfileservice/src/ipc/distributedfile_service.cpp @@ -40,10 +40,6 @@ namespace { REGISTER_SYSTEM_ABILITY_BY_ID(DistributedFileService, STORAGE_DISTRIBUTED_FILE_SERVICE_SA_ID, false); -DistributedFileService::DistributedFileService() : SystemAbility(STORAGE_DISTRIBUTED_FILE_SERVICE_SA_ID, false) {} - -DistributedFileService::~DistributedFileService() {} - void DistributedFileService::OnDump() { LOGI("OnDump"); @@ -62,7 +58,7 @@ void DistributedFileService::OnStop() void DistributedFileService::PublishSA() { - bool ret = SystemAbility::Publish(DelayedSingleton::GetInstance().get()); + bool ret = SystemAbility::Publish(this); if (!ret) { LOGE("Leave, publishing DistributedFileService failed!"); return; diff --git a/utils/system/include/utils_singleton.h b/utils/system/include/utils_singleton.h index f4c0a2eb4..ffa1b58b5 100644 --- a/utils/system/include/utils_singleton.h +++ b/utils/system/include/utils_singleton.h @@ -25,7 +25,7 @@ namespace OHOS { namespace Storage { namespace DistributedFile { namespace Utils { -#define DECLARE_UTILS_SINGLETON(MyClass) \ +#define DECLARE_SINGLETON(MyClass) \ public: \ ~MyClass(); \ MyClass(const MyClass&) = delete; \ -- Gitee From 782a703a4d71a38154b869323d2539be0447ec49 Mon Sep 17 00:00:00 2001 From: hhchinasoft Date: Sat, 12 Feb 2022 17:32:24 +0800 Subject: [PATCH 09/11] sendfile code check clean Signed-off-by: hhchinasoft --- frameworks/js/napi/BUILD.gn | 29 ++++++++++--------- .../include/filetransfer_callback_proxy.h | 4 +-- frameworks/native/include/service_proxy.h | 4 +-- .../src/filetransfer_callback_proxy.cpp | 5 ---- frameworks/native/src/service_proxy.cpp | 4 --- services/distributedfileservice/BUILD.gn | 11 ++----- 6 files changed, 21 insertions(+), 36 deletions(-) diff --git a/frameworks/js/napi/BUILD.gn b/frameworks/js/napi/BUILD.gn index 70f477af9..06873656b 100644 --- a/frameworks/js/napi/BUILD.gn +++ b/frameworks/js/napi/BUILD.gn @@ -12,17 +12,18 @@ # limitations under the License. import("//build/ohos.gni") +import("//foundation/filemanagement/dfs_service/distributedfile.gni") ohos_shared_library("sendfile") { include_dirs = [ "//third_party/libuv/include", "//third_party/node/src", "//foundation/ace/napi/interfaces/kits", - "//foundation/filemanagement/dfs_service/interfaces/innerkits/native", - "//foundation/filemanagement/dfs_service/frameworks/native/include", - "//foundation/filemanagement/dfs_service/frameworks/js/napi/include", - "//foundation/filemanagement/dfs_service/utils/log/include", - "//foundation/filemanagement/dfs_service/services/distributedfileservice/include/ipc", + "${distributedfile_path}/interfaces/innerkits/native", + "${distributedfile_path}/frameworks/native/include", + "${distributedfile_path}/frameworks/js/napi/include", + "${distributedfile_path}/utils/log/include", + "${services_path}/distributedfileservice/include/ipc", "//foundation/communication/ipc/interfaces/innerkits/libdbinder/include", "//foundation/distributedschedule/samgr/services/samgr/native/include", "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include", @@ -31,21 +32,21 @@ ohos_shared_library("sendfile") { ] sources = [ - "//foundation/filemanagement/dfs_service/frameworks/js/napi/src/event_agent.cpp", - "//foundation/filemanagement/dfs_service/frameworks/js/napi/src/sendfile.cpp", - "//foundation/filemanagement/dfs_service/frameworks/js/napi/src/sendfile_napi.cpp", - "//foundation/filemanagement/dfs_service/frameworks/js/napi/src/trans_event.cpp", - "//foundation/filemanagement/dfs_service/frameworks/native/src/dfs_filetransfer_callback.cpp", - "//foundation/filemanagement/dfs_service/frameworks/native/src/filetransfer_callback_proxy.cpp", - "//foundation/filemanagement/dfs_service/frameworks/native/src/filetransfer_callback_stub.cpp", + "${distributedfile_path}/frameworks/js/napi/src/event_agent.cpp", + "${distributedfile_path}/frameworks/js/napi/src/sendfile.cpp", + "${distributedfile_path}/frameworks/js/napi/src/sendfile_napi.cpp", + "${distributedfile_path}/frameworks/js/napi/src/trans_event.cpp", + "${distributedfile_path}/frameworks/native/src/dfs_filetransfer_callback.cpp", + "${distributedfile_path}/frameworks/native/src/filetransfer_callback_proxy.cpp", + "${distributedfile_path}/frameworks/native/src/filetransfer_callback_stub.cpp", ] deps = [ "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", "//foundation/ace/napi:ace_napi", "//foundation/distributeddatamgr/distributedfile/utils/filemgmt_libn:filemgmt_libn", - "//foundation/filemanagement/dfs_service/interfaces/innerkits/native:libdistributedfile_innerkits", - "//foundation/filemanagement/dfs_service/utils:libdistributedfileutils", + "${distributedfile_path}/interfaces/innerkits/native:libdistributedfile_innerkits", + "${utils_path}:libdistributedfileutils", "//utils/native/base:utilsecurec", ] diff --git a/frameworks/native/include/filetransfer_callback_proxy.h b/frameworks/native/include/filetransfer_callback_proxy.h index cc2423ee6..3a6eb8bef 100644 --- a/frameworks/native/include/filetransfer_callback_proxy.h +++ b/frameworks/native/include/filetransfer_callback_proxy.h @@ -23,8 +23,8 @@ namespace Storage { namespace DistributedFile { class FileTransferCallbackProxy : public IRemoteProxy { public: - explicit FileTransferCallbackProxy(const sptr &impl); - virtual ~FileTransferCallbackProxy(); + explicit FileTransferCallbackProxy(const sptr &impl) : IRemoteProxy(impl) {} + virtual ~FileTransferCallbackProxy() {} public: int32_t DeviceOnline(const std::string &cid) override; diff --git a/frameworks/native/include/service_proxy.h b/frameworks/native/include/service_proxy.h index 8e238288b..ba922b5a7 100644 --- a/frameworks/native/include/service_proxy.h +++ b/frameworks/native/include/service_proxy.h @@ -30,8 +30,8 @@ public: * * @param impl */ - explicit ServiceProxy(const sptr &impl); - virtual ~ServiceProxy(); + explicit ServiceProxy(const sptr &impl) : IRemoteProxy(impl) {} + virtual ~ServiceProxy() = default; int32_t SendFile(const std::string &cid, const std::vector &sourceFileList, diff --git a/frameworks/native/src/filetransfer_callback_proxy.cpp b/frameworks/native/src/filetransfer_callback_proxy.cpp index 0991c77df..1fd15b8e2 100644 --- a/frameworks/native/src/filetransfer_callback_proxy.cpp +++ b/frameworks/native/src/filetransfer_callback_proxy.cpp @@ -19,11 +19,6 @@ namespace OHOS { namespace Storage { namespace DistributedFile { -FileTransferCallbackProxy::FileTransferCallbackProxy(const sptr &impl) - : IRemoteProxy(impl) {} - -FileTransferCallbackProxy::~FileTransferCallbackProxy() {} - int32_t FileTransferCallbackProxy::DeviceOnline(const std::string &cid) { MessageParcel data; diff --git a/frameworks/native/src/service_proxy.cpp b/frameworks/native/src/service_proxy.cpp index 533218aba..4d9918f37 100644 --- a/frameworks/native/src/service_proxy.cpp +++ b/frameworks/native/src/service_proxy.cpp @@ -19,10 +19,6 @@ namespace OHOS { namespace Storage { namespace DistributedFile { -ServiceProxy::ServiceProxy(const sptr &impl) : IRemoteProxy(impl) {} - -ServiceProxy::~ServiceProxy() {} - int32_t ServiceProxy::SendFile(const std::string &cid, const std::vector &sourceFileList, const std::vector &destinationFileList, diff --git a/services/distributedfileservice/BUILD.gn b/services/distributedfileservice/BUILD.gn index e3edeedce..18d89bec2 100644 --- a/services/distributedfileservice/BUILD.gn +++ b/services/distributedfileservice/BUILD.gn @@ -19,13 +19,8 @@ ohos_shared_library("libdistributedfileservice") { "include/ipc", "include/network", "${distributedfile_path}/frameworks/native", - "//foundation/ace/napi/interfaces/kits", - "//third_party/node/src", - "//third_party/libuv/include", + "${distributedfile_path}/frameworks/native/include", "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", - "//foundation/filemanagement/dfs_service/frameworks/native/include", - "//foundation/filemanagement/dfs_service/frameworks/native/native_module_sendfile", - "//foundation/filemanagement/dfs_service/frameworks/js/napi/common", ] sources = [ @@ -34,8 +29,6 @@ ohos_shared_library("libdistributedfileservice") { "src/device/device_manager_agent.cpp", "src/network/softbus_agent.cpp", "src/network/softbus_dispatcher.cpp", - "${distributedfile_path}/frameworks/native/src/service_proxy.cpp", - "${distributedfile_path}/frameworks/native/src/filetransfer_callback_proxy.cpp", ] configs = [ "${utils_path}:compiler_configs" ] @@ -44,10 +37,10 @@ ohos_shared_library("libdistributedfileservice") { "ability_base:want", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", + "dsoftbus_standard:softbus_client", "ipc:ipc_core", "safwk:system_ability_fwk", "samgr_standard:samgr_proxy", - "dsoftbus_standard:softbus_client", ] deps = [ -- Gitee From 33db5f225ec181f878c15abd5bdde9a4ff6d1231 Mon Sep 17 00:00:00 2001 From: hhchinasoft Date: Sat, 12 Feb 2022 18:15:16 +0800 Subject: [PATCH 10/11] sendfile code check clean Signed-off-by: hhchinasoft --- frameworks/js/napi/BUILD.gn | 4 ++-- interfaces/innerkits/native/BUILD.gn | 11 +++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/frameworks/js/napi/BUILD.gn b/frameworks/js/napi/BUILD.gn index 06873656b..68edd0125 100644 --- a/frameworks/js/napi/BUILD.gn +++ b/frameworks/js/napi/BUILD.gn @@ -42,11 +42,11 @@ ohos_shared_library("sendfile") { ] deps = [ + "${distributedfile_path}/interfaces/innerkits/native:libdistributedfile_innerkits", + "${utils_path}:libdistributedfileutils", "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", "//foundation/ace/napi:ace_napi", "//foundation/distributeddatamgr/distributedfile/utils/filemgmt_libn:filemgmt_libn", - "${distributedfile_path}/interfaces/innerkits/native:libdistributedfile_innerkits", - "${utils_path}:libdistributedfileutils", "//utils/native/base:utilsecurec", ] diff --git a/interfaces/innerkits/native/BUILD.gn b/interfaces/innerkits/native/BUILD.gn index a08cf85a6..7b31ee81d 100644 --- a/interfaces/innerkits/native/BUILD.gn +++ b/interfaces/innerkits/native/BUILD.gn @@ -18,11 +18,7 @@ config("private_config") { "../../../frameworks/native/include" ] - # configs = [ "//build/config/compiler:exceptions" ] - defines = [ - "LOG_TAG=\"distributedfile\"", - "LOG_DOMAIN=0xD001600", - ] + configs = [ "//build/config/compiler:exceptions" ] } config("public_config") { @@ -30,7 +26,10 @@ config("public_config") { } ohos_shared_library("libdistributedfile_innerkits") { - sources = [ "${distributedfile_path}/frameworks/native/src/service_proxy.cpp" ] + sources = [ + "${distributedfile_path}/frameworks/native/src/service_proxy.cpp", + "${distributedfile_path}/frameworks/native/src/filetransfer_callback_proxy.cpp", + ] configs = [ ":private_config" ] -- Gitee From de2d8f9df4c57489c620367089abffa0a3f0feca Mon Sep 17 00:00:00 2001 From: hhchinasoft Date: Sat, 12 Feb 2022 13:13:44 +0800 Subject: [PATCH 11/11] sendfile code check clean Signed-off-by: hhchinasoft sendfile code check clean Signed-off-by: hhchinasoft sendfile code check clean Signed-off-by: hhchinasoft sendfile code check clean Signed-off-by: hhchinasoft sendfile code check clean Signed-off-by: hhchinasoft sendfile code check clean Signed-off-by: hhchinasoft sendfile code check clean Signed-off-by: hhchinasoft sendfile code check clean Signed-off-by: hhchinasoft sendfile code check clean Signed-off-by: hhchinasoft --- BUILD.gn | 4 +- frameworks/js/napi/BUILD.gn | 41 ++++--- frameworks/js/napi/include/event_agent.h | 16 ++- frameworks/js/napi/include/sendfile.h | 21 ---- frameworks/js/napi/include/trans_event.h | 45 ++++++++ frameworks/js/napi/src/event_agent.cpp | 17 --- frameworks/js/napi/src/sendfile.cpp | 101 ++++-------------- frameworks/js/napi/src/sendfile_napi.cpp | 9 +- frameworks/js/napi/src/trans_event.cpp | 75 +++++++++++++ .../include/filetransfer_callback_proxy.h | 4 +- frameworks/native/include/service_proxy.h | 5 +- .../native/src/dfs_filetransfer_callback.cpp | 1 - .../src/filetransfer_callback_proxy.cpp | 20 ++-- frameworks/native/src/service_proxy.cpp | 32 ++---- interfaces/innerkits/native/BUILD.gn | 11 +- .../include/device/device_manager_agent.h | 2 +- .../include/mountpoint/mount_manager.h | 2 +- services/distributedfileservice/BUILD.gn | 14 +-- .../include/device/device_manager_agent.h | 4 +- .../include/ipc/distributedfile_service.h | 3 +- .../include/network/softbus_agent.h | 6 +- .../src/ipc/distributedfile_service.cpp | 46 ++++---- utils/system/include/utils_singleton.h | 2 +- 23 files changed, 241 insertions(+), 240 deletions(-) create mode 100644 frameworks/js/napi/include/trans_event.h create mode 100644 frameworks/js/napi/src/trans_event.cpp diff --git a/BUILD.gn b/BUILD.gn index 26d862a8b..f630eb5aa 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -29,9 +29,7 @@ group("services_target") { } group("napi_target") { - deps = [ - "${distributedfile_path}/frameworks/js/napi:build_kits_js", - ] + deps = [ "${distributedfile_path}/frameworks/js/napi:build_kits_js" ] } group("dfs_test_moudule") { diff --git a/frameworks/js/napi/BUILD.gn b/frameworks/js/napi/BUILD.gn index 96abcc806..68edd0125 100644 --- a/frameworks/js/napi/BUILD.gn +++ b/frameworks/js/napi/BUILD.gn @@ -12,18 +12,18 @@ # limitations under the License. import("//build/ohos.gni") +import("//foundation/filemanagement/dfs_service/distributedfile.gni") ohos_shared_library("sendfile") { include_dirs = [ "//third_party/libuv/include", "//third_party/node/src", "//foundation/ace/napi/interfaces/kits", - "//foundation/filemanagement/dfs_service/interfaces/innerkits/native", - "//foundation/filemanagement/dfs_service/frameworks/native/include", - "//foundation/filemanagement/dfs_service/frameworks/js/napi/include", - "//foundation/filemanagement/dfs_service/utils/log/include", - "//foundation/filemanagement/dfs_service/services/distributedfileservice/include/ipc", - "//foundation/filemanagement/dfs_service/services/distributedfileservice/include", + "${distributedfile_path}/interfaces/innerkits/native", + "${distributedfile_path}/frameworks/native/include", + "${distributedfile_path}/frameworks/js/napi/include", + "${distributedfile_path}/utils/log/include", + "${services_path}/distributedfileservice/include/ipc", "//foundation/communication/ipc/interfaces/innerkits/libdbinder/include", "//foundation/distributedschedule/samgr/services/samgr/native/include", "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include", @@ -32,33 +32,32 @@ ohos_shared_library("sendfile") { ] sources = [ - "//foundation/filemanagement/dfs_service/frameworks/js/napi/src/sendfile_napi.cpp", - "//foundation/filemanagement/dfs_service/frameworks/js/napi/src/sendfile.cpp", - "//foundation/filemanagement/dfs_service/frameworks/js/napi/src/event_agent.cpp", - "//foundation/filemanagement/dfs_service/frameworks/native/src/dfs_filetransfer_callback.cpp", - "//foundation/filemanagement/dfs_service/frameworks/native/src/filetransfer_callback_proxy.cpp", - "//foundation/filemanagement/dfs_service/frameworks/native/src/filetransfer_callback_stub.cpp", + "${distributedfile_path}/frameworks/js/napi/src/event_agent.cpp", + "${distributedfile_path}/frameworks/js/napi/src/sendfile.cpp", + "${distributedfile_path}/frameworks/js/napi/src/sendfile_napi.cpp", + "${distributedfile_path}/frameworks/js/napi/src/trans_event.cpp", + "${distributedfile_path}/frameworks/native/src/dfs_filetransfer_callback.cpp", + "${distributedfile_path}/frameworks/native/src/filetransfer_callback_proxy.cpp", + "${distributedfile_path}/frameworks/native/src/filetransfer_callback_stub.cpp", ] deps = [ - "//foundation/filemanagement/dfs_service/utils:libdistributedfileutils", - "//foundation/filemanagement/dfs_service/interfaces/innerkits/native:libdistributedfile_innerkits", + "${distributedfile_path}/interfaces/innerkits/native:libdistributedfile_innerkits", + "${utils_path}:libdistributedfileutils", "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", "//foundation/ace/napi:ace_napi", - "//utils/native/base:utilsecurec", "//foundation/distributeddatamgr/distributedfile/utils/filemgmt_libn:filemgmt_libn", + "//utils/native/base:utilsecurec", ] external_deps = [ + "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "samgr_standard:samgr_common", "samgr_standard:samgr_proxy", - "hiviewdfx_hilog_native:libhilog", ] - cflags_cc = [ - "-Wno-unused-function" - ] + cflags_cc = [ "-Wno-unused-function" ] relative_install_dir = "module" subsystem_name = "filemanagement" @@ -66,7 +65,5 @@ ohos_shared_library("sendfile") { } group("build_kits_js") { - deps = [ - ":sendfile", - ] + deps = [ ":sendfile" ] } \ No newline at end of file diff --git a/frameworks/js/napi/include/event_agent.h b/frameworks/js/napi/include/event_agent.h index efb7604d1..5ecb636cf 100644 --- a/frameworks/js/napi/include/event_agent.h +++ b/frameworks/js/napi/include/event_agent.h @@ -16,14 +16,26 @@ #ifndef NATIVE_MODULE_SEND_FILE_EVENT_AGENT_H #define NATIVE_MODULE_SEND_FILE_EVENT_AGENT_H -#include "napi/native_api.h" #include #include +#include "napi/native_api.h" + namespace OHOS { namespace Storage { namespace DistributedFile { -struct EventListener; +namespace { + constexpr int32_t LISTENER_TYPTE_MAX_LENGTH = 64; +} + +struct EventListener { + char type[LISTENER_TYPTE_MAX_LENGTH] = { 0 }; + bool isOnce = false; + napi_ref handlerRef = nullptr; + EventListener* back = nullptr; + EventListener* next = nullptr; +}; + class Event { public: virtual napi_value ToJsObject(napi_env env) = 0; diff --git a/frameworks/js/napi/include/sendfile.h b/frameworks/js/napi/include/sendfile.h index 1b7b07196..a22276433 100644 --- a/frameworks/js/napi/include/sendfile.h +++ b/frameworks/js/napi/include/sendfile.h @@ -18,32 +18,12 @@ #include "event_agent.h" -#include #include #include namespace OHOS { namespace Storage { namespace DistributedFile { -class TransEvent : public Event { -public: - enum { - TRANS_SUCCESS = 0, - TRANS_FAILURE = 1 - }; -public: - TransEvent(int32_t err, std::string fname, int32_t num) : errorCode_(err), fileName_(fname), fileCount_(num) {} - TransEvent(int32_t err, std::string fname) : errorCode_(err), fileName_(fname) {} - TransEvent(int32_t err) : errorCode_(err) {} - virtual ~TransEvent() {} - napi_value ToJsObject(napi_env env); - -private: - int32_t errorCode_ = TRANS_SUCCESS; - std::string fileName_ = ""; - int32_t fileCount_ = 0; -}; - int32_t NapiDeviceOnline(const std::string &cid); int32_t NapiDeviceOffline(const std::string &cid); int32_t NapiSendFinished(const std::string &cid, const std::string &fileName); @@ -54,7 +34,6 @@ int32_t NapiWriteFile(int32_t fd, const std::string &fileName); napi_value RegisterSendFileNotifyCallback(); -static std::mutex g_uidMutex; static std::unordered_map g_mapUidToEventAgent; int32_t ExecSendFile(const std::string &deviceId, const std::vector& srcList, diff --git a/frameworks/js/napi/include/trans_event.h b/frameworks/js/napi/include/trans_event.h new file mode 100644 index 000000000..f44c2837f --- /dev/null +++ b/frameworks/js/napi/include/trans_event.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TRANS_EVENT_H +#define TRANS_EVENT_H + +#include "event_agent.h" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +class TransEvent : public Event { +public: + enum { + TRANS_SUCCESS = 0, + TRANS_FAILURE = 1 + }; +public: + TransEvent(int32_t err, std::string fname, int32_t num) : errorCode_(err), fileName_(fname), fileCount_(num) {} + TransEvent(int32_t err, std::string fname) : errorCode_(err), fileName_(fname) {} + TransEvent(int32_t err) : errorCode_(err) {} + virtual ~TransEvent() {} + napi_value ToJsObject(napi_env env); + +private: + int32_t errorCode_ = TRANS_SUCCESS; + std::string fileName_ = ""; + int32_t fileCount_ = 0; +}; +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS +#endif // TRANS_EVENT_H \ No newline at end of file diff --git a/frameworks/js/napi/src/event_agent.cpp b/frameworks/js/napi/src/event_agent.cpp index 495918c56..bb3d62827 100644 --- a/frameworks/js/napi/src/event_agent.cpp +++ b/frameworks/js/napi/src/event_agent.cpp @@ -21,17 +21,6 @@ namespace OHOS { namespace Storage { namespace DistributedFile { -namespace { - constexpr int32_t LISTENER_TYPTE_MAX_LENGTH = 64; -} -struct EventListener { - char type[LISTENER_TYPTE_MAX_LENGTH] = { 0 }; - bool isOnce = false; - napi_ref handlerRef = nullptr; - EventListener* back = nullptr; - EventListener* next = nullptr; -}; - EventAgent::EventAgent(napi_env env, napi_value thisVar) : env_(env), thisVarRef_(nullptr), first_(nullptr), last_(nullptr) { @@ -132,9 +121,6 @@ void EventAgent::InsertDevice(const std::string& deviceId) if (deviceList_.find(deviceId) == deviceList_.end()) { std::unique_lock lock(deviceListMut_); deviceList_.insert(deviceId); - LOGI("xhl insert device[%{public}s] into the devicelist", deviceId.c_str()); - } else { - LOGI("xhl device[%{public}s] has already in the devicelist", deviceId.c_str()); } } @@ -143,9 +129,6 @@ void EventAgent::RemoveDevice(const std::string& deviceId) if (deviceList_.find(deviceId) != deviceList_.end()) { std::unique_lock lock(deviceListMut_); deviceList_.erase(deviceId); - LOGI("xhl remove device[%{public}s] from the devicelist", deviceId.c_str()); - } else { - LOGI("xhl no device[%{public}s] in the devicelist", deviceId.c_str()); } } diff --git a/frameworks/js/napi/src/sendfile.cpp b/frameworks/js/napi/src/sendfile.cpp index cc165e312..88dcc5692 100644 --- a/frameworks/js/napi/src/sendfile.cpp +++ b/frameworks/js/napi/src/sendfile.cpp @@ -19,81 +19,24 @@ #include #include -#include "system_ability_definition.h" -#include "i_distributedfile_service.h" -#include "service_proxy.h" -#include "utils_log.h" +#include "dfs_filetransfer_callback.h" #include "event_agent.h" #include "filetransfer_callback_proxy.h" -#include "dfs_filetransfer_callback.h" +#include "i_distributedfile_service.h" +#include "iservice_registry.h" +#include "service_proxy.h" +#include "system_ability_definition.h" +#include "system_ability_manager.h" +#include "trans_event.h" #include "utils_directory.h" - -#include -#include +#include "utils_log.h" namespace OHOS { namespace Storage { namespace DistributedFile { -const std::string DfsAppUid{"SendFileTestUid"}; +const std::string DfsAppUid { "SendFileTestUid" }; constexpr int32_t FILE_BLOCK_SIZE = 1024; -napi_value TransEvent::ToJsObject(napi_env env) -{ - napi_value te; - napi_status status = napi_create_object(env, &te); - if (napi_ok == status) { - napi_value err; - status = napi_create_int32(env, errorCode_, &err); - if (napi_ok == status) { - status = napi_set_named_property(env, te, "errorCode", err); - if (napi_ok != status) { - LOGE("ToJsObject: package error code failed.\n"); - return nullptr; - } - } - else { - LOGE("ToJsObject: create error code failed.\n"); - return nullptr; - } - - napi_value fname; - status = napi_create_string_utf8(env, fileName_.c_str(), fileName_.size()+1, &fname); - if (napi_ok == status) { - status = napi_set_named_property(env, te, "fileName", fname); - if (napi_ok != status) { - LOGE("ToJsObject: package filename failed.\n"); - return nullptr; - } - } - else { - LOGE("ToJsObject: create filename failed.\n"); - return nullptr; - } - - napi_value fcount; - status = napi_create_int32(env, fileCount_, &fcount); - if (napi_ok == status) { - status = napi_set_named_property(env, te, "fileCount", fcount); - if (napi_ok != status) { - LOGE("ToJsObject: package file counts failed.\n"); - return nullptr; - } - } - else { - LOGE("ToJsObject: create file counts failed.\n"); - return nullptr; - } - } - else { - LOGE("ToJsObject: create object failed.\n"); - return nullptr; - } - - LOGI("ToJsObject: create parameter object success.\n"); - return te; -} - - napi_value RegisterSendFileNotifyCallback() { sptr systemAbilityMgr = @@ -158,8 +101,7 @@ int32_t NapiSendFinished(const std::string &cid, const std::string &fileName) if (agent->FindDevice(cid)) { LOGI("DEBUG_SENDFILE:OnSendFinished, event agent for device[%{public}s] was found.", cid.c_str()); break; - } - else { + } else { sptr systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (systemAbilityMgr == nullptr) { @@ -186,8 +128,7 @@ int32_t NapiSendFinished(const std::string &cid, const std::string &fileName) agent->Emit("sendFinished", reinterpret_cast(&event)); LOGI("DEBUG_SENDFILE:OnSendFinished, [%{public}s] event was done.", cid.c_str()); return 0; - } - else { + } else { LOGI("DEBUG_SENDFILE:OnSendFinished, [%{public}s] no event processor.", cid.c_str()); return -1; } @@ -202,8 +143,7 @@ int32_t NapiSendError(const std::string &cid) if (agent->FindDevice(cid)) { LOGI("DEBUG_SENDFILE:OnSendError, event agent for device[%{public}s] was found.", cid.c_str()); break; - } - else { + } else { sptr systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (systemAbilityMgr == nullptr) { @@ -230,8 +170,7 @@ int32_t NapiSendError(const std::string &cid) agent->Emit("sendFinished", reinterpret_cast(&event)); LOGI("DEBUG_SENDFILE:OnSendError, [%{public}s] event was done.", cid.c_str()); return 0; - } - else { + } else { LOGI("DEBUG_SENDFILE:OnSendError, [%{public}s] no event processor.", cid.c_str()); return -1; } @@ -246,8 +185,7 @@ int32_t NapiReceiveFinished(const std::string &cid, const std::string &fileName, if (agent->FindDevice(cid)) { LOGI("DEBUG_SENDFILE:OnReceiveFinished, event agent for device[%{public}s] was found.", cid.c_str()); break; - } - else { + } else { sptr systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (systemAbilityMgr == nullptr) { @@ -274,8 +212,7 @@ int32_t NapiReceiveFinished(const std::string &cid, const std::string &fileName, agent->Emit("receiveFinished", reinterpret_cast(&event)); LOGI("DEBUG_SENDFILE:OnReceiveFinished, [%{public}s] event was done.", cid.c_str()); return 0; - } - else { + } else { LOGI("DEBUG_SENDFILE:OnReceiveFinished, [%{public}s] no event processor.", cid.c_str()); return -1; } @@ -290,8 +227,7 @@ int32_t NapiReceiveError(const std::string &cid) if (agent->FindDevice(cid)) { LOGI("OnReceiveError : event agent for device[%{public}s] was found.", cid.c_str()); break; - } - else { + } else { sptr systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (systemAbilityMgr == nullptr) { @@ -318,8 +254,7 @@ int32_t NapiReceiveError(const std::string &cid) agent->Emit("receiveFinished", reinterpret_cast(&event)); LOGI("OnReceiveError : [%{public}s] event was done.", cid.c_str()); return 0; - } - else { + } else { LOGI("OnReceiveError : [%{public}s] no event processor.", cid.c_str()); return -1; } @@ -373,7 +308,7 @@ int32_t ExecSendFile(const std::string &deviceId, const std::vector { if (deviceId.empty()) { LOGE("DeviceId can't be emptey.\n"); - return -2; + return -1; } sptr systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); diff --git a/frameworks/js/napi/src/sendfile_napi.cpp b/frameworks/js/napi/src/sendfile_napi.cpp index 47de94c54..ec0e9abba 100644 --- a/frameworks/js/napi/src/sendfile_napi.cpp +++ b/frameworks/js/napi/src/sendfile_napi.cpp @@ -15,8 +15,11 @@ #include "sendfile_napi.h" +#include + #include "filemgmt_libn.h" #include "sendfile.h" +#include "trans_event.h" #include "utils_log.h" namespace OHOS { @@ -26,7 +29,8 @@ using namespace FileManagement::LibN; namespace { constexpr int32_t MAX_SEND_FILE_HAP_NUMBER = 50; constexpr int32_t SENDFILE_NAPI_BUF_LENGTH = 256; - const std::string DfsAppUid{"SendFileTestUid"}; + const std::string DfsAppUid { "SendFileTestUid" }; + static std::mutex g_uidMutex; } napi_value SendFile(napi_env env, napi_callback_info info) @@ -71,7 +75,6 @@ napi_value SendFile(napi_env env, napi_callback_info info) std::string deviceIdString(deviceId.get()); auto cbExec = [deviceIdString, sourPath, destPath, fileCount, resultCode]() -> NError { *resultCode = ExecSendFile(deviceIdString.c_str(), sourPath, destPath, fileCount); - LOGI("cbExec resultCode : %{public}d", *resultCode); return NError(*resultCode); }; @@ -79,11 +82,9 @@ napi_value SendFile(napi_env env, napi_callback_info info) if (err) { return { env, err.GetNapiErr(env) }; } - LOGI("cbComplete resultCode : %{public}d", *resultCode); return { NVal::CreateInt64(env, *resultCode) }; }; - LOGI("JS SendFile created thread params count %{public}d", funcArg.GetArgc()); std::string procedureName = "SendFile"; NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == DFS_ARG_CNT::FOUR) { diff --git a/frameworks/js/napi/src/trans_event.cpp b/frameworks/js/napi/src/trans_event.cpp new file mode 100644 index 000000000..8d452edf1 --- /dev/null +++ b/frameworks/js/napi/src/trans_event.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "trans_event.h" + +#include "utils_log.h" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +napi_value TransEvent::ToJsObject(napi_env env) +{ + napi_value te; + napi_status status = napi_create_object(env, &te); + if (napi_ok == status) { + napi_value err; + status = napi_create_int32(env, errorCode_, &err); + if (napi_ok == status) { + status = napi_set_named_property(env, te, "errorCode", err); + if (napi_ok != status) { + LOGE("ToJsObject: package error code failed.\n"); + return nullptr; + } + } else { + LOGE("ToJsObject: create error code failed.\n"); + return nullptr; + } + + napi_value fname; + status = napi_create_string_utf8(env, fileName_.c_str(), fileName_.size()+1, &fname); + if (napi_ok == status) { + status = napi_set_named_property(env, te, "fileName", fname); + if (napi_ok != status) { + LOGE("ToJsObject: package filename failed.\n"); + return nullptr; + } + } else { + LOGE("ToJsObject: create filename failed.\n"); + return nullptr; + } + + napi_value fcount; + status = napi_create_int32(env, fileCount_, &fcount); + if (napi_ok == status) { + status = napi_set_named_property(env, te, "fileCount", fcount); + if (napi_ok != status) { + LOGE("ToJsObject: package file counts failed.\n"); + return nullptr; + } + } else { + LOGE("ToJsObject: create file counts failed.\n"); + return nullptr; + } + } else { + LOGE("ToJsObject: create object failed.\n"); + return nullptr; + } + + return te; +} +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/include/filetransfer_callback_proxy.h b/frameworks/native/include/filetransfer_callback_proxy.h index cc2423ee6..3a6eb8bef 100644 --- a/frameworks/native/include/filetransfer_callback_proxy.h +++ b/frameworks/native/include/filetransfer_callback_proxy.h @@ -23,8 +23,8 @@ namespace Storage { namespace DistributedFile { class FileTransferCallbackProxy : public IRemoteProxy { public: - explicit FileTransferCallbackProxy(const sptr &impl); - virtual ~FileTransferCallbackProxy(); + explicit FileTransferCallbackProxy(const sptr &impl) : IRemoteProxy(impl) {} + virtual ~FileTransferCallbackProxy() {} public: int32_t DeviceOnline(const std::string &cid) override; diff --git a/frameworks/native/include/service_proxy.h b/frameworks/native/include/service_proxy.h index e1b2561d4..ba922b5a7 100644 --- a/frameworks/native/include/service_proxy.h +++ b/frameworks/native/include/service_proxy.h @@ -30,9 +30,8 @@ public: * * @param impl */ - explicit ServiceProxy(const sptr &impl); - - virtual ~ServiceProxy(); + explicit ServiceProxy(const sptr &impl) : IRemoteProxy(impl) {} + virtual ~ServiceProxy() = default; int32_t SendFile(const std::string &cid, const std::vector &sourceFileList, diff --git a/frameworks/native/src/dfs_filetransfer_callback.cpp b/frameworks/native/src/dfs_filetransfer_callback.cpp index 19dc846e4..ef8aa22a9 100644 --- a/frameworks/native/src/dfs_filetransfer_callback.cpp +++ b/frameworks/native/src/dfs_filetransfer_callback.cpp @@ -15,7 +15,6 @@ #include "dfs_filetransfer_callback.h" -#include #include "sendfile.h" #include "utils_log.h" diff --git a/frameworks/native/src/filetransfer_callback_proxy.cpp b/frameworks/native/src/filetransfer_callback_proxy.cpp index 39cf790bf..1fd15b8e2 100644 --- a/frameworks/native/src/filetransfer_callback_proxy.cpp +++ b/frameworks/native/src/filetransfer_callback_proxy.cpp @@ -19,23 +19,17 @@ namespace OHOS { namespace Storage { namespace DistributedFile { -FileTransferCallbackProxy::FileTransferCallbackProxy(const sptr &impl) - : IRemoteProxy(impl) {} - -FileTransferCallbackProxy::~FileTransferCallbackProxy() {} - int32_t FileTransferCallbackProxy::DeviceOnline(const std::string &cid) { - MessageParcel data; if (!data.WriteInterfaceToken(FileTransferCallbackProxy::GetDescriptor())) { - LOGE("xhl [---FileTransferCallbackProxy---] WriteInterfaceToken failed"); + LOGE("[---FileTransferCallbackProxy---] WriteInterfaceToken failed"); return DFS_CALLBACK_WRITE_DESCRIPTOR_TOKEN_FAIL; } sptr remote = Remote(); if (remote == nullptr) { - LOGE("xhl [---FileTransferCallbackProxy---] Remote is null"); + LOGE("[---FileTransferCallbackProxy---] Remote is null"); return ERR_NULL_OBJECT; } @@ -53,13 +47,13 @@ int32_t FileTransferCallbackProxy::DeviceOffline(const std::string &cid) { MessageParcel data; if (!data.WriteInterfaceToken(FileTransferCallbackProxy::GetDescriptor())) { - LOGE("xhl [---FileTransferCallbackProxy---] WriteInterfaceToken failed"); + LOGE("[---FileTransferCallbackProxy---] WriteInterfaceToken failed"); return 0; } sptr remote = Remote(); if (remote == nullptr) { - LOGE("xhl [---FileTransferCallbackProxy---] Remote is null"); + LOGE("[---FileTransferCallbackProxy---] Remote is null"); return ERR_NULL_OBJECT; } @@ -178,8 +172,6 @@ int32_t FileTransferCallbackProxy::ReceiveError(const std::string &cid) int32_t FileTransferCallbackProxy::WriteFile(int32_t fd, const std::string &fileName) { - LOGI("xhl WriteFile enter"); - LOGI("xhl WriteFile fd %{public}d, %{public}s", fd, fileName.c_str()); MessageParcel data; if (!data.WriteInterfaceToken(FileTransferCallbackProxy::GetDescriptor())) { return DFS_CALLBACK_WRITE_DESCRIPTOR_TOKEN_FAIL; @@ -189,14 +181,14 @@ int32_t FileTransferCallbackProxy::WriteFile(int32_t fd, const std::string &file data.WriteString(fileName); if (Remote() == nullptr) { - LOGE("xhl WriteFile remote object address is null"); + LOGE("WriteFile remote object address is null"); return DFS_CALLBACK_REMOTE_ADDRESS_IS_NULL; } MessageParcel reply; MessageOption option; Remote()->SendRequest(ON_WRITE_FILE, data, reply, option); - LOGI("xhl WriteFile end"); + return reply.ReadInt32(); } } // namespace DistributedFile diff --git a/frameworks/native/src/service_proxy.cpp b/frameworks/native/src/service_proxy.cpp index 746a12a08..4d9918f37 100644 --- a/frameworks/native/src/service_proxy.cpp +++ b/frameworks/native/src/service_proxy.cpp @@ -19,16 +19,11 @@ namespace OHOS { namespace Storage { namespace DistributedFile { -ServiceProxy::ServiceProxy(const sptr &impl) : IRemoteProxy(impl) {} - -ServiceProxy::~ServiceProxy() {} - int32_t ServiceProxy::SendFile(const std::string &cid, const std::vector &sourceFileList, const std::vector &destinationFileList, const uint32_t fileCount) { - LOGI("xhl sendFile enter"); MessageParcel data; if (!data.WriteInterfaceToken(ServiceProxy::GetDescriptor())) { return DFS_WRITE_DESCRIPTOR_TOKEN_FAIL; @@ -47,7 +42,7 @@ int32_t ServiceProxy::SendFile(const std::string &cid, data.WriteUint32(fileCount); if (Remote() == nullptr) { - LOGE("xhl unregister sendfile remote object address is null"); + LOGE("unregister sendfile remote object address is null"); return DFS_REMOTE_ADDRESS_IS_NULL; } @@ -55,18 +50,15 @@ int32_t ServiceProxy::SendFile(const std::string &cid, MessageOption option; int32_t result = Remote()->SendRequest(SEND_FILE_DISTRIBUTED, data, reply, option); if (result != DFS_SENDFILE_SUCCESS) { - LOGE("xhl sendfile error code : %{public}d", result); + LOGE("sendfile error code : %{public}d", result); return result; } - LOGE("xhl sendfile sendRequest done %{public}d", result); return reply.ReadInt32(); } int32_t ServiceProxy::OpenFile(int32_t fd, const std::string &fileName, int32_t mode) { - LOGI("xhl OpenFile enter"); - LOGI("xhl OpenFile fd %{public}d", fd); MessageParcel data; if (!data.WriteInterfaceToken(ServiceProxy::GetDescriptor())) { return DFS_WRITE_DESCRIPTOR_TOKEN_FAIL; @@ -77,70 +69,67 @@ int32_t ServiceProxy::OpenFile(int32_t fd, const std::string &fileName, int32_t data.WriteInt32(mode); if (Remote() == nullptr) { - LOGE("xhl openfile remote object address is null"); + LOGE("openfile remote object address is null"); return DFS_REMOTE_ADDRESS_IS_NULL; } MessageParcel reply; MessageOption option; Remote()->SendRequest(OPEN_FILE_FD, data, reply, option); - LOGI("xhl RegisterNotifyCallback end"); + return reply.ReadInt32(); } int32_t ServiceProxy::RegisterNotifyCallback(sptr &callback) { - LOGI("xhl OpenFile enter"); MessageParcel data; if (!data.WriteInterfaceToken(ServiceProxy::GetDescriptor())) { return DFS_WRITE_DESCRIPTOR_TOKEN_FAIL; } if (callback == nullptr) { - LOGE("xhl The parameter of callback is nullptr"); + LOGE("The parameter of callback is nullptr"); return DFS_CALLBACK_PARAM_ERROR; } if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) { - LOGE("xhl write remote object failed"); + LOGE("write remote object failed"); return DFS_WRITE_REMOTE_OBJECT_FAIL; } if (Remote() == nullptr) { - LOGE("xhl register filetransfer callback remote object address is null"); + LOGE("register filetransfer callback remote object address is null"); return DFS_REMOTE_ADDRESS_IS_NULL; } MessageParcel reply; MessageOption option; Remote()->SendRequest(REGISTER_NOTIFY_CALLBACK, data, reply, option); - LOGI("xhl RegisterNotifyCallback end"); + return reply.ReadInt32(); } int32_t ServiceProxy::UnRegisterNotifyCallback() { - LOGI("xhl UnRegisterNotifyCallback enter"); MessageParcel data; if (!data.WriteInterfaceToken(ServiceProxy::GetDescriptor())) { return DFS_WRITE_DESCRIPTOR_TOKEN_FAIL; } if (Remote() == nullptr) { - LOGE("xhl unregister filetransfer callback remote object address is null"); + LOGE("unregister filetransfer callback remote object address is null"); return DFS_REMOTE_ADDRESS_IS_NULL; } MessageParcel reply; MessageOption option; Remote()->SendRequest(UN_REGISTER_NOTIFY_CALLBACK, data, reply, option); - LOGI("xhl UnRegisterNotifyCallback end"); + return reply.ReadInt32(); } int32_t ServiceProxy::IsDeviceOnline(const std::string &cid) { - LOGE("xhl IsDeviceOnline enter"); MessageParcel data; if (!data.WriteInterfaceToken(ServiceProxy::GetDescriptor())) { return DFS_WRITE_DESCRIPTOR_TOKEN_FAIL; @@ -159,7 +148,6 @@ int32_t ServiceProxy::IsDeviceOnline(const std::string &cid) LOGE("Function RemoveBundleDistributedDirs! errCode:%{public}d", result); return DFS_NO_DEVICE_ONLINE; } - LOGE("xhl IsDeviceOnline done %{public}d", result); return reply.ReadInt32(); } diff --git a/interfaces/innerkits/native/BUILD.gn b/interfaces/innerkits/native/BUILD.gn index a08cf85a6..7b31ee81d 100644 --- a/interfaces/innerkits/native/BUILD.gn +++ b/interfaces/innerkits/native/BUILD.gn @@ -18,11 +18,7 @@ config("private_config") { "../../../frameworks/native/include" ] - # configs = [ "//build/config/compiler:exceptions" ] - defines = [ - "LOG_TAG=\"distributedfile\"", - "LOG_DOMAIN=0xD001600", - ] + configs = [ "//build/config/compiler:exceptions" ] } config("public_config") { @@ -30,7 +26,10 @@ config("public_config") { } ohos_shared_library("libdistributedfile_innerkits") { - sources = [ "${distributedfile_path}/frameworks/native/src/service_proxy.cpp" ] + sources = [ + "${distributedfile_path}/frameworks/native/src/service_proxy.cpp", + "${distributedfile_path}/frameworks/native/src/filetransfer_callback_proxy.cpp", + ] configs = [ ":private_config" ] diff --git a/services/distributedfiledaemon/include/device/device_manager_agent.h b/services/distributedfiledaemon/include/device/device_manager_agent.h index 2e276027d..27617ac03 100644 --- a/services/distributedfiledaemon/include/device/device_manager_agent.h +++ b/services/distributedfiledaemon/include/device/device_manager_agent.h @@ -54,7 +54,7 @@ class DeviceManagerAgent final : public DistributedHardware::DmInitCallback, public Startable, public Actor, public Utils::Singleton { - DECLARE_UTILS_SINGLETON(DeviceManagerAgent); + DECLARE_SINGLETON(DeviceManagerAgent); public: void Start() override; diff --git a/services/distributedfiledaemon/include/mountpoint/mount_manager.h b/services/distributedfiledaemon/include/mountpoint/mount_manager.h index 2d3b2939f..7b04f10c3 100644 --- a/services/distributedfiledaemon/include/mountpoint/mount_manager.h +++ b/services/distributedfiledaemon/include/mountpoint/mount_manager.h @@ -31,7 +31,7 @@ public: void Mount(std::unique_ptr mp); void Umount(std::weak_ptr wmp); void Umount(const std::string &groupId); - DECLARE_UTILS_SINGLETON(MountManager); + DECLARE_SINGLETON(MountManager); private: void StartInstance() override {} diff --git a/services/distributedfileservice/BUILD.gn b/services/distributedfileservice/BUILD.gn index ecc81370a..18d89bec2 100644 --- a/services/distributedfileservice/BUILD.gn +++ b/services/distributedfileservice/BUILD.gn @@ -19,13 +19,8 @@ ohos_shared_library("libdistributedfileservice") { "include/ipc", "include/network", "${distributedfile_path}/frameworks/native", - "//foundation/ace/napi/interfaces/kits", - "//third_party/node/src", - "//third_party/libuv/include", + "${distributedfile_path}/frameworks/native/include", "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", - "//foundation/filemanagement/dfs_service/frameworks/native/include", - "//foundation/filemanagement/dfs_service/frameworks/native/native_module_sendfile", - "//foundation/filemanagement/dfs_service/frameworks/js/napi/common", ] sources = [ @@ -34,21 +29,18 @@ ohos_shared_library("libdistributedfileservice") { "src/device/device_manager_agent.cpp", "src/network/softbus_agent.cpp", "src/network/softbus_dispatcher.cpp", - "${distributedfile_path}/frameworks/native/src/service_proxy.cpp", - "${distributedfile_path}/frameworks/native/src/filetransfer_callback_proxy.cpp", ] configs = [ "${utils_path}:compiler_configs" ] external_deps = [ - "ability_runtime:app_manager", - "ability_runtime:want", + "ability_base:want", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", + "dsoftbus_standard:softbus_client", "ipc:ipc_core", "safwk:system_ability_fwk", "samgr_standard:samgr_proxy", - "dsoftbus_standard:softbus_client", ] deps = [ diff --git a/services/distributedfileservice/include/device/device_manager_agent.h b/services/distributedfileservice/include/device/device_manager_agent.h index df2cd1fbf..b2a11da1a 100644 --- a/services/distributedfileservice/include/device/device_manager_agent.h +++ b/services/distributedfileservice/include/device/device_manager_agent.h @@ -33,7 +33,7 @@ class DeviceManagerAgent : public DistributedHardware::DmInitCallback, public DistributedHardware::DeviceStateCallback, public std::enable_shared_from_this, public Utils::Singleton { - DECLARE_UTILS_SINGLETON(DeviceManagerAgent); + DECLARE_SINGLETON(DeviceManagerAgent); public: void OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo) override; @@ -57,8 +57,6 @@ private: std::mutex devsRecordMutex_; std::set alreadyOnlineDev_; - - std::string pkgName_{"ohos.storage.distributedfile.service"}; }; } // namespace DistributedFile } // namespace Storage diff --git a/services/distributedfileservice/include/ipc/distributedfile_service.h b/services/distributedfileservice/include/ipc/distributedfile_service.h index 70c2dcbd9..6e5d68f6d 100644 --- a/services/distributedfileservice/include/ipc/distributedfile_service.h +++ b/services/distributedfileservice/include/ipc/distributedfile_service.h @@ -18,7 +18,6 @@ #include "distributedfile_service_stub.h" #include "iremote_stub.h" -#include "singleton.h" #include "system_ability.h" #include @@ -29,10 +28,10 @@ namespace DistributedFile { class DistributedFileService : public SystemAbility, public DistributedFileServiceStub, public std::enable_shared_from_this { - DECLARE_DELAYED_SINGLETON(DistributedFileService) DECLARE_SYSTEM_ABILITY(DistributedFileService) public: explicit DistributedFileService(int32_t saId, bool runOnCreate = true) : SystemAbility(saId, runOnCreate) {} + ~DistributedFileService() {} void OnDump() override; void OnStart() override; void OnStop() override; diff --git a/services/distributedfileservice/include/network/softbus_agent.h b/services/distributedfileservice/include/network/softbus_agent.h index eac1dbb77..13a722371 100644 --- a/services/distributedfileservice/include/network/softbus_agent.h +++ b/services/distributedfileservice/include/network/softbus_agent.h @@ -16,18 +16,18 @@ #ifndef DFS_SOFTBUS_AGENT_H #define DFS_SOFTBUS_AGENT_H -#include "utils_singleton.h" -#include "i_filetransfer_callback.h" #include #include #include + +#include "i_filetransfer_callback.h" #include "utils_singleton.h" namespace OHOS { namespace Storage { namespace DistributedFile { class SoftbusAgent : public std::enable_shared_from_this, public Utils::Singleton { - DECLARE_UTILS_SINGLETON(SoftbusAgent); + DECLARE_SINGLETON(SoftbusAgent); public: void RegisterSessionListener(); diff --git a/services/distributedfileservice/src/ipc/distributedfile_service.cpp b/services/distributedfileservice/src/ipc/distributedfile_service.cpp index a00a1f0a1..cff8adb95 100644 --- a/services/distributedfileservice/src/ipc/distributedfile_service.cpp +++ b/services/distributedfileservice/src/ipc/distributedfile_service.cpp @@ -17,21 +17,19 @@ #include #include #include -#include -#include #include "bundle_mgr_interface.h" #include "bundle_mgr_proxy.h" +#include "device_manager_agent.h" #include "ipc_skeleton.h" #include "iservice_registry.h" #include "message_parcel.h" #include "parcel.h" +#include "softbus_agent.h" +#include "system_ability_definition.h" #include "utils_directory.h" #include "utils_log.h" -#include "softbus_agent.h" -#include "device_manager_agent.h" - namespace OHOS { namespace Storage { namespace DistributedFile { @@ -42,10 +40,6 @@ namespace { REGISTER_SYSTEM_ABILITY_BY_ID(DistributedFileService, STORAGE_DISTRIBUTED_FILE_SERVICE_SA_ID, false); -DistributedFileService::DistributedFileService() : SystemAbility(STORAGE_DISTRIBUTED_FILE_SERVICE_SA_ID, false) {} - -DistributedFileService::~DistributedFileService() {} - void DistributedFileService::OnDump() { LOGI("OnDump"); @@ -64,7 +58,7 @@ void DistributedFileService::OnStop() void DistributedFileService::PublishSA() { - bool ret = SystemAbility::Publish(DelayedSingleton::GetInstance().get()); + bool ret = SystemAbility::Publish(this); if (!ret) { LOGE("Leave, publishing DistributedFileService failed!"); return; @@ -81,7 +75,10 @@ int32_t DistributedFileService::SendFile(const std::string &cid, const std::vector &destinationFileList, const uint32_t fileCount) { - char **sFileList = new char*[fileCount]; + if (fileCount < 0) { + return -1; + } + 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()); @@ -89,26 +86,39 @@ int32_t DistributedFileService::SendFile(const std::string &cid, std::string tmpString("/data/system_ce/tmp"); int32_t length = tmpString.length(); sFileList[index] = new char[length + 1]; - memset_s(sFileList[index], length + 1, '\0', length + 1); - memcpy_s(sFileList[index], length + 1, tmpString.c_str(), length); + if (memset_s(sFileList[index], length + 1, '\0', length + 1) != EOK) { + return -1; + } + if (memcpy_s(sFileList[index], length + 1, tmpString.c_str(), length) != EOK) { + return -1; + } sFileList[index][length] = '\0'; } else { int32_t length = sourceFileList.at(index).length(); sFileList[index] = new char[length + 1]; - memset_s(sFileList[index], length + 1, '\0', length + 1); - memcpy_s(sFileList[index], length + 1, sourceFileList.at(index).c_str(), length); + if (memset_s(sFileList[index], length + 1, '\0', length + 1) != EOK) { + return -1; + } + if (memcpy_s(sFileList[index], length + 1, sourceFileList.at(index).c_str(), length) != EOK) { + return -1; + } sFileList[index][length] = '\0'; } } - char **dFileList = new char*[fileCount]; + char **dFileList = new char* [fileCount]; for (int index = 0; index < destinationFileList.size(); ++index) { LOGI("DistributedFileService::SendFile Destination File List %{public}d, %{public}s", index, destinationFileList.at(index).c_str()); int32_t length = destinationFileList.at(index).length(); dFileList[index] = new char[length + 1]; - memset_s(dFileList[index], length + 1, '\0', length + 1); - memcpy_s(dFileList[index], length + 1, destinationFileList.at(index).c_str(), length); + if (memset_s(dFileList[index], length + 1, '\0', length + 1) != EOK) { + return -1; + } + if (memcpy_s(dFileList[index], length + 1, destinationFileList.at(index).c_str(), length) != EOK) { + LOGE("memory copy failed"); + return -1; + } dFileList[index][length] = '\0'; } diff --git a/utils/system/include/utils_singleton.h b/utils/system/include/utils_singleton.h index f4c0a2eb4..ffa1b58b5 100644 --- a/utils/system/include/utils_singleton.h +++ b/utils/system/include/utils_singleton.h @@ -25,7 +25,7 @@ namespace OHOS { namespace Storage { namespace DistributedFile { namespace Utils { -#define DECLARE_UTILS_SINGLETON(MyClass) \ +#define DECLARE_SINGLETON(MyClass) \ public: \ ~MyClass(); \ MyClass(const MyClass&) = delete; \ -- Gitee