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 "watcher.h"
17 
18 #include <cstring>
19 #include <fcntl.h>
20 #include <memory>
21 #include <tuple>
22 #include <unistd.h>
23 
24 #include "file_utils.h"
25 #include "filemgmt_libhilog.h"
26 #include "../class_watcher/watcher_entity.h"
27 #include "../class_watcher/watcher_n_exporter.h"
28 namespace OHOS::FileManagement::ModuleFileIO {
29 using namespace std;
30 using namespace OHOS::FileManagement::LibN;
31 
CreateAndCheckForWatcherEntity(napi_env env)32 static tuple<napi_value, int32_t> CreateAndCheckForWatcherEntity(napi_env env)
33 {
34     if (FileWatcher::GetInstance().GetNotifyId() < 0 && !FileWatcher::GetInstance().InitNotify()) {
35         HILOGE("Failed to get notifyId or initnotify fail");
36         return {nullptr, errno};
37     }
38     napi_value objWatcher = NClass::InstantiateClass(env, WatcherNExporter::className_, {});
39     if (!objWatcher) {
40         HILOGE("Failed to instantiate watcher");
41         return {nullptr, EIO};
42     }
43     return {objWatcher, ERRNO_NOERR};
44 }
45 
ParseParam(const napi_env & env,const napi_callback_info & info,int32_t & errCode)46 shared_ptr<WatcherInfoArg> ParseParam(const napi_env &env, const napi_callback_info &info, int32_t &errCode)
47 {
48     NFuncArg funcArg(env, info);
49     if (!funcArg.InitArgs(NARG_CNT::THREE)) {
50         HILOGE("Failed to get param.");
51         errCode = EINVAL;
52         return nullptr;
53     }
54 
55     auto [succGetPath, filename, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath();
56     if (!succGetPath) {
57         HILOGE("Failed to get watcher path.");
58         errCode = EINVAL;
59         return nullptr;
60     }
61 
62     auto [succGetEvent, events] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
63     if (!succGetEvent || events <= 0 || !FileWatcher::GetInstance().CheckEventValid(events)) {
64         HILOGE("Failed to get watcher event.");
65         errCode = EINVAL;
66         return nullptr;
67     }
68 
69     if (!NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_function)) {
70         HILOGE("Failed to get callback");
71         errCode = EINVAL;
72         return nullptr;
73     }
74     auto infoArg = CreateSharedPtr<WatcherInfoArg>(NVal(env, funcArg[NARG_POS::THIRD]));
75     if (infoArg == nullptr) {
76         HILOGE("Failed to request heap memory.");
77         errCode = ENOMEM;
78         return nullptr;
79     }
80     infoArg->events = static_cast<uint32_t>(events);
81     infoArg->env = env;
82     infoArg->fileName = string(filename.get());
83 
84     return infoArg;
85 }
86 
CreateWatcher(napi_env env,napi_callback_info info)87 napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info)
88 {
89     int errCode = 0;
90     auto infoArg = ParseParam(env, info, errCode);
91     if (errCode != 0) {
92         HILOGE("Failed to parse param");
93         NError(errCode).ThrowErr(env);
94         return nullptr;
95     }
96 
97     auto [objWatcher, err] = CreateAndCheckForWatcherEntity(env);
98     if (!objWatcher) {
99         HILOGE("Failed to create watcher entity.");
100         NError(err).ThrowErr(env);
101         return nullptr;
102     }
103 
104     auto watcherEntity = NClass::GetEntityOf<WatcherEntity>(env, objWatcher);
105     if (!watcherEntity) {
106         HILOGE("Failed to get WatcherEntity.");
107         NError(EIO).ThrowErr(env);
108         return nullptr;
109     }
110     watcherEntity->data_ = infoArg;
111 
112     bool ret = FileWatcher::GetInstance().AddWatcherInfo(infoArg->fileName, infoArg);
113     if (!ret) {
114         HILOGE("Failed to add watcher info.");
115         NError(EINVAL).ThrowErr(env);
116         return nullptr;
117     }
118     return objWatcher;
119 }
120 } // namespace OHOS::FileManagement::ModuleFileIO
121