1.1.0
This commit is contained in:
parent
f75e9137bd
commit
c969ef7ec9
|
@ -0,0 +1,120 @@
|
|||
//
|
||||
// The MIT License
|
||||
//
|
||||
// Copyright (c) 2010 James E Beveridge
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
|
||||
// This sample code is for my blog entry titled, "Understanding ReadDirectoryChangesW"
|
||||
// http://qualapps.blogspot.com/2010/05/understanding-readdirectorychangesw.html
|
||||
// See ReadMe.txt for overview information.
|
||||
|
||||
#include "stdafx.h"
|
||||
#include "ReadDirectoryChanges.h"
|
||||
#include "ReadDirectoryChangesPrivate.h"
|
||||
|
||||
using namespace ReadDirectoryChangesPrivate;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// CReadDirectoryChanges
|
||||
|
||||
CReadDirectoryChanges::CReadDirectoryChanges()// int nMaxCount) : m_Notifications(nMaxCount)
|
||||
{
|
||||
m_hThread = NULL;
|
||||
m_dwThreadId= 0;
|
||||
m_pServer = new CReadChangesServer(this);
|
||||
}
|
||||
|
||||
CReadDirectoryChanges::~CReadDirectoryChanges()
|
||||
{
|
||||
Terminate();
|
||||
delete m_pServer;
|
||||
}
|
||||
|
||||
void CReadDirectoryChanges::Init()
|
||||
{
|
||||
//
|
||||
// Kick off the worker thread, which will be
|
||||
// managed by CReadChangesServer.
|
||||
//
|
||||
m_hThread = (HANDLE)_beginthreadex(NULL,
|
||||
0,
|
||||
CReadChangesServer::ThreadStartProc,
|
||||
m_pServer,
|
||||
0,
|
||||
&m_dwThreadId
|
||||
);
|
||||
}
|
||||
|
||||
void CReadDirectoryChanges::Terminate()
|
||||
{
|
||||
if (m_hThread)
|
||||
{
|
||||
::QueueUserAPC(CReadChangesServer::TerminateProc, m_hThread, (ULONG_PTR)m_pServer);
|
||||
::WaitForSingleObjectEx(m_hThread, 10000, true);
|
||||
::CloseHandle(m_hThread);
|
||||
|
||||
m_hThread = NULL;
|
||||
m_dwThreadId = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void CReadDirectoryChanges::AddDirectory( LPCTSTR szDirectory, BOOL bWatchSubtree, DWORD dwNotifyFilter, DWORD dwBufferSize )
|
||||
{
|
||||
if (!m_hThread)
|
||||
Init();
|
||||
|
||||
CReadChangesRequest* pRequest = new CReadChangesRequest(m_pServer, szDirectory, bWatchSubtree, dwNotifyFilter, dwBufferSize);
|
||||
QueueUserAPC(CReadChangesServer::AddDirectoryProc, m_hThread, (ULONG_PTR)pRequest);
|
||||
}
|
||||
|
||||
void CReadDirectoryChanges::DetachDirectory( LPCTSTR szDirectory )
|
||||
{
|
||||
if (!m_hThread)
|
||||
Init();
|
||||
|
||||
pair<ReadDirectoryChangesPrivate::CReadChangesServer*, wstring>* pRequest = new pair<ReadDirectoryChangesPrivate::CReadChangesServer*, wstring>(m_pServer, szDirectory);
|
||||
QueueUserAPC(CReadChangesServer::DetachDirectoryProc, m_hThread, (ULONG_PTR)pRequest);
|
||||
}
|
||||
|
||||
//void CReadDirectoryChanges::Push(DWORD dwAction, wstring& wstrFilename)
|
||||
//{
|
||||
// m_Notifications.push( TDirectoryChangeNotification(dwAction, wstrFilename) );
|
||||
//}
|
||||
//
|
||||
//bool CReadDirectoryChanges::Pop(DWORD& dwAction, wstring& wstrFilename)
|
||||
//{
|
||||
// TDirectoryChangeNotification pair;
|
||||
// if (!m_Notifications.pop(pair))
|
||||
// return false;
|
||||
//
|
||||
// dwAction = pair.first;
|
||||
// wstrFilename = pair.second;
|
||||
//
|
||||
// return true;
|
||||
//}
|
||||
//
|
||||
//bool CReadDirectoryChanges::CheckOverflow()
|
||||
//{
|
||||
// bool b = m_Notifications.overflow();
|
||||
// if (b)
|
||||
// m_Notifications.clear();
|
||||
// return b;
|
||||
//}
|
|
@ -0,0 +1,148 @@
|
|||
//
|
||||
// The MIT License
|
||||
//
|
||||
// Copyright (c) 2010 James E Beveridge
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
|
||||
// This sample code is for my blog entry titled, "Understanding ReadDirectoryChangesW"
|
||||
// http://qualapps.blogspot.com/2010/05/understanding-readdirectorychangesw.html
|
||||
// See ReadMe.txt for overview information.
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include "ThreadSafeQueue.h"
|
||||
|
||||
typedef pair<DWORD,wstring> TDirectoryChangeNotification;
|
||||
|
||||
namespace ReadDirectoryChangesPrivate
|
||||
{
|
||||
class CReadChangesServer;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Track changes to filesystem directories and report them
|
||||
/// to the caller via a thread-safe queue.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This sample code is based on my blog entry titled, "Understanding ReadDirectoryChangesW"
|
||||
/// http://qualapps.blogspot.com/2010/05/understanding-readdirectorychangesw.html
|
||||
/// </para><para>
|
||||
/// All functions in CReadDirectoryChangesServer run in
|
||||
/// the context of the calling thread.
|
||||
/// </para>
|
||||
/// <example><code>
|
||||
/// CReadDirectoryChanges changes;
|
||||
/// changes.AddDirectory(_T("C:\\"), false, dwNotificationFlags);
|
||||
///
|
||||
/// const HANDLE handles[] = { hStopEvent, changes.GetWaitHandle() };
|
||||
///
|
||||
/// while (!bTerminate)
|
||||
/// {
|
||||
/// ::MsgWaitForMultipleObjectsEx(
|
||||
/// _countof(handles),
|
||||
/// handles,
|
||||
/// INFINITE,
|
||||
/// QS_ALLINPUT,
|
||||
/// MWMO_INPUTAVAILABLE | MWMO_ALERTABLE);
|
||||
/// switch (rc)
|
||||
/// {
|
||||
/// case WAIT_OBJECT_0 + 0:
|
||||
/// bTerminate = true;
|
||||
/// break;
|
||||
/// case WAIT_OBJECT_0 + 1:
|
||||
/// // We've received a notification in the queue.
|
||||
/// {
|
||||
/// DWORD dwAction;
|
||||
/// wstring wstrFilename;
|
||||
/// changes.Pop(dwAction, wstrFilename);
|
||||
/// wprintf(L"%s %s\n", ExplainAction(dwAction), wstrFilename);
|
||||
/// }
|
||||
/// break;
|
||||
/// case WAIT_OBJECT_0 + _countof(handles):
|
||||
/// // Get and dispatch message
|
||||
/// break;
|
||||
/// case WAIT_IO_COMPLETION:
|
||||
/// // APC complete.No action needed.
|
||||
/// break;
|
||||
/// }
|
||||
/// }
|
||||
/// </code></example>
|
||||
/// </remarks>
|
||||
class CReadDirectoryChanges
|
||||
{
|
||||
public:
|
||||
CReadDirectoryChanges();// int nMaxChanges = 1000);
|
||||
~CReadDirectoryChanges();
|
||||
|
||||
void Init();
|
||||
void Terminate();
|
||||
|
||||
/// <summary>
|
||||
/// Add a new directory to be monitored.
|
||||
/// </summary>
|
||||
/// <param name="wszDirectory">Directory to monitor.</param>
|
||||
/// <param name="bWatchSubtree">True to also monitor subdirectories.</param>
|
||||
/// <param name="dwNotifyFilter">The types of file system events to monitor, such as FILE_NOTIFY_CHANGE_ATTRIBUTES.</param>
|
||||
/// <param name="dwBufferSize">The size of the buffer used for overlapped I/O.</param>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This function will make an APC call to the worker thread to issue a new
|
||||
/// ReadDirectoryChangesW call for the given directory with the given flags.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
void AddDirectory( LPCTSTR wszDirectory, BOOL bWatchSubtree, DWORD dwNotifyFilter, DWORD dwBufferSize=16384 );
|
||||
|
||||
void DetachDirectory( LPCTSTR wszDirectory );
|
||||
|
||||
virtual void Notify( const wstring& strDirectory ) {}
|
||||
|
||||
/// <summary>
|
||||
/// Return a handle for the Win32 Wait... functions that will be
|
||||
/// signaled when there is a queue entry.
|
||||
/// </summary>
|
||||
//HANDLE GetWaitHandle() { return m_Notifications.GetWaitHandle(); }
|
||||
//
|
||||
//bool Pop(DWORD& dwAction, wstring& wstrFilename);
|
||||
//
|
||||
//// "Push" is for usage by ReadChangesRequest. Not intended for external usage.
|
||||
//void Push(DWORD dwAction, wstring& wstrFilename);
|
||||
//
|
||||
//// Check if the queue overflowed. If so, clear it and return true.
|
||||
//bool CheckOverflow();
|
||||
|
||||
unsigned int GetThreadId() { return m_dwThreadId; }
|
||||
|
||||
protected:
|
||||
ReadDirectoryChangesPrivate::CReadChangesServer* m_pServer;
|
||||
|
||||
HANDLE m_hThread;
|
||||
|
||||
unsigned int m_dwThreadId;
|
||||
|
||||
//CThreadSafeQueue<TDirectoryChangeNotification> m_Notifications;
|
||||
};
|
|
@ -0,0 +1,183 @@
|
|||
//
|
||||
// The MIT License
|
||||
//
|
||||
// Copyright (c) 2010 James E Beveridge
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
|
||||
// This sample code is for my blog entry titled, "Understanding ReadDirectoryChangesW"
|
||||
// http://qualapps.blogspot.com/2010/05/understanding-readdirectorychangesw.html
|
||||
// See ReadMe.txt for overview information.
|
||||
|
||||
|
||||
#include "stdafx.h"
|
||||
#include "ReadDirectoryChanges.h"
|
||||
#include "ReadDirectoryChangesPrivate.h"
|
||||
#include <shlwapi.h>
|
||||
|
||||
|
||||
// The namespace is a convenience to emphasize that these are internals
|
||||
// interfaces. The namespace can be safely removed if you need to.
|
||||
namespace ReadDirectoryChangesPrivate
|
||||
{
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// CReadChangesRequest
|
||||
|
||||
CReadChangesRequest::CReadChangesRequest(CReadChangesServer* pServer, LPCTSTR sz, BOOL b, DWORD dw, DWORD size)
|
||||
{
|
||||
m_pServer = pServer;
|
||||
m_dwFilterFlags = dw;
|
||||
m_bIncludeChildren = b;
|
||||
m_wstrDirectory = sz;
|
||||
m_hDirectory = 0;
|
||||
|
||||
::ZeroMemory(&m_Overlapped, sizeof(OVERLAPPED));
|
||||
|
||||
// The hEvent member is not used when there is a completion
|
||||
// function, so it's ok to use it to point to the object.
|
||||
m_Overlapped.hEvent = this;
|
||||
|
||||
m_Buffer.resize(size);
|
||||
m_BackupBuffer.resize(size);
|
||||
}
|
||||
|
||||
|
||||
CReadChangesRequest::~CReadChangesRequest()
|
||||
{
|
||||
// RequestTermination() must have been called successfully.
|
||||
_ASSERTE(m_hDirectory == NULL || m_hDirectory == INVALID_HANDLE_VALUE);
|
||||
}
|
||||
|
||||
|
||||
bool CReadChangesRequest::OpenDirectory()
|
||||
{
|
||||
// Allow this routine to be called redundantly.
|
||||
if (m_hDirectory)
|
||||
return true;
|
||||
|
||||
m_hDirectory = ::CreateFileW(
|
||||
m_wstrDirectory.c_str(), // pointer to the file name
|
||||
FILE_LIST_DIRECTORY, // access (read/write) mode
|
||||
FILE_SHARE_READ // share mode
|
||||
| FILE_SHARE_WRITE
|
||||
| FILE_SHARE_DELETE,
|
||||
NULL, // security descriptor
|
||||
OPEN_EXISTING, // how to create
|
||||
FILE_FLAG_BACKUP_SEMANTICS // file attributes
|
||||
| FILE_FLAG_OVERLAPPED,
|
||||
NULL); // file with attributes to copy
|
||||
|
||||
if (m_hDirectory == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void CReadChangesRequest::BeginRead()
|
||||
{
|
||||
DWORD dwBytes=0;
|
||||
|
||||
// This call needs to be reissued after every APC.
|
||||
BOOL success = ::ReadDirectoryChangesW(
|
||||
m_hDirectory, // handle to directory
|
||||
&m_Buffer[0], // read results buffer
|
||||
m_Buffer.size(), // length of buffer
|
||||
m_bIncludeChildren, // monitoring option
|
||||
m_dwFilterFlags, // filter conditions
|
||||
&dwBytes, // bytes returned
|
||||
&m_Overlapped, // overlapped buffer
|
||||
&NotificationCompletion); // completion routine
|
||||
}
|
||||
|
||||
//static
|
||||
VOID CALLBACK CReadChangesRequest::NotificationCompletion(
|
||||
DWORD dwErrorCode, // completion code
|
||||
DWORD dwNumberOfBytesTransfered, // number of bytes transferred
|
||||
LPOVERLAPPED lpOverlapped) // I/O information buffer
|
||||
{
|
||||
CReadChangesRequest* pBlock = (CReadChangesRequest*)lpOverlapped->hEvent;
|
||||
|
||||
if (dwErrorCode == ERROR_OPERATION_ABORTED)
|
||||
{
|
||||
::InterlockedDecrement(&pBlock->m_pServer->m_nOutstandingRequests);
|
||||
delete pBlock;
|
||||
return;
|
||||
}
|
||||
|
||||
// This might mean overflow? Not sure.
|
||||
if(!dwNumberOfBytesTransfered)
|
||||
return;
|
||||
|
||||
// Can't use sizeof(FILE_NOTIFY_INFORMATION) because
|
||||
// the structure is padded to 16 bytes.
|
||||
_ASSERTE(dwNumberOfBytesTransfered >= offsetof(FILE_NOTIFY_INFORMATION, FileName) + sizeof(WCHAR));
|
||||
|
||||
pBlock->BackupBuffer(dwNumberOfBytesTransfered);
|
||||
|
||||
// Get the new read issued as fast as possible. The documentation
|
||||
// says that the original OVERLAPPED structure will not be used
|
||||
// again once the completion routine is called.
|
||||
pBlock->BeginRead();
|
||||
|
||||
//pBlock->ProcessNotification();
|
||||
|
||||
pBlock->m_pServer->m_pBase->Notify(pBlock->GetDirectory());
|
||||
}
|
||||
|
||||
void CReadChangesRequest::ProcessNotification()
|
||||
{
|
||||
BYTE* pBase = m_BackupBuffer.data();
|
||||
|
||||
for (;;)
|
||||
{
|
||||
FILE_NOTIFY_INFORMATION& fni = (FILE_NOTIFY_INFORMATION&)*pBase;
|
||||
|
||||
wstring wstrFilename(fni.FileName, fni.FileNameLength/sizeof(wchar_t));
|
||||
// Handle a trailing backslash, such as for a root directory.
|
||||
if (m_wstrDirectory.back() != L'\\')
|
||||
wstrFilename = m_wstrDirectory + L"\\" + wstrFilename;
|
||||
else
|
||||
wstrFilename = m_wstrDirectory + wstrFilename;
|
||||
|
||||
// If it could be a short filename, expand it.
|
||||
LPCWSTR wszFilename = PathFindFileNameW(wstrFilename.c_str());
|
||||
int len = lstrlenW(wszFilename);
|
||||
// The maximum length of an 8.3 filename is twelve, including the dot.
|
||||
if (len <= 12 && wcschr(wszFilename, L'~'))
|
||||
{
|
||||
// Convert to the long filename form. Unfortunately, this
|
||||
// does not work for deletions, so it's an imperfect fix.
|
||||
wchar_t wbuf[MAX_PATH];
|
||||
if (::GetLongPathNameW(wstrFilename.c_str(), wbuf, _countof(wbuf)) > 0)
|
||||
wstrFilename = wbuf;
|
||||
}
|
||||
|
||||
//m_pServer->m_pBase->Push(fni.Action, wstrFilename);
|
||||
|
||||
if (!fni.NextEntryOffset)
|
||||
break;
|
||||
pBase += fni.NextEntryOffset;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,212 @@
|
|||
//
|
||||
// The MIT License
|
||||
//
|
||||
// Copyright (c) 2010 James E Beveridge
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
|
||||
// This sample code is for my blog entry titled, "Understanding ReadDirectoryChangesW"
|
||||
// http://qualapps.blogspot.com/2010/05/understanding-readdirectorychangesw.html
|
||||
// See ReadMe.txt for overview information.
|
||||
|
||||
class CReadDirectoryChanges;
|
||||
|
||||
namespace ReadDirectoryChangesPrivate
|
||||
{
|
||||
|
||||
class CReadChangesServer;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// All functions in CReadChangesRequest run in the context of the worker thread.
|
||||
// One instance of this object is created for each call to AddDirectory().
|
||||
class CReadChangesRequest
|
||||
{
|
||||
public:
|
||||
CReadChangesRequest(CReadChangesServer* pServer, LPCTSTR sz, BOOL b, DWORD dw, DWORD size);
|
||||
|
||||
~CReadChangesRequest();
|
||||
|
||||
bool OpenDirectory();
|
||||
|
||||
void BeginRead();
|
||||
|
||||
// The dwSize is the actual number of bytes sent to the APC.
|
||||
void BackupBuffer(DWORD dwSize)
|
||||
{
|
||||
// We could just swap back and forth between the two
|
||||
// buffers, but this code is easier to understand and debug.
|
||||
memcpy(&m_BackupBuffer[0], &m_Buffer[0], dwSize);
|
||||
}
|
||||
|
||||
void ProcessNotification();
|
||||
|
||||
void RequestTermination()
|
||||
{
|
||||
// ::CancelIoEx(m_hDirectory, &m_Overlapped);
|
||||
::CancelIo(m_hDirectory);
|
||||
::CloseHandle(m_hDirectory);
|
||||
m_hDirectory = nullptr;
|
||||
}
|
||||
|
||||
const wstring& GetDirectory() { return m_wstrDirectory; }
|
||||
|
||||
CReadChangesServer* m_pServer;
|
||||
|
||||
protected:
|
||||
|
||||
static VOID CALLBACK NotificationCompletion(
|
||||
DWORD dwErrorCode, // completion code
|
||||
DWORD dwNumberOfBytesTransfered, // number of bytes transferred
|
||||
LPOVERLAPPED lpOverlapped); // I/O information buffer
|
||||
|
||||
// Parameters from the caller for ReadDirectoryChangesW().
|
||||
DWORD m_dwFilterFlags;
|
||||
BOOL m_bIncludeChildren;
|
||||
wstring m_wstrDirectory;
|
||||
|
||||
// Result of calling CreateFile().
|
||||
HANDLE m_hDirectory;
|
||||
|
||||
// Required parameter for ReadDirectoryChangesW().
|
||||
OVERLAPPED m_Overlapped;
|
||||
|
||||
// Data buffer for the request.
|
||||
// Since the memory is allocated by malloc, it will always
|
||||
// be aligned as required by ReadDirectoryChangesW().
|
||||
vector<BYTE> m_Buffer;
|
||||
|
||||
// Double buffer strategy so that we can issue a new read
|
||||
// request before we process the current buffer.
|
||||
vector<BYTE> m_BackupBuffer;
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// All functions in CReadChangesServer run in the context of the worker thread.
|
||||
// One instance of this object is allocated for each instance of CReadDirectoryChanges.
|
||||
// This class is responsible for thread startup, orderly thread shutdown, and shimming
|
||||
// the various C++ member functions with C-style Win32 functions.
|
||||
class CReadChangesServer
|
||||
{
|
||||
public:
|
||||
CReadChangesServer(CReadDirectoryChanges* pParent)
|
||||
{
|
||||
m_bTerminate=false; m_nOutstandingRequests=0;m_pBase=pParent;
|
||||
}
|
||||
|
||||
static unsigned int WINAPI ThreadStartProc(LPVOID arg)
|
||||
{
|
||||
CReadChangesServer* pServer = (CReadChangesServer*)arg;
|
||||
pServer->Run();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Called by QueueUserAPC to start orderly shutdown.
|
||||
static void CALLBACK TerminateProc(__in ULONG_PTR arg)
|
||||
{
|
||||
CReadChangesServer* pServer = (CReadChangesServer*)arg;
|
||||
pServer->RequestTermination();
|
||||
}
|
||||
|
||||
// Called by QueueUserAPC to add another directory.
|
||||
static void CALLBACK AddDirectoryProc(__in ULONG_PTR arg)
|
||||
{
|
||||
CReadChangesRequest* pRequest = (CReadChangesRequest*)arg;
|
||||
pRequest->m_pServer->AddDirectory(pRequest);
|
||||
}
|
||||
|
||||
static void CALLBACK DetachDirectoryProc(__in ULONG_PTR arg)
|
||||
{
|
||||
pair<ReadDirectoryChangesPrivate::CReadChangesServer*, wstring>* pRequest = (pair<ReadDirectoryChangesPrivate::CReadChangesServer*, wstring>*)arg;
|
||||
pRequest->first->DetachDirectory(pRequest);
|
||||
}
|
||||
|
||||
CReadDirectoryChanges* m_pBase;
|
||||
|
||||
volatile DWORD m_nOutstandingRequests;
|
||||
|
||||
protected:
|
||||
|
||||
void Run()
|
||||
{
|
||||
while (m_nOutstandingRequests || !m_bTerminate)
|
||||
{
|
||||
DWORD rc = ::SleepEx(INFINITE, true);
|
||||
}
|
||||
}
|
||||
|
||||
void AddDirectory( CReadChangesRequest* pBlock )
|
||||
{
|
||||
for (auto I = m_pBlocks.begin(); I != m_pBlocks.end(); ++I)
|
||||
{
|
||||
if ((*I)->GetDirectory() == pBlock->GetDirectory())
|
||||
{
|
||||
delete pBlock;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (pBlock->OpenDirectory())
|
||||
{
|
||||
::InterlockedIncrement(&pBlock->m_pServer->m_nOutstandingRequests);
|
||||
m_pBlocks.push_back(pBlock);
|
||||
pBlock->BeginRead();
|
||||
}
|
||||
else
|
||||
delete pBlock;
|
||||
}
|
||||
|
||||
void DetachDirectory(pair<ReadDirectoryChangesPrivate::CReadChangesServer*, wstring>* pRequest)
|
||||
{
|
||||
for (auto I = m_pBlocks.begin(); I != m_pBlocks.end(); )
|
||||
{
|
||||
if ((*I)->GetDirectory() == pRequest->second)
|
||||
{
|
||||
::InterlockedDecrement(&pRequest->first->m_nOutstandingRequests);
|
||||
(*I)->RequestTermination();
|
||||
delete (*I);
|
||||
I = m_pBlocks.erase(I);
|
||||
}
|
||||
else
|
||||
++I;
|
||||
}
|
||||
delete pRequest;
|
||||
}
|
||||
|
||||
void RequestTermination()
|
||||
{
|
||||
m_bTerminate = true;
|
||||
|
||||
for (DWORD i=0; i<m_pBlocks.size(); ++i)
|
||||
{
|
||||
// Each Request object will delete itself.
|
||||
m_pBlocks[i]->RequestTermination();
|
||||
}
|
||||
|
||||
m_pBlocks.clear();
|
||||
}
|
||||
|
||||
vector<CReadChangesRequest*> m_pBlocks;
|
||||
|
||||
bool m_bTerminate;
|
||||
};
|
||||
|
||||
}
|
|
@ -0,0 +1,122 @@
|
|||
//
|
||||
// The MIT License
|
||||
//
|
||||
// Copyright (c) 2010 James E Beveridge
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
|
||||
// This sample code is for my blog entry titled, "Understanding ReadDirectoryChangesW"
|
||||
// http://qualapps.blogspot.com/2010/05/understanding-readdirectorychangesw.html
|
||||
// See ReadMe.txt for overview information.
|
||||
|
||||
#include <list>
|
||||
|
||||
template <typename C>
|
||||
class CThreadSafeQueue : protected std::list<C>
|
||||
{
|
||||
public:
|
||||
CThreadSafeQueue(int nMaxCount)
|
||||
{
|
||||
m_bOverflow = false;
|
||||
|
||||
m_hSemaphore = ::CreateSemaphore(
|
||||
NULL, // no security attributes
|
||||
0, // initial count
|
||||
nMaxCount, // max count
|
||||
NULL); // anonymous
|
||||
}
|
||||
|
||||
~CThreadSafeQueue()
|
||||
{
|
||||
::CloseHandle(m_hSemaphore);
|
||||
m_hSemaphore = NULL;
|
||||
}
|
||||
|
||||
void push(C& c)
|
||||
{
|
||||
//CComCritSecLock<CComAutoCriticalSection> lock( m_Crit, true );
|
||||
QMutexLocker lock(&m_Mutex);
|
||||
push_back( c );
|
||||
//lock.Unlock();
|
||||
lock.unlock();
|
||||
|
||||
if (!::ReleaseSemaphore(m_hSemaphore, 1, NULL))
|
||||
{
|
||||
// If the semaphore is full, then take back the entry.
|
||||
//lock.Lock();
|
||||
lock.relock();
|
||||
pop_back();
|
||||
if (GetLastError() == ERROR_TOO_MANY_POSTS)
|
||||
{
|
||||
m_bOverflow = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool pop(C& c)
|
||||
{
|
||||
//CComCritSecLock<CComAutoCriticalSection> lock( m_Crit, true );
|
||||
QMutexLocker lock(&m_Mutex);
|
||||
|
||||
// If the user calls pop() more than once after the
|
||||
// semaphore is signaled, then the semaphore count will
|
||||
// get out of sync. We fix that when the queue empties.
|
||||
if (empty())
|
||||
{
|
||||
while (::WaitForSingleObject(m_hSemaphore, 0) != WAIT_TIMEOUT)
|
||||
1;
|
||||
return false;
|
||||
}
|
||||
|
||||
c = front();
|
||||
pop_front();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// If overflow, use this to clear the queue.
|
||||
void clear()
|
||||
{
|
||||
//CComCritSecLock<CComAutoCriticalSection> lock( m_Crit, true );
|
||||
QMutexLocker lock(&m_Mutex);
|
||||
|
||||
for (DWORD i=0; i<size(); i++)
|
||||
WaitForSingleObject(m_hSemaphore, 0);
|
||||
|
||||
__super::clear();
|
||||
|
||||
m_bOverflow = false;
|
||||
}
|
||||
|
||||
bool overflow()
|
||||
{
|
||||
return m_bOverflow;
|
||||
}
|
||||
|
||||
HANDLE GetWaitHandle() { return m_hSemaphore; }
|
||||
|
||||
protected:
|
||||
HANDLE m_hSemaphore;
|
||||
|
||||
//CComAutoCriticalSection m_Crit;
|
||||
QMutex m_Mutex;
|
||||
|
||||
bool m_bOverflow;
|
||||
};
|
|
@ -17,6 +17,8 @@ HEADERS += ./stdafx.h \
|
|||
./Dialogs/MultiErrorDialog.h \
|
||||
./Helpers/FindTool.h \
|
||||
./Helpers/WinAdmin.h \
|
||||
./Helpers/ReadDirectoryChanges.h \
|
||||
./Helpers/ReadDirectoryChangesPrivate.h \
|
||||
./Windows/NewBoxWindow.h \
|
||||
./Windows/RecoveryWindow.h \
|
||||
./Windows/PopUpWindow.h \
|
||||
|
@ -42,6 +44,8 @@ SOURCES += ./main.cpp \
|
|||
./Dialogs/MultiErrorDialog.cpp \
|
||||
./Helpers/FindTool.cpp \
|
||||
./Helpers/WinAdmin.cpp \
|
||||
./Helpers/ReadDirectoryChanges.cpp \
|
||||
./Helpers/ReadDirectoryChangesPrivate.cpp \
|
||||
./Helpers/WindowFromPointEx.cpp \
|
||||
./Windows/NewBoxWindow.cpp \
|
||||
./Windows/OptionsWindow.cpp \
|
||||
|
|
Loading…
Reference in New Issue