1 // Copyright (c) 2012 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.
5 #include "base/files/file_path_watcher.h"
13 #include "base/bind.h"
14 #include "base/file_util.h"
15 #include "base/logging.h"
16 #include "base/message_loop.h"
17 #include "base/message_loop_proxy.h"
18 #include "base/stringprintf.h"
20 // On some platforms these are not defined.
21 #if !defined(EV_RECEIPT)
24 #if !defined(O_EVTONLY)
25 #define O_EVTONLY O_RDONLY
33 // Mac-specific file watcher implementation based on kqueue.
34 // Originally it was based on FSEvents so that the semantics were equivalent
35 // on Linux, OSX and Windows where it was able to detect:
36 // - file creation/deletion/modification in a watched directory
37 // - file creation/deletion/modification for a watched file
38 // - modifications to the paths to a watched object that would affect the
39 // object such as renaming/attibute changes etc.
40 // The FSEvents version did all of the above except handling attribute changes
41 // to path components. Unfortunately FSEvents appears to have an issue where the
42 // current implementation (Mac OS X 10.6.7) sometimes drops events and doesn't
43 // send notifications. See
44 // http://code.google.com/p/chromium/issues/detail?id=54822#c31 for source that
45 // will reproduce the problem. FSEvents also required having a CFRunLoop
46 // backing the thread that it was running on, that caused added complexity
48 // The kqueue implementation will handle all of the items in the list above
49 // except for detecting modifications to files in a watched directory. It will
50 // detect the creation and deletion of files, just not the modification of
51 // files. It does however detect the attribute changes that the FSEvents impl
53 class FilePathWatcherImpl
: public FilePathWatcher::PlatformDelegate
,
54 public MessageLoopForIO::Watcher
,
55 public MessageLoop::DestructionObserver
{
57 FilePathWatcherImpl() : kqueue_(-1) {}
59 // MessageLoopForIO::Watcher overrides.
60 virtual void OnFileCanReadWithoutBlocking(int fd
) OVERRIDE
;
61 virtual void OnFileCanWriteWithoutBlocking(int fd
) OVERRIDE
;
63 // MessageLoop::DestructionObserver overrides.
64 virtual void WillDestroyCurrentMessageLoop() OVERRIDE
;
66 // FilePathWatcher::PlatformDelegate overrides.
67 virtual bool Watch(const FilePath
& path
,
69 FilePathWatcher::Delegate
* delegate
) OVERRIDE
;
70 virtual void Cancel() OVERRIDE
;
73 virtual ~FilePathWatcherImpl() {}
78 EventData(const FilePath
& path
, const FilePath::StringType
& subdir
)
79 : path_(path
), subdir_(subdir
) { }
80 FilePath path_
; // Full path to this item.
81 FilePath::StringType subdir_
; // Path to any sub item.
83 typedef std::vector
<struct kevent
> EventVector
;
85 // Can only be called on |io_message_loop_|'s thread.
86 virtual void CancelOnMessageLoopThread() OVERRIDE
;
88 // Returns true if the kevent values are error free.
89 bool AreKeventValuesValid(struct kevent
* kevents
, int count
);
91 // Respond to a change of attributes of the path component represented by
92 // |event|. Sets |target_file_affected| to true if |target_| is affected.
93 // Sets |update_watches| to true if |events_| need to be updated.
94 void HandleAttributesChange(const EventVector::iterator
& event
,
95 bool* target_file_affected
,
96 bool* update_watches
);
98 // Respond to a move of deletion of the path component represented by
99 // |event|. Sets |target_file_affected| to true if |target_| is affected.
100 // Sets |update_watches| to true if |events_| need to be updated.
101 void HandleDeleteOrMoveChange(const EventVector::iterator
& event
,
102 bool* target_file_affected
,
103 bool* update_watches
);
105 // Respond to a creation of an item in the path component represented by
106 // |event|. Sets |target_file_affected| to true if |target_| is affected.
107 // Sets |update_watches| to true if |events_| need to be updated.
108 void HandleCreateItemChange(const EventVector::iterator
& event
,
109 bool* target_file_affected
,
110 bool* update_watches
);
112 // Update |events_| with the current status of the system.
113 // Sets |target_file_affected| to true if |target_| is affected.
114 // Returns false if an error occurs.
115 bool UpdateWatches(bool* target_file_affected
);
117 // Fills |events| with one kevent per component in |path|.
118 // Returns the number of valid events created where a valid event is
119 // defined as one that has a ident (file descriptor) field != -1.
120 static int EventsForPath(FilePath path
, EventVector
*events
);
122 // Release a kevent generated by EventsForPath.
123 static void ReleaseEvent(struct kevent
& event
);
125 // Returns a file descriptor that will not block the system from deleting
126 // the file it references.
127 static int FileDescriptorForPath(const FilePath
& path
);
129 // Closes |*fd| and sets |*fd| to -1.
130 static void CloseFileDescriptor(int* fd
);
132 // Returns true if kevent has open file descriptor.
133 static bool IsKeventFileDescriptorOpen(const struct kevent
& event
) {
134 return event
.ident
!= static_cast<uintptr_t>(-1);
137 static EventData
* EventDataForKevent(const struct kevent
& event
) {
138 return reinterpret_cast<EventData
*>(event
.udata
);
142 scoped_refptr
<base::MessageLoopProxy
> io_message_loop_
;
143 MessageLoopForIO::FileDescriptorWatcher kqueue_watcher_
;
144 scoped_refptr
<FilePathWatcher::Delegate
> delegate_
;
148 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl
);
151 void FilePathWatcherImpl::ReleaseEvent(struct kevent
& event
) {
152 CloseFileDescriptor(reinterpret_cast<int*>(&event
.ident
));
153 EventData
* entry
= EventDataForKevent(event
);
158 int FilePathWatcherImpl::EventsForPath(FilePath path
, EventVector
* events
) {
159 DCHECK(MessageLoopForIO::current());
160 // Make sure that we are working with a clean slate.
161 DCHECK(events
->empty());
163 std::vector
<FilePath::StringType
> components
;
164 path
.GetComponents(&components
);
166 if (components
.size() < 1) {
170 int last_existing_entry
= 0;
172 bool path_still_exists
= true;
173 for(std::vector
<FilePath::StringType
>::iterator i
= components
.begin();
174 i
!= components
.end(); ++i
) {
175 if (i
== components
.begin()) {
176 built_path
= FilePath(*i
);
178 built_path
= built_path
.Append(*i
);
181 if (path_still_exists
) {
182 fd
= FileDescriptorForPath(built_path
);
184 path_still_exists
= false;
186 ++last_existing_entry
;
189 FilePath::StringType subdir
= (i
!= (components
.end() - 1)) ? *(i
+ 1) : "";
190 EventData
* data
= new EventData(built_path
, subdir
);
192 EV_SET(&event
, fd
, EVFILT_VNODE
, (EV_ADD
| EV_CLEAR
| EV_RECEIPT
),
193 (NOTE_DELETE
| NOTE_WRITE
| NOTE_ATTRIB
|
194 NOTE_RENAME
| NOTE_REVOKE
| NOTE_EXTEND
), 0, data
);
195 events
->push_back(event
);
197 return last_existing_entry
;
200 int FilePathWatcherImpl::FileDescriptorForPath(const FilePath
& path
) {
201 return HANDLE_EINTR(open(path
.value().c_str(), O_EVTONLY
));
204 void FilePathWatcherImpl::CloseFileDescriptor(int *fd
) {
209 if (HANDLE_EINTR(close(*fd
)) != 0) {
210 DPLOG(ERROR
) << "close";
215 bool FilePathWatcherImpl::AreKeventValuesValid(struct kevent
* kevents
,
218 DPLOG(ERROR
) << "kevent";
222 for (int i
= 0; i
< count
; ++i
) {
223 if (kevents
[i
].flags
& EV_ERROR
&& kevents
[i
].data
) {
224 // Find the kevent in |events_| that matches the kevent with the error.
225 EventVector::iterator event
= events_
.begin();
226 for (; event
!= events_
.end(); ++event
) {
227 if (event
->ident
== kevents
[i
].ident
) {
231 std::string path_name
;
232 if (event
!= events_
.end()) {
233 EventData
* event_data
= EventDataForKevent(*event
);
234 if (event_data
!= NULL
) {
235 path_name
= event_data
->path_
.value();
238 if (path_name
.empty()) {
239 path_name
= base::StringPrintf(
240 "fd %d", *reinterpret_cast<int*>(&kevents
[i
].ident
));
242 DLOG(ERROR
) << "Error: " << kevents
[i
].data
<< " for " << path_name
;
249 void FilePathWatcherImpl::HandleAttributesChange(
250 const EventVector::iterator
& event
,
251 bool* target_file_affected
,
252 bool* update_watches
) {
253 EventVector::iterator next_event
= event
+ 1;
254 EventData
* next_event_data
= EventDataForKevent(*next_event
);
255 // Check to see if the next item in path is still accessible.
256 int have_access
= FileDescriptorForPath(next_event_data
->path_
);
257 if (have_access
== -1) {
258 *target_file_affected
= true;
259 *update_watches
= true;
260 EventVector::iterator
local_event(event
);
261 for (; local_event
!= events_
.end(); ++local_event
) {
262 // Close all nodes from the event down. This has the side effect of
263 // potentially rendering other events in |updates| invalid.
264 // There is no need to remove the events from |kqueue_| because this
265 // happens as a side effect of closing the file descriptor.
266 CloseFileDescriptor(reinterpret_cast<int*>(&local_event
->ident
));
269 CloseFileDescriptor(&have_access
);
273 void FilePathWatcherImpl::HandleDeleteOrMoveChange(
274 const EventVector::iterator
& event
,
275 bool* target_file_affected
,
276 bool* update_watches
) {
277 *target_file_affected
= true;
278 *update_watches
= true;
279 EventVector::iterator
local_event(event
);
280 for (; local_event
!= events_
.end(); ++local_event
) {
281 // Close all nodes from the event down. This has the side effect of
282 // potentially rendering other events in |updates| invalid.
283 // There is no need to remove the events from |kqueue_| because this
284 // happens as a side effect of closing the file descriptor.
285 CloseFileDescriptor(reinterpret_cast<int*>(&local_event
->ident
));
289 void FilePathWatcherImpl::HandleCreateItemChange(
290 const EventVector::iterator
& event
,
291 bool* target_file_affected
,
292 bool* update_watches
) {
293 // Get the next item in the path.
294 EventVector::iterator next_event
= event
+ 1;
295 EventData
* next_event_data
= EventDataForKevent(*next_event
);
297 // Check to see if it already has a valid file descriptor.
298 if (!IsKeventFileDescriptorOpen(*next_event
)) {
299 // If not, attempt to open a file descriptor for it.
300 next_event
->ident
= FileDescriptorForPath(next_event_data
->path_
);
301 if (IsKeventFileDescriptorOpen(*next_event
)) {
302 *update_watches
= true;
303 if (next_event_data
->subdir_
.empty()) {
304 *target_file_affected
= true;
310 bool FilePathWatcherImpl::UpdateWatches(bool* target_file_affected
) {
311 // Iterate over events adding kevents for items that exist to the kqueue.
312 // Then check to see if new components in the path have been created.
313 // Repeat until no new components in the path are detected.
314 // This is to get around races in directory creation in a watched path.
315 bool update_watches
= true;
316 while (update_watches
) {
318 for (valid
= 0; valid
< events_
.size(); ++valid
) {
319 if (!IsKeventFileDescriptorOpen(events_
[valid
])) {
324 // The root of the file path is inaccessible?
328 EventVector
updates(valid
);
329 int count
= HANDLE_EINTR(kevent(kqueue_
, &events_
[0], valid
, &updates
[0],
331 if (!AreKeventValuesValid(&updates
[0], count
)) {
334 update_watches
= false;
335 for (; valid
< events_
.size(); ++valid
) {
336 EventData
* event_data
= EventDataForKevent(events_
[valid
]);
337 events_
[valid
].ident
= FileDescriptorForPath(event_data
->path_
);
338 if (IsKeventFileDescriptorOpen(events_
[valid
])) {
339 update_watches
= true;
340 if (event_data
->subdir_
.empty()) {
341 *target_file_affected
= true;
351 void FilePathWatcherImpl::OnFileCanReadWithoutBlocking(int fd
) {
352 DCHECK(MessageLoopForIO::current());
353 DCHECK_EQ(fd
, kqueue_
);
354 DCHECK(events_
.size());
356 // Request the file system update notifications that have occurred and return
357 // them in |updates|. |count| will contain the number of updates that have
359 EventVector
updates(events_
.size());
360 struct timespec timeout
= {0, 0};
361 int count
= HANDLE_EINTR(kevent(kqueue_
, NULL
, 0, &updates
[0], updates
.size(),
364 // Error values are stored within updates, so check to make sure that no
366 if (!AreKeventValuesValid(&updates
[0], count
)) {
367 delegate_
->OnFilePathError(target_
);
372 bool update_watches
= false;
373 bool send_notification
= false;
375 // Iterate through each of the updates and react to them.
376 for (int i
= 0; i
< count
; ++i
) {
377 // Find our kevent record that matches the update notification.
378 EventVector::iterator event
= events_
.begin();
379 for (; event
!= events_
.end(); ++event
) {
380 if (!IsKeventFileDescriptorOpen(*event
) ||
381 event
->ident
== updates
[i
].ident
) {
385 if (!IsKeventFileDescriptorOpen(*event
) || event
== events_
.end()) {
386 // The event may no longer exist in |events_| because another event
387 // modified |events_| in such a way to make it invalid. For example if
388 // the path is /foo/bar/bam and foo is deleted, NOTE_DELETE events for
389 // foo, bar and bam will be sent. If foo is processed first, then
390 // the file descriptors for bar and bam will already be closed and set
391 // to -1 before they get a chance to be processed.
395 EventData
* event_data
= EventDataForKevent(*event
);
397 // If the subdir is empty, this is the last item on the path and is the
399 bool target_file_affected
= event_data
->subdir_
.empty();
400 if ((updates
[i
].fflags
& NOTE_ATTRIB
) && !target_file_affected
) {
401 HandleAttributesChange(event
, &target_file_affected
, &update_watches
);
403 if (updates
[i
].fflags
& (NOTE_DELETE
| NOTE_REVOKE
| NOTE_RENAME
)) {
404 HandleDeleteOrMoveChange(event
, &target_file_affected
, &update_watches
);
406 if ((updates
[i
].fflags
& NOTE_WRITE
) && !target_file_affected
) {
407 HandleCreateItemChange(event
, &target_file_affected
, &update_watches
);
409 send_notification
|= target_file_affected
;
412 if (update_watches
) {
413 if (!UpdateWatches(&send_notification
)) {
414 delegate_
->OnFilePathError(target_
);
419 if (send_notification
) {
420 delegate_
->OnFilePathChanged(target_
);
424 void FilePathWatcherImpl::OnFileCanWriteWithoutBlocking(int fd
) {
428 void FilePathWatcherImpl::WillDestroyCurrentMessageLoop() {
429 CancelOnMessageLoopThread();
432 bool FilePathWatcherImpl::Watch(const FilePath
& path
,
434 FilePathWatcher::Delegate
* delegate
) {
435 DCHECK(MessageLoopForIO::current());
436 DCHECK(target_
.value().empty()); // Can only watch one path.
438 DCHECK_EQ(kqueue_
, -1);
441 // Recursive watch is not supported on this platform.
446 delegate_
= delegate
;
449 MessageLoop::current()->AddDestructionObserver(this);
450 io_message_loop_
= base::MessageLoopProxy::current();
454 DPLOG(ERROR
) << "kqueue";
458 int last_entry
= EventsForPath(target_
, &events_
);
459 DCHECK_NE(last_entry
, 0);
461 EventVector
responses(last_entry
);
463 int count
= HANDLE_EINTR(kevent(kqueue_
, &events_
[0], last_entry
,
464 &responses
[0], last_entry
, NULL
));
465 if (!AreKeventValuesValid(&responses
[0], count
)) {
466 // Calling Cancel() here to close any file descriptors that were opened.
467 // This would happen in the destructor anyways, but FilePathWatchers tend to
468 // be long lived, and if an error has occurred, there is no reason to waste
469 // the file descriptors.
474 return MessageLoopForIO::current()->WatchFileDescriptor(
475 kqueue_
, true, MessageLoopForIO::WATCH_READ
, &kqueue_watcher_
, this);
478 void FilePathWatcherImpl::Cancel() {
479 base::MessageLoopProxy
* proxy
= io_message_loop_
.get();
484 if (!proxy
->BelongsToCurrentThread()) {
485 proxy
->PostTask(FROM_HERE
,
486 base::Bind(&FilePathWatcherImpl::Cancel
, this));
489 CancelOnMessageLoopThread();
492 void FilePathWatcherImpl::CancelOnMessageLoopThread() {
493 DCHECK(MessageLoopForIO::current());
494 if (!is_cancelled()) {
496 kqueue_watcher_
.StopWatchingFileDescriptor();
497 CloseFileDescriptor(&kqueue_
);
498 std::for_each(events_
.begin(), events_
.end(), ReleaseEvent
);
500 io_message_loop_
= NULL
;
501 MessageLoop::current()->RemoveDestructionObserver(this);
508 FilePathWatcher::FilePathWatcher() {
509 impl_
= new FilePathWatcherImpl();