【发布时间】:2010-01-01 20:10:07
【问题描述】:
这是一个名为 JNotify 的开源项目的一部分。我正在尝试修复 Win32 实现,它真的让我发疯。我已经阅读了 MSDN 中有关此内容的所有内容,并阅读了有关此糟糕 API 的所有网络帖子。 我正在尝试使用 ReadDirectoryChangesW 使用完成端口在 Windows 上接收文件系统通知。
我看到的行为是它通常可以工作,但有时我在 GetQueuedCompletionStatus 返回时收到的缓冲区以奇怪的方式损坏。 eitehr FILE_NOTIFY_INFORMATION.NextEntryOffset 指向自身(导致无限循环),或者出现其他问题,我收到一个虚假的文件名长度。 仅当我重新观看目录时才会发生这种情况,从不在第一个事件中发生(但需要重新观看,否则您只会获得该目录的一个事件)。
让每件事都崩溃的测试代码是微不足道的,它只是观察许多目录并在每个目录中创建 2 个文件。
这里有一些相关的代码,如果你愿意我可以全部添加(整个东西不是太大),但是感觉太大了在这里提问。
这段代码创建了完成端口,它只运行一次 - 然后我将这个完成端口用于所有目录。
_completionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 1);
这是 WatchData 构造函数,它实际上打开了目录句柄并将其与完成端口相关联。
WatchData::WatchData(const WCHAR* path, int mask, bool watchSubtree, HANDLE completionPort)
:
_watchId(++_counter),
_mask(mask),
_watchSubtree(watchSubtree),
_byteReturned(0),
_completionPort(completionPort)
{
_path = _wcsdup(path);
_hDir = CreateFileW(_path,
FILE_LIST_DIRECTORY | GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, //security attributes
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, NULL);
if(_hDir == INVALID_HANDLE_VALUE )
{
throw GetLastError();
}
if (NULL == CreateIoCompletionPort(_hDir, _completionPort, (ULONG_PTR)&_watchId, 0))
{
throw GetLastError();
}
}
这是我开始查看目录时运行的代码(在 WatchData 对象内):
int WatchData::watchDirectory()
{
printf("(Re)watching %ls\n", _path);
memset(_buffer, 0, sizeof(_buffer));
memset(&_overLapped, 0, sizeof(_overLapped));
if( !ReadDirectoryChangesW( _hDir,
_buffer,//<--FILE_NOTIFY_INFORMATION records are put into this buffer
sizeof(_buffer),
_watchSubtree,
_mask,
&_byteReturned,
&_overLapped,
NULL))
{
return GetLastError();
}
else
{
return 0;
}
}
这是在它自己的线程中运行的主循环,处理完成事件。 请注意,“这不应该发生”,它实际上发生了很多。
DWORD WINAPI Win32FSHook::mainLoop( LPVOID lpParam )
{
debug("mainLoop starts");
Win32FSHook* _this = (Win32FSHook*)lpParam;
HANDLE hPort = _this->_completionPort;
DWORD dwNoOfBytes = 0;
ULONG_PTR ulKey = 0;
OVERLAPPED* pov = NULL;
WCHAR name[1024];
while (_this->_isRunning)
{
pov = NULL;
BOOL fSuccess = GetQueuedCompletionStatus(
hPort, // Completion port handle
&dwNoOfBytes, // Bytes transferred
&ulKey,
&pov, // OVERLAPPED structure
INFINITE // Notification time-out interval
);
if (fSuccess)
{
if (dwNoOfBytes == 0)
{
// can happen after a watch is removed
continue;
}
int wd = *(int*)ulKey;
EnterCriticalSection(&_this->_cSection);
WatchData *watchData = _this->find(wd);
if (!watchData)
{
log("mainLoop : ignoring event for watch id %d, no longer in wid2WatchData map", wd);
LeaveCriticalSection(&_this->_cSection);
continue;
}
//const char* buffer = watchData->getBuffer();
char buffer[watchData->getBufferSize()];
memcpy(buffer, watchData->getBuffer(), watchData->getBufferSize());
LeaveCriticalSection(&_this->_cSection);
FILE_NOTIFY_INFORMATION *event;
DWORD i=0;
do
{
event = (FILE_NOTIFY_INFORMATION*)(buffer+i);
int action = event->Action;
DWORD len = event->FileNameLength / sizeof(WCHAR);
for (DWORD k=0;k<len && k < (sizeof(name)-sizeof(WCHAR))/sizeof(WCHAR);k++)
{
name[k] = event->FileName[k];
}
name[len] = 0;
_this->_callback(watchData->getId(), action, watchData->getPath(), name);
if (i != 0 && event->NextEntryOffset == i)
{
log("should not happen!");
break;
}
i = event->NextEntryOffset;
}
while (i != 0);
int res = watchData->watchDirectory();
if (res != 0)
{
log("Error watching dir %s : %d",watchData->getPath(), res);
}
}
else
{
log("GetQueuedCompletionStatus returned an error");
}
}
debug("mainLoop exits");
return 0;
}
【问题讨论】: