blib/lib/File/Hotfolder.pm

filter: Statementstmt 33.0% 39 / 118 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 33.3% 11 / 33 Podpod 57.1% 4 / 7 totaltotal 19.7% 54 / 273 CC 43 SCAR 33.8
CC
43
Cov
18%
__ANON__
90.0
new
56.0
_callback
56.0
CRAP 29.3
↑ 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.04';
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 File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Linux::Inotify2;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 our %EXPORT_TAGS = (print => [qw(
17 WATCH_DIR FOUND_FILE KEEP_FILE DELETE_FILE
18 CATCH_ERROR WATCH_ERROR HOTFOLDER_ERROR
19 HOTFOLDER_ALL
20 )]);
21 1 our @EXPORT = ('watch', @{$EXPORT_TAGS{'print'}});
22 1 $EXPORT_TAGS{all} = \@EXPORT;
23  
24 use constant {
25 1 WATCH_DIR => 1,
26 UNWATCH_DIR => 2,
27 FOUND_FILE => 4,
28 KEEP_FILE => 8,
29 DELETE_FILE => 16,
30 CATCH_ERROR => 32,
31 WATCH_ERROR => 64,
32 HOTFOLDER_ALL => 128-1,
33 HOTFOLDER_ERROR => 32 | 64,
34 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 # function interface
37 sub watch {
38 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
39 0 ▶ File::Hotfolder->new( @_ % 2 ? (watch => @_) : @_ );
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 0 0
40 }
41  
42 # object interface
43 sub new {
44 0 ▶ my ($class, %args) = @_;
Subroutine new: not called CC=7 SCAR=40.3
Pod undocumented
45  
46 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
47 0 ▶ $path = File::Spec->rel2abs($path) if $args{fullname};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"fullname"} 0 0 0
48 croak "Missing watch directory: $path" unless -d $path,
49  
50 my $self = bless {
51 inotify => (Linux::Inotify2->new
52 or croak "Unable to create new inotify object: $!"),
53 0 ▶ callback => ($args{callback} || sub { 1 }),
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 delete => !!$args{delete},
55 print => 0+($args{print} || 0),
56 filter => _build_filter($args{filter},
57 0 ▶ sub { $_[0] !~ qr{^(.*/)?\.[^/]*$} }),
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 filter_dir => _build_filter($args{filter_dir}, qr{^[^.]|^.$}),
59 scan => $args{scan},
60 catch => _build_catch($args{catch}),
61 0 ▶ logger => _build_logger($args{logger}),
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", _build_filter($args{"filter"}, sub { not $_[0] =~ qr"^(.*/)?\.[^/]*$"; } ), "filter_dir", _build_filter($args{"filter_dir"}, qr/^[^.]|^.$/), "scan", $args{"scan"}, "catch", _build_catch($args{"catch"}), "logger", _build_logger($args{"logger"})}, $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
62 }, $class;
63  
64 0 $self->watch_recursive( $path );
65  
66 0 $self;
67 }
68  
69 sub _build_catch {
70 0 ▶ my ($catch) = @_;
Subroutine _build_catch: not called CC=4 SCAR=30.0
71 0 ▶ return $catch if ref $catch // '' eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if 0 0 0
Condition0%0/2
ref $catch //
l!l
00
MC/DC0%0/1
ref $catch //
ref $catch
Truth table0%0/2
A
A= ref $catch
Aresult
11
00
72 0 ▶ return $catch ? sub { } : undef;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$catch ? : 0 0 0
73 }
74  
75 sub _build_filter {
76 0 ▶ my $filter = $_[0] // $_[1];
Subroutine _build_filter: not called CC=4 SCAR=30.0
Condition0%0/3
$_[0] // $_[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0] // $_[1]
$_[0] $_[1]
Truth table0%0/3
A // B
A= $_[0]B= $_[1]
ABresult
1X1
011
000
77 0 ▶ return unless $filter;
Branch: 1 branch
BranchTrueFalseTotal
unless $filter 0 0 0
78 0 ▶ return sub { $_[0] =~ $filter } if ref $filter eq ref qr//;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if ref $filter eq ref qr// 0 0 0
79 0 $filter;
80 }
81  
82 sub watch_recursive {
83 0 ▶ my ($self, $path) = @_;
Subroutine watch_recursive: not called CC=3 SCAR=24.8
Pod undocumented
84  
85 my $args = {
86 no_chdir => 1,
87 wanted => sub {
88 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
89 0 $self->_watch_directory($_);
90 } elsif( $self->{scan} ) {
91 # TODO: check if not open or modified (lsof or fuser)
92 0 $self->_callback($_);
93 }
94 },
95 0 };
96  
97 0 ▶ if ($self->{filter_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"filter_dir"}) 0 0 0
98 0 ▶ return unless $self->{filter_dir}->(basename($path));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"filter_dir"}->(basename($path)) 0 0 0
99 $args->{preprocess} = sub {
100 0 ▶ grep { $self->{filter_dir}->($_) } @_
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 0 };
102 }
103
104 0 find( $args, $path );
105 }
106  
107 sub _watch_directory {
108 0 ▶ my ($self, $path) = @_;
Subroutine _watch_directory: not called CC=2 SCAR=17.9
109  
110 0 $self->log( WATCH_DIR, $path );
111  
112 0 ▶ unless ( $self->inotify->watch(
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->inotify->watch($path, 4040, sub { my $e = shift(); my $path = $e->fullname; if ($e->IN_Q_OVERFLOW) { $self->log(64, $path, "event queue overflowed"); } ; 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) { $self->log(2, $path); $e->w->cancel; } ; } elsif ($e->IN_CLOSE_WRITE or $e->IN_MOVED_TO) { $self->_callback($path); } ; } )) 0 0 0
113 $path,
114 IN_CREATE | IN_CLOSE_WRITE | IN_MOVE | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF,
115 sub {
116 0 ▶ my $e = shift;
Subroutine __ANON__: not called CC=9 SCAR=45.0
117 0 my $path = $e->fullname;
118
119 0 ▶ if ( $e->IN_Q_OVERFLOW ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->IN_Q_OVERFLOW) 0 0 0
120 0 $self->log( WATCH_ERROR, $path, "event queue overflowed" );
121 }
122
123 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
124 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
125 0 $self->watch_recursive($path);
126 ▶ } 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
127 0 $self->log( UNWATCH_DIR, $path );
128 0 $e->w->cancel;
129 }
130 ▶ } 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
131 0 $self->_callback($path);
132 }
133  
134 }
135 ) ) {
136 0 $self->log( WATCH_ERROR, $path, "failed to create watch: $!" );
137 };
138 }
139  
140 sub _callback {
141 0 ▶ my ($self, $path) = @_;
Subroutine _callback: not called CC=7 SCAR=40.3
142  
143 0 ▶ if ($self->{filter} && !$self->{filter}->($path)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"filter"} and not $self->{"filter"}->($path)) 0 0 0
Condition0%0/3
$self->{"filter"} and not $self->{"filter"}->($path)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"filter"} and not $self->{"filter"}->($path)
$self->{"filter"} not $self->{"filter"}->($path)
Truth table0%0/3
A and not(B)
A= $self->{"filter"}B= not $self->{"filter"}->($path)
ABresult
0X0
100
111
144 0 return;
145 }
146  
147 0 $self->log( FOUND_FILE, $path );
148
149 0 my $status;
150 0 ▶ if ($self->{catch}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'catch'}) { } 0 0 0
151 0 $status = eval { $self->{callback}->($path) };
152 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
153 0 $self->log( CATCH_ERROR, $path, $@ );
154 0 $self->{catch}->($path, $@);
155 0 return;
156 }
157 } else {
158 0 $status = $self->{callback}->($path);
159 }
160  
161 0 ▶ if ( $status && $self->{delete} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status and $self->{'delete'}) { } 0 0 0
Condition0%0/3
$status and $self->{"delete"}
!ll&&!rl&&r
000
MC/DC0%0/2
$status and $self->{"delete"}
$status $self->{"delete"}
Truth table0%0/3
A and B
A= $statusB= $self->{"delete"}
ABresult
0X0
100
111
162 0 unlink $path;
163 0 $self->log( DELETE_FILE, $path );
164 } else {
165 0 $self->log( KEEP_FILE, $path );
166 }
167 }
168  
169 sub loop {
170 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
171 }
172  
173 sub anyevent {
174 0 ▶ my $inotify = $_[0]->inotify;
Subroutine anyevent: not called CC=1 SCAR=6.9
Pod documented
175 AnyEvent->io (
176 0 ▶ fh => $inotify->fileno, poll => 'r', cb => sub { $inotify->poll }
Subroutine __ANON__: not called CC=1 SCAR=6.9
177 0 );
178 }
179  
180 sub inotify {
181 0 ▶ $_[0]->{inotify};
Subroutine inotify: not called CC=1 SCAR=6.9
Pod documented
182 }
183  
184 ## LOGGING
185  
186 1 our %LOGS = (
187 WATCH_DIR , "watching %s",
188 UNWATCH_DIR , "unwatching %s",
189 FOUND_FILE , "found %s",
190 KEEP_FILE , "keep %s",
191 DELETE_FILE , "delete %s",
192 CATCH_ERROR , "error %s: %s",
193 WATCH_ERROR , "failed %s: %s",
194 );
195  
196 sub _build_logger {
197 0 ▶ my ($logger) = @_;
Subroutine _build_logger: not called CC=6 SCAR=37.4
198  
199 0 ▶ if ( not defined $logger ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $logger) { } 0 0 0
elsif (builtin::blessed($logger) and $logger->can('log')) { } 0 0 0
elsif (ref $logger // !1) { } 0 0 0
200 sub {
201 0 ▶ my (%args) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
202 0 ▶ my $fh = $args{event} & HOTFOLDER_ERROR ? *STDERR : *STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
$args{'event'} & 96 ? : 0 0 0
203 0 say $fh $args{message};
204 }
205 0 ▶ } elsif (blessed $logger && $logger->can('log')) {
Condition0%0/3
builtin::blessed($logger) and $logger->can("log")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($logger) and $logger->can("log")
builtin::blessed($logger) $logger->can("log")
Truth table0%0/3
A and B
A= builtin::blessed($logger)B= $logger->can("log")
ABresult
0X0
100
111
206 sub {
207 0 ▶ my (%args) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
208 $logger->log(
209 level => $args{event} & HOTFOLDER_ERROR ? 'error' : 'info',
210 message => $args{message}
211 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$args{'event'} & 96 ? : 0 0 0
212 }
213 0 ▶ } elsif (ref $logger // '' eq 'CODE') {
Condition0%0/2
ref $logger //
l!l
00
MC/DC0%0/1
ref $logger //
ref $logger
Truth table0%0/2
A
A= ref $logger
Aresult
11
00
214 0 $logger;
215 } else {
216 0 croak "logger must be code or provide a log method!";
217 }
218 }
219  
220 sub log {
221 0 ▶ my ($self, $event, $path, $error) = @_;
Subroutine log: not called CC=2 SCAR=17.9
Pod undocumented
222 0 ▶ if ( $event & $self->{print} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event & $self->{"print"}) 0 0 0
223 $self->{logger}->(
224 event => $event,
225 path => $path,
226 0 message => sprintf($LOGS{$event}, $path, $event),
227 );
228 }
229 }
230  
231 1 1;
232 __END__
↑ Summary Build »