FileWatcher.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "File.h"
  25. #include "FileSystem.h"
  26. #include "FileWatcher.h"
  27. #include "Log.h"
  28. #ifdef WIN32
  29. #include <windows.h>
  30. static const unsigned BUFFERSIZE = 4096;
  31. #endif
  32. OBJECTTYPESTATIC(FileWatcher);
  33. FileWatcher::FileWatcher(Context* context) :
  34. Object(context),
  35. watchSubDirs_(false)
  36. {
  37. }
  38. FileWatcher::~FileWatcher()
  39. {
  40. StopWatching();
  41. }
  42. bool FileWatcher::StartWatching(const String& pathName, bool watchSubDirs)
  43. {
  44. // Stop any previous watching
  45. StopWatching();
  46. #if defined(WIN32) && defined(ENABLE_FILEWATCHER)
  47. String nativePath = GetNativePath(RemoveTrailingSlash(pathName));
  48. dirHandle_ = (void*)CreateFileW(
  49. WString(nativePath).CString(),
  50. FILE_LIST_DIRECTORY,
  51. FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
  52. 0,
  53. OPEN_EXISTING,
  54. FILE_FLAG_BACKUP_SEMANTICS,
  55. 0);
  56. if (dirHandle_ != INVALID_HANDLE_VALUE)
  57. {
  58. path_ = AddTrailingSlash(pathName);
  59. watchSubDirs_ = watchSubDirs;
  60. Start();
  61. LOGDEBUG("Started watching path " + pathName);
  62. return true;
  63. }
  64. else
  65. {
  66. LOGERROR("Failed to start watching path " + pathName);
  67. return false;
  68. }
  69. #else
  70. /// \todo Implement on Unix-like systems
  71. LOGERROR("FileWatcher not implemented, can not start watching path " + pathName);
  72. #endif
  73. }
  74. void FileWatcher::StopWatching()
  75. {
  76. if (handle_)
  77. {
  78. shouldRun_ = false;
  79. // Create and delete a dummy file to make sure the watcher loop terminates
  80. String dummyFileName = path_ + "dummy.tmp";
  81. File file(context_, dummyFileName, FILE_WRITE);
  82. file.Close();
  83. GetSubsystem<FileSystem>()->Delete(dummyFileName);
  84. Stop();
  85. #ifdef WIN32
  86. CloseHandle((HANDLE)dirHandle_);
  87. #endif
  88. LOGDEBUG("Stopped watching path " + path_);
  89. path_.Clear();
  90. }
  91. }
  92. void FileWatcher::ThreadFunction()
  93. {
  94. #if defined(WIN32) && defined(ENABLE_FILEWATCHER)
  95. unsigned char buffer[BUFFERSIZE];
  96. DWORD bytesFilled = 0;
  97. while (shouldRun_)
  98. {
  99. if (ReadDirectoryChangesW((HANDLE)dirHandle_,
  100. buffer,
  101. BUFFERSIZE,
  102. watchSubDirs_,
  103. FILE_NOTIFY_CHANGE_FILE_NAME |
  104. FILE_NOTIFY_CHANGE_LAST_WRITE,
  105. &bytesFilled,
  106. 0,
  107. 0))
  108. {
  109. unsigned offset = 0;
  110. while (offset < bytesFilled)
  111. {
  112. FILE_NOTIFY_INFORMATION* record = (FILE_NOTIFY_INFORMATION*)&buffer[offset];
  113. if (record->Action == FILE_ACTION_MODIFIED) // Modify
  114. {
  115. String fileName;
  116. const wchar_t* src = record->FileName;
  117. const wchar_t* end = src + record->FileNameLength / 2;
  118. while (src < end)
  119. fileName.AppendUTF8(String::DecodeUTF16(src));
  120. fileName = GetInternalPath(fileName);
  121. {
  122. MutexLock lock(changesMutex_);
  123. // If we have 2 unprocessed modifies in a row into the same file, only record the first
  124. if (changes_.Empty() || changes_.Back() != fileName)
  125. changes_.Push(fileName);
  126. }
  127. }
  128. if (!record->NextEntryOffset)
  129. break;
  130. else
  131. offset += record->NextEntryOffset;
  132. }
  133. }
  134. }
  135. #endif
  136. }
  137. bool FileWatcher::GetNextChange(String& dest)
  138. {
  139. MutexLock lock(changesMutex_);
  140. if (changes_.Empty())
  141. return false;
  142. else
  143. {
  144. dest = changes_.Front();
  145. changes_.Erase(changes_.Begin());
  146. return true;
  147. }
  148. }