| 1 | # | ||||||||||||||||||||||||||||||||
| 2 | # Scan.pm | ||||||||||||||||||||||||||||||||
| 3 | # Last Modification: Wed May 4 16:31:36 WEST 2005 | ||||||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||||||
| 5 | # Copyright (c) 2005 Henrique Dias <hdias@aesbuc.pt>. All rights reserved. | ||||||||||||||||||||||||||||||||
| 6 | # This module is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||
| 7 | # it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||||||
| 10 | package File::Scan; | ||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 1 | require 5; | |||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 1 | require Exporter; | |||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use File::Copy; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use SelfLoader; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use vars qw($VERSION @ISA @EXPORT $ERROR $SKIPPED $SUSPICIOUS $CALLBACK); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 1 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||
| 22 | 1 | $VERSION = '1.43'; | |||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 1 | ($ERROR, $SKIPPED, $SUSPICIOUS, $CALLBACK) = ("", 0, 0, ""); | |||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 1 | SelfLoader->load_stubs(); | |||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 29 | 0 | my $proto = shift; | |||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||||||
| 31 | 0 | my $self = { | |||||||||||||||||||||||||||||||
| 32 | extension => "", | ||||||||||||||||||||||||||||||||
| 33 | delete => 0, | ||||||||||||||||||||||||||||||||
| 34 | move => "", | ||||||||||||||||||||||||||||||||
| 35 | copy => "", | ||||||||||||||||||||||||||||||||
| 36 | mkdir => 0, | ||||||||||||||||||||||||||||||||
| 37 | max_txt_size => 5120, | ||||||||||||||||||||||||||||||||
| 38 | max_bin_size => 10240, | ||||||||||||||||||||||||||||||||
| 39 | @_, | ||||||||||||||||||||||||||||||||
| 40 | }; | ||||||||||||||||||||||||||||||||
| 41 | 0 | bless ($self, $class); | |||||||||||||||||||||||||||||||
| 42 | 0 | return($self); | |||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub scan { | ||||||||||||||||||||||||||||||
|
Subroutine
scan: not called CC=27 SCAR=66.3
Pod
documented
| |||||||||||||||||||||||||||||||||
| 46 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 47 | 0 | my $file = shift; | |||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 0 | &_set_error(); | |||||||||||||||||||||||||||||||
| 50 | 0 | &_set_skip(); | |||||||||||||||||||||||||||||||
| 51 | 0 | &_set_suspicious(); | |||||||||||||||||||||||||||||||
| 52 | 0 | &ret_callback(); | |||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | (-e $file) or return(&_set_error("No such file or directory: $file")); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 55 | 0 | my $fsize = -s $file; | |||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | $fsize or return(&_set_skip(2)); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 57 | 0 | my $res = ""; | |||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if(-f $file && -T $file) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-f $file and -T $file
MC/DC0%0/2
-f $file and -T $file
-f $file
-T $file
Truth table0%0/3
A and B
A= -f $fileB= -T $file
| |||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | return(&_set_skip(3)) if($fsize < 23); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 60 | return(&_set_skip(4)) | ||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if($self->{'max_txt_size'} && ($fsize > $self->{'max_txt_size'} * 1024)); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"max_txt_size"} and $fsize > $self->{"max_txt_size"} * 1024
MC/DC0%0/2
$self->{"max_txt_size"} and $fsize > $self->{"max_txt_size"} * 1024
$self->{"max_txt_size"}
$fsize > $self->{"max_txt_size"} * 1024
Truth table0%0/3
A and B
A= $self->{"max_txt_size"}B= $fsize > $self->{"max_txt_size"} * 1024
| |||||||||||||||||||||||||||||||||
| 62 | 0 | $res = &scan_text($self, $file); | |||||||||||||||||||||||||||||||
| 63 | } else { | ||||||||||||||||||||||||||||||||
| 64 | return(&_set_skip(5)) | ||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if($self->{'max_bin_size'} && ($fsize > $self->{'max_bin_size'} * 1024)); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"max_bin_size"} and $fsize > $self->{"max_bin_size"} * 1024
MC/DC0%0/2
$self->{"max_bin_size"} and $fsize > $self->{"max_bin_size"} * 1024
$self->{"max_bin_size"}
$fsize > $self->{"max_bin_size"} * 1024
Truth table0%0/3
A and B
A= $self->{"max_bin_size"}B= $fsize > $self->{"max_bin_size"} * 1024
| |||||||||||||||||||||||||||||||||
| 66 | 0 | $res = &scan_binary($self, $file); | |||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if($res) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if($self->{'extension'} && $file !~ /\.$self->{'extension'}$/o) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"extension"} and not $file =~ /\.$self->{"extension"}$/o
MC/DC0%0/2
$self->{"extension"} and not $file =~ /\.$self->{"extension"}$/o
$self->{"extension"}
not $file =~ /\.$self->{"extension"}$/o
Truth table0%0/3
A and not(B)
A= $self->{"extension"}B= not $file =~ /\.$self->{"extension"}$/o
| |||||||||||||||||||||||||||||||||
| 70 | 0 | my $newname = join("\.", $file, $self->{'extension'}); | |||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if(move($file, $newname)) { $file = $newname; } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 72 | 0 | else { &_set_error("Failed to move '$file' to '$newname'"); } | |||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if($self->{'copy'}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if(!(-d $self->{'copy'}) && $self->{'mkdir'}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not -d $self->{"copy"} and $self->{"mkdir"}
MC/DC0%0/2
not -d $self->{"copy"} and $self->{"mkdir"}
not -d $self->{"copy"}
$self->{"mkdir"}
Truth table0%0/3
not(A) and B
A= not -d $self->{"copy"}B= $self->{"mkdir"}
| |||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | mkdir($self->{'copy'}, $self->{'mkdir'}) or &_set_error(join("", "Failed to create directory '", $self->{'copy'}, "' $!")); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||
| 78 | 0 | my ($f) = ($file =~ /([^\/]+)$/o); | |||||||||||||||||||||||||||||||
| 79 | 0 | my $cpdir = join("/", $self->{'copy'}, $f); | |||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | copy($file, $cpdir) or &_set_error("Failed to copy '$file' to $cpdir"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | if($self->{'move'}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if(!(-d $self->{'move'}) && $self->{'mkdir'}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not -d $self->{"move"} and $self->{"mkdir"}
MC/DC0%0/2
not -d $self->{"move"} and $self->{"mkdir"}
not -d $self->{"move"}
$self->{"mkdir"}
Truth table0%0/3
not(A) and B
A= not -d $self->{"move"}B= $self->{"mkdir"}
| |||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | mkdir($self->{'move'}, $self->{'mkdir'}) or &_set_error(join("", "Failed to create directory '", $self->{'move'}, "' $!")); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||
| 86 | 0 | my ($f) = ($file =~ /([^\/]+)$/o); | |||||||||||||||||||||||||||||||
| 87 | 0 | my $mvfile = join("/", $self->{'move'}, $f); | |||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if(move($file, $mvfile)) { $file = $mvfile; } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 89 | 0 | else { &_set_error("Failed to move '$file' to '$mvfile'"); } | |||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | if($self->{'delete'}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if($file =~ /^(.+)$/s) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | unlink($1) or &_set_error("Could not delete $1: $!"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||
| 97 | 0 | return($res); | |||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub set_callback { | ||||||||||||||||||||||||||||||
|
Subroutine
set_callback: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 101 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | my $subref = shift || undef; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || undef
MC/DC0%0/1
shift() || undef
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | if(defined($subref) && ref($subref) eq "CODE") { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
defined $subref and ref $subref eq "CODE"
MC/DC0%0/2
defined $subref and ref $subref eq "CODE"
defined $subref
ref $subref eq "CODE"
Truth table0%0/3
A and B
A= defined $subrefB= ref $subref eq "CODE"
| |||||||||||||||||||||||||||||||||
| 105 | 0 | $self->{'callback'} = $subref; | |||||||||||||||||||||||||||||||
| 106 | } elsif(exists($self->{'callback'})) { | ||||||||||||||||||||||||||||||||
| 107 | 0 | delete($self->{'callback'}); | |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | 0 | return(); | |||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | sub _set_error { | ||||||||||||||||||||||||||||||
|
Subroutine
_set_error: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | $ERROR = shift || ""; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 114 | 0 | return(); | |||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub _set_skip { | ||||||||||||||||||||||||||||||
|
Subroutine
_set_skip: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | $SKIPPED = shift || 0; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 119 | 0 | return(); | |||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | sub _set_suspicious { | ||||||||||||||||||||||||||||||
|
Subroutine
_set_suspicious: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | $SUSPICIOUS = shift || 0; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 124 | 0 | return(); | |||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub ret_callback { | ||||||||||||||||||||||||||||||
|
Subroutine
ret_callback: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | $CALLBACK = shift || ""; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 129 | 0 | return(); | |||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub error { $ERROR; } | ||||||||||||||||||||||||||||||
|
Subroutine
error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | sub skipped { $SKIPPED; } | ||||||||||||||||||||||||||||||
|
Subroutine
skipped: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub suspicious { $SUSPICIOUS; } | ||||||||||||||||||||||||||||||
|
Subroutine
suspicious: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | sub callback { $CALLBACK; } | ||||||||||||||||||||||||||||||
|
Subroutine
callback: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | 1 | 1; | |||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | __DATA__ | ||||||||||||||||||||||||||||||||