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 "napi/native_api.h"
17 #include <ifaddrs.h>
18 #include <js_native_api_types.h>
19 #include <net/if.h>
20 #include <sys/inotify.h>
21 #include <unistd.h>
22 #include <utmp.h>
23 #include <uv.h>
24
25 #define FALSE 0
26 #define TRUE 1
27 #define ERROR (-1)
28 #define TWO 2
29 #define ONE 1
30 #define NO_ERRORS 0
31 #define CHMOD 777
32
InotifyInit(napi_env env,napi_callback_info info)33 static napi_value InotifyInit(napi_env env, napi_callback_info info)
34 {
35 int ret = inotify_init();
36 napi_value result = nullptr;
37 if (ret != ERROR) {
38 ret = NO_ERRORS;
39 }
40 napi_create_int32(env, ret, &result);
41 return result;
42 }
43
InotifyInit1(napi_env env,napi_callback_info info)44 static napi_value InotifyInit1(napi_env env, napi_callback_info info)
45 {
46 size_t argc = ONE;
47 napi_value args[1] = {nullptr};
48 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
49
50 int valueZero;
51 int in_cloexec = TRUE;
52 int inNonblock = TWO;
53 napi_get_value_int32(env, args[0], &valueZero);
54
55 if (valueZero == in_cloexec) {
56 valueZero = IN_CLOEXEC;
57 }
58 if (valueZero == inNonblock) {
59 valueZero = IN_NONBLOCK;
60 }
61 int fd = inotify_init1(valueZero);
62 if (fd >= NO_ERRORS) {
63 fd = TRUE;
64 }
65 napi_value result = nullptr;
66 napi_create_int32(env, fd, &result);
67 return result;
68 }
69
InotifyAddWatch(napi_env env,napi_callback_info info)70 static napi_value InotifyAddWatch(napi_env env, napi_callback_info info)
71 {
72 char path[] = "/data/storage/el2/base/files/testWatch";
73 if (access(path, NO_ERRORS) != NO_ERRORS) {
74 mkdir(path, CHMOD);
75 } else {
76 remove(path);
77 mkdir(path, CHMOD);
78 }
79 errno = FALSE;
80 int fd = inotify_init();
81 int wd = ERROR;
82 if (fd != ERROR) {
83 wd = inotify_add_watch(fd, path, IN_ALL_EVENTS);
84 inotify_rm_watch(fd, wd);
85 }
86 if (wd != ERROR) {
87 wd = TRUE;
88 }
89 napi_value result = nullptr;
90 napi_create_int32(env, wd, &result);
91 return result;
92 }
93
InotifyRmWatch(napi_env env,napi_callback_info info)94 static napi_value InotifyRmWatch(napi_env env, napi_callback_info info)
95 {
96 char path[] = "/data/storage/el2/base/files/testWatch";
97 if (access(path, NO_ERRORS) != NO_ERRORS) {
98 mkdir(path, CHMOD);
99 } else {
100 remove(path);
101 mkdir(path, CHMOD);
102 }
103 errno = FALSE;
104 int fd = inotify_init();
105 int wd = ERROR;
106 if (fd != ERROR) {
107 wd = inotify_add_watch(fd, path, IN_ALL_EVENTS);
108 wd = inotify_rm_watch(fd, wd);
109 }
110 if (wd != ERROR) {
111 wd = TRUE;
112 }
113 napi_value result = nullptr;
114 napi_create_int32(env, wd, &result);
115 return result;
116 }
117
118 EXTERN_C_START
Init(napi_env env,napi_value exports)119 static napi_value Init(napi_env env, napi_value exports)
120 {
121 napi_property_descriptor desc[] = {
122 {"inotifyInit", nullptr, InotifyInit, nullptr, nullptr, nullptr, napi_default, nullptr},
123 {"inotifyInit1", nullptr, InotifyInit1, nullptr, nullptr, nullptr, napi_default, nullptr},
124 {"inotifyAddWatch", nullptr, InotifyAddWatch, nullptr, nullptr, nullptr, napi_default, nullptr},
125 {"inotifyRmWatch", nullptr, InotifyRmWatch, nullptr, nullptr, nullptr, napi_default, nullptr}};
126 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
127 return exports;
128 }
129 EXTERN_C_END
130
131 static napi_module demoModule = {
132 .nm_version = 1,
133 .nm_flags = 0,
134 .nm_filename = nullptr,
135 .nm_register_func = Init,
136 .nm_modname = "libinotify",
137 .nm_priv = ((void *)0),
138 .reserved = {0},
139 };
140
RegisterModule(void)141 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
142