1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "disconnectdfs.h"
17
18 #include <cstring>
19 #include <dirent.h>
20 #include <fcntl.h>
21 #include <memory>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <tuple>
25 #include <unistd.h>
26
27 #include "common_func.h"
28 #include "distributed_file_daemon_manager.h"
29 #include "filemgmt_libhilog.h"
30
31 namespace OHOS {
32 namespace FileManagement {
33 namespace ModuleFileIO {
34 namespace fs = std::filesystem;
35
Async(napi_env env,napi_callback_info info)36 napi_value DisconnectDfs::Async(napi_env env, napi_callback_info info)
37 {
38 HILOGI("DisconnectDfs::Async called");
39 NFuncArg funcArg(env, info);
40 if (!funcArg.InitArgs(NARG_CNT::ONE)) {
41 HILOGE("Number of arguments unmatched");
42 NError(E_PARAMS).ThrowErr(env);
43 return nullptr;
44 }
45 auto [succNetworkId, networkId] = ParseJsOperand(env, { env, funcArg[NARG_POS::FIRST] });
46 if (!succNetworkId) {
47 HILOGE("Failed to get networkId arguments");
48 NError(E_PARAMS).ThrowErr(env);
49 return nullptr;
50 }
51
52 auto cbExec = [networkId = move(networkId)]() -> NError {
53 auto ret = Storage::DistributedFile::DistributedFileDaemonManager::GetInstance().
54 CloseP2PConnectionEx(networkId);
55 if (ret != ERRNO_NOERR) {
56 HILOGE("Failed to CloseP2PConnection, ret = %{public}d", ret);
57 return NError(ret);
58 }
59 return NError(ERRNO_NOERR);
60 };
61
62 auto cbComplete = [](napi_env env, NError err) -> NVal {
63 if (err) {
64 return { env, err.GetNapiErr(env) };
65 }
66 return { NVal::CreateUndefined(env) };
67 };
68
69 NVal thisVar(env, funcArg.GetThisVar());
70 return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_DISCONNECTDFS_NAME, cbExec, cbComplete).val_;
71 }
72
ParseJsOperand(napi_env env,NVal paramFromJsArg)73 tuple<bool, std::string> DisconnectDfs::ParseJsOperand(napi_env env, NVal paramFromJsArg)
74 {
75 auto [succ, param, ignore] = paramFromJsArg.ToUTF8String();
76 if (!succ) {
77 HILOGE("parse parameter failed.");
78 return { false, "" };
79 }
80 std::string paramStr = std::string(param.get());
81 return { true, paramStr };
82 }
83
84 } // namespace ModuleFileIO
85 } // namespace FileManagement
86 } // namespace OHOS