blib/lib/File/Hotfolder.pm

filter: Statementstmt 38.1% 32 / 84 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 40.9% 9 / 22 Podpod 57.1% 4 / 7 totaltotal 21.7% 45 / 207 CC 35 SCAR 40.4
CC
35
Cov
20%
__ANON__
110.0
_callback
110.0
new
90.0
CRAP 57.1
↑ Summary Build »
1 package File::Hotfolder;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.03';
7  
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Linux::Inotify2;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 our %EXPORT_TAGS = (print => [qw(WATCH_DIR FOUND_FILE DELETE_FILE CATCH_ERROR)]);
15 1 our @EXPORT = ('watch', @{$EXPORT_TAGS{'print'}});
16 1 $EXPORT_TAGS{all} = \@EXPORT;
17  
18 use constant {
19 1 WATCH_DIR => 1,
20 FOUND_FILE => 2,
21 DELETE_FILE => 4,
22 CATCH_ERROR => 8,
23 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # function interface
26 sub watch {
27 0 ▶ shift if $_[0] eq 'File::Hotfolder';
Subroutine watch: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "File::Hotfolder" 0 0 0
28 0 ▶ File::Hotfolder->new( @_ % 2 ? (watch => @_) : @_ );
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 0 0
29 }
30  
31 # object interface
32 sub new {
33 0 ▶ my ($class, %args) = @_;
Subroutine new: not called CC=9 SCAR=45.0
Pod undocumented
34  
35 0 ▶ my $path = $args{watch} // '';
Condition0%0/2
$args{"watch"} // ""
l!l
00
MC/DC0%0/1
$args{"watch"} // ""
$args{"watch"}
Truth table0%0/2
A
A= $args{"watch"}
Aresult
11
00
36 0 ▶ $path = File::Spec->rel2abs($path) if $args{fullname};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"fullname"} 0 0 0
37 croak "Missing watch directory: $path" unless -d $path,
38  
39 my $self = bless {
40 inotify => (Linux::Inotify2->new
41 or croak "Unable to create new inotify object: $!"),
42 0 ▶ callback => ($args{callback} || sub { 1 }),
Subroutine __ANON__: not called CC=1 SCAR=6.9
43 delete => !!$args{delete},
44 print => 0+($args{print} || 0),
45 filter => $args{filter},
46 scan => $args{scan},
47 catch => $args{catch},
48 0 ▶ }, $class;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $path, my $self = bless({"inotify", "Linux::Inotify2"->new || croak("Unable to create new inotify object: $!"), "callback", $args{"callback"} || sub { 1; } , "delete", !!$args{"delete"}, "print", 0 + ($args{"print"} || 0), "filter", $args{"filter"}, "scan", $args{"scan"}, "catch", $args{"catch"}}, $class) 0 0 0
Condition0%0/8
'Linux::Inotify2'->new || croak("Unable to create new inotify object: $!")
l!l&&r!l&&!r
000
$args{'callback'} || sub { 1; }
l!l&&r!l&&!r
000
$args{'print'} || 0
l!l
00
MC/DC0%0/2
'Linux::Inotify2'->new || croak("Unable to create new inotify object: $!")
'Linux::Inotify2'->new croak("Unable to create new inotify object: $!")
MC/DC0%0/2
$args{'callback'} || sub { 1; }
$args{'callback'} sub { 1; }
MC/DC0%0/1
$args{'print'} || 0
$args{'print'}
Truth table0%0/3
A || B
A= 'Linux::Inotify2'->newB= croak("Unable to create new inotify object: $!")
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $args{'callback'}B= sub { 1; }
ABresult
1X1
011
000
Truth table0%0/2
A
A= $args{'print'}
Aresult
11
00
49  
50 0 ▶ $self->{catch} //= sub { } if ($self->{print} & CATCH_ERROR);
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"print"} & 8 0 0 0
Condition0%0/3
$self->{"catch"} //= sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"catch"} //= sub { }
$self->{"catch"} sub { }
Truth table0%0/3
A //= B
A= $self->{"catch"}B= sub { }
ABresult
1X1
011
000
51  
52 0 $self->watch_recursive( $path );
53  
54 0 $self;
55 }
56  
57 sub watch_recursive {
58 0 ▶ my ($self, $path) = @_;
Subroutine watch_recursive: not called CC=1 SCAR=6.9
Pod undocumented
59  
60 find({
61 no_chdir => 1,
62 wanted => sub {
63 0 ▶ if (-d $_) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 2 branches
BranchTrueFalseTotal
if (-d $_) { } 0 0 0
elsif ($self->{'scan'}) { } 0 0 0
64 0 $self->watch_directory($_);
65 } elsif( $self->{scan} ) {
66 # TODO: check if not open or modified (lsof or fuser)
67 0 $self->_callback($_);
68 }
69 },
70 0 }, $path );
71 }
72  
73 sub watch_directory {
74 0 ▶ my ($self, $path) = @_;
Subroutine watch_directory: not called CC=4 SCAR=30.0
Pod undocumented
75  
76 0 ▶ unless (-d $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $path) 0 0 0
77 0 warn "missing watch directory: $path\n";
78 0 return;
79 }
80
81 0 ▶ say "watching $path" if ($self->{print} & WATCH_DIR);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"print"} & 1 0 0 0
82  
83 0 ▶ unless ( $self->inotify->watch(
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->inotify->watch($path, 4040, sub { my $e = shift(); my $path = $e->fullname; warn "event queue overflowed\n" if $e->IN_Q_OVERFLOW; if ($e->IN_ISDIR) { if ($e->IN_CREATE or $e->IN_MOVED_TO) { $self->watch_recursive($path); } elsif ($e->IN_DELETE_SELF or $e->IN_MOVE_SELF) { say "unwatching $path" if $self->{"print"} & 1; $e->w->cancel; } ; } elsif ($e->IN_CLOSE_WRITE or $e->IN_MOVED_TO) { $self->_callback($path); } ; } )) 0 0 0
84 $path,
85 IN_CREATE | IN_CLOSE_WRITE | IN_MOVE | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF,
86 sub {
87 0 ▶ my $e = shift;
Subroutine __ANON__: not called CC=10 SCAR=47.0
88 0 my $path = $e->fullname;
89
90 0 ▶ warn "event queue overflowed\n" if $e->IN_Q_OVERFLOW;
Branch: 1 branch
BranchTrueFalseTotal
if $e->IN_Q_OVERFLOW 0 0 0
91
92 0 ▶ if ( $e->IN_ISDIR ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e->IN_ISDIR) { } 0 0 0
elsif ($e->IN_CLOSE_WRITE or $e->IN_MOVED_TO) { } 0 0 0
93 0 ▶ if ( $e->IN_CREATE || $e->IN_MOVED_TO) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e->IN_CREATE or $e->IN_MOVED_TO) { } 0 0 0
elsif ($e->IN_DELETE_SELF or $e->IN_MOVE_SELF) { } 0 0 0
Condition0%0/3
$e->IN_CREATE or $e->IN_MOVED_TO
l!l&&r!l&&!r
000
MC/DC0%0/2
$e->IN_CREATE or $e->IN_MOVED_TO
$e->IN_CREATE $e->IN_MOVED_TO
Truth table0%0/3
A or B
A= $e->IN_CREATEB= $e->IN_MOVED_TO
ABresult
1X1
011
000
94 0 $self->watch_recursive($path);
95 ▶ } elsif ( $e->IN_DELETE_SELF || $e->IN_MOVE_SELF ) {
Condition0%0/3
$e->IN_DELETE_SELF or $e->IN_MOVE_SELF
l!l&&r!l&&!r
000
MC/DC0%0/2
$e->IN_DELETE_SELF or $e->IN_MOVE_SELF
$e->IN_DELETE_SELF $e->IN_MOVE_SELF
Truth table0%0/3
A or B
A= $e->IN_DELETE_SELFB= $e->IN_MOVE_SELF
ABresult
1X1
011
000
96 0 ▶ say "unwatching $path" if ($self->{print} & WATCH_DIR);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"print"} & 1 0 0 0
97 0 $e->w->cancel;
98 }
99 ▶ } elsif ( $e->IN_CLOSE_WRITE || $e->IN_MOVED_TO ) {
Condition0%0/3
$e->IN_CLOSE_WRITE or $e->IN_MOVED_TO
l!l&&r!l&&!r
000
MC/DC0%0/2
$e->IN_CLOSE_WRITE or $e->IN_MOVED_TO
$e->IN_CLOSE_WRITE $e->IN_MOVED_TO
Truth table0%0/3
A or B
A= $e->IN_CLOSE_WRITEB= $e->IN_MOVED_TO
ABresult
1X1
011
000
100 0 $self->_callback($path);
101 }
102  
103 }
104 ) ) {
105 0 warn "watching $path failed: $!\n";
106 };
107 }
108  
109 sub _callback {
110 0 ▶ my ($self, $path) = @_;
Subroutine _callback: not called CC=10 SCAR=47.0
111  
112 0 ▶ if ($self->{filter} && $path !~ $self->{filter}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"filter"} and not $path =~ /$self->{"filter"}/) 0 0 0
Condition0%0/3
$self->{"filter"} and not $path =~ /$self->{"filter"}/
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"filter"} and not $path =~ /$self->{"filter"}/
$self->{"filter"} not $path =~ /$self->{"filter"}/
Truth table0%0/3
A and not(B)
A= $self->{"filter"}B= not $path =~ /$self->{"filter"}/
ABresult
0X0
100
111
113 0 return;
114 }
115  
116 0 ▶ say $path if ($self->{print} & FOUND_FILE);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"print"} & 2 0 0 0
117
118 0 my $delete;
119 0 ▶ if ($self->{catch}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'catch'}) { } 0 0 0
120 0 $delete = eval { $self->{callback}->($path) };
121 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
122 0 ▶ print "$path: $@" if $self->{print} & CATCH_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"print"} & 8 0 0 0
123 0 $self->{catch}->($path, $@);
124 0 return;
125 }
126 } else {
127 0 $delete = $self->{callback}->($path);
128 }
129  
130 0 ▶ if ( $delete && $self->{delete} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delete and $self->{"delete"}) 0 0 0
Condition0%0/3
$delete and $self->{"delete"}
!ll&&!rl&&r
000
MC/DC0%0/2
$delete and $self->{"delete"}
$delete $self->{"delete"}
Truth table0%0/3
A and B
A= $deleteB= $self->{"delete"}
ABresult
0X0
100
111
131 0 ▶ say $path if ($self->{print} & DELETE_FILE);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"print"} & 4 0 0 0
132 0 unlink $path;
133 }
134 }
135  
136 sub inotify {
137 0 ▶ $_[0]->{inotify};
Subroutine inotify: not called CC=1 SCAR=6.9
Pod documented
138 }
139  
140 sub loop {
141 0 ▶ 1 while $_[0]->inotify->poll;
Subroutine loop: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->inotify->poll and do { 1 } 0 0 0
142 }
143  
144 sub anyevent {
145 0 ▶ my $inotify = $_[0]->inotify;
Subroutine anyevent: not called CC=1 SCAR=6.9
Pod documented
146 AnyEvent->io (
147 0 ▶ fh => $inotify->fileno, poll => 'r', cb => sub { $inotify->poll }
Subroutine __ANON__: not called CC=1 SCAR=6.9
148 0 );
149 }
150  
151 1 1;
152 __END__
↑ Summary Build »