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