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 "stat.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 Stat::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, unused] = 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 = stat(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 Stat::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, unuse] = 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 = arg, path = path](napi_env env) -> UniError {
91         if (stat(path.c_str(), &arg->stat_)) {
92             return UniError(errno);
93         } else {
94             return UniError(ERRNO_NOERR);
95         }
96     };
97 
98     auto cbCompl = [arg = arg](napi_env env, UniError err) -> NVal {
99         if (err) {
100             return { env, err.GetNapiErr(env) };
101         }
102 
103         napi_value objStat = NClass::InstantiateClass(env, StatNExporter::className_, {});
104         if (!objStat) {
105             return { env, UniError(EIO).GetNapiErr(env) };
106         }
107 
108         auto statEntity = NClass::GetEntityOf<StatEntity>(env, objStat);
109         if (!statEntity) {
110             return { env, UniError(EIO).GetNapiErr(env) };
111         }
112 
113         statEntity->stat_ = arg->stat_;
114         return { env, objStat };
115     };
116 
117     NVal thisVar(env, funcArg.GetThisVar());
118     const string procedureName = "fileioStatStat";
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