Skip to content

[Filesystem] Add watch method to watch filesystem for changes #31462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add recursive directory handling to inotify watcher
  • Loading branch information
pierredup committed May 5, 2020
commit 78c9969bc949d6236df8691dae86b907b06eecd3
22 changes: 19 additions & 3 deletions src/Symfony/Component/Filesystem/Watcher/INotifyWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@ public function watch($path, callable $callback, float $timeout = null)
stream_set_blocking($inotifyInit, false);

$isDir = is_dir($path);
$watchers = [];

if ($isDir) {
$watchId = inotify_add_watch($inotifyInit, $path, IN_CREATE | IN_DELETE | IN_MODIFY);
$watchers[] = inotify_add_watch($inotifyInit, $path, IN_CREATE | IN_DELETE | IN_MODIFY);

foreach ($this->scanPath("$path/*") as $path) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how does this work in https://github.com/flint/Lurker, because upon quick inspection, not only I don't see this mechanism there, but also no inotify_add_watch. I'm wondering this because recursively iterating over all folders and creating inotify watcher for each doesn't look efficient. I expect startup times in any moderately sized projects to be long doing it like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not only I don't see this mechanism there, but also no inotify_add_watch

inotify_add_watch is used here

Checking recursively can maybe be turned off by default and enabled by either a flag or a new method watchRecursive

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inotify_add_watch is used here

Thanks, GH didn't find it for me.

Checking recursively can maybe be turned off by default and enabled by either a flag or a new method watchRecursive

I don't mean to have ability to turn recursivity off, I am just trying to figure if there isn't smarter way to go around handling recursivity. First thing we need to clear up is: Is it absolute requirement for inotify PHP extension to create watcher for every subdirectory, instead of just parent directory?

$watchers[] = inotify_add_watch($inotifyInit, $path, IN_CREATE | IN_DELETE | IN_MODIFY);
}
} else {
$watchId = inotify_add_watch($inotifyInit, $path, IN_MODIFY);
$watchers[] = inotify_add_watch($inotifyInit, $path, IN_MODIFY);
}

try {
Expand Down Expand Up @@ -77,8 +82,19 @@ public function watch($path, callable $callback, float $timeout = null)
}
}
} finally {
inotify_rm_watch($inotifyInit, $watchId);
foreach ($watchers as $watchId) {
inotify_rm_watch($inotifyInit, $watchId);
}

fclose($inotifyInit);
}
}

private function scanPath($path)
{
foreach (glob($path, GLOB_ONLYDIR) as $directory) {
yield $directory;
yield from $this->scanPath("$directory/*");
}
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy