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