1 /* 2 * Copyright (c) 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 #ifndef INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_WATCHER_WATCHER_DATA_CACHE_H 16 #define INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_WATCHER_WATCHER_DATA_CACHE_H 17 18 #include <memory> 19 #include <mutex> 20 #include <string> 21 #include <unordered_map> 22 #include <vector> 23 24 #include "fs_watch_entity.h" 25 26 namespace OHOS::FileManagement::ModuleFileIO { 27 28 class WatcherDataCache { 29 public: 30 bool AddWatcherInfo(std::shared_ptr<WatcherInfo> info); 31 uint32_t RemoveWatcherInfo(std::shared_ptr<WatcherInfo> info); 32 bool RemoveFileWatcher(const std::string &fileName); 33 std::tuple<bool, int32_t> FindWatchedWd(const std::string &fileName, uint32_t event); 34 bool UpdateWatchedEvents(const std::string &fileName, int32_t wd, uint32_t events); 35 std::tuple<bool, std::string, std::vector<std::shared_ptr<WatcherInfo>>> FindWatcherInfos( 36 int32_t wd, uint32_t eventMask); 37 uint32_t GetFileEvents(const std::string &fileName); 38 bool HasWatcherInfo() const; 39 void ClearCache(); 40 41 public: 42 WatcherDataCache() = default; 43 ~WatcherDataCache() = default; 44 WatcherDataCache(const WatcherDataCache &) = delete; 45 WatcherDataCache &operator=(const WatcherDataCache &) = delete; 46 47 private: 48 mutable std::mutex cacheMutex_; 49 std::vector<std::shared_ptr<WatcherInfo>> watcherInfoCache_; 50 std::unordered_map<std::string, std::pair<int32_t, uint32_t>> wdFileNameCache_; 51 }; 52 53 } // namespace OHOS::FileManagement::ModuleFileIO 54 #endif // INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_WATCHER_WATCHER_DATA_CACHE_H