| 1 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de> | ||||||||||||||||||||||||||||
| 3 | # File: GX/File/Watcher.pm | ||||||||||||||||||||||||||||
| 4 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package GX::File::Watcher; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use GX::Exception; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use File::Find (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use File::Spec (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 15 | # Class setup | ||||||||||||||||||||||||||||
| 16 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use GX::Class::Object; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | has 'directories' => ( | |||||||||||||||||||||||||||
| 21 | isa => 'Hash', | ||||||||||||||||||||||||||||
| 22 | initialize => 1, | ||||||||||||||||||||||||||||
| 23 | accessors => { | ||||||||||||||||||||||||||||
| 24 | 'directories' => { type => 'get_keys' }, | ||||||||||||||||||||||||||||
| 25 | '_directories' => { type => 'get_reference' } | ||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 2 | has 'files' => ( | |||||||||||||||||||||||||||
| 30 | isa => 'Hash', | ||||||||||||||||||||||||||||
| 31 | initialize => 1, | ||||||||||||||||||||||||||||
| 32 | accessors => { | ||||||||||||||||||||||||||||
| 33 | 'files' => { type => 'get_keys' }, | ||||||||||||||||||||||||||||
| 34 | '_files' => { type => 'get_reference' } | ||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 2 | build; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 42 | # Public methods | ||||||||||||||||||||||||||||
| 43 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 26 | ▶ | sub find_changes { | ||||||||||||||||||||||||||
|
Subroutine
find_changes: called CC=20 SCAR=30.5
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 26 | my $self = shift; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 26 | ▶ | my $fast_mode = wantarray ? 0 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 26 | my $directories = $self->_directories; | |||||||||||||||||||||||||||
| 52 | 26 | my $files = $self->_files; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 26 | my @changes; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | CHECK: | ||||||||||||||||||||||||||||
| 57 | { | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 26 | ▶ | while ( my ( $directory, $info ) = each %$directories ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 24 | ▶ | if ( ! -d $directory ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 2 | push @changes, $directory; | |||||||||||||||||||||||||||
| 63 | 2 | ▶ | $fast_mode ? last CHECK : next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 22 | ▶ | my @stat = stat $directory or complain "Cannot stat \"$directory\""; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 22 | ▶ | if ( $stat[9] != $info->[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | push @changes, $directory; | |||||||||||||||||||||||||||
| 70 | 0 | ▶ | $fast_mode ? last CHECK : next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 22 | ▶ | opendir DIR, $directory or complain "Cannot open \"$directory\" ($!)"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 22 | my @content = readdir DIR; | |||||||||||||||||||||||||||
| 75 | 22 | closedir DIR; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 22 | ▶ | if ( @content != $info->[1] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 8 | push @changes, $directory; | |||||||||||||||||||||||||||
| 79 | 8 | ▶ | $fast_mode ? last CHECK : next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 21 | ▶ | while ( my ( $file, $info ) = each %$files ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 25 | ▶ | if ( ! -f $file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 5 | push @changes, $file; | |||||||||||||||||||||||||||
| 88 | 5 | ▶ | $fast_mode ? last CHECK : next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 20 | ▶ | my @stat = stat $file or complain "Cannot stat \"$file\""; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 20 | ▶ | if ( $stat[9] != $info->[0] || $stat[7] != $info->[1] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$stat[9] != $info->[0] or $stat[7] != $info->[1]
MC/DC50%1/2
$stat[9] != $info->[0] or $stat[7] != $info->[1]
$stat[9] != $info->[0]
$stat[7] != $info->[1]
Truth table66%2/3
A or B
A= $stat[9] != $info->[0]B= $stat[7] != $info->[1]
| |||||||||||||||||||||||||||||
| 94 | 5 | push @changes, $file; | |||||||||||||||||||||||||||
| 95 | 5 | ▶ | $fast_mode ? last CHECK : next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 26 | ▶ | if ( $fast_mode ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 13 | keys %$directories; | |||||||||||||||||||||||||||
| 104 | 13 | keys %$files; | |||||||||||||||||||||||||||
| 105 | 13 | ▶ | return @changes ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | else { | ||||||||||||||||||||||||||||
| 108 | 13 | return @changes; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 6 | ▶ | sub watch { | ||||||||||||||||||||||||||
|
Subroutine
watch: called CC=12 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 6 | my @files; | |||||||||||||||||||||||||||
| 118 | my @directories; | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 6 | for ( @_ ) { | |||||||||||||||||||||||||||
| 121 | 6 | ▶ | -d && push( @directories, $_ ) && next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-d $_ and push @directories, $_
MC/DC0%0/2
-d $_ and push @directories, $_
-d $_
push @directories, $_
Truth table33%1/3
A and B
A= -d $_B= push @directories, $_
| |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | -f && push( @files, $_ ) && next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-f $_ and push @files, $_
MC/DC0%0/2
-f $_ and push @files, $_
-f $_
push @files, $_
Truth table0%0/3
A and B
A= -f $_B= push @files, $_
| |||||||||||||||||||||||||||||
| 123 | 0 | complain "Invalid argument"; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 6 | ▶ | if ( @directories ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | File::Find::find( | ||||||||||||||||||||||||||||
| 129 | { | ||||||||||||||||||||||||||||
| 130 | 13 | ▶ | wanted => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=17.8
| |||||||||||||||||||||||||||||
| 131 | 13 | ▶ | -f && push( @files, $_ ) && return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-f $_ and push @files, $_
MC/DC50%1/2
-f $_ and push @files, $_
-f $_
push @files, $_
Truth table66%2/3
A and B
A= -f $_B= push @files, $_
| |||||||||||||||||||||||||||||
| 132 | 6 | ▶ | -d && push( @directories, $_ ) && return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-d $_ and push @directories, $_
MC/DC0%0/2
-d $_ and push @directories, $_
-d $_
push @directories, $_
Truth table33%1/3
A and B
A= -d $_B= push @directories, $_
| |||||||||||||||||||||||||||||
| 133 | }, | ||||||||||||||||||||||||||||
| 134 | 6 | no_chdir => 1 | |||||||||||||||||||||||||||
| 135 | }, | ||||||||||||||||||||||||||||
| 136 | @directories | ||||||||||||||||||||||||||||
| 137 | ) | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 6 | my $directories = $self->_directories; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 6 | for my $directory ( map { File::Spec->canonpath( $_ ) } @directories ) { | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 12 | ▶ | my @stat = stat $directory or complain "Cannot stat \"$directory\""; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 12 | ▶ | opendir DIR, $directory or complain "Cannot open \"$directory\" ($!)"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 12 | my @content = readdir DIR; | |||||||||||||||||||||||||||
| 149 | 12 | closedir DIR; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 12 | $directories->{$directory} = [ $stat[9], scalar @content ]; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 6 | my $files = $self->_files; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 6 | for my $file ( map { File::Spec->canonpath( $_ ) } @files ) { | |||||||||||||||||||||||||||
| 158 | 7 | ▶ | my @stat = stat $file or complain "Cannot stat \"$file\""; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 7 | $files->{$file} = [ $stat[9], $stat[7] ]; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 6 | return; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 2 | 1; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | __END__ | ||||||||||||||||||||||||||||