• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 IGE_FILEMONITOR_H
17 #define IGE_FILEMONITOR_H
18 
19 #include <string>
20 #include <string_view>
21 #include <unordered_map>
22 #include <vector>
23 
24 namespace ige {
25 class FileMonitor {
26 public:
27     /** Adds path to watch list, the monitor will recursively monitor all files in this directory and it's subtree.
28         @param path Path to directory that is being monitored, such as 'x:/images/' or './images'.
29         @return True if path is succesfully added to watch list, otherwise false.
30     */
31     bool AddPath(std::string_view path);
32 
33     /** Removes path from watch list, the monitor will no longer watch files in this directory or it's subtree.
34         @param path Path to directory to be no longer monitored.
35         @return True if the watch is successfully removed, otherwise false.
36     */
37     bool RemovePath(std::string_view path);
38 
39     /** Scans for file modifications since last call to this function.
40         @param aAdded List of files that were added.
41         @param aRemoved List of files that were removed.
42         @param aModified List of files that were modified.
43     */
44     void ScanModifications(
45         std::vector<std::string>& added, std::vector<std::string>& removed, std::vector<std::string>& modified);
46 
47     std::vector<std::string> GetMonitoredFiles() const;
48 
49 private:
50     struct FileInfo {
51         time_t timestamp;
52     };
53 
54     bool AddFile(std::string_view path);
55     bool RemoveFile(const std::string& path);
56 
57     bool IsWatchingDirectory(std::string_view path);
58     bool IsWatchingSubDirectory(std::string_view path);
59 
60     std::vector<std::string> mDirectories;
61     std::unordered_map<std::string, FileInfo> mFiles;
62 };
63 
64 } // namespace ige
65 
66 #endif // IGE_FILEMONITOR_H