MRPT  2.0.4
CFileSystemWatcher.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/system/os.h>
12 #include <mutex>
13 #include <queue>
14 #include <thread>
15 
16 namespace mrpt::system
17 {
18 /** This class subscribes to notifications of file system changes, thus it can
19  * be used to efficiently stay informed about changes in a directory tree.
20  * - Windows: Requires Windows 2000 or newer.
21  * - Linux: Requires kernel 2.6.13 or newer.
22  * Using this class in an old Linux or other unsoported system (Unix,etc...)
23  * has no effect, i.e. no notification will be ever received.
24  * \sa CDirectoryExplorer
25  * \ingroup mrpt_system_grp
26  */
28 {
29  public:
30  /** Each of the changes detected by utils::CFileSystemWatcher
31  */
33  {
34  TFileSystemChange() = default;
35  /** Complete path of the file/directory that has changed. */
36  std::string path{};
37  /** Whether the event happened to a file or a directory. */
38  bool isDir{false};
39  bool eventModified{false};
40  bool eventCloseWrite{false};
41  bool eventDeleted{false};
42  bool eventMovedTo{false};
43  bool eventMovedFrom{false};
44  bool eventCreated{false};
45  bool eventAccessed{false};
46  };
47 
48  using TFileSystemChangeList = std::deque<TFileSystemChange>;
49 
50  /** Creates the subscription to a specified path.
51  * \param path The file or directory to watch.
52  */
53  CFileSystemWatcher(const std::string& path);
54 
55  /** Destructor
56  */
57  virtual ~CFileSystemWatcher();
58 
59  /** Call this method sometimes to get the list of changes in the watched
60  * directory.
61  * \sa processChange
62  */
63  void getChanges(TFileSystemChangeList& out_list);
64 
65  private:
66  /** Ended in "/" */
67  std::string m_watchedDirectory;
68 #ifdef _WIN32
69  void* m_hNotif{nullptr};
70  std::thread m_watchThread;
71  /** Watch thread; only needed in win32 */
72  void thread_win32_watch();
73  std::queue<TFileSystemChange*> m_queue_events_win32_msgs;
74  mutable std::mutex m_queue_events_win32_cs;
75 #endif
76 
77 #if defined(MRPT_OS_LINUX) || defined(__APPLE__)
78  /** The fd returned by inotify_init. */
79  int m_fd;
80  /** The fd of the watch. */
81  int m_wd;
82 #endif
83 
84 }; // End of class def.
85 
86 } // namespace mrpt::system
std::queue< TFileSystemChange * > m_queue_events_win32_msgs
std::string path
Complete path of the file/directory that has changed.
void getChanges(TFileSystemChangeList &out_list)
Call this method sometimes to get the list of changes in the watched directory.
std::deque< TFileSystemChange > TFileSystemChangeList
std::string m_watchedDirectory
Ended in "/".
void thread_win32_watch()
Watch thread; only needed in win32.
This class subscribes to notifications of file system changes, thus it can be used to efficiently sta...
bool isDir
Whether the event happened to a file or a directory.
virtual ~CFileSystemWatcher()
Destructor.
Each of the changes detected by utils::CFileSystemWatcher.
CFileSystemWatcher(const std::string &path)
Creates the subscription to a specified path.



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020