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
|
#include "changewatcher.h"
#include <iostream>
#include <filesystem>
#include <sys/inotify.h>
#include <sys/eventfd.h>
#include <unistd.h>
#include <algorithm>
ChangeWatcher::ChangeWatcher()
{
}
ChangeWatcher::~ChangeWatcher()
{
setEnable(false);
}
void ChangeWatcher::watch(const std::string &newPath)
{
if(std::find(m_watches.begin(), m_watches.end(), newPath) == m_watches.end())
{
m_watches.push_back(newPath);
if(m_enabled)
{
_addWatch(newPath);
}
}
}
void ChangeWatcher::setEnable(bool newVal)
{
if(m_enabled == newVal)
return;
if(newVal)
{
// Start thread
m_fdnotify = inotify_init();
for(auto &newPath: m_watches)
{
_addWatch(newPath);
}
m_fdevent = eventfd(0, 0);
m_enabled = true;
m_thread = std::thread(_runningLoop, this);
}
else
{
// Stop thread
m_enabled = false;
uint64_t value = 1;
write(m_fdevent, &value, sizeof(value));
close(m_fdevent);
m_thread.join();
close(m_fdnotify);
}
}
bool ChangeWatcher::enabled() const
{
return m_enabled;
}
void ChangeWatcher::setFileWrited(const std::function<void (const std::string &)> &callback)
{
m_filewrited = callback;
}
void ChangeWatcher::setFileAdded(const std::function<void (const std::string &)> &callback)
{
m_fileadded = callback;
}
void ChangeWatcher::setFileRemoved(const std::function<void (const std::string &)> &callback)
{
m_fileremoved = callback;
}
void ChangeWatcher::_runningLoop(ChangeWatcher *m)
{
const size_t event_size = sizeof(struct inotify_event);
const size_t buffer_size = 1024 * (event_size + 16);
char buffer[buffer_size];
while (m->m_enabled) {
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(m->m_fdevent, &rfds);
FD_SET(m->m_fdnotify, &rfds);
int nfds = std::max(m->m_fdevent, m->m_fdnotify) + 1;
if (select(nfds, &rfds, nullptr, nullptr, nullptr) > 0) {
if (FD_ISSET(m->m_fdevent, &rfds)) {
break; // Signal d'arrêt reçu
}
if (FD_ISSET(m->m_fdnotify, &rfds)) {
int length = read(m->m_fdnotify, buffer, buffer_size);
if (length > 0) {
size_t i = 0;
while (i < length) {
auto* event = reinterpret_cast<struct inotify_event*>(&buffer[i]);
if((event->mask & IN_CLOSE_WRITE) && (m->m_filewrited != nullptr))
{
m->m_filewrited(m->m_wd[event->wd]);
}
if((event->mask & IN_CREATE) && (m->m_fileadded != nullptr))
{
m->m_fileadded(m->m_wd[event->wd]);
}
if((event->mask & IN_DELETE) && (m->m_fileremoved != nullptr))
{
m->m_fileremoved(m->m_wd[event->wd]);
}
i += event_size + event->len;
}
}
}
}
}
}
void ChangeWatcher::_addWatch(const std::string &newPath)
{
int wd = -1;
if(std::filesystem::is_directory(newPath))
{
wd = inotify_add_watch(m_fdnotify, newPath.c_str(), IN_CREATE | IN_DELETE);
}
else
{
wd = inotify_add_watch(m_fdnotify, newPath.c_str(), IN_CLOSE_WRITE);
}
if(wd > -1) m_wd[wd] = newPath;
}
|