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