Internal new background directory scanning for Windows; see issue #597

This commit is contained in:
Oliver Duis
2011-03-25 22:17:16 +01:00
parent 0f416b379a
commit adc5518d52
8 changed files with 32 additions and 33 deletions

View File

@@ -19,17 +19,19 @@
#include <windirmonitor.h>
static void CALLBACK current_directory_monitor_callback (DWORD error, DWORD nBytes, LPOVERLAPPED lpOverlapped) {
// printf ("hivas: %d %d %d\n", error, nBytes, lpOverlapped);
WinDirMonitor::MonitorData* monData = (WinDirMonitor::MonitorData*)lpOverlapped;
if (!nBytes) {
delete monData;
return;
}
if (monData->listener)
// ReadDirectoryChangesW sometimes emits multiple events per change (one for each change type)
// To make sure it's not flooding update, this gets filtered.
time_t curTime= ::time(NULL);
if (monData->listener && ::difftime(curTime, monData->lastTimeUpdateDir)>1.0) {
monData->listener->winDirChanged ();
monData->lastTimeUpdateDir = curTime;
}
ReadDirectoryChangesW (monData->hDirectory,
monData->file_notify_buffer,
@@ -37,16 +39,14 @@ static void CALLBACK current_directory_monitor_callback (DWORD error, DWORD nByt
FALSE,
FILE_NOTIFY_CHANGE_FILE_NAME |
FILE_NOTIFY_CHANGE_DIR_NAME |
FILE_NOTIFY_CHANGE_ATTRIBUTES |
FILE_NOTIFY_CHANGE_SIZE,
FILE_NOTIFY_CHANGE_LAST_WRITE,
&monData->buffer_filled_bytes,
&monData->overlapped,
current_directory_monitor_callback);
}
WinDirMonitor::WinDirMonitor (Glib::ustring dirName, WinDirChangeListener* listener) : monData(NULL) {
wchar_t* wdirname = (wchar_t*)g_utf8_to_utf16 (dirName.raw().c_str(), -1, NULL, NULL, NULL);
wchar_t* wdirname = (wchar_t*)g_utf8_to_utf16 (dirName.c_str(), -1, NULL, NULL, NULL);
HANDLE hDirectory = CreateFileW (wdirname, FILE_LIST_DIRECTORY,FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, NULL);
g_free (wdirname);
@@ -57,6 +57,7 @@ WinDirMonitor::WinDirMonitor (Glib::ustring dirName, WinDirChangeListener* liste
monData->buffer_allocated_bytes = 32768;
monData->file_notify_buffer = new char [monData->buffer_allocated_bytes];
monData->hDirectory = hDirectory;
monData->lastTimeUpdateDir = ::time(NULL);
// printf ("mondata=%d\n", monData);
ReadDirectoryChangesW (monData->hDirectory,
@@ -65,8 +66,7 @@ WinDirMonitor::WinDirMonitor (Glib::ustring dirName, WinDirChangeListener* liste
FALSE,
FILE_NOTIFY_CHANGE_FILE_NAME |
FILE_NOTIFY_CHANGE_DIR_NAME |
FILE_NOTIFY_CHANGE_ATTRIBUTES |
FILE_NOTIFY_CHANGE_SIZE,
FILE_NOTIFY_CHANGE_LAST_WRITE,
&monData->buffer_filled_bytes,
&monData->overlapped,
current_directory_monitor_callback);