• 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 
16 #include "stat.h"
17 
18 #include "../../common/napi/n_class.h"
19 #include "../../common/napi/n_func_arg.h"
20 #include "../../common/uni_error.h"
21 
22 #include "../class_stat/stat_entity.h"
23 #include "../class_stat/stat_n_exporter.h"
24 
25 namespace OHOS {
26 namespace DistributedFS {
27 namespace ModuleFileIO {
28 using namespace std;
29 
Sync(napi_env env,napi_callback_info info)30 napi_value Stat::Sync(napi_env env, napi_callback_info info)
31 {
32     NFuncArg funcArg(env, info);
33     if (!funcArg.InitArgs(NARG_CNT::ONE)) {
34         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
35         return nullptr;
36     }
37 
38     bool succ = false;
39     unique_ptr<char[]> pathPtr;
40     tie(succ, pathPtr, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
41     if (!succ) {
42         UniError(EINVAL).ThrowErr(env, "The first argument requires type string");
43         return nullptr;
44     }
45 
46     struct stat buf;
47     int ret = stat(pathPtr.get(), &buf);
48     if (ret == -1) {
49         UniError(errno).ThrowErr(env);
50         return nullptr;
51     }
52 
53     napi_value objStat = NClass::InstantiateClass(env, StatNExporter::className_, {});
54     if (!objStat) {
55         return nullptr;
56     }
57 
58     auto statEntity = NClass::GetEntityOf<StatEntity>(env, objStat);
59     if (!statEntity) {
60         return nullptr;
61     }
62 
63     statEntity->stat_ = buf;
64     return objStat;
65 }
66 } // namespace ModuleFileIO
67 } // namespace DistributedFS
68 } // namespace OHOS