-
Notifications
You must be signed in to change notification settings - Fork 1
/
fswatcher.cpp
158 lines (135 loc) · 4.85 KB
/
fswatcher.cpp
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
#include "fswatcher.h"
FSWatcher::FSWatcher(QString path, QObject *parent) :
QObject(parent),
m_path(path),
events(IN_CREATE|IN_DELETE|IN_MOVE|IN_CLOSE_WRITE)
{
if(!inotifytools_initialize()) {
qWarning() << "Unable to initialize inotify watcher";
return;
}
if(!QFileInfo(m_path).exists()) {
qWarning() << "Specified path \"" << m_path << "\" does not exist";
}
if(!inotifytools_watch_recursively(m_path.toStdString().c_str(), this->events)) {
if(inotifytools_error() == ENOSPC) {
qWarning() << "Failed to watch \"" << m_path << "\"; upper limit on inotify watches reached!";
} else {
qWarning() << "Couldn't watch \"" << m_path << "\":" << strerror( inotifytools_error() );
}
return;
}
}
void FSWatcher::watch()
{
qDebug() << "Started local watcher";
struct inotify_event * event;
QString moved_from;
uint32_t cookie;
this->looper = new QTimer(this);
this->looper->setInterval(1000);
this->looper->setTimerType(Qt::VeryCoarseTimer);
connect(looper, &QTimer::timeout, [&](){
event = inotifytools_next_event( 0 );
if ( !event ) {
if ( !inotifytools_error() ) {
this->looper->setTimerType(Qt::VeryCoarseTimer);
this->looper->setInterval(1000);
//qDebug() << "Cycle elapsed";
if(!moved_from.isEmpty()) {
handleMovedAwayFile(moved_from);
moved_from.clear();
cookie = 0;
}
return;
}
else {
qWarning() << "Watching stopped by error:" << strerror( inotifytools_error() );
stop();
return;
}
}
u_int32_t event_cookie = event->cookie;
u_int32_t event_mask = event->mask;
char *event_name = event->name;
int event_wd = event->wd;
this->looper->setTimerType(Qt::PreciseTimer);
this->looper->setInterval(100);
//qDebug() << "Fast cycle elapsed";
QString path;
path.append(inotifytools_filename_from_wd( event_wd )).append( event_name );
if( (event_mask & IN_ISDIR) ) {
path.append(QDir::separator());
}
// Event debug
// qDebug() << event_cookie << inotifytools_event_to_str(event_mask) << path;
// Moved away
if ( !moved_from.isEmpty() && !(event_mask & IN_MOVED_TO) ) {
handleMovedAwayFile(moved_from);
moved_from.clear();
cookie = 0;
}
// Obvious delete
if ( (event_mask & IN_DELETE) ) {
emit deleted(path, (event_mask & IN_ISDIR));
return;
}
// Obvious modification
if( (event_mask & IN_CLOSE_WRITE) ) {
emit modified(path);
return;
}
// Obvious rename
if ( !moved_from.isEmpty() && cookie == event_cookie
&& (event_mask & IN_MOVED_TO) ){
QString new_name = path;
inotifytools_replace_filename( moved_from.toStdString().c_str(),
new_name.toStdString().c_str() );
emit moved(moved_from, new_name, (event_mask & IN_ISDIR));
// necessary cleanup
moved_from.clear();
cookie = 0;
} else if ( ((event_mask & IN_CREATE) || (event_mask & IN_MOVED_TO)) ) {
QString new_file = path;
// New file - if it is a directory, watch it
if (event_mask & IN_ISDIR) {
if( !inotifytools_watch_recursively( new_file.toStdString().c_str(), this->events )) {
qWarning() << "Couldn't watch new directory" << new_file
<< ":" << strerror( inotifytools_error() );
}
}
emit added(new_file, (event_mask & IN_ISDIR));
// cleanup for safe
moved_from.clear();
cookie = 0;
} else if ( (event_mask & IN_MOVED_FROM) ) {
moved_from = path;
cookie = event_cookie;
}
});
this->looper->start();
this->loop.exec();
}
FSWatcher::~FSWatcher()
{
inotifytools_cleanup();
}
void FSWatcher::handleMovedAwayFile(QString path)
{
emit deleted(path, path.endsWith(QDir::separator()));
inotifytools_remove_watch_by_filename(path.toStdString().c_str());
//qWarning() << "Error removing watch on" << path
// << ":" << strerror(inotifytools_error());
}
void FSWatcher::stop()
{
this->looper->stop();
this->loop.exit();
}
void FSWatcher::addRecursiveWatch(QString path)
{
if( !inotifytools_watch_recursively( path.toStdString().c_str(), this->events )) {
qWarning() << "Couldn't watch new directory" << path
<< ":" << strerror( inotifytools_error() );
}
}