FileWatcher.cpp 5.4 KB

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