FileWatcher.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. fileSystem_(GetSubsystem<FileSystem>()),
  36. watchSubDirs_(false)
  37. {
  38. }
  39. FileWatcher::~FileWatcher()
  40. {
  41. StopWatching();
  42. }
  43. bool FileWatcher::StartWatching(const String& pathName, bool watchSubDirs)
  44. {
  45. if (!fileSystem_)
  46. {
  47. LOGERROR("No FileSystem, can not start watching");
  48. return false;
  49. }
  50. // Stop any previous watching
  51. StopWatching();
  52. #if defined(WIN32) && defined(ENABLE_FILEWATCHER)
  53. String nativePath = GetNativePath(RemoveTrailingSlash(pathName));
  54. dirHandle_ = (void*)CreateFileW(
  55. WString(nativePath).CString(),
  56. FILE_LIST_DIRECTORY,
  57. FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
  58. 0,
  59. OPEN_EXISTING,
  60. FILE_FLAG_BACKUP_SEMANTICS,
  61. 0);
  62. if (dirHandle_ != INVALID_HANDLE_VALUE)
  63. {
  64. path_ = AddTrailingSlash(pathName);
  65. watchSubDirs_ = watchSubDirs;
  66. Start();
  67. LOGDEBUG("Started watching path " + pathName);
  68. return true;
  69. }
  70. else
  71. {
  72. LOGERROR("Failed to start watching path " + pathName);
  73. return false;
  74. }
  75. #else
  76. /// \todo Implement on Unix-like systems
  77. LOGERROR("FileWatcher not implemented, can not start watching path " + pathName);
  78. return false;
  79. #endif
  80. }
  81. void FileWatcher::StopWatching()
  82. {
  83. if (handle_)
  84. {
  85. shouldRun_ = false;
  86. // Create and delete a dummy file to make sure the watcher loop terminates
  87. String dummyFileName = path_ + "dummy.tmp";
  88. File file(context_, dummyFileName, FILE_WRITE);
  89. file.Close();
  90. if (fileSystem_)
  91. fileSystem_->Delete(dummyFileName);
  92. Stop();
  93. #ifdef WIN32
  94. CloseHandle((HANDLE)dirHandle_);
  95. #endif
  96. LOGDEBUG("Stopped watching path " + path_);
  97. path_.Clear();
  98. }
  99. }
  100. void FileWatcher::ThreadFunction()
  101. {
  102. #if defined(WIN32) && defined(ENABLE_FILEWATCHER)
  103. unsigned char buffer[BUFFERSIZE];
  104. DWORD bytesFilled = 0;
  105. while (shouldRun_)
  106. {
  107. if (ReadDirectoryChangesW((HANDLE)dirHandle_,
  108. buffer,
  109. BUFFERSIZE,
  110. watchSubDirs_,
  111. FILE_NOTIFY_CHANGE_FILE_NAME |
  112. FILE_NOTIFY_CHANGE_LAST_WRITE,
  113. &bytesFilled,
  114. 0,
  115. 0))
  116. {
  117. unsigned offset = 0;
  118. while (offset < bytesFilled)
  119. {
  120. FILE_NOTIFY_INFORMATION* record = (FILE_NOTIFY_INFORMATION*)&buffer[offset];
  121. if (record->Action == FILE_ACTION_MODIFIED) // Modify
  122. {
  123. String fileName;
  124. const wchar_t* src = record->FileName;
  125. const wchar_t* end = src + record->FileNameLength / 2;
  126. while (src < end)
  127. fileName.AppendUTF8(String::DecodeUTF16(src));
  128. fileName = GetInternalPath(fileName);
  129. {
  130. MutexLock lock(changesMutex_);
  131. // If we have 2 unprocessed modifies in a row into the same file, only record the first
  132. if (changes_.Empty() || changes_.Back() != fileName)
  133. changes_.Push(fileName);
  134. }
  135. }
  136. if (!record->NextEntryOffset)
  137. break;
  138. else
  139. offset += record->NextEntryOffset;
  140. }
  141. }
  142. }
  143. #endif
  144. }
  145. bool FileWatcher::GetNextChange(String& dest)
  146. {
  147. MutexLock lock(changesMutex_);
  148. if (changes_.Empty())
  149. return false;
  150. else
  151. {
  152. dest = changes_.Front();
  153. changes_.Erase(changes_.Begin());
  154. return true;
  155. }
  156. }