Skip to content

Commit 56d2f9e

Browse files
committed
Fix watching specific files on windows
When you are only watching specific file paths, you get change events for all files in the same directory. On linux only changes to watched files get reported.
1 parent 24d4cad commit 56d2f9e

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

lib/Filesys/Notify/Simple.pm

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ sub mk_wait_win32 {
120120
my @path = @_;
121121

122122
my $fs = _full_scan(@path);
123+
my %watched = map { (Cwd::realpath($_), 1) } @path;
123124
my (@notify, @fskey);
124125
for my $path (keys %$fs) {
125126
my $winpath = $is_cygwin ? Cygwin::posix_to_win_path($path) : $path;
@@ -137,7 +138,28 @@ sub mk_wait_win32 {
137138
Carp::croak("Can't wait notifications, maybe ".scalar(@notify)." directories exceeds limitation.") if ! defined $idx;
138139
if($idx > 0) {
139140
--$idx;
141+
# get all file changes in path
140142
my $new_fs = _full_scan($fskey[$idx]);
143+
# process all reported file changes in path
144+
foreach my $root (keys %{$new_fs}) {
145+
# check if we watch particular path
146+
unless (exists $fs->{$root}) {
147+
# remove paths not watched by us
148+
delete $new_fs->{$root};
149+
} else {
150+
foreach my $file (keys %{$new_fs->{$root}}) {
151+
# check if we watch particular file
152+
unless (exists $fs->{$root}->{$file}) {
153+
# check if we watch the directory
154+
unless (exists $watched{$root}) {
155+
# remove files not watched by us
156+
delete $new_fs->{$root}->{$file};
157+
}
158+
}
159+
}
160+
}
161+
}
162+
141163
$notify[$idx]->reset;
142164
my $old_fs = +{ map { ($_ => $fs->{$_}) } keys %$new_fs };
143165
_compare_fs($old_fs, $new_fs, sub { push @events, { path => $_[0] } });
@@ -210,7 +232,7 @@ sub _full_scan {
210232

211233
# remove root entry
212234
# NOTE: On MSWin32, realpath and rel2abs disagree with path separator.
213-
delete $map{$fp}{File::Spec->rel2abs($fp)};
235+
delete $map{$fp}{File::Spec->rel2abs($fp)} if exists $map{$fp};
214236
}
215237

216238
return \%map;

0 commit comments

Comments
 (0)