File-Scan-ClamAV
view release on metacpan or search on metacpan
lib/File/Scan/ClamAV.pm view on Meta::CPAN
# Return all viruses
use File::Scan::ClamAV;
my $av = new File::Scan::ClamAV(find_all => 1);
my %caught = $av->scan('/home/bob');
# Scan a file from command line:
perl -MFile::Scan::ClamAV -e 'printf("%s: %s\n", File::Scan::ClamAV->new->scan($ARGV[0]))' /home/bob/file.zip
# Preform a stream-scan on a scalar
use File::Scan::ClamAV;
if($ARGV[0] =~ /(.+)/){
my $file = $1;
if(-f $file){
my $data;
if(open(my $fh, $file)){
local $/;
$data = <$fh>;
close($fh);
} else {
die "Unable to read file: $file $!\n";
}
my $av = new File::Scan::ClamAV;
my ($code, $virus) = $av->streamscan($data);
if($code eq 'OK'){
print "The file: $file did not contain any virus known to ClamAV\n";
} elsif($code eq 'FOUND'){
print "The file: $file contained the virus: $virus\n";
} else {
print $av->errstr . "\n";
}
} else {
print "Unknown file: $file\n";
}
}
=back
=cut
sub new {
my $class = shift;
my (%options) = @_;
$options{port} ||= '/tmp/clamd';
$options{find_all} ||= 0;
$options{host} ||= 'localhost';
return bless \%options, $class;
}
=head2 ping()
Pings the clamd to check it is alive. Returns true if it is alive, false if it is dead. Note that it is still possible for a race condition to occur between your test for ping() and any call to scan(). See below for more details.
On error nothing is returned and the errstr() error handler is set.
=cut
sub ping {
my ($self) = @_;
my $conn = $self->_get_connection || return;
$self->_send($conn, "PING\n");
my $response = $conn->getline;
$response = q{} unless defined $response;
chomp $response ;
# Run out the buffer?
1 while (<$conn>);
$conn->close;
return ($response eq 'PONG' ? 1 : $self->_seterrstr("Unknown reponse from ClamAV service: $response"));
}
=head2 scan($dir_or_file)
Scan a directory or a file. Note that the resource must be readable by the user the ClamdAV clamd service is running as.
Returns a hash of C<< filename => virusname >> mappings.
On error nothing is returned and the errstr() error handler is set. If no virus is found nothing will be returned and the errstr() error handle won't be set.
=cut
sub scan {
my $self = shift;
$self->_seterrstr;
my @results;
if($self->{find_all}){
@results = $self->_scan('SCAN', @_);
} else {
@results = $self->_scan_shallow('SCAN', @_);
}
my %f;
for(@results){
$f{ $_->[0] } = $_->[1];
}
if(%f){
return %f;
} else {
return;
}
}
=head2 rawscan($dir_or_file)
This method has been deprecated - use scan() instead
( run in 3.687 seconds using v1.01-cache-2.11-cpan-14f38c9f855 )