aboutsummaryrefslogtreecommitdiff
path: root/src/3rdParty/efsw/FileWatcherInotify.hpp
blob: dc922acfb6956a2d8078739f845e41e4c1e4e1e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#ifndef EFSW_FILEWATCHERLINUX_HPP
#define EFSW_FILEWATCHERLINUX_HPP

#include <efsw/FileWatcherImpl.hpp>

#if EFSW_PLATFORM == EFSW_PLATFORM_INOTIFY

#include <efsw/WatcherInotify.hpp>
#include <map>
#include <vector>

namespace efsw {

/// Implementation for Linux based on inotify.
/// @class FileWatcherInotify
class FileWatcherInotify : public FileWatcherImpl {
  public:
	/// type for a map from WatchID to WatchStruct pointer
	typedef std::map<WatchID, WatcherInotify*> WatchMap;

	FileWatcherInotify( FileWatcher* parent );

	virtual ~FileWatcherInotify();

	/// Add a directory watch
	/// On error returns WatchID with Error type.
	WatchID addWatch( const std::string& directory, FileWatchListener* watcher, bool recursive );

	/// Remove a directory watch. This is a brute force lazy search O(nlogn).
	void removeWatch( const std::string& directory );

	/// Remove a directory watch. This is a map lookup O(logn).
	void removeWatch( WatchID watchid );

	/// Updates the watcher. Must be called often.
	void watch();

	/// Handles the action
	void handleAction( Watcher* watch, const std::string& filename, unsigned long action,
					   std::string oldFilename = "" );

	/// @return Returns a list of the directories that are being watched
	std::list<std::string> directories();

  protected:
	/// Map of WatchID to WatchStruct pointers
	WatchMap mWatches;

	/// User added watches
	WatchMap mRealWatches;

	/// inotify file descriptor
	int mFD;

	Thread* mThread;

	Mutex mWatchesLock;
	Mutex mRealWatchesLock;
	Mutex mInitLock;
	std::vector<std::pair<WatcherInotify*, std::string>> mMovedOutsideWatches;

	WatchID addWatch( const std::string& directory, FileWatchListener* watcher, bool recursive,
					  WatcherInotify* parent = NULL );

	bool pathInWatches( const std::string& path );

  private:
	void run();

	void removeWatchLocked( WatchID watchid );

	void checkForNewWatcher( Watcher* watch, std::string fpath );

	Watcher* watcherContainsDirectory( std::string dir );
};

} // namespace efsw

#endif

#endif