FileWatcher.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #endif
  79. }
  80. void FileWatcher::StopWatching()
  81. {
  82. if (handle_)
  83. {
  84. shouldRun_ = false;
  85. // Create and delete a dummy file to make sure the watcher loop terminates
  86. String dummyFileName = path_ + "dummy.tmp";
  87. File file(context_, dummyFileName, FILE_WRITE);
  88. file.Close();
  89. if (fileSystem_)
  90. fileSystem_->Delete(dummyFileName);
  91. Stop();
  92. #ifdef WIN32
  93. CloseHandle((HANDLE)dirHandle_);
  94. #endif
  95. LOGDEBUG("Stopped watching path " + path_);
  96. path_.Clear();
  97. }
  98. }
  99. void FileWatcher::ThreadFunction()
  100. {
  101. #if defined(WIN32) && defined(ENABLE_FILEWATCHER)
  102. unsigned char buffer[BUFFERSIZE];
  103. DWORD bytesFilled = 0;
  104. while (shouldRun_)
  105. {
  106. if (ReadDirectoryChangesW((HANDLE)dirHandle_,
  107. buffer,
  108. BUFFERSIZE,
  109. watchSubDirs_,
  110. FILE_NOTIFY_CHANGE_FILE_NAME |
  111. FILE_NOTIFY_CHANGE_LAST_WRITE,
  112. &bytesFilled,
  113. 0,
  114. 0))
  115. {
  116. unsigned offset = 0;
  117. while (offset < bytesFilled)
  118. {
  119. FILE_NOTIFY_INFORMATION* record = (FILE_NOTIFY_INFORMATION*)&buffer[offset];
  120. if (record->Action == FILE_ACTION_MODIFIED) // Modify
  121. {
  122. String fileName;
  123. const wchar_t* src = record->FileName;
  124. const wchar_t* end = src + record->FileNameLength / 2;
  125. while (src < end)
  126. fileName.AppendUTF8(String::DecodeUTF16(src));
  127. fileName = GetInternalPath(fileName);
  128. {
  129. MutexLock lock(changesMutex_);
  130. // If we have 2 unprocessed modifies in a row into the same file, only record the first
  131. if (changes_.Empty() || changes_.Back() != fileName)
  132. changes_.Push(fileName);
  133. }
  134. }
  135. if (!record->NextEntryOffset)
  136. break;
  137. else
  138. offset += record->NextEntryOffset;
  139. }
  140. }
  141. }
  142. #endif
  143. }
  144. bool FileWatcher::GetNextChange(String& dest)
  145. {
  146. MutexLock lock(changesMutex_);
  147. if (changes_.Empty())
  148. return false;
  149. else
  150. {
  151. dest = changes_.Front();
  152. changes_.Erase(changes_.Begin());
  153. return true;
  154. }
  155. }