file_path_watcher_kqueue.h 4.8 KB

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