• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstring>
20 #include <dirent.h>
21 #include <fcntl.h>
22 #include <ftw.h>
23 #include <js_native_api.h>
24 #include <climits>
25 #include <node_api.h>
26 #include <unistd.h>
27 
28 #define TEST_FLAG_SIZE 4
29 #define TEST_FD_LIMIT 128
30 #define R_OK 4
31 #define PARAM_0 0
32 #define PARAM_1 1
33 #define PARAM_2 2
34 #define ERRNO_0 0
35 
ftw_fn(const char * __filename,const struct stat * __status,int flag)36 int ftw_fn(const char *__filename, const struct stat *__status, int flag) { return PARAM_0; }
Ftw(napi_env env,napi_callback_info info)37 static napi_value Ftw(napi_env env, napi_callback_info info)
38 {
39     char dir[] = "/data/storage/el2/base/files";
40     size_t argc = PARAM_1;
41     napi_value args[1] = {nullptr};
42     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
43     int param;
44     napi_get_value_int32(env, args[0], &param);
45 
46     int ret;
47     if (param == PARAM_0) {
48         errno = ERRNO_0;
49         ret = ftw(dir, ftw_fn, TEST_FD_LIMIT);
50     } else {
51         errno = ERRNO_0;
52         char path[PATH_MAX * PARAM_2];
53         memset(path, 'a', sizeof(path));
54         path[PATH_MAX * PARAM_2 - PARAM_1] = PARAM_0;
55         ret = ftw(path, ftw_fn, TEST_FD_LIMIT);
56     }
57 
58     napi_value result = nullptr;
59     napi_create_int32(env, ret, &result);
60     return result;
61 }
62 
ftw64_fn(const char * __filename,const struct stat64 * __status,int flag)63 int ftw64_fn(const char *__filename, const struct stat64 *__status, int flag) { return PARAM_0; }
Ftw64(napi_env env,napi_callback_info info)64 static napi_value Ftw64(napi_env env, napi_callback_info info)
65 {
66     char dir[] = "/data/storage/el2/base/files";
67     size_t argc = PARAM_1;
68     napi_value args[1] = {nullptr};
69     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
70     int param;
71     napi_get_value_int32(env, args[0], &param);
72 
73     int ret;
74     if (param == PARAM_0) {
75         errno = ERRNO_0;
76         ret = ftw64(dir, ftw64_fn, TEST_FD_LIMIT);
77     } else {
78         errno = ERRNO_0;
79         char path[PATH_MAX * PARAM_2];
80         memset(path, 'a', sizeof(path));
81         path[PATH_MAX * PARAM_2 - PARAM_1] = PARAM_0;
82         ret = ftw64(path, ftw64_fn, TEST_FD_LIMIT);
83     }
84 
85     napi_value result = nullptr;
86     napi_create_int32(env, ret, &result);
87     return result;
88 }
89 
nftw_fn(const char * __filename,const struct stat * __status,int flag,struct FTW * __info)90 static int nftw_fn(const char *__filename, const struct stat *__status, int flag, struct FTW *__info)
91 {
92     return PARAM_0;
93 }
Nftw(napi_env env,napi_callback_info info)94 static napi_value Nftw(napi_env env, napi_callback_info info)
95 {
96     char dir[] = "/data/storage/el2/base/files";
97     size_t argc = PARAM_1;
98     napi_value args[1] = {nullptr};
99     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
100     int param;
101     napi_get_value_int32(env, args[0], &param);
102 
103     int ret;
104     if (param == PARAM_0) {
105         errno = ERRNO_0;
106         ret = nftw(dir, nftw_fn, TEST_FD_LIMIT, FTW_PHYS);
107     } else {
108         errno = ERRNO_0;
109         char path[PATH_MAX * PARAM_2];
110         memset(path, 'a', sizeof(path));
111         path[PATH_MAX * PARAM_2 - PARAM_1] = PARAM_0;
112         ret = nftw(path, nftw_fn, TEST_FD_LIMIT, FTW_PHYS);
113     }
114 
115     napi_value result = nullptr;
116     napi_create_int32(env, ret, &result);
117     return result;
118 }
119 
nftw64_fn(const char * __filename,const struct stat64 * __status,int flag,struct FTW * __info)120 static int nftw64_fn(const char *__filename, const struct stat64 *__status, int flag, struct FTW *__info)
121 {
122     return PARAM_0;
123 }
Nftw64(napi_env env,napi_callback_info info)124 static napi_value Nftw64(napi_env env, napi_callback_info info)
125 {
126     char dir[] = "/data/storage/el2/base/files";
127     size_t argc = PARAM_1;
128     napi_value args[1] = {nullptr};
129     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
130     int param;
131     napi_get_value_int32(env, args[0], &param);
132 
133     int ret;
134     if (param == PARAM_0) {
135         errno = ERRNO_0;
136         ret = nftw64(dir, nftw64_fn, TEST_FD_LIMIT, FTW_PHYS);
137     } else {
138         errno = ERRNO_0;
139         char path[PATH_MAX * PARAM_2];
140         memset(path, 'a', sizeof(path));
141         path[PATH_MAX * PARAM_2 - PARAM_1] = PARAM_0;
142         ret = nftw64(path, nftw64_fn, TEST_FD_LIMIT, FTW_PHYS);
143     }
144 
145     napi_value result = nullptr;
146     napi_create_int32(env, ret, &result);
147     return result;
148 }
149 
150 EXTERN_C_START
Init(napi_env env,napi_value exports)151 static napi_value Init(napi_env env, napi_value exports)
152 {
153     napi_property_descriptor desc[] = {
154         {"ftw", nullptr, Ftw, nullptr, nullptr, nullptr, napi_default, nullptr},
155         {"ftw64", nullptr, Ftw64, nullptr, nullptr, nullptr, napi_default, nullptr},
156         {"nftw", nullptr, Nftw, nullptr, nullptr, nullptr, napi_default, nullptr},
157         {"nftw64", nullptr, Nftw64, nullptr, nullptr, nullptr, napi_default, nullptr},
158     };
159     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
160     return exports;
161 }
162 EXTERN_C_END
163 
164 static napi_module demoModule = {
165     .nm_version = 1,
166     .nm_flags = 0,
167     .nm_filename = nullptr,
168     .nm_register_func = Init,
169     .nm_modname = "ftw1",
170     .nm_priv = ((void *)0),
171     .reserved = {0},
172 };
173 
RegisterEntryModule(void)174 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
175