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