-
Notifications
You must be signed in to change notification settings - Fork 941
Expand file tree
/
Copy pathfolderwatcher.cpp
More file actions
293 lines (245 loc) · 8.49 KB
/
folderwatcher.cpp
File metadata and controls
293 lines (245 loc) · 8.49 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
/*
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2014 ownCloud GmbH
* SPDX-License-Identifier: GPL-2.0-or-later
*/
// event masks
#include "folderwatcher.h"
#include "accountstate.h"
#include "account.h"
#include "capabilities.h"
#if defined(Q_OS_WIN)
#include "folderwatcher_win.h"
#elif defined(Q_OS_MACOS)
#include "folderwatcher_mac.h"
#elif defined(Q_OS_UNIX)
#include "folderwatcher_linux.h"
#endif
#include "folder.h"
#include "filesystem.h"
#include <QFileInfo>
#include <QFlags>
#include <QDir>
#include <QMutexLocker>
#include <QStringList>
#include <array>
#include <cstdint>
namespace
{
constexpr auto lockChangeDebouncingTimerIntervalMs = 500;
}
namespace OCC {
Q_LOGGING_CATEGORY(lcFolderWatcher, "nextcloud.gui.folderwatcher", QtInfoMsg)
FolderWatcher::FolderWatcher(Folder *folder)
: QObject(folder)
, _folder(folder)
{
_lockChangeDebouncingTimer.setInterval(lockChangeDebouncingTimerIntervalMs);
if (_folder && _folder->accountState() && _folder->accountState()->account()) {
connect(_folder->accountState()->account().data(), &Account::capabilitiesChanged, this, &FolderWatcher::folderAccountCapabilitiesChanged);
folderAccountCapabilitiesChanged();
}
}
FolderWatcher::~FolderWatcher() = default;
void FolderWatcher::init(const QString &root)
{
_d.reset(new FolderWatcherPrivate(this, root));
_timer.start();
}
bool FolderWatcher::pathIsIgnored(const QString &path) const
{
return path.isEmpty();
}
bool FolderWatcher::isReliable() const
{
return _isReliable;
}
bool FolderWatcher::canSetPermissions() const
{
return _canSetPermissions;
}
void FolderWatcher::appendSubPaths(QDir dir, QStringList &subPaths)
{
QStringList newSubPaths = dir.entryList(QDir::NoDotAndDotDot | QDir::Dirs | QDir::Files);
for (int i = 0; i < newSubPaths.size(); i++) {
QString path = dir.path() + "/" + newSubPaths[i];
QFileInfo fileInfo(path);
subPaths.append(path);
if (FileSystem::isDir(path)) {
QDir dir(path);
appendSubPaths(dir, subPaths);
}
}
}
void FolderWatcher::startNotificatonTest(const QString &path)
{
#ifdef Q_OS_MACOS
// Testing the folder watcher on OSX is harder because the watcher
// automatically discards changes that were performed by our process.
// It would still be useful to test but the OSX implementation
// is deferred until later.
return;
#endif
Q_ASSERT(_testNotificationPath.isEmpty());
_testNotificationPath = path;
// Don't do the local file modification immediately:
// wait for FolderWatchPrivate::_ready
startNotificationTestWhenReady();
}
void FolderWatcher::performSetPermissionsTest(const QString &path)
{
_canSetPermissions = true;
if (!QFile::exists(path)) {
QFile f(path);
if (!f.open(QIODevice::WriteOnly)) {
qCWarning(lcFolderWatcher()) << "Failed to create test file: " << path;
return;
}
f.write("test");
f.close();
}
if (!FileSystem::isWritable(path)) {
FileSystem::setFileReadOnly(path, false);
if (!FileSystem::isWritable(path)) {
qCWarning(lcFolderWatcher()) << "Cannot make file readable: " << path;
_canSetPermissions = false;
QFile(path).remove();
return;
}
}
FileSystem::setFileReadOnly(path, true);
if (FileSystem::isWritable(path)) {
qCWarning(lcFolderWatcher()) << "Cannot make file read-only: " << path;
_canSetPermissions = false;
}
qCInfo(lcFolderWatcher()) << "Permissions in file system for" << path << (_canSetPermissions ? "works as expected" : "are not reliable");
FileSystem::setFileReadOnly(path, false);
QFile(path).remove();
}
void FolderWatcher::startNotificationTestWhenReady()
{
if (!_d->_ready) {
QTimer::singleShot(1000, this, &FolderWatcher::startNotificationTestWhenReady);
return;
}
auto path = _testNotificationPath;
if (QFile::exists(path)) {
auto mtime = FileSystem::getModTime(path);
qCDebug(lcFolderWatcher()) << "setModTime" << path << (mtime + 1);
FileSystem::setModTime(path, mtime + 1);
} else {
QFile f(path);
if (!f.open(QIODevice::WriteOnly | QIODevice::Append)) {
qCWarning(lcFolderWatcher()) << "Failed to open test notification file for writing:" << path << f.errorString();
}
}
FileSystem::setFileHidden(path, true);
QTimer::singleShot(5000, this, [this]() {
if (!_testNotificationPath.isEmpty())
emit becameUnreliable(tr("The watcher did not receive a test notification."));
_testNotificationPath.clear();
});
}
void FolderWatcher::lockChangeDebouncingTimerTimedOut()
{
if (!_unlockedFiles.isEmpty()) {
const auto unlockedFilesCopy = _unlockedFiles;
emit filesLockReleased(unlockedFilesCopy);
_unlockedFiles.clear();
}
if (!_lockedFiles.isEmpty()) {
const auto lockedFilesCopy = _lockedFiles;
emit filesLockImposed(lockedFilesCopy);
emit lockedFilesFound(lockedFilesCopy);
_lockedFiles.clear();
}
}
int FolderWatcher::testLinuxWatchCount() const
{
#ifdef Q_OS_LINUX
return _d->testWatchCount();
#else
return -1;
#endif
}
void FolderWatcher::slotLockFileDetectedExternally(const QString &lockFile)
{
qCInfo(lcFolderWatcher) << "Lock file detected externally, probably a newly-uploaded office file: " << lockFile;
changeDetected(lockFile);
}
void FolderWatcher::setShouldWatchForFileUnlocking(bool shouldWatchForFileUnlocking)
{
_shouldWatchForFileUnlocking = shouldWatchForFileUnlocking;
}
int FolderWatcher::lockChangeDebouncingTimout() const
{
return _lockChangeDebouncingTimer.interval();
}
void FolderWatcher::changeDetected(const QString &path)
{
QStringList paths(path);
if (FileSystem::isDir(path)) {
QDir dir(path);
appendSubPaths(dir, paths);
}
changeDetected(paths);
}
void FolderWatcher::changeDetected(const QStringList &paths)
{
// TODO: this shortcut doesn't look very reliable:
// - why is the timeout only 1 second?
// - what if there is more than one file being updated frequently?
// - why do we skip the file altogether instead of e.g. reducing the upload frequency?
// Check if the same path was reported within the last second.
const auto pathsSet = QSet<QString>{paths.begin(), paths.end()};
if (pathsSet == _lastPaths && _timer.elapsed() < 1000) {
// the same path was reported within the last second. Skip.
return;
}
_lastPaths = pathsSet;
_timer.restart();
QSet<QString> changedPaths;
for (const auto &path : paths) {
if (!_testNotificationPath.isEmpty()
&& Utility::fileNamesEqual(path, _testNotificationPath)) {
_testNotificationPath.clear();
}
const auto lockFileNamePattern = FileSystem::filePathLockFilePatternMatch(path);
const auto checkResult = FileSystem::lockFileTargetFilePath(path, lockFileNamePattern);
if (_shouldWatchForFileUnlocking) {
// Lock file has been deleted, file now unlocked
if (checkResult.type == FileSystem::FileLockingInfo::Type::Unlocked && !checkResult.path.isEmpty()) {
_lockedFiles.remove(checkResult.path);
_unlockedFiles.insert(checkResult.path);
}
}
if (checkResult.type == FileSystem::FileLockingInfo::Type::Locked && !checkResult.path.isEmpty()) {
_unlockedFiles.remove(checkResult.path);
_lockedFiles.insert(checkResult.path);
}
// ------- handle ignores:
if (pathIsIgnored(path)) {
continue;
}
changedPaths.insert(path);
}
if (!_lockedFiles.isEmpty() || !_unlockedFiles.isEmpty()) {
if (_lockChangeDebouncingTimer.isActive()) {
_lockChangeDebouncingTimer.stop();
}
_lockChangeDebouncingTimer.setSingleShot(true);
_lockChangeDebouncingTimer.start();
_lockChangeDebouncingTimer.connect(&_lockChangeDebouncingTimer, &QTimer::timeout, this, &FolderWatcher::lockChangeDebouncingTimerTimedOut, Qt::UniqueConnection);
}
if (changedPaths.isEmpty()) {
return;
}
for (const auto &path : changedPaths) {
emit pathChanged(path);
}
}
void FolderWatcher::folderAccountCapabilitiesChanged()
{
_shouldWatchForFileUnlocking = _folder->accountState()->account()->capabilities().filesLockAvailable();
}
} // namespace OCC