BsWin32FolderMonitor.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #include "Win32/BsWin32FolderMonitor.h"
  5. #include "BsFileSystem.h"
  6. #include "BsException.h"
  7. #include "BsPath.h"
  8. #include "BsDebug.h"
  9. #include <windows.h>
  10. namespace BansheeEngine
  11. {
  12. enum class MonitorState
  13. {
  14. Inactive,
  15. Starting,
  16. Monitoring,
  17. Shutdown,
  18. Shutdown2
  19. };
  20. class WorkerFunc
  21. {
  22. public:
  23. WorkerFunc(FolderMonitor* owner);
  24. void operator()();
  25. private:
  26. FolderMonitor* mOwner;
  27. };
  28. struct FolderMonitor::FolderWatchInfo
  29. {
  30. FolderWatchInfo(const Path& folderToMonitor, HANDLE dirHandle, bool monitorSubdirectories, DWORD monitorFlags);
  31. ~FolderWatchInfo();
  32. void startMonitor(HANDLE compPortHandle);
  33. void stopMonitor(HANDLE compPortHandle);
  34. static const UINT32 READ_BUFFER_SIZE = 65536;
  35. Path mFolderToMonitor;
  36. HANDLE mDirHandle;
  37. OVERLAPPED mOverlapped;
  38. MonitorState mState;
  39. UINT8 mBuffer[READ_BUFFER_SIZE];
  40. DWORD mBufferSize;
  41. bool mMonitorSubdirectories;
  42. DWORD mMonitorFlags;
  43. DWORD mReadError;
  44. WString mCachedOldFileName; // Used during rename notifications as they are handled in two steps
  45. BS_MUTEX(mStatusMutex)
  46. BS_THREAD_SYNCHRONISER(mStartStopEvent)
  47. };
  48. FolderMonitor::FolderWatchInfo::FolderWatchInfo(const Path& folderToMonitor, HANDLE dirHandle, bool monitorSubdirectories, DWORD monitorFlags)
  49. :mFolderToMonitor(folderToMonitor), mDirHandle(dirHandle), mState(MonitorState::Inactive), mBufferSize(0),
  50. mMonitorSubdirectories(monitorSubdirectories), mMonitorFlags(monitorFlags), mReadError(0)
  51. {
  52. memset(&mOverlapped, 0, sizeof(mOverlapped));
  53. }
  54. FolderMonitor::FolderWatchInfo::~FolderWatchInfo()
  55. {
  56. assert(mState == MonitorState::Inactive);
  57. stopMonitor(0);
  58. }
  59. void FolderMonitor::FolderWatchInfo::startMonitor(HANDLE compPortHandle)
  60. {
  61. if(mState != MonitorState::Inactive)
  62. return; // Already monitoring
  63. {
  64. BS_LOCK_MUTEX_NAMED(mStatusMutex, lock);
  65. mState = MonitorState::Starting;
  66. PostQueuedCompletionStatus(compPortHandle, sizeof(this), (DWORD)this, &mOverlapped);
  67. while(mState != MonitorState::Monitoring)
  68. BS_THREAD_WAIT(mStartStopEvent, mStatusMutex, lock);
  69. }
  70. if(mReadError != ERROR_SUCCESS)
  71. {
  72. {
  73. BS_LOCK_MUTEX(mStatusMutex);
  74. mState = MonitorState::Inactive;
  75. }
  76. BS_EXCEPT(InternalErrorException, "Failed to start folder monitor on folder \"" +
  77. mFolderToMonitor.toString() + "\" because ReadDirectoryChangesW failed.");
  78. }
  79. }
  80. void FolderMonitor::FolderWatchInfo::stopMonitor(HANDLE compPortHandle)
  81. {
  82. if(mState != MonitorState::Inactive)
  83. {
  84. BS_LOCK_MUTEX_NAMED(mStatusMutex, lock);
  85. mState = MonitorState::Shutdown;
  86. PostQueuedCompletionStatus(compPortHandle, sizeof(this), (DWORD)this, &mOverlapped);
  87. while(mState != MonitorState::Inactive)
  88. BS_THREAD_WAIT(mStartStopEvent, mStatusMutex, lock);
  89. }
  90. if(mDirHandle != INVALID_HANDLE_VALUE)
  91. {
  92. CloseHandle(mDirHandle);
  93. mDirHandle = INVALID_HANDLE_VALUE;
  94. }
  95. }
  96. class FolderMonitor::FileNotifyInfo
  97. {
  98. public:
  99. FileNotifyInfo(UINT8* notifyBuffer, DWORD bufferSize)
  100. :mBuffer(notifyBuffer), mBufferSize(bufferSize)
  101. {
  102. mCurrentRecord = (PFILE_NOTIFY_INFORMATION)mBuffer;
  103. }
  104. bool getNext();
  105. DWORD getAction() const;
  106. WString getFileName() const;
  107. WString getFileNameWithPath(const Path& rootPath) const;
  108. protected:
  109. UINT8* mBuffer;
  110. DWORD mBufferSize;
  111. PFILE_NOTIFY_INFORMATION mCurrentRecord;
  112. };
  113. bool FolderMonitor::FileNotifyInfo::getNext()
  114. {
  115. if(mCurrentRecord && mCurrentRecord->NextEntryOffset != 0)
  116. {
  117. PFILE_NOTIFY_INFORMATION oldRecord = mCurrentRecord;
  118. mCurrentRecord = (PFILE_NOTIFY_INFORMATION) ((UINT8*)mCurrentRecord + mCurrentRecord->NextEntryOffset);
  119. if((DWORD)((UINT8*)mCurrentRecord - mBuffer) > mBufferSize)
  120. {
  121. // Gone out of range, something bad happened
  122. assert(false);
  123. mCurrentRecord = oldRecord;
  124. }
  125. return (mCurrentRecord != oldRecord);
  126. }
  127. return false;
  128. }
  129. DWORD FolderMonitor::FileNotifyInfo::getAction() const
  130. {
  131. assert(mCurrentRecord != nullptr);
  132. if(mCurrentRecord)
  133. return mCurrentRecord->Action;
  134. return 0;
  135. }
  136. WString FolderMonitor::FileNotifyInfo::getFileName() const
  137. {
  138. if(mCurrentRecord)
  139. {
  140. wchar_t fileNameBuffer[32768 + 1] = {0};
  141. memcpy(fileNameBuffer, mCurrentRecord->FileName,
  142. std::min(DWORD(32768 * sizeof(wchar_t)), mCurrentRecord->FileNameLength));
  143. return WString(fileNameBuffer);
  144. }
  145. return WString();
  146. }
  147. WString FolderMonitor::FileNotifyInfo::getFileNameWithPath(const Path& rootPath) const
  148. {
  149. Path fullPath = rootPath;
  150. return fullPath.append(getFileName()).toWString();
  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*)bs_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*)bs_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*)bs_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*)bs_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. bs_free(action);
  216. }
  217. WString::value_type* oldName;
  218. WString::value_type* newName;
  219. FileActionType type;
  220. };
  221. struct FolderMonitor::Pimpl
  222. {
  223. Vector<FolderWatchInfo*> mFoldersToWatch;
  224. HANDLE mCompPortHandle;
  225. Queue<FileAction*> mFileActions;
  226. Queue<FileAction*> mActiveFileActions;
  227. BS_MUTEX(mMainMutex);
  228. BS_THREAD_TYPE* mWorkerThread;
  229. };
  230. FolderMonitor::FolderMonitor()
  231. {
  232. mPimpl = bs_new<Pimpl>();
  233. mPimpl->mWorkerThread = nullptr;
  234. mPimpl->mCompPortHandle = nullptr;
  235. }
  236. FolderMonitor::~FolderMonitor()
  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. bs_delete(mPimpl);
  247. }
  248. void FolderMonitor::startMonitor(const Path& folderPath, bool subdirectories, FolderChange changeFilter)
  249. {
  250. if(!FileSystem::isDirectory(folderPath))
  251. {
  252. BS_EXCEPT(InvalidParametersException, "Provided path \"" + folderPath.toString() + "\" is not a directory");
  253. }
  254. WString extendedFolderPath = L"\\\\?\\" + folderPath.toWString(Path::PathType::Windows);
  255. HANDLE dirHandle = CreateFileW(extendedFolderPath.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. BS_EXCEPT(InternalErrorException, "Failed to open folder \"" + folderPath.toString() + "\" for monitoring. Error code: " + toString((UINT64)GetLastError()));
  261. }
  262. DWORD filterFlags = 0;
  263. if((((UINT32)changeFilter) & (UINT32)BansheeEngine::FolderChange::FileName) != 0)
  264. filterFlags |= FILE_NOTIFY_CHANGE_FILE_NAME;
  265. if((((UINT32)changeFilter) & (UINT32)BansheeEngine::FolderChange::DirName) != 0)
  266. filterFlags |= FILE_NOTIFY_CHANGE_DIR_NAME;
  267. if((((UINT32)changeFilter) & (UINT32)BansheeEngine::FolderChange::Attributes) != 0)
  268. filterFlags |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
  269. if((((UINT32)changeFilter) & (UINT32)BansheeEngine::FolderChange::Size) != 0)
  270. filterFlags |= FILE_NOTIFY_CHANGE_SIZE;
  271. if((((UINT32)changeFilter) & (UINT32)BansheeEngine::FolderChange::LastWrite) != 0)
  272. filterFlags |= FILE_NOTIFY_CHANGE_LAST_WRITE;
  273. if((((UINT32)changeFilter) & (UINT32)BansheeEngine::FolderChange::LastAccess) != 0)
  274. filterFlags |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
  275. if((((UINT32)changeFilter) & (UINT32)BansheeEngine::FolderChange::Creation) != 0)
  276. filterFlags |= FILE_NOTIFY_CHANGE_CREATION;
  277. if((((UINT32)changeFilter) & (UINT32)BansheeEngine::FolderChange::Security) != 0)
  278. filterFlags |= FILE_NOTIFY_CHANGE_SECURITY;
  279. mPimpl->mFoldersToWatch.push_back(bs_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. bs_delete(watchInfo);
  286. BS_EXCEPT(InternalErrorException, "Failed to open completition port for folder monitoring. Error code: " + toString((UINT64)GetLastError()));
  287. }
  288. if(mPimpl->mWorkerThread == nullptr)
  289. {
  290. BS_THREAD_CREATE(t, (std::bind(&FolderMonitor::workerThreadMain, this)));
  291. mPimpl->mWorkerThread = t;
  292. if(mPimpl->mWorkerThread == nullptr)
  293. {
  294. mPimpl->mFoldersToWatch.erase(mPimpl->mFoldersToWatch.end() - 1);
  295. bs_delete(watchInfo);
  296. BS_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. bs_delete(watchInfo);
  309. throw(e);
  310. }
  311. }
  312. else
  313. {
  314. mPimpl->mFoldersToWatch.erase(mPimpl->mFoldersToWatch.end() - 1);
  315. bs_delete(watchInfo);
  316. BS_EXCEPT(InternalErrorException, "Failed to create a new worker thread for folder monitoring");
  317. }
  318. }
  319. void FolderMonitor::stopMonitor(const Path& folderPath)
  320. {
  321. auto findIter = std::find_if(mPimpl->mFoldersToWatch.begin(), mPimpl->mFoldersToWatch.end(),
  322. [&](const FolderWatchInfo* x) { return x->mFolderToMonitor == folderPath; });
  323. if(findIter != mPimpl->mFoldersToWatch.end())
  324. {
  325. FolderWatchInfo* watchInfo = *findIter;
  326. watchInfo->stopMonitor(mPimpl->mCompPortHandle);
  327. bs_delete(watchInfo);
  328. mPimpl->mFoldersToWatch.erase(findIter);
  329. }
  330. if(mPimpl->mFoldersToWatch.size() == 0)
  331. stopMonitorAll();
  332. }
  333. void FolderMonitor::stopMonitorAll()
  334. {
  335. for(auto& watchInfo : mPimpl->mFoldersToWatch)
  336. {
  337. watchInfo->stopMonitor(mPimpl->mCompPortHandle);
  338. {
  339. // Note: Need this mutex to ensure worker thread is done with watchInfo.
  340. // Even though we wait for a condition variable from the worker thread in stopMonitor,
  341. // that doesn't mean the worker thread is done with the condition variable
  342. // (which is stored inside watchInfo)
  343. BS_LOCK_MUTEX(mPimpl->mMainMutex);
  344. bs_delete(watchInfo);
  345. }
  346. }
  347. mPimpl->mFoldersToWatch.clear();
  348. if(mPimpl->mWorkerThread != nullptr)
  349. {
  350. PostQueuedCompletionStatus(mPimpl->mCompPortHandle, 0, 0, nullptr);
  351. mPimpl->mWorkerThread->join();
  352. BS_THREAD_DESTROY(mPimpl->mWorkerThread);
  353. mPimpl->mWorkerThread = nullptr;
  354. }
  355. if(mPimpl->mCompPortHandle != nullptr)
  356. {
  357. CloseHandle(mPimpl->mCompPortHandle);
  358. mPimpl->mCompPortHandle = nullptr;
  359. }
  360. }
  361. void FolderMonitor::workerThreadMain()
  362. {
  363. FolderWatchInfo* watchInfo = nullptr;
  364. do
  365. {
  366. DWORD numBytes;
  367. LPOVERLAPPED overlapped;
  368. if(!GetQueuedCompletionStatus(mPimpl->mCompPortHandle, &numBytes, (PULONG_PTR) &watchInfo, &overlapped, INFINITE))
  369. {
  370. assert(false);
  371. // TODO: Folder handle was lost most likely. Not sure how to deal with that. Shutdown watch on this folder and cleanup?
  372. }
  373. if(watchInfo != nullptr)
  374. {
  375. MonitorState state;
  376. {
  377. BS_LOCK_MUTEX(watchInfo->mStatusMutex);
  378. state = watchInfo->mState;
  379. }
  380. switch(state)
  381. {
  382. case MonitorState::Starting:
  383. if(!ReadDirectoryChangesW(watchInfo->mDirHandle, watchInfo->mBuffer, FolderWatchInfo::READ_BUFFER_SIZE,
  384. watchInfo->mMonitorSubdirectories, watchInfo->mMonitorFlags, &watchInfo->mBufferSize, &watchInfo->mOverlapped, nullptr))
  385. {
  386. assert(false); // TODO - Possibly the buffer was too small?
  387. watchInfo->mReadError = GetLastError();
  388. }
  389. else
  390. {
  391. watchInfo->mReadError = ERROR_SUCCESS;
  392. {
  393. BS_LOCK_MUTEX(watchInfo->mStatusMutex);
  394. watchInfo->mState = MonitorState::Monitoring;
  395. }
  396. }
  397. BS_THREAD_NOTIFY_ONE(watchInfo->mStartStopEvent);
  398. break;
  399. case MonitorState::Monitoring:
  400. {
  401. FileNotifyInfo info(watchInfo->mBuffer, FolderWatchInfo::READ_BUFFER_SIZE);
  402. handleNotifications(info, *watchInfo);
  403. if(!ReadDirectoryChangesW(watchInfo->mDirHandle, watchInfo->mBuffer, FolderWatchInfo::READ_BUFFER_SIZE,
  404. watchInfo->mMonitorSubdirectories, watchInfo->mMonitorFlags, &watchInfo->mBufferSize, &watchInfo->mOverlapped, nullptr))
  405. {
  406. assert(false); // TODO: Failed during normal operation, possibly the buffer was too small. Shutdown watch on this folder and cleanup?
  407. watchInfo->mReadError = GetLastError();
  408. }
  409. else
  410. {
  411. watchInfo->mReadError = ERROR_SUCCESS;
  412. }
  413. }
  414. break;
  415. case MonitorState::Shutdown:
  416. if(watchInfo->mDirHandle != INVALID_HANDLE_VALUE)
  417. {
  418. CloseHandle(watchInfo->mDirHandle);
  419. watchInfo->mDirHandle = INVALID_HANDLE_VALUE;
  420. {
  421. BS_LOCK_MUTEX(watchInfo->mStatusMutex);
  422. watchInfo->mState = MonitorState::Shutdown2;
  423. }
  424. }
  425. else
  426. {
  427. {
  428. BS_LOCK_MUTEX(watchInfo->mStatusMutex);
  429. watchInfo->mState = MonitorState::Inactive;
  430. }
  431. {
  432. BS_LOCK_MUTEX(mPimpl->mMainMutex); // Ensures that we don't delete "watchInfo" before this thread is done with mStartStopEvent
  433. BS_THREAD_NOTIFY_ONE(watchInfo->mStartStopEvent);
  434. }
  435. }
  436. break;
  437. case MonitorState::Shutdown2:
  438. if(watchInfo->mDirHandle != INVALID_HANDLE_VALUE)
  439. {
  440. // Handle is still open? Try again.
  441. CloseHandle(watchInfo->mDirHandle);
  442. watchInfo->mDirHandle = INVALID_HANDLE_VALUE;
  443. }
  444. else
  445. {
  446. {
  447. BS_LOCK_MUTEX(watchInfo->mStatusMutex);
  448. watchInfo->mState = MonitorState::Inactive;
  449. }
  450. {
  451. BS_LOCK_MUTEX(mPimpl->mMainMutex); // Ensures that we don't delete "watchInfo" before this thread is done with mStartStopEvent
  452. BS_THREAD_NOTIFY_ONE(watchInfo->mStartStopEvent);
  453. }
  454. }
  455. break;
  456. }
  457. }
  458. } while (watchInfo != nullptr);
  459. }
  460. void FolderMonitor::handleNotifications(FileNotifyInfo& notifyInfo, FolderWatchInfo& watchInfo)
  461. {
  462. Vector<FileAction*> mActions;
  463. do
  464. {
  465. switch(notifyInfo.getAction())
  466. {
  467. case FILE_ACTION_ADDED:
  468. {
  469. WString fileName = notifyInfo.getFileNameWithPath(watchInfo.mFolderToMonitor);
  470. mActions.push_back(FileAction::createAdded(fileName));
  471. }
  472. break;
  473. case FILE_ACTION_REMOVED:
  474. {
  475. WString fileName = notifyInfo.getFileNameWithPath(watchInfo.mFolderToMonitor);
  476. mActions.push_back(FileAction::createRemoved(fileName));
  477. }
  478. break;
  479. case FILE_ACTION_MODIFIED:
  480. {
  481. WString fileName = notifyInfo.getFileNameWithPath(watchInfo.mFolderToMonitor);
  482. mActions.push_back(FileAction::createModified(fileName));
  483. }
  484. break;
  485. case FILE_ACTION_RENAMED_OLD_NAME:
  486. {
  487. WString fileName = notifyInfo.getFileNameWithPath(watchInfo.mFolderToMonitor);
  488. watchInfo.mCachedOldFileName = fileName;
  489. }
  490. break;
  491. case FILE_ACTION_RENAMED_NEW_NAME:
  492. {
  493. WString fileName = notifyInfo.getFileNameWithPath(watchInfo.mFolderToMonitor);
  494. mActions.push_back(FileAction::createRenamed(watchInfo.mCachedOldFileName, fileName));
  495. }
  496. break;
  497. }
  498. } while(notifyInfo.getNext());
  499. {
  500. BS_LOCK_MUTEX(mPimpl->mMainMutex);
  501. for(auto& action : mActions)
  502. mPimpl->mFileActions.push(action);
  503. }
  504. }
  505. void FolderMonitor::_update()
  506. {
  507. {
  508. BS_LOCK_MUTEX(mPimpl->mMainMutex);
  509. mPimpl->mActiveFileActions.swap(mPimpl->mFileActions);
  510. }
  511. while(!mPimpl->mActiveFileActions.empty())
  512. {
  513. FileAction* action = mPimpl->mActiveFileActions.front();
  514. mPimpl->mActiveFileActions.pop();
  515. switch(action->type)
  516. {
  517. case FileActionType::Added:
  518. if(!onAdded.empty())
  519. onAdded(Path(action->newName));
  520. break;
  521. case FileActionType::Removed:
  522. if(!onRemoved.empty())
  523. onRemoved(Path(action->newName));
  524. break;
  525. case FileActionType::Modified:
  526. if(!onModified.empty())
  527. onModified(Path(action->newName));
  528. break;
  529. case FileActionType::Renamed:
  530. if(!onRenamed.empty())
  531. onRenamed(Path(action->oldName), Path(action->newName));
  532. break;
  533. }
  534. FileAction::destroy(action);
  535. }
  536. }
  537. }