1 /*
2 * Copyright (c) 2022 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 "common_func.h"
23 #include "filemgmt_libhilog.h"
24
25 namespace OHOS::FileManagement::ModuleFileIO {
26 using namespace std;
27 using namespace OHOS::FileManagement::LibN;
28
Sync(napi_env env,napi_callback_info info)29 napi_value Lstat::Sync(napi_env env, napi_callback_info info)
30 {
31 NFuncArg funcArg(env, info);
32 if (!funcArg.InitArgs(NARG_CNT::ONE)) {
33 HILOGE("Number of arguments unmatched");
34 NError(EINVAL).ThrowErr(env);
35 return nullptr;
36 }
37
38 auto [resGetFirstArg, pathPtr, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
39 if (!resGetFirstArg) {
40 HILOGE("Invalid path");
41 NError(EINVAL).ThrowErr(env);
42 return nullptr;
43 }
44
45 struct stat buf;
46 int ret = lstat(pathPtr.get(), &buf);
47 if (ret < 0) {
48 HILOGE("Failed to get stat of file by path %{public}s", pathPtr.get());
49 NError(errno).ThrowErr(env);
50 return nullptr;
51 }
52
53 auto stat = CommonFunc::InstantiateStat(env, buf).val_;
54 return stat;
55 }
56
57 struct AsyncStatArg {
58 struct stat stat_;
59 };
60
Async(napi_env env,napi_callback_info info)61 napi_value Lstat::Async(napi_env env, napi_callback_info info)
62 {
63 NFuncArg funcArg(env, info);
64 if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
65 HILOGE("Number of arguments unmatched");
66 NError(EINVAL).ThrowErr(env);
67 return nullptr;
68 }
69
70 auto [resGetFirstArg, tmp, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
71 if (!resGetFirstArg) {
72 HILOGE("Invalid path");
73 NError(EINVAL).ThrowErr(env);
74 return nullptr;
75 }
76
77 string path = tmp.get();
78 auto arg = make_shared<AsyncStatArg>();
79 auto cbExec = [arg, path]() -> NError {
80 int ret = lstat(path.c_str(), &arg->stat_);
81 if (ret < 0) {
82 HILOGE("Failed to get stat of file by path: %{public}s, ret: %{public}d", path.c_str(), ret);
83 return NError(errno);
84 } else {
85 return NError(ERRNO_NOERR);
86 }
87 };
88
89 auto cbCompl = [arg](napi_env env, NError err) -> NVal {
90 if (err) {
91 return { env, err.GetNapiErr(env) };
92 }
93 auto stat = CommonFunc::InstantiateStat(env, arg->stat_);
94 return stat;
95 };
96
97 NVal thisVar(env, funcArg.GetThisVar());
98 if (funcArg.GetArgc() == NARG_CNT::ONE) {
99 return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_LSTAT_NAME, cbExec, cbCompl).val_;
100 } else {
101 NVal cb(env, funcArg[NARG_POS::SECOND]);
102 return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_LSTAT_NAME, cbExec, cbCompl).val_;
103 }
104 }
105 } // namespace OHOS::FileManagement::ModuleFileIO