• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     }
53 }
54 
CreateWatcher(napi_env env,napi_callback_info info)55 napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info)
56 {
57     NFuncArg funcArg(env, info);
58     if (!funcArg.InitArgs(NARG_CNT::THREE)) {
59         UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
60         return nullptr;
61     }
62 
63     bool succGetPath = false;
64     unique_ptr<char[]> filename;
65     tie(succGetPath, filename, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
66     if (!succGetPath) {
67         UniError(EINVAL).ThrowErr(env, "Invalid filename");
68         return nullptr;
69     }
70     bool succGetEvent = false;
71     int event;
72     tie(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     unique_ptr<uv_fs_event_t, WatcherHandleDeleter> fsEventReq(new uv_fs_event_t);
86     uv_fs_event_init(loop, fsEventReq.get());
87     fsEventReq->data = data.get();
88     uv_fs_event_start(fsEventReq.get(), RunCommand, filename.get(), UV_FS_EVENT_RECURSIVE);
89 
90     napi_value objWatcher = NClass::InstantiateClass(env, WatcherNExporter::className_, {});
91     if (!objWatcher) {
92         UniError(EINVAL).ThrowErr(env, "objWatcher create failed");
93         return nullptr;
94     }
95     auto watcherEntity = NClass::GetEntityOf<WatcherEntity>(env, objWatcher);
96     if (!watcherEntity) {
97         UniError(EINVAL).ThrowErr(env, "watcherEntity get failed");
98         return nullptr;
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