| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 #include "base/files/file_path_watcher.h" | |
| 6 | |
| 7 #include <fcntl.h> | |
| 8 #include <sys/event.h> | |
| 9 #include <sys/param.h> | |
| 10 | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/bind.h" | |
| 14 #include "base/file_util.h" | |
| 15 #include "base/message_loop.h" | |
| 16 #include "base/message_loop_proxy.h" | |
| 17 #include "base/stringprintf.h" | |
| 18 | |
| 19 namespace base { | |
| 20 namespace files { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // Mac-specific file watcher implementation based on kqueue. | |
| 25 // Originally it was based on FSEvents so that the semantics were equivalent | |
| 26 // on Linux, OSX and Windows where it was able to detect: | |
| 27 // - file creation/deletion/modification in a watched directory | |
| 28 // - file creation/deletion/modification for a watched file | |
| 29 // - modifications to the paths to a watched object that would affect the | |
| 30 // object such as renaming/attibute changes etc. | |
| 31 // The FSEvents version did all of the above except handling attribute changes | |
| 32 // to path components. Unfortunately FSEvents appears to have an issue where the | |
| 33 // current implementation (Mac OS X 10.6.7) sometimes drops events and doesn't | |
| 34 // send notifications. See | |
| 35 // http://code.google.com/p/chromium/issues/detail?id=54822#c31 for source that | |
| 36 // will reproduce the problem. FSEvents also required having a CFRunLoop | |
| 37 // backing the thread that it was running on, that caused added complexity | |
| 38 // in the interfaces. | |
| 39 // The kqueue implementation will handle all of the items in the list above | |
| 40 // except for detecting modifications to files in a watched directory. It will | |
| 41 // detect the creation and deletion of files, just not the modification of | |
| 42 // files. It does however detect the attribute changes that the FSEvents impl | |
| 43 // would miss. | |
| 44 class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate, | |
| 45 public MessageLoopForIO::Watcher, | |
| 46 public MessageLoop::DestructionObserver { | |
| 47 public: | |
| 48 FilePathWatcherImpl() : kqueue_(-1) {} | |
| 49 virtual ~FilePathWatcherImpl() {} | |
| 50 | |
| 51 // MessageLoopForIO::Watcher overrides. | |
| 52 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; | |
| 53 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; | |
| 54 | |
| 55 // MessageLoop::DestructionObserver overrides. | |
| 56 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | |
| 57 | |
| 58 // FilePathWatcher::PlatformDelegate overrides. | |
| 59 virtual bool Watch(const FilePath& path, | |
| 60 FilePathWatcher::Delegate* delegate) OVERRIDE; | |
| 61 virtual void Cancel() OVERRIDE; | |
| 62 | |
| 63 private: | |
| 64 class EventData { | |
| 65 public: | |
| 66 EventData(const FilePath& path, const FilePath::StringType& subdir) | |
| 67 : path_(path), subdir_(subdir) { } | |
| 68 FilePath path_; // Full path to this item. | |
| 69 FilePath::StringType subdir_; // Path to any sub item. | |
| 70 }; | |
| 71 typedef std::vector<struct kevent> EventVector; | |
| 72 | |
| 73 // Can only be called on |io_message_loop_|'s thread. | |
| 74 virtual void CancelOnMessageLoopThread() OVERRIDE; | |
| 75 | |
| 76 // Returns true if the kevent values are error free. | |
| 77 bool AreKeventValuesValid(struct kevent* kevents, int count); | |
| 78 | |
| 79 // Respond to a change of attributes of the path component represented by | |
| 80 // |event|. Sets |target_file_affected| to true if |target_| is affected. | |
| 81 // Sets |update_watches| to true if |events_| need to be updated. | |
| 82 void HandleAttributesChange(const EventVector::iterator& event, | |
| 83 bool* target_file_affected, | |
| 84 bool* update_watches); | |
| 85 | |
| 86 // Respond to a move of deletion of the path component represented by | |
| 87 // |event|. Sets |target_file_affected| to true if |target_| is affected. | |
| 88 // Sets |update_watches| to true if |events_| need to be updated. | |
| 89 void HandleDeleteOrMoveChange(const EventVector::iterator& event, | |
| 90 bool* target_file_affected, | |
| 91 bool* update_watches); | |
| 92 | |
| 93 // Respond to a creation of an item in the path component represented by | |
| 94 // |event|. Sets |target_file_affected| to true if |target_| is affected. | |
| 95 // Sets |update_watches| to true if |events_| need to be updated. | |
| 96 void HandleCreateItemChange(const EventVector::iterator& event, | |
| 97 bool* target_file_affected, | |
| 98 bool* update_watches); | |
| 99 | |
| 100 // Update |events_| with the current status of the system. | |
| 101 // Sets |target_file_affected| to true if |target_| is affected. | |
| 102 // Returns false if an error occurs. | |
| 103 bool UpdateWatches(bool* target_file_affected); | |
| 104 | |
| 105 // Fills |events| with one kevent per component in |path|. | |
| 106 // Returns the number of valid events created where a valid event is | |
| 107 // defined as one that has a ident (file descriptor) field != -1. | |
| 108 static int EventsForPath(FilePath path, EventVector *events); | |
| 109 | |
| 110 // Release a kevent generated by EventsForPath. | |
| 111 static void ReleaseEvent(struct kevent& event); | |
| 112 | |
| 113 // Returns a file descriptor that will not block the system from deleting | |
| 114 // the file it references. | |
| 115 static int FileDescriptorForPath(const FilePath& path); | |
| 116 | |
| 117 // Closes |*fd| and sets |*fd| to -1. | |
| 118 static void CloseFileDescriptor(int* fd); | |
| 119 | |
| 120 // Returns true if kevent has open file descriptor. | |
| 121 static bool IsKeventFileDescriptorOpen(const struct kevent& event) { | |
| 122 return event.ident != static_cast<uintptr_t>(-1); | |
| 123 } | |
| 124 | |
| 125 static EventData* EventDataForKevent(const struct kevent& event) { | |
| 126 return reinterpret_cast<EventData*>(event.udata); | |
| 127 } | |
| 128 | |
| 129 EventVector events_; | |
| 130 scoped_refptr<base::MessageLoopProxy> io_message_loop_; | |
| 131 MessageLoopForIO::FileDescriptorWatcher kqueue_watcher_; | |
| 132 scoped_refptr<FilePathWatcher::Delegate> delegate_; | |
| 133 FilePath target_; | |
| 134 int kqueue_; | |
| 135 | |
| 136 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl); | |
| 137 }; | |
| 138 | |
| 139 void FilePathWatcherImpl::ReleaseEvent(struct kevent& event) { | |
| 140 CloseFileDescriptor(reinterpret_cast<int*>(&event.ident)); | |
| 141 EventData* entry = EventDataForKevent(event); | |
| 142 delete entry; | |
| 143 event.udata = NULL; | |
| 144 } | |
| 145 | |
| 146 int FilePathWatcherImpl::EventsForPath(FilePath path, EventVector* events) { | |
| 147 DCHECK(MessageLoopForIO::current()); | |
| 148 // Make sure that we are working with a clean slate. | |
| 149 DCHECK(events->empty()); | |
| 150 | |
| 151 std::vector<FilePath::StringType> components; | |
| 152 path.GetComponents(&components); | |
| 153 | |
| 154 if (components.size() < 1) { | |
| 155 return -1; | |
| 156 } | |
| 157 | |
| 158 int last_existing_entry = 0; | |
| 159 FilePath built_path; | |
| 160 bool path_still_exists = true; | |
| 161 for(std::vector<FilePath::StringType>::iterator i = components.begin(); | |
| 162 i != components.end(); ++i) { | |
| 163 if (i == components.begin()) { | |
| 164 built_path = FilePath(*i); | |
| 165 } else { | |
| 166 built_path = built_path.Append(*i); | |
| 167 } | |
| 168 int fd = -1; | |
| 169 if (path_still_exists) { | |
| 170 fd = FileDescriptorForPath(built_path); | |
| 171 if (fd == -1) { | |
| 172 path_still_exists = false; | |
| 173 } else { | |
| 174 ++last_existing_entry; | |
| 175 } | |
| 176 } | |
| 177 FilePath::StringType subdir = (i != (components.end() - 1)) ? *(i + 1) : ""; | |
| 178 EventData* data = new EventData(built_path, subdir); | |
| 179 struct kevent event; | |
| 180 EV_SET(&event, fd, EVFILT_VNODE, (EV_ADD | EV_CLEAR | EV_RECEIPT), | |
| 181 (NOTE_DELETE | NOTE_WRITE | NOTE_ATTRIB | | |
| 182 NOTE_RENAME | NOTE_REVOKE | NOTE_EXTEND), 0, data); | |
| 183 events->push_back(event); | |
| 184 } | |
| 185 return last_existing_entry; | |
| 186 } | |
| 187 | |
| 188 int FilePathWatcherImpl::FileDescriptorForPath(const FilePath& path) { | |
| 189 return HANDLE_EINTR(open(path.value().c_str(), O_EVTONLY)); | |
| 190 } | |
| 191 | |
| 192 void FilePathWatcherImpl::CloseFileDescriptor(int *fd) { | |
| 193 if (*fd == -1) { | |
| 194 return; | |
| 195 } | |
| 196 | |
| 197 if (HANDLE_EINTR(close(*fd)) != 0) { | |
| 198 PLOG(ERROR) << "close"; | |
| 199 } | |
| 200 *fd = -1; | |
| 201 } | |
| 202 | |
| 203 bool FilePathWatcherImpl::AreKeventValuesValid(struct kevent* kevents, | |
| 204 int count) { | |
| 205 if (count < 0) { | |
| 206 PLOG(ERROR) << "kevent"; | |
| 207 return false; | |
| 208 } | |
| 209 bool valid = true; | |
| 210 for (int i = 0; i < count; ++i) { | |
| 211 if (kevents[i].flags & EV_ERROR && kevents[i].data) { | |
| 212 // Find the kevent in |events_| that matches the kevent with the error. | |
| 213 EventVector::iterator event = events_.begin(); | |
| 214 for (; event != events_.end(); ++event) { | |
| 215 if (event->ident == kevents[i].ident) { | |
| 216 break; | |
| 217 } | |
| 218 } | |
| 219 std::string path_name; | |
| 220 if (event != events_.end()) { | |
| 221 EventData* event_data = EventDataForKevent(*event); | |
| 222 if (event_data != NULL) { | |
| 223 path_name = event_data->path_.value(); | |
| 224 } | |
| 225 } | |
| 226 if (path_name.empty()) { | |
| 227 path_name = base::StringPrintf( | |
| 228 "fd %d", *reinterpret_cast<int*>(&kevents[i].ident)); | |
| 229 } | |
| 230 LOG(ERROR) << "Error: " << kevents[i].data << " for " << path_name; | |
| 231 valid = false; | |
| 232 } | |
| 233 } | |
| 234 return valid; | |
| 235 } | |
| 236 | |
| 237 void FilePathWatcherImpl::HandleAttributesChange( | |
| 238 const EventVector::iterator& event, | |
| 239 bool* target_file_affected, | |
| 240 bool* update_watches) { | |
| 241 EventVector::iterator next_event = event + 1; | |
| 242 EventData* next_event_data = EventDataForKevent(*next_event); | |
| 243 // Check to see if the next item in path is still accessible. | |
| 244 int have_access = FileDescriptorForPath(next_event_data->path_); | |
| 245 if (have_access == -1) { | |
| 246 *target_file_affected = true; | |
| 247 *update_watches = true; | |
| 248 EventVector::iterator local_event(event); | |
| 249 for (; local_event != events_.end(); ++local_event) { | |
| 250 // Close all nodes from the event down. This has the side effect of | |
| 251 // potentially rendering other events in |updates| invalid. | |
| 252 // There is no need to remove the events from |kqueue_| because this | |
| 253 // happens as a side effect of closing the file descriptor. | |
| 254 CloseFileDescriptor(reinterpret_cast<int*>(&local_event->ident)); | |
| 255 } | |
| 256 } else { | |
| 257 CloseFileDescriptor(&have_access); | |
| 258 } | |
| 259 } | |
| 260 | |
| 261 void FilePathWatcherImpl::HandleDeleteOrMoveChange( | |
| 262 const EventVector::iterator& event, | |
| 263 bool* target_file_affected, | |
| 264 bool* update_watches) { | |
| 265 *target_file_affected = true; | |
| 266 *update_watches = true; | |
| 267 EventVector::iterator local_event(event); | |
| 268 for (; local_event != events_.end(); ++local_event) { | |
| 269 // Close all nodes from the event down. This has the side effect of | |
| 270 // potentially rendering other events in |updates| invalid. | |
| 271 // There is no need to remove the events from |kqueue_| because this | |
| 272 // happens as a side effect of closing the file descriptor. | |
| 273 CloseFileDescriptor(reinterpret_cast<int*>(&local_event->ident)); | |
| 274 } | |
| 275 } | |
| 276 | |
| 277 void FilePathWatcherImpl::HandleCreateItemChange( | |
| 278 const EventVector::iterator& event, | |
| 279 bool* target_file_affected, | |
| 280 bool* update_watches) { | |
| 281 // Get the next item in the path. | |
| 282 EventVector::iterator next_event = event + 1; | |
| 283 EventData* next_event_data = EventDataForKevent(*next_event); | |
| 284 | |
| 285 // Check to see if it already has a valid file descriptor. | |
| 286 if (!IsKeventFileDescriptorOpen(*next_event)) { | |
| 287 // If not, attempt to open a file descriptor for it. | |
| 288 next_event->ident = FileDescriptorForPath(next_event_data->path_); | |
| 289 if (IsKeventFileDescriptorOpen(*next_event)) { | |
| 290 *update_watches = true; | |
| 291 if (next_event_data->subdir_.empty()) { | |
| 292 *target_file_affected = true; | |
| 293 } | |
| 294 } | |
| 295 } | |
| 296 } | |
| 297 | |
| 298 bool FilePathWatcherImpl::UpdateWatches(bool* target_file_affected) { | |
| 299 // Iterate over events adding kevents for items that exist to the kqueue. | |
| 300 // Then check to see if new components in the path have been created. | |
| 301 // Repeat until no new components in the path are detected. | |
| 302 // This is to get around races in directory creation in a watched path. | |
| 303 bool update_watches = true; | |
| 304 while (update_watches) { | |
| 305 size_t valid; | |
| 306 for (valid = 0; valid < events_.size(); ++valid) { | |
| 307 if (!IsKeventFileDescriptorOpen(events_[valid])) { | |
| 308 break; | |
| 309 } | |
| 310 } | |
| 311 if (valid == 0) { | |
| 312 // The root of the file path is inaccessible? | |
| 313 return false; | |
| 314 } | |
| 315 | |
| 316 EventVector updates(valid); | |
| 317 int count = HANDLE_EINTR(kevent(kqueue_, &events_[0], valid, &updates[0], | |
| 318 valid, NULL)); | |
| 319 if (!AreKeventValuesValid(&updates[0], count)) { | |
| 320 return false; | |
| 321 } | |
| 322 update_watches = false; | |
| 323 for (; valid < events_.size(); ++valid) { | |
| 324 EventData* event_data = EventDataForKevent(events_[valid]); | |
| 325 events_[valid].ident = FileDescriptorForPath(event_data->path_); | |
| 326 if (IsKeventFileDescriptorOpen(events_[valid])) { | |
| 327 update_watches = true; | |
| 328 if (event_data->subdir_.empty()) { | |
| 329 *target_file_affected = true; | |
| 330 } | |
| 331 } else { | |
| 332 break; | |
| 333 } | |
| 334 } | |
| 335 } | |
| 336 return true; | |
| 337 } | |
| 338 | |
| 339 void FilePathWatcherImpl::OnFileCanReadWithoutBlocking(int fd) { | |
| 340 DCHECK(MessageLoopForIO::current()); | |
| 341 CHECK_EQ(fd, kqueue_); | |
| 342 CHECK(events_.size()); | |
| 343 | |
| 344 // Request the file system update notifications that have occurred and return | |
| 345 // them in |updates|. |count| will contain the number of updates that have | |
| 346 // occurred. | |
| 347 EventVector updates(events_.size()); | |
| 348 struct timespec timeout = {0, 0}; | |
| 349 int count = HANDLE_EINTR(kevent(kqueue_, NULL, 0, &updates[0], updates.size(), | |
| 350 &timeout)); | |
| 351 | |
| 352 // Error values are stored within updates, so check to make sure that no | |
| 353 // errors occurred. | |
| 354 if (!AreKeventValuesValid(&updates[0], count)) { | |
| 355 delegate_->OnFilePathError(target_); | |
| 356 Cancel(); | |
| 357 return; | |
| 358 } | |
| 359 | |
| 360 bool update_watches = false; | |
| 361 bool send_notification = false; | |
| 362 | |
| 363 // Iterate through each of the updates and react to them. | |
| 364 for (int i = 0; i < count; ++i) { | |
| 365 // Find our kevent record that matches the update notification. | |
| 366 EventVector::iterator event = events_.begin(); | |
| 367 for (; event != events_.end(); ++event) { | |
| 368 if (!IsKeventFileDescriptorOpen(*event) || | |
| 369 event->ident == updates[i].ident) { | |
| 370 break; | |
| 371 } | |
| 372 } | |
| 373 if (!IsKeventFileDescriptorOpen(*event) || event == events_.end()) { | |
| 374 // The event may no longer exist in |events_| because another event | |
| 375 // modified |events_| in such a way to make it invalid. For example if | |
| 376 // the path is /foo/bar/bam and foo is deleted, NOTE_DELETE events for | |
| 377 // foo, bar and bam will be sent. If foo is processed first, then | |
| 378 // the file descriptors for bar and bam will already be closed and set | |
| 379 // to -1 before they get a chance to be processed. | |
| 380 continue; | |
| 381 } | |
| 382 | |
| 383 EventData* event_data = EventDataForKevent(*event); | |
| 384 | |
| 385 // If the subdir is empty, this is the last item on the path and is the | |
| 386 // target file. | |
| 387 bool target_file_affected = event_data->subdir_.empty(); | |
| 388 if ((updates[i].fflags & NOTE_ATTRIB) && !target_file_affected) { | |
| 389 HandleAttributesChange(event, &target_file_affected, &update_watches); | |
| 390 } | |
| 391 if (updates[i].fflags & (NOTE_DELETE | NOTE_REVOKE | NOTE_RENAME)) { | |
| 392 HandleDeleteOrMoveChange(event, &target_file_affected, &update_watches); | |
| 393 } | |
| 394 if ((updates[i].fflags & NOTE_WRITE) && !target_file_affected) { | |
| 395 HandleCreateItemChange(event, &target_file_affected, &update_watches); | |
| 396 } | |
| 397 send_notification |= target_file_affected; | |
| 398 } | |
| 399 | |
| 400 if (update_watches) { | |
| 401 if (!UpdateWatches(&send_notification)) { | |
| 402 delegate_->OnFilePathError(target_); | |
| 403 Cancel(); | |
| 404 } | |
| 405 } | |
| 406 | |
| 407 if (send_notification) { | |
| 408 delegate_->OnFilePathChanged(target_); | |
| 409 } | |
| 410 } | |
| 411 | |
| 412 void FilePathWatcherImpl::OnFileCanWriteWithoutBlocking(int fd) { | |
| 413 NOTREACHED(); | |
| 414 } | |
| 415 | |
| 416 void FilePathWatcherImpl::WillDestroyCurrentMessageLoop() { | |
| 417 CancelOnMessageLoopThread(); | |
| 418 } | |
| 419 | |
| 420 bool FilePathWatcherImpl::Watch(const FilePath& path, | |
| 421 FilePathWatcher::Delegate* delegate) { | |
| 422 DCHECK(MessageLoopForIO::current()); | |
| 423 DCHECK(target_.value().empty()); // Can only watch one path. | |
| 424 DCHECK(delegate); | |
| 425 DCHECK_EQ(kqueue_, -1); | |
| 426 | |
| 427 delegate_ = delegate; | |
| 428 target_ = path; | |
| 429 | |
| 430 MessageLoop::current()->AddDestructionObserver(this); | |
| 431 io_message_loop_ = base::MessageLoopProxy::current(); | |
| 432 | |
| 433 kqueue_ = kqueue(); | |
| 434 if (kqueue_ == -1) { | |
| 435 PLOG(ERROR) << "kqueue"; | |
| 436 return false; | |
| 437 } | |
| 438 | |
| 439 int last_entry = EventsForPath(target_, &events_); | |
| 440 CHECK_NE(last_entry, 0); | |
| 441 | |
| 442 EventVector responses(last_entry); | |
| 443 | |
| 444 int count = HANDLE_EINTR(kevent(kqueue_, &events_[0], last_entry, | |
| 445 &responses[0], last_entry, NULL)); | |
| 446 if (!AreKeventValuesValid(&responses[0], count)) { | |
| 447 // Calling Cancel() here to close any file descriptors that were opened. | |
| 448 // This would happen in the destructor anyways, but FilePathWatchers tend to | |
| 449 // be long lived, and if an error has occurred, there is no reason to waste | |
| 450 // the file descriptors. | |
| 451 Cancel(); | |
| 452 return false; | |
| 453 } | |
| 454 | |
| 455 return MessageLoopForIO::current()->WatchFileDescriptor( | |
| 456 kqueue_, true, MessageLoopForIO::WATCH_READ, &kqueue_watcher_, this); | |
| 457 } | |
| 458 | |
| 459 void FilePathWatcherImpl::Cancel() { | |
| 460 base::MessageLoopProxy* proxy = io_message_loop_.get(); | |
| 461 if (!proxy) { | |
| 462 set_cancelled(); | |
| 463 return; | |
| 464 } | |
| 465 if (!proxy->BelongsToCurrentThread()) { | |
| 466 proxy->PostTask(FROM_HERE, | |
| 467 base::Bind(&FilePathWatcherImpl::Cancel, this)); | |
| 468 return; | |
| 469 } | |
| 470 CancelOnMessageLoopThread(); | |
| 471 } | |
| 472 | |
| 473 void FilePathWatcherImpl::CancelOnMessageLoopThread() { | |
| 474 DCHECK(MessageLoopForIO::current()); | |
| 475 if (!is_cancelled()) { | |
| 476 set_cancelled(); | |
| 477 kqueue_watcher_.StopWatchingFileDescriptor(); | |
| 478 CloseFileDescriptor(&kqueue_); | |
| 479 std::for_each(events_.begin(), events_.end(), ReleaseEvent); | |
| 480 events_.clear(); | |
| 481 io_message_loop_ = NULL; | |
| 482 MessageLoop::current()->RemoveDestructionObserver(this); | |
| 483 delegate_ = NULL; | |
| 484 } | |
| 485 } | |
| 486 | |
| 487 } // namespace | |
| 488 | |
| 489 FilePathWatcher::FilePathWatcher() { | |
| 490 impl_ = new FilePathWatcherImpl(); | |
| 491 } | |
| 492 | |
| 493 } // namespace files | |
| 494 } // namespace base | |
| OLD | NEW |