CmWin32FolderMonitor.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. #include "CmWin32FolderMonitor.h"
  2. #include "CmFileSystem.h"
  3. #include "CmException.h"
  4. #include <windows.h>
  5. namespace CamelotFramework
  6. {
  7. enum class MonitorState
  8. {
  9. Inactive,
  10. Starting,
  11. Monitoring,
  12. Shutdown,
  13. Shutdown2
  14. };
  15. class WorkerFunc
  16. {
  17. public:
  18. WorkerFunc(Win32FolderMonitor* owner);
  19. void operator()();
  20. private:
  21. Win32FolderMonitor* mOwner;
  22. };
  23. struct Win32FolderMonitor::FolderWatchInfo
  24. {
  25. FolderWatchInfo(const WString& folderToMonitor, HANDLE dirHandle, bool monitorSubdirectories, DWORD monitorFlags);
  26. ~FolderWatchInfo();
  27. void startMonitor(HANDLE compPortHandle);
  28. void stopMonitor(HANDLE compPortHandle);
  29. static const UINT32 READ_BUFFER_SIZE = 65536;
  30. WString mFolderToMonitor;
  31. HANDLE mDirHandle;
  32. OVERLAPPED mOverlapped;
  33. MonitorState mState;
  34. UINT8 mBuffer[READ_BUFFER_SIZE];
  35. DWORD mBufferSize;
  36. bool mMonitorSubdirectories;
  37. DWORD mMonitorFlags;
  38. DWORD mReadError;
  39. WString mCachedOldFileName; // Used during rename notifications as they are handled in two steps
  40. CM_MUTEX(mStatusMutex)
  41. CM_THREAD_SYNCHRONISER(mStartStopEvent)
  42. };
  43. Win32FolderMonitor::FolderWatchInfo::FolderWatchInfo(const WString& folderToMonitor, HANDLE dirHandle, bool monitorSubdirectories, DWORD monitorFlags)
  44. :mFolderToMonitor(folderToMonitor), mDirHandle(dirHandle), mState(MonitorState::Inactive), mBufferSize(0),
  45. mMonitorSubdirectories(monitorSubdirectories), mMonitorFlags(monitorFlags), mReadError(0)
  46. {
  47. StringUtil::trim(mFolderToMonitor, L"\\", false, true);
  48. StringUtil::toLowerCase(mFolderToMonitor);
  49. memset(&mOverlapped, 0, sizeof(mOverlapped));
  50. }
  51. Win32FolderMonitor::FolderWatchInfo::~FolderWatchInfo()
  52. {
  53. assert(mState == MonitorState::Inactive);
  54. stopMonitor(0);
  55. }
  56. void Win32FolderMonitor::FolderWatchInfo::startMonitor(HANDLE compPortHandle)
  57. {
  58. if(mState != MonitorState::Inactive)
  59. return; // Already monitoring
  60. {
  61. CM_LOCK_MUTEX_NAMED(mStatusMutex, lock);
  62. mState = MonitorState::Starting;
  63. PostQueuedCompletionStatus(compPortHandle, sizeof(this), (DWORD)this, &mOverlapped);
  64. while(mState != MonitorState::Monitoring)
  65. CM_THREAD_WAIT(mStartStopEvent, mStatusMutex, lock);
  66. }
  67. if(mReadError != ERROR_SUCCESS)
  68. {
  69. {
  70. CM_LOCK_MUTEX(mStatusMutex);
  71. mState = MonitorState::Inactive;
  72. }
  73. CM_EXCEPT(InternalErrorException, "Failed to start folder monitor on folder \"" +
  74. toString(mFolderToMonitor) + "\" because ReadDirectoryChangesW failed.");
  75. }
  76. }
  77. void Win32FolderMonitor::FolderWatchInfo::stopMonitor(HANDLE compPortHandle)
  78. {
  79. if(mState != MonitorState::Inactive)
  80. {
  81. CM_LOCK_MUTEX_NAMED(mStatusMutex, lock);
  82. mState = MonitorState::Shutdown;
  83. PostQueuedCompletionStatus(compPortHandle, sizeof(this), (DWORD)this, &mOverlapped);
  84. while(mState != MonitorState::Inactive)
  85. CM_THREAD_WAIT(mStartStopEvent, mStatusMutex, lock);
  86. }
  87. if(mDirHandle != INVALID_HANDLE_VALUE)
  88. {
  89. CloseHandle(mDirHandle);
  90. mDirHandle = INVALID_HANDLE_VALUE;
  91. }
  92. }
  93. class Win32FolderMonitor::FileNotifyInfo
  94. {
  95. public:
  96. FileNotifyInfo(UINT8* notifyBuffer, DWORD bufferSize)
  97. :mBuffer(notifyBuffer), mBufferSize(bufferSize)
  98. {
  99. mCurrentRecord = (PFILE_NOTIFY_INFORMATION)mBuffer;
  100. }
  101. bool getNext();
  102. DWORD getAction() const;
  103. WString getFileName() const;
  104. WString getFileNameWithPath(const WString& rootPath) const;
  105. protected:
  106. UINT8* mBuffer;
  107. DWORD mBufferSize;
  108. PFILE_NOTIFY_INFORMATION mCurrentRecord;
  109. };
  110. bool Win32FolderMonitor::FileNotifyInfo::getNext()
  111. {
  112. if(mCurrentRecord && mCurrentRecord->NextEntryOffset != 0)
  113. {
  114. PFILE_NOTIFY_INFORMATION oldRecord = mCurrentRecord;
  115. mCurrentRecord = (PFILE_NOTIFY_INFORMATION) ((UINT8*)mCurrentRecord + mCurrentRecord->NextEntryOffset);
  116. if((DWORD)((UINT8*)mCurrentRecord - mBuffer) > mBufferSize)
  117. {
  118. // Gone out of range, something bad happened
  119. assert(false);
  120. mCurrentRecord = oldRecord;
  121. }
  122. return (mCurrentRecord != oldRecord);
  123. }
  124. return false;
  125. }
  126. DWORD Win32FolderMonitor::FileNotifyInfo::getAction() const
  127. {
  128. assert(mCurrentRecord != nullptr);
  129. if(mCurrentRecord)
  130. return mCurrentRecord->Action;
  131. return 0;
  132. }
  133. WString Win32FolderMonitor::FileNotifyInfo::getFileName() const
  134. {
  135. if(mCurrentRecord)
  136. {
  137. wchar_t fileNameBuffer[32768 + 1] = {0};
  138. memcpy(fileNameBuffer, mCurrentRecord->FileName,
  139. std::min(DWORD(32768 * sizeof(wchar_t)), mCurrentRecord->FileNameLength));
  140. return WString(fileNameBuffer);
  141. }
  142. return WString();
  143. }
  144. WString Win32FolderMonitor::FileNotifyInfo::getFileNameWithPath(const WString& rootPath) const
  145. {
  146. WStringStream wss;
  147. wss<<rootPath;
  148. wss<<L"\\";
  149. wss<<getFileName();
  150. return wss.str();
  151. }
  152. enum class FileActionType
  153. {
  154. Added,
  155. Removed,
  156. Modified,
  157. Renamed
  158. };
  159. struct FileAction
  160. {
  161. static FileAction* createAdded(const WString& fileName)
  162. {
  163. UINT8* bytes = (UINT8*)cm_alloc((UINT32)(sizeof(FileAction) + (fileName.size() + 1) * sizeof(WString::value_type)));
  164. FileAction* action = (FileAction*)bytes;
  165. bytes += sizeof(FileAction);
  166. action->oldName = nullptr;
  167. action->newName = (WString::value_type*)bytes;
  168. action->type = FileActionType::Added;
  169. memcpy(action->newName, fileName.data(), fileName.size() * sizeof(WString::value_type));
  170. action->newName[fileName.size()] = L'\0';
  171. return action;
  172. }
  173. static FileAction* createRemoved(const WString& fileName)
  174. {
  175. UINT8* bytes = (UINT8*)cm_alloc((UINT32)(sizeof(FileAction) + (fileName.size() + 1) * sizeof(WString::value_type)));
  176. FileAction* action = (FileAction*)bytes;
  177. bytes += sizeof(FileAction);
  178. action->oldName = nullptr;
  179. action->newName = (WString::value_type*)bytes;
  180. action->type = FileActionType::Removed;
  181. memcpy(action->newName, fileName.data(), fileName.size() * sizeof(WString::value_type));
  182. action->newName[fileName.size()] = L'\0';
  183. return action;
  184. }
  185. static FileAction* createModified(const WString& fileName)
  186. {
  187. UINT8* bytes = (UINT8*)cm_alloc((UINT32)(sizeof(FileAction) + (fileName.size() + 1) * sizeof(WString::value_type)));
  188. FileAction* action = (FileAction*)bytes;
  189. bytes += sizeof(FileAction);
  190. action->oldName = nullptr;
  191. action->newName = (WString::value_type*)bytes;
  192. action->type = FileActionType::Modified;
  193. memcpy(action->newName, fileName.data(), fileName.size() * sizeof(WString::value_type));
  194. action->newName[fileName.size()] = L'\0';
  195. return action;
  196. }
  197. static FileAction* createRenamed(const WString& oldFilename, const WString& newfileName)
  198. {
  199. UINT8* bytes = (UINT8*)cm_alloc((UINT32)(sizeof(FileAction) +
  200. (oldFilename.size() + newfileName.size() + 2) * sizeof(WString::value_type)));
  201. FileAction* action = (FileAction*)bytes;
  202. bytes += sizeof(FileAction);
  203. action->oldName = (WString::value_type*)bytes;
  204. bytes += (oldFilename.size() + 1) * sizeof(WString::value_type);
  205. action->newName = (WString::value_type*)bytes;
  206. action->type = FileActionType::Modified;
  207. memcpy(action->oldName, oldFilename.data(), oldFilename.size() * sizeof(WString::value_type));
  208. action->oldName[oldFilename.size()] = L'\0';
  209. memcpy(action->newName, newfileName.data(), newfileName.size() * sizeof(WString::value_type));
  210. action->newName[newfileName.size()] = L'\0';
  211. return action;
  212. }
  213. static void destroy(FileAction* action)
  214. {
  215. cm_free(action);
  216. }
  217. WString::value_type* oldName;
  218. WString::value_type* newName;
  219. FileActionType type;
  220. };
  221. struct Win32FolderMonitor::Pimpl
  222. {
  223. Vector<FolderWatchInfo*>::type mFoldersToWatch;
  224. HANDLE mCompPortHandle;
  225. Queue<FileAction*>::type mFileActions;
  226. Queue<FileAction*>::type mActiveFileActions;
  227. CM_MUTEX(mFileActionsMutex);
  228. CM_THREAD_TYPE* mWorkerThread;
  229. };
  230. Win32FolderMonitor::Win32FolderMonitor()
  231. {
  232. mPimpl = cm_new<Pimpl>();
  233. mPimpl->mWorkerThread = nullptr;
  234. mPimpl->mCompPortHandle = nullptr;
  235. }
  236. Win32FolderMonitor::~Win32FolderMonitor()
  237. {
  238. stopMonitorAll();
  239. // No need for mutex since we know worker thread is shut down by now
  240. while(!mPimpl->mFileActions.empty())
  241. {
  242. FileAction* action = mPimpl->mFileActions.front();
  243. mPimpl->mFileActions.pop();
  244. FileAction::destroy(action);
  245. }
  246. cm_delete(mPimpl);
  247. }
  248. void Win32FolderMonitor::startMonitor(const WString& folderPath, bool subdirectories, FolderChange changeFilter)
  249. {
  250. if(!FileSystem::isDirectory(folderPath))
  251. {
  252. CM_EXCEPT(InvalidParametersException, "Provided path \"" + toString(folderPath) + "\" is not a directory");
  253. }
  254. WString extendedFolderPath = L"\\\\?\\" + folderPath;
  255. HANDLE dirHandle = CreateFileW(folderPath.c_str(), FILE_LIST_DIRECTORY,
  256. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING,
  257. FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, nullptr);
  258. if(dirHandle == INVALID_HANDLE_VALUE)
  259. {
  260. CM_EXCEPT(InternalErrorException, "Failed to open folder \"" + toString(folderPath) + "\" for monitoring. Error code: " + toString(GetLastError()));
  261. }
  262. DWORD filterFlags = 0;
  263. if((((UINT32)changeFilter) & (UINT32)CamelotFramework::FolderChange::FileName) != 0)
  264. filterFlags |= FILE_NOTIFY_CHANGE_FILE_NAME;
  265. if((((UINT32)changeFilter) & (UINT32)CamelotFramework::FolderChange::DirName) != 0)
  266. filterFlags |= FILE_NOTIFY_CHANGE_DIR_NAME;
  267. if((((UINT32)changeFilter) & (UINT32)CamelotFramework::FolderChange::Attributes) != 0)
  268. filterFlags |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
  269. if((((UINT32)changeFilter) & (UINT32)CamelotFramework::FolderChange::Size) != 0)
  270. filterFlags |= FILE_NOTIFY_CHANGE_SIZE;
  271. if((((UINT32)changeFilter) & (UINT32)CamelotFramework::FolderChange::LastWrite) != 0)
  272. filterFlags |= FILE_NOTIFY_CHANGE_LAST_WRITE;
  273. if((((UINT32)changeFilter) & (UINT32)CamelotFramework::FolderChange::LastAccess) != 0)
  274. filterFlags |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
  275. if((((UINT32)changeFilter) & (UINT32)CamelotFramework::FolderChange::Creation) != 0)
  276. filterFlags |= FILE_NOTIFY_CHANGE_CREATION;
  277. if((((UINT32)changeFilter) & (UINT32)CamelotFramework::FolderChange::Security) != 0)
  278. filterFlags |= FILE_NOTIFY_CHANGE_SECURITY;
  279. mPimpl->mFoldersToWatch.push_back(cm_new<FolderWatchInfo>(folderPath, dirHandle, subdirectories, filterFlags));
  280. FolderWatchInfo* watchInfo = mPimpl->mFoldersToWatch.back();
  281. mPimpl->mCompPortHandle = CreateIoCompletionPort(dirHandle, mPimpl->mCompPortHandle, (DWORD)watchInfo, 0);
  282. if(mPimpl->mCompPortHandle == nullptr)
  283. {
  284. mPimpl->mFoldersToWatch.erase(mPimpl->mFoldersToWatch.end() - 1);
  285. cm_delete(watchInfo);
  286. CM_EXCEPT(InternalErrorException, "Failed to open completition port for folder monitoring. Error code: " + toString(GetLastError()));
  287. }
  288. if(mPimpl->mWorkerThread == nullptr)
  289. {
  290. CM_THREAD_CREATE(t, (boost::bind(&Win32FolderMonitor::workerThreadMain, this)));
  291. mPimpl->mWorkerThread = t;
  292. if(mPimpl->mWorkerThread == nullptr)
  293. {
  294. mPimpl->mFoldersToWatch.erase(mPimpl->mFoldersToWatch.end() - 1);
  295. cm_delete(watchInfo);
  296. CM_EXCEPT(InternalErrorException, "Failed to create a new worker thread for folder monitoring");
  297. }
  298. }
  299. if(mPimpl->mWorkerThread != nullptr)
  300. {
  301. try
  302. {
  303. watchInfo->startMonitor(mPimpl->mCompPortHandle);
  304. }
  305. catch (Exception* e)
  306. {
  307. mPimpl->mFoldersToWatch.erase(mPimpl->mFoldersToWatch.end() - 1);
  308. cm_delete(watchInfo);
  309. throw(e);
  310. }
  311. }
  312. else
  313. {
  314. mPimpl->mFoldersToWatch.erase(mPimpl->mFoldersToWatch.end() - 1);
  315. cm_delete(watchInfo);
  316. CM_EXCEPT(InternalErrorException, "Failed to create a new worker thread for folder monitoring");
  317. }
  318. }
  319. void Win32FolderMonitor::stopMonitor(const WString& folderPath)
  320. {
  321. WString folderPathForComparison = folderPath;
  322. StringUtil::trim(folderPathForComparison, L"\\", false, true);
  323. StringUtil::toLowerCase(folderPathForComparison);
  324. auto findIter = std::find_if(mPimpl->mFoldersToWatch.begin(), mPimpl->mFoldersToWatch.end(),
  325. [&] (const FolderWatchInfo* x) { return x->mFolderToMonitor == folderPathForComparison; });
  326. if(findIter != mPimpl->mFoldersToWatch.end())
  327. {
  328. FolderWatchInfo* watchInfo = *findIter;
  329. watchInfo->stopMonitor(mPimpl->mCompPortHandle);
  330. cm_delete(watchInfo);
  331. mPimpl->mFoldersToWatch.erase(findIter);
  332. }
  333. if(mPimpl->mFoldersToWatch.size() == 0)
  334. stopMonitorAll();
  335. }
  336. void Win32FolderMonitor::stopMonitorAll()
  337. {
  338. for(auto& watchInfo : mPimpl->mFoldersToWatch)
  339. {
  340. watchInfo->stopMonitor(mPimpl->mCompPortHandle);
  341. cm_delete(watchInfo);
  342. }
  343. mPimpl->mFoldersToWatch.clear();
  344. if(mPimpl->mWorkerThread != nullptr)
  345. {
  346. PostQueuedCompletionStatus(mPimpl->mCompPortHandle, 0, 0, nullptr);
  347. mPimpl->mWorkerThread->join();
  348. CM_THREAD_DESTROY(mPimpl->mWorkerThread);
  349. mPimpl->mWorkerThread = nullptr;
  350. }
  351. if(mPimpl->mCompPortHandle != nullptr)
  352. {
  353. CloseHandle(mPimpl->mCompPortHandle);
  354. mPimpl->mCompPortHandle = nullptr;
  355. }
  356. }
  357. void Win32FolderMonitor::workerThreadMain()
  358. {
  359. FolderWatchInfo* watchInfo = nullptr;
  360. do
  361. {
  362. DWORD numBytes;
  363. LPOVERLAPPED overlapped;
  364. if(!GetQueuedCompletionStatus(mPimpl->mCompPortHandle, &numBytes, (PULONG_PTR) &watchInfo, &overlapped, INFINITE))
  365. {
  366. assert(false);
  367. // TODO: Folder handle was lost most likely. Not sure how to deal with that. Shutdown watch on this folder and cleanup?
  368. }
  369. if(watchInfo != nullptr)
  370. {
  371. MonitorState state;
  372. {
  373. CM_LOCK_MUTEX(watchInfo->mStatusMutex);
  374. state = watchInfo->mState;
  375. }
  376. switch(state)
  377. {
  378. case MonitorState::Starting:
  379. if(!ReadDirectoryChangesW(watchInfo->mDirHandle, watchInfo->mBuffer, FolderWatchInfo::READ_BUFFER_SIZE,
  380. watchInfo->mMonitorSubdirectories, watchInfo->mMonitorFlags, &watchInfo->mBufferSize, &watchInfo->mOverlapped, nullptr))
  381. {
  382. assert(false); // TODO - Possibly the buffer was too small?
  383. watchInfo->mReadError = GetLastError();
  384. }
  385. else
  386. {
  387. watchInfo->mReadError = ERROR_SUCCESS;
  388. {
  389. CM_LOCK_MUTEX(watchInfo->mStatusMutex);
  390. watchInfo->mState = MonitorState::Monitoring;
  391. }
  392. }
  393. CM_THREAD_NOTIFY_ONE(watchInfo->mStartStopEvent);
  394. break;
  395. case MonitorState::Monitoring:
  396. {
  397. FileNotifyInfo info(watchInfo->mBuffer, FolderWatchInfo::READ_BUFFER_SIZE);
  398. handleNotifications(info, *watchInfo);
  399. if(!ReadDirectoryChangesW(watchInfo->mDirHandle, watchInfo->mBuffer, FolderWatchInfo::READ_BUFFER_SIZE,
  400. watchInfo->mMonitorSubdirectories, watchInfo->mMonitorFlags, &watchInfo->mBufferSize, &watchInfo->mOverlapped, nullptr))
  401. {
  402. assert(false); // TODO: Failed during normal operation, possibly the buffer was too small. Shutdown watch on this folder and cleanup?
  403. watchInfo->mReadError = GetLastError();
  404. }
  405. else
  406. {
  407. watchInfo->mReadError = ERROR_SUCCESS;
  408. }
  409. }
  410. break;
  411. case MonitorState::Shutdown:
  412. if(watchInfo->mDirHandle != INVALID_HANDLE_VALUE)
  413. {
  414. CloseHandle(watchInfo->mDirHandle);
  415. watchInfo->mDirHandle = INVALID_HANDLE_VALUE;
  416. {
  417. CM_LOCK_MUTEX(watchInfo->mStatusMutex);
  418. watchInfo->mState = MonitorState::Shutdown2;
  419. }
  420. }
  421. else
  422. {
  423. watchInfo->mState = MonitorState::Inactive;
  424. CM_THREAD_NOTIFY_ONE(watchInfo->mStartStopEvent);
  425. }
  426. break;
  427. case MonitorState::Shutdown2:
  428. if(watchInfo->mDirHandle != INVALID_HANDLE_VALUE)
  429. {
  430. // Handle is still open? Try again.
  431. CloseHandle(watchInfo->mDirHandle);
  432. watchInfo->mDirHandle = INVALID_HANDLE_VALUE;
  433. }
  434. else
  435. {
  436. watchInfo->mState = MonitorState::Inactive;
  437. CM_THREAD_NOTIFY_ONE(watchInfo->mStartStopEvent);
  438. }
  439. break;
  440. }
  441. }
  442. } while (watchInfo != nullptr);
  443. }
  444. void Win32FolderMonitor::handleNotifications(FileNotifyInfo& notifyInfo, FolderWatchInfo& watchInfo)
  445. {
  446. Vector<FileAction*>::type mActions;
  447. do
  448. {
  449. switch(notifyInfo.getAction())
  450. {
  451. case FILE_ACTION_ADDED:
  452. {
  453. WString fileName = notifyInfo.getFileNameWithPath(watchInfo.mFolderToMonitor);
  454. mActions.push_back(FileAction::createAdded(fileName));
  455. }
  456. break;
  457. case FILE_ACTION_REMOVED:
  458. {
  459. WString fileName = notifyInfo.getFileNameWithPath(watchInfo.mFolderToMonitor);
  460. mActions.push_back(FileAction::createRemoved(fileName));
  461. }
  462. break;
  463. case FILE_ACTION_MODIFIED:
  464. {
  465. WString fileName = notifyInfo.getFileNameWithPath(watchInfo.mFolderToMonitor);
  466. mActions.push_back(FileAction::createModified(fileName));
  467. }
  468. break;
  469. case FILE_ACTION_RENAMED_OLD_NAME:
  470. {
  471. WString fileName = notifyInfo.getFileNameWithPath(watchInfo.mFolderToMonitor);
  472. watchInfo.mCachedOldFileName = fileName;
  473. }
  474. break;
  475. case FILE_ACTION_RENAMED_NEW_NAME:
  476. {
  477. WString fileName = notifyInfo.getFileNameWithPath(watchInfo.mFolderToMonitor);
  478. mActions.push_back(FileAction::createRenamed(watchInfo.mCachedOldFileName, fileName));
  479. }
  480. break;
  481. }
  482. } while(notifyInfo.getNext());
  483. {
  484. CM_LOCK_MUTEX(mPimpl->mFileActionsMutex);
  485. for(auto& action : mActions)
  486. mPimpl->mFileActions.push(action);
  487. }
  488. }
  489. void Win32FolderMonitor::update()
  490. {
  491. {
  492. CM_LOCK_MUTEX(mPimpl->mFileActionsMutex);
  493. mPimpl->mActiveFileActions.swap(mPimpl->mFileActions);
  494. }
  495. while(!mPimpl->mActiveFileActions.empty())
  496. {
  497. FileAction* action = mPimpl->mActiveFileActions.front();
  498. mPimpl->mActiveFileActions.pop();
  499. switch(action->type)
  500. {
  501. case FileActionType::Added:
  502. if(!onAdded.empty())
  503. onAdded(WString(action->newName));
  504. break;
  505. case FileActionType::Removed:
  506. if(!onRemoved.empty())
  507. onRemoved(WString(action->newName));
  508. break;
  509. case FileActionType::Modified:
  510. if(!onModified.empty())
  511. onModified(WString(action->newName));
  512. break;
  513. case FileActionType::Renamed:
  514. if(!onRenamed.empty())
  515. onRenamed(WString(action->oldName), WString(action->newName));
  516. break;
  517. }
  518. FileAction::destroy(action);
  519. }
  520. }
  521. }