• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     bool succ = false;
44     unique_ptr<char[]> pathPtr;
45     tie(succ, pathPtr, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
46     if (!succ) {
47         UniError(EINVAL).ThrowErr(env, "The first argument requires type string");
48         return nullptr;
49     }
50 
51     struct stat buf;
52     int ret = lstat(pathPtr.get(), &buf);
53     if (ret == -1) {
54         UniError(errno).ThrowErr(env);
55         return nullptr;
56     }
57 
58     napi_value objStat = NClass::InstantiateClass(env, StatNExporter::className_, {});
59     if (!objStat) {
60         return nullptr;
61     }
62 
63     auto statEntity = NClass::GetEntityOf<StatEntity>(env, objStat);
64     if (!statEntity) {
65         return nullptr;
66     }
67 
68     statEntity->stat_ = buf;
69     return objStat;
70 }
71 
72 struct AsyncStatArg {
73     struct stat stat_;
74 };
75 
Async(napi_env env,napi_callback_info info)76 napi_value Lstat::Async(napi_env env, napi_callback_info info)
77 {
78     NFuncArg funcArg(env, info);
79     if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
80         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
81         return nullptr;
82     }
83     string path;
84     unique_ptr<char[]> tmp;
85     bool succ = false;
86     tie(succ, tmp, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
87     if (!succ) {
88         UniError(EINVAL).ThrowErr(env, "Invalid path");
89         return nullptr;
90     }
91     path = tmp.get();
92 
93     auto arg = make_shared<AsyncStatArg>();
94     auto cbExec = [arg, path](napi_env env) -> UniError {
95         if (lstat(path.c_str(), &arg->stat_)) {
96             return UniError(errno);
97         } else {
98             return UniError(ERRNO_NOERR);
99         }
100     };
101     auto cbCompl = [arg](napi_env env, UniError err) -> NVal {
102         if (err) {
103             return { env, err.GetNapiErr(env) };
104         }
105         napi_value objStat = NClass::InstantiateClass(env, StatNExporter::className_, {});
106         if (!objStat) {
107             return { env, UniError(EIO).GetNapiErr(env) };
108         }
109         auto statEntity = NClass::GetEntityOf<StatEntity>(env, objStat);
110         if (!statEntity) {
111             return { env, UniError(EIO).GetNapiErr(env) };
112         }
113         statEntity->stat_ = arg->stat_;
114         return { env, objStat };
115     };
116     NVal thisVar(env, funcArg.GetThisVar());
117     string procedureName = "fileIOLstat";
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