Directory Notification Revisited

Today I had to implement a service that used directory notification to watch for changes in a directory and then handle each change by migrating it to another location. I went back to my previous post about directory notification and used some of the code there to implement what I needed. Unfortunately, the code in that post for processing the returned data is not quite right. It skips that last entry in the returned buffer, which in practice means it skips most of the entries, since it is most common for there to be a single entry in the buffer.

Following is a code segment that processes the entries correctly.

PFILE_NOTIFY_INFORMATION pNotifyInformation = NULL;
do
{
	//	Move to the next (or first element in the buffer)
	pNotifyInformation = pNotifyInformation
		? (PFILE_NOTIFY_INFORMATION) Add2Ptr( pNotifyInformation, pNotifyInformation->NextEntryOffset )
		: pNotifyBuffer;

	std::wstring filename( pNotifyInformation->FileName, pNotifyInformation->FileNameLength / sizeof(wchar_t) );
	switch ( pNotifyInformation->Action )
	{
	case FILE_ACTION_ADDED:
	case FILE_ACTION_MODIFIED:
	case FILE_ACTION_RENAMED_NEW_NAME:
	case FILE_ACTION_REMOVED:
	case FILE_ACTION_RENAMED_OLD_NAME:
		wprintf( L"Change detected for file: %s", filename.c_str() );
		break;
	}
}
while ( pNotifyInformation->NextEntryOffset != 0 );

Leave a Reply

Your email address will not be published. Required fields are marked *

Complete the following to verify your humanity: * Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.