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 "common/napi_helper.cpp"
17 #include "napi/native_api.h"
18 #include <cstdio>
19 #include <cstring>
20 #include <fcntl.h>
21 #include <ifaddrs.h>
22 #include <js_native_api_types.h>
23 #include <linux/quota.h>
24 #include <net/if.h>
25 #include <string>
26 #include <sys/inotify.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <utmp.h>
30 #include <uv.h>
31 
32 #define ONE 1
33 #define TWO 2
34 #define SIZE_2 2
35 #define PATH "/data/storage/el2/base/files"
36 #define PARAM_777 777
37 #define PARAM_0777 0777
38 #define PARAM_0 0
39 #define PARAM_1 1
40 #define PARAM_2 2
41 #define PARAM_UNNORMAL (-1)
42 #define ERRON_0 0
43 #define SUCCESS 1
44 #define SIZE_64 64
45 #define SEC_TIME 123840
46 #define TEST_MODE 0666
47 #define TEST_AT_FDCWD (-100)
48 #define TEST_ERROR_AT_FDCWD 100
49 #define NO_ERR 0
50 #define SUCCESS 1
51 #define FAIL (-1)
52 #define TEN 10
53 #define TEST_FIFO_MODE 0666
54 #define BUFSIZE 128
55 
56 
Chmod(napi_env env, napi_callback_info info)57 static napi_value Chmod(napi_env env, napi_callback_info info)
58 {
59     int ret = chmod("teststat.txt", S_IRWXU | S_IRWXG | S_IRWXO);
60     napi_value result;
61     napi_create_int32(env, ret, &result);
62     return result;
63 }
64 
Fstatat(napi_env env, napi_callback_info info)65 static napi_value Fstatat(napi_env env, napi_callback_info info)
66 {
67     struct stat st = {PARAM_0};
68     int ret = fstatat(AT_FDCWD, "teststat.txt", &st, PARAM_0);
69     napi_value result;
70     napi_create_int32(env, ret, &result);
71     return result;
72 }
73 
Fstatat64(napi_env env, napi_callback_info info)74 static napi_value Fstatat64(napi_env env, napi_callback_info info)
75 {
76     struct stat64 st = {PARAM_0};
77     int ret = fstatat64(AT_FDCWD, "teststat.txt", &st, PARAM_0);
78     napi_value result;
79     napi_create_int32(env, ret, &result);
80     return result;
81 }
82 
Lstat(napi_env env, napi_callback_info info)83 static napi_value Lstat(napi_env env, napi_callback_info info)
84 {
85     struct stat statbuff;
86     int32_t ret = lstat("teststat.txt", &statbuff);
87     napi_value result = nullptr;
88     if (ret == FAIL) {
89         napi_create_int32(env, PARAM_UNNORMAL, &result);
90     } else {
91         napi_create_int32(env, PARAM_0, &result);
92     }
93     return result;
94 }
95 
Lstat64(napi_env env, napi_callback_info info)96 static napi_value Lstat64(napi_env env, napi_callback_info info)
97 {
98     struct stat64 statbuff;
99     int32_t ret = lstat64("teststat.txt", &statbuff);
100     napi_value result = nullptr;
101     if (ret == FAIL) {
102         napi_create_int32(env, PARAM_UNNORMAL, &result);
103     } else {
104         napi_create_int32(env, PARAM_0, &result);
105     }
106     return result;
107 }
108 
Futimens(napi_env env, napi_callback_info info)109 static napi_value Futimens(napi_env env, napi_callback_info info)
110 {
111     int ret = futimens(PARAM_UNNORMAL, ((struct timespec[2]){{.tv_nsec = UTIME_NOW}, {.tv_nsec = UTIME_NOW}}));
112     napi_value result;
113     napi_create_int32(env, ret, &result);
114     return result;
115 }
116 
Umask(napi_env env, napi_callback_info info)117 static napi_value Umask(napi_env env, napi_callback_info info)
118 {
119     mode_t mode = FAIL;
120     mode_t ret = umask(mode);
121     ret = umask(ret);
122     napi_value result = nullptr;
123     if (ret != mode) {
124         napi_create_int32(env, FAIL, &result);
125     } else {
126         napi_create_int32(env, PARAM_0, &result);
127     }
128     return result;
129 }
130 
131 EXTERN_C_START
Init(napi_env env, napi_value exports)132 static napi_value Init(napi_env env, napi_value exports)
133 {
134     napi_property_descriptor desc[] = {
135         {"chmod", nullptr, Chmod, nullptr, nullptr, nullptr, napi_default, nullptr},
136         {"fstatat", nullptr, Fstatat, nullptr, nullptr, nullptr, napi_default, nullptr},
137         {"fstatat64", nullptr, Fstatat64, nullptr, nullptr, nullptr, napi_default, nullptr},
138         {"lstat", nullptr, Lstat, nullptr, nullptr, nullptr, napi_default, nullptr},
139         {"lstat64", nullptr, Lstat64, nullptr, nullptr, nullptr, napi_default, nullptr},
140         {"futimens", nullptr, Futimens, nullptr, nullptr, nullptr, napi_default, nullptr},
141         {"umask", nullptr, Umask, nullptr, nullptr, nullptr, napi_default, nullptr}};
142     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
143     return exports;
144 }
145 EXTERN_C_END
146 
147 static napi_module demoModule = {
148     .nm_version = 1,
149     .nm_flags = 0,
150     .nm_filename = nullptr,
151     .nm_register_func = Init,
152     .nm_modname = "stat1",
153     .nm_priv = ((void *)0),
154     .reserved = {0},
155 };
156 
RegisterModule(void)157 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
158