1 /* 2 * Copyright (c) 2024 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 #ifndef CORE__IO__DEV__FILEMONITOR_H 17 #define CORE__IO__DEV__FILEMONITOR_H 18 19 #include <cstdint> 20 21 #include <base/containers/string.h> 22 #include <base/containers/string_view.h> 23 #include <base/containers/unordered_map.h> 24 #include <base/containers/vector.h> 25 #include <base/namespace.h> 26 #include <core/namespace.h> 27 28 CORE_BEGIN_NAMESPACE() 29 class IFileManager; 30 class FileMonitor { 31 public: 32 explicit FileMonitor(IFileManager&); 33 /** Adds path to watch list, the monitor will recursively monitor all files in this directory and it's subtree. 34 * @param path Path to directory that is being monitored, such as 'file://x:/images/' or 'file://./images' or 35 * 'shaders://'. 36 * @return True if path is succesfully added to watch list, otherwise false. 37 */ 38 bool AddPath(BASE_NS::string_view path); 39 40 /** Removes path from watch list, the monitor will no longer watch files in this directory or it's subtree. 41 * @param path Path to directory to be no longer monitored. 42 * @return True if the watch is successfully removed, otherwise false. 43 */ 44 bool RemovePath(BASE_NS::string_view path); 45 46 /** Scans for file modifications since last call to this function. 47 * @param added List of files that were added. 48 * @param removed List of files that were removed. 49 * @param modified List of files that were modified. 50 */ 51 void ScanModifications(BASE_NS::vector<BASE_NS::string>& added, BASE_NS::vector<BASE_NS::string>& removed, 52 BASE_NS::vector<BASE_NS::string>& modified); 53 54 /** Get list of currently monitored files. 55 * @return List of monitored files (absolute filepaths). 56 */ 57 BASE_NS::vector<BASE_NS::string> GetMonitoredFiles() const; 58 59 private: 60 struct FileInfo { 61 uint64_t timestamp { 0 }; 62 // removed is a file left over from last scan. which means it should be removed. 63 enum { 64 REMOVED = 0, 65 NOCHANGE = 1, 66 MODIFIED = 2, 67 ADDED = 3, 68 } state { REMOVED }; 69 }; 70 71 bool IsWatchingDirectory(BASE_NS::string_view path); 72 bool IsWatchingSubDirectory(BASE_NS::string_view path); 73 void RecursivelyCollectAllFiles(BASE_NS::string& path); 74 static void CleanPath(BASE_NS::string_view, BASE_NS::string&); 75 76 BASE_NS::vector<BASE_NS::string> directories_; 77 BASE_NS::unordered_map<BASE_NS::string, FileInfo> files_; 78 IFileManager& fileManager_; 79 BASE_NS::string pathTmp_; 80 }; 81 CORE_END_NAMESPACE() 82 83 #endif // CORE__IO__DEV__FILEMONITOR_H 84