| 1 | package File::Dir::Dumper::Scanner; | ||||||||||||||
| 2 | 4 | $File::Dir::Dumper::Scanner::VERSION = '0.6.5'; | |||||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 4 | ▶ | use autodie; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 4 | ▶ | use 5.012; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 4 | ▶ | use parent 'File::Dir::Dumper::Base'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 4 | ▶ | use Carp (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 4 | ▶ | use File::Find::Object (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 4 | ▶ | use Devel::CheckOS qw( os_is ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | 4 | ▶ | use POSIX qw(strftime); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 4 | ▶ | use List::Util qw(min); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | |||||||||||||||
| 19 | 4 | use Class::XSAccessor accessors => { | |||||||||||||
| 20 | _digest_cache => '_digest_cache', | ||||||||||||||
| 21 | _digests => '_digests', | ||||||||||||||
| 22 | _file_find => '_file_find', | ||||||||||||||
| 23 | _group_cache => '_group_cache', | ||||||||||||||
| 24 | _last_result => '_last_result', | ||||||||||||||
| 25 | _queue => '_queue', | ||||||||||||||
| 26 | _reached_end => '_reached_end', | ||||||||||||||
| 27 | _result => '_result', | ||||||||||||||
| 28 | _user_cache => '_user_cache', | ||||||||||||||
| 29 | 4 | ▶ | }; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 30 | |||||||||||||||
| 31 | 4 | ▶ | use Digest (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 32 | |||||||||||||||
| 33 | |||||||||||||||
| 34 | sub _init | ||||||||||||||
| 35 | { | ||||||||||||||
| 36 | 4 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_init: called CC=9 SCAR=22.5
| |||||||||||||||
| 37 | 4 | my $args = shift; | |||||||||||||
| 38 | |||||||||||||||
| 39 | 4 | my $dir_to_dump = $args->{dir}; | |||||||||||||
| 40 | |||||||||||||||
| 41 | 4 | $self->_file_find( | |||||||||||||
| 42 | File::Find::Object->new( | ||||||||||||||
| 43 | { | ||||||||||||||
| 44 | followlink => 0, | ||||||||||||||
| 45 | }, | ||||||||||||||
| 46 | $dir_to_dump, | ||||||||||||||
| 47 | ) | ||||||||||||||
| 48 | ); | ||||||||||||||
| 49 | |||||||||||||||
| 50 | 4 | $self->_queue( [] ); | |||||||||||||
| 51 | |||||||||||||||
| 52 | 4 | $self->_add( | |||||||||||||
| 53 | { | ||||||||||||||
| 54 | type => "header", | ||||||||||||||
| 55 | dir_to_dump => $dir_to_dump, | ||||||||||||||
| 56 | stream_type => "Directory Dump" | ||||||||||||||
| 57 | } | ||||||||||||||
| 58 | ); | ||||||||||||||
| 59 | |||||||||||||||
| 60 | 4 | $self->_digests( undef() ); | |||||||||||||
| 61 | 4 | ▶ | if ( exists( $args->{digests} ) ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 62 | { | ||||||||||||||
| 63 | 2 | my $digests = {}; | |||||||||||||
| 64 | 2 | foreach my $d ( @{ $args->{digests} } ) | |||||||||||||
| 65 | { | ||||||||||||||
| 66 | 4 | ▶ | if ( exists $digests->{$d} ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | { | ||||||||||||||
| 68 | 0 | Carp::confess("Duplicate digest '$d'!"); | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | 4 | $digests->{$d} = 1; | |||||||||||||
| 71 | } | ||||||||||||||
| 72 | 2 | ▶ | if ( !%$digests ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | { | ||||||||||||||
| 74 | 0 | Carp::confess("The list of digests is empty."); | |||||||||||||
| 75 | } | ||||||||||||||
| 76 | 2 | $self->_digests( [ sort { $a cmp $b } keys %$digests ] ); | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | 4 | ▶ | my $base = ( $args->{digest_cache} || 'Dummy' ); | ||||||||||||
|
Condition100%2/2
$args->{'digest_cache'} || 'Dummy'
MC/DC100%1/1
$args->{'digest_cache'} || 'Dummy'
$args->{'digest_cache'}
Truth table100%2/2
A
A= $args->{'digest_cache'}
| |||||||||||||||
| 79 | 4 | ▶ | if ( $base !~ /\A[A-Za-z_][A-Za-z_0-9]*\z/ ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 80 | { | ||||||||||||||
| 81 | 0 | Carp::confess("Invalid digest_cache format."); | |||||||||||||
| 82 | } | ||||||||||||||
| 83 | 4 | my $cl = "File::Dir::Dumper::DigestCache::$base"; | |||||||||||||
| 84 | ## no critic | ||||||||||||||
| 85 | 4 | eval "require $cl"; | |||||||||||||
| 86 | ## use critic | ||||||||||||||
| 87 | 4 | ▶ | if ($@) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 88 | { | ||||||||||||||
| 89 | 0 | die $@; | |||||||||||||
| 90 | } | ||||||||||||||
| 91 | $self->_digest_cache( | ||||||||||||||
| 92 | scalar $cl->new( | ||||||||||||||
| 93 | { | ||||||||||||||
| 94 | 4 | ▶ | params => ( $args->{digest_cache_params} || +{} ), | ||||||||||||
|
Condition100%2/2
$args->{'digest_cache_params'} || {}
MC/DC100%1/1
$args->{'digest_cache_params'} || {}
$args->{'digest_cache_params'}
Truth table100%2/2
A
A= $args->{'digest_cache_params'}
| |||||||||||||||
| 95 | |||||||||||||||
| 96 | } | ||||||||||||||
| 97 | ) | ||||||||||||||
| 98 | ); | ||||||||||||||
| 99 | |||||||||||||||
| 100 | 4 | $self->_user_cache( {} ); | |||||||||||||
| 101 | 4 | $self->_group_cache( {} ); | |||||||||||||
| 102 | |||||||||||||||
| 103 | 4 | return; | |||||||||||||
| 104 | } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | sub _add | ||||||||||||||
| 107 | { | ||||||||||||||
| 108 | 30 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_add: called CC=1 SCAR=0.0
| |||||||||||||||
| 109 | 30 | my $token = shift; | |||||||||||||
| 110 | |||||||||||||||
| 111 | 30 | push @{ $self->_queue() }, $token; | |||||||||||||
| 112 | |||||||||||||||
| 113 | 30 | return; | |||||||||||||
| 114 | } | ||||||||||||||
| 115 | |||||||||||||||
| 116 | sub fetch | ||||||||||||||
| 117 | { | ||||||||||||||
| 118 | 33 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
fetch: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 119 | |||||||||||||||
| 120 | 33 | ▶ | if ( !@{ $self->_queue() } ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 121 | { | ||||||||||||||
| 122 | 21 | $self->_populate_queue(); | |||||||||||||
| 123 | } | ||||||||||||||
| 124 | |||||||||||||||
| 125 | 33 | return shift( @{ $self->_queue() } ); | |||||||||||||
| 126 | } | ||||||||||||||
| 127 | |||||||||||||||
| 128 | sub _up_to_level | ||||||||||||||
| 129 | { | ||||||||||||||
| 130 | 14 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_up_to_level: called CC=2 SCAR=6.9
| |||||||||||||||
| 131 | 14 | my $target_level = shift; | |||||||||||||
| 132 | |||||||||||||||
| 133 | 14 | my $last_result = $self->_last_result(); | |||||||||||||
| 134 | |||||||||||||||
| 135 | 14 | for my $level ( | |||||||||||||
| 136 | 14 | reverse( $target_level .. $#{ $last_result->dir_components() } ) ) | |||||||||||||
| 137 | { | ||||||||||||||
| 138 | 8 | $self->_add( | |||||||||||||
| 139 | { | ||||||||||||||
| 140 | type => "updir", | ||||||||||||||
| 141 | depth => $level + 1, | ||||||||||||||
| 142 | } | ||||||||||||||
| 143 | ); | ||||||||||||||
| 144 | } | ||||||||||||||
| 145 | |||||||||||||||
| 146 | 14 | return; | |||||||||||||
| 147 | } | ||||||||||||||
| 148 | |||||||||||||||
| 149 | sub _find_new_common_depth | ||||||||||||||
| 150 | { | ||||||||||||||
| 151 | 12 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_find_new_common_depth: called CC=3 SCAR=11.0
| |||||||||||||||
| 152 | |||||||||||||||
| 153 | 12 | my $result = $self->_result(); | |||||||||||||
| 154 | 12 | my $last_result = $self->_last_result(); | |||||||||||||
| 155 | |||||||||||||||
| 156 | 12 | my $depth = 0; | |||||||||||||
| 157 | |||||||||||||||
| 158 | my $upper_limit = min( | ||||||||||||||
| 159 | 12 | scalar( @{ $last_result->dir_components() } ), | |||||||||||||
| 160 | 12 | scalar( @{ $result->dir_components() } ), | |||||||||||||
| 161 | ); | ||||||||||||||
| 162 | |||||||||||||||
| 163 | FIND_I: | ||||||||||||||
| 164 | 12 | ▶ | while ( $depth < $upper_limit ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 165 | { | ||||||||||||||
| 166 | 4 | ▶ | if ( $last_result->dir_components()->[$depth] ne | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 167 | $result->dir_components()->[$depth] ) | ||||||||||||||
| 168 | { | ||||||||||||||
| 169 | 2 | last FIND_I; | |||||||||||||
| 170 | } | ||||||||||||||
| 171 | } | ||||||||||||||
| 172 | continue | ||||||||||||||
| 173 | { | ||||||||||||||
| 174 | 2 | $depth++; | |||||||||||||
| 175 | } | ||||||||||||||
| 176 | |||||||||||||||
| 177 | 12 | return $depth; | |||||||||||||
| 178 | } | ||||||||||||||
| 179 | |||||||||||||||
| 180 | BEGIN | ||||||||||||||
| 181 | { | ||||||||||||||
| 182 | 4 | ▶ | if ( os_is('Unix') ) | ||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=8.0
Branch: 1 branch
| |||||||||||||||
| 183 | { | ||||||||||||||
| 184 | *_my_getpwuid = sub { | ||||||||||||||
| 185 | 6 | ▶ | my $uid = shift; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 186 | 6 | return scalar( getpwuid($uid) ); | |||||||||||||
| 187 | 4 | }; | |||||||||||||
| 188 | *_my_getgrgid = sub { | ||||||||||||||
| 189 | 4 | ▶ | my $gid = shift; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 190 | 4 | return scalar( getgrgid($gid) ); | |||||||||||||
| 191 | 4 | }; | |||||||||||||
| 192 | } | ||||||||||||||
| 193 | else | ||||||||||||||
| 194 | { | ||||||||||||||
| 195 | 0 | ▶ | *_my_getpwuid = sub { return "unknown"; }; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 196 | 0 | ▶ | *_my_getgrgid = sub { return "unknown"; }; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 197 | } | ||||||||||||||
| 198 | } | ||||||||||||||
| 199 | |||||||||||||||
| 200 | sub _get_user_name | ||||||||||||||
| 201 | { | ||||||||||||||
| 202 | 12 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_get_user_name: called CC=2 SCAR=6.9
| |||||||||||||||
| 203 | 12 | my $uid = shift; | |||||||||||||
| 204 | |||||||||||||||
| 205 | 12 | ▶ | if ( !exists( $self->_user_cache()->{$uid} ) ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 206 | { | ||||||||||||||
| 207 | 4 | $self->_user_cache()->{$uid} = _my_getpwuid($uid); | |||||||||||||
| 208 | } | ||||||||||||||
| 209 | |||||||||||||||
| 210 | 12 | return $self->_user_cache()->{$uid}; | |||||||||||||
| 211 | } | ||||||||||||||
| 212 | |||||||||||||||
| 213 | sub _get_group_name | ||||||||||||||
| 214 | { | ||||||||||||||
| 215 | 12 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_get_group_name: called CC=2 SCAR=6.9
| |||||||||||||||
| 216 | 12 | my $gid = shift; | |||||||||||||
| 217 | |||||||||||||||
| 218 | 12 | ▶ | if ( !exists( $self->_group_cache()->{$gid} ) ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 219 | { | ||||||||||||||
| 220 | 4 | $self->_group_cache()->{$gid} = _my_getgrgid($gid); | |||||||||||||
| 221 | } | ||||||||||||||
| 222 | |||||||||||||||
| 223 | 12 | return $self->_group_cache()->{$gid}; | |||||||||||||
| 224 | } | ||||||||||||||
| 225 | |||||||||||||||
| 226 | sub _calc_file_digests_key | ||||||||||||||
| 227 | { | ||||||||||||||
| 228 | 6 | ▶ | my ( $self, $stat ) = @_; | ||||||||||||
|
Subroutine
_calc_file_digests_key: called CC=2 SCAR=6.9
| |||||||||||||||
| 229 | |||||||||||||||
| 230 | 6 | my $digests = $self->_digests; | |||||||||||||
| 231 | |||||||||||||||
| 232 | 6 | ▶ | if ( !defined $digests ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 233 | { | ||||||||||||||
| 234 | 2 | return []; | |||||||||||||
| 235 | } | ||||||||||||||
| 236 | 4 | my $result = $self->_result(); | |||||||||||||
| 237 | 4 | my $path = $result->path; | |||||||||||||
| 238 | my $ret = $self->_digest_cache->get_digests( | ||||||||||||||
| 239 | { | ||||||||||||||
| 240 | path => $result->full_components, | ||||||||||||||
| 241 | mtime => $stat->[9], | ||||||||||||||
| 242 | digests => $digests, | ||||||||||||||
| 243 | calc_cb => sub { | ||||||||||||||
| 244 | 4 | ▶ | my %ret; | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||
| 245 | 4 | foreach my $d (@$digests) | |||||||||||||
| 246 | { | ||||||||||||||
| 247 | 8 | my $o = Digest->new($d); | |||||||||||||
| 248 | 8 | open my $fh, '<', $path; | |||||||||||||
| 249 | 8 | binmode $fh; | |||||||||||||
| 250 | 8 | $o->addfile($fh); | |||||||||||||
| 251 | 8 | $ret{$d} = $o->hexdigest; | |||||||||||||
| 252 | 8 | close($fh); | |||||||||||||
| 253 | } | ||||||||||||||
| 254 | 4 | return \%ret; | |||||||||||||
| 255 | }, | ||||||||||||||
| 256 | } | ||||||||||||||
| 257 | 4 | ); | |||||||||||||
| 258 | 4 | return [ digests => $ret, ]; | |||||||||||||
| 259 | } | ||||||||||||||
| 260 | 4 | my $PERM_MASK = oct('07777'); | |||||||||||||
| 261 | |||||||||||||||
| 262 | sub _calc_file_or_dir_token | ||||||||||||||
| 263 | { | ||||||||||||||
| 264 | 12 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_calc_file_or_dir_token: called CC=3 SCAR=11.3
| |||||||||||||||
| 265 | |||||||||||||||
| 266 | 12 | my $result = $self->_result(); | |||||||||||||
| 267 | |||||||||||||||
| 268 | 12 | my @stat = stat( $result->path() ); | |||||||||||||
| 269 | |||||||||||||||
| 270 | 12 | ▶ | if ( not @stat ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 271 | { | ||||||||||||||
| 272 | 0 | Carp::confess( | |||||||||||||
| 273 | 0 | "Could not successfully stat <<@{[$result->path()]}>> - $!"); | |||||||||||||
| 274 | } | ||||||||||||||
| 275 | |||||||||||||||
| 276 | return { | ||||||||||||||
| 277 | filename => $result->full_components()->[-1], | ||||||||||||||
| 278 | 12 | ▶ | depth => scalar( @{ $result->full_components() } ), | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 279 | perms => sprintf( "%04o", ( $stat[2] & $PERM_MASK ) ), | ||||||||||||||
| 280 | mtime => strftime( "%Y-%m-%dT%H:%M:%S", localtime( $stat[9] ) ), | ||||||||||||||
| 281 | user => $self->_get_user_name( $stat[4] ), | ||||||||||||||
| 282 | group => $self->_get_group_name( $stat[5] ), | ||||||||||||||
| 283 | ( | ||||||||||||||
| 284 | $result->is_dir() | ||||||||||||||
| 285 | ? ( type => "dir", ) | ||||||||||||||
| 286 | : ( | ||||||||||||||
| 287 | type => "file", | ||||||||||||||
| 288 | size => $stat[7], | ||||||||||||||
| 289 | 12 | @{ $self->_calc_file_digests_key( \@stat ) }, | |||||||||||||
| 290 | ) | ||||||||||||||
| 291 | ), | ||||||||||||||
| 292 | }; | ||||||||||||||
| 293 | } | ||||||||||||||
| 294 | |||||||||||||||
| 295 | sub _populate_queue | ||||||||||||||
| 296 | { | ||||||||||||||
| 297 | 21 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_populate_queue: called CC=4 SCAR=13.9
| |||||||||||||||
| 298 | |||||||||||||||
| 299 | 21 | ▶ | if ( $self->_reached_end() ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 300 | { | ||||||||||||||
| 301 | 3 | return; | |||||||||||||
| 302 | } | ||||||||||||||
| 303 | |||||||||||||||
| 304 | 18 | $self->_result( $self->_file_find->next_obj() ); | |||||||||||||
| 305 | |||||||||||||||
| 306 | 18 | ▶ | if ( !$self->_last_result() ) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 307 | { | ||||||||||||||
| 308 | 4 | $self->_add( { type => "dir", depth => 0 } ); | |||||||||||||
| 309 | } | ||||||||||||||
| 310 | elsif ( !$self->_result() ) | ||||||||||||||
| 311 | { | ||||||||||||||
| 312 | 2 | $self->_up_to_level(-1); | |||||||||||||
| 313 | |||||||||||||||
| 314 | 2 | $self->_add( { type => "footer" } ); | |||||||||||||
| 315 | |||||||||||||||
| 316 | 2 | $self->_reached_end(1); | |||||||||||||
| 317 | } | ||||||||||||||
| 318 | else | ||||||||||||||
| 319 | { | ||||||||||||||
| 320 | 12 | $self->_up_to_level( $self->_find_new_common_depth() ); | |||||||||||||
| 321 | |||||||||||||||
| 322 | 12 | $self->_add( $self->_calc_file_or_dir_token() ); | |||||||||||||
| 323 | } | ||||||||||||||
| 324 | |||||||||||||||
| 325 | 18 | $self->_last_result( $self->_result() ); | |||||||||||||
| 326 | } | ||||||||||||||
| 327 | |||||||||||||||
| 328 | |||||||||||||||
| 329 | 4 | 1; # End of File::Dir::Dumper | |||||||||||||
| 330 | |||||||||||||||
| 331 | __END__ | ||||||||||||||