• 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 #include "common/napi_helper.cpp"
16 #include "common/native_common.h"
17 #include "napi/native_api.h"
18 #include <cerrno>
19 #include <csignal>
20 #include <cstdlib>
21 #include <cstring>
22 #include <ctime>
23 #include <malloc.h>
24 #include <net/if.h>
25 #include <sched.h>
26 #include <sys/time.h>
27 #include <sys/timex.h>
28 #include <unistd.h>
29 #include <utime.h>
30 #include <utmp.h>
31 #include <uv.h>
32 
33 #define NANOSECONDS (1000000000)
34 #define TEST_FILE "/data/storage/el2/base/files/test.txt"
35 #define TEST_FILE_PATH "/data/storage/el2/base/files/"
36 #define TEST_FILE_NAME "test.txt"
37 #define PARAM_0 0
38 #define PARAM_1 1
39 #define MPARAM_1 (-1)
40 #define PARAM_3 3
41 #define PARAM_111 111
42 #define PARAM_777 777
43 #define PARAM_0666 0666
44 #define PARAM_0777 0777
45 #define MPARAM_123 (-123)
46 
Futimes_One(napi_env env,napi_callback_info info)47 static napi_value Futimes_One(napi_env env, napi_callback_info info)
48 {
49     struct stat s;
50     static struct timeval tv[2];
51     tv[0].tv_sec = s.st_atime;
52     tv[0].tv_usec = PARAM_0;
53     tv[1].tv_sec = s.st_mtime;
54     tv[1].tv_usec = PARAM_0;
55     int ret = MPARAM_1;
56     int fd = open(TEST_FILE, O_RDWR | O_CREAT, PARAM_777);
57     if (fd != MPARAM_1) {
58         ret = futimes(fd, tv);
59     }
60     close(fd);
61     napi_value result = nullptr;
62     napi_create_int32(env, ret == PARAM_0, &result);
63     remove(TEST_FILE);
64     return result;
65 }
66 
Futimes_Two(napi_env env,napi_callback_info info)67 static napi_value Futimes_Two(napi_env env, napi_callback_info info)
68 {
69     static struct timeval tv[2];
70 
71     int fd = open(TEST_FILE, O_CREAT, PARAM_0777);
72     errno = PARAM_0;
73     int ret = futimes(fd, tv);
74     close(fd);
75     napi_value result = nullptr;
76     napi_create_int32(env, ret == PARAM_0 && errno == PARAM_0, &result);
77     remove(TEST_FILE);
78     return result;
79 }
80 
Futimesat_One(napi_env env,napi_callback_info info)81 static napi_value Futimesat_One(napi_env env, napi_callback_info info)
82 {
83     int dir_fd = open(TEST_FILE_PATH, O_RDONLY | O_DIRECTORY);
84     int fd = openat(dir_fd, TEST_FILE_NAME, O_CREAT | O_RDWR | O_EXCL, PARAM_0666);
85     const char *msg = "helloworld";
86     write(fd, "msg", sizeof(msg));
87 
88     struct timeval tv[2];
89     struct stat st;
90     fstat(fd, &st);
91     close(fd);
92 
93     tv[0].tv_sec = st.st_atime + PARAM_1;
94     tv[0].tv_usec = PARAM_0;
95     tv[1].tv_sec = st.st_mtime + PARAM_1;
96     tv[1].tv_usec = PARAM_0;
97     int ret = futimesat(dir_fd, TEST_FILE_NAME, tv);
98     unlinkat(dir_fd, TEST_FILE_NAME, PARAM_0);
99     close(dir_fd);
100     remove(TEST_FILE);
101     napi_value result;
102     napi_create_int32(env, ret == PARAM_0, &result);
103     return result;
104 }
105 
Futimesat_Two(napi_env env,napi_callback_info info)106 static napi_value Futimesat_Two(napi_env env, napi_callback_info info)
107 {
108     int ret = futimesat(MPARAM_1, TEST_FILE_NAME, nullptr);
109     napi_value result;
110     napi_create_int32(env, ret == MPARAM_1 && errno == EBADF, &result);
111     return result;
112 }
113 
TzName_One(napi_env env,napi_callback_info info)114 static napi_value TzName_One(napi_env env, napi_callback_info info)
115 {
116     const char *handlerChar = "Asia/Shanghai";
117     setenv("TZ", handlerChar, PARAM_1);
118     tzset();
119     const char *tzName = "CST";
120     napi_value result = nullptr;
121     napi_create_int32(env, strncmp(tzName, tzname[0], strlen(tzName)) == PARAM_0, &result);
122     return result;
123 }
124 
TzName_Two(napi_env env,napi_callback_info info)125 static napi_value TzName_Two(napi_env env, napi_callback_info info)
126 {
127     setenv("TZ", "UTC+08:00:00", PARAM_1);
128     tzset();
129     const char *tzName = "UTC";
130     napi_value result = nullptr;
131     napi_create_int32(env, strncmp(tzName, tzname[0], strlen(tzName)) == PARAM_0, &result);
132     return result;
133 }
134 
Utimes_One(napi_env env,napi_callback_info info)135 static napi_value Utimes_One(napi_env env, napi_callback_info info)
136 {
137     int fd = open(TEST_FILE, O_RDWR | O_RSYNC | O_CREAT, PARAM_0777);
138     struct timeval tv[2] = {{1, 0}, {1, 0}};
139     int ret = utimes(TEST_FILE, tv);
140     NAPI_ASSERT(env, ret == PARAM_0, "Utimes_One--utimes failed");
141     close(fd);
142     remove(TEST_FILE);
143     napi_value result = nullptr;
144     napi_create_int32(env, ret == PARAM_0, &result);
145     return result;
146 }
147 
Utimes_Two(napi_env env,napi_callback_info info)148 static napi_value Utimes_Two(napi_env env, napi_callback_info info)
149 {
150     int fd = open(TEST_FILE, O_RDWR | O_RSYNC | O_CREAT, PARAM_0777);
151     close(fd);
152 
153     struct timeval tv[2];
154     tv[0].tv_usec = MPARAM_123;
155 
156     int ret = utimes(TEST_FILE, tv);
157 
158     napi_value result = nullptr;
159     napi_create_int32(env, ret == MPARAM_1 && errno == EINVAL, &result);
160 
161     remove(TEST_FILE);
162     return result;
163 }
164 
165 EXTERN_C_START
Init(napi_env env,napi_value exports)166 static napi_value Init(napi_env env, napi_value exports)
167 {
168     napi_property_descriptor desc[] = {
169         {"Futimes_One", nullptr, Futimes_One, nullptr, nullptr, nullptr, napi_default, nullptr},
170         {"Futimes_Two", nullptr, Futimes_Two, nullptr, nullptr, nullptr, napi_default, nullptr},
171         {"Futimesat_One", nullptr, Futimesat_One, nullptr, nullptr, nullptr, napi_default, nullptr},
172         {"Futimesat_Two", nullptr, Futimesat_Two, nullptr, nullptr, nullptr, napi_default, nullptr},
173         {"TzName_One", nullptr, TzName_One, nullptr, nullptr, nullptr, napi_default, nullptr},
174         {"TzName_Two", nullptr, TzName_Two, nullptr, nullptr, nullptr, napi_default, nullptr},
175         {"Utimes_One", nullptr, Utimes_One, nullptr, nullptr, nullptr, napi_default, nullptr},
176         {"Utimes_Two", nullptr, Utimes_Two, nullptr, nullptr, nullptr, napi_default, nullptr},
177     };
178     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
179     return exports;
180 }
181 EXTERN_C_END
182 
183 static napi_module demoModule = {
184     .nm_version = 1,
185     .nm_flags = 0,
186     .nm_filename = nullptr,
187     .nm_register_func = Init,
188     .nm_modname = "libtimendk1",
189     .nm_priv = ((void *)0),
190     .reserved = {0},
191 };
192 
RegisterModule(void)193 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
194