1 /*
2 * Copyright (c) 2021 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 #include "fstat.h"
16
17 #include <memory>
18 #include <tuple>
19
20 #include "../../common/napi/n_async/n_async_work_callback.h"
21 #include "../../common/napi/n_async/n_async_work_promise.h"
22 #include "../../common/napi/n_class.h"
23 #include "../../common/napi/n_func_arg.h"
24 #include "../../common/napi/n_val.h"
25 #include "../../common/uni_error.h"
26
27 #include "../class_stat/stat_entity.h"
28 #include "../class_stat/stat_n_exporter.h"
29
30 namespace OHOS {
31 namespace DistributedFS {
32 namespace ModuleFileIO {
33 using namespace std;
34
Sync(napi_env env,napi_callback_info info)35 napi_value Fstat::Sync(napi_env env, napi_callback_info info)
36 {
37 NFuncArg funcArg(env, info);
38 if (!funcArg.InitArgs(NARG_CNT::ONE)) {
39 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
40 return nullptr;
41 }
42
43 auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
44 if (!resGetFirstArg || fd < 0) {
45 UniError(EINVAL).ThrowErr(env, "Invalid fd");
46 return nullptr;
47 }
48
49 struct stat buf;
50 if (fstat(fd, &buf) == -1) {
51 UniError(errno).ThrowErr(env);
52 return nullptr;
53 }
54
55 napi_value objStat = NClass::InstantiateClass(env, StatNExporter::className_, {});
56 if (!objStat) {
57 UniError(EINVAL).ThrowErr(env, "Cannot instantiate class");
58 return nullptr;
59 }
60
61 auto statEntity = NClass::GetEntityOf<StatEntity>(env, objStat);
62 if (!statEntity) {
63 UniError(EINVAL).ThrowErr(env, "Cannot get the entity of objStat");
64 return nullptr;
65 }
66
67 statEntity->stat_ = buf;
68 return objStat;
69 }
70
71 struct AsyncStatArg {
72 struct stat stat_;
73 };
74
Async(napi_env env,napi_callback_info info)75 napi_value Fstat::Async(napi_env env, napi_callback_info info)
76 {
77 NFuncArg funcArg(env, info);
78 if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
79 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
80 return nullptr;
81 }
82
83 auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
84 if (!resGetFirstArg || fd < 0) {
85 UniError(EINVAL).ThrowErr(env, "Invalid fd");
86 return nullptr;
87 }
88
89 auto arg = make_shared<AsyncStatArg>();
90 auto cbExec = [fd = fd, arg = arg](napi_env env) -> UniError {
91 if (fstat(fd, &arg->stat_)) {
92 return UniError(errno);
93 } else {
94 return UniError(ERRNO_NOERR);
95 }
96 };
97
98 auto cbCompl = [arg = arg](napi_env env, UniError err) -> NVal {
99 if (err) {
100 return { env, err.GetNapiErr(env) };
101 }
102 napi_value objStat = NClass::InstantiateClass(env, StatNExporter::className_, {});
103 if (!objStat) {
104 return { env, UniError(EIO).GetNapiErr(env) };
105 }
106 auto statEntity = NClass::GetEntityOf<StatEntity>(env, objStat);
107 if (!statEntity) {
108 return { env, UniError(EIO).GetNapiErr(env) };
109 }
110 statEntity->stat_ = arg->stat_;
111 return { env, objStat };
112 };
113
114 NVal thisVar(env, funcArg.GetThisVar());
115 const string procedureName = "fileIOFstat";
116 if (funcArg.GetArgc() == NARG_CNT::ONE) {
117 return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_;
118 } else {
119 NVal cb(env, funcArg[NARG_POS::SECOND]);
120 return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_;
121 }
122 }
123 } // namespace ModuleFileIO
124 } // namespace DistributedFS
125 } // namespace OHOS