file_path_watcher_inotify.cc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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. #include "base/files/file_path_watcher.h"
  5. #include <errno.h>
  6. #include <poll.h>
  7. #include <stddef.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. #include <algorithm>
  14. #include <array>
  15. #include <fstream>
  16. #include <map>
  17. #include <memory>
  18. #include <set>
  19. #include <unordered_map>
  20. #include <utility>
  21. #include <vector>
  22. #include "base/bind.h"
  23. #include "base/containers/contains.h"
  24. #include "base/files/file_enumerator.h"
  25. #include "base/files/file_path.h"
  26. #include "base/files/file_path_watcher_inotify.h"
  27. #include "base/files/file_util.h"
  28. #include "base/lazy_instance.h"
  29. #include "base/location.h"
  30. #include "base/logging.h"
  31. #include "base/memory/ptr_util.h"
  32. #include "base/memory/scoped_refptr.h"
  33. #include "base/memory/weak_ptr.h"
  34. #include "base/posix/eintr_wrapper.h"
  35. #include "base/synchronization/lock.h"
  36. #include "base/task/single_thread_task_runner.h"
  37. #include "base/threading/platform_thread.h"
  38. #include "base/threading/scoped_blocking_call.h"
  39. #include "base/threading/sequenced_task_runner_handle.h"
  40. #include "base/trace_event/base_tracing.h"
  41. #include "build/build_config.h"
  42. namespace base {
  43. namespace {
  44. #if !BUILDFLAG(IS_FUCHSIA)
  45. // The /proc path to max_user_watches.
  46. constexpr char kInotifyMaxUserWatchesPath[] =
  47. "/proc/sys/fs/inotify/max_user_watches";
  48. // This is a soft limit. If there are more than |kExpectedFilePathWatches|
  49. // FilePathWatchers for a user, than they might affect each other's inotify
  50. // watchers limit.
  51. constexpr size_t kExpectedFilePathWatchers = 16u;
  52. // The default max inotify watchers limit per user, if reading
  53. // /proc/sys/fs/inotify/max_user_watches fails.
  54. constexpr size_t kDefaultInotifyMaxUserWatches = 8192u;
  55. #endif // !BUILDFLAG(IS_FUCHSIA)
  56. class FilePathWatcherImpl;
  57. class InotifyReader;
  58. // Used by test to override inotify watcher limit.
  59. size_t g_override_max_inotify_watches = 0u;
  60. // Get the maximum number of inotify watches can be used by a FilePathWatcher
  61. // instance. This is based on /proc/sys/fs/inotify/max_user_watches entry.
  62. size_t GetMaxNumberOfInotifyWatches() {
  63. #if BUILDFLAG(IS_FUCHSIA)
  64. // Fuchsia has no limit on the number of watches.
  65. return std::numeric_limits<int>::max();
  66. #else
  67. static const size_t max = []() {
  68. size_t max_number_of_inotify_watches = 0u;
  69. std::ifstream in(kInotifyMaxUserWatchesPath);
  70. if (!in.is_open() || !(in >> max_number_of_inotify_watches)) {
  71. LOG(ERROR) << "Failed to read " << kInotifyMaxUserWatchesPath;
  72. return kDefaultInotifyMaxUserWatches / kExpectedFilePathWatchers;
  73. }
  74. return max_number_of_inotify_watches / kExpectedFilePathWatchers;
  75. }();
  76. return g_override_max_inotify_watches ? g_override_max_inotify_watches : max;
  77. #endif // if BUILDFLAG(IS_FUCHSIA)
  78. }
  79. class InotifyReaderThreadDelegate final : public PlatformThread::Delegate {
  80. public:
  81. explicit InotifyReaderThreadDelegate(int inotify_fd)
  82. : inotify_fd_(inotify_fd) {}
  83. InotifyReaderThreadDelegate(const InotifyReaderThreadDelegate&) = delete;
  84. InotifyReaderThreadDelegate& operator=(const InotifyReaderThreadDelegate&) =
  85. delete;
  86. ~InotifyReaderThreadDelegate() override = default;
  87. private:
  88. void ThreadMain() override;
  89. const int inotify_fd_;
  90. };
  91. // Singleton to manage all inotify watches.
  92. // TODO(tony): It would be nice if this wasn't a singleton.
  93. // http://crbug.com/38174
  94. class InotifyReader {
  95. public:
  96. // Watch descriptor used by AddWatch() and RemoveWatch().
  97. #if BUILDFLAG(IS_ANDROID)
  98. using Watch = uint32_t;
  99. #else
  100. using Watch = int;
  101. #endif
  102. static constexpr Watch kInvalidWatch = static_cast<Watch>(-1);
  103. static constexpr Watch kWatchLimitExceeded = static_cast<Watch>(-2);
  104. InotifyReader(const InotifyReader&) = delete;
  105. InotifyReader& operator=(const InotifyReader&) = delete;
  106. // Watch directory |path| for changes. |watcher| will be notified on each
  107. // change. Returns |kInvalidWatch| on failure.
  108. Watch AddWatch(const FilePath& path, FilePathWatcherImpl* watcher);
  109. // Remove |watch| if it's valid.
  110. void RemoveWatch(Watch watch, FilePathWatcherImpl* watcher);
  111. // Invoked on "inotify_reader" thread to notify relevant watchers.
  112. void OnInotifyEvent(const inotify_event* event);
  113. // Returns true if any paths are actively being watched.
  114. bool HasWatches();
  115. private:
  116. friend struct LazyInstanceTraitsBase<InotifyReader>;
  117. // Record of watchers tracked for watch descriptors.
  118. struct WatcherEntry {
  119. scoped_refptr<SequencedTaskRunner> task_runner;
  120. WeakPtr<FilePathWatcherImpl> watcher;
  121. };
  122. InotifyReader();
  123. // There is no destructor because |g_inotify_reader| is a
  124. // base::LazyInstace::Leaky object. Having a destructor causes build
  125. // issues with GCC 6 (http://crbug.com/636346).
  126. // Returns true on successful thread creation.
  127. bool StartThread();
  128. Lock lock_;
  129. // Tracks which FilePathWatcherImpls to be notified on which watches.
  130. // The tracked FilePathWatcherImpl is keyed by raw pointers for fast look up
  131. // and mapped to a WatchEntry that is used to safely post a notification.
  132. std::unordered_map<Watch, std::map<FilePathWatcherImpl*, WatcherEntry>>
  133. watchers_ GUARDED_BY(lock_);
  134. // File descriptor returned by inotify_init.
  135. const int inotify_fd_;
  136. // Thread delegate for the Inotify thread.
  137. InotifyReaderThreadDelegate thread_delegate_;
  138. // Flag set to true when startup was successful.
  139. bool valid_ = false;
  140. };
  141. class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate {
  142. public:
  143. FilePathWatcherImpl();
  144. FilePathWatcherImpl(const FilePathWatcherImpl&) = delete;
  145. FilePathWatcherImpl& operator=(const FilePathWatcherImpl&) = delete;
  146. ~FilePathWatcherImpl() override;
  147. // Called for each event coming from the watch on the original thread.
  148. // |fired_watch| identifies the watch that fired, |child| indicates what has
  149. // changed, and is relative to the currently watched path for |fired_watch|.
  150. //
  151. // |created| is true if the object appears.
  152. // |deleted| is true if the object disappears.
  153. // |is_dir| is true if the object is a directory.
  154. void OnFilePathChanged(InotifyReader::Watch fired_watch,
  155. const FilePath::StringType& child,
  156. bool created,
  157. bool deleted,
  158. bool is_dir);
  159. // Returns whether the number of inotify watches of this FilePathWatcherImpl
  160. // would exceed the limit if adding one more.
  161. bool WouldExceedWatchLimit() const;
  162. // Returns the task runner to be used with this.
  163. scoped_refptr<SequencedTaskRunner> GetTaskRunner() const;
  164. // Returns the WeakPtr of this, must be called on the original sequence.
  165. WeakPtr<FilePathWatcherImpl> GetWeakPtr() const;
  166. private:
  167. // Start watching |path| for changes and notify |delegate| on each change.
  168. // Returns true if watch for |path| has been added successfully.
  169. bool Watch(const FilePath& path,
  170. Type type,
  171. const FilePathWatcher::Callback& callback) override;
  172. // Cancel the watch. This unregisters the instance with InotifyReader.
  173. void Cancel() override;
  174. // Inotify watches are installed for all directory components of |target_|.
  175. // A WatchEntry instance holds:
  176. // - |watch|: the watch descriptor for a component.
  177. // - |subdir|: the subdirectory that identifies the next component.
  178. // - For the last component, there is no next component, so it is empty.
  179. // - |linkname|: the target of the symlink.
  180. // - Only if the target being watched is a symbolic link.
  181. struct WatchEntry {
  182. explicit WatchEntry(const FilePath::StringType& dirname)
  183. : watch(InotifyReader::kInvalidWatch), subdir(dirname) {}
  184. InotifyReader::Watch watch;
  185. FilePath::StringType subdir;
  186. FilePath::StringType linkname;
  187. };
  188. // Reconfigure to watch for the most specific parent directory of |target_|
  189. // that exists. Also calls UpdateRecursiveWatches() below. Returns true if
  190. // watch limit is not hit. Otherwise, returns false.
  191. [[nodiscard]] bool UpdateWatches();
  192. // Reconfigure to recursively watch |target_| and all its sub-directories.
  193. // - This is a no-op if the watch is not recursive.
  194. // - If |target_| does not exist, then clear all the recursive watches.
  195. // - Assuming |target_| exists, passing kInvalidWatch as |fired_watch| forces
  196. // addition of recursive watches for |target_|.
  197. // - Otherwise, only the directory associated with |fired_watch| and its
  198. // sub-directories will be reconfigured.
  199. // Returns true if watch limit is not hit. Otherwise, returns false.
  200. [[nodiscard]] bool UpdateRecursiveWatches(InotifyReader::Watch fired_watch,
  201. bool is_dir);
  202. // Enumerate recursively through |path| and add / update watches.
  203. // Returns true if watch limit is not hit. Otherwise, returns false.
  204. [[nodiscard]] bool UpdateRecursiveWatchesForPath(const FilePath& path);
  205. // Do internal bookkeeping to update mappings between |watch| and its
  206. // associated full path |path|.
  207. void TrackWatchForRecursion(InotifyReader::Watch watch, const FilePath& path);
  208. // Remove all the recursive watches.
  209. void RemoveRecursiveWatches();
  210. // |path| is a symlink to a non-existent target. Attempt to add a watch to
  211. // the link target's parent directory. Update |watch_entry| on success.
  212. // Returns true if watch limit is not hit. Otherwise, returns false.
  213. [[nodiscard]] bool AddWatchForBrokenSymlink(const FilePath& path,
  214. WatchEntry* watch_entry);
  215. bool HasValidWatchVector() const;
  216. // Callback to notify upon changes.
  217. FilePathWatcher::Callback callback_;
  218. // The file or directory we're supposed to watch.
  219. FilePath target_;
  220. Type type_ = Type::kNonRecursive;
  221. // The vector of watches and next component names for all path components,
  222. // starting at the root directory. The last entry corresponds to the watch for
  223. // |target_| and always stores an empty next component name in |subdir|.
  224. std::vector<WatchEntry> watches_;
  225. std::unordered_map<InotifyReader::Watch, FilePath> recursive_paths_by_watch_;
  226. std::map<FilePath, InotifyReader::Watch> recursive_watches_by_path_;
  227. WeakPtrFactory<FilePathWatcherImpl> weak_factory_{this};
  228. };
  229. LazyInstance<InotifyReader>::Leaky g_inotify_reader = LAZY_INSTANCE_INITIALIZER;
  230. void InotifyReaderThreadDelegate::ThreadMain() {
  231. PlatformThread::SetName("inotify_reader");
  232. std::array<pollfd, 1> fdarray{{{inotify_fd_, POLLIN, 0}}};
  233. while (true) {
  234. // Wait until some inotify events are available.
  235. int poll_result = HANDLE_EINTR(poll(fdarray.data(), fdarray.size(), -1));
  236. if (poll_result < 0) {
  237. DPLOG(WARNING) << "poll failed";
  238. return;
  239. }
  240. // Adjust buffer size to current event queue size.
  241. int buffer_size;
  242. int ioctl_result = HANDLE_EINTR(ioctl(inotify_fd_, FIONREAD, &buffer_size));
  243. if (ioctl_result != 0 || buffer_size < 0) {
  244. DPLOG(WARNING) << "ioctl failed";
  245. return;
  246. }
  247. std::vector<char> buffer(static_cast<size_t>(buffer_size));
  248. ssize_t bytes_read = HANDLE_EINTR(
  249. read(inotify_fd_, buffer.data(), static_cast<size_t>(buffer_size)));
  250. if (bytes_read < 0) {
  251. DPLOG(WARNING) << "read from inotify fd failed";
  252. return;
  253. }
  254. for (size_t i = 0; i < static_cast<size_t>(bytes_read);) {
  255. inotify_event* event = reinterpret_cast<inotify_event*>(&buffer[i]);
  256. size_t event_size = sizeof(inotify_event) + event->len;
  257. DCHECK(i + event_size <= static_cast<size_t>(bytes_read));
  258. g_inotify_reader.Get().OnInotifyEvent(event);
  259. i += event_size;
  260. }
  261. }
  262. }
  263. InotifyReader::InotifyReader()
  264. : inotify_fd_(inotify_init()), thread_delegate_(inotify_fd_) {
  265. if (inotify_fd_ < 0) {
  266. PLOG(ERROR) << "inotify_init() failed";
  267. return;
  268. }
  269. if (!StartThread())
  270. return;
  271. valid_ = true;
  272. }
  273. bool InotifyReader::StartThread() {
  274. // This object is LazyInstance::Leaky, so thread_delegate_ will outlive the
  275. // thread.
  276. return PlatformThread::CreateNonJoinable(0, &thread_delegate_);
  277. }
  278. InotifyReader::Watch InotifyReader::AddWatch(const FilePath& path,
  279. FilePathWatcherImpl* watcher) {
  280. if (!valid_)
  281. return kInvalidWatch;
  282. if (watcher->WouldExceedWatchLimit())
  283. return kWatchLimitExceeded;
  284. AutoLock auto_lock(lock_);
  285. ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::WILL_BLOCK);
  286. const int watch_int =
  287. inotify_add_watch(inotify_fd_, path.value().c_str(),
  288. IN_ATTRIB | IN_CREATE | IN_DELETE | IN_CLOSE_WRITE |
  289. IN_MOVE | IN_ONLYDIR);
  290. if (watch_int == -1)
  291. return kInvalidWatch;
  292. const Watch watch = static_cast<Watch>(watch_int);
  293. watchers_[watch].emplace(std::make_pair(
  294. watcher, WatcherEntry{watcher->GetTaskRunner(), watcher->GetWeakPtr()}));
  295. return watch;
  296. }
  297. void InotifyReader::RemoveWatch(Watch watch, FilePathWatcherImpl* watcher) {
  298. if (!valid_ || (watch == kInvalidWatch))
  299. return;
  300. AutoLock auto_lock(lock_);
  301. auto watchers_it = watchers_.find(watch);
  302. if (watchers_it == watchers_.end())
  303. return;
  304. auto& watcher_map = watchers_it->second;
  305. watcher_map.erase(watcher);
  306. if (watcher_map.empty()) {
  307. watchers_.erase(watchers_it);
  308. ScopedBlockingCall scoped_blocking_call(FROM_HERE,
  309. BlockingType::WILL_BLOCK);
  310. inotify_rm_watch(inotify_fd_, watch);
  311. }
  312. }
  313. void InotifyReader::OnInotifyEvent(const inotify_event* event) {
  314. if (event->mask & IN_IGNORED)
  315. return;
  316. FilePath::StringType child(event->len ? event->name : FILE_PATH_LITERAL(""));
  317. AutoLock auto_lock(lock_);
  318. // In racing conditions, RemoveWatch() could grab `lock_` first and remove
  319. // the entry for `event->wd`.
  320. auto watchers_it = watchers_.find(static_cast<Watch>(event->wd));
  321. if (watchers_it == watchers_.end())
  322. return;
  323. auto& watcher_map = watchers_it->second;
  324. for (const auto& entry : watcher_map) {
  325. auto& watcher_entry = entry.second;
  326. watcher_entry.task_runner->PostTask(
  327. FROM_HERE,
  328. BindOnce(&FilePathWatcherImpl::OnFilePathChanged, watcher_entry.watcher,
  329. static_cast<Watch>(event->wd), child,
  330. event->mask & (IN_CREATE | IN_MOVED_TO),
  331. event->mask & (IN_DELETE | IN_MOVED_FROM),
  332. event->mask & IN_ISDIR));
  333. }
  334. }
  335. bool InotifyReader::HasWatches() {
  336. AutoLock auto_lock(lock_);
  337. return !watchers_.empty();
  338. }
  339. FilePathWatcherImpl::FilePathWatcherImpl() = default;
  340. FilePathWatcherImpl::~FilePathWatcherImpl() {
  341. DCHECK(!task_runner() || task_runner()->RunsTasksInCurrentSequence());
  342. }
  343. void FilePathWatcherImpl::OnFilePathChanged(InotifyReader::Watch fired_watch,
  344. const FilePath::StringType& child,
  345. bool created,
  346. bool deleted,
  347. bool is_dir) {
  348. DCHECK(task_runner()->RunsTasksInCurrentSequence());
  349. DCHECK(!watches_.empty());
  350. DCHECK(HasValidWatchVector());
  351. // Used below to avoid multiple recursive updates.
  352. bool did_update = false;
  353. // Whether kWatchLimitExceeded is encountered during update.
  354. bool exceeded_limit = false;
  355. // Find the entries in |watches_| that correspond to |fired_watch|.
  356. for (size_t i = 0; i < watches_.size(); ++i) {
  357. const WatchEntry& watch_entry = watches_[i];
  358. if (fired_watch != watch_entry.watch)
  359. continue;
  360. // Check whether a path component of |target_| changed.
  361. bool change_on_target_path = child.empty() ||
  362. (child == watch_entry.linkname) ||
  363. (child == watch_entry.subdir);
  364. // Check if the change references |target_| or a direct child of |target_|.
  365. bool target_changed;
  366. if (watch_entry.subdir.empty()) {
  367. // The fired watch is for a WatchEntry without a subdir. Thus for a given
  368. // |target_| = "/path/to/foo", this is for "foo". Here, check either:
  369. // - the target has no symlink: it is the target and it changed.
  370. // - the target has a symlink, and it matches |child|.
  371. target_changed =
  372. (watch_entry.linkname.empty() || child == watch_entry.linkname);
  373. } else {
  374. // The fired watch is for a WatchEntry with a subdir. Thus for a given
  375. // |target_| = "/path/to/foo", this is for {"/", "/path", "/path/to"}.
  376. // So we can safely access the next WatchEntry since we have not reached
  377. // the end yet. Check |watch_entry| is for "/path/to", i.e. the next
  378. // element is "foo".
  379. bool next_watch_may_be_for_target = watches_[i + 1].subdir.empty();
  380. if (next_watch_may_be_for_target) {
  381. // The current |watch_entry| is for "/path/to", so check if the |child|
  382. // that changed is "foo".
  383. target_changed = watch_entry.subdir == child;
  384. } else {
  385. // The current |watch_entry| is not for "/path/to", so the next entry
  386. // cannot be "foo". Thus |target_| has not changed.
  387. target_changed = false;
  388. }
  389. }
  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. if (!UpdateWatches()) {
  397. exceeded_limit = true;
  398. break;
  399. }
  400. did_update = true;
  401. }
  402. // Report the following events:
  403. // - The target or a direct child of the target got changed (in case the
  404. // watched path refers to a directory).
  405. // - One of the parent directories got moved or deleted, since the target
  406. // disappears in this case.
  407. // - One of the parent directories appears. The event corresponding to
  408. // the target appearing might have been missed in this case, so recheck.
  409. if (target_changed || (change_on_target_path && deleted) ||
  410. (change_on_target_path && created && PathExists(target_))) {
  411. if (!did_update) {
  412. if (!UpdateRecursiveWatches(fired_watch, is_dir)) {
  413. exceeded_limit = true;
  414. break;
  415. }
  416. did_update = true;
  417. }
  418. callback_.Run(target_, /*error=*/false); // `this` may be deleted.
  419. return;
  420. }
  421. }
  422. if (!exceeded_limit && Contains(recursive_paths_by_watch_, fired_watch)) {
  423. if (!did_update) {
  424. if (!UpdateRecursiveWatches(fired_watch, is_dir))
  425. exceeded_limit = true;
  426. }
  427. if (!exceeded_limit) {
  428. callback_.Run(target_, /*error=*/false); // `this` may be deleted.
  429. return;
  430. }
  431. }
  432. if (exceeded_limit) {
  433. // Cancels all in-flight events from inotify thread.
  434. weak_factory_.InvalidateWeakPtrs();
  435. // Reset states and cancels all watches.
  436. auto callback = callback_;
  437. Cancel();
  438. // Fires the "error=true" callback.
  439. callback.Run(target_, /*error=*/true); // `this` may be deleted.
  440. }
  441. }
  442. bool FilePathWatcherImpl::WouldExceedWatchLimit() const {
  443. DCHECK(task_runner()->RunsTasksInCurrentSequence());
  444. // `watches_` contains inotify watches of all dir components of `target_`.
  445. // `recursive_paths_by_watch_` contains inotify watches for sub dirs under
  446. // `target_` of a Type::kRecursive watcher and keyed by inotify watches.
  447. // All inotify watches used by this FilePathWatcherImpl are either in
  448. // `watches_` or as a key in `recursive_paths_by_watch_`. As a result, the
  449. // two provide a good estimate on the number of inofiy watches used by this
  450. // FilePathWatcherImpl.
  451. const size_t number_of_inotify_watches =
  452. watches_.size() + recursive_paths_by_watch_.size();
  453. return number_of_inotify_watches >= GetMaxNumberOfInotifyWatches();
  454. }
  455. scoped_refptr<SequencedTaskRunner> FilePathWatcherImpl::GetTaskRunner() const {
  456. DCHECK(task_runner()->RunsTasksInCurrentSequence());
  457. return task_runner();
  458. }
  459. WeakPtr<FilePathWatcherImpl> FilePathWatcherImpl::GetWeakPtr() const {
  460. DCHECK(task_runner()->RunsTasksInCurrentSequence());
  461. return weak_factory_.GetWeakPtr();
  462. }
  463. bool FilePathWatcherImpl::Watch(const FilePath& path,
  464. Type type,
  465. const FilePathWatcher::Callback& callback) {
  466. DCHECK(target_.empty());
  467. set_task_runner(SequencedTaskRunnerHandle::Get());
  468. callback_ = callback;
  469. target_ = path;
  470. type_ = type;
  471. std::vector<FilePath::StringType> comps = target_.GetComponents();
  472. DCHECK(!comps.empty());
  473. for (size_t i = 1; i < comps.size(); ++i)
  474. watches_.emplace_back(comps[i]);
  475. watches_.emplace_back(FilePath::StringType());
  476. if (!UpdateWatches()) {
  477. Cancel();
  478. // Note `callback` is not invoked since false is returned.
  479. return false;
  480. }
  481. return true;
  482. }
  483. void FilePathWatcherImpl::Cancel() {
  484. if (!callback_) {
  485. // Watch() was never called.
  486. set_cancelled();
  487. return;
  488. }
  489. DCHECK(task_runner()->RunsTasksInCurrentSequence());
  490. DCHECK(!is_cancelled());
  491. set_cancelled();
  492. callback_.Reset();
  493. for (const auto& watch : watches_)
  494. g_inotify_reader.Get().RemoveWatch(watch.watch, this);
  495. watches_.clear();
  496. target_.clear();
  497. RemoveRecursiveWatches();
  498. }
  499. bool FilePathWatcherImpl::UpdateWatches() {
  500. // Ensure this runs on the task_runner() exclusively in order to avoid
  501. // concurrency issues.
  502. DCHECK(task_runner()->RunsTasksInCurrentSequence());
  503. DCHECK(HasValidWatchVector());
  504. // Walk the list of watches and update them as we go.
  505. FilePath path(FILE_PATH_LITERAL("/"));
  506. for (WatchEntry& watch_entry : watches_) {
  507. InotifyReader::Watch old_watch = watch_entry.watch;
  508. watch_entry.watch = InotifyReader::kInvalidWatch;
  509. watch_entry.linkname.clear();
  510. watch_entry.watch = g_inotify_reader.Get().AddWatch(path, this);
  511. if (watch_entry.watch == InotifyReader::kWatchLimitExceeded)
  512. return false;
  513. if (watch_entry.watch == InotifyReader::kInvalidWatch) {
  514. // Ignore the error code (beyond symlink handling) to attempt to add
  515. // watches on accessible children of unreadable directories. Note that
  516. // this is a best-effort attempt; we may not catch events in this
  517. // scenario.
  518. if (IsLink(path)) {
  519. if (!AddWatchForBrokenSymlink(path, &watch_entry))
  520. return false;
  521. }
  522. }
  523. if (old_watch != watch_entry.watch)
  524. g_inotify_reader.Get().RemoveWatch(old_watch, this);
  525. path = path.Append(watch_entry.subdir);
  526. }
  527. return UpdateRecursiveWatches(InotifyReader::kInvalidWatch, /*is_dir=*/false);
  528. }
  529. bool FilePathWatcherImpl::UpdateRecursiveWatches(
  530. InotifyReader::Watch fired_watch,
  531. bool is_dir) {
  532. DCHECK(HasValidWatchVector());
  533. if (type_ != Type::kRecursive)
  534. return true;
  535. if (!DirectoryExists(target_)) {
  536. RemoveRecursiveWatches();
  537. return true;
  538. }
  539. // Check to see if this is a forced update or if some component of |target_|
  540. // has changed. For these cases, redo the watches for |target_| and below.
  541. if (!Contains(recursive_paths_by_watch_, fired_watch) &&
  542. fired_watch != watches_.back().watch) {
  543. return UpdateRecursiveWatchesForPath(target_);
  544. }
  545. // Underneath |target_|, only directory changes trigger watch updates.
  546. if (!is_dir)
  547. return true;
  548. const FilePath& changed_dir = Contains(recursive_paths_by_watch_, fired_watch)
  549. ? recursive_paths_by_watch_[fired_watch]
  550. : target_;
  551. auto start_it = recursive_watches_by_path_.upper_bound(changed_dir);
  552. auto end_it = start_it;
  553. for (; end_it != recursive_watches_by_path_.end(); ++end_it) {
  554. const FilePath& cur_path = end_it->first;
  555. if (!changed_dir.IsParent(cur_path))
  556. break;
  557. // There could be a race when another process is changing contents under
  558. // `changed_dir` while chrome is watching (e.g. an Android app updating
  559. // a dir with Chrome OS file manager open for the dir). In such case,
  560. // `cur_dir` under `changed_dir` could exist in this loop but not in
  561. // the FileEnumerator loop in the upcoming UpdateRecursiveWatchesForPath(),
  562. // As a result, `g_inotify_reader` would have an entry in its `watchers_`
  563. // pointing to `this` but `this` is no longer aware of that. Crash in
  564. // http://crbug/990004 could happen later.
  565. //
  566. // Remove the watcher of `cur_path` regardless of whether it exists
  567. // or not to keep `this` and `g_inotify_reader` consistent even when the
  568. // race happens. The watcher will be added back if `cur_path` exists in
  569. // the FileEnumerator loop in UpdateRecursiveWatchesForPath().
  570. g_inotify_reader.Get().RemoveWatch(end_it->second, this);
  571. // Keep it in sync with |recursive_watches_by_path_| crbug.com/995196.
  572. recursive_paths_by_watch_.erase(end_it->second);
  573. }
  574. recursive_watches_by_path_.erase(start_it, end_it);
  575. return UpdateRecursiveWatchesForPath(changed_dir);
  576. }
  577. bool FilePathWatcherImpl::UpdateRecursiveWatchesForPath(const FilePath& path) {
  578. DCHECK_EQ(type_, Type::kRecursive);
  579. DCHECK(!path.empty());
  580. DCHECK(DirectoryExists(path));
  581. // Note: SHOW_SYM_LINKS exposes symlinks as symlinks, so they are ignored
  582. // rather than followed. Following symlinks can easily lead to the undesirable
  583. // situation where the entire file system is being watched.
  584. FileEnumerator enumerator(
  585. path, true /* recursive enumeration */,
  586. FileEnumerator::DIRECTORIES | FileEnumerator::SHOW_SYM_LINKS);
  587. for (FilePath current = enumerator.Next(); !current.empty();
  588. current = enumerator.Next()) {
  589. DCHECK(enumerator.GetInfo().IsDirectory());
  590. if (!Contains(recursive_watches_by_path_, current)) {
  591. // Add new watches.
  592. InotifyReader::Watch watch =
  593. g_inotify_reader.Get().AddWatch(current, this);
  594. if (watch == InotifyReader::kWatchLimitExceeded)
  595. return false;
  596. TrackWatchForRecursion(watch, current);
  597. } else {
  598. // Update existing watches.
  599. InotifyReader::Watch old_watch = recursive_watches_by_path_[current];
  600. DCHECK_NE(InotifyReader::kInvalidWatch, old_watch);
  601. InotifyReader::Watch watch =
  602. g_inotify_reader.Get().AddWatch(current, this);
  603. if (watch == InotifyReader::kWatchLimitExceeded)
  604. return false;
  605. if (watch != old_watch) {
  606. g_inotify_reader.Get().RemoveWatch(old_watch, this);
  607. recursive_paths_by_watch_.erase(old_watch);
  608. recursive_watches_by_path_.erase(current);
  609. TrackWatchForRecursion(watch, current);
  610. }
  611. }
  612. }
  613. return true;
  614. }
  615. void FilePathWatcherImpl::TrackWatchForRecursion(InotifyReader::Watch watch,
  616. const FilePath& path) {
  617. DCHECK_EQ(type_, Type::kRecursive);
  618. DCHECK(!path.empty());
  619. DCHECK(target_.IsParent(path));
  620. if (watch == InotifyReader::kInvalidWatch)
  621. return;
  622. DCHECK(!Contains(recursive_paths_by_watch_, watch));
  623. DCHECK(!Contains(recursive_watches_by_path_, path));
  624. recursive_paths_by_watch_[watch] = path;
  625. recursive_watches_by_path_[path] = watch;
  626. }
  627. void FilePathWatcherImpl::RemoveRecursiveWatches() {
  628. if (type_ != Type::kRecursive)
  629. return;
  630. for (const auto& it : recursive_paths_by_watch_)
  631. g_inotify_reader.Get().RemoveWatch(it.first, this);
  632. recursive_paths_by_watch_.clear();
  633. recursive_watches_by_path_.clear();
  634. }
  635. bool FilePathWatcherImpl::AddWatchForBrokenSymlink(const FilePath& path,
  636. WatchEntry* watch_entry) {
  637. #if BUILDFLAG(IS_FUCHSIA)
  638. // Fuchsia does not support symbolic links.
  639. return false;
  640. #else // BUILDFLAG(IS_FUCHSIA)
  641. DCHECK_EQ(InotifyReader::kInvalidWatch, watch_entry->watch);
  642. FilePath link;
  643. if (!ReadSymbolicLink(path, &link))
  644. return true;
  645. if (!link.IsAbsolute())
  646. link = path.DirName().Append(link);
  647. // Try watching symlink target directory. If the link target is "/", then we
  648. // shouldn't get here in normal situations and if we do, we'd watch "/" for
  649. // changes to a component "/" which is harmless so no special treatment of
  650. // this case is required.
  651. InotifyReader::Watch watch =
  652. g_inotify_reader.Get().AddWatch(link.DirName(), this);
  653. if (watch == InotifyReader::kWatchLimitExceeded)
  654. return false;
  655. if (watch == InotifyReader::kInvalidWatch) {
  656. // TODO(craig) Symlinks only work if the parent directory for the target
  657. // exist. Ideally we should make sure we've watched all the components of
  658. // the symlink path for changes. See crbug.com/91561 for details.
  659. DPLOG(WARNING) << "Watch failed for " << link.DirName().value();
  660. return true;
  661. }
  662. watch_entry->watch = watch;
  663. watch_entry->linkname = link.BaseName().value();
  664. return true;
  665. #endif // BUILDFLAG(IS_FUCHSIA)
  666. }
  667. bool FilePathWatcherImpl::HasValidWatchVector() const {
  668. if (watches_.empty())
  669. return false;
  670. for (size_t i = 0; i < watches_.size() - 1; ++i) {
  671. if (watches_[i].subdir.empty())
  672. return false;
  673. }
  674. return watches_.back().subdir.empty();
  675. }
  676. } // namespace
  677. ScopedMaxNumberOfInotifyWatchesOverrideForTest::
  678. ScopedMaxNumberOfInotifyWatchesOverrideForTest(size_t override_max) {
  679. DCHECK_EQ(g_override_max_inotify_watches, 0u);
  680. g_override_max_inotify_watches = override_max;
  681. }
  682. ScopedMaxNumberOfInotifyWatchesOverrideForTest::
  683. ~ScopedMaxNumberOfInotifyWatchesOverrideForTest() {
  684. g_override_max_inotify_watches = 0u;
  685. }
  686. FilePathWatcher::FilePathWatcher() {
  687. sequence_checker_.DetachFromSequence();
  688. impl_ = std::make_unique<FilePathWatcherImpl>();
  689. }
  690. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  691. // Put inside "BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)" because Android
  692. // includes file_path_watcher_linux.cc.
  693. // static
  694. bool FilePathWatcher::HasWatchesForTest() {
  695. return g_inotify_reader.Get().HasWatches();
  696. }
  697. #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  698. } // namespace base