BsWin32FolderMonitor.cpp 20 KB

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