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