-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInotify.cpp
More file actions
115 lines (94 loc) · 3.3 KB
/
Inotify.cpp
File metadata and controls
115 lines (94 loc) · 3.3 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* \file Inotify.cpp
* \brief inotify API
*
* Usage: demo_inotify [pathname]
*
* The program monitors each of the files specified on the command line for all
* possible file events.
*
* This program is Linux-specific. The inotify API is available in Linux 2.6.13
* and later.
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
#include <sys/inotify.h>
//-------------------------------------------------------------------------------------------------
// Display information from inotify_event structure
static void
inotifyEvent_print(struct inotify_event *a_ev)
{
printf(" wd =%2d; ", a_ev->wd);
if (a_ev->cookie > 0) {
printf("cookie =%4d; ", a_ev->cookie);
}
printf("mask = ");
if (a_ev->mask & IN_ACCESS) printf("IN_ACCESS ");
if (a_ev->mask & IN_ATTRIB) printf("IN_ATTRIB ");
if (a_ev->mask & IN_CLOSE_NOWRITE) printf("IN_CLOSE_NOWRITE ");
if (a_ev->mask & IN_CLOSE_WRITE) printf("IN_CLOSE_WRITE ");
if (a_ev->mask & IN_CREATE) printf("IN_CREATE ");
if (a_ev->mask & IN_DELETE) printf("IN_DELETE ");
if (a_ev->mask & IN_DELETE_SELF) printf("IN_DELETE_SELF ");
if (a_ev->mask & IN_IGNORED) printf("IN_IGNORED ");
if (a_ev->mask & IN_ISDIR) printf("IN_ISDIR ");
if (a_ev->mask & IN_MODIFY) printf("IN_MODIFY ");
if (a_ev->mask & IN_MOVE_SELF) printf("IN_MOVE_SELF ");
if (a_ev->mask & IN_MOVED_FROM) printf("IN_MOVED_FROM ");
if (a_ev->mask & IN_MOVED_TO) printf("IN_MOVED_TO ");
if (a_ev->mask & IN_OPEN) printf("IN_OPEN ");
if (a_ev->mask & IN_Q_OVERFLOW) printf("IN_Q_OVERFLOW ");
if (a_ev->mask & IN_UNMOUNT) printf("IN_UNMOUNT ");
printf("\n");
if (a_ev->len > 0) {
printf(" name = %s\n", a_ev->name);
}
}
//-------------------------------------------------------------------------------------------------
int main(int argc, char **argv)
{
if (argc < 2 || strcmp(argv[1], "--help") == 0) {
printf("Usage: %s [PATH_NAME]\n", argv[0]);
return EXIT_SUCCESS;
}
constexpr std::size_t EVENT_SIZE {sizeof(struct inotify_event)};
constexpr std::size_t BUF_LEN {(EVENT_SIZE + NAME_MAX + 1) * 10};
int iRv {};
// Create inotify instance
int fdNotify = ::inotify_init();
STD_TEST(fdNotify != -1);
// For each command-line argument, add a watch for all events
std::vector<int> watchFds;
{
for (int j = 1; j < argc; ++ j) {
int watchFd = ::inotify_add_watch(fdNotify, argv[j], IN_ALL_EVENTS);
STD_TEST(watchFd != - 1);
printf("Watching %s using wd %d\n", argv[j], watchFd);
watchFds.push_back(watchFd);
}
}
// Read events forever
for ( ; ; ) {
char buff[BUF_LEN] {};
const ssize_t numRead = ::read(fdNotify, buff, BUF_LEN);
STD_TEST(numRead > 0);
printf("Read %zu bytes from inotify fd\n", numRead);
// Process all of the events in buffer returned by read()
for (char *p = buff; p < buff + numRead; ) {
struct inotify_event *event = (struct inotify_event *)p;
::inotifyEvent_print(event);
p += EVENT_SIZE + event->len;
}
}
// Remove watch
for (size_t i = 0; i < watchFds.size(); ++ i) {
iRv = ::inotify_rm_watch(fdNotify, watchFds[i]);
STD_TEST(iRv != -1)
}
// Clean
iRv = ::close(fdNotify);
STD_TEST(iRv != -1);
return EXIT_SUCCESS;
}
//-------------------------------------------------------------------------------------------------