AnyEvent-Filesys-Notify

 view release on metacpan or  search on metacpan

lib/AnyEvent/Filesys/Notify/Role/Inotify2.pm  view on Meta::CPAN

package AnyEvent::Filesys::Notify::Role::Inotify2;

# ABSTRACT: Use Linux::Inotify2 to watch for changed files

use Moo::Role;
use MooX::late;
use namespace::autoclean;
use AnyEvent;
use Linux::Inotify2;
use Carp;
use Path::Iterator::Rule;

our $VERSION = '1.23';

# use Scalar::Util qw(weaken);  # Attempt to address RT#57104, but alas...

sub _init {
    my $self = shift;

    my $inotify = Linux::Inotify2->new()
      or croak "Unable to create new Linux::Inotify2 object: $!";

    # Need to add all the subdirs to the watch list, this will catch
    # modifications to files too.
    my $old_fs = $self->_old_fs;
    my @dirs = grep { $old_fs->{$_}->{is_dir} } keys %$old_fs;

    # weaken $self; # Attempt to address RT#57104, but alas...

    for my $dir (@dirs) {
        $inotify->watch(
            $dir,
            IN_MODIFY | IN_CREATE | IN_DELETE | IN_DELETE_SELF |
              IN_MOVE | IN_MOVE_SELF | IN_ATTRIB,
            sub { my $e = shift; $self->_process_events($e); } );
    }

    $self->_fs_monitor($inotify);

    $self->_watcher(
        AnyEvent->io(
            fh   => $inotify->fileno,
            poll => 'r',
            cb   => sub {
                $inotify->poll;
            } ) );

    return 1;
}

# Parse the events returned by Inotify2 instead of rescanning the files.
# There are small changes in behavior compared to the previous releases
# without parse_events:
#
# 1. `touch test` causes an additional "modified" event after the "created"
# 2. `mv test2 test` if test exists before, event for test would be "modified"
#     in parent code, but is "created" here
#
# Because of these differences, we default to the original behavior unless the
# parse_events flag is true.
sub _parse_events {
    my ( $self, $filter_cb, @raw_events ) = @_;

    my @events =
      map  { $filter_cb->($_) }                    # filter new event
      grep { defined }                             # filter undef events
      map  { $self->_mk_event($_) } @raw_events;



( run in 0.549 second using v1.01-cache-2.11-cpan-39bf76dae61 )