FileWatcher.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "FileWatcher.h"
  26. #include "FileSystem.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& path, bool watchSubDirs)
  43. {
  44. // Stop any previous watching
  45. StopWatching();
  46. #ifdef WIN32
  47. String nativePath = GetNativePath(RemoveTrailingSlash(path));
  48. dirHandle_ = (void*)CreateFile(
  49. 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(path);
  59. watchSubDirs_ = watchSubDirs;
  60. Start();
  61. LOGDEBUG("Started watching path " + path);
  62. return true;
  63. }
  64. else
  65. {
  66. LOGERROR("Failed to start watching path " + path);
  67. return false;
  68. }
  69. #else
  70. LOGERROR("Can not start watching path " + path + ", FileWatcher not implemented yet");
  71. #endif
  72. }
  73. void FileWatcher::StopWatching()
  74. {
  75. if (handle_)
  76. {
  77. shouldRun_ = false;
  78. // Create and delete a dummy file to make sure the watcher loop terminates
  79. String dummyFileName = path_ + "dummy.tmp";
  80. File file(context_, dummyFileName, FILE_WRITE);
  81. file.Close();
  82. GetSubsystem<FileSystem>()->Delete(dummyFileName);
  83. Stop();
  84. #ifdef WIN32
  85. CloseHandle((HANDLE)dirHandle_);
  86. #endif
  87. LOGDEBUG("Stopped watching path " + path_);
  88. path_.Clear();
  89. }
  90. }
  91. void FileWatcher::ThreadFunction()
  92. {
  93. #ifdef WIN32
  94. char buffer[BUFFERSIZE];
  95. DWORD bytesFilled = 0;
  96. while (shouldRun_)
  97. {
  98. if (ReadDirectoryChangesW((HANDLE)dirHandle_,
  99. buffer,
  100. BUFFERSIZE,
  101. watchSubDirs_,
  102. FILE_NOTIFY_CHANGE_FILE_NAME |
  103. FILE_NOTIFY_CHANGE_LAST_WRITE,
  104. &bytesFilled,
  105. 0,
  106. 0))
  107. {
  108. unsigned offset = 0;
  109. for (;;)
  110. {
  111. FILE_NOTIFY_INFORMATION* record = (FILE_NOTIFY_INFORMATION*)&buffer[offset];
  112. if (record->Action == FILE_ACTION_MODIFIED) // Modify
  113. {
  114. String fileName;
  115. fileName.Resize(record->FileNameLength / 2);
  116. /// \todo Proper Unicode filename support
  117. for (unsigned i = 0; i < record->FileNameLength / 2; ++i)
  118. fileName[i] = (char)record->FileName[i];
  119. fileName = GetInternalPath(fileName);
  120. {
  121. MutexLock lock(changesMutex_);
  122. // If we have 2 unprocessed modifies in a row into the same file, only record the first
  123. if (changes_.Empty() || changes_.Back() != fileName)
  124. changes_.Push(fileName);
  125. }
  126. }
  127. if (!record->NextEntryOffset)
  128. break;
  129. else
  130. offset += record->NextEntryOffset;
  131. }
  132. }
  133. }
  134. #endif
  135. }
  136. bool FileWatcher::GetNextChange(String& dest)
  137. {
  138. MutexLock lock(changesMutex_);
  139. if (changes_.Empty())
  140. return false;
  141. else
  142. {
  143. dest = changes_.Front();
  144. changes_.Erase(changes_.Begin());
  145. return true;
  146. }
  147. }