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/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"
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 virtual ~FilePathWatcherImpl() {}
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
,
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.
133 explicit WatchEntry(const FilePath::StringType
& dirname
)
134 : watch(InotifyReader::kInvalidWatch
),
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|
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.
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
,
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();
205 FD_SET(inotify_fd
, &rfds
);
206 FD_SET(shutdown_fd
, &rfds
);
208 // Wait until some inotify events are available.
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";
217 if (FD_ISSET(shutdown_fd
, &rfds
))
220 // Adjust buffer size to current event queue size.
222 int ioctl_result
= HANDLE_EINTR(ioctl(inotify_fd
, FIONREAD
,
225 if (ioctl_result
!= 0) {
226 DPLOG(WARNING
) << "ioctl failed";
230 std::vector
<char> buffer(buffer_size
);
232 ssize_t bytes_read
= HANDLE_EINTR(read(inotify_fd
, &buffer
[0],
235 if (bytes_read
< 0) {
236 DPLOG(WARNING
) << "read from inotify fd failed";
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
);
251 static LazyInstance
<InotifyReader
>::Leaky g_inotify_reader
=
252 LAZY_INSTANCE_INITIALIZER
;
254 InotifyReader::InotifyReader()
255 : thread_("inotify_reader"),
256 inotify_fd_(inotify_init()),
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(
266 Bind(&InotifyReaderCallback
, this, inotify_fd_
, shutdown_pipe_
[0]));
271 InotifyReader::~InotifyReader() {
273 // Write to the self-pipe so that the select call in InotifyReaderTask
275 ssize_t ret
= HANDLE_EINTR(write(shutdown_pipe_
[1], "", 1));
280 if (inotify_fd_
>= 0)
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
) {
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
|
300 if (watch
== kInvalidWatch
)
301 return kInvalidWatch
;
303 watchers_
[watch
].insert(watcher
);
308 void InotifyReader::RemoveWatch(Watch watch
, FilePathWatcherImpl
* watcher
) {
309 if (!valid_
|| (watch
== kInvalidWatch
))
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
)
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();
332 (*watcher
)->OnFilePathChanged(event
->wd
,
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
,
349 if (!message_loop()->BelongsToCurrentThread()) {
350 // Switch to message_loop() to access |watches_| safely.
351 message_loop()->PostTask(
353 Bind(&FilePathWatcherImpl::OnFilePathChanged
, this,
354 fired_watch
, child
, created
, deleted
, is_dir
));
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());
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
)
377 // Check whether a path component of |target_| changed.
378 bool change_on_target_path
=
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
) {
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_
))) {
411 UpdateRecursiveWatches(fired_watch
, is_dir
);
414 callback_
.Run(target_
, false /* error */);
419 if (ContainsKey(recursive_paths_by_watch_
, fired_watch
)) {
421 UpdateRecursiveWatches(fired_watch
, is_dir
);
422 callback_
.Run(target_
, false /* error */);
426 bool FilePathWatcherImpl::Watch(const FilePath
& path
,
428 const FilePathWatcher::Callback
& callback
) {
429 DCHECK(target_
.empty());
430 DCHECK(MessageLoopForIO::current());
432 set_message_loop(MessageLoopProxy::current().get());
433 callback_
= callback
;
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()));
448 void FilePathWatcherImpl::Cancel() {
449 if (callback_
.is_null()) {
450 // Watch was never called, or the message_loop() thread is already gone.
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)));
461 CancelOnMessageLoopThread();
465 void FilePathWatcherImpl::CancelOnMessageLoopThread() {
466 DCHECK(message_loop()->BelongsToCurrentThread());
469 if (!callback_
.is_null()) {
470 MessageLoop::current()->RemoveDestructionObserver(this);
474 for (size_t i
= 0; i
< watches_
.size(); ++i
)
475 g_inotify_reader
.Get().RemoveWatch(watches_
[i
].watch
, this);
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();
502 watch_entry
.watch
= g_inotify_reader
.Get().AddWatch(path
, this);
503 if (watch_entry
.watch
== InotifyReader::kInvalidWatch
) {
505 path_valid
= AddWatchForBrokenSymlink(path
, &watch_entry
);
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
,
526 if (!DirectoryExists(target_
)) {
527 RemoveRecursiveWatches();
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_
);
538 // Underneath |target_|, only directory changes trigger watch updates.
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
))
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
) {
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(
568 true /* recursive enumeration */,
569 FileEnumerator::DIRECTORIES
| FileEnumerator::SHOW_SYM_LINKS
);
570 for (FilePath current
= enumerator
.Next();
572 current
= enumerator
.Next()) {
573 DCHECK(enumerator
.GetInfo().IsDirectory());
575 if (!ContainsKey(recursive_watches_by_path_
, current
)) {
577 InotifyReader::Watch watch
=
578 g_inotify_reader
.Get().AddWatch(current
, this);
579 TrackWatchForRecursion(watch
, current
);
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
) {
599 DCHECK(!path
.empty());
600 DCHECK(target_
.IsParent(path
));
602 if (watch
== InotifyReader::kInvalidWatch
)
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() {
615 for (hash_map
<InotifyReader::Watch
, FilePath
>::const_iterator it
=
616 recursive_paths_by_watch_
.begin();
617 it
!= recursive_paths_by_watch_
.end();
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
);
629 if (!ReadSymbolicLink(path
, &link
))
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();
648 watch_entry
->watch
= watch
;
649 watch_entry
->linkname
= link
.BaseName().value();
653 bool FilePathWatcherImpl::HasValidWatchVector() const {
654 if (watches_
.empty())
656 for (size_t i
= 0; i
< watches_
.size() - 1; ++i
) {
657 if (watches_
[i
].subdir
.empty())
660 return watches_
[watches_
.size() - 1].subdir
.empty();
665 FilePathWatcher::FilePathWatcher() {
666 impl_
= new FilePathWatcherImpl();