BsWin32FolderMonitor.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. #include "Win32/BsWin32FolderMonitor.h"
  2. #include "BsFileSystem.h"
  3. #include "BsException.h"
  4. #include "BsDebug.h"
  5. #include <windows.h>
  6. namespace BansheeEngine
  7. {
  8. enum class MonitorState
  9. {
  10. Inactive,
  11. Starting,
  12. Monitoring,
  13. Shutdown,
  14. Shutdown2
  15. };
  16. class WorkerFunc
  17. {
  18. public:
  19. WorkerFunc(FolderMonitor* owner);
  20. void operator()();
  21. private:
  22. FolderMonitor* mOwner;
  23. };
  24. struct FolderMonitor::FolderWatchInfo
  25. {
  26. FolderWatchInfo(const Path& folderToMonitor, HANDLE dirHandle, bool monitorSubdirectories, DWORD monitorFlags);
  27. ~FolderWatchInfo();
  28. void startMonitor(HANDLE compPortHandle);
  29. void stopMonitor(HANDLE compPortHandle);
  30. static const UINT32 READ_BUFFER_SIZE = 65536;
  31. Path mFolderToMonitor;
  32. HANDLE mDirHandle;
  33. OVERLAPPED mOverlapped;
  34. MonitorState mState;
  35. UINT8 mBuffer[READ_BUFFER_SIZE];
  36. DWORD mBufferSize;
  37. bool mMonitorSubdirectories;
  38. DWORD mMonitorFlags;
  39. DWORD mReadError;
  40. WString mCachedOldFileName; // Used during rename notifications as they are handled in two steps
  41. BS_MUTEX(mStatusMutex)
  42. BS_THREAD_SYNCHRONISER(mStartStopEvent)
  43. };
  44. FolderMonitor::FolderWatchInfo::FolderWatchInfo(const Path& folderToMonitor, HANDLE dirHandle, bool monitorSubdirectories, DWORD monitorFlags)
  45. :mFolderToMonitor(folderToMonitor), mDirHandle(dirHandle), mState(MonitorState::Inactive), mBufferSize(0),
  46. mMonitorSubdirectories(monitorSubdirectories), mMonitorFlags(monitorFlags), mReadError(0)
  47. {
  48. memset(&mOverlapped, 0, sizeof(mOverlapped));
  49. }
  50. FolderMonitor::FolderWatchInfo::~FolderWatchInfo()
  51. {
  52. assert(mState == MonitorState::Inactive);
  53. stopMonitor(0);
  54. }
  55. void FolderMonitor::FolderWatchInfo::startMonitor(HANDLE compPortHandle)
  56. {
  57. if(mState != MonitorState::Inactive)
  58. return; // Already monitoring
  59. {
  60. BS_LOCK_MUTEX_NAMED(mStatusMutex, lock);
  61. mState = MonitorState::Starting;
  62. PostQueuedCompletionStatus(compPortHandle, sizeof(this), (ULONG_PTR)this, &mOverlapped);
  63. while(mState != MonitorState::Monitoring)
  64. BS_THREAD_WAIT(mStartStopEvent, mStatusMutex, lock);
  65. }
  66. if(mReadError != ERROR_SUCCESS)
  67. {
  68. {
  69. BS_LOCK_MUTEX(mStatusMutex);
  70. mState = MonitorState::Inactive;
  71. }
  72. BS_EXCEPT(InternalErrorException, "Failed to start folder monitor on folder \"" +
  73. mFolderToMonitor.toString() + "\" because ReadDirectoryChangesW failed.");
  74. }
  75. }
  76. void FolderMonitor::FolderWatchInfo::stopMonitor(HANDLE compPortHandle)
  77. {
  78. if(mState != MonitorState::Inactive)
  79. {
  80. BS_LOCK_MUTEX_NAMED(mStatusMutex, lock);
  81. mState = MonitorState::Shutdown;
  82. PostQueuedCompletionStatus(compPortHandle, sizeof(this), (ULONG_PTR)this, &mOverlapped);
  83. while(mState != MonitorState::Inactive)
  84. BS_THREAD_WAIT(mStartStopEvent, mStatusMutex, lock);
  85. }
  86. if(mDirHandle != INVALID_HANDLE_VALUE)
  87. {
  88. CloseHandle(mDirHandle);
  89. mDirHandle = INVALID_HANDLE_VALUE;
  90. }
  91. }
  92. class FolderMonitor::FileNotifyInfo
  93. {
  94. public:
  95. FileNotifyInfo(UINT8* notifyBuffer, DWORD bufferSize)
  96. :mBuffer(notifyBuffer), mBufferSize(bufferSize)
  97. {
  98. mCurrentRecord = (PFILE_NOTIFY_INFORMATION)mBuffer;
  99. }
  100. bool getNext();
  101. DWORD getAction() const;
  102. WString getFileName() const;
  103. WString getFileNameWithPath(const Path& rootPath) const;
  104. protected:
  105. UINT8* mBuffer;
  106. DWORD mBufferSize;
  107. PFILE_NOTIFY_INFORMATION mCurrentRecord;
  108. };
  109. bool FolderMonitor::FileNotifyInfo::getNext()
  110. {
  111. if(mCurrentRecord && mCurrentRecord->NextEntryOffset != 0)
  112. {
  113. PFILE_NOTIFY_INFORMATION oldRecord = mCurrentRecord;
  114. mCurrentRecord = (PFILE_NOTIFY_INFORMATION) ((UINT8*)mCurrentRecord + mCurrentRecord->NextEntryOffset);
  115. if((DWORD)((UINT8*)mCurrentRecord - mBuffer) > mBufferSize)
  116. {
  117. // Gone out of range, something bad happened
  118. assert(false);
  119. mCurrentRecord = oldRecord;
  120. }
  121. return (mCurrentRecord != oldRecord);
  122. }
  123. return false;
  124. }
  125. DWORD FolderMonitor::FileNotifyInfo::getAction() const
  126. {
  127. assert(mCurrentRecord != nullptr);
  128. if(mCurrentRecord)
  129. return mCurrentRecord->Action;
  130. return 0;
  131. }
  132. WString FolderMonitor::FileNotifyInfo::getFileName() const
  133. {
  134. if(mCurrentRecord)
  135. {
  136. wchar_t fileNameBuffer[32768 + 1] = {0};
  137. memcpy(fileNameBuffer, mCurrentRecord->FileName,
  138. std::min(DWORD(32768 * sizeof(wchar_t)), mCurrentRecord->FileNameLength));
  139. return WString(fileNameBuffer);
  140. }
  141. return WString();
  142. }
  143. WString FolderMonitor::FileNotifyInfo::getFileNameWithPath(const Path& rootPath) const
  144. {
  145. Path fullPath = rootPath;
  146. return fullPath.append(getFileName()).toWString();
  147. }
  148. enum class FileActionType
  149. {
  150. Added,
  151. Removed,
  152. Modified,
  153. Renamed
  154. };
  155. struct FileAction
  156. {
  157. static FileAction* createAdded(const WString& fileName)
  158. {
  159. UINT8* bytes = (UINT8*)bs_alloc((UINT32)(sizeof(FileAction) + (fileName.size() + 1) * sizeof(WString::value_type)));
  160. FileAction* action = (FileAction*)bytes;
  161. bytes += sizeof(FileAction);
  162. action->oldName = nullptr;
  163. action->newName = (WString::value_type*)bytes;
  164. action->type = FileActionType::Added;
  165. memcpy(action->newName, fileName.data(), fileName.size() * sizeof(WString::value_type));
  166. action->newName[fileName.size()] = L'\0';
  167. return action;
  168. }
  169. static FileAction* createRemoved(const WString& fileName)
  170. {
  171. UINT8* bytes = (UINT8*)bs_alloc((UINT32)(sizeof(FileAction) + (fileName.size() + 1) * sizeof(WString::value_type)));
  172. FileAction* action = (FileAction*)bytes;
  173. bytes += sizeof(FileAction);
  174. action->oldName = nullptr;
  175. action->newName = (WString::value_type*)bytes;
  176. action->type = FileActionType::Removed;
  177. memcpy(action->newName, fileName.data(), fileName.size() * sizeof(WString::value_type));
  178. action->newName[fileName.size()] = L'\0';
  179. return action;
  180. }
  181. static FileAction* createModified(const WString& fileName)
  182. {
  183. UINT8* bytes = (UINT8*)bs_alloc((UINT32)(sizeof(FileAction) + (fileName.size() + 1) * sizeof(WString::value_type)));
  184. FileAction* action = (FileAction*)bytes;
  185. bytes += sizeof(FileAction);
  186. action->oldName = nullptr;
  187. action->newName = (WString::value_type*)bytes;
  188. action->type = FileActionType::Modified;
  189. memcpy(action->newName, fileName.data(), fileName.size() * sizeof(WString::value_type));
  190. action->newName[fileName.size()] = L'\0';
  191. return action;
  192. }
  193. static FileAction* createRenamed(const WString& oldFilename, const WString& newfileName)
  194. {
  195. UINT8* bytes = (UINT8*)bs_alloc((UINT32)(sizeof(FileAction) +
  196. (oldFilename.size() + newfileName.size() + 2) * sizeof(WString::value_type)));
  197. FileAction* action = (FileAction*)bytes;
  198. bytes += sizeof(FileAction);
  199. action->oldName = (WString::value_type*)bytes;
  200. bytes += (oldFilename.size() + 1) * sizeof(WString::value_type);
  201. action->newName = (WString::value_type*)bytes;
  202. action->type = FileActionType::Modified;
  203. memcpy(action->oldName, oldFilename.data(), oldFilename.size() * sizeof(WString::value_type));
  204. action->oldName[oldFilename.size()] = L'\0';
  205. memcpy(action->newName, newfileName.data(), newfileName.size() * sizeof(WString::value_type));
  206. action->newName[newfileName.size()] = L'\0';
  207. return action;
  208. }
  209. static void destroy(FileAction* action)
  210. {
  211. bs_free(action);
  212. }
  213. WString::value_type* oldName;
  214. WString::value_type* newName;
  215. FileActionType type;
  216. };
  217. struct FolderMonitor::Pimpl
  218. {
  219. Vector<FolderWatchInfo*> mFoldersToWatch;
  220. HANDLE mCompPortHandle;
  221. Queue<FileAction*> mFileActions;
  222. Queue<FileAction*> mActiveFileActions;
  223. BS_MUTEX(mMainMutex);
  224. BS_THREAD_TYPE* mWorkerThread;
  225. };
  226. FolderMonitor::FolderMonitor()
  227. {
  228. mPimpl = bs_new<Pimpl>();
  229. mPimpl->mWorkerThread = nullptr;
  230. mPimpl->mCompPortHandle = nullptr;
  231. }
  232. FolderMonitor::~FolderMonitor()
  233. {
  234. stopMonitorAll();
  235. // No need for mutex since we know worker thread is shut down by now
  236. while(!mPimpl->mFileActions.empty())
  237. {
  238. FileAction* action = mPimpl->mFileActions.front();
  239. mPimpl->mFileActions.pop();
  240. FileAction::destroy(action);
  241. }
  242. bs_delete(mPimpl);
  243. }
  244. void FolderMonitor::startMonitor(const Path& folderPath, bool subdirectories, FolderChange changeFilter)
  245. {
  246. if(!FileSystem::isDirectory(folderPath))
  247. {
  248. BS_EXCEPT(InvalidParametersException, "Provided path \"" + folderPath.toString() + "\" is not a directory");
  249. }
  250. WString extendedFolderPath = L"\\\\?\\" + folderPath.toWString(Path::PathType::Windows);
  251. HANDLE dirHandle = CreateFileW(extendedFolderPath.c_str(), FILE_LIST_DIRECTORY,
  252. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING,
  253. FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, nullptr);
  254. if(dirHandle == INVALID_HANDLE_VALUE)
  255. {
  256. BS_EXCEPT(InternalErrorException, "Failed to open folder \"" + folderPath.toString() + "\" for monitoring. Error code: " + toString((UINT64)GetLastError()));
  257. }
  258. DWORD filterFlags = 0;
  259. if((((UINT32)changeFilter) & (UINT32)BansheeEngine::FolderChange::FileName) != 0)
  260. filterFlags |= FILE_NOTIFY_CHANGE_FILE_NAME;
  261. if((((UINT32)changeFilter) & (UINT32)BansheeEngine::FolderChange::DirName) != 0)
  262. filterFlags |= FILE_NOTIFY_CHANGE_DIR_NAME;
  263. if((((UINT32)changeFilter) & (UINT32)BansheeEngine::FolderChange::Attributes) != 0)
  264. filterFlags |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
  265. if((((UINT32)changeFilter) & (UINT32)BansheeEngine::FolderChange::Size) != 0)
  266. filterFlags |= FILE_NOTIFY_CHANGE_SIZE;
  267. if((((UINT32)changeFilter) & (UINT32)BansheeEngine::FolderChange::LastWrite) != 0)
  268. filterFlags |= FILE_NOTIFY_CHANGE_LAST_WRITE;
  269. if((((UINT32)changeFilter) & (UINT32)BansheeEngine::FolderChange::LastAccess) != 0)
  270. filterFlags |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
  271. if((((UINT32)changeFilter) & (UINT32)BansheeEngine::FolderChange::Creation) != 0)
  272. filterFlags |= FILE_NOTIFY_CHANGE_CREATION;
  273. if((((UINT32)changeFilter) & (UINT32)BansheeEngine::FolderChange::Security) != 0)
  274. filterFlags |= FILE_NOTIFY_CHANGE_SECURITY;
  275. mPimpl->mFoldersToWatch.push_back(bs_new<FolderWatchInfo>(folderPath, dirHandle, subdirectories, filterFlags));
  276. FolderWatchInfo* watchInfo = mPimpl->mFoldersToWatch.back();
  277. mPimpl->mCompPortHandle = CreateIoCompletionPort(dirHandle, mPimpl->mCompPortHandle, (ULONG_PTR)watchInfo, 0);
  278. if(mPimpl->mCompPortHandle == nullptr)
  279. {
  280. mPimpl->mFoldersToWatch.erase(mPimpl->mFoldersToWatch.end() - 1);
  281. bs_delete(watchInfo);
  282. BS_EXCEPT(InternalErrorException, "Failed to open completition port for folder monitoring. Error code: " + toString((UINT64)GetLastError()));
  283. }
  284. if(mPimpl->mWorkerThread == nullptr)
  285. {
  286. BS_THREAD_CREATE(t, (std::bind(&FolderMonitor::workerThreadMain, this)));
  287. mPimpl->mWorkerThread = t;
  288. if(mPimpl->mWorkerThread == nullptr)
  289. {
  290. mPimpl->mFoldersToWatch.erase(mPimpl->mFoldersToWatch.end() - 1);
  291. bs_delete(watchInfo);
  292. BS_EXCEPT(InternalErrorException, "Failed to create a new worker thread for folder monitoring");
  293. }
  294. }
  295. if(mPimpl->mWorkerThread != nullptr)
  296. {
  297. try
  298. {
  299. watchInfo->startMonitor(mPimpl->mCompPortHandle);
  300. }
  301. catch (Exception* e)
  302. {
  303. mPimpl->mFoldersToWatch.erase(mPimpl->mFoldersToWatch.end() - 1);
  304. bs_delete(watchInfo);
  305. throw(e);
  306. }
  307. }
  308. else
  309. {
  310. mPimpl->mFoldersToWatch.erase(mPimpl->mFoldersToWatch.end() - 1);
  311. bs_delete(watchInfo);
  312. BS_EXCEPT(InternalErrorException, "Failed to create a new worker thread for folder monitoring");
  313. }
  314. }
  315. void FolderMonitor::stopMonitor(const Path& folderPath)
  316. {
  317. auto findIter = std::find_if(mPimpl->mFoldersToWatch.begin(), mPimpl->mFoldersToWatch.end(),
  318. [&](const FolderWatchInfo* x) { return x->mFolderToMonitor == folderPath; });
  319. if(findIter != mPimpl->mFoldersToWatch.end())
  320. {
  321. FolderWatchInfo* watchInfo = *findIter;
  322. watchInfo->stopMonitor(mPimpl->mCompPortHandle);
  323. bs_delete(watchInfo);
  324. mPimpl->mFoldersToWatch.erase(findIter);
  325. }
  326. if(mPimpl->mFoldersToWatch.size() == 0)
  327. stopMonitorAll();
  328. }
  329. void FolderMonitor::stopMonitorAll()
  330. {
  331. for(auto& watchInfo : mPimpl->mFoldersToWatch)
  332. {
  333. watchInfo->stopMonitor(mPimpl->mCompPortHandle);
  334. {
  335. // Note: Need this mutex to ensure worker thread is done with watchInfo.
  336. // Even though we wait for a condition variable from the worker thread in stopMonitor,
  337. // that doesn't mean the worker thread is done with the condition variable
  338. // (which is stored inside watchInfo)
  339. BS_LOCK_MUTEX(mPimpl->mMainMutex);
  340. bs_delete(watchInfo);
  341. }
  342. }
  343. mPimpl->mFoldersToWatch.clear();
  344. if(mPimpl->mWorkerThread != nullptr)
  345. {
  346. PostQueuedCompletionStatus(mPimpl->mCompPortHandle, 0, 0, nullptr);
  347. mPimpl->mWorkerThread->join();
  348. BS_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 FolderMonitor::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. BS_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. BS_LOCK_MUTEX(watchInfo->mStatusMutex);
  390. watchInfo->mState = MonitorState::Monitoring;
  391. }
  392. }
  393. BS_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. BS_LOCK_MUTEX(watchInfo->mStatusMutex);
  418. watchInfo->mState = MonitorState::Shutdown2;
  419. }
  420. }
  421. else
  422. {
  423. {
  424. BS_LOCK_MUTEX(watchInfo->mStatusMutex);
  425. watchInfo->mState = MonitorState::Inactive;
  426. }
  427. {
  428. BS_LOCK_MUTEX(mPimpl->mMainMutex); // Ensures that we don't delete "watchInfo" before this thread is done with mStartStopEvent
  429. BS_THREAD_NOTIFY_ONE(watchInfo->mStartStopEvent);
  430. }
  431. }
  432. break;
  433. case MonitorState::Shutdown2:
  434. if(watchInfo->mDirHandle != INVALID_HANDLE_VALUE)
  435. {
  436. // Handle is still open? Try again.
  437. CloseHandle(watchInfo->mDirHandle);
  438. watchInfo->mDirHandle = INVALID_HANDLE_VALUE;
  439. }
  440. else
  441. {
  442. {
  443. BS_LOCK_MUTEX(watchInfo->mStatusMutex);
  444. watchInfo->mState = MonitorState::Inactive;
  445. }
  446. {
  447. BS_LOCK_MUTEX(mPimpl->mMainMutex); // Ensures that we don't delete "watchInfo" before this thread is done with mStartStopEvent
  448. BS_THREAD_NOTIFY_ONE(watchInfo->mStartStopEvent);
  449. }
  450. }
  451. break;
  452. }
  453. }
  454. } while (watchInfo != nullptr);
  455. }
  456. void FolderMonitor::handleNotifications(FileNotifyInfo& notifyInfo, FolderWatchInfo& watchInfo)
  457. {
  458. Vector<FileAction*> mActions;
  459. do
  460. {
  461. switch(notifyInfo.getAction())
  462. {
  463. case FILE_ACTION_ADDED:
  464. {
  465. WString fileName = notifyInfo.getFileNameWithPath(watchInfo.mFolderToMonitor);
  466. mActions.push_back(FileAction::createAdded(fileName));
  467. }
  468. break;
  469. case FILE_ACTION_REMOVED:
  470. {
  471. WString fileName = notifyInfo.getFileNameWithPath(watchInfo.mFolderToMonitor);
  472. mActions.push_back(FileAction::createRemoved(fileName));
  473. }
  474. break;
  475. case FILE_ACTION_MODIFIED:
  476. {
  477. WString fileName = notifyInfo.getFileNameWithPath(watchInfo.mFolderToMonitor);
  478. mActions.push_back(FileAction::createModified(fileName));
  479. }
  480. break;
  481. case FILE_ACTION_RENAMED_OLD_NAME:
  482. {
  483. WString fileName = notifyInfo.getFileNameWithPath(watchInfo.mFolderToMonitor);
  484. watchInfo.mCachedOldFileName = fileName;
  485. }
  486. break;
  487. case FILE_ACTION_RENAMED_NEW_NAME:
  488. {
  489. WString fileName = notifyInfo.getFileNameWithPath(watchInfo.mFolderToMonitor);
  490. mActions.push_back(FileAction::createRenamed(watchInfo.mCachedOldFileName, fileName));
  491. }
  492. break;
  493. }
  494. } while(notifyInfo.getNext());
  495. {
  496. BS_LOCK_MUTEX(mPimpl->mMainMutex);
  497. for(auto& action : mActions)
  498. mPimpl->mFileActions.push(action);
  499. }
  500. }
  501. void FolderMonitor::_update()
  502. {
  503. {
  504. BS_LOCK_MUTEX(mPimpl->mMainMutex);
  505. mPimpl->mActiveFileActions.swap(mPimpl->mFileActions);
  506. }
  507. while(!mPimpl->mActiveFileActions.empty())
  508. {
  509. FileAction* action = mPimpl->mActiveFileActions.front();
  510. mPimpl->mActiveFileActions.pop();
  511. switch(action->type)
  512. {
  513. case FileActionType::Added:
  514. if(!onAdded.empty())
  515. onAdded(Path(action->newName));
  516. break;
  517. case FileActionType::Removed:
  518. if(!onRemoved.empty())
  519. onRemoved(Path(action->newName));
  520. break;
  521. case FileActionType::Modified:
  522. if(!onModified.empty())
  523. onModified(Path(action->newName));
  524. break;
  525. case FileActionType::Renamed:
  526. if(!onRenamed.empty())
  527. onRenamed(Path(action->oldName), Path(action->newName));
  528. break;
  529. }
  530. FileAction::destroy(action);
  531. }
  532. }
  533. }