Filesys-Notify-KQueue

 view release on metacpan or  search on metacpan

lib/Filesys/Notify/KQueue.pm  view on Meta::CPAN

package Filesys::Notify::KQueue;
use strict;
use warnings;
our $VERSION = '0.11';

use File::Find ();
use IO::KQueue;

sub default_timeout { 1000 }

sub new {
    my $class = shift;
    my $args  = (@_ == 1) ? $_[0] : +{ @_ };
    my $self  = bless(+{} => $class);

    $self->timeout(exists $args->{timeout} ? $args->{timeout} : $class->default_timeout);
    $self->{_kqueue} = $args->{kqueue} if exists($args->{kqueue});
    $self->add(@{$args->{path}})       if exists($args->{path});

    return $self;
}

sub kqueue {
    my $self = shift;
    $self->{_kqueue} ||= IO::KQueue->new;
}

sub timeout {
    my $self = shift;
    (@_ == 1) ? ($self->{_timeout} = shift) : $self->{_timeout};
}

sub add {
    my $self = shift;

    foreach my $path (@_) {
        next if exists($self->{_files}{$path});
        if (-f $path) {
            $self->add_file($path);
        }
        elsif (-d $path) {
            $self->add_dir($path);
        }
        else {
            die "Unknown file '$path'";
        }
    }
}

sub add_file {
    my($self, $file) = @_;

    $self->{_files}{$file} = do {
        open(my $fh, '<', $file) or die("Can't open '$file': $!");
        die "Can't get fileno '$file'" unless defined fileno($fh);

        # add to watch
        $self->kqueue->EV_SET(
            fileno($fh),
            EVFILT_VNODE,
            EV_ADD | EV_CLEAR,
            NOTE_DELETE | NOTE_WRITE | NOTE_RENAME | NOTE_REVOKE,
            0,
            $file,
        );

        $fh;
    };
}

sub add_dir {
    my($self, $dir) = @_;

    $self->add_file($dir);

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.967 second using v1.00-cache-2.02-grep-82fe00e-cpan-1925d2aa809 )