-
Notifications
You must be signed in to change notification settings - Fork 0
/
fswatcher.cpp
138 lines (118 loc) · 4.24 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
#include "fswatcher.h"
FSWatcher::FSWatcher(QString path, QObject *parent) :
QObject(parent),
m_path(path),
m_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->m_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;
}
qDebug() << "Watches established";
}
void FSWatcher::watch()
{
qDebug() << "Started watching";
struct inotify_event * event;
QString moved_from;
uint32_t cookie;
while(true) {
event = inotifytools_next_event( DELETE_DELAY );
if ( !event ) {
if ( !inotifytools_error() ) {
// qDebug() << "Cycle elapsed";
if(!moved_from.isEmpty()) {
handleMovedAwayFile(moved_from);
moved_from.clear();
cookie = 0;
}
continue;
}
else {
qWarning() << "Watching stopped by error:" << strerror( inotifytools_error() );
return;
}
}
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;
// Obvious delete
if (event->mask & IN_DELETE) {
emit deleted(path, (event->mask & IN_ISDIR));
continue;
}
// Moved away
if ( !moved_from.isEmpty() && !(event->mask & IN_MOVED_TO)) {
handleMovedAwayFile(moved_from);
moved_from.clear();
cookie = 0;
}
// Obvious modification
if( (event->mask & IN_CLOSE_WRITE) ) {
emit modified(path);
continue;
}
// 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->m_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;
if(event->mask & IN_ISDIR) {
// if not watched...
if ( inotifytools_wd_from_filename(moved_from.toStdString().c_str()) == -1 ) {
moved_from.clear();
cookie = 0;
}
}
}
}
}
FSWatcher::~FSWatcher()
{
inotifytools_cleanup();
}
void FSWatcher::handleMovedAwayFile(QString path)
{
if ( !inotifytools_remove_watch_by_filename(
path.toStdString().c_str() ) ) {
qWarning() << "Error removing watch on" << path
<< ":" << strerror( inotifytools_error() );
}
emit deleted(path, path.endsWith(QDir::separator()));
}