| 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 | 9 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 9 | my $class= shift; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 9 | my $options = shift; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | return bless ({ | ||||||||||||||||||||||||||||||||||||||||||
| 22 | dir => $options->{dir} // undef, | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 9 | ▶ | oldstats => {}, | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options->{"dir"} // undef
MC/DC100%1/1
$options->{"dir"} // undef
$options->{"dir"}
Truth table100%2/2
A
A= $options->{"dir"}
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | }, $class); | ||||||||||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 37 | ▶ | sub dir { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dir: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 37 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 37 | ▶ | croak "dir attribute is read-only" if @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 37 | return $self->{dir}; | |||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 15 | ▶ | sub scan { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
scan: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 15 | my ($self, $callback, $skip_dirs) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 15 | ▶ | return 0 unless (defined $callback && defined $self->dir && -d $self->dir); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $callback and defined $self->dir and -d $self->dir
defined $callback and defined $self->dir
MC/DC66%2/3
defined $callback and defined $self->dir and -d $self->dir
defined $callback
defined $self->dir
-d $self->dir
Truth table75%3/4
A and B and C
A= defined $callbackB= defined $self->dirC= -d $self->dir
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 12 | my $newstat = $self->_get_files_info( $self->dir, $skip_dirs ); | |||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 12 | my $changes = 0; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 12 | ▶ | while ( my( $fname, $stat ) = each %{$newstat->{files}} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 50 | ▶ | unless ($self->_deep_compare ($self->_get_stat ($fname), $stat )) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 35 | $self->_set_stat ($fname, $stat); | |||||||||||||||||||||||||||||||||||||||||
| 44 | 35 | $callback->($fname); | |||||||||||||||||||||||||||||||||||||||||
| 45 | 35 | $changes++; | |||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 12 | return $changes; | |||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 7 | ▶ | sub is_changed { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_changed: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 7 | my ( $self, $filename ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 7 | my %objstat; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 7 | @objstat{@STAT_FIELDS} = stat ( $filename ); | |||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | # this file may never present before and not exist, return false | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 7 | ▶ | return 0 unless (defined ($objstat{atime}) && -f $filename); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $objstat{"atime"} and -f $filename
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
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 6 | not $self->_deep_compare ( | |||||||||||||||||||||||||||||||||||||||||
| 60 | $self->_get_stat ($filename), | ||||||||||||||||||||||||||||||||||||||||||
| 61 | \%objstat); | ||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 5 | ▶ | sub make_dirty { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
make_dirty: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 5 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 5 | $self->{oldstats} = {}; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 56 | ▶ | sub _get_stat { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_stat: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 56 | my ( $self, $filename ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 56 | ▶ | return $self->{oldstats}{$filename} // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"oldstats"}{$filename} // {}
MC/DC100%1/1
$self->{"oldstats"}{$filename} // {}
$self->{"oldstats"}{$filename}
Truth table100%2/2
A
A= $self->{"oldstats"}{$filename}
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 35 | ▶ | sub _set_stat { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_stat: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 35 | my ( $self, $filename, $stat ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 35 | $self->{oldstats}{$filename} = $stat; | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 56 | ▶ | sub _deep_compare { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_deep_compare: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 56 | my ( $self, $this, $that ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 2 | ▶ | use Storable qw/freeze/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 56 | local $Storable::canonical = 1; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 56 | return freeze( $this ) eq freeze( $that ); | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | # Scan our target object | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 12 | ▶ | sub _get_files_info { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_files_info: called CC=10 SCAR=23.4
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 12 | my ( $self, $dir, $skip_dirs ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 12 | my %info; | |||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 12 | ▶ | $skip_dirs ||= []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$skip_dirs ||= []
MC/DC100%1/1
$skip_dirs ||= []
$skip_dirs
Truth table100%2/2
A
A= $skip_dirs
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 12 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 94 | 12 | ▶ | if ( -d $dir ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | # Expand whole directory tree | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 12 | my @work = $self->_read_dir( $dir ); | |||||||||||||||||||||||||||||||||||||||||
| 98 | 12 | ▶ | while ( my $obj = shift @work ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | next # // skip symlinks that have "../" (circular symlink) | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 64 | ▶ | if ( -d $obj | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
-d $obj and -l $obj and readlink($obj) =~ m[\.\./]
-d $obj and -l $obj
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[\.\./]
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | && -l $obj | ||||||||||||||||||||||||||||||||||||||||||
| 102 | && readlink($obj) =~ m|\.\./| ); | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 64 | ▶ | if (-f $obj) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 50 | my %objstat; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 50 | @objstat{@STAT_FIELDS} = stat ( $obj ); | |||||||||||||||||||||||||||||||||||||||||
| 106 | 50 | $info{ files }{ $obj } = \%objstat; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 14 | ▶ | elsif ( -d $obj && ( !any { $obj =~ m/$_/; } @{$skip_dirs} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
Condition66%2/3
-d $obj and not &any(sub {
$obj =~ /$_/;
}
, @{$skip_dirs;})
MC/DC50%1/2
-d $obj and not &any(sub {
$obj =~ /$_/;
}
, @{$skip_dirs;})
-d $obj
not &any(sub {
$obj =~ /$_/;
}
, @{$skip_dirs;})
Truth table66%2/3
A and not(B)
A= -d $objB= not &any(sub {
$obj =~ /$_/;
}
, @{$skip_dirs;})
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | # Depth first to simulate recursion | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 12 | unshift @work, $self->_read_dir( $obj ); | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 12 | $info{error} = $@; | |||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 12 | return \%info; | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 24 | ▶ | sub _read_dir { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_read_dir: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 24 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 24 | my $dir = shift; | |||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 24 | ▶ | opendir( my $dh, $dir ) or LOGDIE "Can't read $dir ($!)"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 64 | my @files = map { File::Spec->catfile( $dir, $_ ) } | |||||||||||||||||||||||||||||||||||||||||
| 127 | sort | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 24 | grep { $_ !~ /^[.]{1,2}$/ } readdir( $dh ); | |||||||||||||||||||||||||||||||||||||||||
| 129 | 24 | closedir( $dh ); | |||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 24 | 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 | ||||||||||||||||||||||||||||||||||||||||||