1 #ifndef IGE_FILEMONITOR_H
2 #define IGE_FILEMONITOR_H
3 
4 /*
5  * Copyright (C) 2023 Huawei Device Co., Ltd.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include <string>
20 #include <vector>
21 #include <map>
22 
23 namespace ige
24 {
25 
26 class FileMonitor
27 {
28 public:
29     FileMonitor();
30     ~FileMonitor();
31 
32     /** Adds path to watch list, the monitor will recursively monitor all files in this directory and it's subtree.
33         @param aPath Path to directory that is being monitored, such as 'x:/images/' or './images'.
34         @return True if path is succesfully added to watch list, otherwise false.
35     */
36     bool AddPath(const std::string &aPath);
37 
38     /** Removes path from watch list, the monitor will no longer watch files in this directory or it's subtree.
39         @param aPath Path to directory to be no longer monitored.
40         @return True if the watch is successfully removed, otherwise false.
41     */
42     bool RemovePath(const std::string &aPath);
43 
44     /** Scans for file modifications since last call to this function.
45         @param aAdded List of files that were added.
46         @param aRemoved List of files that were removed.
47         @param aModified List of files that were modified.
48     */
49     void scanModifications(std::vector<std::string>& aAdded, std::vector<std::string>& aRemoved, std::vector<std::string>& aModified);
50 
51     std::vector<std::string> getMonitoredFiles() const;
52 
53 private:
54 
55     struct FileInfo
56     {
57         time_t timestamp;
58     };
59 
60     bool AddFile(const std::string &aPath);
61     bool RemoveFile(const std::string &aPath);
62 
63     bool IsWatchingDirectory(const std::string &aPath);
64     bool IsWatchingSubDirectory(const std::string &aPath);
65 
66     std::vector<std::string> mDirectories;
67     std::map<std::string, FileInfo> mFiles;
68 };
69 
70 } // ige
71 
72 #endif // IGE_FILEMONITOR_H