2010-01-01 34 views
0

時,這是所謂的JNotify一個開源項目的一部分。我試圖修復Win32實現,這真的讓我瘋狂。我已經閱讀了MSDN中關於此的所有內容,並閱讀了每篇關於這個糟糕的API的文章。 我正嘗試使用ReadDirectoryChangesW在窗口上使用完成端口接收文件系統通知。崩潰使用ReadDirectoryChangesW

我看到的行爲是,通常它的工作原理,但有些時候緩衝區我收到的時候則GetQueuedCompletionStatus返回以奇怪的方式被破壞。 eitehr FILE_NOTIFY_INFORMATION.NextEntryOffset指向自身(導致無限循環),或者其他出錯,並且我收到僞造文件名稱長度。 這隻發生,如果我重新觀看目錄,從來沒有在第一個事件(但重新觀看的需要,否則你只能得到該目錄的一個事件)。

的測試代碼崩潰每一件事情是微不足道的,它只是看多顯示目錄和每個目錄下創建兩個文件。

這裏是一些相關的代碼,我可以添加所有的,如果你想(整個事情是不是太大),但感覺太大了這裏的問題。

這段代碼創建完成端口,它只能運行一次 - 然後我用這個完成端口的所有目錄。

_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(); 
    } 
} 

這是運行的代碼(握奇數據對象中),當我開始觀看目錄:

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; 
} 

回答

2

我很確定NextEntryOffset是相對於當前記錄而不是第一條記錄。

... 
char* current = buffer; 
do 
{ 
    event = (FILE_NOTIFY_INFORMATION*)current; 
    ... 
    i = event->NextEntryOffset; 
    current += i; 
} 
while (i != 0); 
... 
+0

太棒了,看起來像是這個問題。 一件小事:請修正您的代碼:while行應改爲while(event-> NextEntryOffset!= 0); – 2010-01-01 22:43:17

+0

不要怪我,我只是使用你原來的代碼。 – Luke 2010-01-01 22:52:14

+0

不是怪你。在NextEntryOffset是絕對偏移的錯誤假設下,原始代碼是正確的。通過修復你剛剛介紹的一個新的小錯誤。 – 2010-01-05 12:49:07