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