-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscannerservice.cpp
More file actions
291 lines (243 loc) · 8 KB
/
scannerservice.cpp
File metadata and controls
291 lines (243 loc) · 8 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
/**
* This file is part of dirage2.
* 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 3 of the License, or
* (at your option) any later version.
*/
#include <optional>
#include <stack>
#include <boost/smart_ptr/intrusive_ptr.hpp>
#include <boost/pool/object_pool.hpp>
#include "scannerservice.h"
// POSIX.
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
// Path element used by the scanning routine. Only supposed to be allocated via the pool.
struct _El;
typedef boost::intrusive_ptr<_El> _ElPtr;
typedef boost::object_pool<_El> _ElPool;
struct _El
{
Q_DISABLE_COPY_MOVE(_El)
_El() = default;
std::string name;
_ElPtr parent;
_ElPool* pool;
int refs = 0;
friend void intrusive_ptr_add_ref(_El* e);
friend void intrusive_ptr_release(_El *e);
};
void intrusive_ptr_add_ref(_El *e)
{ ++e->refs; }
void intrusive_ptr_release(_El *e)
{ if (--e->refs == 0) e->pool->destroy(e); }
// Shared data structure of the scanning state.
struct ScannerService::State::Private
{
Q_DISABLE_COPY_MOVE(Private)
ScannerService::Progress progress;
QPromise<DirTree*> promise;
QPromise<void> track;
QAtomicInt mutex = 0; // Only need to avoid a single read/write race.
_ElPool elPool; // One allocator per scan.
Private()
{ }
void lock()
{ while (!mutex.testAndSetAcquire(0, 1)) { }; }
void unlock()
{ mutex.storeRelease(0); }
void incrFiles()
{ lock(); ++progress.numFiles; unlock(); }
void incrDirs()
{ lock(); ++progress.numDirs; unlock(); }
void incrSkipped()
{ lock(); ++progress.numSkipped; unlock(); }
void incrErrors()
{ lock(); ++progress.numErrors; unlock(); }
_ElPtr allocElement(std::string &&name, _ElPtr parent)
{
_El *e = elPool.construct();
e->pool = &elPool;
e->name = std::move(name);
e->parent = parent;
return e;
}
};
ScannerService::State::State():
p{new Private}
{ }
QFuture<DirTree*> ScannerService::State::future() const
{
return p->promise.future();
}
ScannerService::Progress ScannerService::State::get() const
{
return p->progress;
}
// Worker QRunnable that does that actual scan.
class ScanWorker: public QRunnable
{
public:
ScanWorker(QString path, QSharedPointer<ScannerService::State::Private> state):
m_state{state}, m_rootPath{path}
{ m_state->promise.start(); }
virtual void run()
{
auto &promise = m_state->promise;
auto &track = m_state->track;
DirTree *r = nullptr;
try {
r = scanPriv();
if (r != nullptr) {
promise.addResult(r);
promise.finish();
track.finish();
return;
}
else {
qDebug() << "scanPriv() returned null";
promise.addResult(static_cast<DirTree*>(nullptr));
promise.finish();
track.finish();
return;
}
}
catch (const std::exception &e) {
qWarning() << "ScanWorker::run(): error while scanning" << m_rootPath << ":"
<< e.what();
promise.setException(std::make_exception_ptr(e));
track.finish();
return;
}
catch (...) {
qWarning() << "ScanWorker::run(): unknown error while scanning" << m_rootPath;
promise.setException(std::make_exception_ptr(std::exception()));
track.finish();
return;
}
}
private:
static void fullPath(_ElPtr el, std::string &buf, size_t totLen = 0)
{
static const char sep[] = {QDir::separator().toLatin1(), '\0'};
if (el->parent == nullptr) {
buf.reserve(totLen + el->name.size() + 1);
buf.clear();
buf.append(el->name);
buf.append(sep);
}
else {
fullPath(el->parent, buf, totLen + el->name.size() + 1);
buf.append(el->name);
buf.append(sep);
}
}
DirTree *scanPriv()
{
std::unique_ptr<DirTree> root = std::make_unique<DirTree>();
root->name(m_rootPath);
std::stack<DirTree*> stack;
stack.push(root.get());
// Keeping a parallel string to skip encoding/decoding from QString.
std::stack<_ElPtr> nameStack;
nameStack.push(m_state->allocElement(m_rootPath.toStdString(), nullptr));
while (!stack.empty()) {
if (m_state->promise.isCanceled())
return nullptr;
DirTree *top = stack.top();
stack.pop();
_ElPtr topName = nameStack.top();
nameStack.pop();
std::string nameBuffer;
// Reserve and copy the full path to a buffer.
// The buffer's capacity never shrinks to avoid reallocs.
fullPath(topName, nameBuffer, 0);
DIR *dir = opendir(nameBuffer.c_str());
if (dir == nullptr) {
m_state->incrErrors();
continue;
}
struct dirent *ent;
size_t nameBufferLength = nameBuffer.size();
// Breadth-first: read all children at this level before proceeding.
while ((ent = readdir(dir)) != nullptr) {
// Trim the path to parent's length.
nameBuffer.resize(nameBufferLength);
if (m_state->promise.isCanceled())
return nullptr;
if (strncmp(ent->d_name, ".", sizeof(ent->d_name)) == 0 ||
strncmp(ent->d_name, "..", sizeof(ent->d_name)) == 0)
continue;
// Append name of current child and stat().
nameBuffer.append(ent->d_name);
struct stat st;
if (lstat(nameBuffer.c_str(), &st) == -1) {
m_state->incrErrors();
continue;
}
if (S_ISDIR(st.st_mode)) {
m_state->incrDirs();
DirTree *p = new DirTree();
p->name(QString(ent->d_name));
top->append(p);
stack.push(p);
nameStack.push(m_state->allocElement(ent->d_name, topName));
}
else if (S_ISREG(st.st_mode)) {
m_state->incrFiles();
top->append(st.st_size, st.st_mtime);
}
else {
// Symlinks and all other types are skipped.
m_state->incrSkipped();
}
}
// Sort files at this level by timestamp and close the descriptor.
top->finalize();
closedir(dir);
}
return root.release();
}
QString m_rootPath;
QSharedPointer<ScannerService::State::Private> m_state;
};
struct ScannerService::Private
{
std::optional<ScannerService::State> currentScan;
};
ScannerService::ScannerService():
p(new Private())
{
}
ScannerService::~ScannerService()
{
delete p;
}
bool ScannerService::isScanning() const
{
return p->currentScan.has_value();
}
ScannerService::State ScannerService::start(QString dir)
{
cancel();
State state;
ScanWorker *task = new ScanWorker(dir, state.p);
p->currentScan = state;
auto fut = state.p->track.future();
auto reset = [this]() { p->currentScan.reset(); };
fut.then(this, reset).onCanceled(this, reset);
task->setAutoDelete(true);
QThreadPool::globalInstance()->start(task);
return state;
}
void ScannerService::cancel()
{
if (p->currentScan.has_value()) {
State &state = p->currentScan.value();
auto fut = state.future();
fut.cancel();
fut.waitForFinished();
}
}