aboutsummaryrefslogtreecommitdiff
path: root/src/3rdParty/efsw/WatcherKqueue.hpp
blob: 87d898c92147fbbcf5bcce0e7ca8f3ed3193dc16 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef EFSW_WATCHEROSX_HPP
#define EFSW_WATCHEROSX_HPP

#include <efsw/FileWatcherImpl.hpp>

#if EFSW_PLATFORM == EFSW_PLATFORM_KQUEUE || EFSW_PLATFORM == EFSW_PLATFORM_FSEVENTS

#include <efsw/DirectorySnapshot.hpp>
#include <map>
#include <sys/event.h>
#include <sys/types.h>
#include <vector>

namespace efsw {

class FileWatcherKqueue;
class WatcherKqueue;

typedef struct kevent KEvent;

/// type for a map from WatchID to WatcherKqueue pointer
typedef std::map<WatchID, Watcher*> WatchMap;

class WatcherKqueue : public Watcher {
  public:
	WatcherKqueue( WatchID watchid, const std::string& dirname, FileWatchListener* listener,
				   bool recursive, FileWatcherKqueue* watcher, WatcherKqueue* parent = NULL );

	virtual ~WatcherKqueue();

	void addFile( const std::string& name, bool emitEvents = true );

	void removeFile( const std::string& name, bool emitEvents = true );

	// called when the directory is actually changed
	// means a file has been added or removed
	// rescans the watched directory adding/removing files and sending notices
	void rescan();

	void handleAction( const std::string& filename, efsw::Action action,
					   const std::string& oldFilename = "" );

	void handleFolderAction( std::string filename, efsw::Action action,
							 const std::string& oldFilename = "" );

	void addAll();

	void removeAll();

	WatchID watchingDirectory( std::string dir );

	void watch();

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

	void removeWatch( WatchID watchid );

	bool initOK();

	int lastErrno();

  protected:
	WatchMap mWatches;
	int mLastWatchID;

	// index 0 is always the directory
	std::vector<KEvent> mChangeList;
	size_t mChangeListCount;
	DirectorySnapshot mDirSnap;

	/// The descriptor for the kqueue
	int mKqueue;

	FileWatcherKqueue* mWatcher;

	WatcherKqueue* mParent;

	bool mInitOK;
	int mErrno;

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

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

	Watcher* findWatcher( const std::string path );

	void moveDirectory( std::string oldPath, std::string newPath, bool emitEvents = true );

	void sendDirChanged();
};

} // namespace efsw

#endif

#endif