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 "stat.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
ParseJsFile(napi_env env,napi_value pathOrFdFromJsArg)29 static tuple<bool, FileInfo> ParseJsFile(napi_env env, napi_value pathOrFdFromJsArg)
30 {
31 auto [isPath, path, ignore] = NVal(env, pathOrFdFromJsArg).ToUTF8String();
32 if (isPath) {
33 return { true, FileInfo { true, move(path), {} } };
34 }
35 auto [isFd, fd] = NVal(env, pathOrFdFromJsArg).ToInt32();
36 if (isFd) {
37 if (fd < 0) {
38 HILOGE("Invalid fd");
39 NError(EINVAL).ThrowErr(env);
40 return { false, FileInfo { false, {}, {} } };
41 }
42 auto fdg = make_unique<DistributedFS::FDGuard>(fd, false);
43 if (!fdg) {
44 HILOGE("Failed to request heap memory.");
45 NError(ENOMEM).ThrowErr(env);
46 return { false, FileInfo { false, {}, {} } };
47 }
48 return { true, FileInfo { false, {}, move(fdg) } };
49 }
50 HILOGE("Invalid parameter");
51 NError(EINVAL).ThrowErr(env);
52 return { false, FileInfo { false, {}, {} } };
53 };
54
Sync(napi_env env,napi_callback_info info)55 napi_value Stat::Sync(napi_env env, napi_callback_info info)
56 {
57 NFuncArg funcArg(env, info);
58 if (!funcArg.InitArgs(NARG_CNT::ONE)) {
59 HILOGE("Number of arguments unmatched");
60 NError(EINVAL).ThrowErr(env);
61 return nullptr;
62 }
63 auto [succ, fileInfo] = ParseJsFile(env, funcArg[NARG_POS::FIRST]);
64 if (!succ) {
65 return nullptr;
66 }
67
68 struct stat buf;
69 if (fileInfo.isPath) {
70 if (stat(fileInfo.path.get(), &buf) < 0) {
71 HILOGE("Failed to stat file with path");
72 NError(errno).ThrowErr(env);
73 return nullptr;
74 }
75 } else {
76 if (fstat(fileInfo.fdg->GetFD(), &buf) < 0) {
77 HILOGE("Failed to stat file with fd");
78 NError(errno).ThrowErr(env);
79 return nullptr;
80 }
81 }
82
83 auto stat = CommonFunc::InstantiateStat(env, buf).val_;
84 return stat;
85 }
86
87 struct AsyncStatArg {
88 struct stat stat_;
89 };
90
Async(napi_env env,napi_callback_info info)91 napi_value Stat::Async(napi_env env, napi_callback_info info)
92 {
93 NFuncArg funcArg(env, info);
94 if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
95 HILOGE("Number of arguments unmatched");
96 NError(EINVAL).ThrowErr(env);
97 return nullptr;
98 }
99 auto [succ, fileInfo] = ParseJsFile(env, funcArg[NARG_POS::FIRST]);
100 if (!succ) {
101 return nullptr;
102 }
103
104 auto arg = make_shared<AsyncStatArg>();
105 auto cbExec = [arg, fileInfo = make_shared<FileInfo>(move(fileInfo))]() -> NError {
106 if (fileInfo->isPath) {
107 if (stat(fileInfo->path.get(), &arg->stat_) < 0) {
108 HILOGE("Failed to stat file with path");
109 return NError(errno);
110 }
111 } else {
112 if (fstat(fileInfo->fdg->GetFD(), &arg->stat_) < 0) {
113 HILOGE("Failed to stat file with fd");
114 return NError(errno);
115 }
116 }
117 return NError(ERRNO_NOERR);
118 };
119 auto cbCompl = [arg](napi_env env, NError err) -> NVal {
120 if (err) {
121 return { env, err.GetNapiErr(env) };
122 }
123 auto stat = CommonFunc::InstantiateStat(env, arg->stat_);
124 return stat;
125 };
126 NVal thisVar(env, funcArg.GetThisVar());
127 if (funcArg.GetArgc() == NARG_CNT::ONE) {
128 return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_STAT_NAME, cbExec, cbCompl).val_;
129 } else {
130 NVal cb(env, funcArg[NARG_POS::SECOND]);
131 return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_STAT_NAME, cbExec, cbCompl).val_;
132 }
133 }
134 } // namespace OHOS::FileManagement::ModuleFileIO