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. 4 5 #include "base/files/file_path_watcher.h" 6 7 #include <errno.h> 8 #include <stddef.h> 9 #include <string.h> 10 #include <sys/inotify.h> 11 #include <sys/ioctl.h> 12 #include <sys/select.h> 13 #include <unistd.h> 14 15 #include <algorithm> 16 #include <map> 17 #include <set> 18 #include <utility> 19 #include <vector> 20 21 #include "base/bind.h" 22 #include "base/containers/hash_tables.h" 23 #include "base/files/file_enumerator.h" 24 #include "base/files/file_path.h" 25 #include "base/files/file_util.h" 26 #include "base/lazy_instance.h" 27 #include "base/location.h" 28 #include "base/logging.h" 29 #include "base/macros.h" 30 #include "base/memory/scoped_ptr.h" 31 #include "base/posix/eintr_wrapper.h" 32 #include "base/single_thread_task_runner.h" 33 #include "base/stl_util.h" 34 #include "base/synchronization/lock.h" 35 #include "base/thread_task_runner_handle.h" 36 #include "base/threading/thread.h" 37 #include "base/trace_event/trace_event.h" 38 39 namespace base { 40 41 namespace { 42 43 class FilePathWatcherImpl; 44 45 // Singleton to manage all inotify watches. 46 // TODO(tony): It would be nice if this wasn't a singleton. 47 // http://crbug.com/38174 48 class InotifyReader { 49 public: 50 typedef int Watch; // Watch descriptor used by AddWatch and RemoveWatch. 51 static const Watch kInvalidWatch = -1; 52 53 // Watch directory |path| for changes. |watcher| will be notified on each 54 // change. Returns kInvalidWatch on failure. 55 Watch AddWatch(const FilePath& path, FilePathWatcherImpl* watcher); 56 57 // Remove |watch| if it's valid. 58 void RemoveWatch(Watch watch, FilePathWatcherImpl* watcher); 59 60 // Callback for InotifyReaderTask. 61 void OnInotifyEvent(const inotify_event* event); 62 63 private: 64 friend struct DefaultLazyInstanceTraits<InotifyReader>; 65 66 typedef std::set<FilePathWatcherImpl*> WatcherSet; 67 68 InotifyReader(); 69 ~InotifyReader(); 70 71 // We keep track of which delegates want to be notified on which watches. 72 hash_map<Watch, WatcherSet> watchers_; 73 74 // Lock to protect watchers_. 75 Lock lock_; 76 77 // Separate thread on which we run blocking read for inotify events. 78 Thread thread_; 79 80 // File descriptor returned by inotify_init. 81 const int inotify_fd_; 82 83 // Use self-pipe trick to unblock select during shutdown. 84 int shutdown_pipe_[2]; 85 86 // Flag set to true when startup was successful. 87 bool valid_; 88 89 DISALLOW_COPY_AND_ASSIGN(InotifyReader); 90 }; 91 92 class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate, 93 public MessageLoop::DestructionObserver { 94 public: 95 FilePathWatcherImpl(); 96 97 // Called for each event coming from the watch. |fired_watch| identifies the 98 // watch that fired, |child| indicates what has changed, and is relative to 99 // the currently watched path for |fired_watch|. 100 // 101 // |created| is true if the object appears. 102 // |deleted| is true if the object disappears. 103 // |is_dir| is true if the object is a directory. 104 void OnFilePathChanged(InotifyReader::Watch fired_watch, 105 const FilePath::StringType& child, 106 bool created, 107 bool deleted, 108 bool is_dir); 109 110 protected: 111 ~FilePathWatcherImpl() override {} 112 113 private: 114 // Start watching |path| for changes and notify |delegate| on each change. 115 // Returns true if watch for |path| has been added successfully. 116 bool Watch(const FilePath& path, 117 bool recursive, 118 const FilePathWatcher::Callback& callback) override; 119 120 // Cancel the watch. This unregisters the instance with InotifyReader. 121 void Cancel() override; 122 123 // Cleans up and stops observing the message_loop() thread. 124 void CancelOnMessageLoopThread() override; 125 126 // Deletion of the FilePathWatcher will call Cancel() to dispose of this 127 // object in the right thread. This also observes destruction of the required 128 // cleanup thread, in case it quits before Cancel() is called. 129 void WillDestroyCurrentMessageLoop() override; 130 131 // Inotify watches are installed for all directory components of |target_|. 132 // A WatchEntry instance holds: 133 // - |watch|: the watch descriptor for a component. 134 // - |subdir|: the subdirectory that identifies the next component. 135 // - For the last component, there is no next component, so it is empty. 136 // - |linkname|: the target of the symlink. 137 // - Only if the target being watched is a symbolic link. 138 struct WatchEntry { 139 explicit WatchEntry(const FilePath::StringType& dirname) 140 : watch(InotifyReader::kInvalidWatch), 141 subdir(dirname) {} 142 143 InotifyReader::Watch watch; 144 FilePath::StringType subdir; 145 FilePath::StringType linkname; 146 }; 147 typedef std::vector<WatchEntry> WatchVector; 148 149 // Reconfigure to watch for the most specific parent directory of |target_| 150 // that exists. Also calls UpdateRecursiveWatches() below. 151 void UpdateWatches(); 152 153 // Reconfigure to recursively watch |target_| and all its sub-directories. 154 // - This is a no-op if the watch is not recursive. 155 // - If |target_| does not exist, then clear all the recursive watches. 156 // - Assuming |target_| exists, passing kInvalidWatch as |fired_watch| forces 157 // addition of recursive watches for |target_|. 158 // - Otherwise, only the directory associated with |fired_watch| and its 159 // sub-directories will be reconfigured. 160 void UpdateRecursiveWatches(InotifyReader::Watch fired_watch, bool is_dir); 161 162 // Enumerate recursively through |path| and add / update watches. 163 void UpdateRecursiveWatchesForPath(const FilePath& path); 164 165 // Do internal bookkeeping to update mappings between |watch| and its 166 // associated full path |path|. 167 void TrackWatchForRecursion(InotifyReader::Watch watch, const FilePath& path); 168 169 // Remove all the recursive watches. 170 void RemoveRecursiveWatches(); 171 172 // |path| is a symlink to a non-existent target. Attempt to add a watch to 173 // the link target's parent directory. Update |watch_entry| on success. 174 void AddWatchForBrokenSymlink(const FilePath& path, WatchEntry* watch_entry); 175 176 bool HasValidWatchVector() const; 177 178 // Callback to notify upon changes. 179 FilePathWatcher::Callback callback_; 180 181 // The file or directory we're supposed to watch. 182 FilePath target_; 183 184 bool recursive_; 185 186 // The vector of watches and next component names for all path components, 187 // starting at the root directory. The last entry corresponds to the watch for 188 // |target_| and always stores an empty next component name in |subdir|. 189 WatchVector watches_; 190 191 hash_map<InotifyReader::Watch, FilePath> recursive_paths_by_watch_; 192 std::map<FilePath, InotifyReader::Watch> recursive_watches_by_path_; 193 194 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl); 195 }; 196 197 void InotifyReaderCallback(InotifyReader* reader, int inotify_fd, 198 int shutdown_fd) { 199 // Make sure the file descriptors are good for use with select(). 200 CHECK_LE(0, inotify_fd); 201 CHECK_GT(FD_SETSIZE, inotify_fd); 202 CHECK_LE(0, shutdown_fd); 203 CHECK_GT(FD_SETSIZE, shutdown_fd); 204 205 trace_event::TraceLog::GetInstance()->SetCurrentThreadBlocksMessageLoop(); 206 207 while (true) { 208 fd_set rfds; 209 FD_ZERO(&rfds); 210 FD_SET(inotify_fd, &rfds); 211 FD_SET(shutdown_fd, &rfds); 212 213 // Wait until some inotify events are available. 214 int select_result = 215 HANDLE_EINTR(select(std::max(inotify_fd, shutdown_fd) + 1, 216 &rfds, NULL, NULL, NULL)); 217 if (select_result < 0) { 218 DPLOG(WARNING) << "select failed"; 219 return; 220 } 221 222 if (FD_ISSET(shutdown_fd, &rfds)) 223 return; 224 225 // Adjust buffer size to current event queue size. 226 int buffer_size; 227 int ioctl_result = HANDLE_EINTR(ioctl(inotify_fd, FIONREAD, 228 &buffer_size)); 229 230 if (ioctl_result != 0) { 231 DPLOG(WARNING) << "ioctl failed"; 232 return; 233 } 234 235 std::vector<char> buffer(buffer_size); 236 237 ssize_t bytes_read = HANDLE_EINTR(read(inotify_fd, &buffer[0], 238 buffer_size)); 239 240 if (bytes_read < 0) { 241 DPLOG(WARNING) << "read from inotify fd failed"; 242 return; 243 } 244 245 ssize_t i = 0; 246 while (i < bytes_read) { 247 inotify_event* event = reinterpret_cast<inotify_event*>(&buffer[i]); 248 size_t event_size = sizeof(inotify_event) + event->len; 249 DCHECK(i + event_size <= static_cast<size_t>(bytes_read)); 250 reader->OnInotifyEvent(event); 251 i += event_size; 252 } 253 } 254 } 255 256 static LazyInstance<InotifyReader>::Leaky g_inotify_reader = 257 LAZY_INSTANCE_INITIALIZER; 258 259 InotifyReader::InotifyReader() 260 : thread_("inotify_reader"), 261 inotify_fd_(inotify_init()), 262 valid_(false) { 263 if (inotify_fd_ < 0) 264 PLOG(ERROR) << "inotify_init() failed"; 265 266 shutdown_pipe_[0] = -1; 267 shutdown_pipe_[1] = -1; 268 if (inotify_fd_ >= 0 && pipe(shutdown_pipe_) == 0 && thread_.Start()) { 269 thread_.task_runner()->PostTask( 270 FROM_HERE, 271 Bind(&InotifyReaderCallback, this, inotify_fd_, shutdown_pipe_[0])); 272 valid_ = true; 273 } 274 } 275 276 InotifyReader::~InotifyReader() { 277 if (valid_) { 278 // Write to the self-pipe so that the select call in InotifyReaderTask 279 // returns. 280 ssize_t ret = HANDLE_EINTR(write(shutdown_pipe_[1], "", 1)); 281 DPCHECK(ret > 0); 282 DCHECK_EQ(ret, 1); 283 thread_.Stop(); 284 } 285 if (inotify_fd_ >= 0) 286 close(inotify_fd_); 287 if (shutdown_pipe_[0] >= 0) 288 close(shutdown_pipe_[0]); 289 if (shutdown_pipe_[1] >= 0) 290 close(shutdown_pipe_[1]); 291 } 292 293 InotifyReader::Watch InotifyReader::AddWatch( 294 const FilePath& path, FilePathWatcherImpl* watcher) { 295 if (!valid_) 296 return kInvalidWatch; 297 298 AutoLock auto_lock(lock_); 299 300 Watch watch = inotify_add_watch(inotify_fd_, path.value().c_str(), 301 IN_ATTRIB | IN_CREATE | IN_DELETE | 302 IN_CLOSE_WRITE | IN_MOVE | 303 IN_ONLYDIR); 304 305 if (watch == kInvalidWatch) 306 return kInvalidWatch; 307 308 watchers_[watch].insert(watcher); 309 310 return watch; 311 } 312 313 void InotifyReader::RemoveWatch(Watch watch, FilePathWatcherImpl* watcher) { 314 if (!valid_ || (watch == kInvalidWatch)) 315 return; 316 317 AutoLock auto_lock(lock_); 318 319 watchers_[watch].erase(watcher); 320 321 if (watchers_[watch].empty()) { 322 watchers_.erase(watch); 323 inotify_rm_watch(inotify_fd_, watch); 324 } 325 } 326 327 void InotifyReader::OnInotifyEvent(const inotify_event* event) { 328 if (event->mask & IN_IGNORED) 329 return; 330 331 FilePath::StringType child(event->len ? event->name : FILE_PATH_LITERAL("")); 332 AutoLock auto_lock(lock_); 333 334 for (WatcherSet::iterator watcher = watchers_[event->wd].begin(); 335 watcher != watchers_[event->wd].end(); 336 ++watcher) { 337 (*watcher)->OnFilePathChanged(event->wd, 338 child, 339 event->mask & (IN_CREATE | IN_MOVED_TO), 340 event->mask & (IN_DELETE | IN_MOVED_FROM), 341 event->mask & IN_ISDIR); 342 } 343 } 344 345 FilePathWatcherImpl::FilePathWatcherImpl() 346 : recursive_(false) { 347 } 348 349 void FilePathWatcherImpl::OnFilePathChanged(InotifyReader::Watch fired_watch, 350 const FilePath::StringType& child, 351 bool created, 352 bool deleted, 353 bool is_dir) { 354 if (!task_runner()->BelongsToCurrentThread()) { 355 // Switch to task_runner() to access |watches_| safely. 356 task_runner()->PostTask(FROM_HERE, 357 Bind(&FilePathWatcherImpl::OnFilePathChanged, this, 358 fired_watch, child, created, deleted, is_dir)); 359 return; 360 } 361 362 // Check to see if CancelOnMessageLoopThread() has already been called. 363 // May happen when code flow reaches here from the PostTask() above. 364 if (watches_.empty()) { 365 DCHECK(target_.empty()); 366 return; 367 } 368 369 DCHECK(MessageLoopForIO::current()); 370 DCHECK(HasValidWatchVector()); 371 372 // Used below to avoid multiple recursive updates. 373 bool did_update = false; 374 375 // Find the entry in |watches_| that corresponds to |fired_watch|. 376 for (size_t i = 0; i < watches_.size(); ++i) { 377 const WatchEntry& watch_entry = watches_[i]; 378 if (fired_watch != watch_entry.watch) 379 continue; 380 381 // Check whether a path component of |target_| changed. 382 bool change_on_target_path = 383 child.empty() || 384 (child == watch_entry.linkname) || 385 (child == watch_entry.subdir); 386 387 // Check if the change references |target_| or a direct child of |target_|. 388 bool target_changed; 389 if (watch_entry.subdir.empty()) { 390 // The fired watch is for a WatchEntry without a subdir. Thus for a given 391 // |target_| = "/path/to/foo", this is for "foo". Here, check either: 392 // - the target has no symlink: it is the target and it changed. 393 // - the target has a symlink, and it matches |child|. 394 target_changed = (watch_entry.linkname.empty() || 395 child == watch_entry.linkname); 396 } else { 397 // The fired watch is for a WatchEntry with a subdir. Thus for a given 398 // |target_| = "/path/to/foo", this is for {"/", "/path", "/path/to"}. 399 // So we can safely access the next WatchEntry since we have not reached 400 // the end yet. Check |watch_entry| is for "/path/to", i.e. the next 401 // element is "foo". 402 bool next_watch_may_be_for_target = watches_[i + 1].subdir.empty(); 403 if (next_watch_may_be_for_target) { 404 // The current |watch_entry| is for "/path/to", so check if the |child| 405 // that changed is "foo". 406 target_changed = watch_entry.subdir == child; 407 } else { 408 // The current |watch_entry| is not for "/path/to", so the next entry 409 // cannot be "foo". Thus |target_| has not changed. 410 target_changed = false; 411 } 412 } 413 414 // Update watches if a directory component of the |target_| path 415 // (dis)appears. Note that we don't add the additional restriction of 416 // checking the event mask to see if it is for a directory here as changes 417 // to symlinks on the target path will not have IN_ISDIR set in the event 418 // masks. As a result we may sometimes call UpdateWatches() unnecessarily. 419 if (change_on_target_path && (created || deleted) && !did_update) { 420 UpdateWatches(); 421 did_update = true; 422 } 423 424 // Report the following events: 425 // - The target or a direct child of the target got changed (in case the 426 // watched path refers to a directory). 427 // - One of the parent directories got moved or deleted, since the target 428 // disappears in this case. 429 // - One of the parent directories appears. The event corresponding to 430 // the target appearing might have been missed in this case, so recheck. 431 if (target_changed || 432 (change_on_target_path && deleted) || 433 (change_on_target_path && created && PathExists(target_))) { 434 if (!did_update) { 435 UpdateRecursiveWatches(fired_watch, is_dir); 436 did_update = true; 437 } 438 callback_.Run(target_, false /* error */); 439 return; 440 } 441 } 442 443 if (ContainsKey(recursive_paths_by_watch_, fired_watch)) { 444 if (!did_update) 445 UpdateRecursiveWatches(fired_watch, is_dir); 446 callback_.Run(target_, false /* error */); 447 } 448 } 449 450 bool FilePathWatcherImpl::Watch(const FilePath& path, 451 bool recursive, 452 const FilePathWatcher::Callback& callback) { 453 DCHECK(target_.empty()); 454 DCHECK(MessageLoopForIO::current()); 455 456 set_task_runner(ThreadTaskRunnerHandle::Get()); 457 callback_ = callback; 458 target_ = path; 459 recursive_ = recursive; 460 MessageLoop::current()->AddDestructionObserver(this); 461 462 std::vector<FilePath::StringType> comps; 463 target_.GetComponents(&comps); 464 DCHECK(!comps.empty()); 465 for (size_t i = 1; i < comps.size(); ++i) 466 watches_.push_back(WatchEntry(comps[i])); 467 watches_.push_back(WatchEntry(FilePath::StringType())); 468 UpdateWatches(); 469 return true; 470 } 471 472 void FilePathWatcherImpl::Cancel() { 473 if (callback_.is_null()) { 474 // Watch was never called, or the message_loop() thread is already gone. 475 set_cancelled(); 476 return; 477 } 478 479 // Switch to the message_loop() if necessary so we can access |watches_|. 480 if (!task_runner()->BelongsToCurrentThread()) { 481 task_runner()->PostTask(FROM_HERE, Bind(&FilePathWatcher::CancelWatch, 482 make_scoped_refptr(this))); 483 } else { 484 CancelOnMessageLoopThread(); 485 } 486 } 487 488 void FilePathWatcherImpl::CancelOnMessageLoopThread() { 489 DCHECK(task_runner()->BelongsToCurrentThread()); 490 set_cancelled(); 491 492 if (!callback_.is_null()) { 493 MessageLoop::current()->RemoveDestructionObserver(this); 494 callback_.Reset(); 495 } 496 497 for (size_t i = 0; i < watches_.size(); ++i) 498 g_inotify_reader.Get().RemoveWatch(watches_[i].watch, this); 499 watches_.clear(); 500 target_.clear(); 501 502 if (recursive_) 503 RemoveRecursiveWatches(); 504 } 505 506 void FilePathWatcherImpl::WillDestroyCurrentMessageLoop() { 507 CancelOnMessageLoopThread(); 508 } 509 510 void FilePathWatcherImpl::UpdateWatches() { 511 // Ensure this runs on the message_loop() exclusively in order to avoid 512 // concurrency issues. 513 DCHECK(task_runner()->BelongsToCurrentThread()); 514 DCHECK(HasValidWatchVector()); 515 516 // Walk the list of watches and update them as we go. 517 FilePath path(FILE_PATH_LITERAL("/")); 518 for (size_t i = 0; i < watches_.size(); ++i) { 519 WatchEntry& watch_entry = watches_[i]; 520 InotifyReader::Watch old_watch = watch_entry.watch; 521 watch_entry.watch = InotifyReader::kInvalidWatch; 522 watch_entry.linkname.clear(); 523 watch_entry.watch = g_inotify_reader.Get().AddWatch(path, this); 524 if (watch_entry.watch == InotifyReader::kInvalidWatch) { 525 // Ignore the error code (beyond symlink handling) to attempt to add 526 // watches on accessible children of unreadable directories. Note that 527 // this is a best-effort attempt; we may not catch events in this 528 // scenario. 529 if (IsLink(path)) 530 AddWatchForBrokenSymlink(path, &watch_entry); 531 } 532 if (old_watch != watch_entry.watch) 533 g_inotify_reader.Get().RemoveWatch(old_watch, this); 534 path = path.Append(watch_entry.subdir); 535 } 536 537 UpdateRecursiveWatches(InotifyReader::kInvalidWatch, 538 false /* is directory? */); 539 } 540 541 void FilePathWatcherImpl::UpdateRecursiveWatches( 542 InotifyReader::Watch fired_watch, 543 bool is_dir) { 544 if (!recursive_) 545 return; 546 547 if (!DirectoryExists(target_)) { 548 RemoveRecursiveWatches(); 549 return; 550 } 551 552 // Check to see if this is a forced update or if some component of |target_| 553 // has changed. For these cases, redo the watches for |target_| and below. 554 if (!ContainsKey(recursive_paths_by_watch_, fired_watch)) { 555 UpdateRecursiveWatchesForPath(target_); 556 return; 557 } 558 559 // Underneath |target_|, only directory changes trigger watch updates. 560 if (!is_dir) 561 return; 562 563 const FilePath& changed_dir = recursive_paths_by_watch_[fired_watch]; 564 565 std::map<FilePath, InotifyReader::Watch>::iterator start_it = 566 recursive_watches_by_path_.lower_bound(changed_dir); 567 std::map<FilePath, InotifyReader::Watch>::iterator end_it = start_it; 568 for (; end_it != recursive_watches_by_path_.end(); ++end_it) { 569 const FilePath& cur_path = end_it->first; 570 if (!changed_dir.IsParent(cur_path)) 571 break; 572 if (!DirectoryExists(cur_path)) 573 g_inotify_reader.Get().RemoveWatch(end_it->second, this); 574 } 575 recursive_watches_by_path_.erase(start_it, end_it); 576 UpdateRecursiveWatchesForPath(changed_dir); 577 } 578 579 void FilePathWatcherImpl::UpdateRecursiveWatchesForPath(const FilePath& path) { 580 DCHECK(recursive_); 581 DCHECK(!path.empty()); 582 DCHECK(DirectoryExists(path)); 583 584 // Note: SHOW_SYM_LINKS exposes symlinks as symlinks, so they are ignored 585 // rather than followed. Following symlinks can easily lead to the undesirable 586 // situation where the entire file system is being watched. 587 FileEnumerator enumerator( 588 path, 589 true /* recursive enumeration */, 590 FileEnumerator::DIRECTORIES | FileEnumerator::SHOW_SYM_LINKS); 591 for (FilePath current = enumerator.Next(); 592 !current.empty(); 593 current = enumerator.Next()) { 594 DCHECK(enumerator.GetInfo().IsDirectory()); 595 596 if (!ContainsKey(recursive_watches_by_path_, current)) { 597 // Add new watches. 598 InotifyReader::Watch watch = 599 g_inotify_reader.Get().AddWatch(current, this); 600 TrackWatchForRecursion(watch, current); 601 } else { 602 // Update existing watches. 603 InotifyReader::Watch old_watch = recursive_watches_by_path_[current]; 604 DCHECK_NE(InotifyReader::kInvalidWatch, old_watch); 605 InotifyReader::Watch watch = 606 g_inotify_reader.Get().AddWatch(current, this); 607 if (watch != old_watch) { 608 g_inotify_reader.Get().RemoveWatch(old_watch, this); 609 recursive_paths_by_watch_.erase(old_watch); 610 recursive_watches_by_path_.erase(current); 611 TrackWatchForRecursion(watch, current); 612 } 613 } 614 } 615 } 616 617 void FilePathWatcherImpl::TrackWatchForRecursion(InotifyReader::Watch watch, 618 const FilePath& path) { 619 DCHECK(recursive_); 620 DCHECK(!path.empty()); 621 DCHECK(target_.IsParent(path)); 622 623 if (watch == InotifyReader::kInvalidWatch) 624 return; 625 626 DCHECK(!ContainsKey(recursive_paths_by_watch_, watch)); 627 DCHECK(!ContainsKey(recursive_watches_by_path_, path)); 628 recursive_paths_by_watch_[watch] = path; 629 recursive_watches_by_path_[path] = watch; 630 } 631 632 void FilePathWatcherImpl::RemoveRecursiveWatches() { 633 if (!recursive_) 634 return; 635 636 for (hash_map<InotifyReader::Watch, FilePath>::const_iterator it = 637 recursive_paths_by_watch_.begin(); 638 it != recursive_paths_by_watch_.end(); 639 ++it) { 640 g_inotify_reader.Get().RemoveWatch(it->first, this); 641 } 642 recursive_paths_by_watch_.clear(); 643 recursive_watches_by_path_.clear(); 644 } 645 646 void FilePathWatcherImpl::AddWatchForBrokenSymlink(const FilePath& path, 647 WatchEntry* watch_entry) { 648 DCHECK_EQ(InotifyReader::kInvalidWatch, watch_entry->watch); 649 FilePath link; 650 if (!ReadSymbolicLink(path, &link)) 651 return; 652 653 if (!link.IsAbsolute()) 654 link = path.DirName().Append(link); 655 656 // Try watching symlink target directory. If the link target is "/", then we 657 // shouldn't get here in normal situations and if we do, we'd watch "/" for 658 // changes to a component "/" which is harmless so no special treatment of 659 // this case is required. 660 InotifyReader::Watch watch = 661 g_inotify_reader.Get().AddWatch(link.DirName(), this); 662 if (watch == InotifyReader::kInvalidWatch) { 663 // TODO(craig) Symlinks only work if the parent directory for the target 664 // exist. Ideally we should make sure we've watched all the components of 665 // the symlink path for changes. See crbug.com/91561 for details. 666 DPLOG(WARNING) << "Watch failed for " << link.DirName().value(); 667 return; 668 } 669 watch_entry->watch = watch; 670 watch_entry->linkname = link.BaseName().value(); 671 } 672 673 bool FilePathWatcherImpl::HasValidWatchVector() const { 674 if (watches_.empty()) 675 return false; 676 for (size_t i = 0; i < watches_.size() - 1; ++i) { 677 if (watches_[i].subdir.empty()) 678 return false; 679 } 680 return watches_[watches_.size() - 1].subdir.empty(); 681 } 682 683 } // namespace 684 685 FilePathWatcher::FilePathWatcher() { 686 impl_ = new FilePathWatcherImpl(); 687 } 688 689 } // namespace base 690