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 <cerrno>
17 #include "common/native_common.h"
18 #include <dirent.h>
19 #include <fcntl.h>
20 #include <js_native_api.h>
21 #include <js_native_api_types.h>
22 #include <node_api.h>
23 #include <semaphore.h>
24 #include <stdio_ext.h>
25 #include <sys/eventfd.h>
26 #include <unistd.h>
27 
28 #define ERRORNUMBER 0
29 #define SUCCESSRET 1
30 #define PARAM_0 0
31 #define PARAM_5 5
32 #define INIT (-1)
33 #define SUCCESS 0
34 #define PARAM_0777 0777
35 
Flushlbf(napi_env env, napi_callback_info info)36 static napi_value Flushlbf(napi_env env, napi_callback_info info)
37 {
38     napi_value result = nullptr;
39     errno = ERRORNUMBER;
40     _flushlbf();
41     int returnValue = ERRORNUMBER;
42     if (errno == ERRORNUMBER) {
43         returnValue = SUCCESSRET;
44     }
45     napi_create_int32(env, returnValue, &result);
46     return result;
47 }
48 
Flbf(napi_env env, napi_callback_info info)49 static napi_value Flbf(napi_env env, napi_callback_info info)
50 {
51     napi_value result = nullptr;
52     errno = ERRORNUMBER;
53     int fileDescribe = open("/data/storage/el2/base/files/fzl.txt", O_CREAT, PARAM_0777);
54     FILE *file = fopen("/data/storage/el2/base/files/fzl.txt", "a+");
55     NAPI_ASSERT(env, file != nullptr, "Flbf fopen Error");
56     __flbf(file);
57     int returnValue = ERRORNUMBER;
58     if (errno == ERRORNUMBER) {
59         returnValue = SUCCESSRET;
60     }
61     close(fileDescribe);
62     fclose(file);
63     napi_create_int32(env, returnValue, &result);
64     return result;
65 }
66 
Fwriting(napi_env env, napi_callback_info info)67 static napi_value Fwriting(napi_env env, napi_callback_info info)
68 {
69     napi_value result = nullptr;
70     int fileDescribe = open("/data/storage/el2/base/files/fzl.txt", O_CREAT, PARAM_0777);
71     FILE *file = fopen("/data/storage/el2/base/files/fzl.txt", "a+");
72     NAPI_ASSERT(env, file != nullptr, "Fwriting fopen Error");
73     int returnValue = __fwriting(file);
74     int backInfo = ERRORNUMBER;
75     if (returnValue == ERRORNUMBER) {
76         backInfo = SUCCESSRET;
77     }
78     close(fileDescribe);
79     fclose(file);
80     napi_create_int32(env, backInfo, &result);
81     return result;
82 }
83 
Fpending(napi_env env, napi_callback_info info)84 static napi_value Fpending(napi_env env, napi_callback_info info)
85 {
86     napi_value result = nullptr;
87     int fileDescribe = open("/data/storage/el2/base/files/fzl.txt", O_CREAT, PARAM_0777);
88     FILE *file = fopen("/data/storage/el2/base/files/fzl.txt", "a+");
89     NAPI_ASSERT(env, file != nullptr, "Fpending fopen Error");
90     int returnValue = __fpending(file);
91     int backInfo = ERRORNUMBER;
92     if (returnValue != PARAM_0) {
93         backInfo = SUCCESSRET;
94     }
95     close(fileDescribe);
96     fclose(file);
97     napi_create_int32(env, backInfo, &result);
98     return result;
99 }
100 
FBufSize(napi_env env, napi_callback_info info)101 static napi_value FBufSize(napi_env env, napi_callback_info info)
102 {
103     napi_value result = nullptr;
104     int fileDescribe = open("/data/storage/el2/base/files/fzl.txt", O_CREAT, PARAM_0777);
105     FILE *file = fopen("/data/storage/el2/base/files/fzl.txt", "a+");
106     NAPI_ASSERT(env, file != nullptr, "FBufSize fopen Error");
107     int returnValue = __fbufsize(file);
108     int backInfo = ERRORNUMBER;
109     if (returnValue != PARAM_0) {
110         backInfo = SUCCESSRET;
111     }
112     close(fileDescribe);
113     fclose(file);
114     napi_create_int32(env, backInfo, &result);
115     return result;
116 }
117 
Fpurge(napi_env env, napi_callback_info info)118 static napi_value Fpurge(napi_env env, napi_callback_info info)
119 {
120     napi_value result = nullptr;
121     int fileDescribe = open("/data/storage/el2/base/files/fzl.txt", O_CREAT, PARAM_0777);
122     FILE *file = fopen("/data/storage/el2/base/files/fzl.txt", "a+");
123     NAPI_ASSERT(env, file != nullptr, "Fpurge fopen Error");
124     __fpurge(file);
125     int backInfo = ERRORNUMBER;
126     if (errno == ERRORNUMBER) {
127         backInfo = SUCCESSRET;
128     }
129     close(fileDescribe);
130     fclose(file);
131     napi_create_int32(env, backInfo, &result);
132     return result;
133 }
134 
Freadable(napi_env env, napi_callback_info info)135 static napi_value Freadable(napi_env env, napi_callback_info info)
136 {
137     napi_value result = nullptr;
138     int fileDescribe = open("/data/storage/el2/base/files/fzl.txt", O_CREAT, PARAM_0777);
139     FILE *file = fopen("/data/storage/el2/base/files/fzl.txt", "a+");
140     NAPI_ASSERT(env, file != nullptr, "Freadable fopen Error");
141     int returnValue = __freadable(file);
142     int backInfo = ERRORNUMBER;
143     if (returnValue != ERRORNUMBER) {
144         backInfo = SUCCESSRET;
145     }
146     close(fileDescribe);
147     fclose(file);
148     napi_create_int32(env, backInfo, &result);
149     return result;
150 }
151 
Freading(napi_env env, napi_callback_info info)152 static napi_value Freading(napi_env env, napi_callback_info info)
153 {
154     napi_value result = nullptr;
155     int fileDescribe = open("/data/storage/el2/base/files/fzl.txt", O_CREAT, PARAM_0777);
156     FILE *file = fopen("/data/storage/el2/base/files/fzl.txt", "a+");
157     NAPI_ASSERT(env, file != nullptr, "Freading fopen Error");
158     int returnValue = __freading(file);
159     int backInfo = ERRORNUMBER;
160     if (returnValue != ERRORNUMBER) {
161         backInfo = SUCCESSRET;
162     }
163     close(fileDescribe);
164     fclose(file);
165     napi_create_int32(env, backInfo, &result);
166     return result;
167 }
168 
Fseterr(napi_env env, napi_callback_info info)169 static napi_value Fseterr(napi_env env, napi_callback_info info)
170 {
171     napi_value result = nullptr;
172     napi_create_int32(env, SUCCESS, &result);
173     return result;
174 }
175 
Fsetlocking(napi_env env, napi_callback_info info)176 static napi_value Fsetlocking(napi_env env, napi_callback_info info)
177 {
178     napi_value result = nullptr;
179     napi_create_int32(env, SUCCESS, &result);
180     return result;
181 }
182 
Fwritable(napi_env env, napi_callback_info info)183 static napi_value Fwritable(napi_env env, napi_callback_info info)
184 {
185     napi_value result = nullptr;
186     int fileDescribe = open("/data/storage/el2/base/files/fzl.txt", O_CREAT, PARAM_0777);
187     FILE *file = fopen("/data/storage/el2/base/files/fzl.txt", "a+");
188     NAPI_ASSERT(env, file != nullptr, "Fwritable fopen Error");
189     int returnValue = __fwritable(file);
190     int backInfo = ERRORNUMBER;
191     if (returnValue != ERRORNUMBER) {
192         backInfo = SUCCESSRET;
193     }
194     close(fileDescribe);
195     fclose(file);
196     napi_create_int32(env, backInfo, &result);
197     return result;
198 }
199 
DPrintf(napi_env env, napi_callback_info info)200 static napi_value DPrintf(napi_env env, napi_callback_info info)
201 {
202     const char *path = "/data/storage/el2/base/files/Fzl.txt";
203     const char *mode = "r";
204     int backParam = INIT;
205     FILE *file = fopen(path, mode);
206     NAPI_ASSERT(env, file != nullptr, "DPrintf fopen Error");
207     napi_value result = nullptr;
208     backParam = dprintf(SUCCESS, path);
209     fclose(file);
210     napi_create_int32(env, backParam, &result);
211     return result;
212 }
213 
FEof_unlocked(napi_env env, napi_callback_info info)214 static napi_value FEof_unlocked(napi_env env, napi_callback_info info)
215 {
216     const char *path = "/data/storage/el2/base/files/Fzl.txt";
217     const char *mode = "r";
218     int backParam = INIT;
219     FILE *stream = fopen(path, mode);
220     NAPI_ASSERT(env, stream != nullptr, "FEof_unlocked fopen Error");
221     backParam = feof_unlocked(stream);
222     fclose(stream);
223     napi_value result = nullptr;
224     napi_create_int32(env, backParam, &result);
225     return result;
226 }
227 
FError_unlocked(napi_env env, napi_callback_info info)228 static napi_value FError_unlocked(napi_env env, napi_callback_info info)
229 {
230     const char *path = "/data/storage/el2/base/files/Fzl.txt";
231     const char *mode = "r";
232     int fileDescribe = SUCCESS;
233     FILE *stream = fopen(path, mode);
234     NAPI_ASSERT(env, stream != nullptr, "FError_unlocked fopen Error");
235     napi_value result = nullptr;
236     if (stream != nullptr) {
237         fileDescribe = ferror_unlocked(stream);
238         napi_create_int32(env, fileDescribe, &result);
239     }
240     fclose(stream);
241     napi_create_int32(env, fileDescribe, &result);
242     return result;
243 }
244 
FFlush_unlocked(napi_env env, napi_callback_info info)245 static napi_value FFlush_unlocked(napi_env env, napi_callback_info info)
246 {
247     const char *path = "/data/storage/el2/base/files/Fzl.txt";
248     const char *mode = "r";
249     FILE *stream = fopen(path, mode);
250     NAPI_ASSERT(env, stream != nullptr, "FFlush_unlocked fopen Error");
251     napi_value result = nullptr;
252     int fileDescribe = SUCCESS;
253     fileDescribe = fflush_unlocked(stream);
254     fclose(stream);
255     napi_create_int32(env, fileDescribe, &result);
256     return result;
257 }
258 
FGetC_unlocked(napi_env env, napi_callback_info info)259 static napi_value FGetC_unlocked(napi_env env, napi_callback_info info)
260 {
261     const char *path = "/data/storage/el2/base/files/Fzl.txt";
262     const char *mode = "r";
263     FILE *stream = fopen(path, mode);
264     NAPI_ASSERT(env, stream != nullptr, "FGetC_unlocked fopen Error");
265     napi_value result = nullptr;
266     int fileDescribe;
267     fileDescribe = fgetc_unlocked(stream);
268     fclose(stream);
269     napi_create_int32(env, fileDescribe, &result);
270     return result;
271 }
272 
FGetLn(napi_env env, napi_callback_info info)273 static napi_value FGetLn(napi_env env, napi_callback_info info)
274 {
275     int backResult = INIT;
276     const char *path = "/data/storage/el2/base/files/Fzl.txt";
277     const char *mode = "r";
278     char *getResult = nullptr;
279     size_t size_t1 = PARAM_0;
280     FILE *stream = fopen(path, mode);
281     NAPI_ASSERT(env, stream != nullptr, "FGetLn fopen Error");
282     napi_value result = nullptr;
283     getResult = fgetln(stream, &size_t1);
284     if (getResult != nullptr) {
285         getResult = SUCCESS;
286     }
287     fclose(stream);
288     napi_create_int32(env, backResult, &result);
289     return result;
290 }
291 
FGetS_unlocked(napi_env env, napi_callback_info info)292 static napi_value FGetS_unlocked(napi_env env, napi_callback_info info)
293 {
294     int backResult = INIT;
295     const char *path = "/data/storage/el2/base/files/Fzl.txt";
296     const char *mode = "r";
297     char *getResult = nullptr;
298     char *gets = nullptr;
299     FILE *stream = fopen(path, mode);
300     NAPI_ASSERT(env, stream != nullptr, "FGetS_unlocked fopen Error");
301     int fileDescribe = PARAM_0;
302     napi_value result = nullptr;
303     getResult = fgets_unlocked(gets, fileDescribe, stream);
304     if (getResult != nullptr) {
305         getResult = SUCCESS;
306     }
307     fclose(stream);
308     napi_create_int32(env, backResult, &result);
309     return result;
310 }
311 
FileNo_unlocked(napi_env env, napi_callback_info info)312 static napi_value FileNo_unlocked(napi_env env, napi_callback_info info)
313 {
314     const char *path = "/data/storage/el2/base/files/Fzl.txt";
315     const char *mode = "r";
316     FILE *stream = fopen(path, mode);
317     NAPI_ASSERT(env, stream != nullptr, "FileNo_unlocked fopen Error");
318     int backResult;
319     napi_value result = nullptr;
320     backResult = fileno_unlocked(stream);
321     fclose(stream);
322     napi_create_int32(env, backResult, &result);
323     return result;
324 }
325 
FClose(napi_env env, napi_callback_info info)326 static napi_value FClose(napi_env env, napi_callback_info info)
327 {
328     int backParam;
329     const char *path = "/data/storage/el2/base/files/Fzl.txt";
330     const char *mode = "r";
331     FILE *stream = fopen(path, mode);
332     NAPI_ASSERT(env, stream != nullptr, "FClose fopen Error");
333     backParam = fclose(stream);
334     napi_value result = nullptr;
335     napi_create_int32(env, backParam, &result);
336     return result;
337 }
338 
FMemOpen(napi_env env, napi_callback_info info)339 static napi_value FMemOpen(napi_env env, napi_callback_info info)
340 {
341     int backParam = INIT;
342     void *buf = nullptr;
343     size_t size = PARAM_5;
344     const char *path = "/data/storage/el2/base/files/Fzl.txt";
345     const char *mode = "r";
346     FILE *stream = fopen(path, mode);
347     NAPI_ASSERT(env, stream != nullptr, "FMemOpen fopen Error");
348     FILE *newStreeam = nullptr;
349     newStreeam = fmemopen(buf, size, mode);
350     if (newStreeam != nullptr) {
351         backParam = SUCCESS;
352     }
353     fclose(stream);
354     fclose(newStreeam);
355     napi_value result = nullptr;
356     napi_create_int32(env, backParam, &result);
357     return result;
358 }
359 
FlockFile(napi_env env, napi_callback_info info)360 static napi_value FlockFile(napi_env env, napi_callback_info info)
361 {
362     int backParam = INIT;
363     fpos_t pos;
364     const char *path = "/data/storage/el2/base/files/Fzl.txt";
365     const char *mode = "r";
366     FILE *stream = fopen(path, mode);
367     NAPI_ASSERT(env, stream != nullptr, "FlockFile fopen Error");
368     flockfile(stream);
369     backParam = fgetpos(stream, &pos);
370     funlockfile(stream);
371     fclose(stream);
372     napi_value result = nullptr;
373     napi_create_int32(env, backParam, &result);
374     return result;
375 }
376 
FileNo(napi_env env, napi_callback_info info)377 static napi_value FileNo(napi_env env, napi_callback_info info)
378 {
379     int backParam = INIT;
380     const char *path = "/data/storage/el2/base/files/Fzl.txt";
381     const char *mode = "r";
382     FILE *stream = fopen(path, mode);
383     NAPI_ASSERT(env, stream != nullptr, "FileNo fopen Error");
384     backParam = fileno(stream);
385     fclose(stream);
386     napi_value result = nullptr;
387     napi_create_int32(env, backParam, &result);
388     return result;
389 }
390 
FError(napi_env env, napi_callback_info info)391 static napi_value FError(napi_env env, napi_callback_info info)
392 {
393     int backParam;
394     const char *path = "/data/storage/el2/base/files/Fzl.txt";
395     const char *mode = "r";
396     FILE *stream = fopen(path, mode);
397     NAPI_ASSERT(env, stream != nullptr, "FError fopen Error");
398     backParam = ferror(stream);
399     fclose(stream);
400     napi_value result = nullptr;
401     napi_create_int32(env, backParam, &result);
402     return result;
403 }
404 
FGets(napi_env env, napi_callback_info info)405 static napi_value FGets(napi_env env, napi_callback_info info)
406 {
407     int backParam = INIT;
408     char *getResult = nullptr;
409     char str[256];
410     const char *path = "hidumper -s 1201";
411     const char *mode = "r";
412     FILE *stream = fopen(path, mode);
413     NAPI_ASSERT(env, stream != nullptr, "FGets fopen Error");
414     fprintf(stream, "Hello, world!\n");
415     getResult = fgets(str, sizeof(str), stream);
416     if (getResult == nullptr) {
417         backParam = SUCCESS;
418     }
419     fclose(stream);
420     napi_value result = nullptr;
421     napi_create_int32(env, backParam, &result);
422     return result;
423 }
424 
FGetPos64(napi_env env, napi_callback_info info)425 static napi_value FGetPos64(napi_env env, napi_callback_info info)
426 {
427     int backParam;
428     const char *path = "/data/storage/el2/base/files/Fzl.txt";
429     const char *mode = "r";
430     FILE *stream = fopen(path, mode);
431     NAPI_ASSERT(env, stream != nullptr, "FGetPos64 fopen Error");
432     fpos_t pos;
433     backParam = fgetpos64(stream, &pos);
434     fclose(stream);
435     napi_value result = nullptr;
436     napi_create_int32(env, backParam, &result);
437     return result;
438 }
439 
FGetPos(napi_env env, napi_callback_info info)440 static napi_value FGetPos(napi_env env, napi_callback_info info)
441 {
442     int backParam;
443     const char *path = "/data/storage/el2/base/files/Fzl.txt";
444     const char *mode = "r";
445     FILE *stream = fopen(path, mode);
446     NAPI_ASSERT(env, stream != nullptr, "FGetPos fopen Error");
447     fpos_t pos;
448     backParam = fgetpos(stream, &pos);
449     fclose(stream);
450     napi_value result = nullptr;
451     napi_create_int32(env, backParam, &result);
452     return result;
453 }
454 
FGetC(napi_env env, napi_callback_info info)455 static napi_value FGetC(napi_env env, napi_callback_info info)
456 {
457     const char *path = "/data/storage/el2/base/files/Fzl.txt";
458     const char *mode = "r";
459     FILE *stream = fopen(path, mode);
460     NAPI_ASSERT(env, stream != nullptr, "FGetC fopen Error");
461     fprintf(stream, "Hello, world!\n");
462     int backParam = INIT;
463     backParam = fgetc(stream);
464     fclose(stream);
465     napi_value result = nullptr;
466     napi_create_int32(env, backParam, &result);
467     return result;
468 }
469 
FFlush(napi_env env, napi_callback_info info)470 static napi_value FFlush(napi_env env, napi_callback_info info)
471 {
472     int backParam;
473     const char *path = "/data/storage/el2/base/files/Fzl.txt";
474     const char *mode = "r";
475     FILE *stream = fopen(path, mode);
476     NAPI_ASSERT(env, stream != nullptr, "FFlush fopen Error");
477     backParam = fflush(stream);
478     fclose(stream);
479     napi_value result = nullptr;
480     napi_create_int32(env, backParam, &result);
481     return result;
482 }
483 
FdOpen(napi_env env, napi_callback_info info)484 static napi_value FdOpen(napi_env env, napi_callback_info info)
485 {
486     int backResult;
487     const char *path = "/data/storage/el2/base/files/Fzl.txt";
488     const char *mode = "r";
489     int fileDescribe = open(path, O_CREAT, PARAM_0777);
490     FILE *stream = fdopen(fileDescribe, mode);
491     backResult = fclose(stream);
492     close(fileDescribe);
493     napi_value result = nullptr;
494     napi_create_int32(env, backResult, &result);
495     return result;
496 }
497 
Init(napi_env env, napi_value exports)498 EXTERN_C_START static napi_value Init(napi_env env, napi_value exports)
499 {
500     napi_property_descriptor desc[] = {
501         {"flushLbf", nullptr, Flushlbf, nullptr, nullptr, nullptr, napi_default, nullptr},
502         {"fWriting", nullptr, Fwriting, nullptr, nullptr, nullptr, napi_default, nullptr},
503         {"fPending", nullptr, Fpending, nullptr, nullptr, nullptr, napi_default, nullptr},
504         {"fPurge", nullptr, Fpurge, nullptr, nullptr, nullptr, napi_default, nullptr},
505         {"fReadable", nullptr, Freadable, nullptr, nullptr, nullptr, napi_default, nullptr},
506         {"fReading", nullptr, Freading, nullptr, nullptr, nullptr, napi_default, nullptr},
507         {"fSetErr", nullptr, Fseterr, nullptr, nullptr, nullptr, napi_default, nullptr},
508         {"fSetLocking", nullptr, Fsetlocking, nullptr, nullptr, nullptr, napi_default, nullptr},
509         {"fWritable", nullptr, Fwritable, nullptr, nullptr, nullptr, napi_default, nullptr},
510         {"fLbf", nullptr, Flbf, nullptr, nullptr, nullptr, napi_default, nullptr},
511         {"fBufSize", nullptr, FBufSize, nullptr, nullptr, nullptr, napi_default, nullptr},
512         {"fclose", nullptr, FClose, nullptr, nullptr, nullptr, napi_default, nullptr},
513         {"fmemopen", nullptr, FMemOpen, nullptr, nullptr, nullptr, napi_default, nullptr},
514         {"flockfile", nullptr, FlockFile, nullptr, nullptr, nullptr, napi_default, nullptr},
515         {"fileno", nullptr, FileNo, nullptr, nullptr, nullptr, napi_default, nullptr},
516         {"ferror", nullptr, FError, nullptr, nullptr, nullptr, napi_default, nullptr},
517         {"fgets", nullptr, FGets, nullptr, nullptr, nullptr, napi_default, nullptr},
518         {"fgetpos", nullptr, FGetPos, nullptr, nullptr, nullptr, napi_default, nullptr},
519         {"fgetpos64", nullptr, FGetPos64, nullptr, nullptr, nullptr, napi_default, nullptr},
520         {"fgetc", nullptr, FGetC, nullptr, nullptr, nullptr, napi_default, nullptr},
521         {"fflush", nullptr, FFlush, nullptr, nullptr, nullptr, napi_default, nullptr},
522         {"fdopen", nullptr, FdOpen, nullptr, nullptr, nullptr, napi_default, nullptr},
523         {"fileno_unlocked", nullptr, FileNo_unlocked, nullptr, nullptr, nullptr, napi_default, nullptr},
524         {"fgets_unlocked", nullptr, FGetS_unlocked, nullptr, nullptr, nullptr, napi_default, nullptr},
525         {"fgetln", nullptr, FGetLn, nullptr, nullptr, nullptr, napi_default, nullptr},
526         {"fgetc_unlocked", nullptr, FGetC_unlocked, nullptr, nullptr, nullptr, napi_default, nullptr},
527         {"fflush_unlocked", nullptr, FFlush_unlocked, nullptr, nullptr, nullptr, napi_default, nullptr},
528         {"ferror_unlocked", nullptr, FError_unlocked, nullptr, nullptr, nullptr, napi_default, nullptr},
529         {"feof_unlocked", nullptr, FEof_unlocked, nullptr, nullptr, nullptr, napi_default, nullptr},
530         {"dprintf", nullptr, DPrintf, nullptr, nullptr, nullptr, napi_default, nullptr},
531     };
532     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
533     return exports;
534 }
535 EXTERN_C_END
536 
537 static napi_module demoModule = {
538     .nm_version = 1,
539     .nm_flags = 0,
540     .nm_filename = nullptr,
541     .nm_register_func = Init,
542     .nm_modname = "libstdioextndk",
543     .nm_priv = ((void *)0),
544     .reserved = {0},
545 };
546 
RegisterModule(void)547 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }