1 /*
2 * Copyright (c) 2021 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 "watcher.h"
17
18 #include <cstring>
19 #include <fcntl.h>
20 #include <tuple>
21 #include <unistd.h>
22
23 #include "../../common/napi/n_async/n_ref.h"
24 #include "../../common/napi/n_class.h"
25 #include "../../common/napi/n_func_arg.h"
26 #include "../../common/napi/n_val.h"
27 #include "../../common/uni_error.h"
28
29 #include "../class_watcher/watcher_entity.h"
30 #include "../class_watcher/watcher_n_exporter.h"
31 namespace OHOS {
32 namespace DistributedFS {
33 namespace ModuleFileIO {
34 using namespace std;
35
RunCommand(uv_fs_event_t * handle,const char * filename,int events,int status)36 void Watcher::RunCommand(uv_fs_event_t *handle, const char *filename, int events, int status)
37 {
38 WatcherInforArg *information = (WatcherInforArg *)handle->data;
39 uint32_t eventsFirst { events };
40 uint32_t eventsSecond { information->events };
41 if (eventsFirst & eventsSecond) {
42 napi_handle_scope scope = nullptr;
43 napi_open_handle_scope(information->env, &scope);
44 napi_value callback = nullptr;
45 napi_get_reference_value(information->env, information->ref, &callback);
46 vector<napi_value> argv;
47 argv.push_back(NVal::CreateInt64(information->env, events).val_);
48 napi_value global = nullptr;
49 napi_get_global(information->env, &global);
50 napi_value tmp = nullptr;
51 napi_call_function(information->env, global, callback, argv.size(), argv.data(), &tmp);
52 napi_close_handle_scope(information->env, scope);
53 }
54 }
55
CreateWatcher(napi_env env,napi_callback_info info)56 napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info)
57 {
58 NFuncArg funcArg(env, info);
59 if (!funcArg.InitArgs(NARG_CNT::THREE)) {
60 UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
61 return nullptr;
62 }
63
64 auto [succGetPath, filename, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
65 if (!succGetPath) {
66 UniError(EINVAL).ThrowErr(env, "Invalid filename");
67 return nullptr;
68 }
69
70 auto [succGetEvent, event] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
71 if (!succGetEvent) {
72 UniError(EINVAL).ThrowErr(env, "Invalid event");
73 return nullptr;
74 }
75
76 unique_ptr<WatcherInforArg> data = make_unique<WatcherInforArg>();
77 data->events = event;
78 data->env = env;
79 NVal val = NVal(env, funcArg[NARG_POS::THIRD]);
80 napi_create_reference(val.env_, val.val_, 1, &(data->ref));
81 uv_loop_s *loop = nullptr;
82 napi_get_uv_event_loop(env, &loop);
83 unique_ptr<uv_fs_event_t, WatcherHandleDeleter> fsEventReq(new uv_fs_event_t);
84 uv_fs_event_init(loop, fsEventReq.get());
85 fsEventReq->data = data.get();
86 uv_fs_event_start(fsEventReq.get(), RunCommand, filename.get(), UV_FS_EVENT_RECURSIVE);
87
88 napi_value objWatcher = NClass::InstantiateClass(env, WatcherNExporter::className_, {});
89 if (!objWatcher) {
90 UniError(EINVAL).ThrowErr(env, "objWatcher create failed");
91 return nullptr;
92 }
93
94 auto watcherEntity = NClass::GetEntityOf<WatcherEntity>(env, objWatcher);
95 if (!watcherEntity) {
96 UniError(EINVAL).ThrowErr(env, "watcherEntity get failed");
97 return nullptr;
98 }
99
100 watcherEntity->fsEventReq_ = std::move(fsEventReq);
101 watcherEntity->data_ = std::move(data);
102
103 return objWatcher;
104 }
105 } // namespace ModuleFileIO
106 } // namespace DistributedFS
107 } // namespace OHOS