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