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