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 <dirent.h>
18 #include <fcntl.h>
19 #include <ftw.h>
20 #include <js_native_api.h>
21 #include <node_api.h>
22 #include <unistd.h>
23 
24 #define TEST_FLAG_SIZE 4
25 #define TEST_FD_LIMIT 128
26 #define PARAM_0666 0666
27 #define PARAM_0777 0777
28 #define R_OK 4
29 
copytoU_device(const char *file, const struct stat *sb, int flag)30 int copytoU_device(const char *file, const struct stat *sb, int flag) { return 0; }
31 
Ftw(napi_env env, napi_callback_info info)32 static napi_value Ftw(napi_env env, napi_callback_info info)
33 {
34     napi_value result = nullptr;
35     int fileDescribe = open("/data/storage/el2/base/files/fzl.txt", O_CREAT, PARAM_0777);
36     int returnValue = ftw("/data/storage/el2/base/files/", copytoU_device, fileDescribe);
37     napi_create_int32(env, returnValue, &result);
38     close(fileDescribe);
39     return result;
40 }
41 
Ftw64(napi_env env, napi_callback_info info)42 static napi_value Ftw64(napi_env env, napi_callback_info info)
43 {
44     napi_value result = nullptr;
45     int fileDescribe = open("/data/storage/el2/base/files/fzl.txt", O_CREAT, PARAM_0777);
46     int returnValue = ftw64("/data/storage/el2/base/files/", copytoU_device, fileDescribe);
47     napi_create_int32(env, returnValue, &result);
48     close(fileDescribe);
49     return result;
50 }
nftw_callback(const char *pathname, const struct stat *sb, int flag, struct FTW *ftw)51 static int nftw_callback(const char *pathname, const struct stat *sb, int flag, struct FTW *ftw) { return 0; }
52 
Nftw(napi_env env, napi_callback_info info)53 static napi_value Nftw(napi_env env, napi_callback_info info)
54 {
55     int flag[TEST_FLAG_SIZE] = {FTW_PHYS, FTW_MOUNT, FTW_CHDIR, FTW_DEPTH};
56     const char *path = "/data/storage/el2/base/files/Fzl.txt";
57     int fp = open(path, O_CREAT, PARAM_0666);
58     write(fp, "666", sizeof("666"));
59     close(fp);
60     int ret = nftw(path, nftw_callback, TEST_FD_LIMIT, flag[0]);
61     napi_value result;
62     napi_create_int32(env, ret, &result);
63     return result;
64 }
Nftw64(napi_env env, napi_callback_info info)65 static napi_value Nftw64(napi_env env, napi_callback_info info)
66 {
67     int flag[TEST_FLAG_SIZE] = {FTW_PHYS, FTW_MOUNT, FTW_CHDIR, FTW_DEPTH};
68     const char *path = "/data/storage/el2/base/files/Fzl.txt";
69     int fp = open(path, O_CREAT, PARAM_0666);
70     write(fp, "666", sizeof("666"));
71     close(fp);
72     int ret = nftw64(path, nftw_callback, TEST_FD_LIMIT, flag[0]);
73     napi_value result;
74     napi_create_int32(env, ret, &result);
75     return result;
76 }
77 EXTERN_C_START
Init(napi_env env, napi_value exports)78 static napi_value Init(napi_env env, napi_value exports)
79 {
80     napi_property_descriptor desc[] = {
81         {"ftw", nullptr, Ftw, nullptr, nullptr, nullptr, napi_default, nullptr},
82         {"ftw64", nullptr, Ftw64, nullptr, nullptr, nullptr, napi_default, nullptr},
83         {"nftw", nullptr, Nftw, nullptr, nullptr, nullptr, napi_default, nullptr},
84         {"nftw64", nullptr, Nftw64, nullptr, nullptr, nullptr, napi_default, nullptr},
85 
86     };
87     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
88     return exports;
89 }
90 
91 EXTERN_C_END
92 
93 static napi_module demoModule = {
94     .nm_version = 1,
95     .nm_flags = 0,
96     .nm_filename = nullptr,
97     .nm_register_func = Init,
98     .nm_modname = "ftw",
99     .nm_priv = ((void *)0),
100     .reserved = {0},
101 };
102 
RegisterEntryModule(void)103 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
104