15bebb993Sopenharmony_ci/* 25bebb993Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 35bebb993Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 45bebb993Sopenharmony_ci * you may not use this file except in compliance with the License. 55bebb993Sopenharmony_ci * You may obtain a copy of the License at 65bebb993Sopenharmony_ci * 75bebb993Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 85bebb993Sopenharmony_ci * 95bebb993Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 105bebb993Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 115bebb993Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 125bebb993Sopenharmony_ci * See the License for the specific language governing permissions and 135bebb993Sopenharmony_ci * limitations under the License. 145bebb993Sopenharmony_ci */ 155bebb993Sopenharmony_ci 165bebb993Sopenharmony_ci#ifndef OHOS_FILE_FS_WATCHER_IMPL_H 175bebb993Sopenharmony_ci#define OHOS_FILE_FS_WATCHER_IMPL_H 185bebb993Sopenharmony_ci 195bebb993Sopenharmony_ci#include <mutex> 205bebb993Sopenharmony_ci#include <sys/inotify.h> 215bebb993Sopenharmony_ci#include <unordered_map> 225bebb993Sopenharmony_ci#include <unordered_set> 235bebb993Sopenharmony_ci 245bebb993Sopenharmony_ci#include "uni_error.h" 255bebb993Sopenharmony_ci#include "ffi_remote_data.h" 265bebb993Sopenharmony_ci#include "cj_common_ffi.h" 275bebb993Sopenharmony_ci#include "cj_lambda.h" 285bebb993Sopenharmony_ci#include "singleton.h" 295bebb993Sopenharmony_ci 305bebb993Sopenharmony_cinamespace OHOS { 315bebb993Sopenharmony_cinamespace CJSystemapi { 325bebb993Sopenharmony_ciconstexpr int BUF_SIZE = 1024; 335bebb993Sopenharmony_ci 345bebb993Sopenharmony_cistruct CWatchEvent { 355bebb993Sopenharmony_ci char* fileName; 365bebb993Sopenharmony_ci uint32_t event; 375bebb993Sopenharmony_ci uint32_t cookie; 385bebb993Sopenharmony_ci}; 395bebb993Sopenharmony_cistruct WatcherInfoArg { 405bebb993Sopenharmony_ci std::string fileName = ""; 415bebb993Sopenharmony_ci uint32_t events = 0; 425bebb993Sopenharmony_ci int wd = -1; 435bebb993Sopenharmony_ci int64_t callbackId = -1; 445bebb993Sopenharmony_ci std::function<void(CWatchEvent)> watchCallback_; 455bebb993Sopenharmony_ci explicit WatcherInfoArg(void (*callback)(CWatchEvent)) 465bebb993Sopenharmony_ci { 475bebb993Sopenharmony_ci watchCallback_ = CJLambda::Create(callback); 485bebb993Sopenharmony_ci callbackId = reinterpret_cast<int64_t>(callback); 495bebb993Sopenharmony_ci } 505bebb993Sopenharmony_ci ~WatcherInfoArg() = default; 515bebb993Sopenharmony_ci}; 525bebb993Sopenharmony_ci 535bebb993Sopenharmony_ciclass WatcherImpl : public OHOS::FFI::FFIData { 545bebb993Sopenharmony_cipublic: 555bebb993Sopenharmony_ci std::shared_ptr<WatcherInfoArg> data_; 565bebb993Sopenharmony_ci OHOS::FFI::RuntimeType* GetRuntimeType() override { return GetClassType(); } 575bebb993Sopenharmony_ciprivate: 585bebb993Sopenharmony_ci friend class OHOS::FFI::RuntimeType; 595bebb993Sopenharmony_ci friend class OHOS::FFI::TypeBase; 605bebb993Sopenharmony_ci static OHOS::FFI::RuntimeType* GetClassType() 615bebb993Sopenharmony_ci { 625bebb993Sopenharmony_ci static OHOS::FFI::RuntimeType runtimeType = OHOS::FFI::RuntimeType::Create<OHOS::FFI::FFIData>("WatcherImpl"); 635bebb993Sopenharmony_ci return &runtimeType; 645bebb993Sopenharmony_ci } 655bebb993Sopenharmony_ci}; 665bebb993Sopenharmony_ci 675bebb993Sopenharmony_ciclass FileWatcherManager : public Singleton<FileWatcherManager> { 685bebb993Sopenharmony_cipublic: 695bebb993Sopenharmony_ci FileWatcherManager(); 705bebb993Sopenharmony_ci ~FileWatcherManager(); 715bebb993Sopenharmony_ci FileWatcherManager(FileWatcherManager const &) = delete; 725bebb993Sopenharmony_ci void operator=(FileWatcherManager const &) = delete; 735bebb993Sopenharmony_ci 745bebb993Sopenharmony_ci int32_t GetNotifyId(); 755bebb993Sopenharmony_ci bool InitNotify(); 765bebb993Sopenharmony_ci bool AddWatcherInfo(const std::string &fileName, std::shared_ptr<WatcherInfoArg> arg); 775bebb993Sopenharmony_ci bool CheckEventValid(const uint32_t &event); 785bebb993Sopenharmony_ci int32_t StartNotify(std::shared_ptr<WatcherInfoArg> arg); 795bebb993Sopenharmony_ci void GetNotifyEvent(std::shared_ptr<WatcherInfoArg> arg); 805bebb993Sopenharmony_ci void ReadNotifyEvent(std::function<void(CWatchEvent)> callback); 815bebb993Sopenharmony_ci int32_t StopNotify(std::shared_ptr<WatcherInfoArg> arg); 825bebb993Sopenharmony_ci 835bebb993Sopenharmony_ciprivate: 845bebb993Sopenharmony_ci uint32_t RemoveWatcherInfo(std::shared_ptr<WatcherInfoArg> arg); 855bebb993Sopenharmony_ci std::tuple<bool, int> CheckEventWatched(const std::string &fileName, const uint32_t &event); 865bebb993Sopenharmony_ci void NotifyEvent(const struct inotify_event *event, std::function<void(CWatchEvent)> callback); 875bebb993Sopenharmony_ci int CloseNotifyFd(); 885bebb993Sopenharmony_ci int NotifyToWatchNewEvents(const std::string &fileName, const int &wd, const uint32_t &watchEvents); 895bebb993Sopenharmony_ci 905bebb993Sopenharmony_ciprivate: 915bebb993Sopenharmony_ci static std::mutex watchMutex_; 925bebb993Sopenharmony_ci bool run_ = false; 935bebb993Sopenharmony_ci int32_t notifyFd_ = -1; 945bebb993Sopenharmony_ci int32_t eventFd_ = -1; 955bebb993Sopenharmony_ci std::unordered_set<std::shared_ptr<WatcherInfoArg>> watcherInfoSet_; 965bebb993Sopenharmony_ci std::unordered_map<std::string, std::pair<int, uint32_t>> wdFileNameMap_; 975bebb993Sopenharmony_ci}; 985bebb993Sopenharmony_ci} // OHOS::FileManagement::ModuleFileIO 995bebb993Sopenharmony_ci} 1005bebb993Sopenharmony_ci 1015bebb993Sopenharmony_ci#endif // OHOS_FILE_FS_IMPL_H