• 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 <cerrno>
17 #include <climits>
18 #include <cstdlib>
19 #include <dirent.h>
20 #include <fcntl.h>
21 #include <js_native_api.h>
22 #include <node_api.h>
23 #include <unistd.h>
24 #include <hilog/log.h>
25 
26 #define FAIL (-1)
27 #define MFAIL (-2)
28 #define FALSE 0
29 #define ERROR (-1)
30 #define FIVE 5
31 #define ONE 1
32 #define PATH "/data/storage/el2/base/files"
33 #define ONEVAL 1
34 #define MINUSONE (-1)
35 #define TWOVAL 2
36 #define SIXFOURVAL 64
37 #define PARAM_0 0
38 #define PARAM_1 1
39 #define PARAM_2 2
40 #define PARAM_UNNORMAL (-1)
41 #define RETURN_0 0
42 #define FAILD (-1)
43 #define ERRON_0 0
44 #define SIZE_10 10
45 #define SIZE_100 100
46 #define SIZE_4096 4096
47 #define SIZE_8192 8192
48 
49 #define INIT (-1)
50 #define SUCCESS_DIRENT 0
51 
52 #undef LOG_DOMAIN
53 #undef LOG_TAG
54 #define LOG_DOMAIN 0xFEFE
55 #define LOG_TAG "MUSL_LIBCTEST"
56 
Opendir(napi_env env,napi_callback_info info)57 static napi_value Opendir(napi_env env, napi_callback_info info)
58 {
59     DIR *dir = opendir("/data/storage/el2/base/files/test.txt");
60     int ret = FAIL;
61     if (!dir) {
62         ret = SUCCESS_DIRENT;
63     }
64     napi_value result = nullptr;
65     napi_create_int32(env, ret, &result);
66     return result;
67 }
68 
Scandir64(napi_env env,napi_callback_info info)69 static napi_value Scandir64(napi_env env, napi_callback_info info)
70 {
71     errno = ERRON_0;
72     size_t argc = PARAM_2;
73     napi_value result;
74     napi_value args[2] = {nullptr};
75     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
76     size_t length = SIXFOURVAL, stresult = PARAM_0;
77     char *strTemp = static_cast<char *>(malloc(sizeof(char) * length));
78     if (strTemp == nullptr) {
79         OH_LOG_INFO(LOG_APP, "Scandir64 malloc failed");
80         napi_create_int32(env, MFAIL, &result);
81         return result;
82     }
83     napi_get_value_string_utf8(env, args[0], strTemp, length, &stresult);
84     int valueFirst;
85     napi_get_value_int32(env, args[1], &valueFirst);
86     struct dirent **namelist;
87     if (valueFirst == PARAM_0) {
88         int total = scandir64(strTemp, &namelist, PARAM_0, alphasort);
89         napi_create_int32(env, total, &result);
90     } else {
91         int total = scandir64(nullptr, &namelist, PARAM_0, alphasort);
92         napi_create_int32(env, total, &result);
93     }
94     return result;
95 }
96 
Scandir(napi_env env,napi_callback_info info)97 static napi_value Scandir(napi_env env, napi_callback_info info)
98 {
99     errno = ERRON_0;
100     size_t argc = PARAM_2;
101     napi_value result;
102     napi_value args[2] = {nullptr};
103     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
104     size_t length = SIXFOURVAL, stresult = PARAM_0;
105     char *strTemp = static_cast<char *>(malloc(sizeof(char) * length));
106     if (strTemp == nullptr) {
107         OH_LOG_INFO(LOG_APP, "Scandir malloc failed");
108         napi_create_int32(env, MFAIL, &result);
109         return result;
110     }
111     napi_get_value_string_utf8(env, args[0], strTemp, length, &stresult);
112     int valueFirst;
113     napi_get_value_int32(env, args[1], &valueFirst);
114     struct dirent **namelist;
115     if (valueFirst == PARAM_0) {
116         int total = scandir(strTemp, &namelist, PARAM_0, alphasort);
117         napi_create_int32(env, total, &result);
118     } else {
119         int total = scandir(nullptr, &namelist, PARAM_0, alphasort);
120         napi_create_int32(env, total, &result);
121     }
122     return result;
123 }
124 
Seekdir(napi_env env,napi_callback_info info)125 static napi_value Seekdir(napi_env env, napi_callback_info info)
126 {
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 valueFirst;
131     napi_get_value_int32(env, args[PARAM_0], &valueFirst);
132     napi_value result;
133     errno = 0;
134     DIR *dir = opendir("/data/storage/el2/base/files/");
135     if (dir == nullptr) {
136         OH_LOG_INFO(LOG_APP, "Seekdir opendir failed errno : %{public}d", errno);
137         napi_create_int32(env, FAIL, &result);
138         return result;
139     }
140     readdir(dir);
141     errno = ERRON_0;
142     if (valueFirst == PARAM_0) {
143         seekdir(dir, TWOVAL);
144     } else if (valueFirst == ONEVAL) {
145         seekdir(dir, PARAM_0);
146     }
147     if (errno == PARAM_0) {
148         napi_create_int32(env, PARAM_0, &result);
149     } else {
150         napi_create_int32(env, MINUSONE, &result);
151     }
152     return result;
153 }
154 
Getdents(napi_env env,napi_callback_info info)155 static napi_value Getdents(napi_env env, napi_callback_info info)
156 {
157     napi_value result = nullptr;
158     napi_create_int32(env, SUCCESS_DIRENT, &result);
159     return result;
160 }
161 
Getdents64(napi_env env,napi_callback_info info)162 static napi_value Getdents64(napi_env env, napi_callback_info info)
163 {
164     napi_value result = nullptr;
165     napi_create_int32(env, SUCCESS_DIRENT, &result);
166     return result;
167 }
168 
169 EXTERN_C_START
Init(napi_env env,napi_value exports)170 static napi_value Init(napi_env env, napi_value exports)
171 {
172     napi_property_descriptor desc[] = {
173         {"scandir64", nullptr, Scandir64, nullptr, nullptr, nullptr, napi_default, nullptr},
174         {"scandir", nullptr, Scandir, nullptr, nullptr, nullptr, napi_default, nullptr},
175         {"seekdir", nullptr, Seekdir, nullptr, nullptr, nullptr, napi_default, nullptr},
176         {"getdents", nullptr, Getdents, nullptr, nullptr, nullptr, napi_default, nullptr},
177         {"getdents64", nullptr, Getdents64, nullptr, nullptr, nullptr, napi_default, nullptr},
178         {"opendir", nullptr, Opendir, nullptr, nullptr, nullptr, napi_default, nullptr},
179     };
180     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
181     return exports;
182 }
183 EXTERN_C_END
184 
185 static napi_module demoModule = {
186     .nm_version = 1,
187     .nm_flags = 0,
188     .nm_filename = nullptr,
189     .nm_register_func = Init,
190     .nm_modname = "dirent",
191     .nm_priv = ((void *)0),
192     .reserved = {0},
193 };
194 
RegisterEntryModule(void)195 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
196