Introduce new SPDY Version UMA histogram.
[chromium-blink-merge.git] / base / files / file_path_watcher_linux.cc
blob26eafb379c0795c802d25cc4b1c8fc82153a38ad
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"
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>
14 #include <algorithm>
15 #include <map>
16 #include <set>
17 #include <utility>
18 #include <vector>
20 #include "base/bind.h"
21 #include "base/containers/hash_tables.h"
22 #include "base/debug/trace_event.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/memory/scoped_ptr.h"
30 #include "base/message_loop/message_loop.h"
31 #include "base/message_loop/message_loop_proxy.h"
32 #include "base/posix/eintr_wrapper.h"
33 #include "base/synchronization/lock.h"
34 #include "base/threading/thread.h"
36 namespace base {
38 namespace {
40 class FilePathWatcherImpl;
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;
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);
54 // Remove |watch| if it's valid.
55 void RemoveWatch(Watch watch, FilePathWatcherImpl* watcher);
57 // Callback for InotifyReaderTask.
58 void OnInotifyEvent(const inotify_event* event);
60 private:
61 friend struct DefaultLazyInstanceTraits<InotifyReader>;
63 typedef std::set<FilePathWatcherImpl*> WatcherSet;
65 InotifyReader();
66 ~InotifyReader();
68 // We keep track of which delegates want to be notified on which watches.
69 hash_map<Watch, WatcherSet> watchers_;
71 // Lock to protect watchers_.
72 Lock lock_;
74 // Separate thread on which we run blocking read for inotify events.
75 Thread thread_;
77 // File descriptor returned by inotify_init.
78 const int inotify_fd_;
80 // Use self-pipe trick to unblock select during shutdown.
81 int shutdown_pipe_[2];
83 // Flag set to true when startup was successful.
84 bool valid_;
86 DISALLOW_COPY_AND_ASSIGN(InotifyReader);
89 class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate,
90 public MessageLoop::DestructionObserver {
91 public:
92 FilePathWatcherImpl();
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|.
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);
107 protected:
108 virtual ~FilePathWatcherImpl() {}
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 virtual bool Watch(const FilePath& path,
114 bool recursive,
115 const FilePathWatcher::Callback& callback) OVERRIDE;
117 // Cancel the watch. This unregisters the instance with InotifyReader.
118 virtual void Cancel() OVERRIDE;
120 // Cleans up and stops observing the message_loop() thread.
121 virtual void CancelOnMessageLoopThread() OVERRIDE;
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 virtual void WillDestroyCurrentMessageLoop() OVERRIDE;
128 // Inotify watches are installed for all directory components of |target_|. A
129 // WatchEntry instance holds the watch descriptor for a component and the
130 // subdirectory for that identifies the next component. If a symbolic link
131 // is being watched, the target of the link is also kept.
132 struct WatchEntry {
133 explicit WatchEntry(const FilePath::StringType& dirname)
134 : watch(InotifyReader::kInvalidWatch),
135 subdir(dirname) {}
137 InotifyReader::Watch watch;
138 FilePath::StringType subdir;
139 FilePath::StringType linkname;
141 typedef std::vector<WatchEntry> WatchVector;
143 // Reconfigure to watch for the most specific parent directory of |target_|
144 // that exists. Also calls UpdateRecursiveWatches() below.
145 void UpdateWatches();
147 // Reconfigure to recursively watch |target_| and all its sub-directories.
148 // - This is a no-op if the watch is not recursive.
149 // - If |target_| does not exist, then clear all the recursive watches.
150 // - Assuming |target_| exists, passing kInvalidWatch as |fired_watch| forces
151 // addition of recursive watches for |target_|.
152 // - Otherwise, only the directory associated with |fired_watch| and its
153 // sub-directories will be reconfigured.
154 void UpdateRecursiveWatches(InotifyReader::Watch fired_watch, bool is_dir);
156 // Enumerate recursively through |path| and add / update watches.
157 void UpdateRecursiveWatchesForPath(const FilePath& path);
159 // Do internal bookkeeping to update mappings between |watch| and its
160 // associated full path |path|.
161 void TrackWatchForRecursion(InotifyReader::Watch watch, const FilePath& path);
163 // Remove all the recursive watches.
164 void RemoveRecursiveWatches();
166 // |path| is a symlink to a non-existent target. Attempt to add a watch to
167 // the link target's parent directory. Returns true and update |watch_entry|
168 // on success.
169 bool AddWatchForBrokenSymlink(const FilePath& path, WatchEntry* watch_entry);
171 bool HasValidWatchVector() const;
173 // Callback to notify upon changes.
174 FilePathWatcher::Callback callback_;
176 // The file or directory we're supposed to watch.
177 FilePath target_;
179 bool recursive_;
181 // The vector of watches and next component names for all path components,
182 // starting at the root directory. The last entry corresponds to the watch for
183 // |target_| and always stores an empty next component name in |subdir|.
184 WatchVector watches_;
186 hash_map<InotifyReader::Watch, FilePath> recursive_paths_by_watch_;
187 std::map<FilePath, InotifyReader::Watch> recursive_watches_by_path_;
189 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl);
192 void InotifyReaderCallback(InotifyReader* reader, int inotify_fd,
193 int shutdown_fd) {
194 // Make sure the file descriptors are good for use with select().
195 CHECK_LE(0, inotify_fd);
196 CHECK_GT(FD_SETSIZE, inotify_fd);
197 CHECK_LE(0, shutdown_fd);
198 CHECK_GT(FD_SETSIZE, shutdown_fd);
200 debug::TraceLog::GetInstance()->SetCurrentThreadBlocksMessageLoop();
202 while (true) {
203 fd_set rfds;
204 FD_ZERO(&rfds);
205 FD_SET(inotify_fd, &rfds);
206 FD_SET(shutdown_fd, &rfds);
208 // Wait until some inotify events are available.
209 int select_result =
210 HANDLE_EINTR(select(std::max(inotify_fd, shutdown_fd) + 1,
211 &rfds, NULL, NULL, NULL));
212 if (select_result < 0) {
213 DPLOG(WARNING) << "select failed";
214 return;
217 if (FD_ISSET(shutdown_fd, &rfds))
218 return;
220 // Adjust buffer size to current event queue size.
221 int buffer_size;
222 int ioctl_result = HANDLE_EINTR(ioctl(inotify_fd, FIONREAD,
223 &buffer_size));
225 if (ioctl_result != 0) {
226 DPLOG(WARNING) << "ioctl failed";
227 return;
230 std::vector<char> buffer(buffer_size);
232 ssize_t bytes_read = HANDLE_EINTR(read(inotify_fd, &buffer[0],
233 buffer_size));
235 if (bytes_read < 0) {
236 DPLOG(WARNING) << "read from inotify fd failed";
237 return;
240 ssize_t i = 0;
241 while (i < bytes_read) {
242 inotify_event* event = reinterpret_cast<inotify_event*>(&buffer[i]);
243 size_t event_size = sizeof(inotify_event) + event->len;
244 DCHECK(i + event_size <= static_cast<size_t>(bytes_read));
245 reader->OnInotifyEvent(event);
246 i += event_size;
251 static LazyInstance<InotifyReader>::Leaky g_inotify_reader =
252 LAZY_INSTANCE_INITIALIZER;
254 InotifyReader::InotifyReader()
255 : thread_("inotify_reader"),
256 inotify_fd_(inotify_init()),
257 valid_(false) {
258 if (inotify_fd_ < 0)
259 PLOG(ERROR) << "inotify_init() failed";
261 shutdown_pipe_[0] = -1;
262 shutdown_pipe_[1] = -1;
263 if (inotify_fd_ >= 0 && pipe(shutdown_pipe_) == 0 && thread_.Start()) {
264 thread_.message_loop()->PostTask(
265 FROM_HERE,
266 Bind(&InotifyReaderCallback, this, inotify_fd_, shutdown_pipe_[0]));
267 valid_ = true;
271 InotifyReader::~InotifyReader() {
272 if (valid_) {
273 // Write to the self-pipe so that the select call in InotifyReaderTask
274 // returns.
275 ssize_t ret = HANDLE_EINTR(write(shutdown_pipe_[1], "", 1));
276 DPCHECK(ret > 0);
277 DCHECK_EQ(ret, 1);
278 thread_.Stop();
280 if (inotify_fd_ >= 0)
281 close(inotify_fd_);
282 if (shutdown_pipe_[0] >= 0)
283 close(shutdown_pipe_[0]);
284 if (shutdown_pipe_[1] >= 0)
285 close(shutdown_pipe_[1]);
288 InotifyReader::Watch InotifyReader::AddWatch(
289 const FilePath& path, FilePathWatcherImpl* watcher) {
290 if (!valid_)
291 return kInvalidWatch;
293 AutoLock auto_lock(lock_);
295 Watch watch = inotify_add_watch(inotify_fd_, path.value().c_str(),
296 IN_ATTRIB | IN_CREATE | IN_DELETE |
297 IN_CLOSE_WRITE | IN_MOVE |
298 IN_ONLYDIR);
300 if (watch == kInvalidWatch)
301 return kInvalidWatch;
303 watchers_[watch].insert(watcher);
305 return watch;
308 void InotifyReader::RemoveWatch(Watch watch, FilePathWatcherImpl* watcher) {
309 if (!valid_ || (watch == kInvalidWatch))
310 return;
312 AutoLock auto_lock(lock_);
314 watchers_[watch].erase(watcher);
316 if (watchers_[watch].empty()) {
317 watchers_.erase(watch);
318 inotify_rm_watch(inotify_fd_, watch);
322 void InotifyReader::OnInotifyEvent(const inotify_event* event) {
323 if (event->mask & IN_IGNORED)
324 return;
326 FilePath::StringType child(event->len ? event->name : FILE_PATH_LITERAL(""));
327 AutoLock auto_lock(lock_);
329 for (WatcherSet::iterator watcher = watchers_[event->wd].begin();
330 watcher != watchers_[event->wd].end();
331 ++watcher) {
332 (*watcher)->OnFilePathChanged(event->wd,
333 child,
334 event->mask & (IN_CREATE | IN_MOVED_TO),
335 event->mask & (IN_DELETE | IN_MOVED_FROM),
336 event->mask & IN_ISDIR);
340 FilePathWatcherImpl::FilePathWatcherImpl()
341 : recursive_(false) {
344 void FilePathWatcherImpl::OnFilePathChanged(InotifyReader::Watch fired_watch,
345 const FilePath::StringType& child,
346 bool created,
347 bool deleted,
348 bool is_dir) {
349 if (!message_loop()->BelongsToCurrentThread()) {
350 // Switch to message_loop() to access |watches_| safely.
351 message_loop()->PostTask(
352 FROM_HERE,
353 Bind(&FilePathWatcherImpl::OnFilePathChanged, this,
354 fired_watch, child, created, deleted, is_dir));
355 return;
358 // Check to see if CancelOnMessageLoopThread() has already been called.
359 // May happen when code flow reaches here from the PostTask() above.
360 if (watches_.empty()) {
361 DCHECK(target_.empty());
362 return;
365 DCHECK(MessageLoopForIO::current());
366 DCHECK(HasValidWatchVector());
368 // Used below to avoid multiple recursive updates.
369 bool did_update = false;
371 // Find the entry in |watches_| that corresponds to |fired_watch|.
372 for (size_t i = 0; i < watches_.size(); ++i) {
373 const WatchEntry& watch_entry = watches_[i];
374 if (fired_watch != watch_entry.watch)
375 continue;
377 // Check whether a path component of |target_| changed.
378 bool change_on_target_path =
379 child.empty() ||
380 (child == watch_entry.linkname) ||
381 (child == watch_entry.subdir);
383 // Check if the change references |target_| or a direct child of |target_|.
384 bool is_watch_for_target = watch_entry.subdir.empty();
385 bool target_changed =
386 (is_watch_for_target && (child == watch_entry.linkname)) ||
387 (is_watch_for_target && watch_entry.linkname.empty()) ||
388 (watch_entry.subdir == child && watches_[i + 1].subdir.empty());
390 // Update watches if a directory component of the |target_| path
391 // (dis)appears. Note that we don't add the additional restriction of
392 // checking the event mask to see if it is for a directory here as changes
393 // to symlinks on the target path will not have IN_ISDIR set in the event
394 // masks. As a result we may sometimes call UpdateWatches() unnecessarily.
395 if (change_on_target_path && (created || deleted) && !did_update) {
396 UpdateWatches();
397 did_update = true;
400 // Report the following events:
401 // - The target or a direct child of the target got changed (in case the
402 // watched path refers to a directory).
403 // - One of the parent directories got moved or deleted, since the target
404 // disappears in this case.
405 // - One of the parent directories appears. The event corresponding to
406 // the target appearing might have been missed in this case, so recheck.
407 if (target_changed ||
408 (change_on_target_path && deleted) ||
409 (change_on_target_path && created && PathExists(target_))) {
410 if (!did_update) {
411 UpdateRecursiveWatches(fired_watch, is_dir);
412 did_update = true;
414 callback_.Run(target_, false /* error */);
415 return;
419 if (ContainsKey(recursive_paths_by_watch_, fired_watch)) {
420 if (!did_update)
421 UpdateRecursiveWatches(fired_watch, is_dir);
422 callback_.Run(target_, false /* error */);
426 bool FilePathWatcherImpl::Watch(const FilePath& path,
427 bool recursive,
428 const FilePathWatcher::Callback& callback) {
429 DCHECK(target_.empty());
430 DCHECK(MessageLoopForIO::current());
432 set_message_loop(MessageLoopProxy::current().get());
433 callback_ = callback;
434 target_ = path;
435 recursive_ = recursive;
436 MessageLoop::current()->AddDestructionObserver(this);
438 std::vector<FilePath::StringType> comps;
439 target_.GetComponents(&comps);
440 DCHECK(!comps.empty());
441 for (size_t i = 1; i < comps.size(); ++i)
442 watches_.push_back(WatchEntry(comps[i]));
443 watches_.push_back(WatchEntry(FilePath::StringType()));
444 UpdateWatches();
445 return true;
448 void FilePathWatcherImpl::Cancel() {
449 if (callback_.is_null()) {
450 // Watch was never called, or the message_loop() thread is already gone.
451 set_cancelled();
452 return;
455 // Switch to the message_loop() if necessary so we can access |watches_|.
456 if (!message_loop()->BelongsToCurrentThread()) {
457 message_loop()->PostTask(FROM_HERE,
458 Bind(&FilePathWatcher::CancelWatch,
459 make_scoped_refptr(this)));
460 } else {
461 CancelOnMessageLoopThread();
465 void FilePathWatcherImpl::CancelOnMessageLoopThread() {
466 DCHECK(message_loop()->BelongsToCurrentThread());
467 set_cancelled();
469 if (!callback_.is_null()) {
470 MessageLoop::current()->RemoveDestructionObserver(this);
471 callback_.Reset();
474 for (size_t i = 0; i < watches_.size(); ++i)
475 g_inotify_reader.Get().RemoveWatch(watches_[i].watch, this);
476 watches_.clear();
477 target_.clear();
479 if (recursive_)
480 RemoveRecursiveWatches();
483 void FilePathWatcherImpl::WillDestroyCurrentMessageLoop() {
484 CancelOnMessageLoopThread();
487 void FilePathWatcherImpl::UpdateWatches() {
488 // Ensure this runs on the message_loop() exclusively in order to avoid
489 // concurrency issues.
490 DCHECK(message_loop()->BelongsToCurrentThread());
491 DCHECK(HasValidWatchVector());
493 // Walk the list of watches and update them as we go.
494 FilePath path(FILE_PATH_LITERAL("/"));
495 bool path_valid = true;
496 for (size_t i = 0; i < watches_.size(); ++i) {
497 WatchEntry& watch_entry = watches_[i];
498 InotifyReader::Watch old_watch = watch_entry.watch;
499 watch_entry.watch = InotifyReader::kInvalidWatch;
500 watch_entry.linkname.clear();
501 if (path_valid) {
502 watch_entry.watch = g_inotify_reader.Get().AddWatch(path, this);
503 if (watch_entry.watch == InotifyReader::kInvalidWatch) {
504 if (IsLink(path)) {
505 path_valid = AddWatchForBrokenSymlink(path, &watch_entry);
506 } else {
507 path_valid = false;
511 if (old_watch != watch_entry.watch)
512 g_inotify_reader.Get().RemoveWatch(old_watch, this);
513 path = path.Append(watch_entry.subdir);
516 UpdateRecursiveWatches(InotifyReader::kInvalidWatch,
517 false /* is directory? */);
520 void FilePathWatcherImpl::UpdateRecursiveWatches(
521 InotifyReader::Watch fired_watch,
522 bool is_dir) {
523 if (!recursive_)
524 return;
526 if (!DirectoryExists(target_)) {
527 RemoveRecursiveWatches();
528 return;
531 // Check to see if this is a forced update or if some component of |target_|
532 // has changed. For these cases, redo the watches for |target_| and below.
533 if (!ContainsKey(recursive_paths_by_watch_, fired_watch)) {
534 UpdateRecursiveWatchesForPath(target_);
535 return;
538 // Underneath |target_|, only directory changes trigger watch updates.
539 if (!is_dir)
540 return;
542 const FilePath& changed_dir = recursive_paths_by_watch_[fired_watch];
544 std::map<FilePath, InotifyReader::Watch>::iterator start_it =
545 recursive_watches_by_path_.lower_bound(changed_dir);
546 std::map<FilePath, InotifyReader::Watch>::iterator end_it = start_it;
547 for (; end_it != recursive_watches_by_path_.end(); ++end_it) {
548 const FilePath& cur_path = end_it->first;
549 if (!changed_dir.IsParent(cur_path))
550 break;
551 if (!DirectoryExists(cur_path))
552 g_inotify_reader.Get().RemoveWatch(end_it->second, this);
554 recursive_watches_by_path_.erase(start_it, end_it);
555 UpdateRecursiveWatchesForPath(changed_dir);
558 void FilePathWatcherImpl::UpdateRecursiveWatchesForPath(const FilePath& path) {
559 DCHECK(recursive_);
560 DCHECK(!path.empty());
561 DCHECK(DirectoryExists(path));
563 // Note: SHOW_SYM_LINKS exposes symlinks as symlinks, so they are ignored
564 // rather than followed. Following symlinks can easily lead to the undesirable
565 // situation where the entire file system is being watched.
566 FileEnumerator enumerator(
567 path,
568 true /* recursive enumeration */,
569 FileEnumerator::DIRECTORIES | FileEnumerator::SHOW_SYM_LINKS);
570 for (FilePath current = enumerator.Next();
571 !current.empty();
572 current = enumerator.Next()) {
573 DCHECK(enumerator.GetInfo().IsDirectory());
575 if (!ContainsKey(recursive_watches_by_path_, current)) {
576 // Add new watches.
577 InotifyReader::Watch watch =
578 g_inotify_reader.Get().AddWatch(current, this);
579 TrackWatchForRecursion(watch, current);
580 } else {
581 // Update existing watches.
582 InotifyReader::Watch old_watch = recursive_watches_by_path_[current];
583 DCHECK_NE(InotifyReader::kInvalidWatch, old_watch);
584 InotifyReader::Watch watch =
585 g_inotify_reader.Get().AddWatch(current, this);
586 if (watch != old_watch) {
587 g_inotify_reader.Get().RemoveWatch(old_watch, this);
588 recursive_paths_by_watch_.erase(old_watch);
589 recursive_watches_by_path_.erase(current);
590 TrackWatchForRecursion(watch, current);
596 void FilePathWatcherImpl::TrackWatchForRecursion(InotifyReader::Watch watch,
597 const FilePath& path) {
598 DCHECK(recursive_);
599 DCHECK(!path.empty());
600 DCHECK(target_.IsParent(path));
602 if (watch == InotifyReader::kInvalidWatch)
603 return;
605 DCHECK(!ContainsKey(recursive_paths_by_watch_, watch));
606 DCHECK(!ContainsKey(recursive_watches_by_path_, path));
607 recursive_paths_by_watch_[watch] = path;
608 recursive_watches_by_path_[path] = watch;
611 void FilePathWatcherImpl::RemoveRecursiveWatches() {
612 if (!recursive_)
613 return;
615 for (hash_map<InotifyReader::Watch, FilePath>::const_iterator it =
616 recursive_paths_by_watch_.begin();
617 it != recursive_paths_by_watch_.end();
618 ++it) {
619 g_inotify_reader.Get().RemoveWatch(it->first, this);
621 recursive_paths_by_watch_.clear();
622 recursive_watches_by_path_.clear();
625 bool FilePathWatcherImpl::AddWatchForBrokenSymlink(const FilePath& path,
626 WatchEntry* watch_entry) {
627 DCHECK_EQ(InotifyReader::kInvalidWatch, watch_entry->watch);
628 FilePath link;
629 if (!ReadSymbolicLink(path, &link))
630 return false;
632 if (!link.IsAbsolute())
633 link = path.DirName().Append(link);
635 // Try watching symlink target directory. If the link target is "/", then we
636 // shouldn't get here in normal situations and if we do, we'd watch "/" for
637 // changes to a component "/" which is harmless so no special treatment of
638 // this case is required.
639 InotifyReader::Watch watch =
640 g_inotify_reader.Get().AddWatch(link.DirName(), this);
641 if (watch == InotifyReader::kInvalidWatch) {
642 // TODO(craig) Symlinks only work if the parent directory for the target
643 // exist. Ideally we should make sure we've watched all the components of
644 // the symlink path for changes. See crbug.com/91561 for details.
645 DPLOG(WARNING) << "Watch failed for " << link.DirName().value();
646 return false;
648 watch_entry->watch = watch;
649 watch_entry->linkname = link.BaseName().value();
650 return true;
653 bool FilePathWatcherImpl::HasValidWatchVector() const {
654 if (watches_.empty())
655 return false;
656 for (size_t i = 0; i < watches_.size() - 1; ++i) {
657 if (watches_[i].subdir.empty())
658 return false;
660 return watches_[watches_.size() - 1].subdir.empty();
663 } // namespace
665 FilePathWatcher::FilePathWatcher() {
666 impl_ = new FilePathWatcherImpl();
669 } // namespace base