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"
9 #include <sys/inotify.h>
10 #include <sys/ioctl.h>
11 #include <sys/select.h>
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"
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
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
);
61 friend struct DefaultLazyInstanceTraits
<InotifyReader
>;
63 typedef std::set
<FilePathWatcherImpl
*> WatcherSet
;
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_.
74 // Separate thread on which we run blocking read for inotify events.
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.
86 DISALLOW_COPY_AND_ASSIGN(InotifyReader
);
89 class FilePathWatcherImpl
: public FilePathWatcher::PlatformDelegate
,
90 public MessageLoop::DestructionObserver
{
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
,
108 ~FilePathWatcherImpl() override
{}
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
,
115 const FilePathWatcher::Callback
& callback
) override
;
117 // Cancel the watch. This unregisters the instance with InotifyReader.
118 void Cancel() override
;
120 // Cleans up and stops observing the message_loop() thread.
121 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 void WillDestroyCurrentMessageLoop() override
;
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.
136 explicit WatchEntry(const FilePath::StringType
& dirname
)
137 : watch(InotifyReader::kInvalidWatch
),
140 InotifyReader::Watch watch
;
141 FilePath::StringType subdir
;
142 FilePath::StringType linkname
;
144 typedef std::vector
<WatchEntry
> WatchVector
;
146 // Reconfigure to watch for the most specific parent directory of |target_|
147 // that exists. Also calls UpdateRecursiveWatches() below.
148 void UpdateWatches();
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
);
159 // Enumerate recursively through |path| and add / update watches.
160 void UpdateRecursiveWatchesForPath(const FilePath
& path
);
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
);
166 // Remove all the recursive watches.
167 void RemoveRecursiveWatches();
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|
172 bool AddWatchForBrokenSymlink(const FilePath
& path
, WatchEntry
* watch_entry
);
174 bool HasValidWatchVector() const;
176 // Callback to notify upon changes.
177 FilePathWatcher::Callback callback_
;
179 // The file or directory we're supposed to watch.
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_
;
189 hash_map
<InotifyReader::Watch
, FilePath
> recursive_paths_by_watch_
;
190 std::map
<FilePath
, InotifyReader::Watch
> recursive_watches_by_path_
;
192 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl
);
195 void InotifyReaderCallback(InotifyReader
* reader
, int inotify_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
);
203 trace_event::TraceLog::GetInstance()->SetCurrentThreadBlocksMessageLoop();
208 FD_SET(inotify_fd
, &rfds
);
209 FD_SET(shutdown_fd
, &rfds
);
211 // Wait until some inotify events are available.
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";
220 if (FD_ISSET(shutdown_fd
, &rfds
))
223 // Adjust buffer size to current event queue size.
225 int ioctl_result
= HANDLE_EINTR(ioctl(inotify_fd
, FIONREAD
,
228 if (ioctl_result
!= 0) {
229 DPLOG(WARNING
) << "ioctl failed";
233 std::vector
<char> buffer(buffer_size
);
235 ssize_t bytes_read
= HANDLE_EINTR(read(inotify_fd
, &buffer
[0],
238 if (bytes_read
< 0) {
239 DPLOG(WARNING
) << "read from inotify fd failed";
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
);
254 static LazyInstance
<InotifyReader
>::Leaky g_inotify_reader
=
255 LAZY_INSTANCE_INITIALIZER
;
257 InotifyReader::InotifyReader()
258 : thread_("inotify_reader"),
259 inotify_fd_(inotify_init()),
262 PLOG(ERROR
) << "inotify_init() failed";
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(
269 Bind(&InotifyReaderCallback
, this, inotify_fd_
, shutdown_pipe_
[0]));
274 InotifyReader::~InotifyReader() {
276 // Write to the self-pipe so that the select call in InotifyReaderTask
278 ssize_t ret
= HANDLE_EINTR(write(shutdown_pipe_
[1], "", 1));
283 if (inotify_fd_
>= 0)
285 if (shutdown_pipe_
[0] >= 0)
286 close(shutdown_pipe_
[0]);
287 if (shutdown_pipe_
[1] >= 0)
288 close(shutdown_pipe_
[1]);
291 InotifyReader::Watch
InotifyReader::AddWatch(
292 const FilePath
& path
, FilePathWatcherImpl
* watcher
) {
294 return kInvalidWatch
;
296 AutoLock
auto_lock(lock_
);
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
|
303 if (watch
== kInvalidWatch
)
304 return kInvalidWatch
;
306 watchers_
[watch
].insert(watcher
);
311 void InotifyReader::RemoveWatch(Watch watch
, FilePathWatcherImpl
* watcher
) {
312 if (!valid_
|| (watch
== kInvalidWatch
))
315 AutoLock
auto_lock(lock_
);
317 watchers_
[watch
].erase(watcher
);
319 if (watchers_
[watch
].empty()) {
320 watchers_
.erase(watch
);
321 inotify_rm_watch(inotify_fd_
, watch
);
325 void InotifyReader::OnInotifyEvent(const inotify_event
* event
) {
326 if (event
->mask
& IN_IGNORED
)
329 FilePath::StringType
child(event
->len
? event
->name
: FILE_PATH_LITERAL(""));
330 AutoLock
auto_lock(lock_
);
332 for (WatcherSet::iterator watcher
= watchers_
[event
->wd
].begin();
333 watcher
!= watchers_
[event
->wd
].end();
335 (*watcher
)->OnFilePathChanged(event
->wd
,
337 event
->mask
& (IN_CREATE
| IN_MOVED_TO
),
338 event
->mask
& (IN_DELETE
| IN_MOVED_FROM
),
339 event
->mask
& IN_ISDIR
);
343 FilePathWatcherImpl::FilePathWatcherImpl()
344 : recursive_(false) {
347 void FilePathWatcherImpl::OnFilePathChanged(InotifyReader::Watch fired_watch
,
348 const FilePath::StringType
& child
,
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
));
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());
367 DCHECK(MessageLoopForIO::current());
368 DCHECK(HasValidWatchVector());
370 // Used below to avoid multiple recursive updates.
371 bool did_update
= false;
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
)
379 // Check whether a path component of |target_| changed.
380 bool change_on_target_path
=
382 (child
== watch_entry
.linkname
) ||
383 (child
== watch_entry
.subdir
);
385 // Check if the change references |target_| or a direct child of |target_|.
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
);
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
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
;
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;
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
) {
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_
))) {
433 UpdateRecursiveWatches(fired_watch
, is_dir
);
436 callback_
.Run(target_
, false /* error */);
441 if (ContainsKey(recursive_paths_by_watch_
, fired_watch
)) {
443 UpdateRecursiveWatches(fired_watch
, is_dir
);
444 callback_
.Run(target_
, false /* error */);
448 bool FilePathWatcherImpl::Watch(const FilePath
& path
,
450 const FilePathWatcher::Callback
& callback
) {
451 DCHECK(target_
.empty());
452 DCHECK(MessageLoopForIO::current());
454 set_task_runner(ThreadTaskRunnerHandle::Get());
455 callback_
= callback
;
457 recursive_
= recursive
;
458 MessageLoop::current()->AddDestructionObserver(this);
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()));
470 void FilePathWatcherImpl::Cancel() {
471 if (callback_
.is_null()) {
472 // Watch was never called, or the message_loop() thread is already gone.
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)));
482 CancelOnMessageLoopThread();
486 void FilePathWatcherImpl::CancelOnMessageLoopThread() {
487 DCHECK(task_runner()->BelongsToCurrentThread());
490 if (!callback_
.is_null()) {
491 MessageLoop::current()->RemoveDestructionObserver(this);
495 for (size_t i
= 0; i
< watches_
.size(); ++i
)
496 g_inotify_reader
.Get().RemoveWatch(watches_
[i
].watch
, this);
501 RemoveRecursiveWatches();
504 void FilePathWatcherImpl::WillDestroyCurrentMessageLoop() {
505 CancelOnMessageLoopThread();
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());
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();
523 watch_entry
.watch
= g_inotify_reader
.Get().AddWatch(path
, this);
524 if (watch_entry
.watch
== InotifyReader::kInvalidWatch
) {
526 path_valid
= AddWatchForBrokenSymlink(path
, &watch_entry
);
532 if (old_watch
!= watch_entry
.watch
)
533 g_inotify_reader
.Get().RemoveWatch(old_watch
, this);
534 path
= path
.Append(watch_entry
.subdir
);
537 UpdateRecursiveWatches(InotifyReader::kInvalidWatch
,
538 false /* is directory? */);
541 void FilePathWatcherImpl::UpdateRecursiveWatches(
542 InotifyReader::Watch fired_watch
,
547 if (!DirectoryExists(target_
)) {
548 RemoveRecursiveWatches();
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_
);
559 // Underneath |target_|, only directory changes trigger watch updates.
563 const FilePath
& changed_dir
= recursive_paths_by_watch_
[fired_watch
];
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
))
572 if (!DirectoryExists(cur_path
))
573 g_inotify_reader
.Get().RemoveWatch(end_it
->second
, this);
575 recursive_watches_by_path_
.erase(start_it
, end_it
);
576 UpdateRecursiveWatchesForPath(changed_dir
);
579 void FilePathWatcherImpl::UpdateRecursiveWatchesForPath(const FilePath
& path
) {
581 DCHECK(!path
.empty());
582 DCHECK(DirectoryExists(path
));
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(
589 true /* recursive enumeration */,
590 FileEnumerator::DIRECTORIES
| FileEnumerator::SHOW_SYM_LINKS
);
591 for (FilePath current
= enumerator
.Next();
593 current
= enumerator
.Next()) {
594 DCHECK(enumerator
.GetInfo().IsDirectory());
596 if (!ContainsKey(recursive_watches_by_path_
, current
)) {
598 InotifyReader::Watch watch
=
599 g_inotify_reader
.Get().AddWatch(current
, this);
600 TrackWatchForRecursion(watch
, current
);
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
);
617 void FilePathWatcherImpl::TrackWatchForRecursion(InotifyReader::Watch watch
,
618 const FilePath
& path
) {
620 DCHECK(!path
.empty());
621 DCHECK(target_
.IsParent(path
));
623 if (watch
== InotifyReader::kInvalidWatch
)
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
;
632 void FilePathWatcherImpl::RemoveRecursiveWatches() {
636 for (hash_map
<InotifyReader::Watch
, FilePath
>::const_iterator it
=
637 recursive_paths_by_watch_
.begin();
638 it
!= recursive_paths_by_watch_
.end();
640 g_inotify_reader
.Get().RemoveWatch(it
->first
, this);
642 recursive_paths_by_watch_
.clear();
643 recursive_watches_by_path_
.clear();
646 bool FilePathWatcherImpl::AddWatchForBrokenSymlink(const FilePath
& path
,
647 WatchEntry
* watch_entry
) {
648 DCHECK_EQ(InotifyReader::kInvalidWatch
, watch_entry
->watch
);
650 if (!ReadSymbolicLink(path
, &link
))
653 if (!link
.IsAbsolute())
654 link
= path
.DirName().Append(link
);
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();
669 watch_entry
->watch
= watch
;
670 watch_entry
->linkname
= link
.BaseName().value();
674 bool FilePathWatcherImpl::HasValidWatchVector() const {
675 if (watches_
.empty())
677 for (size_t i
= 0; i
< watches_
.size() - 1; ++i
) {
678 if (watches_
[i
].subdir
.empty())
681 return watches_
[watches_
.size() - 1].subdir
.empty();
686 FilePathWatcher::FilePathWatcher() {
687 impl_
= new FilePathWatcherImpl();