From 5e208bf3d453804c89236818fb890d6e864a4824 Mon Sep 17 00:00:00 2001 From: zhangyaomaggie Date: Tue, 14 May 2024 23:14:46 +0800 Subject: [PATCH] file sync after compile Signed-off-by: zhangyaomaggie --- interfaces/kits/js/BUILD.gn | 3 + .../js/src/mod_fs/properties/connectdfs.cpp | 319 ++++++++++++++++++ .../js/src/mod_fs/properties/connectdfs.h | 98 ++++++ .../dfs_listener/file_dfs_listener_stub.cpp | 67 ++++ .../dfs_listener/file_dfs_listener_stub.h | 46 +++ .../src/mod_fs/properties/disconnectdfs.cpp | 90 +++++ .../js/src/mod_fs/properties/disconnectdfs.h | 49 +++ .../src/mod_fs/properties/prop_n_exporter.cpp | 4 + utils/filemgmt_libn/include/n_error.h | 12 +- 9 files changed, 687 insertions(+), 1 deletion(-) create mode 100644 interfaces/kits/js/src/mod_fs/properties/connectdfs.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/connectdfs.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/dfs_listener/file_dfs_listener_stub.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/dfs_listener/file_dfs_listener_stub.h create mode 100644 interfaces/kits/js/src/mod_fs/properties/disconnectdfs.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/disconnectdfs.h diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 83763238a..c742cdca2 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -212,6 +212,9 @@ ohos_shared_library("fs") { "src/mod_fs/properties/read_text.cpp", "src/mod_fs/properties/symlink.cpp", "src/mod_fs/properties/watcher.cpp", + "src/mod_fs/properties/connectdfs.cpp", + "src/mod_fs/properties/disconnectdfs.cpp", + "src/mod_fs/properties/dfs_listener/file_dfs_listener_stub.cpp", ] external_deps += [ "ability_base:zuri", diff --git a/interfaces/kits/js/src/mod_fs/properties/connectdfs.cpp b/interfaces/kits/js/src/mod_fs/properties/connectdfs.cpp new file mode 100644 index 000000000..e69e23584 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/connectdfs.cpp @@ -0,0 +1,319 @@ +/* + * Copyright (c) 2023-2024 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.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include "common_func.h" +#include "filemgmt_libhilog.h" +#include "distributed_file_daemon_manager.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace fs = std::filesystem; + +ConnectDfsCB *ConnectDfs::CreateConnectDfsCBCBInfo(napi_env &env) +{ + HILOGI("zhangyao::CreateConnectDfsCBCBInfo called"); + auto connectDfsCB = new(std::nothrow) ConnectDfsCB; + if (connectDfsCB == nullptr) { + HILOGE("zhangyao::CreateConnectDfsCBCBInfo failed, connectDfsCB == nullptr"); + } + connectDfsCB->cbBase.cbInfo.env = env; + connectDfsCB->cbBase.asyncWork = nullptr; + connectDfsCB->cbBase.deferred = nullptr; + connectDfsCB->callbackRef = nullptr; + HILOGI("zhangyao::CreateConnectDfsCBCBInfo end"); + return connectDfsCB; +} + +void cbExec(napi_env env, void *data) +{ + HILOGI("zhangyao::cbExec for connectDfs called"); + auto connectDfsCB = static_cast(data); + HILOGI("zhangyao::create connectDfsCB success"); + sptr dfsListeners(new (std::nothrow) NAPIDfsListener()); + connectDfsCB->jsCallbackObject = dfsListeners; + if (connectDfsCB->jsCallbackObject == nullptr) { + HILOGE("zhangyao::connectDfsCB == nullptr"); + return; + } + connectDfsCB->jsCallbackObject->SetConnectDfsEnv(env); + HILOGI("zhangyao::connectDfsCB set env success"); + if (connectDfsCB->dfsConnectCB.callback[0] != nullptr) { + connectDfsCB->jsCallbackObject-> + SetConnectDfsCBRef(connectDfsCB->dfsConnectCB.callback[0]); + HILOGI("zhangyao::connectDfsCB set callback success"); + } else { + connectDfsCB->jsCallbackObject-> + SetConnectDfsPromiseRef(connectDfsCB->cbBase.deferred); + HILOGI("zhangyao::connectDfsCB set promise success"); + } + + connectDfsCB->result = -1; + connectDfsCB->result = Storage::DistributedFile::DistributedFileDaemonManager::GetInstance(). + OpenP2PConnectionEx(connectDfsCB->networkId, connectDfsCB->jsCallbackObject); + HILOGI("zhangyao:: cbExec end ret = %{public}d", connectDfsCB->result); +} + +void cbCompl(napi_env env, napi_status status, void *data) +{ + HILOGI("zhangyao::cbCompl for connectDfs called"); + auto connectDfsCB = static_cast(data); + napi_value result[2] = { nullptr }; + napi_get_undefined(env, &result[1]); + if (connectDfsCB->result == 0) { + napi_get_undefined(env, &result[0]); + napi_resolve_deferred(env, connectDfsCB->cbBase.deferred, result[1]); + } else { + result[0] = NError(connectDfsCB->result).GetNapiErr(env); + napi_reject_deferred(env, connectDfsCB->cbBase.deferred, result[0]); + } + napi_delete_async_work(env, connectDfsCB->cbBase.asyncWork); + delete connectDfsCB; + connectDfsCB = nullptr; + HILOGI("zhangyao::cbCompl for connectDfs end"); +} + +tuple ConnectDfs::GetListenerFromOptionArg(napi_env env, const NFuncArg &funcArg) +{ + NVal op(env, funcArg[NARG_POS::SECOND]); + if (op.HasProp("onStatus") && !op.GetProp("onStatus").TypeIs(napi_undefined)) { + if (!op.GetProp("onStatus").TypeIs(napi_function)) { + HILOGE("Illegal dfsListeners.onStatus type"); + return { false, NVal() }; + } + return { true, op.GetProp("onStatus") }; + } + return { true, NVal() }; +} + +tuple ConnectDfs::ParseJsOperand(napi_env env, NVal paramFromJsArg) +{ + auto [succ, param, ignore] = paramFromJsArg.ToUTF8String(); + if (!succ) { + HILOGE("parse parameter failed."); + return { false, "" }; + } + std::string paramStr = std::string(param.get()); + return { true, paramStr }; +} + +int ConnectDfs::ParseJsParam(napi_env env, NFuncArg &funcArg, ConnectDfsCB *connectDfsCB) +{ + if (!funcArg.InitArgs(NARG_CNT::TWO)) { + HILOGE("Number of arguments unmatched"); + return E_PARAMS; + } + auto [succNetworkId, networkId] = ParseJsOperand(env, { env, funcArg[NARG_POS::FIRST] }); + auto [succDfsListeners, dfsListeners] = GetListenerFromOptionArg(env, funcArg); + if (!succNetworkId) { + HILOGE("The first argument requires networkId"); + return E_PARAMS; + } + if (!succDfsListeners) { + HILOGE("The second argument requires napi_function"); + return E_PARAMS; + } + connectDfsCB->networkId = networkId; + napi_create_reference(env, dfsListeners.val_, 1, &connectDfsCB->dfsConnectCB.callback[0]); + return ERRNO_NOERR; +} + +napi_value ConnectDfs::Async(napi_env env, napi_callback_info info) +{ + HILOGI("zhangyao::ConnectDfs::Async called"); + ConnectDfsCB *connectDfsCB = CreateConnectDfsCBCBInfo(env); + if (connectDfsCB == nullptr) { + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + NFuncArg funcArg(env, info); + auto result = ParseJsParam(env, funcArg, connectDfsCB); + if (result != ERRNO_NOERR) { + NError(result).ThrowErr(env); + return nullptr; + } + + napi_value ret = nullptr; + napi_create_promise(env, &connectDfsCB->cbBase.deferred, &ret); + napi_create_async_work(env, nullptr, NVal::CreateUTF8String(env, "ResourceName").val_, + cbExec, cbCompl, static_cast(connectDfsCB), &connectDfsCB->cbBase.asyncWork); + napi_queue_async_work(env, connectDfsCB->cbBase.asyncWork); + if (ret == nullptr) { + HILOGE("zhangyao::napi_async_work ret = nullptr"); + delete connectDfsCB; + connectDfsCB = nullptr; + NError(E_PARAMS).ThrowErr(env); + return NVal::CreateUndefined(env).val_; + } + HILOGI("zhangyao::ConnectDfs end"); + return ret; +} + +ConnectDfsCB *CheckAndGetParameters(uv_work_t *work, napi_handle_scope *scope) +{ + HILOGI("zhangyao::ConnectDfsCB::CheckAndGetParameters GetParam called"); + if (work == nullptr) { + HILOGE("zhangyao::ConnectDfsCB, GetParam work is null"); + return nullptr; + } + ConnectDfsCB *connectDfsCB = static_cast(work->data); + if (connectDfsCB == nullptr) { + HILOGE("zhangyao::ConnectDfsCB, GetParam connectDfsCB is null"); + delete work; + return nullptr; + } + napi_open_handle_scope(connectDfsCB->cbBase.cbInfo.env, scope); + if (scope == nullptr) { + delete connectDfsCB; + connectDfsCB = nullptr; + delete work; + return nullptr; + } + HILOGI("zhangyao::ConnectDfsCB::CheckAndGetParameters GetParam end"); + return connectDfsCB; +} + +void NAPIDfsListener::SetConnectDfsEnv(const napi_env &env) +{ + env_ = env; +} + +void NAPIDfsListener::SetConnectDfsCBRef(const napi_ref &ref) +{ + onStatusRef_ = ref; +} + +void NAPIDfsListener::SetConnectDfsPromiseRef(const napi_deferred &promiseDeferred) +{ + promiseDeferred_ = promiseDeferred; +} + +napi_value WrapInt32(napi_env &env, int32_t num, const std::string ¶mName) +{ + HILOGI("zhangyao::WrapInt32 called"); + napi_value jsObject = nullptr; + napi_create_object(env, &jsObject); + napi_value jsValue = nullptr; + HILOGD("zhangyao::WrapInt32 called. %{public}s = %{public}d", paramName.c_str(), num); + napi_create_int32(env, num, &jsValue); + napi_set_named_property(env, jsObject, paramName.c_str(), jsValue); + + return jsObject; +} + +napi_value WrapString(napi_env &env, const std::string ¶m, const std::string ¶mName) +{ + HILOGI("zhangyao::WrapString called"); + napi_value jsValue = nullptr; + HILOGD("zhangyao::WrapString called. %{public}s = %{public}s", paramName.c_str(), param.c_str()); + napi_create_string_utf8(env, param.c_str(), NAPI_AUTO_LENGTH, &jsValue); + + return jsValue; +} + +void UvWorkAfterOnStaus(uv_work_t *work, int status) +{ + HILOGI("zhangyao::UvWorkAfterOnStaus called"); + napi_handle_scope scope = nullptr; + ConnectDfsCB *connectDfsCB = CheckAndGetParameters(work, &scope); + if (connectDfsCB == nullptr) { + return; + } + HILOGI("zhangyao::UvWorkAfterOnStaus, status = %{public}d", connectDfsCB->status); + + napi_value result[2] = {nullptr}; + result[0] = WrapString(connectDfsCB->cbBase.cbInfo.env, connectDfsCB->networkId.c_str(), "networkId"); + result[1] = WrapInt32(connectDfsCB->cbBase.cbInfo.env, connectDfsCB->status, "status"); + if (connectDfsCB->cbBase.deferred == nullptr) { + napi_value callback = nullptr; + napi_value undefined = nullptr; + napi_get_undefined(connectDfsCB->cbBase.cbInfo.env, &undefined); + napi_value callResult = nullptr; + napi_get_reference_value(connectDfsCB->cbBase.cbInfo.env, + connectDfsCB->cbBase.cbInfo.callback, &callback); + napi_call_function(connectDfsCB->cbBase.cbInfo.env, undefined, callback, 1, result, &callResult); + if (connectDfsCB->cbBase.cbInfo.callback != nullptr) { + napi_delete_reference(connectDfsCB->cbBase.cbInfo.env, connectDfsCB->cbBase.cbInfo.callback); + } + } else { + napi_value result[2] = { nullptr }; + napi_get_undefined(connectDfsCB->cbBase.cbInfo.env, &result[1]); + if (connectDfsCB->status == 0) { + napi_resolve_deferred(connectDfsCB->cbBase.cbInfo.env, connectDfsCB->cbBase.deferred, result[1]); + } else { + result[0] = NError(connectDfsCB->status).GetNapiErr(connectDfsCB->cbBase.cbInfo.env); + napi_reject_deferred(connectDfsCB->cbBase.cbInfo.env, connectDfsCB->cbBase.deferred, result[0]); + } + } + + napi_close_handle_scope(connectDfsCB->cbBase.cbInfo.env, scope); + delete connectDfsCB; + connectDfsCB = nullptr; + delete work; + HILOGI("zhangyao::UvWorkAfterOnStaus end"); +} + +void NAPIDfsListener::OnStatus(const std::string &networkId, int32_t status) +{ + HILOGI("zhangyao::NAPIDfsListener::OnStatus called, networkId = %{public}s, \ + status = %{public}d", networkId.c_str(), status); + uv_loop_s *loop = nullptr; + + napi_get_uv_event_loop(env_, &loop); + if (loop == nullptr) { + HILOGE("zhangyao::NAPIDfsListener::OnStatus, loop == nullptr"); + return; + } + + uv_work_t *work = new uv_work_t; + + auto connectDfsCB = new (std::nothrow) ConnectDfsCB; + if (connectDfsCB == nullptr) { + HILOGE("zhangyao::NAPIDfsListener::OnStatus, connectDfsCb == nullptr"); + delete work; + return; + } + connectDfsCB->cbBase.cbInfo.env = env_; + if (onStatusRef_ != nullptr) { + connectDfsCB->cbBase.cbInfo.callback = onStatusRef_; + } else { + connectDfsCB->cbBase.deferred = promiseDeferred_; + } + connectDfsCB->networkId = networkId; + connectDfsCB->status = status; + work->data = static_cast(connectDfsCB); + + int rev = uv_queue_work( + loop, work, [](uv_work_t *work) {}, UvWorkAfterOnStaus); + if (rev != 0) { + delete connectDfsCB; + connectDfsCB = nullptr; + delete work; + } + HILOGI("zhangyao::NAPIDfsListener::OnStatus end"); +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/connectdfs.h b/interfaces/kits/js/src/mod_fs/properties/connectdfs.h new file mode 100644 index 000000000..68e626006 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/connectdfs.h @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2024 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 FILEMANAGEMENT_FILE_API_CONNECTDFS_H +#define FILEMANAGEMENT_FILE_API_CONNECTDFS_H + +#include +#include +#include +#include +#include "bundle_mgr_client_impl.h" +#include "common_func.h" +#include "filemgmt_libn.h" +#include "n_async/n_ref.h" +#include "distributed_file_daemon_manager.h" +#include "dfs_listener/file_dfs_listener_stub.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +using namespace OHOS::FileManagement::LibN; +using namespace OHOS::AppExecFwk; + +napi_value WrapInt32(napi_env &env, int32_t num, const std::string ¶mName); +napi_value WrapString(napi_env &env, const std::string ¶m, const std::string ¶mName); + +struct CallbackInfo { + napi_env env; + napi_ref callback; + napi_deferred deferred; +}; + +struct CBBase { + CallbackInfo cbInfo; + napi_async_work asyncWork = nullptr; + napi_deferred deferred = nullptr; +}; + +struct DfsConnectCB { + napi_env env; + napi_ref callback[1] = {nullptr}; +}; + +class NAPIDfsListener : public FileDfsListenerStub { +public: + void OnStatus(const std::string &networkId, int32_t status) override; + void SetConnectDfsEnv(const napi_env &env); + void SetConnectDfsCBRef(const napi_ref &ref); + void SetConnectDfsPromiseRef(const napi_deferred &promiseDeferred); + +private: + napi_env env_ = nullptr; + napi_ref onStatusRef_ = nullptr; + napi_deferred promiseDeferred_ = nullptr; +}; + +struct ConnectDfsCB { + CBBase cbBase; + std::string networkId; + sptr jsCallbackObject; + DfsConnectCB dfsConnectCB; + int result = 0; + int status = 0; + napi_ref callbackRef; +}; + +ConnectDfsCB *CheckAndGetParameters(uv_work_t *work, napi_handle_scope *scope); + +class ConnectDfs final { +public: + static napi_value Async(napi_env env, napi_callback_info info); + +private: + // operator of napi + static tuple ParseJsOperand(napi_env env, NVal paramFromJsArg); + static tuple GetListenerFromOptionArg(napi_env env, const NFuncArg &funcArg); + static int ParseJsParam(napi_env env, NFuncArg &funcArg, ConnectDfsCB *connectDfsCB); + static ConnectDfsCB *CreateConnectDfsCBCBInfo(napi_env &env); +}; + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // FILEMANAGEMENT_FILE_API_CONNECTDFS_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/dfs_listener/file_dfs_listener_stub.cpp b/interfaces/kits/js/src/mod_fs/properties/dfs_listener/file_dfs_listener_stub.cpp new file mode 100644 index 000000000..080a6349c --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/dfs_listener/file_dfs_listener_stub.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2024 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 "file_dfs_listener_stub.h" +#include "file_dfs_listener_interface_code.h" +#include "filemgmt_libhilog.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace FileManagement; +constexpr int NO_ERROR = 0; +constexpr int E_INVAL_ARG = 1; + +FileDfsListenerStub::FileDfsListenerStub() +{ + opToInterfaceMap_[static_cast + (Storage::DistributedFile::FileDfsListenerInterfaceCode::FILE_DFS_LISTENER_ON_STATUS)] = + &FileDfsListenerStub::HandleOnStatus; +} + +int32_t FileDfsListenerStub::OnRemoteRequest(uint32_t code, + MessageParcel &data, + MessageParcel &reply, + MessageOption &option) +{ + if (data.ReadInterfaceToken() != GetDescriptor()) { + return FILE_DFS_LISTENER_DESCRIPTOR_IS_EMPTY; + } + auto interfaceIndex = opToInterfaceMap_.find(code); + if (interfaceIndex == opToInterfaceMap_.end() || !interfaceIndex->second) { + HILOGE("Cannot response request %d: unknown tranction", code); + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + return (this->*(interfaceIndex->second))(data, reply); +} + +int32_t FileDfsListenerStub::HandleOnStatus(MessageParcel &data, MessageParcel &reply) +{ + std::string networkId; + if (!data.ReadString(networkId)) { + HILOGE("read networkId failed"); + return E_INVAL_ARG; + } + int32_t status; + if (!data.ReadInt32(status)) { + HILOGE("read status failed"); + return E_INVAL_ARG; + } + OnStatus(networkId, status); + return NO_ERROR; +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/dfs_listener/file_dfs_listener_stub.h b/interfaces/kits/js/src/mod_fs/properties/dfs_listener/file_dfs_listener_stub.h new file mode 100644 index 000000000..898db131e --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/dfs_listener/file_dfs_listener_stub.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2024 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 FILEMANAGEMENT_FILE_API_FILE_DFS_LISTENER_STUB_H +#define FILEMANAGEMENT_FILE_API_FILE_DFS_LISTENER_STUB_H + +#include "i_file_dfs_listener.h" +#include "iremote_stub.h" +#include "message_option.h" +#include "message_parcel.h" +#include "refbase.h" + + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +class FileDfsListenerStub : public IRemoteStub { +public: + FileDfsListenerStub(); + virtual ~FileDfsListenerStub() = default; + int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + +private: + using FileDfsListenerInterface = int32_t (FileDfsListenerStub::*)(MessageParcel &data, MessageParcel &reply); + std::map opToInterfaceMap_; + + int32_t HandleOnStatus(MessageParcel &data, MessageParcel &reply); +}; + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // FILEMANAGEMENT_FILE_API_FILE_DFS_LISTENER_STUB_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/disconnectdfs.cpp b/interfaces/kits/js/src/mod_fs/properties/disconnectdfs.cpp new file mode 100644 index 000000000..a423c7534 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/disconnectdfs.cpp @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2023-2024 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.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include "common_func.h" +#include "filemgmt_libhilog.h" +#include "distributed_file_daemon_manager.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace fs = std::filesystem; + +napi_value DisconnectDfs::Async(napi_env env, napi_callback_info info) +{ + HILOGI("zhangyao::DisconnectDfs::Async called"); + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + auto [succNetworkId, networkId] = ParseJsOperand(env, { env, funcArg[NARG_POS::FIRST] }); + if (!succNetworkId) { + HILOGE("Failed to get networkId arguments"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + auto cbExec = [networkId = move(networkId)]() -> NError { + std::unique_ptr disconnectdfs_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!disconnectdfs_req) { + HILOGE("Failed to request heap memory."); + return NError(E_PARAMS); + } + auto ret = Storage::DistributedFile::DistributedFileDaemonManager::GetInstance(). + CloseP2PConnectionEx(networkId); + if (ret != 0) { + HILOGE("Failed to CloseP2PConnection, ret = %{public}d", ret); + return NError(ret); + } + return NError(ERRNO_NOERR); + }; + + auto cbComplete = [](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return { NVal::CreateUndefined(env) }; + }; + + NVal thisVar(env, funcArg.GetThisVar()); + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_DISCONNECTDFS_NAME, cbExec, cbComplete).val_; +} + +tuple DisconnectDfs::ParseJsOperand(napi_env env, NVal paramFromJsArg) +{ + auto [succ, param, ignore] = paramFromJsArg.ToUTF8String(); + if (!succ) { + HILOGE("parse parameter failed."); + return { false, "" }; + } + std::string paramStr = std::string(param.get()); + return { true, paramStr }; +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/disconnectdfs.h b/interfaces/kits/js/src/mod_fs/properties/disconnectdfs.h new file mode 100644 index 000000000..0c4c43404 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/disconnectdfs.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2024 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 FILEMANAGEMENT_FILE_API_DISCONNECTDFS_H +#define FILEMANAGEMENT_FILE_API_DISCONNECTDFS_H + +#include +#include +#include +#include +#include "bundle_mgr_client_impl.h" +#include "common_func.h" +#include "filemgmt_libn.h" +#include "n_async/n_ref.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +using namespace OHOS::FileManagement::LibN; +using namespace OHOS::AppExecFwk; +const std::string PROCEDURE_DISCONNECTDFS_NAME = "FileFSDisconnectDfs"; + +class DisconnectDfs final { +public: + static napi_value Async(napi_env env, napi_callback_info info); + +private: + // operator of napi + static tuple ParseJsOperand(napi_env env, NVal paramFromJsArg); +}; + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // FILEMANAGEMENT_FILE_API_DISCONNECTDFS_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index 50766ad16..e50b21b05 100644 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -58,6 +58,8 @@ #include "rust_file.h" #include "symlink.h" #include "watcher.h" +#include "connectdfs.h" +#include "disconnectdfs.h" #endif namespace OHOS { @@ -712,6 +714,8 @@ bool PropNExporter::ExportAsync() NVal::DeclareNapiFunction("readText", ReadText::Async), NVal::DeclareNapiFunction("symlink", Symlink::Async), NVal::DeclareNapiFunction("createWatcher", Watcher::CreateWatcher), + NVal::DeclareNapiFunction("connectDfs", ConnectDfs::Async), + NVal::DeclareNapiFunction("disconnectDfs", DisconnectDfs::Async), #endif }); } diff --git a/utils/filemgmt_libn/include/n_error.h b/utils/filemgmt_libn/include/n_error.h index 18d4a7384..389db164b 100644 --- a/utils/filemgmt_libn/include/n_error.h +++ b/utils/filemgmt_libn/include/n_error.h @@ -37,6 +37,8 @@ constexpr int EDQUOT = 122; #endif constexpr int UNKROWN_ERR = -1; constexpr int ERRNO_NOERR = 0; +constexpr int ECONNECTION = -4; +constexpr int EABORT = -5; constexpr int STORAGE_SERVICE_SYS_CAP_TAG = 13600000; constexpr int FILEIO_SYS_CAP_TAG = 13900000; constexpr int USER_FILE_MANAGER_SYS_CAP_TAG = 14000000; @@ -89,7 +91,9 @@ enum ErrCodeSuffixOfFileIO { E_DQUOT, E_UKERR, E_NOLCK, - E_NETUNREACH + E_NETUNREACH, + E_CONNECTION, + E_ABORT, }; enum ErrCodeSuffixOfUserFileManager { @@ -197,6 +201,8 @@ static inline std::unordered_map uvCode2ErrCodeTable { { "ERESTART", ERESTART }, { "EDQUOT", EDQUOT }, { "ENETUNREACH", ENETUNREACH }, + { "ECONNECTION", ECONNECTION }, + { "EABORT", EABORT }, }; static inline std::unordered_map> errCodeTable { @@ -245,6 +251,8 @@ static inline std::unordered_map> errCodeTa { UNKROWN_ERR, { FILEIO_SYS_CAP_TAG + E_UKERR, "Unknown error" } }, { ENOLCK, { FILEIO_SYS_CAP_TAG + E_NOLCK, "No record locks available" } }, { ENETUNREACH, { FILEIO_SYS_CAP_TAG + E_NETUNREACH, "Network is unreachable" } }, + { ECONNECTION, { FILEIO_SYS_CAP_TAG + E_CONNECTION, "Connection failed" } }, + { EABORT, { FILEIO_SYS_CAP_TAG + E_ABORT, "Software caused connection abort" } }, { FILEIO_SYS_CAP_TAG + E_PERM, { FILEIO_SYS_CAP_TAG + E_PERM, "Operation not permitted" } }, { FILEIO_SYS_CAP_TAG + E_NOENT, { FILEIO_SYS_CAP_TAG + E_NOENT, "No such file or directory" } }, { FILEIO_SYS_CAP_TAG + E_SRCH, { FILEIO_SYS_CAP_TAG + E_SRCH, "No such process" } }, @@ -290,6 +298,8 @@ static inline std::unordered_map> errCodeTa { FILEIO_SYS_CAP_TAG + E_UKERR, { FILEIO_SYS_CAP_TAG + E_UKERR, "Unknown error" } }, { FILEIO_SYS_CAP_TAG + E_NOLCK, { FILEIO_SYS_CAP_TAG + E_NOLCK, "No record locks available" } }, { FILEIO_SYS_CAP_TAG + E_NETUNREACH, { FILEIO_SYS_CAP_TAG + E_NETUNREACH, "Network is unreachable" } }, + { FILEIO_SYS_CAP_TAG + E_CONNECTION, { FILEIO_SYS_CAP_TAG + E_CONNECTION, "Connection failed" } }, + { FILEIO_SYS_CAP_TAG + E_ABORT, { FILEIO_SYS_CAP_TAG + E_ABORT, "Software caused connection abort" } }, { USER_FILE_MANAGER_SYS_CAP_TAG + E_DISPLAYNAME, { USER_FILE_MANAGER_SYS_CAP_TAG + E_DISPLAYNAME, "Invalid display name" } }, { USER_FILE_MANAGER_SYS_CAP_TAG + E_URIM, { USER_FILE_MANAGER_SYS_CAP_TAG + E_URIM, "Invalid uri" } }, -- Gitee