blib/lib/POEx/DirNotify.pm

filter: Statementstmt 93.3% 126 / 135 Branchbran 67.8% 19 / 28 Conditioncond 71.4% 5 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 95.2% 20 / 21 Podpod 66.6% 4 / 6 totaltotal 87.5% 176 / 201 CC 23 SCAR 12.9
CC
23
Cov
88%
unmonitor
7.1
shutdown
6.0
monitor
5.0
CRAP 3.6
↑ Summary
1 ## $Id$
2 #####################################################################
3 package POEx::DirNotify;
4  
5 6 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 our $VERSION = '0.02_00';
10 6 $VERSION = eval $VERSION; # see L<perlmodstyle>
11  
12 6 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use POE::Session::PlainCall;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 6 ▶use Digest::MurmurHash qw( murmur_hash );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use Storable qw( dclone );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use POSIX qw( sigaction );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use Fcntl qw/F_NOTIFY O_RDONLY
Subroutine BEGIN: called CC=1 SCAR=0.0
19 DN_CREATE DN_ACCESS DN_MODIFY DN_RENAME
20 DN_DELETE DN_ATTRIB DN_MULTISHOT
21 6 /;
22  
23 sub DEBUG () { 0 }
24  
25 #############################################
26 sub spawn
27 3 ▶{
Subroutine spawn: called CC=2 SCAR=7.0
Pod documented
28 3 my( $package, %init ) = @_;
29  
30 3 my $options = delete $init{options};
31 3 ▶ $options ||= {};
Condition50%1/2
$options ||= {}
l!l
03
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
32  
33 3 POE::Session::PlainCall->create(
34 package => $package,
35 ctor_args => [ \%init ],
36 options => $options,
37 states => [ qw( _start _stop shutdown
38 monitor unmonitor
39 sig_notify
40 ) ]
41 );
42
43 }
44  
45 #############################################
46 sub new
47 3 ▶{
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
48 3 my( $package, $args ) = @_;
49  
50 3 my $self = bless {
51 path=>{} # path => $notifies
52 }, $package;
53 3 ▶ $self->{alias} = $args->{alias} || 'dnotify';
Condition100%2/2
$args->{'alias'} || 'dnotify'
l!l
12
MC/DC100%1/1
$args->{'alias'} || 'dnotify'
$args->{'alias'}
Truth table100%2/2
A
A= $args->{'alias'}
Aresult
11
00
54 3 return $self;
55 }
56  
57  
58 #############################################
59 sub _start
60 3 ▶{
Subroutine _start: called CC=1 SCAR=0.0
61 3 my( $self ) = @_;
62 3 poe->kernel->alias_set( $self->{alias} );
63 3 poe->kernel->sig( IO => 'sig_notify' );
64 3 poe->kernel->sig( shutdown => 'shutdown' );
65 }
66  
67 #############################################
68 sub _stop
69 3 ▶{
Subroutine _stop: called CC=1 SCAR=0.0
70 }
71  
72  
73 #############################################
74 sub sig_notify
75 4 ▶{
Subroutine sig_notify: called CC=2 SCAR=6.9
Pod undocumented
76 4 my( $self, $signame ) = @_;
77 4 DEBUG and warn "Notify $signame";
78 4 poe->kernel->sig_handled;
79 4 foreach my $event ( $self->_find_changes ) {
80 6 DEBUG and warn "Call @$event";
81 6 poe->kernel->post( @$event );
82 }
83 }
84  
85 #############################################
86 sub _find_path
87 9 ▶{
Subroutine _find_path: called CC=1 SCAR=0.0
88 9 my( $self, $path ) = @_;
89 9 return $self->{path}{ $path };
90 }
91  
92  
93 sub _build_details
94 7 ▶{
Subroutine _build_details: called CC=2 SCAR=7.0
95 7 my( $self, $path ) = @_;
96 7 my @stat = stat $path;
97 7 ▶ return 0 unless @stat;
Branch: 1 branch
BranchTrueFalseTotal
unless @stat 0 7 7
98 7 $stat[11] = 0;
99 7 $stat[12] = 0;
100 7 return murmur_hash( join '-', @stat );
101 }
102  
103 #############################################
104 sub _find_changes
105 4 ▶{
Subroutine _find_changes: called CC=5 SCAR=16.2
106 4 my( $self ) = @_;
107 4 my @ret;
108 4 foreach my $notify ( values %{ $self->{path} } ) {
109 4 my $details = $self->_build_details( $notify->{path} );
110 4 my $op = 'change';
111 4 ▶ $op = 'delete' unless $details;
Branch: 1 branch
BranchTrueFalseTotal
unless $details 0 4 4
112 4 ▶ if( $details ne $notify->{details} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($details ne $notify->{"details"}) 4 0 4
113 4 foreach my $event ( @{ $notify->{call} } ) {
114 6 my $E = dclone $event;
115 $E->[2] = { op=>$op,
116 path=>$notify->{path}
117 6 };
118 6 push @ret, $E;
119 }
120 }
121 }
122 4 return @ret;
123 }
124  
125 #############################################
126 sub monitor
127 5 ▶{
Subroutine monitor: called CC=5 SCAR=16.2
Pod documented
128 5 my( $self, $args ) = @_;
129 5 my $path = $args->{path};
130 5 my $caller = join ' ', at => poe->caller_file,
131 line => poe->caller_line;
132  
133 5 my $flags = DN_MODIFY|DN_CREATE|DN_RENAME|DN_DELETE|DN_ATTRIB|DN_MULTISHOT; # XXX
134  
135 5 my $call = $self->_build_call( $args );
136  
137 5 my $notify = $self->_find_path( $path );
138 5 ▶ if( $notify ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($notify) 2 3 5
139 2 DEBUG and warn "Monitor $path again\n";
140 2 push @{ $notify->{call} }, $call;
141 2 poe->kernel->refcount_increment( poe->sender, "NOTIFY $path" );
142 2 return 1;
143 }
144  
145  
146 3 ▶ unless( -d $path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $path) 0 3 3
147 0 die "$path isn't a directory $caller\n";
148 }
149  
150 3 DEBUG and warn "Monitor $path\n";
151  
152 3 my $details = $self->_build_details( $path );
153  
154 3 $notify = {
155 path => $path,
156 call => [ $call ],
157 details => $details,
158 fd => undef,
159 };
160  
161 3 $self->{path}{$path} = $notify;
162  
163 3 my $fd;
164 3 ▶ sysopen( $fd, $path, O_RDONLY ) or die "sysopen $path: $! $caller\n";
Branch: 1 branch
BranchTrueFalseTotal
sysopen $fd, $path, 0 or die "sysopen ${path}: $! $caller\n" 3 0 3
165 3 ▶ fcntl( $fd, F_NOTIFY, $flags ) or warn "fcntl F_NOTIFY: $!";
Branch: 1 branch
BranchTrueFalseTotal
fcntl $fd, 1026, $flags or warn "fcntl F_NOTIFY: $!" 3 0 3
166  
167 3 $notify->{fd} = $fd;
168 3 poe->kernel->refcount_increment( poe->session->ID, "NOTIFY $path" );
169 3 poe->kernel->refcount_increment( poe->sender, "NOTIFY $path" );
170  
171 3 return 1;
172 }
173  
174 sub _build_call
175 5 ▶{
Subroutine _build_call: called CC=4 SCAR=13.9
176 5 my( $self, $args ) = @_;
177 5 my $event = $args->{event};
178 5 my $A = $args->{args};
179 5 my $session = poe->sender;
180  
181 5 my $call = [ $session, $event, undef ];
182 5 ▶ if( $A ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($A) 5 0 5
183 5 ▶ $A = dclone $A if ref $A;
Branch: 1 branch
BranchTrueFalseTotal
if ref $A 1 4 5
184 5 ▶ if( 'ARRAY' eq ref $A ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('ARRAY' eq ref $A) { } 1 4 5
185 1 push @$call, @$A;
186 }
187 else {
188 4 push @$call, $A;
189 }
190 }
191 5 return $call;
192 }
193  
194  
195 #############################################
196 sub unmonitor
197 4 ▶{
Subroutine unmonitor: called CC=7 SCAR=19.7
Pod documented
198 4 my( $self, $args ) = @_;
199 4 my $path = $args->{path};
200 4 my $session = poe->sender;
201 4 my $caller = join ' ', at => poe->caller_file,
202 line => poe->caller_line;
203 4 my $notify = $self->_find_path( $path );
204 4 ▶ unless( $notify ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($notify) 0 4 4
205 0 warn "$path wasn't monitored $caller\n";
206 0 return;
207 }
208  
209 4 my @calls;
210 4 foreach my $call ( @{ $notify->{call} } ) {
211 6 ▶ if( $call->[0] eq $session or $self->{force} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($call->[0] eq $session or $self->{'force'}) { } 5 1 6
Condition66%2/3
$call->[0] eq $session or $self->{"force"}
l!l&&r!l&&!r
501
MC/DC50%1/2
$call->[0] eq $session or $self->{"force"}
$call->[0] eq $session $self->{"force"}
Truth table66%2/3
A or B
A= $call->[0] eq $sessionB= $self->{"force"}
ABresult
1X1
011
000
212 5 poe->kernel->refcount_decrement( $session, "NOTIFY $notify->{path}" );
213 }
214 else {
215 1 push @calls, $call;
216 }
217 }
218  
219 4 ▶ if( @calls ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@calls) 1 3 4
220 1 $notify->{call} = \@calls;
221 1 DEBUG and warn "$path still being monitored\n";
222 1 return;
223 }
224 3 DEBUG and warn "Unmonitor $path\n";
225  
226 3 ▶ fcntl( $notify->{fd}, F_NOTIFY, 0 ) or warn "fcntl F_NOTIFY: $! $caller\n";
Branch: 1 branch
BranchTrueFalseTotal
fcntl $notify->{"fd"}, 1026, 0 or warn "fcntl F_NOTIFY: $! $caller\n" 3 0 3
227  
228 3 poe->kernel->refcount_decrement( poe->session->ID, "NOTIFY $notify->{path}" );
229 3 delete $notify->{fd};
230 3 delete $self->{path}{ $path };
231 3 return;
232 }
233  
234 #############################################
235 sub shutdown
236 0 ▶{
Subroutine shutdown: not called CC=2 SCAR=17.9
Pod documented
237 0 my( $self ) = @_;
238 0 DEBUG and warn "Shutdown $self->{alias}\n";
239 0 foreach my $path ( keys %{ $self->{path} } ) {
240 0 local $self->{force} = 1;
241 0 $self->unmonitor( { path=>$path } );
242 }
243 }
244  
245  
246  
247  
248 6 1;
249 __END__
↑ Summary