diff --git a/lib/Filesys/Notify/Simple.pm b/lib/Filesys/Notify/Simple.pm index a7e4b4f..87467aa 100644 --- a/lib/Filesys/Notify/Simple.pm +++ b/lib/Filesys/Notify/Simple.pm @@ -156,18 +156,23 @@ sub _full_scan { my %map; for my $path (@paths) { my $fp = eval { Cwd::realpath($path) } or next; - File::Find::finddepth({ - wanted => sub { - my $fullname = $File::Find::fullname || File::Spec->rel2abs($File::Find::name); - $map{Cwd::realpath($File::Find::dir)}{$fullname} = _stat($fullname); - }, - follow_fast => 1, - follow_skip => 2, - no_chdir => 1, - }, $path); - - # remove root entry - delete $map{$fp}{$fp}; + if ( -f $fp ) { + $map{$fp}{$fp} = _stat($fp); + } + else { + File::Find::finddepth({ + wanted => sub { + my $fullname = $File::Find::fullname || File::Spec->rel2abs($File::Find::name); + $map{Cwd::realpath($File::Find::dir)}{$fullname} = _stat($fullname); + }, + follow_fast => 1, + follow_skip => 2, + no_chdir => 1, + }, $path); + + # remove root entry + delete $map{$fp}{$fp}; + } } return \%map; diff --git a/t/plain_file_with_inotify2.t b/t/plain_file_with_inotify2.t new file mode 100644 index 0000000..645216d --- /dev/null +++ b/t/plain_file_with_inotify2.t @@ -0,0 +1,54 @@ +use strict; +use warnings; + +use Filesys::Notify::Simple; +use Test::More; +use FindBin; + +eval { require Linux::Inotify2 }; + +plan skip_all => 'Linux::Inotify2 is required to run this test' if $@; + +my $test_file = "$FindBin::Bin/x/plain_file_for_inotify2.data"; +open my $out, ">", $test_file; +print $out "foo" . time; +close $out; + +my $w = Filesys::Notify::Simple->new( [$test_file] ); + +my $pid = fork; +if ( $pid == 0 ) { + sleep 3; + + unlink $test_file; + + my $other_file = "$FindBin::Bin/x/bar"; + open $out, ">", $other_file; + print $out "bar" . time; + close $out; + + unlink $other_file; + + exit; +} +elsif ( $pid != 0 ) { + my $event; + + local $SIG{ALRM} = sub { return }; + alarm 10; + + $w->wait( sub { $event = shift } ); + like $event->{path}, qr/plain_file_for_inotify2/, + 'first event is from watched file'; + + $w->wait( sub { $event = shift } ); + is $event, undef, 'only one event'; + + waitpid $pid, 0; +} +else { + die $!; +} + +done_testing(); +