lib/CSS/Watcher/Monitor.pm

filter: Statementstmt 98.7% 81 / 82 Branchbran 77.2% 17 / 22 Conditioncond 75.0% 18 / 24 MC/DCmcdc 53.8% 7 / 13 Subroutinesub 94.7% 18 / 19 Podpod 0.0% 0 / 5 totaltotal 85.4% 141 / 165 CC 21 SCAR 15.7
CC
21
Cov
91%
_get_files_info
11.3
scan
6.0
is_changed
3.0
CRAP 4.8
« lib/CSS/Watcher.pm ↑ Summary
1 package CSS::Watcher::Monitor;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Fcntl ':mode';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use List::MoreUtils qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our @STAT_FIELDS = qw(
13 dev inode mode num_links uid gid rdev size atime mtime ctime
14 blk_size blocks
15 );
16  
17 sub new {
18 8 ▶ my $class= shift;
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
19 8 my $options = shift;
20  
21 return bless ({
22 dir => $options->{dir} // undef,
23 8 ▶ oldstats => {},
Condition100%2/2
$options->{"dir"} // undef
l!l
71
MC/DC100%1/1
$options->{"dir"} // undef
$options->{"dir"}
Truth table100%2/2
A
A= $options->{"dir"}
Aresult
11
00
24 }, $class);
25 }
26  
27 sub dir {
28 33 ▶ my $self = shift;
Subroutine dir: called CC=2 SCAR=7.1
Pod undocumented
29 33 ▶ croak "dir attribute is read-only" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 33 33
30 33 return $self->{dir};
31 }
32  
33 sub scan {
34 14 ▶ my ($self, $callback, $skip_dirs) = @_;
Subroutine scan: called CC=6 SCAR=17.9
Pod undocumented
35  
36 14 ▶ return 0 unless (defined $callback && defined $self->dir && -d $self->dir);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $callback and defined $self->dir and -d $self->dir 2 10 12
Condition100%6/6
defined $callback and defined $self->dir and -d $self->dir
!ll&&!rl&&r
3110
defined $callback and defined $self->dir
!ll&&!rl&&r
2111
MC/DC100%3/3
defined $callback and defined $self->dir and -d $self->dir
defined $callback defined $self->dir -d $self->dir
Truth table100%4/4
A and B and C
A= defined $callbackB= defined $self->dirC= -d $self->dir
ABCresult
0XX0
10X0
1100
1111
37  
38 10 my $newstat = $self->_get_files_info( $self->dir, $skip_dirs );
39  
40 10 my $changes = 0;
41 10 ▶ while ( my( $fname, $stat ) = each %{$newstat->{files}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$newstat{"files"};}) 44 10 54
42 44 ▶ unless ($self->_deep_compare ($self->_get_stat ($fname), $stat )) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_deep_compare($self->_get_stat($fname), $stat)) 29 15 44
43 29 $self->_set_stat ($fname, $stat);
44 29 $callback->($fname);
45 29 $changes++;
46 }
47 }
48 10 return $changes;
49 }
50  
51 sub is_changed {
52 6 ▶ my ( $self, $filename ) = @_;
Subroutine is_changed: called CC=3 SCAR=11.0
Pod undocumented
53 6 my %objstat;
54 6 @objstat{@STAT_FIELDS} = stat ( $filename );
55  
56 # this file may never present before and not exist, return false
57 6 ▶ return 0 unless (defined ($objstat{atime}) && -f $filename);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $objstat{"atime"} and -f $filename 1 5 6
Condition66%2/3
defined $objstat{"atime"} and -f $filename
!ll&&!rl&&r
105
MC/DC50%1/2
defined $objstat{"atime"} and -f $filename
defined $objstat{"atime"} -f $filename
Truth table66%2/3
A and B
A= defined $objstat{"atime"}B= -f $filename
ABresult
0X0
100
111
58  
59 5 not $self->_deep_compare (
60 $self->_get_stat ($filename),
61 \%objstat);
62 }
63  
64 sub make_dirty {
65 4 ▶ my $self = shift;
Subroutine make_dirty: called CC=1 SCAR=0.0
Pod undocumented
66 4 $self->{oldstats} = {};
67 }
68  
69 sub _get_stat {
70 49 ▶ my ( $self, $filename ) = @_;
Subroutine _get_stat: called CC=2 SCAR=6.9
71 49 ▶ return $self->{oldstats}{$filename} // {};
Condition100%2/2
$self->{"oldstats"}{$filename} // {}
l!l
1732
MC/DC100%1/1
$self->{"oldstats"}{$filename} // {}
$self->{"oldstats"}{$filename}
Truth table100%2/2
A
A= $self->{"oldstats"}{$filename}
Aresult
11
00
72 }
73  
74 sub _set_stat {
75 29 ▶ my ( $self, $filename, $stat ) = @_;
Subroutine _set_stat: called CC=1 SCAR=0.0
76 29 $self->{oldstats}{$filename} = $stat;
77 }
78  
79 sub _deep_compare {
80 49 ▶ my ( $self, $this, $that ) = @_;
Subroutine _deep_compare: called CC=1 SCAR=0.0
81 2 ▶ use Storable qw/freeze/;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 49 local $Storable::canonical = 1;
83 49 return freeze( $this ) eq freeze( $that );
84 }
85  
86 # Scan our target object
87 sub _get_files_info {
88 10 ▶ my ( $self, $dir, $skip_dirs ) = @_;
Subroutine _get_files_info: called CC=10 SCAR=24.3
89 10 my %info;
90  
91 10 ▶ $skip_dirs ||= [];
Condition100%2/2
$skip_dirs ||= []
l!l
46
MC/DC100%1/1
$skip_dirs ||= []
$skip_dirs
Truth table100%2/2
A
A= $skip_dirs
Aresult
11
00
92
93 10 eval {
94 10 ▶ if ( -d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dir) 10 0 10
95  
96 # Expand whole directory tree
97 10 my @work = $self->_read_dir( $dir );
98 10 ▶ while ( my $obj = shift @work ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = shift @work) 54 10 64
99 next # // skip symlinks that have "../" (circular symlink)
100 54 ▶ if ( -d $obj
Branch: 1 branch
BranchTrueFalseTotal
if -d $obj and -l $obj and readlink($obj) =~ m[\.\./] 0 54 54
Condition50%3/6
-d $obj and -l $obj and readlink($obj) =~ m[\.\./]
!ll&&!rl&&r
5400
-d $obj and -l $obj
!ll&&!rl&&r
44100
MC/DC0%0/3
-d $obj and -l $obj and readlink($obj) =~ m[\.\./]
-d $obj -l $obj readlink($obj) =~ m[\.\./]
Truth table50%2/4
A and B and C
A= -d $objB= -l $objC= readlink($obj) =~ m[\.\./]
ABCresult
0XX0
10X0
1100
1111
101 && -l $obj
102 && readlink($obj) =~ m|\.\./| );
103 54 ▶ if (-f $obj) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $obj) { } 44 10 54
elsif (-d $obj and not &any(sub { $obj =~ /$_/; } , @{$skip_dirs;})) { } 10 0 10
104 44 my %objstat;
105 44 @objstat{@STAT_FIELDS} = stat ( $obj );
106 44 $info{ files }{ $obj } = \%objstat;
107 }
108 10 ▶ elsif ( -d $obj && ( !any { $obj =~ m/$_/; } @{$skip_dirs} ) ) {
Subroutine __ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
-d $obj and not &any(sub {
!ll&&!rl&&r
0010
MC/DC0%0/2
-d $obj and not &any(sub { $obj =~ /$_/; } , @{$skip_dirs;})
-d $obj not &any(sub { $obj =~ /$_/; } , @{$skip_dirs;})
Truth table33%1/3
A and not(B)
A= -d $objB= not &any(sub { $obj =~ /$_/; } , @{$skip_dirs;})
ABresult
0X0
100
111
109 # Depth first to simulate recursion
110 10 unshift @work, $self->_read_dir( $obj );
111 }
112 }
113 }
114 };
115  
116 10 $info{error} = $@;
117  
118 10 return \%info;
119 }
120  
121 sub _read_dir {
122 20 ▶ my $self = shift;
Subroutine _read_dir: called CC=2 SCAR=7.0
123 20 my $dir = shift;
124  
125 20 ▶ opendir( my $dh, $dir ) or LOGDIE "Can't read $dir ($!)";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or LOGDIE("Can't read $dir ($!)") 0 20 20
126 54 my @files = map { File::Spec->catfile( $dir, $_ ) }
127 sort
128 20 grep { $_ !~ /^[.]{1,2}$/ } readdir( $dh );
129 20 closedir( $dh );
130  
131 20 return @files;
132 }
133  
134  
135 2 1;
136  
137 =head1 NAME
138  
139 CSS::Watcher::Monitor - Monitor files for changes.
140  
141 =head1 SYNOPSIS
142  
143 use CSS::Watcher::Monitor;
144 my $cm = CSS::Watcher::Monitor->new (dir => '/foo/bar');
145  
146 # return num of files modified
147 $cm->scan(
148 sub {
149 my $file = shift;
150 # process changed file or first scan new file
151 } );
152  
153 # Check does file changed since last $cm->scan
154 say $cm->is_changed('/foo/bar/baz.txt');
155  
156 # clean old file stat cache
157 $cm->make_dirty();
158  
159 =head1 DESCRIPTION
160  
161 Watch for changes, call callback sub. Call callback on first scan too.
162  
163 =head1 SEE ALSO
164  
165 File::Monitor - I get some patterns from there
166  
167 =head1 AUTHOR
168  
169 Olexandr Sydorchuk (olexandr.syd@gmail.com)
170  
171 =head1 COPYRIGHT AND LICENSE
172  
173 Copyright (C) 2014 by Olexandr Sydorchuk
174  
175 This program is free software; you can redistribute it and/or modify
176 it under the same terms as Perl itself, either Perl version 5.8.2 or,
177 at your option, any later version of Perl 5 you may have available.
178  
179 =cut
« lib/CSS/Watcher.pm ↑ Summary