2012-12-04 21:15:37 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Daniel Molkentin <danimo@owncloud.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
|
2014-01-13 19:16:19 +04:00
|
|
|
* the Free Software Foundation; version 2 of the License.
|
2012-12-04 21:15:37 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2012-12-05 21:23:35 +04:00
|
|
|
#include <QThread>
|
|
|
|
#include <QDebug>
|
2014-11-06 02:36:04 +03:00
|
|
|
#include <QDir>
|
2012-12-05 21:23:35 +04:00
|
|
|
|
2014-07-11 02:31:24 +04:00
|
|
|
#include "folderwatcher.h"
|
|
|
|
#include "folderwatcher_win.h"
|
2012-12-05 21:23:35 +04:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <tchar.h>
|
2012-12-04 21:15:37 +04:00
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
namespace OCC {
|
2012-12-04 21:15:37 +04:00
|
|
|
|
2014-11-06 02:36:04 +03:00
|
|
|
void WatcherThread::watchChanges(size_t fileNotifyBufferSize,
|
|
|
|
bool* increaseBufferSize)
|
2012-12-05 21:23:35 +04:00
|
|
|
{
|
2014-11-06 02:36:04 +03:00
|
|
|
*increaseBufferSize = false;
|
|
|
|
|
|
|
|
_handle = CreateFileW(
|
|
|
|
(wchar_t*)_path.utf16(),
|
|
|
|
FILE_LIST_DIRECTORY,
|
|
|
|
FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
|
|
|
|
NULL,
|
|
|
|
OPEN_EXISTING,
|
|
|
|
FILE_FLAG_BACKUP_SEMANTICS,
|
|
|
|
NULL
|
|
|
|
);
|
2012-12-04 21:15:37 +04:00
|
|
|
|
2012-12-06 19:26:27 +04:00
|
|
|
if (_handle == INVALID_HANDLE_VALUE)
|
2012-12-05 21:23:35 +04:00
|
|
|
{
|
2014-11-06 02:36:04 +03:00
|
|
|
DWORD errorCode = GetLastError();
|
|
|
|
qDebug() << Q_FUNC_INFO << "Failed to create handle for" << _path << ", error:" << errorCode;
|
2012-12-06 19:26:27 +04:00
|
|
|
_handle = 0;
|
2012-12-05 21:23:35 +04:00
|
|
|
return;
|
|
|
|
}
|
2012-12-04 21:15:37 +04:00
|
|
|
|
2014-11-06 02:36:04 +03:00
|
|
|
// QVarLengthArray ensures the stack-buffer is aligned like double and qint64.
|
|
|
|
QVarLengthArray<char, 4096*10> fileNotifyBuffer;
|
|
|
|
fileNotifyBuffer.resize(fileNotifyBufferSize);
|
|
|
|
|
|
|
|
const size_t fileNameBufferSize = 4096;
|
|
|
|
TCHAR fileNameBuffer[fileNameBufferSize];
|
|
|
|
|
|
|
|
forever {
|
|
|
|
FILE_NOTIFY_INFORMATION *pFileNotifyBuffer =
|
|
|
|
(FILE_NOTIFY_INFORMATION*)fileNotifyBuffer.data();
|
|
|
|
DWORD dwBytesReturned = 0;
|
|
|
|
SecureZeroMemory(pFileNotifyBuffer, fileNotifyBufferSize);
|
|
|
|
if(ReadDirectoryChangesW( _handle, (LPVOID)pFileNotifyBuffer,
|
|
|
|
fileNotifyBufferSize, true,
|
|
|
|
FILE_NOTIFY_CHANGE_FILE_NAME |
|
|
|
|
FILE_NOTIFY_CHANGE_DIR_NAME |
|
|
|
|
FILE_NOTIFY_CHANGE_LAST_WRITE,
|
|
|
|
&dwBytesReturned, NULL, NULL))
|
|
|
|
{
|
|
|
|
FILE_NOTIFY_INFORMATION *curEntry = pFileNotifyBuffer;
|
|
|
|
forever {
|
|
|
|
size_t len = curEntry->FileNameLength / 2;
|
|
|
|
QString file = _path + "\\" + QString::fromWCharArray(curEntry->FileName, len);
|
|
|
|
|
|
|
|
// Unless the file was removed or renamed, get its full long name
|
|
|
|
// TODO: We could still try expanding the path in the tricky cases...
|
|
|
|
QString longfile = file;
|
|
|
|
if (curEntry->Action != FILE_ACTION_REMOVED
|
|
|
|
&& curEntry->Action != FILE_ACTION_RENAMED_OLD_NAME) {
|
|
|
|
size_t longNameSize = GetLongPathNameW(reinterpret_cast<LPCWSTR>(file.utf16()), fileNameBuffer, fileNameBufferSize);
|
|
|
|
if (longNameSize > 0) {
|
|
|
|
longfile = QString::fromUtf16(reinterpret_cast<const ushort *>(fileNameBuffer), longNameSize);
|
|
|
|
} else {
|
|
|
|
qDebug() << Q_FUNC_INFO << "Error converting file name to full length, keeping original name.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
longfile = QDir::cleanPath(longfile);
|
|
|
|
|
|
|
|
qDebug() << Q_FUNC_INFO << "Found change in" << longfile << "action:" << curEntry->Action;
|
|
|
|
emit changed(longfile);
|
2012-12-05 21:23:35 +04:00
|
|
|
|
2014-11-06 02:36:04 +03:00
|
|
|
if (curEntry->NextEntryOffset == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
curEntry = (FILE_NOTIFY_INFORMATION*)(
|
|
|
|
(char*)curEntry + curEntry->NextEntryOffset);
|
2012-12-06 19:26:27 +04:00
|
|
|
}
|
2014-11-06 02:36:04 +03:00
|
|
|
} else {
|
|
|
|
DWORD errorCode = GetLastError();
|
|
|
|
switch(errorCode) {
|
|
|
|
case ERROR_NOTIFY_ENUM_DIR:
|
|
|
|
qDebug() << Q_FUNC_INFO << "The buffer for changes overflowed! Triggering a generic change and resizing";
|
|
|
|
emit changed(_path);
|
|
|
|
*increaseBufferSize = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
qDebug() << Q_FUNC_INFO << "General error" << errorCode << "while watching. Exiting.";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
CloseHandle(_handle);
|
|
|
|
_handle = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WatcherThread::run()
|
|
|
|
{
|
|
|
|
// If this buffer fills up before we've extracted its data we will lose
|
|
|
|
// change information. Therefore start big.
|
|
|
|
size_t bufferSize = 4096*10;
|
|
|
|
size_t maxBuffer = 64*1024;
|
|
|
|
|
|
|
|
forever {
|
|
|
|
bool increaseBufferSize = false;
|
|
|
|
watchChanges(bufferSize, &increaseBufferSize);
|
|
|
|
|
|
|
|
if (increaseBufferSize) {
|
|
|
|
bufferSize = qMin(bufferSize*2, maxBuffer);
|
|
|
|
} else {
|
|
|
|
// Other errors shouldn't actually happen,
|
|
|
|
// so sleep a bit to avoid running into the same error case in a
|
|
|
|
// tight loop.
|
|
|
|
sleep(2);
|
2012-12-05 21:23:35 +04:00
|
|
|
}
|
|
|
|
}
|
2012-12-04 21:15:37 +04:00
|
|
|
}
|
2012-12-06 19:26:27 +04:00
|
|
|
|
|
|
|
WatcherThread::~WatcherThread()
|
|
|
|
{
|
2014-11-06 02:36:04 +03:00
|
|
|
if (_handle) {
|
|
|
|
CloseHandle(_handle);
|
|
|
|
_handle = NULL;
|
|
|
|
}
|
2012-12-06 19:26:27 +04:00
|
|
|
}
|
2012-12-05 21:23:35 +04:00
|
|
|
|
2014-01-13 18:23:52 +04:00
|
|
|
FolderWatcherPrivate::FolderWatcherPrivate(FolderWatcher *p, const QString& path)
|
2012-12-05 21:23:35 +04:00
|
|
|
: _parent(p)
|
|
|
|
{
|
2014-01-13 18:23:52 +04:00
|
|
|
_thread = new WatcherThread(path);
|
2012-12-06 19:26:27 +04:00
|
|
|
connect(_thread, SIGNAL(changed(const QString&)),
|
|
|
|
_parent,SLOT(changeDetected(const QString&)));
|
|
|
|
_thread->start();
|
2012-12-05 21:23:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
FolderWatcherPrivate::~FolderWatcherPrivate()
|
|
|
|
{
|
2012-12-06 19:26:27 +04:00
|
|
|
_thread->terminate();
|
|
|
|
_thread->wait();
|
2012-12-05 21:23:35 +04:00
|
|
|
delete _thread;
|
|
|
|
}
|
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
} // namespace OCC
|