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 bool succ = false;
44
45 int fd;
46 tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
47 if (!succ) {
48 UniError(EINVAL).ThrowErr(env, "Invalid fd");
49 return nullptr;
50 }
51
52 struct stat buf;
53 if (fstat(fd, &buf) == -1) {
54 UniError(errno).ThrowErr(env);
55 return nullptr;
56 }
57
58 napi_value objStat = NClass::InstantiateClass(env, StatNExporter::className_, {});
59 if (!objStat) {
60 UniError(EINVAL).ThrowErr(env, "Cannot instantiate class");
61 return nullptr;
62 }
63
64 auto statEntity = NClass::GetEntityOf<StatEntity>(env, objStat);
65 if (!statEntity) {
66 UniError(EINVAL).ThrowErr(env, "Cannot get the entity of objStat");
67 return nullptr;
68 }
69 statEntity->stat_ = buf;
70
71 return objStat;
72 }
73
74 struct AsyncStatArg {
75 struct stat stat_;
76 };
77
Async(napi_env env,napi_callback_info info)78 napi_value Fstat::Async(napi_env env, napi_callback_info info)
79 {
80 NFuncArg funcArg(env, info);
81 if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
82 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
83 return nullptr;
84 }
85
86 bool succ = false;
87 int fd;
88 tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32();
89 if (!succ) {
90 UniError(EINVAL).ThrowErr(env, "Invalid fd");
91 return nullptr;
92 }
93
94 auto arg = make_shared<AsyncStatArg>();
95 auto cbExec = [fd, arg](napi_env env) -> UniError {
96 if (fstat(fd, &arg->stat_)) {
97 return UniError(errno);
98 } else {
99 return UniError(ERRNO_NOERR);
100 }
101 };
102 auto cbCompl = [arg](napi_env env, UniError err) -> NVal {
103 if (err) {
104 return { env, err.GetNapiErr(env) };
105 }
106 napi_value objStat = NClass::InstantiateClass(env, StatNExporter::className_, {});
107 if (!objStat) {
108 return { env, UniError(EIO).GetNapiErr(env) };
109 }
110 auto statEntity = NClass::GetEntityOf<StatEntity>(env, objStat);
111 if (!statEntity) {
112 return { env, UniError(EIO).GetNapiErr(env) };
113 }
114 statEntity->stat_ = arg->stat_;
115 return { env, objStat };
116 };
117 NVal thisVar(env, funcArg.GetThisVar());
118 string procedureName = "fileIOFstat";
119 if (funcArg.GetArgc() == NARG_CNT::ONE) {
120 return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_;
121 } else {
122 NVal cb(env, funcArg[NARG_POS::SECOND]);
123 return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_;
124 }
125 }
126 } // namespace ModuleFileIO
127 } // namespace DistributedFS
128 } // namespace OHOS