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 <cstdlib>
19 #include <ctime>
20 #include <ifaddrs.h>
21 #include <js_native_api_types.h>
22 #include <net/if.h>
23 #include <sys/inotify.h>
24 #include <unistd.h>
25 #include <utime.h>
26 #include <utmp.h>
27 #include <uv.h>
28
29 #define FAIL (-1)
30 #define PARAM_0 0
31 #define PARAM_1 1
32 #define SIZE_64 64
33 #define PARAM_0777 0777
34
Utime(napi_env env, napi_callback_info info)35 static napi_value Utime(napi_env env, napi_callback_info info)
36 {
37 size_t argc = PARAM_1;
38 napi_value args[1] = {nullptr};
39 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
40
41 struct utimbuf ubuf;
42 struct stat statinfo;
43 int toCppResult = FAIL;
44
45 size_t length = SIZE_64, stresult = PARAM_0;
46 char strTemp[length];
47 napi_get_value_string_utf8(env, args[0], strTemp, length, &stresult);
48
49 int fd = open(strTemp, O_CREAT, PARAM_0777);
50
51 close(fd);
52
53 stat(strTemp, &statinfo);
54 ubuf.modtime = PARAM_0;
55 time(&ubuf.actime);
56 if (utime(strTemp, &ubuf) == PARAM_0) {
57 stat(strTemp, &statinfo);
58 if (statinfo.st_mtim.tv_sec == PARAM_0) {
59 toCppResult = PARAM_1;
60 }
61 }
62
63 napi_value result = nullptr;
64 napi_create_int32(env, toCppResult, &result);
65 return result;
66 }
67
68 EXTERN_C_START
Init(napi_env env, napi_value exports)69 static napi_value Init(napi_env env, napi_value exports)
70 {
71 napi_property_descriptor desc[] = {
72 {"utime", nullptr, Utime, nullptr, nullptr, nullptr, napi_default, nullptr},
73 };
74 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
75 return exports;
76 }
77 EXTERN_C_END
78
79 static napi_module demoModule = {
80 .nm_version = 1,
81 .nm_flags = 0,
82 .nm_filename = nullptr,
83 .nm_register_func = Init,
84 .nm_modname = "utime",
85 .nm_priv = ((void *)0),
86 .reserved = {0},
87 };
88
RegisterModule(void)89 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
90