-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.cpp
More file actions
59 lines (47 loc) · 1.32 KB
/
main.cpp
File metadata and controls
59 lines (47 loc) · 1.32 KB
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
#include <iostream>
#include <thread>
#include "InotifyManager.h"
using namespace std;
/** A simple implementation of the InotifyEventHandler interface for the demo
application. It will print the name and type of each event it receives, then
ask the InotifyManager to terminate after the 5th event.
*/
class MyEventHandler : public InotifyEventHandler {
private:
int eventCount = 0;
public:
bool handle(InotifyEvent &e){
string t;
if(e.getFlags() & IN_CREATE)
t = string("creation");
else if(e.getFlags() & IN_DELETE)
t = string("deletion");
else
t = string("unknown");
cout << "An event occurred!" << endl;
cout << " * Name : " << e.getName() << endl;
cout << " * Type : " << t << endl;
cout << endl;
if(++eventCount == 5)
return true;
return false;
}
};
/** The directory specified in the first command line argument will be watched
for creation and deletion events. Each event will be handled by the
MyEventHandler class.
*/
int main(int argc, char *argv[]){
if(argc != 2){
cerr << "Please specify a directory to watch." << endl;
cerr << "usage: " << argv[0] << " <directory_path>" << endl;
return 1;
}
InotifyManager m;
MyEventHandler h;
InotifyWatch *w = m.addWatch(argv[1], IN_CREATE | IN_DELETE);
w->addEventHandler(h);
thread t = m.startWatching();
t.join();
return 0;
}