BOOL CCheckFileNotificationsThread::CreateHandlesOld() { ASSERT(m_pEventHandles==NULL); ASSERT(m_lState==sInitializing); FnDebugMessage("FN: creating handles (old method)"); CLocateDlg* pLocateDlg=GetLocateDlg(); ASSERT(pLocateDlg!=NULL); // Loads roods from databases so that we know what to listen CArrayFAP<LPWSTR> aRoots; const CArray<PDATABASE>& aAllDatabases=GetLocateApp()->GetDatabases(); CArray<PDATABASE> aUsedDatabases; for (int i=0;i<aAllDatabases.GetSize();i++) { if (pLocateDlg->IsDatabaseUsedInSearch(aAllDatabases[i]->GetID())) aUsedDatabases.Add(aAllDatabases[i]); } if (aUsedDatabases.GetSize()==0) return FALSE; CDatabaseInfo::GetRootsFromDatabases(aRoots,aUsedDatabases); if (aRoots.GetSize()==0) return FALSE; // Create arrays for event handles and data structures // // The first handle in m_pEventHandles is stop event, the rest are change notification // objects returned by FindFirstChangeNotification function. // The first pointer in m_pRoots is NULL, the rest are pointers // to root directory. The lists are terminated with NULL // Allocating arraysn, note that the size of the list is not // necessary aRoots.GetSize()+2 if FindFirstChangeNotification returns error m_pEventHandles=new HANDLE[aRoots.GetSize()+1]; ASSERT(m_pEventHandles!=NULL); m_pRoots=new LPWSTR[aRoots.GetSize()+1]; ASSERT(m_pRoots!=NULL); // First handle in event handles array is stop handle and // first pointer to root directory is NULL, // so that each element in m_pEventHandles (with index >0) // corresponds to element in m_pRoots array with the same index m_pEventHandles[0]=m_hStopEvent; m_pRoots[0]=NULL; // Creating handles for directories in aRoots array using FindFirstChangeNotification m_nHandles=1; for (int i=0;i<aRoots.GetSize();i++) { if (m_lFlags&fwStopWhenPossible) { // Notify to Stop() that we are going to stop what // we are doing SetEvent(m_hStopEvent); InterlockedExchange(&m_lState,sStopping); break; } CStringW sRoot=aRoots.GetAt(i); // If root of the type "X:", change it to "X:\" if (sRoot[1]==':' && sRoot[2]=='\0') sRoot << L'\\'; #ifdef _DEBUG_LOGGING // If logging is on, do not use change notifications for root containing log file LPCSTR pLogFile=GetDebugLoggingFile(); if (pLogFile!=NULL) { // No debug logging for drive containing hfcdebug.log char* szPath=alloccopyWtoA(sRoot); MakeLower(szPath); BOOL bSame=strncmp(szPath,pLogFile,sRoot.GetLength())==0; delete[] szPath; if (bSame) continue; } #endif // Create find change notification objects if (IsUnicodeSystem()) m_pEventHandles[m_nHandles]=FindFirstChangeNotificationW(sRoot,TRUE, FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_DIR_NAME|FILE_NOTIFY_CHANGE_SIZE|FILE_NOTIFY_CHANGE_LAST_WRITE); else m_pEventHandles[m_nHandles]=FindFirstChangeNotification(W2A(sRoot),TRUE, FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_DIR_NAME|FILE_NOTIFY_CHANGE_SIZE|FILE_NOTIFY_CHANGE_LAST_WRITE); if (m_pEventHandles[m_nHandles]==INVALID_HANDLE_VALUE) { // FindFirstChangeNotification returned error, skipping this directory continue; } DebugOpenEvent(m_pEventHandles[m_nHandles]); sRoot.MakeLower(); sRoot.FreeExtra(); m_pRoots[m_nHandles]=sRoot.GiveBuffer(); m_nHandles++; } FnDebugMessage("FN handles created"); return TRUE; }
BOOL CShortcut::GetDefaultShortcuts(CArrayFP<CShortcut*>& aShortcuts,BYTE bLoadFlag) { // This code saves currently saved shortcuts to correct file // Uncommend and run once /* // BEGIN { LPCSTR szFile="C:\\Users\\jmhuttun\\Programming\\projects\\Locate\\Locate32\\commonres\\defaultshortcuts.dat"; CRegKey2 RegKey; if (RegKey.OpenKey(HKCU,"\\General",CRegKey::defRead)!=ERROR_SUCCESS) return FALSE; DWORD dwDataLength=RegKey.QueryValueLength("Shortcuts"); if (dwDataLength<4) return FALSE; BYTE* pData=new BYTE[dwDataLength]; RegKey.QueryValue("Shortcuts",(LPSTR)pData,dwDataLength,NULL); RegKey.CloseKey(); CFile File(TRUE); File.CloseOnDelete(); try { File.Open(szFile,CFile::defWrite); File.Write(pData,dwDataLength); File.Close(); } catch (...) { } } // END */ // Check file CStringW Path(GetLocateApp()->GetExeNameW()); Path.FreeExtra(Path.FindLast(L'\\')+1); Path << L"defshrtc.dat"; BYTE* pData=NULL; DWORD dwLength; try { CFile File(Path,CFile::defRead|CFile::otherErrorWhenEOF,TRUE); File.CloseOnDelete(); dwLength=File.GetLength(); pData=new BYTE[dwLength]; File.Read(pData,dwLength); File.Close(); BOOL bRet=LoadShortcuts(pData,dwLength,aShortcuts,bLoadFlag); delete[] pData; if (bRet) return TRUE; } catch (...) { if (pData!=NULL) delete[] pData; } HRSRC hRsrc=FindResource(GetInstanceHandle(),MAKEINTRESOURCE(IDR_DEFAULTSHORTCUTS),"DATA"); if (hRsrc==NULL) return FALSE; dwLength=SizeofResource(GetInstanceHandle(),hRsrc); if (dwLength<4) return FALSE; HGLOBAL hGlobal=LoadResource(GetInstanceHandle(),hRsrc); if (hGlobal==NULL) return FALSE; return LoadShortcuts((const BYTE*)LockResource(hGlobal),dwLength,aShortcuts,bLoadFlag); }
BOOL CCheckFileNotificationsThread::CreateHandlesNew() { ASSERT(m_pEventHandles==NULL); ASSERT(m_lState==sInitializing); FnDebugMessage("FN: creating handles"); CLocateDlg* pLocateDlg=GetLocateDlg(); ASSERT(pLocateDlg!=NULL); // Loads roods from databases so that we know what to listen CArrayFAP<LPWSTR> aRoots; const CArray<PDATABASE>& aAllDatabases=GetLocateApp()->GetDatabases(); CArray<PDATABASE> aUsedDatabases; for (int i=0;i<aAllDatabases.GetSize();i++) { if (pLocateDlg->IsDatabaseUsedInSearch(aAllDatabases[i]->GetID())) aUsedDatabases.Add(aAllDatabases[i]); } if (aUsedDatabases.GetSize()==0) return FALSE; CDatabaseInfo::GetRootsFromDatabases(aRoots,aUsedDatabases); if (aRoots.GetSize()==0) return FALSE; // Create arrays for event handles and data structures // // The first handle in m_pEventHandles is stop event, the rest of // handles are events which are used in overlay structure (m_pDirDatas[i].ol) // The first pointer in m_pDirDatas is NULL, the rest are pointers // to DIRCHANGEDATA structures. // The lists are terminated with NULL // Allocating arraysn, note that the size of the list is not // necessary aRoots.GetSize()+2 if CreateFileW or m_pReadDirectoryChangesW // return error m_pEventHandles=new HANDLE[aRoots.GetSize()+1]; ASSERT(m_pEventHandles!=NULL); m_pDirDatas=new DIRCHANGEDATA*[aRoots.GetSize()+1]; ASSERT(m_pDirDatas!=NULL); // First event in events array is stop event and first pointer to // DIRCHANGEDATA structure is NULL, so that each element in m_pEventHandles (with index >0) // corresponds to element in m_pChangeDatas with the same index m_pEventHandles[0]=m_hStopEvent; m_pDirDatas[0]=NULL; // Creating handles and DIRCHANGEDATA structures for directories in aRoots array m_nHandles=1; // Number of handles currently in arrays, first element is stop event / NULL DIRCHANGEDATA* pDirData=NULL; for (int i=0;i<aRoots.GetSize();i++) { if (m_lFlags&fwStopWhenPossible) { // Notify to Stop() that we are going to stop what // we are doing InterlockedExchange(&m_lState,sStopping); SetEvent(m_hStopEvent); break; } CStringW sRoot=aRoots.GetAt(i); // If root of the type "X:", change it to "X:\" if (sRoot[1]==':' && sRoot[2]=='\0') sRoot << L'\\'; /* #ifdef _DEBUG_LOGGING // If logging is on, do not use change notifications for root containing log file LPCSTR pLogFile=GetDebugLoggingFile(); if (pLogFile!=NULL) { // No debug logging for drive containing hfcdebug.log char* szPath=alloccopyWtoA(sRoot); MakeLower(szPath); BOOL bSame=strncmp(szPath,pLogFile,sRoot.GetLength())==0; delete[] szPath; if (bSame) continue; } #endif */ // Allocating new DIRCHANGEDATA struct if (pDirData==NULL) { pDirData=new DIRCHANGEDATA; // Create event for overlay structure pDirData->ol.hEvent=CreateEvent(NULL,FALSE,FALSE,NULL); DebugOpenEvent(pDirData->ol.hEvent); // Allocate buffer pDirData->pBuffer=new BYTE[CHANGE_BUFFER_LEN]; } // Create handle to directory if (IsUnicodeSystem()) pDirData->hDirHandle=CreateFileW(sRoot,GENERIC_READ /*FILE_LIST_DIRECTORY*/,FILE_SHARE_READ|FILE_SHARE_DELETE|FILE_SHARE_WRITE, NULL,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OVERLAPPED,NULL); else pDirData->hDirHandle=CreateFile(W2A(sRoot),GENERIC_READ /*FILE_LIST_DIRECTORY*/,FILE_SHARE_READ|FILE_SHARE_DELETE|FILE_SHARE_WRITE, NULL,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OVERLAPPED,NULL); // If pDirData handle is invalid, skip this root if (pDirData->hDirHandle==INVALID_HANDLE_VALUE) continue; DebugOpenHandle(dhtFile,pDirData->hDirHandle,sRoot); // Test this again if (m_lFlags&fwStopWhenPossible) { // Notify to Stop() that we are going to stop what // we are doing InterlockedExchange(&m_lState,sStopping); SetEvent(m_hStopEvent); break; } // Start to read directory changes, asynchronous mode BOOL bRet=m_pReadDirectoryChangesW(pDirData->hDirHandle,pDirData->pBuffer,CHANGE_BUFFER_LEN,TRUE, FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_DIR_NAME| FILE_NOTIFY_CHANGE_ATTRIBUTES|FILE_NOTIFY_CHANGE_SIZE|FILE_NOTIFY_CHANGE_LAST_WRITE| FILE_NOTIFY_CHANGE_CREATION|FILE_NOTIFY_CHANGE_SECURITY, NULL,&pDirData->ol,NULL); if (!bRet) { // Cannot read directory changes (maybe UNC path), closing directory handle and skipping // this directory. Allocated pDirData can be left untouched for the next try CloseHandle(pDirData->hDirHandle); DebugCloseHandle(dhtFile,pDirData->hDirHandle,sRoot); continue; } // And yet again if (m_lFlags&fwStopWhenPossible) { // Notify to Stop() that we are going to stop what // we are doing SetEvent(m_hStopEvent); InterlockedExchange(&m_lState,sStopping); break; } // Copy root path to pDirData structure if (sRoot.LastChar()!=L'\\') sRoot << L'\\'; sRoot.MakeLower(); sRoot.FreeExtra(); pDirData->dwRootLength=sRoot.GetLength(); pDirData->pRoot=sRoot.GiveBuffer(); // Handle in m_pEventHandles was the event used in overlay structure, set it m_pEventHandles[m_nHandles]=pDirData->ol.hEvent; // Add pointer to m_pDirDatas structure ASSERT(m_nHandles<UINT(aRoots.GetSize()+2)); m_pDirDatas[m_nHandles]=pDirData; m_nHandles++; // New DIRCHANGEDATA structure should be allocated pDirData=NULL; } // Free extra DIRCHANGEDATA structure if (pDirData!=NULL) delete pDirData; FnDebugMessage("FN handles created"); return TRUE; }