-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcryptowatcher.cpp
More file actions
151 lines (124 loc) · 3.67 KB
/
cryptowatcher.cpp
File metadata and controls
151 lines (124 loc) · 3.67 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
#include "cryptowatcher.h"
#include <QDebug>
#include <QDirIterator>
#define EVENT_SIZE (sizeof (struct inotify_event))
#define BUF_LEN (1024*(EVENT_SIZE + 16))
int CryptoWatcher::deduceEvent(struct inotify_event *event)
{
int retVal = 0;
if (event->len && !(event->mask & IN_ISDIR)) {
if (event->mask & IN_CREATE) {
// New file, just add it to the queue
qDebug() << "The file" << event->name << "was created.";
retVal = 1;
} else if (event->mask & IN_MODIFY) {
// File is modified and algo is running, so we need to readd it
qDebug() << "The file" << event->name << "was modified.";
retVal = 2;
} else if (event->mask & IN_DELETE) {
// If algo is not running and we need to remove the file from queue
qDebug() << "The file" << event->name << "was deleted.";
retVal = 3;
}
}
return retVal;
}
void CryptoWatcher::dispatchSignal(struct inotify_event *event)
{
int deduceVal = this->deduceEvent(event);
QString path = this->inputDirPath + '/' + event->name;
switch (deduceVal) {
case 1:
emit this->newFile(path);
break;
case 2:
emit this->modifiedFile(path);
break;
case 3:
emit this->removedFile(path);
break;
}
}
QList<QString> CryptoWatcher::getAllFromFolder(const QString &folderName)
{
QDirIterator folder(folderName, QDir::Files);
QList<QString> absolutPaths;
while (folder.hasNext()) {
QFileInfo next(folder.next());
absolutPaths.append(next.absoluteFilePath());
}
return absolutPaths;
}
CryptoWatcher::CryptoWatcher(QObject *parent)
{
this->setParent(parent);
this->fd = inotify_init();
if (this->fd < 0) {
qDebug() << "Error for inotify";
exit(0);
}
}
CryptoWatcher::~CryptoWatcher()
{
this->appRunning = false;
close(this->fd);
}
void CryptoWatcher::run()
{
struct pollfd pfd = {this->fd, POLLIN, 0};
while (this->appRunning) {
while (this->watchMode) {
char buffer[BUF_LEN];
int i = 0;
int ret = poll(&pfd, 1, 500);
if (ret < 0) {
qDebug() << "Poll fail";
return;
} else if (ret == 0) {
continue; // no new events
} else {
int length = read(this->fd, buffer, BUF_LEN);
if (length < 0) {
qDebug() << "Read fail";
return;
}
while (i < length) {
struct inotify_event *event = (struct inotify_event *) &buffer[i];
this->dispatchSignal(event);
i += EVENT_SIZE + event->len;
}
}
}
}
}
void CryptoWatcher::setInputDir(const QString &inDir)
{
this->inputDirPath = inDir;
QList<QString> inFiles(this->getAllFromFolder(this->inputDirPath));
for(int i = 0; i < inFiles.length(); i++) {
emit this->newFile(inFiles[i]);
}
}
void CryptoWatcher::setOutputDir(const QString &outDir)
{
this->outputDirPath = outDir;
}
QString CryptoWatcher::getInputDir()
{
return this->inputDirPath;
}
QString CryptoWatcher::getOutputDir()
{
return this->outputDirPath;
}
void CryptoWatcher::watchModeChange(bool watchMode)
{
///TODO autoWatch must be disabled while no input dir is made
this->watchMode = watchMode;
if (this->watchMode) {
this->wd = inotify_add_watch(this->fd, this->inputDirPath.toLatin1(),
IN_MODIFY | IN_CREATE | IN_DELETE);
} else {
inotify_rm_watch(this->fd, this->wd);
}
}