1 /*
2  * Copyright (c) 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 
16 #include "common/napi_helper.cpp"
17 #include "napi/native_api.h"
18 #include <sys/statvfs.h>
19 #define ONE 1
20 #define TWO 2
21 #define PATH "/data/storage/el2/base/files"
22 #define PARAM_0 0
23 #define PARAM_1 1
24 
Statvfs(napi_env env, napi_callback_info info)25 static napi_value Statvfs(napi_env env, napi_callback_info info)
26 {
27     size_t argc = PARAM_1;
28     napi_value args[1] = {nullptr};
29     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
30 
31     int value;
32     napi_get_value_int32(env, args[0], &value);
33     int intValue = PARAM_0;
34     struct statvfs sb;
35     if (value == ONE) {
36         intValue = statvfs(PATH, &sb);
37     } else if (value == TWO) {
38         intValue = statvfs(nullptr, &sb);
39     }
40     napi_value result = nullptr;
41     napi_create_int32(env, intValue, &result);
42     return result;
43 }
Statvfs64(napi_env env, napi_callback_info info)44 static napi_value Statvfs64(napi_env env, napi_callback_info info)
45 {
46     size_t argc = PARAM_1;
47     napi_value args[1] = {nullptr};
48     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
49 
50     int value;
51     napi_get_value_int32(env, args[0], &value);
52     int intValue = PARAM_0;
53     struct statvfs64 sb;
54     if (value == ONE) {
55         intValue = statvfs(PATH, &sb);
56     } else if (value == TWO) {
57         intValue = statvfs(nullptr, &sb);
58     }
59     napi_value result = nullptr;
60     napi_create_int32(env, intValue, &result);
61     return result;
62 }
63 EXTERN_C_START
Init(napi_env env, napi_value exports)64 static napi_value Init(napi_env env, napi_value exports)
65 {
66     napi_property_descriptor desc[] = {
67         {"statvfs", nullptr, Statvfs, nullptr, nullptr, nullptr, napi_default, nullptr},
68         {"statvfs64", nullptr, Statvfs64, nullptr, nullptr, nullptr, napi_default, nullptr},
69 
70     };
71     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
72     return exports;
73 }
74 EXTERN_C_END
75 
76 static napi_module demoModule = {
77     .nm_version = 1,
78     .nm_flags = 0,
79     .nm_filename = nullptr,
80     .nm_register_func = Init,
81     .nm_modname = "statvfs",
82     .nm_priv = ((void *)0),
83     .reserved = {0},
84 };
85 
RegisterModule(void)86 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
87