From 584f04944e5b15f615298e5879993ee4edf61252 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=B6=E9=87=91=E6=B2=9B?= Date: Sat, 26 Jul 2025 14:41:18 +0800 Subject: [PATCH] dfs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 陶金沛 Change-Id: I243fe7378e63b1033f49f28e126eefbe4fe978ec --- .../mod_fs/properties/ani/connectdfs_ani.cpp | 95 +++++++++++++++++++ .../mod_fs/properties/ani/connectdfs_ani.h | 54 +++++++++++ .../properties/ani/disconnectdfs_ani.cpp | 46 +++++++++ .../mod_fs/properties/ani/disconnectdfs_ani.h | 38 ++++++++ .../src/mod_fs/properties/connectdfs_core.cpp | 50 ++++++++++ .../src/mod_fs/properties/connectdfs_core.h | 41 ++++++++ .../mod_fs/properties/disconnectdfs_core.cpp | 50 ++++++++++ .../mod_fs/properties/disconnectdfs_core.h | 33 +++++++ 8 files changed, 407 insertions(+) create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/connectdfs_ani.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/connectdfs_ani.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/disconnectdfs_ani.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/ani/disconnectdfs_ani.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/connectdfs_core.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/connectdfs_core.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/disconnectdfs_core.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/disconnectdfs_core.h diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/connectdfs_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/connectdfs_ani.cpp new file mode 100644 index 000000000..da3f63b52 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/connectdfs_ani.cpp @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2025 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 "connectdfs_ani.h" +#include "connectdfs_core.h" +#include "filemgmt_libhilog.h" +#include "error_handler.h" +#include "type_converter.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +using namespace std; +void ConnectDfsAni::ConnectDfsSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string networkId, + ani_object listener) +{ + //注册 + auto [succNetworkId, networkIdStr] = TypeConverter::ToUTF8String(env, networkId); + if (!succNetworkId) { + HILOGE("Invalid NetworkId"); + ErrorHandler::Throw(env, E_PARAMS); + return; + } + sptr anidfslistener_(new(std::nothrow) ANIDfsListener()); + anidfslistener_->SetConnectDfsEnv(env); + ani_ref promiseDeferred = nullptr; + + if (listener != nullptr) { + anidfslistener_->SetConnectDfsCBRef(listener); + HILOGE("Connectdfs set callback success"); + } else { + anidfslistener_->SetConnectDfsPromiseRef(promiseDeferred); + HILOGE("Connectdfs set Promise success"); + } + + auto ret = ConnectDfsCore::ConnectDfsExec(networkIdStr, anidfslistener_); + if (!ret.IsSuccess()) { + HILOGE("ConnectDfsExec failed"); + ErrorHandler::Throw(env, E_PARAMS); + return; + } +} + +void ANIDfsListener::SetConnectDfsEnv(ani_env *env) +{ + env_ = env; +} + +void ANIDfsListener::SetConnectDfsCBRef(ani_ref ref) +{ + onStatusRef_ = ref; +} + +void ANIDfsListener::SetConnectDfsPromiseRef(ani_ref promiseDeferred) +{ + promiseDeferred_ = promiseDeferred; +} + +void ANIDfsListener::OnStatus(const std::string &networkId, int32_t status) +{ + //回调 + ani_string networkId_; + ani_status res = env_->String_NewUTF8(networkId.c_str(), networkId.size(), &networkId_); + if (res != ANI_OK) { + HILOGE("std string to ani string failed. ret = %{public}d", static_cast(res)); + ErrorHandler::Throw(env_, E_PARAMS); + return; + } + ani_double status_ = static_cast(status); + ani_status ret = env_->Object_CallMethodByName_Void(static_cast(objectlistener), "onStatus", nullptr, + networkId_, status_); + if (ret != ANI_OK) { + HILOGE("Object_CallMethodByName_Void failed. ret = %{public}d", static_cast(ret)); + ErrorHandler::Throw(env_, E_PARAMS); + return; + } +} +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/connectdfs_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/connectdfs_ani.h new file mode 100644 index 000000000..b215b37aa --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/connectdfs_ani.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 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 INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_CONNECTDFS_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_CONNECTDFS_ANI_H + +#include +#include +#include "dfs_listener/file_dfs_listener_stub.h" +#include "distributed_file_daemon_manager.h" +#include "filemgmt_libfs.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { +using namespace std; + +class ANIDfsListener : public FileDfsListenerStub { +public: + void OnStatus(const std::string &networkId, int32_t status); + void SetConnectDfsEnv(ani_env *env); + void SetConnectDfsCBRef(ani_ref ref); + void SetConnectDfsPromiseRef(ani_ref promiseDeferred); + +private: + ani_env *env_ = nullptr; + ani_ref onStatusRef_ = nullptr; + ani_ref promiseDeferred_ = nullptr; + ani_ref objectlistener = nullptr; +}; +class ConnectDfsAni final { +public: + static void ConnectDfsSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string networkId, + ani_object listener); +}; + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_CONNECTDFS_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/disconnectdfs_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/disconnectdfs_ani.cpp new file mode 100644 index 000000000..7beafaeaa --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/disconnectdfs_ani.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2025 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 "disconnectdfs_ani.h" +#include "disconnectdfs_core.h" +#include "filemgmt_libhilog.h" +#include "error_handler.h" +#include "type_converter.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +using namespace std; +void DisConnectDfsAni::DisConnectDfsSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string networkId) +{ + auto [succNetworkId, networkIdStr] = TypeConverter::ToUTF8String(env, networkId); + if (!succNetworkId) { + HILOGE("Invalid NetworkId"); + ErrorHandler::Throw(env, E_PARAMS); + return; + } + auto ret = DisConnectDfsCore::DisConnectDfsExec(networkIdStr); + if (!ret.IsSuccess()) { + HILOGE("DisConnectDfsExec failed"); + ErrorHandler::Throw(env, E_PARAMS); + return; + } +} +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/disconnectdfs_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/disconnectdfs_ani.h new file mode 100644 index 000000000..a42dab9cc --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/disconnectdfs_ani.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2025 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 INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_DISCONNECTDFS_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_DISCONNECTDFS_ANI_H + +#include +#include "dfs_listener/file_dfs_listener_stub.h" +#include "distributed_file_daemon_manager.h" +#include "filemgmt_libfs.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +class DisConnectDfsAni final { +public: + static void DisConnectDfsSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string networkId); +}; + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_ANI_DISCONNECTDFS_ANI_H diff --git a/interfaces/kits/js/src/mod_fs/properties/connectdfs_core.cpp b/interfaces/kits/js/src/mod_fs/properties/connectdfs_core.cpp new file mode 100644 index 000000000..999a460b8 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/connectdfs_core.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2025 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include "filemgmt_libhilog.h" +#include "distributed_file_daemon_manager.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace fs = std::filesystem; + +FsResult ConnectDfsCore::ConnectDfsExec(const std::string &networkId, sptr listener) +{ + if (networkId == "" || listener == nullptr) { + return FsResult::Error(E_PARAMS); + } + + int result = Storage::DistributedFile::DistributedFileDaemonManager::GetInstance(). + OpenP2PConnectionEx(networkId, listener); + if (result != ERRNO_NOERR) { + HILOGE("Fail to openp2pconnection"); + return FsResult::Error(result); + } + return FsResult::Success(); +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/connectdfs_core.h b/interfaces/kits/js/src/mod_fs/properties/connectdfs_core.h new file mode 100644 index 000000000..9e6d713bc --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/connectdfs_core.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2025 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 INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_CONNECTDFS_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_CONNECTDFS_CORE_H +#include +#include +#include +#include +#include "bundle_mgr_client_impl.h" +#include "dfs_listener/file_dfs_listener_stub.h" +#include "distributed_file_daemon_manager.h" +#include "filemgmt_libfs.h" +#include "fs_utils.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +class ConnectDfsCore final { +public: + static FsResult ConnectDfsExec(const std::string &networkId, sptr listener); +}; + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_CONNECTDFS_CORE_H + diff --git a/interfaces/kits/js/src/mod_fs/properties/disconnectdfs_core.cpp b/interfaces/kits/js/src/mod_fs/properties/disconnectdfs_core.cpp new file mode 100644 index 000000000..92020ae37 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/disconnectdfs_core.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2025 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include "filemgmt_libhilog.h" +#include "distributed_file_daemon_manager.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace fs = std::filesystem; + +FsResult DisConnectDfsCore::DisConnectDfsExec(const std::string &networkId) +{ + if (networkId == "") { + return FsResult::Error(E_PARAMS); + } + + int result = Storage::DistributedFile::DistributedFileDaemonManager::GetInstance(). + CloseP2PConnectionEx(networkId); + if (result != ERRNO_NOERR) { + HILOGE("Fail to Closep2pconnection"); + return FsResult::Error(result); + } + return FsResult::Success(); +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/disconnectdfs_core.h b/interfaces/kits/js/src/mod_fs/properties/disconnectdfs_core.h new file mode 100644 index 000000000..0cc11a1b9 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/disconnectdfs_core.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2025 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 INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_DISCONNECTDFS_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_DISCONNECTDFS_CORE_H + +#include "filemgmt_libfs.h" +#include "fs_utils.h" +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { + +class DisConnectDfsCore final { +public: + static FsResult DisConnectDfsExec(const std::string &networkId); +}; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_DISCONNECTDFS_CORE_H \ No newline at end of file -- Gitee