• 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 "stat.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 Stat::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, pathPtr, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath();
43     if (!resGetFirstArg) {
44         UniError(EINVAL).ThrowErr(env, "The first argument requires type string");
45         return nullptr;
46     }
47 
48     struct stat buf;
49     int ret = stat(pathPtr.get(), &buf);
50     if (ret == -1) {
51         UniError(errno).ThrowErr(env);
52         return nullptr;
53     }
54 
55     napi_value objStat = NClass::InstantiateClass(env, StatNExporter::className_, {});
56     if (!objStat) {
57         return nullptr;
58     }
59 
60     auto statEntity = NClass::GetEntityOf<StatEntity>(env, objStat);
61     if (!statEntity) {
62         return nullptr;
63     }
64 
65     statEntity->stat_ = buf;
66     return objStat;
67 }
68 
69 struct AsyncStatArg {
70     struct stat stat_;
71 };
72 
Async(napi_env env,napi_callback_info info)73 napi_value Stat::Async(napi_env env, napi_callback_info info)
74 {
75     NFuncArg funcArg(env, info);
76     if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
77         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
78         return nullptr;
79     }
80 
81     auto [resGetFirstArg, tmp, unuse] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath();
82     if (!resGetFirstArg) {
83         UniError(EINVAL).ThrowErr(env, "Invalid path");
84         return nullptr;
85     }
86 
87     string path = tmp.get();
88     auto arg = make_shared<AsyncStatArg>();
89     auto cbExec = [arg = arg, path = path](napi_env env) -> UniError {
90         if (stat(path.c_str(), &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 
102         napi_value objStat = NClass::InstantiateClass(env, StatNExporter::className_, {});
103         if (!objStat) {
104             return { env, UniError(EIO).GetNapiErr(env) };
105         }
106 
107         auto statEntity = NClass::GetEntityOf<StatEntity>(env, objStat);
108         if (!statEntity) {
109             return { env, UniError(EIO).GetNapiErr(env) };
110         }
111 
112         statEntity->stat_ = arg->stat_;
113         return { env, objStat };
114     };
115 
116     NVal thisVar(env, funcArg.GetThisVar());
117     const string procedureName = "fileioStatStat";
118     if (funcArg.GetArgc() == NARG_CNT::ONE) {
119         return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_;
120     } else {
121         NVal cb(env, funcArg[NARG_POS::SECOND]);
122         return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_;
123     }
124 }
125 } // namespace ModuleFileIO
126 } // namespace DistributedFS
127 } // namespace OHOS