From 0a8fc39c92a9927d39980b29ad3e32ceeb6fd711 Mon Sep 17 00:00:00 2001 From: sunnavy Date: Wed, 18 Jan 2012 21:36:45 +0800 Subject: [PATCH 1/4] support watching plain files with Linux::Inotify2 don't add parent dir into %map if the path is a plain file. two $fp in $map{$fp}{$fp} is to make structure of %map consistent. --- lib/Filesys/Notify/Simple.pm | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/Filesys/Notify/Simple.pm b/lib/Filesys/Notify/Simple.pm index a7e4b4f..e333462 100644 --- a/lib/Filesys/Notify/Simple.pm +++ b/lib/Filesys/Notify/Simple.pm @@ -156,6 +156,10 @@ sub _full_scan { my %map; for my $path (@paths) { my $fp = eval { Cwd::realpath($path) } or next; + 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); @@ -168,6 +172,7 @@ sub _full_scan { # remove root entry delete $map{$fp}{$fp}; + } } return \%map; From 1520b6465ad40d563a472a7c73a10b4356ba0868 Mon Sep 17 00:00:00 2001 From: sunnavy Date: Wed, 18 Jan 2012 21:41:52 +0800 Subject: [PATCH 2/4] fix indent --- lib/Filesys/Notify/Simple.pm | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/Filesys/Notify/Simple.pm b/lib/Filesys/Notify/Simple.pm index e333462..87467aa 100644 --- a/lib/Filesys/Notify/Simple.pm +++ b/lib/Filesys/Notify/Simple.pm @@ -160,18 +160,18 @@ sub _full_scan { $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}; + 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}; } } From fc60d25ebd3324eeb23738c839a0e5e0ed67994a Mon Sep 17 00:00:00 2001 From: sunnavy Date: Thu, 19 Jan 2012 01:59:58 +0800 Subject: [PATCH 3/4] test of watching plain files with Linux::Inotify2 --- t/plain_file_with_inotify2.t | 52 ++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 t/plain_file_with_inotify2.t diff --git a/t/plain_file_with_inotify2.t b/t/plain_file_with_inotify2.t new file mode 100644 index 0000000..ffe689f --- /dev/null +++ b/t/plain_file_with_inotify2.t @@ -0,0 +1,52 @@ +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; + + 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(); + From 2ab084ca5524e0f9d9b46c6ab13bfedfb0055072 Mon Sep 17 00:00:00 2001 From: sunnavy Date: Thu, 19 Jan 2012 02:34:40 +0800 Subject: [PATCH 4/4] forgot to unlink tmp file --- t/plain_file_with_inotify2.t | 2 ++ 1 file changed, 2 insertions(+) diff --git a/t/plain_file_with_inotify2.t b/t/plain_file_with_inotify2.t index ffe689f..645216d 100644 --- a/t/plain_file_with_inotify2.t +++ b/t/plain_file_with_inotify2.t @@ -27,6 +27,8 @@ if ( $pid == 0 ) { print $out "bar" . time; close $out; + unlink $other_file; + exit; } elsif ( $pid != 0 ) {