2018-10-01 10:26:24 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 Felix Geyer <debfx@fobos.de>
|
|
|
|
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
|
|
|
|
*
|
|
|
|
* 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 or (at your option)
|
|
|
|
* version 3 of the License.
|
|
|
|
*
|
|
|
|
* 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "FileWatcher.h"
|
|
|
|
|
2019-11-09 08:02:34 -05:00
|
|
|
#include "core/AsyncTask.h"
|
2018-10-01 10:26:24 -04:00
|
|
|
#include "core/Clock.h"
|
2019-11-09 08:02:34 -05:00
|
|
|
|
2019-10-14 09:31:23 -04:00
|
|
|
#include <QCryptographicHash>
|
2018-10-01 10:26:24 -04:00
|
|
|
#include <QFileInfo>
|
|
|
|
|
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
#include <sys/vfs.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2019-10-14 09:31:23 -04:00
|
|
|
const int FileChangeDelay = 200;
|
2019-01-20 09:50:20 -05:00
|
|
|
} // namespace
|
2018-10-01 10:26:24 -04:00
|
|
|
|
2019-10-14 09:31:23 -04:00
|
|
|
FileWatcher::FileWatcher(QObject* parent)
|
2018-10-01 10:26:24 -04:00
|
|
|
: QObject(parent)
|
|
|
|
{
|
2019-12-12 22:40:17 -05:00
|
|
|
connect(&m_fileWatcher, SIGNAL(fileChanged(QString)), SLOT(checkFileChanged()));
|
|
|
|
connect(&m_fileChecksumTimer, SIGNAL(timeout()), SLOT(checkFileChanged()));
|
2019-10-14 09:31:23 -04:00
|
|
|
connect(&m_fileChangeDelayTimer, SIGNAL(timeout()), SIGNAL(fileChanged()));
|
2018-10-01 10:26:24 -04:00
|
|
|
m_fileChangeDelayTimer.setSingleShot(true);
|
2019-10-14 09:31:23 -04:00
|
|
|
m_fileIgnoreDelayTimer.setSingleShot(true);
|
2018-10-01 10:26:24 -04:00
|
|
|
}
|
|
|
|
|
2020-03-05 22:59:07 -05:00
|
|
|
FileWatcher::~FileWatcher()
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
2019-10-30 06:40:56 -04:00
|
|
|
void FileWatcher::start(const QString& filePath, int checksumIntervalSeconds, int checksumSizeKibibytes)
|
2018-10-01 10:26:24 -04:00
|
|
|
{
|
2019-10-14 09:31:23 -04:00
|
|
|
stop();
|
2018-10-01 10:26:24 -04:00
|
|
|
|
|
|
|
#if defined(Q_OS_LINUX)
|
|
|
|
struct statfs statfsBuf;
|
|
|
|
bool forcePolling = false;
|
|
|
|
const auto NFS_SUPER_MAGIC = 0x6969;
|
|
|
|
|
|
|
|
if (!statfs(filePath.toLocal8Bit().constData(), &statfsBuf)) {
|
|
|
|
forcePolling = (statfsBuf.f_type == NFS_SUPER_MAGIC);
|
|
|
|
} else {
|
|
|
|
// if we can't get the fs type let's fall back to polling
|
|
|
|
forcePolling = true;
|
|
|
|
}
|
|
|
|
auto objectName = forcePolling ? QLatin1String("_qt_autotest_force_engine_poller") : QLatin1String("");
|
|
|
|
m_fileWatcher.setObjectName(objectName);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
m_fileWatcher.addPath(filePath);
|
2019-10-14 09:31:23 -04:00
|
|
|
m_filePath = filePath;
|
2019-10-30 06:40:56 -04:00
|
|
|
|
|
|
|
// Handle file checksum
|
|
|
|
m_fileChecksumSizeBytes = checksumSizeKibibytes * 1024;
|
2019-10-14 09:31:23 -04:00
|
|
|
m_fileChecksum = calculateChecksum();
|
2019-10-30 06:40:56 -04:00
|
|
|
if (checksumIntervalSeconds > 0) {
|
|
|
|
m_fileChecksumTimer.start(checksumIntervalSeconds * 1000);
|
|
|
|
}
|
|
|
|
|
2019-10-14 09:31:23 -04:00
|
|
|
m_ignoreFileChange = false;
|
|
|
|
}
|
2018-10-01 10:26:24 -04:00
|
|
|
|
2019-10-14 09:31:23 -04:00
|
|
|
void FileWatcher::stop()
|
|
|
|
{
|
|
|
|
if (!m_filePath.isEmpty()) {
|
|
|
|
m_fileWatcher.removePath(m_filePath);
|
2018-10-01 10:26:24 -04:00
|
|
|
}
|
2019-10-14 09:31:23 -04:00
|
|
|
m_filePath.clear();
|
|
|
|
m_fileChecksum.clear();
|
|
|
|
m_fileChangeDelayTimer.stop();
|
2018-10-01 10:26:24 -04:00
|
|
|
}
|
|
|
|
|
2019-10-14 09:31:23 -04:00
|
|
|
void FileWatcher::pause()
|
2018-10-01 10:26:24 -04:00
|
|
|
{
|
|
|
|
m_ignoreFileChange = true;
|
|
|
|
m_fileChangeDelayTimer.stop();
|
|
|
|
}
|
|
|
|
|
2019-10-14 09:31:23 -04:00
|
|
|
void FileWatcher::resume()
|
2018-10-01 10:26:24 -04:00
|
|
|
{
|
2019-10-14 09:31:23 -04:00
|
|
|
m_ignoreFileChange = false;
|
|
|
|
// Add a short delay to start in the next event loop
|
|
|
|
if (!m_fileIgnoreDelayTimer.isActive()) {
|
|
|
|
m_fileIgnoreDelayTimer.start(0);
|
2018-10-01 10:26:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-14 09:31:23 -04:00
|
|
|
bool FileWatcher::shouldIgnoreChanges()
|
|
|
|
{
|
|
|
|
return m_filePath.isEmpty() || m_ignoreFileChange || m_fileIgnoreDelayTimer.isActive()
|
|
|
|
|| m_fileChangeDelayTimer.isActive();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileWatcher::hasSameFileChecksum()
|
|
|
|
{
|
|
|
|
return calculateChecksum() == m_fileChecksum;
|
|
|
|
}
|
|
|
|
|
2019-12-12 22:40:17 -05:00
|
|
|
void FileWatcher::checkFileChanged()
|
2019-10-14 09:31:23 -04:00
|
|
|
{
|
|
|
|
if (shouldIgnoreChanges()) {
|
2018-10-01 10:26:24 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-12 22:40:17 -05:00
|
|
|
// Prevent reentrance
|
|
|
|
m_ignoreFileChange = true;
|
|
|
|
|
2020-03-05 22:59:07 -05:00
|
|
|
auto checksum = AsyncTask::runAndWaitForFuture([this]() -> QByteArray { return calculateChecksum(); });
|
2019-12-12 22:40:17 -05:00
|
|
|
if (checksum != m_fileChecksum) {
|
|
|
|
m_fileChecksum = checksum;
|
|
|
|
m_fileChangeDelayTimer.start(0);
|
2019-10-14 09:31:23 -04:00
|
|
|
}
|
2019-12-12 22:40:17 -05:00
|
|
|
|
|
|
|
m_ignoreFileChange = false;
|
2019-10-14 09:31:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray FileWatcher::calculateChecksum()
|
|
|
|
{
|
2020-03-05 22:59:07 -05:00
|
|
|
QFile file(m_filePath);
|
|
|
|
if (file.open(QFile::ReadOnly)) {
|
|
|
|
QCryptographicHash hash(QCryptographicHash::Sha256);
|
|
|
|
if (m_fileChecksumSizeBytes > 0) {
|
|
|
|
hash.addData(file.read(m_fileChecksumSizeBytes));
|
|
|
|
} else {
|
|
|
|
hash.addData(&file);
|
2019-10-14 09:31:23 -04:00
|
|
|
}
|
2020-03-05 22:59:07 -05:00
|
|
|
return hash.result();
|
|
|
|
}
|
|
|
|
// If we fail to open the file return the last known checksum, this
|
|
|
|
// prevents unnecessary merge requests on intermittent network shares
|
|
|
|
return m_fileChecksum;
|
2018-10-01 10:26:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
BulkFileWatcher::BulkFileWatcher(QObject* parent)
|
|
|
|
: QObject(parent)
|
|
|
|
{
|
|
|
|
connect(&m_fileWatcher, SIGNAL(fileChanged(QString)), SLOT(handleFileChanged(QString)));
|
|
|
|
connect(&m_fileWatcher, SIGNAL(directoryChanged(QString)), SLOT(handleDirectoryChanged(QString)));
|
2019-04-12 13:59:50 -04:00
|
|
|
connect(&m_watchedFilesIgnoreTimer, SIGNAL(timeout()), this, SLOT(observeFileChanges()));
|
2019-01-09 10:25:35 -05:00
|
|
|
connect(&m_pendingSignalsTimer, SIGNAL(timeout()), this, SLOT(emitSignals()));
|
2019-04-12 13:59:50 -04:00
|
|
|
m_watchedFilesIgnoreTimer.setSingleShot(true);
|
2019-02-22 15:46:01 -05:00
|
|
|
m_pendingSignalsTimer.setSingleShot(true);
|
2018-10-01 10:26:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void BulkFileWatcher::clear()
|
|
|
|
{
|
|
|
|
for (const QString& path : m_fileWatcher.files() + m_fileWatcher.directories()) {
|
|
|
|
const QFileInfo info(path);
|
|
|
|
m_fileWatcher.removePath(info.absoluteFilePath());
|
|
|
|
m_fileWatcher.removePath(info.absolutePath());
|
|
|
|
}
|
2019-01-09 10:25:35 -05:00
|
|
|
m_watchedPaths.clear();
|
2018-10-01 10:26:24 -04:00
|
|
|
m_watchedFilesInDirectory.clear();
|
2019-04-12 13:59:50 -04:00
|
|
|
m_watchedFilesIgnored.clear();
|
2018-10-01 10:26:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void BulkFileWatcher::removePath(const QString& path)
|
|
|
|
{
|
|
|
|
const QFileInfo info(path);
|
2019-01-09 10:25:35 -05:00
|
|
|
const QString filePath = info.absoluteFilePath();
|
|
|
|
const QString directoryPath = info.absolutePath();
|
|
|
|
m_watchedFilesInDirectory[directoryPath].remove(filePath);
|
|
|
|
m_fileWatcher.removePath(filePath);
|
|
|
|
m_watchedPaths.remove(filePath);
|
|
|
|
if (m_watchedFilesInDirectory[directoryPath].isEmpty()) {
|
|
|
|
m_fileWatcher.removePath(directoryPath);
|
|
|
|
m_watchedPaths.remove(directoryPath);
|
|
|
|
m_watchedFilesInDirectory.remove(directoryPath);
|
|
|
|
}
|
2018-10-01 10:26:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void BulkFileWatcher::addPath(const QString& path)
|
|
|
|
{
|
|
|
|
const QFileInfo info(path);
|
2019-01-09 10:25:35 -05:00
|
|
|
const QString filePath = info.absoluteFilePath();
|
|
|
|
const QString directoryPath = info.absolutePath();
|
|
|
|
if (!m_watchedPaths.value(filePath)) {
|
|
|
|
const bool fileSuccess = m_fileWatcher.addPath(filePath);
|
|
|
|
m_watchedPaths[filePath] = fileSuccess;
|
|
|
|
}
|
|
|
|
if (!m_watchedPaths.value(directoryPath)) {
|
|
|
|
const bool directorySuccess = m_fileWatcher.addPath(directoryPath);
|
|
|
|
m_watchedPaths[directoryPath] = directorySuccess;
|
|
|
|
}
|
2019-04-12 13:59:50 -04:00
|
|
|
m_watchedFilesInDirectory[directoryPath][filePath] = info.exists() ? info.lastModified().toMSecsSinceEpoch() : 0;
|
2018-10-01 10:26:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void BulkFileWatcher::handleFileChanged(const QString& path)
|
|
|
|
{
|
2019-01-09 10:25:35 -05:00
|
|
|
const QFileInfo info(path);
|
|
|
|
const QString filePath = info.absoluteFilePath();
|
|
|
|
const QString directoryPath = info.absolutePath();
|
2019-04-12 13:59:50 -04:00
|
|
|
const QMap<QString, qint64>& watchedFiles = m_watchedFilesInDirectory[directoryPath];
|
|
|
|
const qint64 lastModificationTime = info.lastModified().toMSecsSinceEpoch();
|
|
|
|
const bool created = watchedFiles[filePath] == 0 && info.exists();
|
|
|
|
const bool deleted = watchedFiles[filePath] != 0 && !info.exists();
|
|
|
|
const bool changed = !created && !deleted && lastModificationTime != watchedFiles[filePath];
|
|
|
|
|
2018-10-01 10:26:24 -04:00
|
|
|
addPath(path);
|
|
|
|
|
2019-04-12 13:59:50 -04:00
|
|
|
if (m_watchedFilesIgnored[info.canonicalFilePath()] > Clock::currentDateTimeUtc()) {
|
2018-10-01 10:26:24 -04:00
|
|
|
// changes are blocked
|
|
|
|
return;
|
|
|
|
}
|
2019-01-09 10:25:35 -05:00
|
|
|
if (created) {
|
|
|
|
qDebug("File created %s", qPrintable(path));
|
|
|
|
scheduleSignal(Created, filePath);
|
|
|
|
}
|
|
|
|
if (changed) {
|
|
|
|
qDebug("File changed %s", qPrintable(path));
|
|
|
|
scheduleSignal(Updated, filePath);
|
|
|
|
}
|
|
|
|
if (deleted) {
|
|
|
|
qDebug("File removed %s", qPrintable(path));
|
|
|
|
scheduleSignal(Removed, filePath);
|
|
|
|
}
|
2018-10-01 10:26:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void BulkFileWatcher::handleDirectoryChanged(const QString& path)
|
|
|
|
{
|
|
|
|
qDebug("Directory changed %s", qPrintable(path));
|
2019-01-03 11:50:36 -05:00
|
|
|
const QFileInfo directoryInfo(path);
|
2019-01-09 10:25:35 -05:00
|
|
|
const QString directoryPath = directoryInfo.absoluteFilePath();
|
2019-04-12 13:59:50 -04:00
|
|
|
QMap<QString, qint64>& watchedFiles = m_watchedFilesInDirectory[directoryPath];
|
2019-01-03 11:50:36 -05:00
|
|
|
for (const QString& filename : watchedFiles.keys()) {
|
|
|
|
const QFileInfo fileInfo(filename);
|
2019-01-09 10:25:35 -05:00
|
|
|
const QString filePath = fileInfo.absoluteFilePath();
|
2019-04-12 13:59:50 -04:00
|
|
|
const qint64 previousModificationTime = watchedFiles[filePath];
|
|
|
|
const qint64 lastModificationTime = fileInfo.lastModified().toMSecsSinceEpoch();
|
|
|
|
if (!fileInfo.exists() && previousModificationTime != 0) {
|
|
|
|
qDebug("Remove watch file %s", qPrintable(fileInfo.absoluteFilePath()));
|
2019-01-09 10:25:35 -05:00
|
|
|
m_fileWatcher.removePath(filePath);
|
|
|
|
m_watchedPaths.remove(filePath);
|
|
|
|
watchedFiles.remove(filePath);
|
|
|
|
scheduleSignal(Removed, filePath);
|
2018-10-01 10:26:24 -04:00
|
|
|
}
|
2019-04-12 13:59:50 -04:00
|
|
|
if (previousModificationTime == 0 && fileInfo.exists()) {
|
|
|
|
qDebug("Add watch file %s", qPrintable(fileInfo.absoluteFilePath()));
|
2019-01-09 10:25:35 -05:00
|
|
|
if (!m_watchedPaths.value(filePath)) {
|
|
|
|
const bool success = m_fileWatcher.addPath(filePath);
|
|
|
|
m_watchedPaths[filePath] = success;
|
2019-04-12 13:59:50 -04:00
|
|
|
watchedFiles[filePath] = lastModificationTime;
|
2019-01-09 10:25:35 -05:00
|
|
|
}
|
|
|
|
scheduleSignal(Created, filePath);
|
2018-10-01 10:26:24 -04:00
|
|
|
}
|
2019-04-12 13:59:50 -04:00
|
|
|
if (fileInfo.exists() && previousModificationTime != lastModificationTime) {
|
2019-01-09 10:25:35 -05:00
|
|
|
// this case is handled using
|
2019-01-03 11:50:36 -05:00
|
|
|
qDebug("Refresh watch file %s", qPrintable(fileInfo.absoluteFilePath()));
|
|
|
|
m_fileWatcher.removePath(fileInfo.absolutePath());
|
|
|
|
m_fileWatcher.addPath(fileInfo.absolutePath());
|
2019-01-09 10:25:35 -05:00
|
|
|
scheduleSignal(Updated, filePath);
|
|
|
|
}
|
2019-04-12 13:59:50 -04:00
|
|
|
m_watchedFilesInDirectory[directoryPath][filePath] = fileInfo.exists() ? lastModificationTime : 0;
|
2019-01-09 10:25:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BulkFileWatcher::emitSignals()
|
|
|
|
{
|
|
|
|
QMap<QString, QList<Signal>> queued;
|
|
|
|
m_pendingSignals.swap(queued);
|
2019-01-20 09:50:20 -05:00
|
|
|
for (const auto& path : queued.keys()) {
|
|
|
|
const auto& signal = queued[path];
|
2019-01-09 10:25:35 -05:00
|
|
|
if (signal.last() == Removed) {
|
2019-04-12 13:59:50 -04:00
|
|
|
qDebug("Emit %s removed", qPrintable(path));
|
2019-01-09 10:25:35 -05:00
|
|
|
emit fileRemoved(path);
|
|
|
|
continue;
|
2018-10-01 10:26:24 -04:00
|
|
|
}
|
2019-01-20 09:50:20 -05:00
|
|
|
if (signal.first() == Created) {
|
2019-04-12 13:59:50 -04:00
|
|
|
qDebug("Emit %s created", qPrintable(path));
|
2019-01-09 10:25:35 -05:00
|
|
|
emit fileCreated(path);
|
|
|
|
continue;
|
|
|
|
}
|
2019-04-12 13:59:50 -04:00
|
|
|
qDebug("Emit %s changed", qPrintable(path));
|
2019-01-09 10:25:35 -05:00
|
|
|
emit fileChanged(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-20 09:50:20 -05:00
|
|
|
void BulkFileWatcher::scheduleSignal(Signal signal, const QString& path)
|
2019-01-09 10:25:35 -05:00
|
|
|
{
|
|
|
|
// we need to collect signals since the file watcher API may send multiple signals for a "single" change
|
|
|
|
// therefore we wait until the event loop finished before starting to import any changes
|
|
|
|
const QString filePath = QFileInfo(path).absoluteFilePath();
|
|
|
|
m_pendingSignals[filePath] << signal;
|
|
|
|
|
|
|
|
if (!m_pendingSignalsTimer.isActive()) {
|
|
|
|
m_pendingSignalsTimer.start();
|
2018-10-01 10:26:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BulkFileWatcher::ignoreFileChanges(const QString& path)
|
|
|
|
{
|
|
|
|
const QFileInfo info(path);
|
2019-04-12 13:59:50 -04:00
|
|
|
m_watchedFilesIgnored[info.canonicalFilePath()] = Clock::currentDateTimeUtc().addMSecs(FileChangeDelay);
|
2018-10-01 10:26:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void BulkFileWatcher::observeFileChanges(bool delayed)
|
|
|
|
{
|
|
|
|
int timeout = 0;
|
|
|
|
if (delayed) {
|
2019-10-14 09:31:23 -04:00
|
|
|
timeout = FileChangeDelay;
|
2018-10-01 10:26:24 -04:00
|
|
|
} else {
|
|
|
|
const QDateTime current = Clock::currentDateTimeUtc();
|
2019-04-12 13:59:50 -04:00
|
|
|
for (const QString& key : m_watchedFilesIgnored.keys()) {
|
|
|
|
if (m_watchedFilesIgnored[key] < current) {
|
2018-10-01 10:26:24 -04:00
|
|
|
// We assume that there was no concurrent change of the database
|
|
|
|
// during our block - so no need to reimport
|
|
|
|
qDebug("Remove block from %s", qPrintable(key));
|
2019-04-12 13:59:50 -04:00
|
|
|
m_watchedFilesIgnored.remove(key);
|
2018-10-01 10:26:24 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
qDebug("Keep block from %s", qPrintable(key));
|
2019-04-12 13:59:50 -04:00
|
|
|
timeout = qMin(timeout, static_cast<int>(current.msecsTo(m_watchedFilesIgnored[key])));
|
2018-10-01 10:26:24 -04:00
|
|
|
}
|
|
|
|
}
|
2019-04-12 13:59:50 -04:00
|
|
|
if (timeout > 0 && !m_watchedFilesIgnoreTimer.isActive()) {
|
|
|
|
m_watchedFilesIgnoreTimer.start(timeout);
|
2018-10-01 10:26:24 -04:00
|
|
|
}
|
|
|
|
}
|