-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmonitordir_linux_inotify.cc
More file actions
214 lines (190 loc) · 4.75 KB
/
Copy pathmonitordir_linux_inotify.cc
File metadata and controls
214 lines (190 loc) · 4.75 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "monitordir.hpp"
#include <cassert>
#include <fcntl.h>
#include <iostream>
#include <sys/inotify.h>
#include <thread>
#include <unistd.h>
std::string maskToString(uint32_t mask)
{
if ((mask & IN_ACCESS) != 0U) {
return "IN_ACCESS: ";
}
if ((mask & IN_MODIFY) != 0U) {
return "IN_MODIFY: ";
}
if ((mask & IN_ATTRIB) != 0U) {
return "IN_ATTRIB: ";
}
if ((mask & IN_CLOSE_WRITE) != 0U) {
return "IN_CLOSE_WRITE: ";
}
if ((mask & IN_CLOSE_NOWRITE) != 0U) {
return "IN_CLOSE_NOWRITE: ";
}
if ((mask & IN_CLOSE) != 0U) {
return "IN_CLOSE: ";
}
if ((mask & IN_OPEN) != 0U) {
return "IN_OPEN: ";
}
if ((mask & IN_MOVED_FROM) != 0U) {
return "IN_MOVED_FROM: ";
}
if ((mask & IN_MOVED_TO) != 0U) {
return "IN_MOVED_TO: ";
}
if ((mask & IN_MOVE) != 0U) {
return "IN_MOVE: ";
}
if ((mask & IN_CREATE) != 0U) {
return "IN_CREATE: ";
}
if ((mask & IN_DELETE) != 0U) {
return "IN_DELETE: ";
}
if ((mask & IN_DELETE_SELF) != 0U) {
return "IN_DELETE_SELF: ";
}
if ((mask & IN_MOVE_SELF) != 0U) {
return "IN_MOVE_SELF: ";
}
if ((mask & IN_UNMOUNT) != 0U) {
return "IN_UNMOUNT: ";
}
if ((mask & IN_Q_OVERFLOW) != 0U) {
return "IN_Q_OVERFLOW: ";
}
if ((mask & IN_IGNORED) != 0U) {
return "IN_IGNORED: ";
}
if ((mask & IN_ISDIR) != 0U) {
return "IN_ISDIR: ";
}
return "Unknown mask " + std::to_string(mask) + ": ";
}
class MonitorDir::MonitorDirPrivate
{
public:
explicit MonitorDirPrivate(MonitorDir *q)
: q_ptr(q)
{}
~MonitorDirPrivate() = default;
auto createFd() -> bool
{
// 创建inotify实例
inotifyFd = inotify_init();
if (inotifyFd == -1) {
perror("inotify_init");
exit(EXIT_FAILURE);
}
return true;
}
void closeFd()
{
if (inotifyFd != -1) {
close(inotifyFd);
inotifyFd = -1;
}
}
auto addWatch() -> bool
{
// 添加监控目录
watchFd = inotify_add_watch(inotifyFd, dir.c_str(), IN_ALL_EVENTS);
if (watchFd == -1) {
perror("inotify_add_watch");
exit(EXIT_FAILURE);
}
std::cout << "add watch: " << dir << std::endl;
return true;
}
void removeWatch()
{
if (watchFd != -1) {
inotify_rm_watch(inotifyFd, watchFd);
watchFd = -1;
}
}
void monitor()
{
constexpr size_t eventSize = sizeof(struct inotify_event);
constexpr size_t bufLen = 1024 * (eventSize + 16); // Buffer to hold multiple events
char buf[bufLen];
auto len = read(inotifyFd, buf, bufLen);
if (!isRunning.load()) {
return;
}
if (len == -1) {
std::cerr << "read failed" << std::endl;
return;
}
for (char *ptr = buf; ptr < buf + len;
ptr += eventSize + reinterpret_cast<struct inotify_event *>(ptr)->len) {
auto *event = reinterpret_cast<struct inotify_event *>(ptr);
std::string fileEvent = maskToString(event->mask);
if (event->len > 0) {
fileEvent += event->name;
}
std::cout << fileEvent << std::endl;
}
}
void run()
{
if (!createFd()) {
return;
}
if (!addWatch()) {
closeFd();
return;
}
isRunning.store(true);
while (isRunning.load()) {
monitor();
}
removeWatch();
closeFd();
}
MonitorDir *q_ptr;
int inotifyFd = -1;
int watchFd = -1;
std::filesystem::path dir;
std::atomic_bool isRunning;
std::thread monitorThread;
};
MonitorDir::MonitorDir(const std::filesystem::path &dir)
: d_ptr(std::make_unique<MonitorDirPrivate>(this))
, m_dir(dir)
, m_isRunning(false)
{
assert(std::filesystem::is_directory(dir) && std::filesystem::exists(dir));
d_ptr->dir = dir;
}
MonitorDir::~MonitorDir()
{
stop();
}
auto MonitorDir::start() -> bool
{
if (m_isRunning) {
std::cerr << "MonitorDir is already running" << std::endl;
return false;
}
m_isRunning.store(true);
d_ptr->monitorThread = std::thread([this] {
d_ptr->run();
m_isRunning.store(false);
});
return true;
}
void MonitorDir::stop()
{
if (!m_isRunning.load()) {
std::cerr << "MonitorDir is not running" << std::endl;
return;
}
d_ptr->isRunning.store(false);
wakeWatch(m_dir.string());
if (d_ptr->monitorThread.joinable()) {
d_ptr->monitorThread.join();
}
}