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 <fcntl.h>
19 #include <sys/statfs.h>
20 #include <sys/vfs.h>
21 #include <unistd.h>
22
23 #define ONE 1
24 #define TWO 2
25 #define PATH "/data/storage/el2/base/files"
26 #define PARAM_0 0
27 #define PARAM_1 1
28 #define PARAM_0777 0777
29
Statfs(napi_env env,napi_callback_info info)30 static napi_value Statfs(napi_env env, napi_callback_info info)
31 {
32 size_t argc = PARAM_1;
33 napi_value args[1] = {nullptr};
34 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
35
36 int value;
37 napi_get_value_int32(env, args[0], &value);
38 int int_value = PARAM_0;
39 struct statfs sb;
40
41 if (value == ONE) {
42 int_value = statfs(PATH, &sb);
43 } else if (value == TWO) {
44 int_value = statfs(nullptr, &sb);
45 }
46 napi_value result = nullptr;
47 napi_create_int32(env, int_value, &result);
48 return result;
49 }
50
Statfs64(napi_env env,napi_callback_info info)51 static napi_value Statfs64(napi_env env, napi_callback_info info)
52 {
53 size_t argc = PARAM_1;
54 napi_value args[1] = {nullptr};
55 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
56
57 int value = PARAM_0;
58 napi_get_value_int32(env, args[0], &value);
59 int int_value = PARAM_0;
60 struct statfs64 sb = {PARAM_0};
61
62 if (value == ONE) {
63 int_value = statfs64(PATH, &sb);
64 } else if (value == TWO) {
65 int_value = statfs64(nullptr, &sb);
66 }
67 napi_value result = nullptr;
68 napi_create_int32(env, int_value, &result);
69 return result;
70 }
71
Fstatfs(napi_env env,napi_callback_info info)72 static napi_value Fstatfs(napi_env env, napi_callback_info info)
73 {
74 struct statfs st = {PARAM_0};
75 int fd = open("/data/storage/el2/base/files/Fzl.txt", O_CREAT, PARAM_0777);
76 lseek(fd, PARAM_0, SEEK_SET);
77 int ret = fstatfs(fd, &st);
78 close(fd);
79 napi_value result;
80 napi_create_int32(env, ret, &result);
81 return result;
82 }
83
Fstatfs64(napi_env env,napi_callback_info info)84 static napi_value Fstatfs64(napi_env env, napi_callback_info info)
85 {
86 struct statfs st = {PARAM_0};
87 int fd = open("/data/storage/el2/base/files/Fzl.txt", O_CREAT, PARAM_0777);
88 lseek(fd, PARAM_0, SEEK_SET);
89 int ret = fstatfs64(fd, &st);
90 close(fd);
91 napi_value result;
92 napi_create_int32(env, ret, &result);
93 return result;
94 }
95
Fstatvfs(napi_env env,napi_callback_info info)96 static napi_value Fstatvfs(napi_env env, napi_callback_info info)
97 {
98 struct statvfs sts = {PARAM_0};
99 int fd = open("/data/storage/el2/base/files/Fzl.txt", O_CREAT, PARAM_0777);
100 lseek(fd, PARAM_0, SEEK_SET);
101 int ret = fstatvfs(fd, &sts);
102 close(fd);
103 napi_value result = nullptr;
104 napi_create_int32(env, ret, &result);
105 return result;
106 }
107
Fstatvfs64(napi_env env,napi_callback_info info)108 static napi_value Fstatvfs64(napi_env env, napi_callback_info info)
109 {
110 struct statvfs sts = {PARAM_0};
111 int fd = open("/data/storage/el2/base/files/Fzl.txt", O_CREAT, PARAM_0777);
112 lseek(fd, PARAM_0, SEEK_SET);
113 int ret = fstatvfs64(fd, &sts);
114 close(fd);
115 napi_value result;
116 napi_create_int32(env, ret, &result);
117 return result;
118 }
119
120 EXTERN_C_START
Init(napi_env env,napi_value exports)121 static napi_value Init(napi_env env, napi_value exports)
122 {
123 napi_property_descriptor desc[] = {
124 {"statfs", nullptr, Statfs, nullptr, nullptr, nullptr, napi_default, nullptr},
125 {"statfs64", nullptr, Statfs64, nullptr, nullptr, nullptr, napi_default, nullptr},
126 {"fstatfs", nullptr, Fstatfs, nullptr, nullptr, nullptr, napi_default, nullptr},
127 {"fstatfs64", nullptr, Fstatfs64, nullptr, nullptr, nullptr, napi_default, nullptr},
128 {"fstatvfs", nullptr, Fstatvfs, nullptr, nullptr, nullptr, napi_default, nullptr},
129 {"fstatvfs64", nullptr, Fstatvfs64, nullptr, nullptr, nullptr, napi_default, nullptr},
130 };
131 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
132 return exports;
133 }
134 EXTERN_C_END
135
136 static napi_module demoModule = {
137 .nm_version = 1,
138 .nm_flags = 0,
139 .nm_filename = nullptr,
140 .nm_register_func = Init,
141 .nm_modname = "statfs",
142 .nm_priv = ((void *)0),
143 .reserved = {0},
144 };
145
RegisterModule(void)146 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
147