AnyEvent-Inotify-Simple
view release on metacpan or search on metacpan
lib/AnyEvent/Inotify/Simple.pm view on Meta::CPAN
}
}
sub BUILD {
my $self = shift;
$self->_watch_directory($self->directory->resolve->absolute);
}
my %events = (
IN_ACCESS => 'handle_access',
IN_MODIFY => 'handle_modify',
IN_ATTRIB => 'handle_attribute_change',
IN_CLOSE_WRITE => 'handle_close_write',
IN_CLOSE_NOWRITE => 'handle_close_nowrite',
IN_OPEN => 'handle_open',
IN_CREATE => 'handle_create',
IN_DELETE => 'handle_delete',
);
sub handle_event {
my ($self, $file, $event) = @_;
my $wrapper = $event->IN_ISDIR ? 'subdir' : 'file';
my $event_file = $file->$wrapper($event->name);
if( $event->IN_DELETE_SELF || $event->IN_MOVE_SELF ){
#warn "canceling $file";
#$event->w->cancel;
return;
}
if($self->is_filtered($event_file)){
# we get this when a directory watcher notices something
# about a file that should be ignored
return;
}
my $relative = $event_file->relative($self->directory);
my $handled = 0;
for my $type (keys %events){
my $method = $events{$type};
if( $event->$type ){
$self->$method($relative);
$handled = 1;
}
}
if( $event->IN_MOVED_FROM ){
$self->handle_move_from($relative, $event->cookie);
$handled = 1;
}
if( $event->IN_MOVED_TO ){
$self->handle_move_to($relative, $event->cookie);
$handled = 1;
}
if (!$handled){
require Data::Dumper;
local $Data::Dumper::Maxdepth = 2;
Carp::cluck "BUGBUG: Unhandled event: ".
Data::Dumper->Dump($event);
}
}
sub rel2abs {
my ($self, $file) = @_;
return $file if $file->is_absolute;
return $file->absolute($self->directory)->resolve->absolute;
}
sub handle_move_from {
my ($self, $file, $cookie) = @_;
$self->cookie_jar->{from}{$cookie} = $file;
}
sub handle_move_to {
my ($self, $to, $cookie) = @_;
my $from = delete $self->cookie_jar->{from}{$cookie};
confess "Invalid move cookie '$cookie' (moved to '$to')"
unless $from;
my $abs = eval { $self->rel2abs($to) };
$self->_watch_directory($abs) if $abs && -d $abs;
$self->handle_move($from, $to);
}
# inject our magic
before 'handle_create' => sub {
my ($self, $dir) = @_;
my $abs = eval { $self->rel2abs($dir) };
return unless $abs && -d $abs;
$self->_watch_directory($abs);
};
sub DEMOLISH {
my $self = shift;
return unless $self->inotify;
for my $w (values %{$self->inotify->{w}}){
next unless $w;
$w->cancel;
}
}
1;
__END__
=head1 NAME
AnyEvent::Inotify::Simple - monitor a directory tree in a non-blocking way
=head1 SYNOPSIS
use AnyEvent::Inotify::Simple;
use EV; # or POE, or Event, or ...
( run in 2.164 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )