file_path_watcher_kqueue.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_FILES_FILE_PATH_WATCHER_KQUEUE_H_
  5. #define BASE_FILES_FILE_PATH_WATCHER_KQUEUE_H_
  6. #include <sys/event.h>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/files/file_descriptor_watcher_posix.h"
  10. #include "base/files/file_path.h"
  11. #include "base/files/file_path_watcher.h"
  12. namespace base {
  13. // Mac-specific file watcher implementation based on kqueue.
  14. // The Linux and Windows versions are able to detect:
  15. // - file creation/deletion/modification in a watched directory
  16. // - file creation/deletion/modification for a watched file
  17. // - modifications to the paths to a watched object that would affect the
  18. // object such as renaming/attibute changes etc.
  19. // The kqueue implementation will handle all of the items in the list above
  20. // except for detecting modifications to files in a watched directory. It will
  21. // detect the creation and deletion of files, just not the modification of
  22. // files. It does however detect the attribute changes that the FSEvents impl
  23. // would miss.
  24. class FilePathWatcherKQueue : public FilePathWatcher::PlatformDelegate {
  25. public:
  26. FilePathWatcherKQueue();
  27. FilePathWatcherKQueue(const FilePathWatcherKQueue&) = delete;
  28. FilePathWatcherKQueue& operator=(const FilePathWatcherKQueue&) = delete;
  29. ~FilePathWatcherKQueue() override;
  30. // FilePathWatcher::PlatformDelegate overrides.
  31. bool Watch(const FilePath& path,
  32. Type type,
  33. const FilePathWatcher::Callback& callback) override;
  34. void Cancel() override;
  35. private:
  36. class EventData {
  37. public:
  38. EventData(const FilePath& path, const FilePath::StringType& subdir)
  39. : path_(path), subdir_(subdir) { }
  40. FilePath path_; // Full path to this item.
  41. FilePath::StringType subdir_; // Path to any sub item.
  42. };
  43. typedef std::vector<struct kevent> EventVector;
  44. // Called when data is available in |kqueue_|.
  45. void OnKQueueReadable();
  46. // Returns true if the kevent values are error free.
  47. bool AreKeventValuesValid(struct kevent* kevents, int count);
  48. // Respond to a change of attributes of the path component represented by
  49. // |event|. Sets |target_file_affected| to true if |target_| is affected.
  50. // Sets |update_watches| to true if |events_| need to be updated.
  51. void HandleAttributesChange(const EventVector::iterator& event,
  52. bool* target_file_affected,
  53. bool* update_watches);
  54. // Respond to a move or deletion of the path component represented by
  55. // |event|. Sets |target_file_affected| to true if |target_| is affected.
  56. // Sets |update_watches| to true if |events_| need to be updated.
  57. void HandleDeleteOrMoveChange(const EventVector::iterator& event,
  58. bool* target_file_affected,
  59. bool* update_watches);
  60. // Respond to a creation of an item in the path component represented by
  61. // |event|. Sets |target_file_affected| to true if |target_| is affected.
  62. // Sets |update_watches| to true if |events_| need to be updated.
  63. void HandleCreateItemChange(const EventVector::iterator& event,
  64. bool* target_file_affected,
  65. bool* update_watches);
  66. // Update |events_| with the current status of the system.
  67. // Sets |target_file_affected| to true if |target_| is affected.
  68. // Returns false if an error occurs.
  69. bool UpdateWatches(bool* target_file_affected);
  70. // Fills |events| with one kevent per component in |path|.
  71. // Returns the number of valid events created where a valid event is
  72. // defined as one that has a ident (file descriptor) field != -1.
  73. static size_t EventsForPath(FilePath path, EventVector* events);
  74. // Fills |events| with one kevent for |path|. Returns 1 if a file descriptor
  75. // for |path| could be created, or 0 otherwise.
  76. static size_t EventForItem(const FilePath& path, EventVector* events);
  77. // Release a kevent generated by EventsForPath.
  78. static void ReleaseEvent(struct kevent& event);
  79. // Returns a file descriptor that will not block the system from deleting
  80. // the file it references.
  81. static uintptr_t FileDescriptorForPath(const FilePath& path);
  82. static const uintptr_t kNoFileDescriptor = static_cast<uintptr_t>(-1);
  83. // Closes |*fd| and sets |*fd| to -1.
  84. static void CloseFileDescriptor(uintptr_t* fd);
  85. // Returns true if kevent has open file descriptor.
  86. static bool IsKeventFileDescriptorOpen(const struct kevent& event) {
  87. return event.ident != kNoFileDescriptor;
  88. }
  89. static EventData* EventDataForKevent(const struct kevent& event) {
  90. return reinterpret_cast<EventData*>(event.udata);
  91. }
  92. EventVector events_;
  93. FilePathWatcher::Callback callback_;
  94. FilePath target_;
  95. int kqueue_;
  96. // Throughout the lifetime of this, OnKQueueReadable() will be called when
  97. // data is available in |kqueue_|.
  98. std::unique_ptr<FileDescriptorWatcher::Controller> kqueue_watch_controller_;
  99. };
  100. } // namespace base
  101. #endif // BASE_FILES_FILE_PATH_WATCHER_KQUEUE_H_