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 "lstat.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 Lstat::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, pathPtr, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath();
44     if (!resGetFirstArg) {
45         UniError(EINVAL).ThrowErr(env, "The first argument requires type string");
46         return nullptr;
47     }
48 
49     struct stat buf;
50     int ret = lstat(pathPtr.get(), &buf);
51     if (ret == -1) {
52         UniError(errno).ThrowErr(env);
53         return nullptr;
54     }
55 
56     napi_value objStat = NClass::InstantiateClass(env, StatNExporter::className_, {});
57     if (!objStat) {
58         return nullptr;
59     }
60 
61     auto statEntity = NClass::GetEntityOf<StatEntity>(env, objStat);
62     if (!statEntity) {
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 Lstat::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, tmp, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath();
83     if (!resGetFirstArg) {
84         UniError(EINVAL).ThrowErr(env, "Invalid path");
85         return nullptr;
86     }
87 
88     string path = tmp.get();
89     auto arg = make_shared<AsyncStatArg>();
90     auto cbExec = [arg, path](napi_env env) -> UniError {
91         if (lstat(path.c_str(), &arg->stat_)) {
92             return UniError(errno);
93         } else {
94             return UniError(ERRNO_NOERR);
95         }
96     };
97 
98     auto cbCompl = [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 = "FileIOLstat";
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