FileWatcher.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Container/List.h"
  24. #include "../Core/Mutex.h"
  25. #include "../Core/Object.h"
  26. #include "../Core/Thread.h"
  27. #include "../Core/Timer.h"
  28. namespace Atomic
  29. {
  30. class FileSystem;
  31. /// Watches a directory and its subdirectories for files being modified.
  32. class ATOMIC_API FileWatcher : public Object, public Thread
  33. {
  34. ATOMIC_OBJECT(FileWatcher, Object);
  35. public:
  36. /// Construct.
  37. FileWatcher(Context* context);
  38. /// Destruct.
  39. virtual ~FileWatcher();
  40. /// Directory watching loop.
  41. virtual void ThreadFunction();
  42. /// Start watching a directory. Return true if successful.
  43. bool StartWatching(const String& pathName, bool watchSubDirs);
  44. /// Stop watching the directory.
  45. void StopWatching();
  46. /// Set the delay in seconds before file changes are notified. This (hopefully) avoids notifying when a file save is still in progress. Default 1 second.
  47. void SetDelay(float interval);
  48. /// Add a file change into the changes queue.
  49. void AddChange(const String& fileName);
  50. /// Return a file change (true if was found, false if not.)
  51. bool GetNextChange(String& dest);
  52. /// Return the path being watched, or empty if not watching.
  53. const String& GetPath() const { return path_; }
  54. /// Return the delay in seconds for notifying file changes.
  55. float GetDelay() const { return delay_; }
  56. private:
  57. /// Filesystem.
  58. SharedPtr<FileSystem> fileSystem_;
  59. /// The path being watched.
  60. String path_;
  61. /// Pending changes. These will be returned and removed from the list when their timer has exceeded the delay.
  62. HashMap<String, Timer> changes_;
  63. /// Mutex for the change buffer.
  64. Mutex changesMutex_;
  65. /// Delay in seconds for notifying changes.
  66. float delay_;
  67. /// Watch subdirectories flag.
  68. bool watchSubDirs_;
  69. #ifdef _WIN32
  70. /// Directory handle for the path being watched.
  71. void* dirHandle_;
  72. #elif __linux__
  73. /// HashMap for the directory and sub-directories (needed for inotify's int handles).
  74. HashMap<int, String> dirHandle_;
  75. /// Linux inotify needs a handle.
  76. int watchHandle_;
  77. #elif defined(__APPLE__) && !defined(IOS)
  78. /// Flag indicating whether the running OS supports individual file watching.
  79. bool supported_;
  80. /// Pointer to internal MacFileWatcher delegate.
  81. void* watcher_;
  82. #endif
  83. };
  84. }