| 1 | package File::Tail::Inotify2; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.008008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Linux::Inotify2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use File::Basename qw(dirname); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Fcntl qw(SEEK_SET SEEK_END); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 3 | our $VERSION = '1.00'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | use constant { | ||||||||||||||||||||||||||||
| 14 | 3 | FMASK => IN_MODIFY | IN_MOVE_SELF, | |||||||||||||||||||||||||||
| 15 | DMASK => IN_CREATE | IN_MOVED_FROM, | ||||||||||||||||||||||||||||
| 16 | 3 | ▶ | }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | sub new { | ||||||||||||||||||||||||||||
| 19 | 2 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=22.4
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 2 | my %args = ( | |||||||||||||||||||||||||||
| 21 | file => undef, | ||||||||||||||||||||||||||||
| 22 | on_read => undef, | ||||||||||||||||||||||||||||
| 23 | @_, | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 2 | for (qw(file on_read)) { | |||||||||||||||||||||||||||
| 27 | Carp::croak "Mandatory parameter $_ was missing." | ||||||||||||||||||||||||||||
| 28 | 4 | ▶ | unless $args{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | unless ( ref $args{on_read} && ref $args{on_read} eq 'CODE' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $args{"on_read"} and ref $args{"on_read"} eq "CODE"
MC/DC0%0/2
ref $args{"on_read"} and ref $args{"on_read"} eq "CODE"
ref $args{"on_read"}
ref $args{"on_read"} eq "CODE"
Truth table33%1/3
A and B
A= ref $args{"on_read"}B= ref $args{"on_read"} eq "CODE"
| |||||||||||||||||||||||||||||
| 32 | 0 | Carp::croak "on_read must be CODE"; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 2 | ▶ | my $inotify = Linux::Inotify2->new | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | or Carp::croak "Cannot create Inotify2 object: $!"; | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | my $self = bless { | ||||||||||||||||||||||||||||
| 39 | file => $args{file}, | ||||||||||||||||||||||||||||
| 40 | on_read => $args{on_read}, | ||||||||||||||||||||||||||||
| 41 | 2 | ▶ | curpos => 0, | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 42 | in_move => 0, | ||||||||||||||||||||||||||||
| 43 | inotify => $inotify, | ||||||||||||||||||||||||||||
| 44 | }, | ||||||||||||||||||||||||||||
| 45 | ref $class || $class; | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 2 | $self->_set_watcher; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 2 | return $self; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | sub poll { | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
poll: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | 1 while $self->{inotify}->poll || $self->{in_move}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"inotify"}->poll or $self->{"in_move"}
MC/DC0%0/2
$self->{"inotify"}->poll or $self->{"in_move"}
$self->{"inotify"}->poll
$self->{"in_move"}
Truth table0%0/3
A or B
A= $self->{"inotify"}->pollB= $self->{"in_move"}
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub _set_watcher { | ||||||||||||||||||||||||||||
| 58 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_set_watcher: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | 2 | $self->_set_file_watcher; | |||||||||||||||||||||||||||
| 60 | 2 | $self->_set_dir_watcher; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | sub _set_file_watcher { | ||||||||||||||||||||||||||||
| 64 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_set_file_watcher: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | 3 | my $size = (stat $self->{file})[7]; | |||||||||||||||||||||||||||
| 66 | 3 | $self->{curpos} = $size; | |||||||||||||||||||||||||||
| 67 | 3 | $self->{inotify}->watch( $self->{file}, FMASK, $self->_in_modify ); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | sub _in_modify { | ||||||||||||||||||||||||||||
| 71 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_in_modify: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | return sub { | ||||||||||||||||||||||||||||
| 73 | 3 | ▶ | my $event = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 74 | 3 | ▶ | if ( $event->IN_MODIFY ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 2 | ▶ | open my $fh, '<', $event->fullname | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | or Carp::croak "Cannot open " . $event->fullname . ": $!"; | ||||||||||||||||||||||||||||
| 77 | 2 | seek $fh, $self->{curpos}, SEEK_SET; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 2 | ▶ | while (<$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 2 | $self->{on_read}->($_); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 2 | $self->{curpos} = tell $fh; | |||||||||||||||||||||||||||
| 84 | 2 | ▶ | close $fh | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | or Carp::croak "Cannot close " . $event->fullname . ": $!"; | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 3 | ▶ | if ( $event->IN_MOVE_SELF ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 1 | $event->w->cancel; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 3 | }; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | sub _set_dir_watcher { | ||||||||||||||||||||||||||||
| 94 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_set_dir_watcher: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 95 | $self->{inotify}->watch( | ||||||||||||||||||||||||||||
| 96 | dirname( $self->{file} ), | ||||||||||||||||||||||||||||
| 97 | DMASK, | ||||||||||||||||||||||||||||
| 98 | sub { | ||||||||||||||||||||||||||||
| 99 | 2 | ▶ | my $event = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 100 | 2 | ▶ | if ( $event->IN_MOVED_FROM && $event->fullname eq $self->{file} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$event->IN_MOVED_FROM and $event->fullname eq $self->{"file"}
MC/DC50%1/2
$event->IN_MOVED_FROM and $event->fullname eq $self->{"file"}
$event->IN_MOVED_FROM
$event->fullname eq $self->{"file"}
Truth table66%2/3
A and B
A= $event->IN_MOVED_FROMB= $event->fullname eq $self->{"file"}
| |||||||||||||||||||||||||||||
| 101 | 1 | $self->{in_move} = 1; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 2 | ▶ | if ( $event->IN_CREATE && $event->fullname eq $self->{file} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$event->IN_CREATE and $event->fullname eq $self->{"file"}
MC/DC50%1/2
$event->IN_CREATE and $event->fullname eq $self->{"file"}
$event->IN_CREATE
$event->fullname eq $self->{"file"}
Truth table66%2/3
A and B
A= $event->IN_CREATEB= $event->fullname eq $self->{"file"}
| |||||||||||||||||||||||||||||
| 104 | 1 | $self->{curpos} = (stat $event->fullname)[7]; | |||||||||||||||||||||||||||
| 105 | 1 | $self->_set_file_watcher; | |||||||||||||||||||||||||||
| 106 | 1 | $self->{in_move} = 0; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | 2 | ); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 3 | 1; | |||||||||||||||||||||||||||
| 113 | __END__ | ||||||||||||||||||||||||||||