• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_WATCHER_WATCHER_ENTITY_H
16 #define INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_WATCHER_WATCHER_ENTITY_H
17 
18 #include <memory>
19 #include <mutex>
20 #include <string>
21 #include <sys/inotify.h>
22 #include <unordered_map>
23 #include <unordered_set>
24 #include <uv.h>
25 
26 #include "filemgmt_libn.h"
27 #include "singleton.h"
28 namespace OHOS::FileManagement::ModuleFileIO {
29 using WatcherCallback = void (*)(napi_env env,
30                                  LibN::NRef &callback,
31                                  const std::string &filename,
32                                  const uint32_t &event,
33                                  const uint32_t &cookie);
34 
35 constexpr int BUF_SIZE = 1024;
36 struct WatcherInfoArg {
37     std::string fileName = "";
38     uint32_t events = 0;
39     int wd = -1;
40     napi_env env = nullptr;
41     LibN::NRef nRef;
WatcherInfoArgWatcherInfoArg42     explicit WatcherInfoArg(LibN::NVal jsVal) : nRef(jsVal) {}
43     ~WatcherInfoArg() = default;
44 };
45 
46 class FileWatcher : public Singleton<FileWatcher> {
47 public:
48     FileWatcher();
49     ~FileWatcher();
50 
51     FileWatcher(FileWatcher const &) = delete;
52     void operator=(FileWatcher const &) = delete;
53 
54     int32_t GetNotifyId();
55     bool InitNotify();
56     int StartNotify(std::shared_ptr<WatcherInfoArg> arg);
57     int StopNotify(std::shared_ptr<WatcherInfoArg> arg);
58     void GetNotifyEvent(WatcherCallback callback);
59     bool AddWatcherInfo(const std::string &fileName, std::shared_ptr<WatcherInfoArg> arg);
60     bool CheckEventValid(const uint32_t &event);
61 private:
62     uint32_t RemoveWatcherInfo(std::shared_ptr<WatcherInfoArg> arg);
63     std::tuple<bool, int> CheckEventWatched(const std::string &fileName, const uint32_t &event);
64     void NotifyEvent(const struct inotify_event *event, WatcherCallback callback);
65     int CloseNotifyFd();
66     int NotifyToWatchNewEvents(const std::string &fileName, const int &wd, const uint32_t &watchEvents);
67 
68 private:
69     static std::mutex watchMutex_;
70     bool run_ = false;
71     int32_t notifyFd_ = -1;
72     std::unordered_set<std::shared_ptr<WatcherInfoArg>> watcherInfoSet_;
73     std::unordered_map<std::string, std::pair<int, uint32_t>> wdFileNameMap_;
74 };
75 
76 struct WatcherEntity {
77     std::shared_ptr<WatcherInfoArg> data_;
78 };
79 } // namespace OHOS::FileManagement::ModuleFileIO namespace OHOS
80 #endif
81