Repository navigation
Expand file tree
/
Copy pathFileChangeMonitor.cpp
More file actions
457 lines (369 loc) · 14.1 KB
/
Copy pathFileChangeMonitor.cpp
File metadata and controls
457 lines (369 loc) · 14.1 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/***************************************************************************
* Copyright (C) 2013~2013 by Lenky *
* lenky0401@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/inotify.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/inotify.h>
#include <signal.h>
#include <dirent.h>
#include <QMap>
#include <QString>
#include <QDebug>
#include <QDir>
#include "FileChangeMonitor.h"
#define MAX_FAIL_COUNT 10
#define EVENT_SIZE (sizeof(struct inotify_event))
#define BUF_LEN (1024 * (EVENT_SIZE + 16))
#define CARE_EVENT_MASK (IN_MODIFY | IN_ATTRIB | IN_MOVED_FROM | IN_MOVED_TO | \
IN_CREATE | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF | IN_DONT_FOLLOW)
FileChangeMonitor::FileChangeMonitor()
{
mDisableWatchEventTime = 0;
mChangePathHaveOut = false;
}
FileChangeMonitor::~FileChangeMonitor()
{
free(mRootDiskDirPathName);
close(mFileMonitor);
}
bool FileChangeMonitor::init()
{
if ((mFileMonitor = inotify_init()) == -1)
return false;
//设置描述符为非阻塞,这个很重要,因为后面对该描述符会进行循环read
fcntl(mFileMonitor, F_SETFL, O_NONBLOCK);
return true;
}
int FileChangeMonitor::isWatchedKuaiPanDir(const char *kuaiPanDirPathName)
{
QHashIterator<int, QString> iter(mWatchKuaiPanDirHash);
while (iter.hasNext()) {
iter.next();
if (iter.value() == kuaiPanDirPathName)
return iter.key();
}
return -1;
}
bool FileChangeMonitor::addWatchDiskDir(const char *diskDirPathName)
{
int wd;
QDir watchDir;
char *buf;
int pathLen;
char *p;
char *kuaiPanDirPathName;
pathLen = strlen(diskDirPathName);
if ((buf = (char *)malloc(pathLen + 2)) == NULL) {
perror("malloc error");
return false;
}
memset(buf, 0, pathLen + 2);
memcpy(buf, diskDirPathName, pathLen);
watchDir = QDir(buf);
if (!watchDir.exists()) {
free(buf);
return false;
}
if (buf[pathLen - 1] != '/')
buf[pathLen] = '/';
if ((p = strstr(buf, mRootDiskDirPathName)) == NULL) {
free(buf);
return false;
}
kuaiPanDirPathName = buf + strlen(mRootDiskDirPathName);
//不考虑已加入监控的情况,直接重复加入,以免遗漏
//if (isWatchedKuaiPanDir(kuaiPanDirPathName) != -1) {
// free(buf);
// return true;
//}
wd = inotify_add_watch(mFileMonitor, buf, CARE_EVENT_MASK);
if (wd == -1) {
perror("inotify_add_watch error");
free(buf);
return false;
}
// qDebug() << buf;
// qDebug() << (buf + strlen(mRootDiskDirPathName) - 1);
mWatchKuaiPanDirHash.insert(wd, kuaiPanDirPathName);
free(buf);
return true;
}
bool FileChangeMonitor::setWatchRootDiskDir(const char *rootDiskDirPathName)
{
int pathLen;
pathLen = strlen(rootDiskDirPathName);
if ((mRootDiskDirPathName = (char *)malloc(pathLen + 2)) == NULL) {
perror("malloc error");
return false;
}
memset(mRootDiskDirPathName, 0, pathLen + 2);
memcpy(mRootDiskDirPathName, rootDiskDirPathName, pathLen);
if (mRootDiskDirPathName[pathLen - 1] == '/')
mRootDiskDirPathName[pathLen - 1] = '\0';
return scanAndAddWatchDiskDirRecursive(rootDiskDirPathName);
}
bool FileChangeMonitor::scanAndAddWatchDiskDirRecursive(const char *diskDirPathName)
{
QDir watchDir;
QFileInfoList list;
QFileInfoList::Iterator iter;
watchDir = QDir(diskDirPathName);
if (!watchDir.exists()) {
mDelayAddWatch.insert(diskDirPathName, 1);
return false;
}
if (!addWatchDiskDir(diskDirPathName)) {
mDelayAddWatch.insert(diskDirPathName, 1);
return false;
}
watchDir.setFilter(QDir::Dirs | QDir::Hidden);
list = watchDir.entryInfoList();
for (iter = list.begin(); iter != list.end(); ++ iter) {
if (iter->isDir() && "." != iter->fileName() && ".." != iter->fileName()) {
scanAndAddWatchDiskDirRecursive(Q2stdstring(iter->absoluteFilePath()).data());
}
}
return true;
}
//过滤文件/文件夹,即对用户指定不做同步的文件/文件夹进行事件忽略,返回true则忽略
bool FileChangeMonitor::FilterExcludeFileAndPath(inotify_event *event)
{
int wd;
int len;
QString name;
QString totalDiskPath;
std::vector<std::string>::iterator iter;
wd = event->wd;
len = event->len;
if (len == 0 || mWatchKuaiPanDirHash.contains(wd) == false)
return true;
name = event->name;
totalDiskPath = mRootDiskDirPathName + mWatchKuaiPanDirHash[wd] + name;
if (event->mask & IN_ISDIR) {
totalDiskPath += "/";
}
// qDebug() << totalDiskPath;
//过滤一个特定路径不同步
//std::vector<std::string> mExcludeFolderPath;
for (iter = mExcludeFolderPath.begin(); iter != mExcludeFolderPath.end(); ++ iter) {
if (totalDiskPath.startsWith(std2QString(((std::string)*iter))) == true)
return true;
}
if (event->mask & IN_ISDIR) {
//过滤所有路径下文件夹前缀不同步
//std::vector<std::string> mExcludeFolderPrefix;
for (iter = mExcludeFolderPrefix.begin(); iter != mExcludeFolderPrefix.end(); ++ iter) {
if (name.startsWith(std2QString(((std::string)*iter))) == true)
return true;
}
//过滤所有路径下文件夹后缀不同步
//std::vector<std::string> mExcludeFolderPostfix;
for (iter = mExcludeFolderPostfix.begin(); iter != mExcludeFolderPostfix.end(); ++ iter) {
if (name.endsWith(std2QString(((std::string)*iter))) == true)
return true;
}
} else {
//过滤所有路径下文件前缀不同步
//std::vector<std::string> mExcludeFilePrefix;
for (iter = mExcludeFilePrefix.begin(); iter != mExcludeFilePrefix.end(); ++ iter) {
if (name.startsWith(std2QString(((std::string)*iter))) == true)
return true;
}
//过滤所有路径下文件后缀不同步
//std::vector<std::string> mExcludeFilePostfix;
for (iter = mExcludeFilePostfix.begin(); iter != mExcludeFilePostfix.end(); ++ iter) {
if (name.endsWith(std2QString(((std::string)*iter))) == true)
return true;
}
}
//过滤所有路径下的文件和文件夹名称不同步
//std::vector<std::string> mExcludeFileName;
for (iter = mExcludeFileName.begin(); iter != mExcludeFileName.end(); ++ iter) {
if (name == std2QString(((std::string)*iter)))
return true;
}
return false;
}
void FileChangeMonitor::delayAddWatchDiskDir()
{
QHashIterator<QString, int> iter(mDelayAddWatch);
while (iter.hasNext()) {
iter.next();
QString totalDiskPath = iter.key();
int failCount = iter.value();
if (failCount > MAX_FAIL_COUNT) {
mDelayAddWatch.remove(totalDiskPath);
continue;
}
if (!addWatchDiskDir(Q2stdstring(totalDiskPath).data())) {
mDelayAddWatch.insert(totalDiskPath, ++ failCount);
} else {
mDelayAddWatch.remove(totalDiskPath);
}
}
}
void FileChangeMonitor::addWatchDiskDir(inotify_event *event)
{
QString totalDiskPath;
if (event->mask & IN_ISDIR == 0 || event->len == 0 || mWatchKuaiPanDirHash.contains(event->wd) == false)
return;
totalDiskPath = mRootDiskDirPathName + mWatchKuaiPanDirHash[event->wd] + event->name + "/";
// qDebug() << totalDiskPath;
scanAndAddWatchDiskDirRecursive(Q2stdstring(totalDiskPath).data());
}
void FileChangeMonitor::removeWatchDiskDir(inotify_event *event)
{
int wd;
QString kuaiPanDirPathName;
if (event->mask & IN_ISDIR == 0 || event->len == 0 || mWatchKuaiPanDirHash.contains(event->wd) == false)
return;
kuaiPanDirPathName = mWatchKuaiPanDirHash[event->wd] + event->name + "/";
if ((wd = isWatchedKuaiPanDir(Q2stdstring(kuaiPanDirPathName).data())) == -1)
return;
inotify_rm_watch(mFileMonitor, wd);
//不考虑inotify_rm_watch失败情况,直接删除,以便后续加入进来
mWatchKuaiPanDirHash.remove(wd);
}
void FileChangeMonitor::removeWatchDiskDirSelf(inotify_event *event)
{
//len must be zero
if (event->mask & IN_ISDIR == 0 || event->len != 0 || mWatchKuaiPanDirHash.contains(event->wd) == false)
return;
//文件夹被删除了,没法做递归了,不过inotify本身应该会针对每个监控做事件触发
inotify_rm_watch(mFileMonitor, event->wd);
//不考虑inotify_rm_watch失败情况,直接删除,以便后续加入进来
mWatchKuaiPanDirHash.remove(event->wd);
}
//处理事件,成功处理返回true
bool FileChangeMonitor::dealWithEvent(inotify_event *event)
{
if (event->mask & IN_IGNORED) {
//qDebug() << "event->wd: " << event->wd;
//qDebug() << "event->mask: " << event->mask;
//qDebug() << "event->cookie: " << event->cookie;
//qDebug() << "event->len: " << event->len;
//qDebug() << "event->name: " << event->name;
return false;
} else if ((event->mask & IN_MOVED_FROM) || (event->mask & IN_DELETE)) {
if (event->mask & IN_ISDIR) {
removeWatchDiskDir(event);
}
} else if ((event->mask & IN_DELETE_SELF) || (event->mask & IN_MOVE_SELF)) {
if (event->mask & IN_ISDIR) {
removeWatchDiskDirSelf(event);
}
} else if ((event->mask & IN_CREATE) || (event->mask & IN_MOVED_TO)) {
if (event->mask & IN_ISDIR) {
addWatchDiskDir(event);
}
} else if ((event->mask & IN_MODIFY) || (event->mask & IN_ATTRIB)) {
//Ok, Do Nothing
} else {
return false;
}
return true;
}
//将事件对应文件/文件夹加入到mChangePath
void FileChangeMonitor::addEvent2ChangePath(inotify_event *event)
{
QString totalKuaiPanPath;
std::string totalKuaiPanPathString;
if (event->len == 0 || mWatchKuaiPanDirHash.contains(event->wd) == false)
return;
totalKuaiPanPath = mWatchKuaiPanDirHash[event->wd] + event->name;
if (event->mask & IN_ISDIR) {
totalKuaiPanPath += "/";
}
totalKuaiPanPathString = Q2stdstring(totalKuaiPanPath);
//这个查找,如果外部并不关心是否重复的话,可以去掉,以提高效率
if (std::find(mChangePath.begin(), mChangePath.end(), totalKuaiPanPathString) != mChangePath.end())
return;
mChangePath.push_back(totalKuaiPanPathString);
}
void FileChangeMonitor::discardAllEvent()
{
char buffer[BUF_LEN];
int length;
while((length = read(mFileMonitor, buffer, BUF_LEN)) > 0)
;
}
std::vector<std::string>* FileChangeMonitor::EventLoop()
{
char buffer[BUF_LEN];
fd_set watchSet;
int length, i;
int ret;
struct timeval timeout;
FD_ZERO(&watchSet);
FD_SET(mFileMonitor, &watchSet);
//这个timeout值不可随意改动,除非你知道你自己在做什么
timeout.tv_sec = 1;
timeout.tv_usec = 0;
if (mChangePathHaveOut == true) {
mChangePathHaveOut = false;
mChangePath.clear();
}
ret = select(FD_SETSIZE, &watchSet, NULL, NULL, &timeout);
if (mDisableWatchEventTime == (time_t)-1) {
discardAllEvent();
return NULL;
} else if (mDisableWatchEventTime != (time_t)0) {
if (mDisableWatchEventTime > (time_t)time(NULL)) {
discardAllEvent();
return NULL;
} else
mDisableWatchEventTime = 0;
}
if (ret > 0) {
if (FD_ISSET(mFileMonitor, &watchSet)) {
do {
i = 0;
length = read(mFileMonitor, buffer, BUF_LEN);
while (i < length) {
struct inotify_event *event = (struct inotify_event *) &buffer[i];
i += EVENT_SIZE + event->len;
if (FilterExcludeFileAndPath(event))
continue;
if (dealWithEvent(event))
addEvent2ChangePath(event);
}
} while (length > 0);
}
} else if (((ret == -1 && errno == EINTR) || ret == 0) && mChangePath.size() > 0) {
//如果在一个timeout内有事件发生,那么不将列表导出
//只有在:1,发生信号,即:(ret == -1 && errno == EINTR);
//2,在一个timeout内没有事件发生,即ret == 0
//才将列表导出,这样做的原因是,留个timeout时间的缓存,避免事件频繁导出,比如在做大量文件拷贝时,
//也许就只是在文件拷贝完的最后才做一次导出,提高效率。
if (mDelayAddWatch.size() > 0)
delayAddWatchDiskDir();
mChangePathHaveOut = true;
return &mChangePath;
}
return NULL;
}