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