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