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 "lstat.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 Lstat::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, ignore] = 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 = lstat(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 Lstat::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, unused] = 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, path](napi_env env) -> UniError {
90 if (lstat(path.c_str(), &arg->stat_)) {
91 return UniError(errno);
92 } else {
93 return UniError(ERRNO_NOERR);
94 }
95 };
96
97 auto cbCompl = [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 = "FileIOLstat";
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