• 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 "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 <utmp.h>
22 #include <uv.h>
23 
24 #define FALSE 0
25 #define TRUE 1
26 #define ERROR (-1)
27 #define TWO 2
28 #define NO_ERRORS 0
29 #define CHMOD 777
30 
InotifyAddWatch(napi_env env,napi_callback_info info)31 static napi_value InotifyAddWatch(napi_env env, napi_callback_info info)
32 {
33     char path[] = "/data/storage/el2/base/files/";
34     int ret = inotify_add_watch(ERROR, path, IN_ALL_EVENTS);
35     if (ret != ERROR) {
36         ret = TRUE;
37     }
38     napi_value result = nullptr;
39     napi_create_int32(env, ret, &result);
40     return result;
41 }
42 
InotifyRmWatch(napi_env env,napi_callback_info info)43 static napi_value InotifyRmWatch(napi_env env, napi_callback_info info)
44 {
45     int ret = inotify_rm_watch(ERROR, ERROR);
46     if (ret != ERROR) {
47         ret = TRUE;
48     }
49     napi_value result = nullptr;
50     napi_create_int32(env, ret, &result);
51     return result;
52 }
53 
54 EXTERN_C_START
Init(napi_env env,napi_value exports)55 static napi_value Init(napi_env env, napi_value exports)
56 {
57     napi_property_descriptor desc[] = {
58         {"inotifyAddWatch", nullptr, InotifyAddWatch, nullptr, nullptr, nullptr, napi_default, nullptr},
59         {"inotifyRmWatch", nullptr, InotifyRmWatch, nullptr, nullptr, nullptr, napi_default, nullptr}};
60     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
61     return exports;
62 }
63 EXTERN_C_END
64 
65 static napi_module demoModule = {
66     .nm_version = 1,
67     .nm_flags = 0,
68     .nm_filename = nullptr,
69     .nm_register_func = Init,
70     .nm_modname = "libinotify1",
71     .nm_priv = ((void *)0),
72     .reserved = {0},
73 };
74 
RegisterModule(void)75 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
76