1 /*
2  * Copyright (c) 2022-2023 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 #include <unistd.h>
20 
21 #include "class_stat/stat_entity.h"
22 #include "class_stat/stat_n_exporter.h"
23 #include "common_func.h"
24 #include "file_utils.h"
25 #include "filemgmt_libhilog.h"
26 
27 namespace OHOS::FileManagement::ModuleFileIO {
28 using namespace std;
29 using namespace OHOS::FileManagement::LibN;
30 
ParseJsFile(napi_env env,napi_value pathOrFdFromJsArg)31 static tuple<bool, FileInfo> ParseJsFile(napi_env env, napi_value pathOrFdFromJsArg)
32 {
33     auto [isPath, path, ignore] = NVal(env, pathOrFdFromJsArg).ToUTF8StringPath();
34     if (isPath) {
35         return { true, FileInfo { true, move(path), {} } };
36     }
37     auto [isFd, fd] = NVal(env, pathOrFdFromJsArg).ToInt32();
38     if (isFd) {
39         if (fd < 0) {
40             HILOGE("Invalid fd");
41             NError(EINVAL).ThrowErr(env);
42             return { false, FileInfo { false, {}, {} } };
43         }
44         auto fdg = CreateUniquePtr<DistributedFS::FDGuard>(fd, false);
45         if (fdg == nullptr) {
46             HILOGE("Failed to request heap memory.");
47             NError(ENOMEM).ThrowErr(env);
48             return { false, FileInfo { false, {}, {} } };
49         }
50         return { true, FileInfo { false, {}, move(fdg) } };
51     }
52     HILOGE("Invalid parameter");
53     NError(EINVAL).ThrowErr(env);
54     return { false, FileInfo { false, {}, {} } };
55 };
56 
CheckFsStat(const FileInfo & fileInfo,uv_fs_t * req)57 static NError CheckFsStat(const FileInfo &fileInfo, uv_fs_t* req)
58 {
59     if (fileInfo.isPath) {
60         int ret = uv_fs_stat(nullptr, req, fileInfo.path.get(), nullptr);
61         if (ret < 0) {
62             HILOGD("Failed to stat file with path, ret is %{public}d", ret);
63             return NError(ret);
64         }
65     } else {
66         int ret = uv_fs_fstat(nullptr, req, fileInfo.fdg->GetFD(), nullptr);
67         if (ret < 0) {
68             HILOGE("Failed to stat file with fd, ret is %{public}d", ret);
69             return NError(ret);
70         }
71     }
72     return NError(ERRNO_NOERR);
73 }
74 
Sync(napi_env env,napi_callback_info info)75 napi_value Stat::Sync(napi_env env, napi_callback_info info)
76 {
77     NFuncArg funcArg(env, info);
78     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
79         HILOGE("Number of arguments unmatched");
80         NError(EINVAL).ThrowErr(env);
81         return nullptr;
82     }
83     auto [succ, fileInfo] = ParseJsFile(env, funcArg[NARG_POS::FIRST]);
84     if (!succ) {
85         return nullptr;
86     }
87 
88     std::unique_ptr<uv_fs_t, decltype(CommonFunc::fs_req_cleanup)*> stat_req = {
89         new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup };
90     if (!stat_req) {
91         HILOGE("Failed to request heap memory.");
92         NError(ENOMEM).ThrowErr(env);
93         return nullptr;
94     }
95     auto err = CheckFsStat(fileInfo, stat_req.get());
96     if (err) {
97         err.ThrowErr(env);
98         return nullptr;
99     }
100 #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM)
101     auto arg = CreateSharedPtr<FileInfo>(move(fileInfo));
102     if (arg == nullptr) {
103         HILOGE("Failed to request heap memory.");
104         NError(ENOMEM).ThrowErr(env);
105         return nullptr;
106     }
107     auto stat = CommonFunc::InstantiateStat(env, stat_req->statbuf, arg).val_;
108 #else
109     auto stat = CommonFunc::InstantiateStat(env, stat_req->statbuf).val_;
110 #endif
111     return stat;
112 }
113 
Async(napi_env env,napi_callback_info info)114 napi_value Stat::Async(napi_env env, napi_callback_info info)
115 {
116     NFuncArg funcArg(env, info);
117     if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
118         HILOGE("Number of arguments unmatched");
119         NError(EINVAL).ThrowErr(env);
120         return nullptr;
121     }
122     auto [succ, fileInfo] = ParseJsFile(env, funcArg[NARG_POS::FIRST]);
123     if (!succ) {
124         return nullptr;
125     }
126     auto arg = CreateSharedPtr<StatEntity>();
127     if (arg == nullptr) {
128         HILOGE("Failed to request heap memory.");
129         NError(ENOMEM).ThrowErr(env);
130         return nullptr;
131     }
132     auto cbExec = [arg, fileInfo = make_shared<FileInfo>(move(fileInfo))]() -> NError {
133         std::unique_ptr<uv_fs_t, decltype(CommonFunc::fs_req_cleanup)*> stat_req = {
134             new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup };
135         if (!stat_req) {
136             HILOGE("Failed to request heap memory.");
137             return NError(ENOMEM);
138         }
139         auto err = CheckFsStat(*fileInfo, stat_req.get());
140         if (err) {
141             return err;
142         }
143         arg->stat_ = stat_req->statbuf;
144 #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM)
145         arg->fileInfo_ = fileInfo;
146 #endif
147         return NError(ERRNO_NOERR);
148     };
149     auto cbCompl = [arg](napi_env env, NError err) -> NVal {
150         if (err) {
151             return { env, err.GetNapiErr(env) };
152         }
153 #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM)
154         return CommonFunc::InstantiateStat(env, arg->stat_, arg->fileInfo_, true);
155 #else
156         return CommonFunc::InstantiateStat(env, arg->stat_, true);
157 #endif
158     };
159 
160     NVal thisVar(env, funcArg.GetThisVar());
161     NVal callbackVal(env, funcArg[SECOND]);
162     return (funcArg.GetArgc() == NARG_CNT::ONE) ?
163         NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_STAT_NAME, cbExec, cbCompl).val_ :
164         NAsyncWorkCallback(env, thisVar, callbackVal).Schedule(PROCEDURE_STAT_NAME, cbExec, cbCompl).val_;
165 }
166 } // namespace OHOS::FileManagement::ModuleFileIO