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 "napi/native_api.h"
17 #include <cstdio>
18 #include <cstdlib>
19 #include <cstring>
20 #include <fcntl.h>
21 #include <js_native_api.h>
22 #include <malloc.h>
23 #include <node_api.h>
24 #include <sys/xattr.h>
25 #include <unistd.h>
26
27 #define ONEVAL 1
28 #define MINUSONE (-1)
29 #define SUCCESS 1
30 #define FAIL (-1)
31 #define NO_ERR 0
32 #define SIZE_THREE 3
33 #define PARAM_0 0
34 #define PARAM_1 1
35 #define PARAM_2 2
36 #define PARAM_3 3
37 #define PARAM_4 4
38 #define PARAM_0667 0667
39 #define PARAM_0777 0777
40 #define PARAM_64 64
41
42 #define PARAM_UNNORMAL (-1)
43 #define RETURN_0 0
44 #define FAILD (-1)
45 #define ERRON_0 0
46 #define SIZE_10 10
47 #define SIZE_100 100
48 #define SIZE_4096 4096
49 #define SIZE_8192 8192
50
Lgetxattr(napi_env env, napi_callback_info info)51 static napi_value Lgetxattr(napi_env env, napi_callback_info info)
52 {
53 mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
54 int fd = open("/data/storage/el2/base/files/test.txt", O_RDWR | O_CREAT, perms);
55 char str[] = "dat";
56 write(fd, str, sizeof(str));
57 close(fd);
58 system("chmod 777 /data/storage/el2/base/files/test.txt");
59 char buf[BUFSIZ] = {0};
60 int ret = lsetxattr("/data/storage/el2/base/files/test.txt", "/data/storage/el2/base/files/user.txt", "dat",
61 sizeof("dat"), XATTR_CREATE);
62 if (ret == NO_ERR) {
63 ret = lgetxattr("/data/storage/el2/base/files/test.txt", "/data/storage/el2/base/files/user.txt", buf,
64 sizeof(buf));
65 } else {
66 ret = FAIL;
67 }
68 remove("/data/storage/el2/base/files/test.txt");
69 napi_value result = nullptr;
70 if (ret != FAIL) {
71 napi_create_int32(env, FAIL, &result);
72 } else {
73 napi_create_int32(env, SUCCESS, &result);
74 }
75 return result;
76 }
77
Setxattr(napi_env env, napi_callback_info info)78 static napi_value Setxattr(napi_env env, napi_callback_info info)
79 {
80 mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
81 int fd = open("/data/storage/el2/base/files/test.txt", O_RDWR | O_CREAT, perms);
82
83 char str[] = "dat";
84 write(fd, str, sizeof(str));
85 close(fd);
86 int ret = setxattr("/data/storage/el2/base/files/test.txt", "/data/storage/el2/base/files/user.txt", "dat",
87 sizeof("dat"), XATTR_CREATE);
88 if (ret == NO_ERR) {
89 ret = NO_ERR;
90 } else {
91 ret = FAIL;
92 }
93 remove("/data/storage/el2/base/files/test.txt");
94 napi_value result = nullptr;
95 napi_create_int32(env, ret, &result);
96 return result;
97 }
98
Getxattr(napi_env env, napi_callback_info info)99 static napi_value Getxattr(napi_env env, napi_callback_info info)
100 {
101 mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
102 int fd = open("/data/storage/el2/base/files/test.txt", O_RDWR | O_CREAT, perms);
103
104 char str[] = "dat";
105 write(fd, str, sizeof(str));
106 close(fd);
107 char buf[BUFSIZ] = {0};
108 int ret = setxattr("/data/storage/el2/base/files/test.txt", "/data/storage/el2/base/files/user.txt", "dat",
109 sizeof("dat"), XATTR_CREATE);
110 if (ret == NO_ERR) {
111 ret = getxattr("/data/storage/el2/base/files/test.txt", "/data/storage/el2/base/files/user.txt", buf,
112 sizeof(buf));
113 } else {
114 ret = FAIL;
115 }
116 remove("/data/storage/el2/base/files/test.txt");
117 napi_value result = nullptr;
118 if (ret != FAIL) {
119 napi_create_int32(env, FAIL, &result);
120 } else {
121 napi_create_int32(env, SUCCESS, &result);
122 }
123 return result;
124 }
125
Listxattr(napi_env env, napi_callback_info info)126 static napi_value Listxattr(napi_env env, napi_callback_info info)
127 {
128 mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
129 int fd = open("/data/storage/el2/base/files/test.txt", O_RDWR | O_CREAT, perms);
130
131 char str[] = "dat";
132 write(fd, str, sizeof(str));
133 close(fd);
134
135 system("chmod 777 /data/storage/el2/base/files/test.txt");
136 char list[BUFSIZ] = {0};
137 int ret = setxattr("/data/storage/el2/base/files/test.txt", "/data/storage/el2/base/files/user.txt", "dat",
138 sizeof("dat"), XATTR_CREATE);
139 if (ret == NO_ERR) {
140 ret = listxattr("/data/storage/el2/base/files/test.txt", list, sizeof(list));
141 } else {
142 ret = FAIL;
143 }
144 remove("/data/storage/el2/base/files/test.txt");
145 napi_value result = nullptr;
146 if (ret != FAIL) {
147 napi_create_int32(env, FAIL, &result);
148 } else {
149 napi_create_int32(env, SUCCESS, &result);
150 }
151 return result;
152 }
153
LListxattr(napi_env env, napi_callback_info info)154 static napi_value LListxattr(napi_env env, napi_callback_info info)
155 {
156 mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
157 int fd = open("/data/storage/el2/base/files/test.txt", O_RDWR | O_CREAT, perms);
158
159 char str[] = "dat";
160 write(fd, str, sizeof(str));
161 close(fd);
162
163 system("chmod 777 /data/storage/el2/base/files/test.txt");
164 char list[BUFSIZ] = {0};
165 int ret = lsetxattr("/data/storage/el2/base/files/test.txt", "/data/storage/el2/base/files/user.txt", "dat",
166 sizeof("dat"), XATTR_CREATE);
167 if (ret == NO_ERR) {
168 ret = llistxattr("/data/storage/el2/base/files/test.txt", list, sizeof(list));
169 } else {
170 ret = FAIL;
171 }
172 remove("/data/storage/el2/base/files/test.txt");
173
174 napi_value result = nullptr;
175 if (ret != FAIL) {
176 napi_create_int32(env, FAIL, &result);
177 } else {
178 napi_create_int32(env, SUCCESS, &result);
179 }
180 return result;
181 }
182
FListXAttr(napi_env env, napi_callback_info info)183 static napi_value FListXAttr(napi_env env, napi_callback_info info)
184 {
185 ssize_t size_t = PARAM_0, len = PARAM_64;
186 int firstParam = open("/data/storage/el2/base/files/Fzl.txt", O_CREAT, PARAM_0777);
187 char secondParam[] = "/data/storage/el2/base/files";
188 size_t = flistxattr(firstParam, secondParam, len);
189 close(firstParam);
190 napi_value result = nullptr;
191 napi_create_int32(env, size_t, &result);
192 return result;
193 }
194
Lsetxattr(napi_env env, napi_callback_info info)195 static napi_value Lsetxattr(napi_env env, napi_callback_info info)
196 {
197 mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
198 int fd = open("/data/storage/el2/base/files/test.txt", O_RDWR | O_CREAT, perms);
199 char str[] = "dat";
200 write(fd, str, sizeof(str));
201 close(fd);
202 int ret = lsetxattr("/data/storage/el2/base/files/test.txt", "user.txt", "dat", PARAM_3, XATTR_CREATE);
203 napi_value result = nullptr;
204 if (ret == NO_ERR) {
205 napi_create_int32(env, SUCCESS, &result);
206 } else {
207 napi_create_int32(env, FAIL, &result);
208 }
209 remove("/data/storage/el2/base/files/test.txt");
210 return result;
211 }
212
Removexattr(napi_env env, napi_callback_info info)213 static napi_value Removexattr(napi_env env, napi_callback_info info)
214 {
215 char path[] = "/data/storage/el2/base/files/Fzl.txt";
216 int fd = open(path, O_CREAT | O_WRONLY, PARAM_0667);
217 setxattr(path, "user.foo", "bar", PARAM_4, XATTR_CREATE);
218 int result = removexattr(path, "user.foo");
219 napi_value ret = nullptr;
220 napi_create_int32(env, result, &ret);
221 close(fd);
222 return ret;
223 }
224
Lremovexattr(napi_env env, napi_callback_info info)225 static napi_value Lremovexattr(napi_env env, napi_callback_info info)
226 {
227 char path[] = "/data/storage/el2/base/files/Fzl.txt";
228 int fd = open(path, O_CREAT | O_WRONLY, PARAM_0667);
229 lsetxattr(path, "user.foo", "bar", PARAM_4, XATTR_CREATE);
230 int result = lremovexattr(path, "user.foo");
231 napi_value ret = nullptr;
232 napi_create_int32(env, result, &ret);
233 close(fd);
234 if (access(path, F_OK) == PARAM_0) {
235 remove(path);
236 }
237 return ret;
238 }
Fremovexattr(napi_env env, napi_callback_info info)239 static napi_value Fremovexattr(napi_env env, napi_callback_info info)
240 {
241 const char *path = "/data/storage/el2/base/files/Fzl.txt";
242 char buf[10];
243
244 int fd = open(path, O_CREAT | O_WRONLY, PARAM_0667);
245
246 int result = fsetxattr(fd, "user.foo", "bar", PARAM_4, PARAM_0);
247 result = fgetxattr(fd, "user.foo", buf, sizeof(buf));
248 result = fremovexattr(fd, "user.foo");
249 close(fd);
250 napi_value ret = nullptr;
251 napi_create_int32(env, result, &ret);
252 return ret;
253 }
254
Fsetxattr(napi_env env, napi_callback_info info)255 static napi_value Fsetxattr(napi_env env, napi_callback_info info)
256 {
257 const char *path = "/data/storage/el2/base/files/Fzl.txt";
258
259 int fd = open(path, O_CREAT | O_WRONLY, PARAM_0667);
260
261 int result = fsetxattr(fd, "user.foo", "bar", PARAM_4, PARAM_0);
262 close(fd);
263 napi_value ret = nullptr;
264 napi_create_int32(env, result, &ret);
265 return ret;
266 }
267
268 EXTERN_C_START
Init(napi_env env, napi_value exports)269 static napi_value Init(napi_env env, napi_value exports)
270 {
271 napi_property_descriptor desc[] = {
272 {"setxattr", nullptr, Setxattr, nullptr, nullptr, nullptr, napi_default, nullptr},
273 {"removexattr", nullptr, Removexattr, nullptr, nullptr, nullptr, napi_default, nullptr},
274 {"fremovexattr", nullptr, Fremovexattr, nullptr, nullptr, nullptr, napi_default, nullptr},
275 {"fsetxattr", nullptr, Fsetxattr, nullptr, nullptr, nullptr, napi_default, nullptr},
276 {"lgetxattr", nullptr, Lgetxattr, nullptr, nullptr, nullptr, napi_default, nullptr},
277 {"getxattr", nullptr, Getxattr, nullptr, nullptr, nullptr, napi_default, nullptr},
278 {"llistxattr", nullptr, LListxattr, nullptr, nullptr, nullptr, napi_default, nullptr},
279 {"listxattr", nullptr, Listxattr, nullptr, nullptr, nullptr, napi_default, nullptr},
280 {"lremovexattr", nullptr, Lremovexattr, nullptr, nullptr, nullptr, napi_default, nullptr},
281 {"removexattr", nullptr, Removexattr, nullptr, nullptr, nullptr, napi_default, nullptr},
282 {"lsetxattr", nullptr, Lsetxattr, nullptr, nullptr, nullptr, napi_default, nullptr},
283 {"flistxattr", nullptr, FListXAttr, nullptr, nullptr, nullptr, napi_default, nullptr}};
284 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
285 return exports;
286 }
287
288 EXTERN_C_END
289
290 static napi_module demoModule = {
291 .nm_version = 1,
292 .nm_flags = 0,
293 .nm_filename = nullptr,
294 .nm_register_func = Init,
295 .nm_modname = "xattr",
296 .nm_priv = ((void *)0),
297 .reserved = {0},
298 };
299
RegisterEntryModule(void)300 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
301